1//===- LLVMOps.h - LLVM operation implementation helpers -------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef MLIR_LIB_DIALECT_LLVMIR_IR_LLVMOPS_H
10#define MLIR_LIB_DIALECT_LLVMIR_IR_LLVMOPS_H
11
12#include "mlir/Dialect/LLVMIR/LLVMDialectDecl.h"
13#include "mlir/IR/OpImplementation.h"
14
15#include <optional>
16
17namespace mlir::LLVM {
18class GEPOp;
19class IndirectBrOp;
20class SwitchOp;
21
22mlir::Type getI1SameShape(mlir::Type type);
23mlir::LLVM::LLVMStructType getValAndBoolStructType(mlir::Type valType);
24
25mlir::ParseResult parseCmpPredicate(mlir::OpAsmParser &parser,
26 mlir::LLVM::ICmpPredicateAttr &predicate);
27mlir::ParseResult parseCmpPredicate(mlir::OpAsmParser &parser,
28 mlir::LLVM::FCmpPredicateAttr &predicate);
29void printCmpPredicate(mlir::OpAsmPrinter &printer, mlir::Operation *,
30 mlir::LLVM::ICmpPredicateAttr predicate);
31void printCmpPredicate(mlir::OpAsmPrinter &printer, mlir::Operation *,
32 mlir::LLVM::FCmpPredicateAttr predicate);
33
34void printLLVMLinkage(mlir::OpAsmPrinter &p, mlir::Operation *,
35 mlir::LLVM::LinkageAttr val);
36mlir::ParseResult parseLLVMLinkage(mlir::OpAsmParser &p,
37 mlir::LLVM::LinkageAttr &val);
38
39void printOpBundles(mlir::OpAsmPrinter &p, mlir::Operation *op,
40 mlir::OperandRangeRange opBundleOperands,
41 mlir::TypeRangeRange opBundleOperandTypes,
42 std::optional<mlir::ArrayAttr> opBundleTags);
43std::optional<mlir::ParseResult> parseOpBundles(
44 mlir::OpAsmParser &p,
45 mlir::SmallVector<mlir::SmallVector<mlir::OpAsmParser::UnresolvedOperand>>
46 &opBundleOperands,
47 mlir::SmallVector<mlir::SmallVector<mlir::Type>> &opBundleOperandTypes,
48 mlir::ArrayAttr &opBundleTags);
49
50mlir::ParseResult parseSwitchOpCases(
51 mlir::OpAsmParser &parser, mlir::Type flagType,
52 mlir::DenseIntElementsAttr &caseValues,
53 mlir::SmallVectorImpl<mlir::Block *> &caseDestinations,
54 mlir::SmallVectorImpl<
55 mlir::SmallVector<mlir::OpAsmParser::UnresolvedOperand>> &caseOperands,
56 mlir::SmallVectorImpl<mlir::SmallVector<mlir::Type>> &caseOperandTypes);
57void printSwitchOpCases(mlir::OpAsmPrinter &p, mlir::LLVM::SwitchOp op,
58 mlir::Type flagType,
59 mlir::DenseIntElementsAttr caseValues,
60 mlir::SuccessorRange caseDestinations,
61 mlir::OperandRangeRange caseOperands,
62 const mlir::TypeRangeRange &caseOperandTypes);
63
64mlir::ParseResult parseGEPIndices(
65 mlir::OpAsmParser &parser,
66 mlir::SmallVectorImpl<mlir::OpAsmParser::UnresolvedOperand> &indices,
67 mlir::DenseI32ArrayAttr &rawConstantIndices);
68void printGEPIndices(mlir::OpAsmPrinter &printer, mlir::LLVM::GEPOp gepOp,
69 mlir::OperandRange indices,
70 mlir::DenseI32ArrayAttr rawConstantIndices);
71
72mlir::ParseResult parseInsertExtractValueElementType(
73 mlir::AsmParser &parser, mlir::Type &valueType, mlir::Type containerType,
74 mlir::DenseI64ArrayAttr position);
75void printInsertExtractValueElementType(mlir::AsmPrinter &printer,
76 mlir::Operation *op,
77 mlir::Type valueType,
78 mlir::Type containerType,
79 mlir::DenseI64ArrayAttr position);
80
81mlir::ParseResult parseShuffleType(mlir::AsmParser &parser, mlir::Type v1Type,
82 mlir::Type &resType,
83 mlir::DenseI32ArrayAttr mask);
84void printShuffleType(mlir::AsmPrinter &printer, mlir::Operation *op,
85 mlir::Type v1Type, mlir::Type resType,
86 mlir::DenseI32ArrayAttr mask);
87
88mlir::ParseResult parseIndirectBrOpSucessors(
89 mlir::OpAsmParser &parser, mlir::Type &flagType,
90 mlir::SmallVectorImpl<mlir::Block *> &succOperandBlocks,
91 mlir::SmallVectorImpl<
92 mlir::SmallVector<mlir::OpAsmParser::UnresolvedOperand>> &succOperands,
93 mlir::SmallVectorImpl<mlir::SmallVector<mlir::Type>> &succOperandsTypes);
94void printIndirectBrOpSucessors(mlir::OpAsmPrinter &p,
95 mlir::LLVM::IndirectBrOp op,
96 mlir::Type flagType, mlir::SuccessorRange succs,
97 mlir::OperandRangeRange succOperands,
98 const mlir::TypeRangeRange &succOperandsTypes);
99
100//===----------------------------------------------------------------------===//
101// Helpers shared by the op partitions
102//===----------------------------------------------------------------------===//
103
104/// Returns the attributes of `op` to print: its discardable attributes and
105/// all its inherent attributes.
106NamedAttrList getAttrsForPrinting(Operation *op);
107
108/// Returns the attributes of `op` to print: its discardable attributes and the
109/// inherent attributes `inherentAttrNames`.
110NamedAttrList getAttrsForPrinting(Operation *op,
111 ArrayRef<StringAttr> inherentAttrNames);
112
113/// Returns the elemental type of any LLVM-compatible vector type or self.
114Type extractVectorElementType(Type type);
115
116/// Conditions the deletion of the operation to the removal of all its uses.
117bool forwardToUsers(Operation *op,
118 SmallVectorImpl<OpOperand *> &newBlockingUses);
119
120// Parses one of the keywords provided in the list `keywords` and returns the
121// position of the parsed keyword in the list. If none of the keywords from the
122// list is parsed, returns -1.
123int parseOptionalKeywordAlternative(OpAsmParser &parser,
124 ArrayRef<StringRef> keywords);
125
126namespace detail {
127using cconv::getMaxEnumValForCConv;
128using linkage::getMaxEnumValForLinkage;
129using tailcallkind::getMaxEnumValForTailCallKind;
130
131template <typename Ty>
132struct EnumTraits {};
133
134#define REGISTER_ENUM_TYPE(Ty) \
135 template <> \
136 struct EnumTraits<Ty> { \
137 static StringRef stringify(Ty value) { return stringify##Ty(value); } \
138 static unsigned getMaxEnumVal() { return getMaxEnumValFor##Ty(); } \
139 }
140
141REGISTER_ENUM_TYPE(Linkage);
142REGISTER_ENUM_TYPE(UnnamedAddr);
143REGISTER_ENUM_TYPE(CConv);
144REGISTER_ENUM_TYPE(TailCallKind);
145REGISTER_ENUM_TYPE(ThreadLocalMode);
146REGISTER_ENUM_TYPE(Visibility);
147
148#undef REGISTER_ENUM_TYPE
149} // namespace detail
150
151/// Parse an enum from the keyword, or default to the provided default value.
152/// The return type is the enum type by default, unless overridden with the
153/// second template argument.
154template <typename EnumTy, typename RetTy = EnumTy>
155RetTy parseOptionalLLVMKeyword(OpAsmParser &parser, EnumTy defaultValue) {
156 SmallVector<StringRef, 10> names;
157 for (unsigned i = 0, e = detail::EnumTraits<EnumTy>::getMaxEnumVal(); i <= e;
158 ++i)
159 names.push_back(
160 Elt: detail::EnumTraits<EnumTy>::stringify(static_cast<EnumTy>(i)));
161
162 int index = parseOptionalKeywordAlternative(parser, keywords: names);
163 if (index == -1)
164 return static_cast<RetTy>(defaultValue);
165 return static_cast<RetTy>(index);
166}
167
168/// Verifies the operand bundles of `op`.
169template <typename OpType>
170LogicalResult verifyOperandBundles(OpType &op) {
171 OperandRangeRange opBundleOperands = op.getOpBundleOperands();
172 std::optional<ArrayAttr> opBundleTags = op.getOpBundleTags();
173
174 auto isStringAttr = [](Attribute tagAttr) {
175 return isa<StringAttr>(Val: tagAttr);
176 };
177 if (opBundleTags && !llvm::all_of(*opBundleTags, isStringAttr))
178 return op.emitError("operand bundle tag must be a StringAttr");
179
180 size_t numOpBundles = opBundleOperands.size();
181 size_t numOpBundleTags = opBundleTags ? opBundleTags->size() : 0;
182 if (numOpBundles != numOpBundleTags)
183 return op.emitError("expected ")
184 << numOpBundles << " operand bundle tags, but actually got "
185 << numOpBundleTags;
186
187 return success();
188}
189} // namespace mlir::LLVM
190
191#endif // MLIR_LIB_DIALECT_LLVMIR_IR_LLVMOPS_H
192