1//===- IRDL.cpp - IRDL dialect ----------------------------------*- C++ -*-===//
2//
3// This file is licensed 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#include "mlir/Dialect/IRDL/IR/IRDL.h"
10#include "mlir/Dialect/IRDL/IRDLSymbols.h"
11#include "mlir/IR/Builders.h"
12#include "mlir/IR/BuiltinAttributes.h"
13#include "mlir/IR/Diagnostics.h"
14#include "mlir/IR/DialectImplementation.h"
15#include "mlir/IR/ExtensibleDialect.h"
16#include "mlir/IR/OpDefinition.h"
17#include "mlir/IR/OpImplementation.h"
18#include "mlir/IR/Operation.h"
19#include "mlir/Support/LLVM.h"
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"
25
26using namespace mlir;
27using namespace mlir::irdl;
28
29//===----------------------------------------------------------------------===//
30// IRDL dialect.
31//===----------------------------------------------------------------------===//
32
33#include "mlir/Dialect/IRDL/IR/IRDL.cpp.inc"
34
35#include "mlir/Dialect/IRDL/IR/IRDLDialect.cpp.inc"
36
37void IRDLDialect::initialize() {
38 addOperations<
39#define GET_OP_LIST
40#include "mlir/Dialect/IRDL/IR/IRDLOps.cpp.inc"
41 >();
42 addTypes<
43#define GET_TYPEDEF_LIST
44#include "mlir/Dialect/IRDL/IR/IRDLTypesGen.cpp.inc"
45 >();
46 addAttributes<
47#define GET_ATTRDEF_LIST
48#include "mlir/Dialect/IRDL/IR/IRDLAttributes.cpp.inc"
49 >();
50}
51
52//===----------------------------------------------------------------------===//
53// Parsing/Printing/Verifying
54//===----------------------------------------------------------------------===//
55
56/// Parse a region, and add a single block if the region is empty.
57/// If no region is parsed, create a new region with a single empty block.
58static ParseResult parseSingleBlockRegion(OpAsmParser &p, Region &region) {
59 auto regionParseRes = p.parseOptionalRegion(region);
60 if (regionParseRes.has_value() && failed(Result: regionParseRes.value()))
61 return failure();
62
63 // If the region is empty, add a single empty block.
64 if (region.empty())
65 region.push_back(block: new Block());
66
67 return success();
68}
69
70static void printSingleBlockRegion(OpAsmPrinter &p, Operation *op,
71 Region &region) {
72 if (!region.getBlocks().front().empty())
73 p.printRegion(blocks&: region);
74}
75static llvm::LogicalResult isValidName(llvm::StringRef in, mlir::Operation *loc,
76 const Twine &label) {
77 if (in.empty())
78 return loc->emitError(message: "name of ") << label << " is empty";
79
80 char prev = '_'; // treat the initial as _ to eliminate leading underscores
81 for (const auto &elem : in) {
82 if (elem == '_') {
83 if (prev == '_')
84 return loc->emitError(message: "name of ")
85 << label << " should not contain leading or double underscores";
86 } else {
87 if (elem == '.') {
88 if (prev == '.')
89 return loc->emitError(message: "empty namespace not allowed");
90 } else {
91 if (!isalnum(elem))
92 return loc->emitError(message: "name of ")
93 << label
94 << " must contain only lowercase letters, digits and "
95 "underscores";
96
97 if (llvm::isUpper(C: elem))
98 return loc->emitError(message: "name of ")
99 << label << " should not contain uppercase letters";
100 }
101 }
102
103 prev = elem;
104 }
105
106 return success();
107}
108
109LogicalResult DialectOp::verify() {
110 if (!Dialect::isValidNamespace(str: getName()))
111 return emitOpError(message: "invalid dialect name");
112 if (failed(Result: isValidName(in: getSymName(), loc: getOperation(), label: "dialect")))
113 return failure();
114
115 return success();
116}
117
118LogicalResult OperationOp::verify() {
119 return isValidName(in: getSymName(), loc: getOperation(), label: "operation");
120}
121
122LogicalResult TypeOp::verify() {
123 auto symName = getSymName();
124 if (!symName.empty() && symName.front() == '!')
125 symName = symName.substr(Start: 1);
126 return isValidName(in: symName, loc: getOperation(), label: "type");
127}
128
129LogicalResult AttributeOp::verify() {
130 auto symName = getSymName();
131 if (!symName.empty() && symName.front() == '#')
132 symName = symName.substr(Start: 1);
133 return isValidName(in: symName, loc: getOperation(), label: "attribute");
134}
135
136LogicalResult OperationOp::verifyRegions() {
137 // Stores pairs of value kinds and the list of names of values of this kind in
138 // the operation.
139 SmallVector<std::tuple<StringRef, llvm::SmallDenseSet<StringRef>>> valueNames;
140
141 auto insertNames = [&](StringRef kind, ArrayAttr names) {
142 llvm::SmallDenseSet<StringRef> nameSet;
143 nameSet.reserve(Size: names.size());
144 for (auto name : names)
145 nameSet.insert(V: llvm::cast<StringAttr>(Val&: name).getValue());
146 valueNames.emplace_back(Args&: kind, Args: std::move(nameSet));
147 };
148
149 for (Operation &op : getBody().getOps()) {
150 TypeSwitch<Operation *>(&op)
151 .Case(caseFn: [&](OperandsOp op) { insertNames("operands", op.getNames()); })
152 .Case(caseFn: [&](ResultsOp op) { insertNames("results", op.getNames()); })
153 .Case(caseFn: [&](RegionsOp op) { insertNames("regions", op.getNames()); });
154 }
155
156 // Verify that no two operand, result or region share the same name.
157 // The absence of duplicates within each value kind is checked by the
158 // associated operation's verifier.
159 for (size_t i : llvm::seq(Size: valueNames.size())) {
160 for (size_t j : llvm::seq(Begin: i + 1, End: valueNames.size())) {
161 auto [lhs, lhsSet] = valueNames[i];
162 auto &[rhs, rhsSet] = valueNames[j];
163 llvm::set_intersect(S1&: lhsSet, S2: rhsSet);
164 if (!lhsSet.empty())
165 return emitOpError(message: "contains a value named '")
166 << *lhsSet.begin() << "' for both its " << lhs << " and " << rhs;
167 }
168 }
169
170 return success();
171}
172
173static LogicalResult verifyNames(Operation *op, StringRef kindName,
174 ArrayAttr names, size_t numOperands) {
175 if (numOperands != names.size())
176 return op->emitOpError()
177 << "the number of " << kindName
178 << "s and their names must be "
179 "the same, but got "
180 << numOperands << " and " << names.size() << " respectively";
181
182 DenseMap<StringRef, size_t> nameMap;
183 for (auto [i, name] : llvm::enumerate(First&: names)) {
184 StringRef nameRef = llvm::cast<StringAttr>(Val: name).getValue();
185
186 if (failed(Result: isValidName(in: nameRef, loc: op, label: Twine(kindName) + " #" + Twine(i))))
187 return failure();
188
189 if (nameMap.contains(Val: nameRef))
190 return op->emitOpError() << "name of " << kindName << " #" << i
191 << " is a duplicate of the name of " << kindName
192 << " #" << nameMap[nameRef];
193 nameMap.insert(KV: {nameRef, i});
194 }
195
196 return success();
197}
198
199LogicalResult ParametersOp::verify() {
200 return verifyNames(op: *this, kindName: "parameter", names: getNames(), numOperands: getNumOperands());
201}
202
203template <typename ValueListOp>
204static LogicalResult verifyOperandsResultsCommon(ValueListOp op,
205 StringRef kindName) {
206 size_t numVariadicities = op.getVariadicity().size();
207 size_t numOperands = op.getNumOperands();
208
209 if (numOperands != numVariadicities)
210 return op.emitOpError()
211 << "the number of " << kindName
212 << "s and their variadicities must be "
213 "the same, but got "
214 << numOperands << " and " << numVariadicities << " respectively";
215
216 return verifyNames(op, kindName, op.getNames(), numOperands);
217}
218
219LogicalResult OperandsOp::verify() {
220 return verifyOperandsResultsCommon(op: *this, kindName: "operand");
221}
222
223LogicalResult ResultsOp::verify() {
224 return verifyOperandsResultsCommon(op: *this, kindName: "result");
225}
226
227LogicalResult AttributesOp::verify() {
228 size_t namesSize = getAttributeValueNames().size();
229 size_t valuesSize = getAttributeValues().size();
230
231 if (namesSize != valuesSize)
232 return emitOpError()
233 << "the number of attribute names and their constraints must be "
234 "the same but got "
235 << namesSize << " and " << valuesSize << " respectively";
236
237 return success();
238}
239
240LogicalResult BaseOp::verify() {
241 std::optional<StringRef> baseName = getBaseName();
242 std::optional<SymbolRefAttr> baseRef = getBaseRef();
243 if (baseName.has_value() == baseRef.has_value())
244 return emitOpError() << "the base type or attribute should be specified by "
245 "either a name or a reference";
246
247 if (baseName &&
248 (baseName->empty() || ((*baseName)[0] != '!' && (*baseName)[0] != '#')))
249 return emitOpError() << "the base type or attribute name should start with "
250 "'!' or '#'";
251
252 return success();
253}
254
255/// Finds whether the provided symbol is an IRDL type or attribute definition.
256/// The source operation must be within a DialectOp.
257static LogicalResult
258checkSymbolIsTypeOrAttribute(SymbolTableCollection &symbolTable,
259 Operation *source, SymbolRefAttr symbol) {
260 Operation *targetOp =
261 irdl::lookupSymbolNearDialect(symbolTable, source, symbol);
262
263 if (!targetOp)
264 return source->emitOpError() << "symbol '" << symbol << "' not found";
265
266 if (!isa<TypeOp, AttributeOp>(Val: targetOp))
267 return source->emitOpError() << "symbol '" << symbol
268 << "' does not refer to a type or attribute "
269 "definition (refers to '"
270 << targetOp->getName() << "')";
271
272 return success();
273}
274
275LogicalResult BaseOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
276 std::optional<SymbolRefAttr> baseRef = getBaseRef();
277 if (!baseRef)
278 return success();
279
280 return checkSymbolIsTypeOrAttribute(symbolTable, source: *this, symbol: *baseRef);
281}
282
283LogicalResult
284ParametricOp::verifySymbolUses(SymbolTableCollection &symbolTable) {
285 std::optional<SymbolRefAttr> baseRef = getBaseType();
286 if (!baseRef)
287 return success();
288
289 return checkSymbolIsTypeOrAttribute(symbolTable, source: *this, symbol: *baseRef);
290}
291
292/// Parse a value with its variadicity first. By default, the variadicity is
293/// single.
294///
295/// value-with-variadicity ::= ("single" | "optional" | "variadic")? ssa-value
296static ParseResult
297parseValueWithVariadicity(OpAsmParser &p,
298 OpAsmParser::UnresolvedOperand &operand,
299 VariadicityAttr &variadicityAttr) {
300 MLIRContext *ctx = p.getBuilder().getContext();
301
302 // Parse the variadicity, if present
303 if (p.parseOptionalKeyword(keyword: "single").succeeded()) {
304 variadicityAttr = VariadicityAttr::get(context: ctx, value: Variadicity::single);
305 } else if (p.parseOptionalKeyword(keyword: "optional").succeeded()) {
306 variadicityAttr = VariadicityAttr::get(context: ctx, value: Variadicity::optional);
307 } else if (p.parseOptionalKeyword(keyword: "variadic").succeeded()) {
308 variadicityAttr = VariadicityAttr::get(context: ctx, value: Variadicity::variadic);
309 } else {
310 variadicityAttr = VariadicityAttr::get(context: ctx, value: Variadicity::single);
311 }
312
313 // Parse the value
314 if (p.parseOperand(result&: operand))
315 return failure();
316 return success();
317}
318
319static ParseResult parseNamedValueListImpl(
320 OpAsmParser &p, SmallVectorImpl<OpAsmParser::UnresolvedOperand> &operands,
321 ArrayAttr &valueNamesAttr, VariadicityArrayAttr *variadicityAttr) {
322 Builder &builder = p.getBuilder();
323 MLIRContext *ctx = builder.getContext();
324 SmallVector<Attribute> valueNames;
325 SmallVector<VariadicityAttr> variadicities;
326
327 // Parse a single value with its variadicity
328 auto parseOne = [&] {
329 StringRef name;
330 OpAsmParser::UnresolvedOperand operand;
331 VariadicityAttr variadicity;
332 if (p.parseKeyword(keyword: &name) || p.parseColon())
333 return failure();
334
335 if (variadicityAttr) {
336 if (parseValueWithVariadicity(p, operand, variadicityAttr&: variadicity))
337 return failure();
338 variadicities.push_back(Elt: variadicity);
339 } else {
340 if (p.parseOperand(result&: operand))
341 return failure();
342 }
343
344 valueNames.push_back(Elt: StringAttr::get(context: ctx, bytes: name));
345 operands.push_back(Elt: operand);
346 return success();
347 };
348
349 if (p.parseCommaSeparatedList(delimiter: OpAsmParser::Delimiter::Paren, parseElementFn: parseOne))
350 return failure();
351 valueNamesAttr = ArrayAttr::get(context: ctx, value: valueNames);
352 if (variadicityAttr)
353 *variadicityAttr = VariadicityArrayAttr::get(context: ctx, value: variadicities);
354 return success();
355}
356
357/// Parse a list of named values.
358///
359/// values ::=
360/// `(` (named-value (`,` named-value)*)? `)`
361/// named-value := bare-id `:` ssa-value
362static ParseResult
363parseNamedValueList(OpAsmParser &p,
364 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &operands,
365 ArrayAttr &valueNamesAttr) {
366 return parseNamedValueListImpl(p, operands, valueNamesAttr, variadicityAttr: nullptr);
367}
368
369/// Parse a list of named values with their variadicities first. By default, the
370/// variadicity is single.
371///
372/// values-with-variadicity ::=
373/// `(` (value-with-variadicity (`,` value-with-variadicity)*)? `)`
374/// value-with-variadicity
375/// ::= bare-id `:` ("single" | "optional" | "variadic")? ssa-value
376static ParseResult parseNamedValueListWithVariadicity(
377 OpAsmParser &p, SmallVectorImpl<OpAsmParser::UnresolvedOperand> &operands,
378 ArrayAttr &valueNamesAttr, VariadicityArrayAttr &variadicityAttr) {
379 return parseNamedValueListImpl(p, operands, valueNamesAttr, variadicityAttr: &variadicityAttr);
380}
381
382static void printNamedValueListImpl(OpAsmPrinter &p, Operation *op,
383 OperandRange operands,
384 ArrayAttr valueNamesAttr,
385 VariadicityArrayAttr variadicityAttr) {
386 p << "(";
387 interleaveComma(c: llvm::seq<int>(Begin: 0, End: operands.size()), os&: p, each_fn: [&](int i) {
388 p << llvm::cast<StringAttr>(Val: valueNamesAttr[i]).getValue() << ": ";
389 if (variadicityAttr) {
390 Variadicity variadicity = variadicityAttr[i].getValue();
391 if (variadicity != Variadicity::single) {
392 p << stringifyVariadicity(variadicity) << " ";
393 }
394 }
395 p << operands[i];
396 });
397 p << ")";
398}
399
400/// Print a list of named values.
401///
402/// values ::=
403/// `(` (named-value (`,` named-value)*)? `)`
404/// named-value := bare-id `:` ssa-value
405static void printNamedValueList(OpAsmPrinter &p, Operation *op,
406 OperandRange operands,
407 ArrayAttr valueNamesAttr) {
408 printNamedValueListImpl(p, op, operands, valueNamesAttr, variadicityAttr: nullptr);
409}
410
411/// Print a list of named values with their variadicities first. By default, the
412/// variadicity is single.
413///
414/// values-with-variadicity ::=
415/// `(` (value-with-variadicity (`,` value-with-variadicity)*)? `)`
416/// value-with-variadicity ::=
417/// bare-id `:` ("single" | "optional" | "variadic")? ssa-value
418static void printNamedValueListWithVariadicity(
419 OpAsmPrinter &p, Operation *op, OperandRange operands,
420 ArrayAttr valueNamesAttr, VariadicityArrayAttr variadicityAttr) {
421 printNamedValueListImpl(p, op, operands, valueNamesAttr, variadicityAttr);
422}
423
424static ParseResult
425parseAttributesOp(OpAsmParser &p,
426 SmallVectorImpl<OpAsmParser::UnresolvedOperand> &attrOperands,
427 ArrayAttr &attrNamesAttr) {
428 Builder &builder = p.getBuilder();
429 SmallVector<Attribute> attrNames;
430 if (succeeded(Result: p.parseOptionalLBrace())) {
431 auto parseOperands = [&]() {
432 if (p.parseAttribute(result&: attrNames.emplace_back()) || p.parseEqual() ||
433 p.parseOperand(result&: attrOperands.emplace_back()))
434 return failure();
435 return success();
436 };
437 if (p.parseCommaSeparatedList(parseElementFn: parseOperands) || p.parseRBrace())
438 return failure();
439 }
440 attrNamesAttr = builder.getArrayAttr(value: attrNames);
441 return success();
442}
443
444static void printAttributesOp(OpAsmPrinter &p, AttributesOp op,
445 OperandRange attrArgs, ArrayAttr attrNames) {
446 if (attrNames.empty())
447 return;
448 p << "{";
449 interleaveComma(c: llvm::seq<int>(Begin: 0, End: attrNames.size()), os&: p,
450 each_fn: [&](int i) { p << attrNames[i] << " = " << attrArgs[i]; });
451 p << '}';
452}
453
454LogicalResult RegionOp::verify() {
455 if (IntegerAttr numberOfBlocks = getNumberOfBlocksAttr())
456 if (int64_t number = numberOfBlocks.getInt(); number <= 0) {
457 return emitOpError(message: "the number of blocks is expected to be >= 1 but got ")
458 << number;
459 }
460 return success();
461}
462
463LogicalResult RegionsOp::verify() {
464 return verifyNames(op: *this, kindName: "region", names: getNames(), numOperands: getNumOperands());
465}
466
467#include "mlir/Dialect/IRDL/IR/IRDLInterfaces.cpp.inc"
468
469#define GET_TYPEDEF_CLASSES
470#include "mlir/Dialect/IRDL/IR/IRDLTypesGen.cpp.inc"
471
472#include "mlir/Dialect/IRDL/IR/IRDLEnums.cpp.inc"
473
474#define GET_ATTRDEF_CLASSES
475#include "mlir/Dialect/IRDL/IR/IRDLAttributes.cpp.inc"
476
477#define GET_OP_CLASSES
478#include "mlir/Dialect/IRDL/IR/IRDLOps.cpp.inc"
479