20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/SetOperations.h"
22 #include "llvm/ADT/SmallString.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/TypeSwitch.h"
25 #include "llvm/IR/Metadata.h"
26 #include "llvm/Support/Casting.h"
35 #include "mlir/Dialect/IRDL/IR/IRDL.cpp.inc"
37 #include "mlir/Dialect/IRDL/IR/IRDLDialect.cpp.inc"
39 void IRDLDialect::initialize() {
42 #include "mlir/Dialect/IRDL/IR/IRDLOps.cpp.inc"
45 #define GET_TYPEDEF_LIST
46 #include "mlir/Dialect/IRDL/IR/IRDLTypesGen.cpp.inc"
49 #define GET_ATTRDEF_LIST
50 #include "mlir/Dialect/IRDL/IR/IRDLAttributes.cpp.inc"
62 if (regionParseRes.has_value() && failed(regionParseRes.value()))
80 return emitOpError(
"invalid dialect name");
84 LogicalResult OperationOp::verifyRegions() {
89 auto insertNames = [&](StringRef
kind, ArrayAttr names) {
90 llvm::SmallDenseSet<StringRef> nameSet;
91 nameSet.reserve(names.size());
92 for (
auto name : names)
93 nameSet.insert(llvm::cast<StringAttr>(name).getValue());
94 valueNames.emplace_back(
kind, std::move(nameSet));
97 for (
Operation &op : getBody().getOps()) {
100 [&](OperandsOp op) { insertNames(
"operands", op.getNames()); })
102 [&](ResultsOp op) { insertNames(
"results", op.getNames()); })
104 [&](RegionsOp op) { insertNames(
"regions", op.getNames()); });
110 for (
size_t i : llvm::seq(valueNames.size())) {
111 for (
size_t j : llvm::seq(i + 1, valueNames.size())) {
112 auto [lhs, lhsSet] = valueNames[i];
113 auto &[rhs, rhsSet] = valueNames[
j];
114 llvm::set_intersect(lhsSet, rhsSet);
116 return emitOpError(
"contains a value named '")
117 << *lhsSet.begin() <<
"' for both its " << lhs <<
" and " << rhs;
125 ArrayAttr names,
size_t numOperands) {
126 if (numOperands != names.size())
128 <<
"the number of " << kindName
129 <<
"s and their names must be "
131 << numOperands <<
" and " << names.size() <<
" respectively";
135 StringRef nameRef = llvm::cast<StringAttr>(name).getValue();
138 <<
"name of " << kindName <<
" #" << i <<
" is empty";
139 if (!llvm::isAlpha(nameRef[0]) && nameRef[0] !=
'_')
141 <<
"name of " << kindName <<
" #" << i
142 <<
" must start with either a letter or an underscore";
143 if (llvm::any_of(nameRef,
144 [](
char c) {
return !llvm::isAlnum(c) && c !=
'_'; }))
146 <<
"name of " << kindName <<
" #" << i
147 <<
" must contain only letters, digits and underscores";
148 if (nameMap.contains(nameRef))
149 return op->
emitOpError() <<
"name of " << kindName <<
" #" << i
150 <<
" is a duplicate of the name of " << kindName
151 <<
" #" << nameMap[nameRef];
152 nameMap.insert({nameRef, i});
159 return verifyNames(*
this,
"parameter", getNames(), getNumOperands());
162 template <
typename ValueListOp>
164 StringRef kindName) {
165 size_t numVariadicities = op.getVariadicity().size();
166 size_t numOperands = op.getNumOperands();
168 if (numOperands != numVariadicities)
169 return op.emitOpError()
170 <<
"the number of " << kindName
171 <<
"s and their variadicities must be "
173 << numOperands <<
" and " << numVariadicities <<
" respectively";
175 return verifyNames(op, kindName, op.getNames(), numOperands);
187 size_t namesSize = getAttributeValueNames().size();
188 size_t valuesSize = getAttributeValues().size();
190 if (namesSize != valuesSize)
192 <<
"the number of attribute names and their constraints must be "
194 << namesSize <<
" and " << valuesSize <<
" respectively";
200 std::optional<StringRef> baseName = getBaseName();
201 std::optional<SymbolRefAttr> baseRef = getBaseRef();
202 if (baseName.has_value() == baseRef.has_value())
203 return emitOpError() <<
"the base type or attribute should be specified by "
204 "either a name or a reference";
207 (baseName->empty() || ((*baseName)[0] !=
'!' && (*baseName)[0] !=
'#')))
208 return emitOpError() <<
"the base type or attribute name should start with "
218 Operation *source, SymbolRefAttr symbol) {
223 return source->
emitOpError() <<
"symbol '" << symbol <<
"' not found";
225 if (!isa<TypeOp, AttributeOp>(targetOp))
226 return source->
emitOpError() <<
"symbol '" << symbol
227 <<
"' does not refer to a type or attribute "
228 "definition (refers to '"
229 << targetOp->
getName() <<
"')";
235 std::optional<SymbolRefAttr> baseRef = getBaseRef();
244 std::optional<SymbolRefAttr> baseRef = getBaseType();
258 VariadicityAttr &variadicityAttr) {
280 ArrayAttr &valueNamesAttr, VariadicityArrayAttr *variadicityAttr) {
287 auto parseOne = [&] {
290 VariadicityAttr variadicity;
294 if (variadicityAttr) {
297 variadicities.push_back(variadicity);
304 operands.push_back(operand);
324 ArrayAttr &valueNamesAttr) {
337 ArrayAttr &valueNamesAttr, VariadicityArrayAttr &variadicityAttr) {
343 ArrayAttr valueNamesAttr,
344 VariadicityArrayAttr variadicityAttr) {
346 interleaveComma(llvm::seq<int>(0, operands.size()), p, [&](
int i) {
347 p << llvm::cast<StringAttr>(valueNamesAttr[i]).getValue() <<
": ";
348 if (variadicityAttr) {
349 Variadicity variadicity = variadicityAttr[i].getValue();
350 if (variadicity != Variadicity::single) {
351 p << stringifyVariadicity(variadicity) <<
" ";
366 ArrayAttr valueNamesAttr) {
379 ArrayAttr valueNamesAttr, VariadicityArrayAttr variadicityAttr) {
386 ArrayAttr &attrNamesAttr) {
390 auto parseOperands = [&]() {
405 if (attrNames.empty())
408 interleaveComma(llvm::seq<int>(0, attrNames.size()), p,
409 [&](
int i) { p << attrNames[i] <<
" = " << attrArgs[i]; });
414 if (IntegerAttr numberOfBlocks = getNumberOfBlocksAttr())
415 if (int64_t number = numberOfBlocks.getInt(); number <= 0) {
416 return emitOpError(
"the number of blocks is expected to be >= 1 but got ")
423 return verifyNames(*
this,
"region", getNames(), getNumOperands());
426 #include "mlir/Dialect/IRDL/IR/IRDLInterfaces.cpp.inc"
428 #define GET_TYPEDEF_CLASSES
429 #include "mlir/Dialect/IRDL/IR/IRDLTypesGen.cpp.inc"
431 #include "mlir/Dialect/IRDL/IR/IRDLEnums.cpp.inc"
433 #define GET_ATTRDEF_CLASSES
434 #include "mlir/Dialect/IRDL/IR/IRDLAttributes.cpp.inc"
436 #define GET_OP_CLASSES
437 #include "mlir/Dialect/IRDL/IR/IRDLOps.cpp.inc"
static LogicalResult verifyNames(Operation *op, StringRef kindName, ArrayAttr names, size_t numOperands)
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 LogicalResult verifyOperandsResultsCommon(ValueListOp op, StringRef kindName)
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 printNamedValueList(OpAsmPrinter &p, Operation *op, OperandRange operands, ArrayAttr valueNamesAttr)
Print a list of named values.
static void printNamedValueListWithVariadicity(OpAsmPrinter &p, Operation *op, OperandRange operands, ArrayAttr valueNamesAttr, VariadicityArrayAttr variadicityAttr)
Print a list of named values with their variadicities first.
static ParseResult parseNamedValueList(OpAsmParser &p, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &operands, ArrayAttr &valueNamesAttr)
Parse a list of named values.
static ParseResult parseNamedValueListImpl(OpAsmParser &p, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &operands, ArrayAttr &valueNamesAttr, VariadicityArrayAttr *variadicityAttr)
static void printNamedValueListImpl(OpAsmPrinter &p, Operation *op, OperandRange operands, ArrayAttr valueNamesAttr, VariadicityArrayAttr variadicityAttr)
static ParseResult parseNamedValueListWithVariadicity(OpAsmParser &p, SmallVectorImpl< OpAsmParser::UnresolvedOperand > &operands, ArrayAttr &valueNamesAttr, VariadicityArrayAttr &variadicityAttr)
Parse a list of named values with their variadicities first.
static void printSingleBlockRegion(OpAsmPrinter &p, Operation *op, Region ®ion)
union mlir::linalg::@1195::ArityGroupAndKind::Kind kind
@ 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 parseColon()=0
Parse a : token.
ParseResult parseKeyword(StringRef keyword)
Parse a given keyword.
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.
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
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.
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.