20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/TypeSwitch.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/Support/Casting.h"
32 #include "mlir/Dialect/IRDL/IR/IRDL.cpp.inc"
34 #include "mlir/Dialect/IRDL/IR/IRDLDialect.cpp.inc"
36 void IRDLDialect::initialize() {
39 #include "mlir/Dialect/IRDL/IR/IRDLOps.cpp.inc"
42 #define GET_TYPEDEF_LIST
43 #include "mlir/Dialect/IRDL/IR/IRDLTypesGen.cpp.inc"
46 #define GET_ATTRDEF_LIST
47 #include "mlir/Dialect/IRDL/IR/IRDLAttributes.cpp.inc"
59 if (regionParseRes.has_value() && failed(regionParseRes.value()))
77 return emitOpError(
"invalid dialect name");
82 size_t numVariadicities = getVariadicity().size();
83 size_t numOperands = getNumOperands();
85 if (numOperands != numVariadicities)
87 <<
"the number of operands and their variadicities must be "
89 << numOperands <<
" and " << numVariadicities <<
" respectively";
95 size_t numVariadicities = getVariadicity().size();
96 size_t numOperands = this->getNumOperands();
98 if (numOperands != numVariadicities)
100 <<
"the number of operands and their variadicities must be "
102 << numOperands <<
" and " << numVariadicities <<
" respectively";
108 size_t namesSize = getAttributeValueNames().size();
109 size_t valuesSize = getAttributeValues().size();
111 if (namesSize != valuesSize)
113 <<
"the number of attribute names and their constraints must be "
115 << namesSize <<
" and " << valuesSize <<
" respectively";
121 std::optional<StringRef> baseName = getBaseName();
122 std::optional<SymbolRefAttr> baseRef = getBaseRef();
123 if (baseName.has_value() == baseRef.has_value())
124 return emitOpError() <<
"the base type or attribute should be specified by "
125 "either a name or a reference";
128 (baseName->empty() || ((*baseName)[0] !=
'!' && (*baseName)[0] !=
'#')))
129 return emitOpError() <<
"the base type or attribute name should start with "
139 Operation *source, SymbolRefAttr symbol) {
144 return source->
emitOpError() <<
"symbol '" << symbol <<
"' not found";
146 if (!isa<TypeOp, AttributeOp>(targetOp))
147 return source->
emitOpError() <<
"symbol '" << symbol
148 <<
"' does not refer to a type or attribute "
149 "definition (refers to '"
150 << targetOp->
getName() <<
"')";
156 std::optional<SymbolRefAttr> baseRef = getBaseRef();
165 std::optional<SymbolRefAttr> baseRef = getBaseType();
179 VariadicityAttr &variadicityAttr) {
207 VariadicityArrayAttr &variadicityAttr) {
213 auto parseOne = [&] {
215 VariadicityAttr variadicity;
218 operands.push_back(operand);
219 variadicities.push_back(variadicity);
237 VariadicityArrayAttr variadicityAttr) {
239 interleaveComma(llvm::seq<int>(0, operands.size()), p, [&](
int i) {
240 Variadicity variadicity = variadicityAttr[i].getValue();
241 if (variadicity != Variadicity::single) {
242 p << stringifyVariadicity(variadicity) <<
" ";
252 ArrayAttr &attrNamesAttr) {
256 auto parseOperands = [&]() {
271 if (attrNames.empty())
274 interleaveComma(llvm::seq<int>(0, attrNames.size()), p,
275 [&](
int i) { p << attrNames[i] <<
" = " << attrArgs[i]; });
280 if (IntegerAttr numberOfBlocks = getNumberOfBlocksAttr())
281 if (int64_t number = numberOfBlocks.getInt(); number <= 0) {
282 return emitOpError(
"the number of blocks is expected to be >= 1 but got ")
288 #include "mlir/Dialect/IRDL/IR/IRDLInterfaces.cpp.inc"
290 #define GET_TYPEDEF_CLASSES
291 #include "mlir/Dialect/IRDL/IR/IRDLTypesGen.cpp.inc"
293 #include "mlir/Dialect/IRDL/IR/IRDLEnums.cpp.inc"
295 #define GET_ATTRDEF_CLASSES
296 #include "mlir/Dialect/IRDL/IR/IRDLAttributes.cpp.inc"
298 #define GET_OP_CLASSES
299 #include "mlir/Dialect/IRDL/IR/IRDLOps.cpp.inc"
static ParseResult parseValueWithVariadicity(OpAsmParser &p, OpAsmParser::UnresolvedOperand &operand, VariadicityAttr &variadicityAttr)
Parse a value with its variadicity first.
static void printAttributesOp(OpAsmPrinter &p, AttributesOp op, OperandRange attrArgs, ArrayAttr attrNames)
static LogicalResult checkSymbolIsTypeOrAttribute(SymbolTableCollection &symbolTable, Operation *source, SymbolRefAttr symbol)
Finds whether the provided symbol is an IRDL type or attribute definition.
static ParseResult parseValuesWithVariadicity(OpAsmParser &p, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &operands, VariadicityArrayAttr &variadicityAttr)
Parse a list of values with their variadicities first.
static ParseResult parseSingleBlockRegion(OpAsmParser &p, Region ®ion)
Parse a region, and add a single block if the region is empty.
static ParseResult parseAttributesOp(OpAsmParser &p, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &attrOperands, ArrayAttr &attrNamesAttr)
static void printSingleBlockRegion(OpAsmPrinter &p, Operation *op, Region ®ion)
static void printValuesWithVariadicity(OpAsmPrinter &p, Operation *op, OperandRange operands, VariadicityArrayAttr variadicityAttr)
Print a list of values with their variadicities first.
@ Paren
Parens surrounding zero or more operands.
virtual ParseResult parseCommaSeparatedList(Delimiter delimiter, function_ref< ParseResult()> parseElementFn, StringRef contextMessage=StringRef())=0
Parse a list of comma-separated items with an optional delimiter.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
virtual ParseResult parseOptionalKeyword(StringRef keyword)=0
Parse the given keyword if present.
virtual ParseResult parseRBrace()=0
Parse a } token.
virtual ParseResult parseEqual()=0
Parse a = token.
virtual ParseResult parseAttribute(Attribute &result, Type type={})=0
Parse an arbitrary attribute of a given type and return it in result.
virtual ParseResult parseOptionalLBrace()=0
Parse a { token if present.
Block represents an ordered list of Operations.
This class is a general helper class for creating context-global objects like types,...
MLIRContext * getContext() const
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
static bool isValidNamespace(StringRef str)
Utility function that returns if the given string is a valid dialect namespace.
MLIRContext is the top-level object for a collection of MLIR operations.
The OpAsmParser has methods for interacting with the asm parser: parsing things from it,...
virtual OptionalParseResult parseOptionalRegion(Region ®ion, ArrayRef< Argument > arguments={}, bool enableNameShadowing=false)=0
Parses a region if present.
virtual ParseResult parseOperand(UnresolvedOperand &result, bool allowResultNumber=true)=0
Parse a single SSA value operand name along with a result number if allowResultNumber is true.
This is a pure-virtual base class that exposes the asmprinter hooks necessary to implement a custom p...
virtual void printRegion(Region &blocks, bool printEntryBlockArgs=true, bool printBlockTerminators=true, bool printEmptyBlock=false)=0
Prints a region.
This class implements the operand iterators for the Operation class.
Operation is the basic unit of execution within MLIR.
OperationName getName()
The name of an operation is the key identifier for it.
InFlightDiagnostic emitOpError(const Twine &message={})
Emit an error with the op name prefixed, like "'dim' op " which is convenient for verifiers.
This class contains a list of basic blocks and a link to the parent operation it is attached to.
void push_back(Block *block)
BlockListType & getBlocks()
This class represents a collection of SymbolTables.
Operation * lookupSymbolNearDialect(SymbolTableCollection &symbolTable, Operation *source, SymbolRefAttr symbol)
Looks up a symbol from the symbol table containing the source operation's dialect definition operatio...
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
This is the representation of an operand reference.