20#include "llvm/ADT/STLExtras.h"
21#include "llvm/ADT/SetOperations.h"
22#include "llvm/ADT/StringExtras.h"
23#include "llvm/ADT/TypeSwitch.h"
24#include "llvm/Support/Casting.h"
33#include "mlir/Dialect/IRDL/IR/IRDL.cpp.inc"
35#include "mlir/Dialect/IRDL/IR/IRDLDialect.cpp.inc"
45 if (regionParseRes.has_value() && failed(regionParseRes.value()))
63 return loc->
emitError(
"name of ") << label <<
" is empty";
66 for (
const auto &elem : in) {
70 << label <<
" should not contain leading or double underscores";
74 return loc->
emitError(
"empty namespace not allowed");
79 <<
" must contain only lowercase letters, digits and "
82 if (llvm::isUpper(elem))
84 << label <<
" should not contain uppercase letters";
94LogicalResult DialectOp::verify() {
96 return emitOpError(
"invalid dialect name");
103LogicalResult OperationOp::verify() {
104 return isValidName(getSymName(), getOperation(),
"operation");
107LogicalResult TypeOp::verify() {
108 auto symName = getSymName();
109 if (!symName.empty() && symName.front() ==
'!')
110 symName = symName.substr(1);
111 return isValidName(symName, getOperation(),
"type");
114LogicalResult AttributeOp::verify() {
115 auto symName = getSymName();
116 if (!symName.empty() && symName.front() ==
'#')
117 symName = symName.substr(1);
118 return isValidName(symName, getOperation(),
"attribute");
121LogicalResult OperationOp::verifyRegions() {
126 auto insertNames = [&](StringRef kind,
ArrayAttr names) {
127 llvm::SmallDenseSet<StringRef> nameSet;
128 nameSet.reserve(names.size());
129 for (
auto name : names)
130 nameSet.insert(llvm::cast<StringAttr>(name).getValue());
131 valueNames.emplace_back(kind, std::move(nameSet));
134 for (
Operation &op : getBody().getOps()) {
136 .Case([&](OperandsOp op) { insertNames(
"operands", op.getNames()); })
137 .Case([&](ResultsOp op) { insertNames(
"results", op.getNames()); })
138 .Case([&](RegionsOp op) { insertNames(
"regions", op.getNames()); });
144 for (
size_t i : llvm::seq(valueNames.size())) {
145 for (
size_t j : llvm::seq(i + 1, valueNames.size())) {
146 auto [lhs, lhsSet] = valueNames[i];
147 auto &[rhs, rhsSet] = valueNames[
j];
148 llvm::set_intersect(lhsSet, rhsSet);
150 return emitOpError(
"contains a value named '")
151 << *lhsSet.begin() <<
"' for both its " << lhs <<
" and " << rhs;
160 if (numOperands != names.size())
162 <<
"the number of " << kindName
163 <<
"s and their names must be "
165 << numOperands <<
" and " << names.size() <<
" respectively";
168 for (
auto [i, name] : llvm::enumerate(names)) {
169 StringRef nameRef = llvm::cast<StringAttr>(name).getValue();
171 if (failed(
isValidName(nameRef, op, Twine(kindName) +
" #" + Twine(i))))
174 if (nameMap.contains(nameRef))
175 return op->
emitOpError() <<
"name of " << kindName <<
" #" << i
176 <<
" is a duplicate of the name of " << kindName
177 <<
" #" << nameMap[nameRef];
178 nameMap.insert({nameRef, i});
184LogicalResult ParametersOp::verify() {
185 return verifyNames(*
this,
"parameter", getNames(), getNumOperands());
188template <
typename ValueListOp>
190 StringRef kindName) {
191 size_t numVariadicities = op.getVariadicity().size();
192 size_t numOperands = op.getNumOperands();
194 if (numOperands != numVariadicities)
195 return op.emitOpError()
196 <<
"the number of " << kindName
197 <<
"s and their variadicities must be "
199 << numOperands <<
" and " << numVariadicities <<
" respectively";
201 return verifyNames(op, kindName, op.getNames(), numOperands);
204LogicalResult OperandsOp::verify() {
208LogicalResult ResultsOp::verify() {
212LogicalResult AttributesOp::verify() {
213 size_t namesSize = getAttributeValueNames().size();
214 size_t valuesSize = getAttributeValues().size();
216 if (namesSize != valuesSize)
218 <<
"the number of attribute names and their constraints must be "
220 << namesSize <<
" and " << valuesSize <<
" respectively";
225LogicalResult BaseOp::verify() {
226 std::optional<StringRef> baseName = getBaseName();
227 std::optional<SymbolRefAttr> baseRef = getBaseRef();
228 if (baseName.has_value() == baseRef.has_value())
229 return emitOpError() <<
"the base type or attribute should be specified by "
230 "either a name or a reference";
233 (baseName->empty() || ((*baseName)[0] !=
'!' && (*baseName)[0] !=
'#')))
234 return emitOpError() <<
"the base type or attribute name should start with "
244 Operation *source, SymbolRefAttr symbol) {
249 return source->
emitOpError() <<
"symbol '" << symbol <<
"' not found";
251 if (!isa<TypeOp, AttributeOp>(targetOp))
252 return source->
emitOpError() <<
"symbol '" << symbol
253 <<
"' does not refer to a type or attribute "
254 "definition (refers to '"
255 << targetOp->
getName() <<
"')";
261 std::optional<SymbolRefAttr> baseRef = getBaseRef();
270 std::optional<SymbolRefAttr> baseRef = getBaseType();
284 VariadicityAttr &variadicityAttr) {
289 variadicityAttr = VariadicityAttr::get(ctx, Variadicity::single);
291 variadicityAttr = VariadicityAttr::get(ctx, Variadicity::optional);
293 variadicityAttr = VariadicityAttr::get(ctx, Variadicity::variadic);
295 variadicityAttr = VariadicityAttr::get(ctx, Variadicity::single);
306 ArrayAttr &valueNamesAttr, VariadicityArrayAttr *variadicityAttr) {
313 auto parseOne = [&] {
316 VariadicityAttr variadicity;
320 if (variadicityAttr) {
323 variadicities.push_back(variadicity);
329 valueNames.push_back(StringAttr::get(ctx, name));
330 operands.push_back(operand);
336 valueNamesAttr = ArrayAttr::get(ctx, valueNames);
338 *variadicityAttr = VariadicityArrayAttr::get(ctx, variadicities);
363 ArrayAttr &valueNamesAttr, VariadicityArrayAttr &variadicityAttr) {
370 VariadicityArrayAttr variadicityAttr) {
372 interleaveComma(llvm::seq<int>(0, operands.size()), p, [&](
int i) {
373 p << llvm::cast<StringAttr>(valueNamesAttr[i]).getValue() <<
": ";
374 if (variadicityAttr) {
375 Variadicity variadicity = variadicityAttr[i].getValue();
376 if (variadicity != Variadicity::single) {
377 p << stringifyVariadicity(variadicity) <<
" ";
405 ArrayAttr valueNamesAttr, VariadicityArrayAttr variadicityAttr) {
416 auto parseOperands = [&]() {
431 if (attrNames.empty())
434 interleaveComma(llvm::seq<int>(0, attrNames.size()), p,
435 [&](
int i) { p << attrNames[i] <<
" = " << attrArgs[i]; });
439LogicalResult RegionOp::verify() {
440 if (IntegerAttr numberOfBlocks = getNumberOfBlocksAttr())
441 if (
int64_t number = numberOfBlocks.getInt(); number <= 0) {
442 return emitOpError(
"the number of blocks is expected to be >= 1 but got ")
448LogicalResult RegionsOp::verify() {
449 return verifyNames(*
this,
"region", getNames(), getNumOperands());
452#include "mlir/Dialect/IRDL/IR/IRDLInterfaces.cpp.inc"
454#define GET_TYPEDEF_CLASSES
455#include "mlir/Dialect/IRDL/IR/IRDLTypesGen.cpp.inc"
457#include "mlir/Dialect/IRDL/IR/IRDLEnums.cpp.inc"
459#define GET_ATTRDEF_CLASSES
460#include "mlir/Dialect/IRDL/IR/IRDLAttributes.cpp.inc"
462#define GET_OP_CLASSES
463#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 llvm::LogicalResult isValidName(llvm::StringRef in, mlir::Operation *loc, const Twine &label)
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)
@ Paren
Parens surrounding zero or more operands.
virtual Builder & getBuilder() const =0
Return a builder which provides useful access to MLIRContext, global objects like types and attribute...
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 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.
This class is a general helper class for creating context-global objects like types,...
ArrayAttr getArrayAttr(ArrayRef< Attribute > value)
MLIRContext * getContext() const
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.
InFlightDiagnostic emitError(const Twine &message={})
Emit an error about fatal conditions with this operation, reporting up to any diagnostic handlers tha...
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.
llvm::TypeSwitch< T, ResultT > TypeSwitch
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
This is the representation of an operand reference.
Eliminates variable at the specified position using Fourier-Motzkin variable elimination.