MLIR 24.0.0git
LLVMOps.h
Go to the documentation of this file.
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
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);
30 mlir::LLVM::ICmpPredicateAttr predicate);
32 mlir::LLVM::FCmpPredicateAttr predicate);
33
35 mlir::LLVM::LinkageAttr val);
37 mlir::LLVM::LinkageAttr &val);
38
40 mlir::OperandRangeRange opBundleOperands,
41 mlir::TypeRangeRange opBundleOperandTypes,
42 std::optional<mlir::ArrayAttr> opBundleTags);
43std::optional<mlir::ParseResult> parseOpBundles(
46 &opBundleOperands,
48 mlir::ArrayAttr &opBundleTags);
49
50mlir::ParseResult parseSwitchOpCases(
51 mlir::OpAsmParser &parser, mlir::Type flagType,
57void printSwitchOpCases(mlir::OpAsmPrinter &p, mlir::LLVM::SwitchOp op,
58 mlir::Type flagType,
60 mlir::SuccessorRange caseDestinations,
61 mlir::OperandRangeRange caseOperands,
62 const mlir::TypeRangeRange &caseOperandTypes);
63
64mlir::ParseResult parseGEPIndices(
65 mlir::OpAsmParser &parser,
67 mlir::DenseI32ArrayAttr &rawConstantIndices);
68void printGEPIndices(mlir::OpAsmPrinter &printer, mlir::LLVM::GEPOp gepOp,
70 mlir::DenseI32ArrayAttr rawConstantIndices);
71
73 mlir::AsmParser &parser, mlir::Type &valueType, mlir::Type containerType,
77 mlir::Type valueType,
78 mlir::Type containerType,
80
81mlir::ParseResult parseShuffleType(mlir::AsmParser &parser, mlir::Type v1Type,
82 mlir::Type &resType,
85 mlir::Type v1Type, mlir::Type resType,
87
88mlir::ParseResult parseIndirectBrOpSucessors(
89 mlir::OpAsmParser &parser, mlir::Type &flagType,
90 mlir::SmallVectorImpl<mlir::Block *> &succOperandBlocks,
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.
107
108/// Returns the attributes of `op` to print: its discardable attributes and the
109/// inherent attributes `inherentAttrNames`.
111 ArrayRef<StringAttr> inherentAttrNames);
112
113/// Returns the elemental type of any LLVM-compatible vector type or self.
115
116/// Conditions the deletion of the operation to the removal of all its uses.
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.
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
144REGISTER_ENUM_TYPE(TailCallKind);
145REGISTER_ENUM_TYPE(ThreadLocalMode);
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) {
157 for (unsigned i = 0, e = detail::EnumTraits<EnumTy>::getMaxEnumVal(); i <= e;
158 ++i)
159 names.push_back(
160 detail::EnumTraits<EnumTy>::stringify(static_cast<EnumTy>(i)));
161
162 int index = parseOptionalKeywordAlternative(parser, 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>(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
return success()
#define REGISTER_ENUM_TYPE(Ty)
Definition LLVMOps.h:134
This base class exposes generic asm parser hooks, usable across the various derived parsers.
This base class exposes generic asm printer hooks, usable across the various derived printers.
Attributes are known-constant values of operations.
Definition Attributes.h:25
An attribute that represents a reference to a dense integer vector or tensor object.
NamedAttrList is array of NamedAttributes that tracks whether it is sorted and does some basic work t...
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
This class represents a contiguous range of operand ranges, e.g.
Definition ValueRange.h:85
This class implements the operand iterators for the Operation class.
Definition ValueRange.h:44
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
This class implements the successor iterators for Block.
This class provides an abstraction for a range of TypeRange.
Definition TypeRange.h:107
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
mlir::ParseResult parseCmpPredicate(mlir::OpAsmParser &parser, mlir::LLVM::ICmpPredicateAttr &predicate)
NamedAttrList getAttrsForPrinting(Operation *op)
Returns the attributes of op to print: its discardable attributes and all its inherent attributes.
void printCmpPredicate(mlir::OpAsmPrinter &printer, mlir::Operation *, mlir::LLVM::ICmpPredicateAttr predicate)
Type extractVectorElementType(Type type)
Returns the elemental type of any LLVM-compatible vector type or self.
mlir::ParseResult parseInsertExtractValueElementType(mlir::AsmParser &parser, mlir::Type &valueType, mlir::Type containerType, mlir::DenseI64ArrayAttr position)
Infer the value type from the container type and position.
void printLLVMLinkage(mlir::OpAsmPrinter &p, mlir::Operation *, mlir::LLVM::LinkageAttr val)
void printOpBundles(mlir::OpAsmPrinter &p, mlir::Operation *op, mlir::OperandRangeRange opBundleOperands, mlir::TypeRangeRange opBundleOperandTypes, std::optional< mlir::ArrayAttr > opBundleTags)
RetTy parseOptionalLLVMKeyword(OpAsmParser &parser, EnumTy defaultValue)
Parse an enum from the keyword, or default to the provided default value.
Definition LLVMOps.h:155
LogicalResult verifyOperandBundles(OpType &op)
Verifies the operand bundles of op.
Definition LLVMOps.h:170
bool forwardToUsers(Operation *op, SmallVectorImpl< OpOperand * > &newBlockingUses)
Conditions the deletion of the operation to the removal of all its uses.
mlir::ParseResult parseShuffleType(mlir::AsmParser &parser, mlir::Type v1Type, mlir::Type &resType, mlir::DenseI32ArrayAttr mask)
Build the result type of a shuffle vector operation.
void printShuffleType(mlir::AsmPrinter &printer, mlir::Operation *op, mlir::Type v1Type, mlir::Type resType, mlir::DenseI32ArrayAttr mask)
Nothing to do when the result type is inferred.
mlir::Type getI1SameShape(mlir::Type type)
Returns a boolean type that has the same shape as type.
void printSwitchOpCases(mlir::OpAsmPrinter &p, mlir::LLVM::SwitchOp op, mlir::Type flagType, mlir::DenseIntElementsAttr caseValues, mlir::SuccessorRange caseDestinations, mlir::OperandRangeRange caseOperands, const mlir::TypeRangeRange &caseOperandTypes)
void printIndirectBrOpSucessors(mlir::OpAsmPrinter &p, mlir::LLVM::IndirectBrOp op, mlir::Type flagType, mlir::SuccessorRange succs, mlir::OperandRangeRange succOperands, const mlir::TypeRangeRange &succOperandsTypes)
mlir::ParseResult parseIndirectBrOpSucessors(mlir::OpAsmParser &parser, mlir::Type &flagType, mlir::SmallVectorImpl< mlir::Block * > &succOperandBlocks, mlir::SmallVectorImpl< mlir::SmallVector< mlir::OpAsmParser::UnresolvedOperand > > &succOperands, mlir::SmallVectorImpl< mlir::SmallVector< mlir::Type > > &succOperandsTypes)
mlir::ParseResult parseLLVMLinkage(mlir::OpAsmParser &p, mlir::LLVM::LinkageAttr &val)
mlir::LLVM::LLVMStructType getValAndBoolStructType(mlir::Type valType)
Returns an LLVM struct type that contains a value type and a boolean type.
int parseOptionalKeywordAlternative(OpAsmParser &parser, ArrayRef< StringRef > keywords)
std::optional< mlir::ParseResult > parseOpBundles(mlir::OpAsmParser &p, mlir::SmallVector< mlir::SmallVector< mlir::OpAsmParser::UnresolvedOperand > > &opBundleOperands, mlir::SmallVector< mlir::SmallVector< mlir::Type > > &opBundleOperandTypes, mlir::ArrayAttr &opBundleTags)
mlir::ParseResult parseGEPIndices(mlir::OpAsmParser &parser, mlir::SmallVectorImpl< mlir::OpAsmParser::UnresolvedOperand > &indices, mlir::DenseI32ArrayAttr &rawConstantIndices)
void printGEPIndices(mlir::OpAsmPrinter &printer, mlir::LLVM::GEPOp gepOp, mlir::OperandRange indices, mlir::DenseI32ArrayAttr rawConstantIndices)
void printInsertExtractValueElementType(mlir::AsmPrinter &printer, mlir::Operation *op, mlir::Type valueType, mlir::Type containerType, mlir::DenseI64ArrayAttr position)
Nothing to print for an inferred type.
mlir::ParseResult parseSwitchOpCases(mlir::OpAsmParser &parser, mlir::Type flagType, mlir::DenseIntElementsAttr &caseValues, mlir::SmallVectorImpl< mlir::Block * > &caseDestinations, mlir::SmallVectorImpl< mlir::SmallVector< mlir::OpAsmParser::UnresolvedOperand > > &caseOperands, mlir::SmallVectorImpl< mlir::SmallVector< mlir::Type > > &caseOperandTypes)
<cases> ::= [ (case (, case )* )?
detail::DenseArrayAttrImpl< int64_t > DenseI64ArrayAttr
detail::DenseArrayAttrImpl< int32_t > DenseI32ArrayAttr