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 "
136 std::optional<SymbolRefAttr> baseRef = getBaseRef();
149 return emitOpError() <<
"'" << *baseRef
150 <<
"' does not refer to a type or attribute definition";
160 VariadicityAttr &variadicityAttr) {
188 VariadicityArrayAttr &variadicityAttr) {
194 auto parseOne = [&] {
196 VariadicityAttr variadicity;
199 operands.push_back(operand);
200 variadicities.push_back(variadicity);
218 VariadicityArrayAttr variadicityAttr) {
220 interleaveComma(llvm::seq<int>(0, operands.size()), p, [&](
int i) {
221 Variadicity variadicity = variadicityAttr[i].getValue();
222 if (variadicity != Variadicity::single) {
223 p << stringifyVariadicity(variadicity) <<
" ";
233 ArrayAttr &attrNamesAttr) {
237 auto parseOperands = [&]() {
252 if (attrNames.empty())
255 interleaveComma(llvm::seq<int>(0, attrNames.size()), p,
256 [&](
int i) { p << attrNames[i] <<
" = " << attrArgs[i]; });
261 if (IntegerAttr numberOfBlocks = getNumberOfBlocksAttr())
262 if (int64_t number = numberOfBlocks.getInt(); number <= 0) {
263 return emitOpError(
"the number of blocks is expected to be >= 1 but got ")
269 #include "mlir/Dialect/IRDL/IR/IRDLInterfaces.cpp.inc"
271 #define GET_TYPEDEF_CLASSES
272 #include "mlir/Dialect/IRDL/IR/IRDLTypesGen.cpp.inc"
274 #include "mlir/Dialect/IRDL/IR/IRDLEnums.cpp.inc"
276 #define GET_ATTRDEF_CLASSES
277 #include "mlir/Dialect/IRDL/IR/IRDLAttributes.cpp.inc"
279 #define GET_OP_CLASSES
280 #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 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.
This class represents success/failure for parsing-like operations that find it important to chain tog...
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 * lookupNearestSymbolFrom(Operation *from, StringAttr symbol)
Returns the operation registered with the given symbol name within the closest parent operation of,...
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
bool succeeded(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a success value.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
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,...
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
This class represents an efficient way to signal success or failure.
bool succeeded() const
Returns true if the provided LogicalResult corresponds to a success value.
This is the representation of an operand reference.