1//===- OpDefinitionsGen.cpp - MLIR op definitions generator ---------------===//
2//
3// Part of the LLVM Project, 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// OpDefinitionsGen uses the description of operations to generate C++
10// definitions for ops.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CppGenUtilities.h"
15#include "OpClass.h"
16#include "OpFormatGen.h"
17#include "OpGenHelpers.h"
18#include "mlir/TableGen/Argument.h"
19#include "mlir/TableGen/Attribute.h"
20#include "mlir/TableGen/Builder.h"
21#include "mlir/TableGen/Class.h"
22#include "mlir/TableGen/CodeGenHelpers.h"
23#include "mlir/TableGen/Format.h"
24#include "mlir/TableGen/GenInfo.h"
25#include "mlir/TableGen/Interfaces.h"
26#include "mlir/TableGen/Operator.h"
27#include "mlir/TableGen/Property.h"
28#include "mlir/TableGen/Region.h"
29#include "mlir/TableGen/SideEffects.h"
30#include "mlir/TableGen/Successor.h"
31#include "mlir/TableGen/Trait.h"
32#include "llvm/ADT/BitVector.h"
33#include "llvm/ADT/MapVector.h"
34#include "llvm/ADT/PointerUnion.h"
35#include "llvm/ADT/STLExtras.h"
36#include "llvm/ADT/Sequence.h"
37#include "llvm/ADT/SetVector.h"
38#include "llvm/ADT/SmallVector.h"
39#include "llvm/ADT/StringExtras.h"
40#include "llvm/ADT/StringMap.h"
41#include "llvm/ADT/StringRef.h"
42#include "llvm/ADT/StringSet.h"
43#include "llvm/Support/Casting.h"
44#include "llvm/Support/Debug.h"
45#include "llvm/Support/ErrorHandling.h"
46#include "llvm/Support/FormatVariadic.h"
47#include "llvm/Support/Signals.h"
48#include "llvm/Support/raw_ostream.h"
49#include "llvm/TableGen/CodeGenHelpers.h"
50#include "llvm/TableGen/Error.h"
51#include "llvm/TableGen/Record.h"
52#include "llvm/TableGen/TableGenBackend.h"
53
54#define DEBUG_TYPE "mlir-tblgen-opdefgen"
55
56using namespace llvm;
57using namespace mlir;
58using namespace mlir::tblgen;
59
60static const char *const tblgenNamePrefix = "tblgen_";
61static const char *const generatedArgName = "odsArg";
62static const char *const odsBuilder = "odsBuilder";
63static const char *const builderOpState = "odsState";
64static const char *const builderOpStateProperties =
65 "odsState.getOrAddProperties<Properties>()";
66static constexpr StringLiteral legacyBuilderDeprecation =
67 "use the overload taking Properties and discardableAttributes instead";
68static const char *const propertyStorage = "propStorage";
69static const char *const propertyValue = "propValue";
70static const char *const propertyAttr = "propAttr";
71static const char *const propertyDiag = "emitError";
72
73/// The logic to calculate the actual value range for a declared operand/result
74/// of an op with variadic operands/results. Note that this logic is not for
75/// general use; it assumes all variadic operands/results must have the same
76/// number of values.
77///
78/// {0}: The list of whether each declared operand/result is variadic.
79/// {1}: The total number of non-variadic operands/results.
80/// {2}: The total number of variadic operands/results.
81/// {3}: The total number of actual values.
82/// {4}: "operand" or "result".
83static const char *const sameVariadicSizeValueRangeCalcCode = R"(
84 bool isVariadic[] = {{{0}};
85 int prevVariadicCount = 0;
86 for (unsigned i = 0; i < index; ++i)
87 if (isVariadic[i]) ++prevVariadicCount;
88
89 // Calculate how many dynamic values a static variadic {4} corresponds to.
90 // This assumes all static variadic {4}s have the same dynamic value count.
91 int variadicSize = ({3} - {1}) / {2};
92 // `index` passed in as the parameter is the static index which counts each
93 // {4} (variadic or not) as size 1. So here for each previous static variadic
94 // {4}, we need to offset by (variadicSize - 1) to get where the dynamic
95 // value pack for this static {4} starts.
96 int start = index + (variadicSize - 1) * prevVariadicCount;
97 int size = isVariadic[index] ? variadicSize : 1;
98 return {{start, size};
99)";
100
101/// The logic to calculate the actual value range for a declared operand/result
102/// of an op with variadic operands/results. Note that this logic is assumes
103/// the op has an attribute specifying the size of each operand/result segment
104/// (variadic or not).
105static const char *const attrSizedSegmentValueRangeCalcCode = R"(
106 unsigned start = 0;
107 for (unsigned i = 0; i < index; ++i)
108 start += sizeAttr[i];
109 return {start, sizeAttr[index]};
110)";
111/// The code snippet to initialize the sizes for the value range calculation.
112///
113/// {0}: The code to get the attribute.
114static const char *const adapterSegmentSizeAttrInitCodeProperties = R"(
115 ::llvm::ArrayRef<int32_t> sizeAttr = {0};
116)";
117
118/// The logic to calculate the actual value range for a declared operand
119/// of an op with variadic of variadic operands within the OpAdaptor.
120///
121/// {0}: The name of the segment attribute.
122/// {1}: The index of the main operand.
123/// {2}: The range type of adaptor.
124static const char *const variadicOfVariadicAdaptorCalcCode = R"(
125 auto tblgenTmpOperands = getODSOperands({1});
126 auto sizes = {0}();
127
128 ::llvm::SmallVector<{2}> tblgenTmpOperandGroups;
129 for (int i = 0, e = sizes.size(); i < e; ++i) {{
130 tblgenTmpOperandGroups.push_back(tblgenTmpOperands.take_front(sizes[i]));
131 tblgenTmpOperands = tblgenTmpOperands.drop_front(sizes[i]);
132 }
133 return tblgenTmpOperandGroups;
134)";
135
136/// The logic to build a range of either operand or result values.
137///
138/// {0}: The begin iterator of the actual values.
139/// {1}: The call to generate the start and length of the value range.
140static const char *const valueRangeReturnCode = R"(
141 auto valueRange = {1};
142 return {{std::next({0}, valueRange.first),
143 std::next({0}, valueRange.first + valueRange.second)};
144)";
145
146/// Parse operand/result segment_size property.
147/// {0}: Number of elements in the segment array
148static const char *const parseTextualSegmentSizeFormat = R"(
149 size_t i = 0;
150 auto parseElem = [&]() -> ::mlir::ParseResult {
151 if (i >= {0})
152 return $_parser.emitError($_parser.getCurrentLocation(),
153 "expected `]` after {0} segment sizes");
154 if (failed($_parser.parseInteger($_storage[i])))
155 return ::mlir::failure();
156 i += 1;
157 return ::mlir::success();
158 };
159 if (failed($_parser.parseCommaSeparatedList(
160 ::mlir::AsmParser::Delimeter::Square, parseElem)))
161 return failure();
162 if (i < {0})
163 return $_parser.emitError($_parser.getCurrentLocation(),
164 "expected {0} segment sizes, found only ") << i;
165 return success();
166)";
167
168static const char *const printTextualSegmentSize = R"(
169 [&]() {
170 $_printer << '[';
171 ::llvm::interleaveComma($_storage, $_printer);
172 $_printer << ']';
173 }()
174)";
175
176/// Read operand/result segment_size from bytecode.
177static const char *const readBytecodeSegmentSizeNative = R"(
178 if ($_reader.getBytecodeVersion() >= /*kNativePropertiesODSSegmentSize=*/6)
179 return $_reader.readSparseArray(::llvm::MutableArrayRef($_storage));
180)";
181
182static const char *const readBytecodeSegmentSizeLegacy = R"(
183 if ($_reader.getBytecodeVersion() < /*kNativePropertiesODSSegmentSize=*/6) {
184 auto &$_storage = prop.$_propName;
185 ::mlir::DenseI32ArrayAttr attr;
186 if (::mlir::failed($_reader.readAttribute(attr))) return ::mlir::failure();
187 if (attr.size() > static_cast<int64_t>(sizeof($_storage) / sizeof(int32_t))) {
188 $_reader.emitError("size mismatch for operand/result_segment_size");
189 return ::mlir::failure();
190 }
191 ::llvm::copy(::llvm::ArrayRef<int32_t>(attr), $_storage.begin());
192 }
193)";
194
195/// Write operand/result segment_size to bytecode.
196static const char *const writeBytecodeSegmentSizeNative = R"(
197 if ($_writer.getBytecodeVersion() >= /*kNativePropertiesODSSegmentSize=*/6)
198 $_writer.writeSparseArray(::llvm::ArrayRef($_storage));
199)";
200
201/// Write operand/result segment_size to bytecode.
202static const char *const writeBytecodeSegmentSizeLegacy = R"(
203if ($_writer.getBytecodeVersion() < /*kNativePropertiesODSSegmentSize=*/6) {
204 auto &$_storage = prop.$_propName;
205 $_writer.writeAttribute(::mlir::DenseI32ArrayAttr::get($_ctxt, $_storage));
206}
207)";
208
209/// A header for indicating code sections.
210///
211/// {0}: Some text, or a class name.
212/// {1}: Some text.
213static const char *const opCommentHeader = R"(
214//===----------------------------------------------------------------------===//
215// {0} {1}
216//===----------------------------------------------------------------------===//
217
218)";
219
220static const char *const inlineCreateBody = R"(
221 ::mlir::OperationState __state__({0}, getOperationName());
222 build(builder, __state__{1});
223 auto __res__ = builder.create(__state__);
224 assert((::llvm::isa<{2}>(__res__)) && "builder didn't return the right type");
225 return ::llvm::cast<{2}>(__res__);
226)";
227
228static const char *const inlineCreateBodyImplicitLoc = R"(
229 return create(builder, builder.getLoc(){0});
230)";
231
232//===----------------------------------------------------------------------===//
233// Utility structs and functions
234//===----------------------------------------------------------------------===//
235
236// Replaces all occurrences of `match` in `str` with `substitute`.
237static std::string replaceAllSubstrs(std::string str, const std::string &match,
238 const std::string &substitute) {
239 std::string::size_type scanLoc = 0, matchLoc = std::string::npos;
240 while ((matchLoc = str.find(str: match, pos: scanLoc)) != std::string::npos) {
241 str = str.replace(pos: matchLoc, n: match.size(), str: substitute);
242 scanLoc = matchLoc + substitute.size();
243 }
244 return str;
245}
246
247// Returns whether the record has a value of the given name that can be returned
248// via getValueAsString.
249static inline bool hasStringAttribute(const Record &record,
250 StringRef fieldName) {
251 auto *valueInit = record.getValueInit(FieldName: fieldName);
252 return isa<StringInit>(Val: valueInit);
253}
254
255static std::string getArgumentName(const Operator &op, int index) {
256 const auto &operand = op.getOperand(index);
257 if (!operand.name.empty())
258 return std::string(operand.name);
259 return std::string(formatv(Fmt: "{0}_{1}", Vals: generatedArgName, Vals&: index));
260}
261
262// Returns true if we can use unwrapped value for the given `attr` in builders.
263static bool canUseUnwrappedRawValue(const tblgen::Attribute &attr) {
264 return attr.getReturnType() != attr.getStorageType() &&
265 // We need to wrap the raw value into an attribute in the builder impl
266 // so we need to make sure that the attribute specifies how to do that.
267 !attr.getConstBuilderTemplate().empty();
268}
269
270/// Build an attribute from a parameter value using the constant builder.
271static std::string constBuildAttrFromParam(const tblgen::Attribute &attr,
272 FmtContext &fctx,
273 StringRef paramName) {
274 std::string builderTemplate = attr.getConstBuilderTemplate().str();
275
276 // For StringAttr, its constant builder call will wrap the input in
277 // quotes, which is correct for normal string literals, but incorrect
278 // here given we use function arguments. So we need to strip the
279 // wrapping quotes.
280 if (StringRef(builderTemplate).contains(Other: "\"$0\""))
281 builderTemplate = replaceAllSubstrs(str: builderTemplate, match: "\"$0\"", substitute: "$0");
282
283 return tgfmt(fmt: builderTemplate, ctx: &fctx, vals&: paramName).str();
284}
285
286namespace {
287/// Metadata required to emit a registered or implicit attribute.
288struct AttributeMetadata {
289 /// The attribute name.
290 StringRef attrName;
291 /// Whether the attribute is required.
292 bool isRequired;
293 /// The ODS attribute constraint. Not present for implicit attributes.
294 std::optional<Attribute> constraint;
295};
296
297/// Helper class to select between OpAdaptor and Op code templates.
298class OpOrAdaptorHelper {
299public:
300 OpOrAdaptorHelper(const Operator &op, bool emitForOp)
301 : op(op), emitForOp(emitForOp) {
302 computeAttrMetadata();
303 }
304
305 /// Object that wraps a functor in a stream operator for interop with
306 /// llvm::formatv.
307 class Formatter {
308 public:
309 template <typename Functor>
310 Formatter(Functor &&func) : func(std::forward<Functor>(func)) {}
311
312 std::string str() const {
313 std::string result;
314 llvm::raw_string_ostream os(result);
315 os << *this;
316 return os.str();
317 }
318
319 private:
320 std::function<raw_ostream &(raw_ostream &)> func;
321
322 friend raw_ostream &operator<<(raw_ostream &os, const Formatter &fmt) {
323 return fmt.func(os);
324 }
325 };
326
327 // Generate code for getting an attribute.
328 Formatter getAttr(StringRef attrName) const {
329 assert(attrMetadata.count(attrName) && "expected attribute metadata");
330 return [attrName](raw_ostream &os) -> raw_ostream & {
331 return os << "getProperties()." << attrName;
332 };
333 }
334
335 // Get the prefix code for emitting an error.
336 Formatter emitErrorPrefix() const {
337 return [this](raw_ostream &os) -> raw_ostream & {
338 if (emitForOp)
339 return os << "emitOpError(\"";
340 return os << formatv(Fmt: "emitError(loc, \"'{0}' op ", Vals: op.getOperationName());
341 };
342 }
343
344 // Get the call to get an operand or segment of operands.
345 Formatter getOperand(unsigned index) const {
346 return [this, index](raw_ostream &os) -> raw_ostream & {
347 return os << formatv(Fmt: op.getOperand(index).isVariadic()
348 ? "this->getODSOperands({0})"
349 : "(*this->getODSOperands({0}).begin())",
350 Vals: index);
351 };
352 }
353
354 // Get the call to get a result of segment of results.
355 Formatter getResult(unsigned index) const {
356 return [this, index](raw_ostream &os) -> raw_ostream & {
357 if (!emitForOp)
358 return os << "<no results should be generated>";
359 return os << formatv(Fmt: op.getResult(index).isVariadic()
360 ? "this->getODSResults({0})"
361 : "(*this->getODSResults({0}).begin())",
362 Vals: index);
363 };
364 }
365
366 // Return whether an op instance is available.
367 bool isEmittingForOp() const { return emitForOp; }
368
369 // Return the ODS operation wrapper.
370 const Operator &getOp() const { return op; }
371
372 // Get the attribute metadata sorted by name.
373 const llvm::MapVector<StringRef, AttributeMetadata> &getAttrMetadata() const {
374 return attrMetadata;
375 }
376
377 /// Returns whether the operation will have a non-empty `Properties` struct.
378 bool hasNonEmptyPropertiesStruct() const {
379 return op.hasNonEmptyProperties();
380 }
381
382 std::optional<NamedProperty> &getOperandSegmentsSize() {
383 return operandSegmentsSize;
384 }
385
386 std::optional<NamedProperty> &getResultSegmentsSize() {
387 return resultSegmentsSize;
388 }
389
390 uint32_t getOperandSegmentSizesLegacyIndex() {
391 return operandSegmentSizesLegacyIndex;
392 }
393
394 uint32_t getResultSegmentSizesLegacyIndex() {
395 return resultSegmentSizesLegacyIndex;
396 }
397
398private:
399 // Compute the attribute metadata.
400 void computeAttrMetadata();
401
402 // The operation ODS wrapper.
403 const Operator &op;
404 // True if code is being generate for an op. False for an adaptor.
405 const bool emitForOp;
406
407 // The attribute metadata, mapped by name.
408 llvm::MapVector<StringRef, AttributeMetadata> attrMetadata;
409
410 // Property
411 std::optional<NamedProperty> operandSegmentsSize;
412 std::string operandSegmentsSizeStorage;
413 std::string operandSegmentsSizeParser;
414 std::optional<NamedProperty> resultSegmentsSize;
415 std::string resultSegmentsSizeStorage;
416 std::string resultSegmentsSizeParser;
417
418 // Indices to store the position in the emission order of the operand/result
419 // segment sizes attribute if emitted as part of the properties for legacy
420 // bytecode encodings, i.e. versions less than 6.
421 uint32_t operandSegmentSizesLegacyIndex = 0;
422 uint32_t resultSegmentSizesLegacyIndex = 0;
423};
424
425} // namespace
426
427void OpOrAdaptorHelper::computeAttrMetadata() {
428 // Enumerate the attribute names of this op, ensuring the attribute names are
429 // unique in case implicit attributes are explicitly registered.
430 for (const NamedAttribute &namedAttr : op.getAttributes()) {
431 Attribute attr = namedAttr.attr;
432 bool isOptional =
433 attr.hasDefaultValue() || attr.isOptional() || attr.isDerivedAttr();
434 attrMetadata.insert(
435 KV: {namedAttr.name, AttributeMetadata{.attrName: namedAttr.name, .isRequired: !isOptional, .constraint: attr}});
436 }
437
438 auto makeProperty = [&](StringRef storageType, StringRef parserCall) {
439 return Property(/*maybeDef=*/nullptr,
440 /*summary=*/"",
441 /*description=*/"",
442 /*storageType=*/storageType,
443 /*interfaceType=*/"::llvm::ArrayRef<int32_t>",
444 /*convertFromStorageCall=*/"$_storage",
445 /*assignToStorageCall=*/
446 "::llvm::copy($_value, $_storage.begin())",
447 /*convertToAttributeCall=*/
448 "return ::mlir::DenseI32ArrayAttr::get($_ctxt, $_storage);",
449 /*convertFromAttributeCall=*/
450 "return convertFromAttribute($_storage, $_attr, $_diag);",
451 /*parserCall=*/parserCall,
452 /*optionalParserCall=*/"",
453 /*printerCall=*/printTextualSegmentSize,
454 /*readFromMlirBytecodeCall=*/readBytecodeSegmentSizeNative,
455 /*writeToMlirBytecodeCall=*/writeBytecodeSegmentSizeNative,
456 /*hashPropertyCall=*/
457 "::llvm::hash_combine_range(std::begin($_storage), "
458 "std::end($_storage));",
459 /*StringRef defaultValue=*/"",
460 /*storageTypeValueOverride=*/"");
461 };
462
463 auto checkSegmentSizePropertyCollision =
464 [&](StringRef propertyName, StringRef legacyName, StringRef traitName) {
465 auto checkName = [&](StringRef name, StringRef entity) {
466 if (name != propertyName && name != legacyName)
467 return;
468 PrintFatalError(ErrorLoc: op.getLoc(),
469 Msg: entity + " '" + name +
470 "' conflicts with the property generated by '" +
471 traitName + "'");
472 };
473 for (const NamedAttribute &attr : op.getAttributes())
474 checkName(attr.name, "attribute");
475 for (const NamedProperty &prop : op.getProperties())
476 checkName(prop.name, "property");
477 };
478
479 // Include key attributes from several traits as implicitly registered.
480 if (op.getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments")) {
481 checkSegmentSizePropertyCollision(operandSegmentAttrName,
482 legacyOperandSegmentAttrName,
483 "AttrSizedOperandSegments");
484 operandSegmentsSizeStorage =
485 llvm::formatv(Fmt: "std::array<int32_t, {0}>", Vals: op.getNumOperands());
486 operandSegmentsSizeParser =
487 llvm::formatv(Fmt: parseTextualSegmentSizeFormat, Vals: op.getNumOperands());
488 operandSegmentsSize = {
489 .name: "operandSegmentSizes",
490 .prop: makeProperty(operandSegmentsSizeStorage, operandSegmentsSizeParser)};
491 }
492 if (op.getTrait(trait: "::mlir::OpTrait::AttrSizedResultSegments")) {
493 checkSegmentSizePropertyCollision(resultSegmentAttrName,
494 legacyResultSegmentAttrName,
495 "AttrSizedResultSegments");
496 resultSegmentsSizeStorage =
497 llvm::formatv(Fmt: "std::array<int32_t, {0}>", Vals: op.getNumResults());
498 resultSegmentsSizeParser =
499 llvm::formatv(Fmt: parseTextualSegmentSizeFormat, Vals: op.getNumResults());
500 resultSegmentsSize = {
501 .name: "resultSegmentSizes",
502 .prop: makeProperty(resultSegmentsSizeStorage, resultSegmentsSizeParser)};
503 }
504
505 // Store the metadata in sorted order.
506 SmallVector<AttributeMetadata> sortedAttrMetadata =
507 llvm::to_vector(Range: llvm::make_second_range(c: attrMetadata.takeVector()));
508 llvm::sort(C&: sortedAttrMetadata,
509 Comp: [](const AttributeMetadata &lhs, const AttributeMetadata &rhs) {
510 return lhs.attrName < rhs.attrName;
511 });
512
513 // Store the position of the legacy operand_segment_sizes /
514 // result_segment_sizes so we can emit a backward compatible property readers
515 // and writers.
516 operandSegmentSizesLegacyIndex = 0;
517 resultSegmentSizesLegacyIndex = 0;
518 for (auto item : sortedAttrMetadata) {
519 if (item.attrName < legacyOperandSegmentAttrName)
520 ++operandSegmentSizesLegacyIndex;
521 if (item.attrName < legacyResultSegmentAttrName)
522 ++resultSegmentSizesLegacyIndex;
523 }
524
525 // Store the results back into the map.
526 for (const AttributeMetadata &attr : sortedAttrMetadata)
527 attrMetadata.insert(KV: {attr.attrName, attr});
528}
529
530//===----------------------------------------------------------------------===//
531// Op emitter
532//===----------------------------------------------------------------------===//
533
534namespace {
535// Helper class to emit a record into the given output stream.
536class OpEmitter {
537 using ConstArgument =
538 llvm::PointerUnion<const AttributeMetadata *, const NamedProperty *>;
539
540public:
541 // Emits the declaration of `op` to `os` and appends the headers declaring
542 // the traits and constraints it uses to `includes`.
543 static void
544 emitDecl(const Operator &op, raw_ostream &os,
545 const StaticVerifierFunctionEmitter &staticVerifierEmitter,
546 SmallVectorImpl<StringRef> &includes);
547 // Emits the definition of `op` to `os` and appends the headers required by
548 // the definition beyond those of the declaration to `includes`.
549 static void
550 emitDef(const Operator &op, raw_ostream &os,
551 const StaticVerifierFunctionEmitter &staticVerifierEmitter,
552 SmallVectorImpl<StringRef> &includes);
553
554private:
555 OpEmitter(const Operator &op,
556 const StaticVerifierFunctionEmitter &staticVerifierEmitter);
557
558 // Returns the inherent attributes and properties represented by the
559 // operation's Properties struct.
560 SmallVector<ConstArgument> getAttrOrProperties();
561
562 void emitDecl(raw_ostream &os);
563 void emitDef(raw_ostream &os);
564
565 // Adds `trait` to the op class and records `cppIncludes` as headers required
566 // by the op declaration.
567 void addTrait(const Twine &trait, ArrayRef<StringRef> cppIncludes);
568 void addTrait(const tblgen::NativeTrait &trait);
569 void addTrait(const tblgen::InterfaceTrait &trait);
570
571 // Records the headers declaring the type and attribute constraints of the
572 // operands, results and attributes as required by the op declaration.
573 void addConstraintIncludes();
574
575 // Generate methods for accessing the attribute names of this operation.
576 void genAttrNameGetters();
577
578 // Generates the OpAsmOpInterface for this operation if possible.
579 void genOpAsmInterface();
580
581 // Generates the `getOperationName` method for this op.
582 void genOpNameGetter();
583
584 // Generates code to manage the properties, if any!
585 void genPropertiesSupport();
586
587 // Generates code to manage the encoding of properties to bytecode.
588 void
589 genPropertiesSupportForBytecode(ArrayRef<ConstArgument> attrOrProperties);
590
591 // Generates getters for the properties.
592 void genPropGetters();
593
594 // Generates seters for the properties.
595 void genPropSetters();
596
597 // Generates getters for the attributes.
598 void genAttrGetters();
599
600 // Generates setter for the attributes.
601 void genAttrSetters();
602
603 // Generates removers for optional attributes.
604 void genOptionalAttrRemovers();
605
606 // Generates getters for named operands.
607 void genNamedOperandGetters();
608
609 // Generates setters for named operands.
610 void genNamedOperandSetters();
611
612 // Generates getters for named results.
613 void genNamedResultGetters();
614
615 // Generates getters for named regions.
616 void genNamedRegionGetters();
617
618 // Generates getters for named successors.
619 void genNamedSuccessorGetters();
620
621 // Generates the method to populate default attributes.
622 void genPopulateDefaultAttributes();
623
624 // Generates builder methods for the operation.
625 void genBuilder();
626
627 // Generates the build() method that takes each operand/attribute
628 // as a stand-alone parameter.
629 void genSeparateArgParamBuilder();
630 void genInlineCreateBody(const SmallVector<MethodParameter> &paramList,
631 bool deprecated = false);
632
633 // Generates the build() method that takes each operand/attribute as a
634 // stand-alone parameter. The generated build() method uses first operand's
635 // type as all results' types.
636 void genUseOperandAsResultTypeSeparateParamBuilder();
637
638 // The kind of collective builder to generate
639 enum class CollectiveBuilderKind {
640 PropStruct, // Inherent attributes/properties are passed by `const
641 // Properties&`
642 AttrDict, // Inherent attributes/properties are passed by attribute
643 // dictionary
644 };
645
646 // Generates the internal helper used by legacy aggregate builders to split
647 // inherent properties from discardable attributes.
648 void genLegacyPropertiesBuilderHelper();
649
650 // Adds aggregate properties and attributes to the operation state.
651 void genCodeForAddingPropertiesAndAttributes(MethodBody &body,
652 CollectiveBuilderKind kind,
653 StringRef attributesName);
654
655 // Generates the build() method that takes all operands/attributes
656 // collectively as one parameter. The generated build() method uses first
657 // operand's type as all results' types.
658 void
659 genUseOperandAsResultTypeCollectiveParamBuilder(CollectiveBuilderKind kind);
660
661 // Generates the build() method that takes aggregate operands/attributes
662 // parameters. This build() method uses inferred types as result types.
663 // Requires: The type needs to be inferable via InferTypeOpInterface.
664 void genInferredTypeCollectiveParamBuilder(CollectiveBuilderKind kind);
665
666 // Generates the build() method that takesaggregate operands/attributes as
667 // parameters. The generated build() method uses first attribute's
668 // type as all result's types.
669 void genUseAttrAsResultTypeCollectiveParamBuilder(CollectiveBuilderKind kind);
670
671 // Generates the build() method that takes all result types collectively as
672 // one parameter. Similarly for operands and attributes.
673 void genCollectiveParamBuilder(CollectiveBuilderKind kind);
674
675 // The kind of parameter to generate for result types in builders.
676 enum class TypeParamKind {
677 None, // No result type in parameter list.
678 Separate, // A separate parameter for each result type.
679 Collective, // An ArrayRef<Type> for all result types.
680 };
681
682 // The kind of parameter to generate for attributes in builders.
683 enum class AttrParamKind {
684 WrappedAttr, // A wrapped MLIR Attribute instance.
685 UnwrappedValue, // A raw value without MLIR Attribute wrapper.
686 };
687
688 // Builds the parameter list for build() method of this op. This method writes
689 // to `paramList` the comma-separated parameter list and updates
690 // `resultTypeNames` with the names for parameters for specifying result
691 // types. `inferredAttributes` is populated with any attributes that are
692 // elided from the build list. The given `typeParamKind` and `attrParamKind`
693 // controls how result types and attributes are placed in the parameter list.
694 void buildParamList(SmallVectorImpl<MethodParameter> &paramList,
695 llvm::StringSet<> &inferredAttributes,
696 SmallVectorImpl<std::string> &resultTypeNames,
697 TypeParamKind typeParamKind,
698 AttrParamKind attrParamKind = AttrParamKind::WrappedAttr);
699
700 // Adds op arguments and regions into operation state for build() methods.
701 void
702 genCodeForAddingArgAndRegionForBuilder(MethodBody &body,
703 llvm::StringSet<> &inferredAttributes,
704 bool isRawValueAttr = false);
705
706 // Generates canonicalizer declaration for the operation.
707 void genCanonicalizerDecls();
708
709 // Generates the folder declaration for the operation.
710 void genFolderDecls();
711
712 // Generates the parser for the operation.
713 void genParser();
714
715 // Generates the printer for the operation.
716 void genPrinter();
717
718 // Generates verify method for the operation.
719 void genVerifier();
720
721 // Generates custom verify methods for the operation.
722 void genCustomVerifier();
723
724 // Generates verify statements for operands and results in the operation.
725 // The generated code will be attached to `body`.
726 void genOperandResultVerifier(MethodBody &body,
727 Operator::const_value_range values,
728 StringRef valueKind);
729
730 // Generates verify statements for regions in the operation.
731 // The generated code will be attached to `body`.
732 void genRegionVerifier(MethodBody &body);
733
734 // Generates verify statements for successors in the operation.
735 // The generated code will be attached to `body`.
736 void genSuccessorVerifier(MethodBody &body);
737
738 // Generates the traits used by the object.
739 void genTraits();
740
741 // Generate the OpInterface methods for all interfaces.
742 void genOpInterfaceMethods();
743
744 // Generate op interface methods for the given interface.
745 void genOpInterfaceMethods(const tblgen::InterfaceTrait *trait);
746
747 // Generate op interface method for the given interface method. If
748 // 'declaration' is true, generates a declaration, else a definition.
749 Method *genOpInterfaceMethod(const tblgen::InterfaceMethod &method,
750 bool declaration = true);
751
752 // Generate a `using` declaration for the op interface method to include
753 // the default implementation from the interface trait.
754 // This is needed when the interface defines multiple methods with the same
755 // name, but some have a default implementation and some don't.
756 UsingDeclaration *
757 genOpInterfaceMethodUsingDecl(const tblgen::InterfaceTrait *opTrait,
758 const tblgen::InterfaceMethod &method);
759
760 // Generate the side effect interface methods.
761 void genSideEffectInterfaceMethods();
762
763 // Generate the type inference interface methods.
764 void genTypeInterfaceMethods();
765
766private:
767 // The TableGen record for this op.
768 // TODO: OpEmitter should not have a Record directly,
769 // it should rather go through the Operator for better abstraction.
770 const Record &def;
771
772 // The wrapper operator class for querying information from this op.
773 const Operator &op;
774
775 // The C++ code builder for this op
776 OpClass opClass;
777
778 // The format context for verification code generation.
779 FmtContext verifyCtx;
780
781 // The emitter containing all of the locally emitted verification functions.
782 const StaticVerifierFunctionEmitter &staticVerifierEmitter;
783
784 // Helper for emitting op code.
785 OpOrAdaptorHelper emitHelper;
786
787 // Keep track of the interface using declarations that have been generated to
788 // avoid duplicates.
789 llvm::StringSet<> interfaceUsingNames;
790
791 // The headers declaring the traits and constraints used by the op class.
792 SmallVector<StringRef> requiredIncludes;
793
794 // The headers required by the op definition beyond those of the declaration.
795 SmallVector<StringRef> requiredDefIncludes;
796};
797
798} // namespace
799
800// Populate the format context `ctx` with substitutions of attributes, operands
801// and results.
802static void populateSubstitutions(const OpOrAdaptorHelper &emitHelper,
803 FmtContext &ctx) {
804 // Populate substitutions for attributes.
805 auto &op = emitHelper.getOp();
806 for (const auto &namedAttr : op.getAttributes())
807 ctx.addSubst(placeholder: namedAttr.name,
808 subst: emitHelper.getOp().getGetterName(name: namedAttr.name) + "()");
809
810 // Populate substitutions for named operands.
811 for (int i = 0, e = op.getNumOperands(); i < e; ++i) {
812 auto &value = op.getOperand(index: i);
813 if (!value.name.empty())
814 ctx.addSubst(placeholder: value.name, subst: emitHelper.getOperand(index: i).str());
815 }
816
817 // Populate substitutions for results.
818 for (int i = 0, e = op.getNumResults(); i < e; ++i) {
819 auto &value = op.getResult(index: i);
820 if (!value.name.empty())
821 ctx.addSubst(placeholder: value.name, subst: emitHelper.getResult(index: i).str());
822 }
823}
824
825// Return true if a verifier can be emitted for the attribute: it is not a
826// derived attribute, it has a predicate, its condition is not empty, and, for
827// adaptors, the condition does not reference the op.
828static bool canEmitAttrVerifier(Attribute attr, bool isEmittingForOp) {
829 if (attr.isDerivedAttr())
830 return false;
831 Pred pred = attr.getPredicate();
832 if (pred.isNull())
833 return false;
834 std::string condition = pred.getCondition();
835 return !condition.empty() &&
836 (!StringRef(condition).contains(Other: "$_op") || isEmittingForOp);
837}
838
839// Generate attribute verification. If an op instance is not available, then
840// attribute checks that require one will not be emitted.
841//
842// Attribute verification is performed as follows:
843//
844// 1. Load inherent attributes from the Properties struct and verify that all
845// required attributes are present.
846// 2. Verify native trait attributes so that other attributes may call methods
847// that depend on the validity of these attributes, e.g. segment size attributes
848// and operand or result getters.
849// 3. Verify the constraints on all present attributes.
850static void genAttributeVerifier(
851 const OpOrAdaptorHelper &emitHelper, FmtContext &ctx, MethodBody &body,
852 const StaticVerifierFunctionEmitter &staticVerifierEmitter) {
853 if (emitHelper.getAttrMetadata().empty())
854 return;
855
856 // Verify the attribute if it is present. This assumes that default values
857 // are valid. This code snippet pastes the condition inline.
858 //
859 // TODO: verify the default value is valid (perhaps in debug mode only).
860 //
861 // {0}: Attribute variable name.
862 // {1}: Attribute condition code.
863 // {2}: Emit error prefix.
864 // {3}: Attribute name.
865 // {4}: Attribute/constraint description.
866 const char *const verifyAttrInline = R"(
867 if ({0} && !({1}))
868 return {2}attribute '{3}' failed to satisfy constraint: {4}");
869)";
870 // Verify the attribute using a uniqued constraint. Can only be used within
871 // the context of an op.
872 //
873 // {0}: Unique constraint name.
874 // {1}: Attribute variable name.
875 // {2}: Attribute name.
876 const char *const verifyAttrUnique = R"(
877 if (::mlir::failed({0}(*this, {1}, "{2}")))
878 return ::mlir::failure();
879)";
880
881 // Emit the verifier for the attribute.
882 const auto emitVerifier = [&](Attribute attr, StringRef attrName,
883 StringRef varName) {
884 std::string condition = attr.getPredicate().getCondition();
885
886 std::optional<StringRef> constraintFn;
887 if (emitHelper.isEmittingForOp() &&
888 (constraintFn = staticVerifierEmitter.getAttrConstraintFn(constraint: attr))) {
889 body << formatv(Fmt: verifyAttrUnique, Vals&: *constraintFn, Vals&: varName, Vals&: attrName);
890 } else {
891 body << formatv(
892 Fmt: verifyAttrInline, Vals&: varName, Vals: tgfmt(fmt: condition, ctx: &ctx.withSelf(subst: varName)),
893 Vals: emitHelper.emitErrorPrefix(), Vals&: attrName,
894 Vals: buildErrorStreamingString(message: attr.getSummary(), ctx: ctx.withSelf(subst: varName),
895 errorStreamType: ErrorStreamType::InsideOpError));
896 }
897 };
898
899 // Prefix variables with `tblgen_` to avoid hiding the attribute accessor.
900 const auto getVarName = [&](StringRef attrName) {
901 return (tblgenNamePrefix + attrName).str();
902 };
903
904 body.indent();
905 for (const std::pair<StringRef, AttributeMetadata> &it :
906 emitHelper.getAttrMetadata()) {
907 const AttributeMetadata &metadata = it.second;
908 if (metadata.constraint && metadata.constraint->isDerivedAttr())
909 continue;
910 body << formatv(
911 Fmt: "auto tblgen_{0} = getProperties().{0}; (void)tblgen_{0};\n", Vals: it.first);
912 if (metadata.isRequired)
913 body << formatv(
914 Fmt: "if (!tblgen_{0}) return {1}requires attribute '{0}'\");\n", Vals: it.first,
915 Vals: emitHelper.emitErrorPrefix());
916 }
917 body.unindent();
918
919 bool isEmittingForOp = emitHelper.isEmittingForOp();
920 for (const auto &namedAttr : emitHelper.getOp().getAttributes())
921 if (canEmitAttrVerifier(attr: namedAttr.attr, isEmittingForOp))
922 emitVerifier(namedAttr.attr, namedAttr.name, getVarName(namedAttr.name));
923}
924
925static void genPropertyVerifier(
926 const OpOrAdaptorHelper &emitHelper, FmtContext &ctx, MethodBody &body,
927 const StaticVerifierFunctionEmitter &staticVerifierEmitter) {
928
929 // Code to get a reference to a property into a variable to avoid multiple
930 // evaluations while verifying a property.
931 // {0}: Property variable name.
932 // {1}: Property name, with the first letter capitalized, to find the getter.
933 // {2}: Property interface type.
934 const char *const fetchProperty = R"(
935 [[maybe_unused]] {2} {0} = this->get{1}();
936)";
937
938 // Code to verify that the predicate of a property holds. Embeds the
939 // condition inline.
940 // {0}: Property condition code, with tgfmt() applied.
941 // {1}: Emit error prefix.
942 // {2}: Property name.
943 // {3}: Property description.
944 const char *const verifyPropertyInline = R"(
945 if (!({0}))
946 return {1}property '{2}' failed to satisfy constraint: {3}");
947)";
948
949 // Verify the property using a uniqued constraint. Can only be used
950 // within the context of an op.
951 //
952 // {0}: Unique constraint name.
953 // {1}: Property variable name in interface type.
954 // {2}: Property name.
955 const char *const verifyPropertyUniqued = R"(
956 if (::mlir::failed({0}(*this, {1}, "{2}")))
957 return ::mlir::failure();
958)";
959
960 // Prefix variables with `tblgen_` to avoid hiding the attribute accessor.
961 const auto getVarName = [&](const NamedProperty &prop) {
962 std::string varName =
963 convertToCamelFromSnakeCase(input: prop.name, /*capitalizeFirst=*/false);
964 return (tblgenNamePrefix + Twine(varName)).str();
965 };
966
967 for (const NamedProperty &prop : emitHelper.getOp().getProperties()) {
968 Pred predicate = prop.prop.getPredicate();
969 // Null predicate, nothing to verify.
970 if (predicate == Pred())
971 continue;
972
973 std::string rawCondition = predicate.getCondition();
974 if (rawCondition == "true")
975 continue;
976 bool needsOp = StringRef(rawCondition).contains(Other: "$_op");
977 if (needsOp && !emitHelper.isEmittingForOp())
978 continue;
979
980 auto scope = body.scope(open: "{\n", close: "}\n", /*indent=*/true);
981 std::string varName = getVarName(prop);
982 std::string getterName =
983 convertToCamelFromSnakeCase(input: prop.name, /*capitalizeFirst=*/true);
984 body << formatv(Fmt: fetchProperty, Vals&: varName, Vals&: getterName,
985 Vals: prop.prop.getInterfaceType());
986 auto uniquedFn = staticVerifierEmitter.getPropConstraintFn(constraint: prop.prop);
987 if (uniquedFn.has_value() && emitHelper.isEmittingForOp())
988 body << formatv(Fmt: verifyPropertyUniqued, Vals&: *uniquedFn, Vals&: varName, Vals: prop.name);
989 else
990 body << formatv(Fmt: verifyPropertyInline,
991 Vals: tgfmt(fmt: rawCondition, ctx: &ctx.withSelf(subst: varName)),
992 Vals: emitHelper.emitErrorPrefix(), Vals: prop.name,
993 Vals: buildErrorStreamingString(
994 message: prop.prop.getSummary(), ctx: ctx.withSelf(subst: varName),
995 errorStreamType: ErrorStreamType::InsideOpError));
996 }
997}
998
999/// Include declarations specified on NativeTrait
1000static std::string formatExtraDeclarations(const Operator &op) {
1001 SmallVector<StringRef> extraDeclarations;
1002 // Include extra class declarations from NativeTrait
1003 for (const auto &trait : op.getTraits()) {
1004 if (auto *opTrait = dyn_cast<tblgen::NativeTrait>(Val: &trait)) {
1005 StringRef value = opTrait->getExtraConcreteClassDeclaration();
1006 if (value.empty())
1007 continue;
1008 extraDeclarations.push_back(Elt: value);
1009 }
1010 }
1011 extraDeclarations.push_back(Elt: op.getExtraClassDeclaration());
1012 return llvm::join(R&: extraDeclarations, Separator: "\n");
1013}
1014
1015/// Op extra class definitions have a `$cppClass` substitution that is to be
1016/// replaced by the C++ class name.
1017/// Include declarations specified on NativeTrait
1018static std::string formatExtraDefinitions(const Operator &op) {
1019 SmallVector<StringRef> extraDefinitions;
1020 // Include extra class definitions from NativeTrait
1021 for (const auto &trait : op.getTraits()) {
1022 if (auto *opTrait = dyn_cast<tblgen::NativeTrait>(Val: &trait)) {
1023 StringRef value = opTrait->getExtraConcreteClassDefinition();
1024 if (value.empty())
1025 continue;
1026 extraDefinitions.push_back(Elt: value);
1027 }
1028 }
1029 extraDefinitions.push_back(Elt: op.getExtraClassDefinition());
1030 FmtContext ctx = FmtContext().addSubst(placeholder: "cppClass", subst: op.getCppClassName());
1031 return tgfmt(fmt: llvm::join(R&: extraDefinitions, Separator: "\n"), ctx: &ctx).str();
1032}
1033
1034OpEmitter::OpEmitter(const Operator &op,
1035 const StaticVerifierFunctionEmitter &staticVerifierEmitter)
1036 : def(op.getDef()), op(op),
1037 opClass(op.getCppClassName(), formatExtraDeclarations(op),
1038 formatExtraDefinitions(op)),
1039 staticVerifierEmitter(staticVerifierEmitter),
1040 emitHelper(op, /*emitForOp=*/true) {
1041 verifyCtx.addSubst(placeholder: "_op", subst: "(*this->getOperation())");
1042 verifyCtx.addSubst(placeholder: "_ctxt", subst: "this->getOperation()->getContext()");
1043
1044 genTraits();
1045 addConstraintIncludes();
1046
1047 // Generate C++ code for various op methods. The order here determines the
1048 // methods in the generated file.
1049 genAttrNameGetters();
1050 genOpAsmInterface();
1051 genOpNameGetter();
1052 genNamedOperandGetters();
1053 genNamedOperandSetters();
1054 genNamedResultGetters();
1055 genNamedRegionGetters();
1056 genNamedSuccessorGetters();
1057 genPropertiesSupport();
1058 genPropGetters();
1059 genPropSetters();
1060 genAttrGetters();
1061 genAttrSetters();
1062 genOptionalAttrRemovers();
1063 genBuilder();
1064 genPopulateDefaultAttributes();
1065 genParser();
1066 genPrinter();
1067 genVerifier();
1068 genCustomVerifier();
1069 genCanonicalizerDecls();
1070 genFolderDecls();
1071 genTypeInterfaceMethods();
1072 genOpInterfaceMethods();
1073 generateOpFormat(constOp: op, opClass);
1074 genSideEffectInterfaceMethods();
1075}
1076void OpEmitter::emitDecl(
1077 const Operator &op, raw_ostream &os,
1078 const StaticVerifierFunctionEmitter &staticVerifierEmitter,
1079 SmallVectorImpl<StringRef> &includes) {
1080 OpEmitter emitter(op, staticVerifierEmitter);
1081 emitter.emitDecl(os);
1082 llvm::append_range(C&: includes, R&: emitter.requiredIncludes);
1083}
1084
1085void OpEmitter::emitDef(
1086 const Operator &op, raw_ostream &os,
1087 const StaticVerifierFunctionEmitter &staticVerifierEmitter,
1088 SmallVectorImpl<StringRef> &includes) {
1089 OpEmitter emitter(op, staticVerifierEmitter);
1090 emitter.emitDef(os);
1091 llvm::append_range(C&: includes, R&: emitter.requiredDefIncludes);
1092}
1093
1094void OpEmitter::emitDecl(raw_ostream &os) {
1095 opClass.finalize();
1096 opClass.writeDeclTo(rawOs&: os);
1097}
1098
1099void OpEmitter::emitDef(raw_ostream &os) {
1100 opClass.finalize();
1101 opClass.writeDefTo(rawOs&: os);
1102}
1103
1104void OpEmitter::addTrait(const Twine &trait, ArrayRef<StringRef> cppIncludes) {
1105 opClass.addTrait(trait);
1106 llvm::append_range(C&: requiredIncludes, R&: cppIncludes);
1107}
1108
1109void OpEmitter::addTrait(const tblgen::NativeTrait &trait) {
1110 addTrait(trait: trait.getFullyQualifiedTraitName(), cppIncludes: trait.getCppIncludes());
1111}
1112
1113void OpEmitter::addTrait(const tblgen::InterfaceTrait &trait) {
1114 addTrait(trait: trait.getFullyQualifiedTraitName(), cppIncludes: trait.getCppIncludes());
1115}
1116
1117void OpEmitter::addConstraintIncludes() {
1118 for (const NamedTypeConstraint &operand : op.getOperands())
1119 llvm::append_range(C&: requiredIncludes, R: operand.constraint.getCppIncludes());
1120 for (const NamedTypeConstraint &result : op.getResults())
1121 llvm::append_range(C&: requiredIncludes, R: result.constraint.getCppIncludes());
1122 for (const NamedAttribute &namedAttr : op.getAttributes())
1123 llvm::append_range(C&: requiredIncludes, R: namedAttr.attr.getCppIncludes());
1124}
1125
1126static void errorIfPruned(size_t line, Method *m, const Twine &methodName,
1127 const Operator &op) {
1128 if (m)
1129 return;
1130 PrintFatalError(ErrorLoc: op.getLoc(), Msg: "Unexpected overlap when generating `" +
1131 methodName + "` for " +
1132 op.getOperationName() + " (from line " +
1133 Twine(line) + ")");
1134}
1135
1136#define ERROR_IF_PRUNED(M, N, O) errorIfPruned(__LINE__, M, N, O)
1137
1138void OpEmitter::genAttrNameGetters() {
1139 const llvm::MapVector<StringRef, AttributeMetadata> &attributes =
1140 emitHelper.getAttrMetadata();
1141 bool hasOperandSegmentsSize =
1142 op.getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments");
1143 // Emit the getAttributeNames method.
1144 {
1145 auto *method = opClass.addStaticInlineMethod(
1146 retType: "::llvm::ArrayRef<::llvm::StringRef>", name: "getAttributeNames");
1147 ERROR_IF_PRUNED(method, "getAttributeNames", op);
1148 auto &body = method->body();
1149 if (!hasOperandSegmentsSize && attributes.empty()) {
1150 body << " return {};";
1151 // Nothing else to do if there are no registered attributes. Exit early.
1152 return;
1153 }
1154 body << " static ::llvm::StringRef attrNames[] = {";
1155 llvm::interleaveComma(c: llvm::make_first_range(c: attributes), os&: body,
1156 each_fn: [&](StringRef attrName) {
1157 body << "::llvm::StringRef(\"" << attrName << "\")";
1158 });
1159 if (hasOperandSegmentsSize) {
1160 if (!attributes.empty())
1161 body << ", ";
1162 body << "::llvm::StringRef(\"" << operandSegmentAttrName << "\")";
1163 }
1164 body << "};\n return ::llvm::ArrayRef(attrNames);";
1165 }
1166
1167 // Emit the getAttributeNameForIndex methods.
1168 {
1169 auto *method = opClass.addInlineMethod<Method::Private>(
1170 retType: "::mlir::StringAttr", name: "getAttributeNameForIndex",
1171 args: MethodParameter("unsigned", "index"));
1172 ERROR_IF_PRUNED(method, "getAttributeNameForIndex", op);
1173 method->body()
1174 << " return getAttributeNameForIndex((*this)->getName(), index);";
1175 }
1176 {
1177 auto *method = opClass.addStaticInlineMethod<Method::Private>(
1178 retType: "::mlir::StringAttr", name: "getAttributeNameForIndex",
1179 args: MethodParameter("::mlir::OperationName", "name"),
1180 args: MethodParameter("unsigned", "index"));
1181 ERROR_IF_PRUNED(method, "getAttributeNameForIndex", op);
1182
1183 if (attributes.empty()) {
1184 method->body() << " return {};";
1185 } else {
1186 const char *const getAttrName = R"(
1187 assert(index < {0} && "invalid attribute index");
1188 assert(name.getStringRef() == getOperationName() && "invalid operation name");
1189 assert(name.isRegistered() && "Operation isn't registered, missing a "
1190 "dependent dialect loading?");
1191 return name.getAttributeNames()[index];
1192)";
1193 method->body() << formatv(Fmt: getAttrName, Vals: attributes.size());
1194 }
1195 }
1196
1197 // Generate the <attr>AttrName methods, that expose the attribute names to
1198 // users.
1199 const char *attrNameMethodBody = " return getAttributeNameForIndex({0});";
1200 for (auto [index, attr] :
1201 llvm::enumerate(First: llvm::make_first_range(c: attributes))) {
1202 std::string name = op.getGetterName(name: attr);
1203 std::string methodName = name + "AttrName";
1204
1205 // Generate the non-static variant.
1206 {
1207 auto *method = opClass.addInlineMethod(retType: "::mlir::StringAttr", name&: methodName);
1208 ERROR_IF_PRUNED(method, methodName, op);
1209 method->body() << llvm::formatv(Fmt: attrNameMethodBody, Vals&: index);
1210 }
1211
1212 // Generate the static variant.
1213 {
1214 auto *method = opClass.addStaticInlineMethod(
1215 retType: "::mlir::StringAttr", name&: methodName,
1216 args: MethodParameter("::mlir::OperationName", "name"));
1217 ERROR_IF_PRUNED(method, methodName, op);
1218 method->body() << llvm::formatv(Fmt: attrNameMethodBody,
1219 Vals: "name, " + Twine(index));
1220 }
1221 }
1222 if (hasOperandSegmentsSize) {
1223 std::string name = op.getGetterName(name: operandSegmentAttrName);
1224 std::string methodName = name + "AttrName";
1225 // Generate the non-static variant.
1226 {
1227 auto *method = opClass.addInlineMethod(retType: "::mlir::StringAttr", name&: methodName);
1228 ERROR_IF_PRUNED(method, methodName, op);
1229 method->body()
1230 << " return (*this)->getName().getAttributeNames().back();";
1231 }
1232
1233 // Generate the static variant.
1234 {
1235 auto *method = opClass.addStaticInlineMethod(
1236 retType: "::mlir::StringAttr", name&: methodName,
1237 args: MethodParameter("::mlir::OperationName", "name"));
1238 ERROR_IF_PRUNED(method, methodName, op);
1239 method->body() << " return name.getAttributeNames().back();";
1240 }
1241 }
1242}
1243
1244// Emit the getter for a named property.
1245// It is templated to be shared between the Op and the adaptor class.
1246template <typename OpClassOrAdaptor>
1247static void emitPropGetter(OpClassOrAdaptor &opClass, const Operator &op,
1248 StringRef name, const Property &prop) {
1249 auto *method = opClass.addInlineMethod(prop.getInterfaceType(), name);
1250 ERROR_IF_PRUNED(method, name, op);
1251 method->body() << formatv(Fmt: " return getProperties().{0}();", Vals&: name);
1252}
1253
1254// Emit the getter for an attribute with the return type specified.
1255// It is templated to be shared between the Op and the adaptor class.
1256template <typename OpClassOrAdaptor>
1257static void emitAttrGetterWithReturnType(FmtContext &fctx,
1258 OpClassOrAdaptor &opClass,
1259 const Operator &op, StringRef name,
1260 Attribute attr) {
1261 auto *method = opClass.addMethod(attr.getReturnType(), name);
1262 ERROR_IF_PRUNED(method, name, op);
1263 auto &body = method->body();
1264 body << " auto attr = " << name << "Attr();\n";
1265 if (attr.hasDefaultValue() && attr.isOptional()) {
1266 // Returns the default value if not set.
1267 // TODO: this is inefficient, we are recreating the attribute for every
1268 // call. This should be set instead.
1269 if (!attr.isConstBuildable()) {
1270 PrintFatalError(Msg: "DefaultValuedAttr of type " + attr.getAttrDefName() +
1271 " must have a constBuilder");
1272 }
1273 std::string defaultValue =
1274 std::string(tgfmt(fmt: attr.getConstBuilderTemplate(), ctx: &fctx,
1275 vals: tgfmt(fmt: attr.getDefaultValue(), ctx: &fctx)));
1276 body << " if (!attr)\n return "
1277 << tgfmt(fmt: attr.getConvertFromStorageCall(),
1278 ctx: &fctx.withSelf(subst: defaultValue))
1279 << ";\n";
1280 }
1281 body << " return "
1282 << tgfmt(fmt: attr.getConvertFromStorageCall(), ctx: &fctx.withSelf(subst: "attr"))
1283 << ";\n";
1284}
1285
1286SmallVector<OpEmitter::ConstArgument> OpEmitter::getAttrOrProperties() {
1287 SmallVector<ConstArgument> attrOrProperties;
1288 for (const std::pair<StringRef, AttributeMetadata> &it :
1289 emitHelper.getAttrMetadata()) {
1290 if (!it.second.constraint || !it.second.constraint->isDerivedAttr())
1291 attrOrProperties.push_back(Elt: &it.second);
1292 }
1293 for (const NamedProperty &prop : op.getProperties())
1294 attrOrProperties.push_back(Elt: &prop);
1295 if (emitHelper.getOperandSegmentsSize())
1296 attrOrProperties.push_back(Elt: &emitHelper.getOperandSegmentsSize().value());
1297 if (emitHelper.getResultSegmentsSize())
1298 attrOrProperties.push_back(Elt: &emitHelper.getResultSegmentsSize().value());
1299 return attrOrProperties;
1300}
1301
1302void OpEmitter::genPropertiesSupport() {
1303 SmallVector<ConstArgument> attrOrProperties = getAttrOrProperties();
1304 auto &setPropMethod =
1305 opClass
1306 .addStaticMethod(
1307 retType: "::llvm::LogicalResult", name: "setPropertiesFromAttr",
1308 args: MethodParameter("Properties &", "prop"),
1309 args: MethodParameter("::mlir::Attribute", "attr"),
1310 args: MethodParameter(
1311 "::llvm::function_ref<::mlir::InFlightDiagnostic()>",
1312 "emitError"))
1313 ->body();
1314 auto &getPropMethod =
1315 opClass
1316 .addStaticMethod(retType: "::mlir::Attribute", name: "getPropertiesAsAttr",
1317 args: MethodParameter("::mlir::MLIRContext *", "ctx"),
1318 args: MethodParameter("const Properties &", "prop"))
1319 ->body();
1320 auto &hashMethod =
1321 opClass
1322 .addStaticMethod(retType: "llvm::hash_code", name: "computePropertiesHash",
1323 args: MethodParameter("const Properties &", "prop"))
1324 ->body();
1325 auto &getInherentAttrMethod =
1326 opClass
1327 .addStaticMethod(retType: "std::optional<mlir::Attribute>", name: "getInherentAttr",
1328 args: MethodParameter("::mlir::MLIRContext *", "ctx"),
1329 args: MethodParameter("const Properties &", "prop"),
1330 args: MethodParameter("llvm::StringRef", "name"))
1331 ->body();
1332 auto &setInherentAttrMethod =
1333 opClass
1334 .addStaticMethod(retType: "void", name: "setInherentAttr",
1335 args: MethodParameter("Properties &", "prop"),
1336 args: MethodParameter("llvm::StringRef", "name"),
1337 args: MethodParameter("mlir::Attribute", "value"))
1338 ->body();
1339 auto &walkInherentAttrsMethod =
1340 opClass
1341 .addStaticMethod(retType: "void", name: "walkInherentAttrs",
1342 args: MethodParameter("::mlir::MLIRContext *", "ctx"),
1343 args: MethodParameter("Properties &", "prop"),
1344 args: MethodParameter("::llvm::function_ref<void("
1345 "::llvm::StringRef, "
1346 "::mlir::Attribute &)>",
1347 "visitor"))
1348 ->body();
1349 auto &verifyInherentAttrsMethod =
1350 opClass
1351 .addStaticMethod(
1352 retType: "::llvm::LogicalResult", name: "verifyInherentAttrs",
1353 args: MethodParameter("::mlir::OperationName", "opName"),
1354 args: MethodParameter("::mlir::NamedAttrList &", "attrs"),
1355 args: MethodParameter(
1356 "llvm::function_ref<::mlir::InFlightDiagnostic()>",
1357 "emitError"))
1358 ->body();
1359
1360 opClass.declare<UsingDeclaration>(args: "Properties", args: "FoldAdaptor::Properties");
1361
1362 // Convert the property to the attribute form.
1363
1364 setPropMethod << R"decl(
1365 ::mlir::DictionaryAttr dict = ::llvm::dyn_cast<::mlir::DictionaryAttr>(attr);
1366 if (!dict) {
1367 emitError() << "expected DictionaryAttr to set properties";
1368 return ::mlir::failure();
1369 }
1370 )decl";
1371 const char *propFromAttrFmt = R"decl(
1372 auto setFromAttr = [] (auto &propStorage, ::mlir::Attribute propAttr,
1373 ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError) -> ::mlir::LogicalResult {{
1374 {0}
1375 };
1376 {1};
1377)decl";
1378 const char *attrGetNoDefaultFmt = R"decl(;
1379 if (attr && ::mlir::failed(setFromAttr(prop.{0}, attr, [&]() {{
1380 return emitError() << "for `{0}`: ";
1381 })))
1382 return ::mlir::failure();
1383)decl";
1384 const char *attrGetDefaultFmt = R"decl(;
1385 if (attr) {{
1386 if (::mlir::failed(setFromAttr(prop.{0}, attr, [&]() {{
1387 return emitError() << "for `{0}`: ";
1388 })))
1389 return ::mlir::failure();
1390 } else {{
1391 prop.{0} = {1};
1392 }
1393)decl";
1394
1395 for (const auto &attrOrProp : attrOrProperties) {
1396 if (const auto *namedProperty =
1397 llvm::dyn_cast_if_present<const NamedProperty *>(Val: attrOrProp)) {
1398 StringRef name = namedProperty->name;
1399 auto &prop = namedProperty->prop;
1400 FmtContext fctx;
1401
1402 std::string getAttr;
1403 llvm::raw_string_ostream os(getAttr);
1404 os << " auto attr = dict.get(\"" << name << "\");";
1405 if (name == operandSegmentAttrName) {
1406 // Backward compat for now, TODO: Remove at some point.
1407 os << " if (!attr) attr = dict.get(\"" << legacyOperandSegmentAttrName
1408 << "\");";
1409 }
1410 if (name == resultSegmentAttrName) {
1411 // Backward compat for now, TODO: Remove at some point.
1412 os << " if (!attr) attr = dict.get(\"" << legacyResultSegmentAttrName
1413 << "\");";
1414 }
1415
1416 fctx.withBuilder(subst: odsBuilder);
1417 setPropMethod << "{\n"
1418 << formatv(Fmt: propFromAttrFmt,
1419 Vals: tgfmt(fmt: prop.getConvertFromAttributeCall(),
1420 ctx: &fctx.addSubst(placeholder: "_attr", subst: propertyAttr)
1421 .addSubst(placeholder: "_storage", subst: propertyStorage)
1422 .addSubst(placeholder: "_diag", subst: propertyDiag)),
1423 Vals&: getAttr);
1424 if (prop.hasStorageTypeValueOverride()) {
1425 setPropMethod << formatv(Fmt: attrGetDefaultFmt, Vals&: name,
1426 Vals: prop.getStorageTypeValueOverride());
1427 } else if (prop.hasDefaultValue()) {
1428 setPropMethod << formatv(Fmt: attrGetDefaultFmt, Vals&: name,
1429 Vals: tgfmt(fmt: prop.getDefaultValue(), ctx: &fctx));
1430 } else {
1431 setPropMethod << formatv(Fmt: attrGetNoDefaultFmt, Vals&: name);
1432 }
1433 setPropMethod << " }\n";
1434 } else {
1435 const auto *namedAttr =
1436 llvm::dyn_cast_if_present<const AttributeMetadata *>(Val: attrOrProp);
1437 StringRef name = namedAttr->attrName;
1438 std::string getAttr;
1439 llvm::raw_string_ostream os(getAttr);
1440 os << " auto attr = dict.get(\"" << name << "\");";
1441 if (name == operandSegmentAttrName) {
1442 // Backward compat for now
1443 os << " if (!attr) attr = dict.get(\"" << legacyOperandSegmentAttrName
1444 << "\");";
1445 }
1446 if (name == resultSegmentAttrName) {
1447 // Backward compat for now
1448 os << " if (!attr) attr = dict.get(\"" << legacyResultSegmentAttrName
1449 << "\");";
1450 }
1451
1452 setPropMethod << formatv(Fmt: R"decl(
1453 {{
1454 {1}
1455 if (::mlir::failed(::mlir::detail::setAttributeProperty(
1456 prop.{0}, attr, "{0}", emitError)))
1457 return ::mlir::failure();
1458 }
1459)decl",
1460 Vals&: name, Vals&: getAttr);
1461 }
1462 }
1463 setPropMethod << " return ::mlir::success();\n";
1464
1465 // Convert the attribute form to the property.
1466
1467 getPropMethod << " ::mlir::SmallVector<::mlir::NamedAttribute> attrs;\n"
1468 << " ::mlir::Builder odsBuilder{ctx};\n";
1469 const char *propToAttrFmt = R"decl(
1470 {
1471 const auto &propStorage = prop.{0};
1472 auto attr = [&]() -> ::mlir::Attribute {{
1473 {1}
1474 }();
1475 attrs.push_back(odsBuilder.getNamedAttr("{0}", attr));
1476 }
1477)decl";
1478 for (const auto &attrOrProp : attrOrProperties) {
1479 if (const auto *namedProperty =
1480 llvm::dyn_cast_if_present<const NamedProperty *>(Val: attrOrProp)) {
1481 StringRef name = namedProperty->name;
1482 auto &prop = namedProperty->prop;
1483 FmtContext fctx;
1484 getPropMethod << formatv(
1485 Fmt: propToAttrFmt, Vals&: name,
1486 Vals: tgfmt(fmt: prop.getConvertToAttributeCall(),
1487 ctx: &fctx.addSubst(placeholder: "_ctxt", subst: "ctx")
1488 .addSubst(placeholder: "_storage", subst: propertyStorage)));
1489 continue;
1490 }
1491 const auto *namedAttr =
1492 llvm::dyn_cast_if_present<const AttributeMetadata *>(Val: attrOrProp);
1493 StringRef name = namedAttr->attrName;
1494 getPropMethod << formatv(
1495 Fmt: " ::mlir::detail::appendAttributeProperty(attrs, \"{0}\", "
1496 "prop.{0});\n",
1497 Vals&: name);
1498 }
1499 getPropMethod << R"decl(
1500 if (!attrs.empty())
1501 return odsBuilder.getDictionaryAttr(attrs);
1502 return {};
1503)decl";
1504
1505 // Hashing for the property
1506
1507 const char *propHashFmt = R"decl(
1508 auto hash_{0}_ = [] (const auto &propStorage) -> llvm::hash_code {
1509 using ::llvm::hash_value;
1510 return {1};
1511 };
1512)decl";
1513 for (const auto &attrOrProp : attrOrProperties) {
1514 if (const auto *namedProperty =
1515 llvm::dyn_cast_if_present<const NamedProperty *>(Val: attrOrProp)) {
1516 StringRef name = namedProperty->name;
1517 auto &prop = namedProperty->prop;
1518 FmtContext fctx;
1519 if (!prop.getHashPropertyCall().empty()) {
1520 hashMethod << formatv(
1521 Fmt: propHashFmt, Vals&: name,
1522 Vals: tgfmt(fmt: prop.getHashPropertyCall(),
1523 ctx: &fctx.addSubst(placeholder: "_storage", subst: propertyStorage)));
1524 }
1525 }
1526 }
1527 hashMethod << " using llvm::hash_value;\n";
1528 hashMethod << " return llvm::hash_combine(";
1529 llvm::interleaveComma(
1530 c: attrOrProperties, os&: hashMethod, each_fn: [&](const ConstArgument &attrOrProp) {
1531 if (const auto *namedProperty =
1532 llvm::dyn_cast_if_present<const NamedProperty *>(Val: attrOrProp)) {
1533 if (!namedProperty->prop.getHashPropertyCall().empty()) {
1534 hashMethod << "\n hash_" << namedProperty->name << "_(prop."
1535 << namedProperty->name << ")";
1536 } else {
1537 hashMethod << "\n hash_value(prop." << namedProperty->name
1538 << ")";
1539 }
1540 return;
1541 }
1542 const auto *namedAttr =
1543 llvm::dyn_cast_if_present<const AttributeMetadata *>(Val: attrOrProp);
1544 StringRef name = namedAttr->attrName;
1545 hashMethod << "\n llvm::hash_value(prop." << name
1546 << ".getAsOpaquePointer())";
1547 });
1548 hashMethod << ");\n";
1549
1550 const char *getInherentAttrMethodFmt = R"decl(
1551 if (name == "{0}")
1552 return prop.{0};
1553)decl";
1554 const char *setInherentAttrMethodFmt = R"decl(
1555 if (name == "{0}") {{
1556 prop.{0} = ::llvm::dyn_cast_or_null<std::remove_reference_t<decltype(prop.{0})>>(value);
1557 return;
1558 }
1559)decl";
1560 const char *walkInherentAttrsMethodFmt = R"decl(
1561 if (prop.{0}) {{
1562 ::mlir::Attribute value = prop.{0};
1563 ::mlir::Attribute originalValue = value;
1564 visitor("{0}", value);
1565 if (value != originalValue)
1566 setInherentAttr(prop, "{0}", value);
1567 }
1568)decl";
1569 for (const auto &attrOrProp : attrOrProperties) {
1570 if (const auto *namedAttr =
1571 llvm::dyn_cast_if_present<const AttributeMetadata *>(Val: attrOrProp)) {
1572 StringRef name = namedAttr->attrName;
1573 getInherentAttrMethod << formatv(Fmt: getInherentAttrMethodFmt, Vals&: name);
1574 setInherentAttrMethod << formatv(Fmt: setInherentAttrMethodFmt, Vals&: name);
1575 walkInherentAttrsMethod << formatv(Fmt: walkInherentAttrsMethodFmt, Vals&: name);
1576 continue;
1577 }
1578 // The ODS segment size property is "special": we expose it as an attribute
1579 // even though it is a native property.
1580 const auto *namedProperty = cast<const NamedProperty *>(Val: attrOrProp);
1581 StringRef name = namedProperty->name;
1582 if (name != operandSegmentAttrName && name != resultSegmentAttrName)
1583 continue;
1584 auto &prop = namedProperty->prop;
1585 FmtContext fctx;
1586 fctx.addSubst(placeholder: "_ctxt", subst: "ctx");
1587 fctx.addSubst(placeholder: "_storage", subst: Twine("prop.") + name);
1588 if (name == operandSegmentAttrName) {
1589 getInherentAttrMethod
1590 << formatv(Fmt: " if (name == \"{0}\" || name == \"{1}\") return ",
1591 Vals: legacyOperandSegmentAttrName, Vals: operandSegmentAttrName);
1592 } else {
1593 getInherentAttrMethod
1594 << formatv(Fmt: " if (name == \"{0}\" || name == \"{1}\") return ",
1595 Vals: legacyResultSegmentAttrName, Vals: resultSegmentAttrName);
1596 }
1597 getInherentAttrMethod << "[&]() -> ::mlir::Attribute { "
1598 << tgfmt(fmt: prop.getConvertToAttributeCall(), ctx: &fctx)
1599 << " }();\n";
1600
1601 if (name == operandSegmentAttrName) {
1602 setInherentAttrMethod
1603 << formatv(Fmt: " if (name == \"{0}\" || name == \"{1}\") {{",
1604 Vals: legacyOperandSegmentAttrName, Vals: operandSegmentAttrName);
1605 } else {
1606 setInherentAttrMethod
1607 << formatv(Fmt: " if (name == \"{0}\" || name == \"{1}\") {{",
1608 Vals: legacyResultSegmentAttrName, Vals: resultSegmentAttrName);
1609 }
1610 setInherentAttrMethod << formatv(Fmt: R"decl(
1611 auto arrAttr = ::llvm::dyn_cast_or_null<::mlir::DenseI32ArrayAttr>(value);
1612 if (!arrAttr) return;
1613 if (arrAttr.size() != sizeof(prop.{0}) / sizeof(int32_t))
1614 return;
1615 llvm::copy(arrAttr.asArrayRef(), prop.{0}.begin());
1616 return;
1617 }
1618)decl",
1619 Vals&: name);
1620 walkInherentAttrsMethod
1621 << formatv(Fmt: " {{\n"
1622 " ::mlir::Attribute value = [&]() -> ::mlir::Attribute "
1623 "{ {1} }();\n"
1624 " ::mlir::Attribute originalValue = value;\n"
1625 " visitor(\"{0}\", value);\n"
1626 " if (value != originalValue)\n"
1627 " setInherentAttr(prop, \"{0}\", value);\n"
1628 " }\n",
1629 Vals&: name, Vals: tgfmt(fmt: prop.getConvertToAttributeCall(), ctx: &fctx));
1630 }
1631 getInherentAttrMethod << " return std::nullopt;\n";
1632
1633 // Emit the verifiers method for backward compatibility with the generic
1634 // syntax. This method verifies the constraint on the properties attributes
1635 // before they are set, since dyn_cast<> will silently omit failures.
1636 for (const auto &attrOrProp : attrOrProperties) {
1637 const auto *namedAttr =
1638 llvm::dyn_cast_if_present<const AttributeMetadata *>(Val: attrOrProp);
1639 if (!namedAttr || !namedAttr->constraint)
1640 continue;
1641 Attribute attr = *namedAttr->constraint;
1642 std::optional<StringRef> constraintFn =
1643 staticVerifierEmitter.getAttrConstraintFn(constraint: attr);
1644 if (!constraintFn)
1645 continue;
1646 if (canEmitAttrVerifier(attr,
1647 /*isEmittingForOp=*/false)) {
1648 std::string name = op.getGetterName(name: namedAttr->attrName);
1649 verifyInherentAttrsMethod
1650 << formatv(Fmt: R"(
1651 {{
1652 ::mlir::Attribute attr = attrs.get({0}AttrName(opName));
1653 if (attr && ::mlir::failed({1}(attr, "{2}", emitError)))
1654 return ::mlir::failure();
1655 }
1656)",
1657 Vals&: name, Vals&: constraintFn, Vals: namedAttr->attrName);
1658 }
1659 }
1660 verifyInherentAttrsMethod << " return ::mlir::success();";
1661
1662 // Generate methods to interact with bytecode.
1663 genPropertiesSupportForBytecode(attrOrProperties);
1664}
1665
1666void OpEmitter::genPropertiesSupportForBytecode(
1667 ArrayRef<ConstArgument> attrOrProperties) {
1668 if (attrOrProperties.empty())
1669 return;
1670
1671 if (op.useCustomPropertiesEncoding()) {
1672 opClass.declareStaticMethod(
1673 retType: "::llvm::LogicalResult", name: "readProperties",
1674 args: MethodParameter("::mlir::DialectBytecodeReader &", "reader"),
1675 args: MethodParameter("::mlir::OperationState &", "state"));
1676 opClass.declareMethod(
1677 retType: "void", name: "writeProperties",
1678 args: MethodParameter("::mlir::DialectBytecodeWriter &", "writer"));
1679 return;
1680 }
1681
1682 auto &readPropertiesMethod =
1683 opClass
1684 .addStaticMethod(
1685 retType: "::llvm::LogicalResult", name: "readProperties",
1686 args: MethodParameter("::mlir::DialectBytecodeReader &", "reader"),
1687 args: MethodParameter("::mlir::OperationState &", "state"))
1688 ->body();
1689
1690 auto &writePropertiesMethod =
1691 opClass
1692 .addMethod(
1693 retType: "void", name: "writeProperties",
1694 args: MethodParameter("::mlir::DialectBytecodeWriter &", "writer"))
1695 ->body();
1696
1697 // Populate bytecode serialization logic.
1698 readPropertiesMethod
1699 << " auto &prop = state.getOrAddProperties<Properties>(); (void)prop;";
1700 writePropertiesMethod << " auto &prop = getProperties(); (void)prop;\n";
1701 for (const auto &item : llvm::enumerate(First&: attrOrProperties)) {
1702 auto &attrOrProp = item.value();
1703 FmtContext fctx;
1704 fctx.addSubst(placeholder: "_reader", subst: "reader")
1705 .addSubst(placeholder: "_writer", subst: "writer")
1706 .addSubst(placeholder: "_storage", subst: propertyStorage)
1707 .addSubst(placeholder: "_ctxt", subst: "this->getContext()");
1708 // If the op emits operand/result segment sizes as a property, emit the
1709 // legacy reader/writer in the appropriate order to allow backward
1710 // compatibility and back deployment.
1711 if (emitHelper.getOperandSegmentsSize().has_value() &&
1712 item.index() == emitHelper.getOperandSegmentSizesLegacyIndex()) {
1713 FmtContext fmtCtxt(fctx);
1714 fmtCtxt.addSubst(placeholder: "_propName", subst: operandSegmentAttrName);
1715 readPropertiesMethod << tgfmt(fmt: readBytecodeSegmentSizeLegacy, ctx: &fmtCtxt);
1716 writePropertiesMethod << tgfmt(fmt: writeBytecodeSegmentSizeLegacy, ctx: &fmtCtxt);
1717 }
1718 if (emitHelper.getResultSegmentsSize().has_value() &&
1719 item.index() == emitHelper.getResultSegmentSizesLegacyIndex()) {
1720 FmtContext fmtCtxt(fctx);
1721 fmtCtxt.addSubst(placeholder: "_propName", subst: resultSegmentAttrName);
1722 readPropertiesMethod << tgfmt(fmt: readBytecodeSegmentSizeLegacy, ctx: &fmtCtxt);
1723 writePropertiesMethod << tgfmt(fmt: writeBytecodeSegmentSizeLegacy, ctx: &fmtCtxt);
1724 }
1725 if (const auto *namedProperty =
1726 dyn_cast<const NamedProperty *>(Val: attrOrProp)) {
1727 StringRef name = namedProperty->name;
1728 readPropertiesMethod << formatv(
1729 Fmt: R"(
1730 {{
1731 auto &propStorage = prop.{0};
1732 auto readProp = [&]() {
1733 {1};
1734 return ::mlir::success();
1735 };
1736 if (::mlir::failed(readProp()))
1737 return ::mlir::failure();
1738 }
1739)",
1740 Vals&: name,
1741 Vals: tgfmt(fmt: namedProperty->prop.getReadFromMlirBytecodeCall(), ctx: &fctx));
1742 writePropertiesMethod << formatv(
1743 Fmt: R"(
1744 {{
1745 auto &propStorage = prop.{0};
1746 {1};
1747 }
1748)",
1749 Vals&: name, Vals: tgfmt(fmt: namedProperty->prop.getWriteToMlirBytecodeCall(), ctx: &fctx));
1750 continue;
1751 }
1752 const auto *namedAttr = dyn_cast<const AttributeMetadata *>(Val: attrOrProp);
1753 StringRef name = namedAttr->attrName;
1754 if (namedAttr->isRequired) {
1755 readPropertiesMethod << formatv(Fmt: R"(
1756 if (::mlir::failed(reader.readAttribute(prop.{0})))
1757 return ::mlir::failure();
1758)",
1759 Vals&: name);
1760 writePropertiesMethod
1761 << formatv(Fmt: " writer.writeAttribute(prop.{0});\n", Vals&: name);
1762 } else {
1763 readPropertiesMethod << formatv(Fmt: R"(
1764 if (::mlir::failed(reader.readOptionalAttribute(prop.{0})))
1765 return ::mlir::failure();
1766)",
1767 Vals&: name);
1768 writePropertiesMethod << formatv(Fmt: R"(
1769 writer.writeOptionalAttribute(prop.{0});
1770)",
1771 Vals&: name);
1772 }
1773 }
1774 readPropertiesMethod << " return ::mlir::success();";
1775}
1776
1777void OpEmitter::genPropGetters() {
1778 for (const NamedProperty &prop : op.getProperties()) {
1779 std::string name = op.getGetterName(name: prop.name);
1780 emitPropGetter(opClass, op, name, prop: prop.prop);
1781 }
1782}
1783
1784void OpEmitter::genPropSetters() {
1785 for (const NamedProperty &prop : op.getProperties()) {
1786 std::string name = op.getSetterName(name: prop.name);
1787 std::string argName = "new" + convertToCamelFromSnakeCase(
1788 input: prop.name, /*capitalizeFirst=*/true);
1789 auto *method = opClass.addInlineMethod(
1790 retType: "void", name, args: MethodParameter(prop.prop.getInterfaceType(), argName));
1791 if (!method)
1792 return;
1793 method->body() << formatv(Fmt: " getProperties().{0}({1});", Vals&: name, Vals&: argName);
1794 }
1795}
1796
1797void OpEmitter::genAttrGetters() {
1798 FmtContext fctx;
1799 fctx.withBuilder(subst: "::mlir::Builder((*this)->getContext())");
1800
1801 // Emit the derived attribute body.
1802 auto emitDerivedAttr = [&](StringRef name, Attribute attr) {
1803 if (auto *method = opClass.addMethod(retType: attr.getReturnType(), name))
1804 method->body() << " " << attr.getDerivedCodeBody() << "\n";
1805 };
1806
1807 // Generate named accessor with Attribute return type. This is a wrapper
1808 // class that allows referring to the attributes via accessors instead of
1809 // having to use the string interface for better compile time verification.
1810 auto emitAttrWithStorageType = [&](StringRef name, StringRef attrName,
1811 Attribute attr) {
1812 // The method body for this getter is trivial. Emit it inline.
1813 auto *method =
1814 opClass.addInlineMethod(retType: attr.getStorageType(), name: name + "Attr");
1815 if (!method)
1816 return;
1817 method->body() << formatv(
1818 Fmt: " return ::llvm::{1}<{2}>({0});", Vals: emitHelper.getAttr(attrName),
1819 Vals: attr.isOptional() || attr.hasDefaultValue() ? "dyn_cast_or_null"
1820 : "cast",
1821 Vals: attr.getStorageType());
1822 };
1823
1824 for (const NamedAttribute &namedAttr : op.getAttributes()) {
1825 std::string name = op.getGetterName(name: namedAttr.name);
1826 if (namedAttr.attr.isDerivedAttr()) {
1827 emitDerivedAttr(name, namedAttr.attr);
1828 } else {
1829 emitAttrWithStorageType(name, namedAttr.name, namedAttr.attr);
1830 emitAttrGetterWithReturnType(fctx, opClass, op, name, attr: namedAttr.attr);
1831 }
1832 }
1833
1834 auto derivedAttrs = make_filter_range(Range: op.getAttributes(),
1835 Pred: [](const NamedAttribute &namedAttr) {
1836 return namedAttr.attr.isDerivedAttr();
1837 });
1838 if (derivedAttrs.empty())
1839 return;
1840
1841 addTrait(trait: "::mlir::DerivedAttributeOpInterface::Trait",
1842 cppIncludes: {"mlir/Interfaces/DerivedAttributeOpInterface.h"});
1843 // Generate helper method to query whether a named attribute is a derived
1844 // attribute. This enables, for example, avoiding adding an attribute that
1845 // overlaps with a derived attribute.
1846 {
1847 auto *method =
1848 opClass.addStaticMethod(retType: "bool", name: "isDerivedAttribute",
1849 args: MethodParameter("::llvm::StringRef", "name"));
1850 ERROR_IF_PRUNED(method, "isDerivedAttribute", op);
1851 auto &body = method->body();
1852 for (auto namedAttr : derivedAttrs)
1853 body << " if (name == \"" << namedAttr.name << "\") return true;\n";
1854 body << " return false;";
1855 }
1856 // Generate method to materialize derived attributes as a DictionaryAttr.
1857 {
1858 auto *method = opClass.addMethod(retType: "::mlir::DictionaryAttr",
1859 name: "materializeDerivedAttributes");
1860 ERROR_IF_PRUNED(method, "materializeDerivedAttributes", op);
1861 auto &body = method->body();
1862
1863 auto nonMaterializable =
1864 make_filter_range(Range&: derivedAttrs, Pred: [](const NamedAttribute &namedAttr) {
1865 return namedAttr.attr.getConvertFromStorageCall().empty();
1866 });
1867 if (!nonMaterializable.empty()) {
1868 std::string attrs;
1869 llvm::raw_string_ostream os(attrs);
1870 interleaveComma(c: nonMaterializable, os, each_fn: [&](const NamedAttribute &attr) {
1871 os << op.getGetterName(name: attr.name);
1872 });
1873 PrintWarning(
1874 WarningLoc: op.getLoc(),
1875 Msg: formatv(
1876 Fmt: "op has non-materializable derived attributes '{0}', skipping",
1877 Vals&: os.str()));
1878 body << formatv(Fmt: " emitOpError(\"op has non-materializable derived "
1879 "attributes '{0}'\");\n",
1880 Vals&: attrs);
1881 body << " return nullptr;";
1882 return;
1883 }
1884
1885 body << " ::mlir::MLIRContext* ctx = getContext();\n";
1886 body << " ::mlir::Builder odsBuilder(ctx); (void)odsBuilder;\n";
1887 body << " return ::mlir::DictionaryAttr::get(";
1888 body << " ctx, {\n";
1889 interleave(
1890 c: derivedAttrs, os&: body,
1891 each_fn: [&](const NamedAttribute &namedAttr) {
1892 auto tmpl = namedAttr.attr.getConvertFromStorageCall();
1893 std::string name = op.getGetterName(name: namedAttr.name);
1894 body << " {" << name << "AttrName(),\n"
1895 << tgfmt(fmt: tmpl, ctx: &fctx.withSelf(subst: name + "()")
1896 .withBuilder(subst: "odsBuilder")
1897 .addSubst(placeholder: "_ctxt", subst: "ctx")
1898 .addSubst(placeholder: "_storage", subst: "ctx"))
1899 << "}";
1900 },
1901 separator: ",\n");
1902 body << "});";
1903 }
1904}
1905
1906void OpEmitter::genAttrSetters() {
1907 // Generate the code to set an attribute.
1908 auto emitSetAttr = [&](Method *method, StringRef getterName,
1909 StringRef attrName, StringRef attrVar) {
1910 method->body() << formatv(Fmt: " getProperties().{0} = {1};", Vals&: attrName,
1911 Vals&: attrVar);
1912 };
1913
1914 // Generate raw named setter type. This is a wrapper class that allows setting
1915 // to the attributes via setters instead of having to use the string interface
1916 // for better compile time verification.
1917 auto emitAttrWithStorageType = [&](StringRef setterName, StringRef getterName,
1918 StringRef attrName, Attribute attr) {
1919 // This method body is trivial, so emit it inline.
1920 auto *method =
1921 opClass.addInlineMethod(retType: "void", name: setterName + "Attr",
1922 args: MethodParameter(attr.getStorageType(), "attr"));
1923 if (method)
1924 emitSetAttr(method, getterName, attrName, "attr");
1925 };
1926
1927 // Generate a setter that accepts the underlying C++ type as opposed to the
1928 // attribute type.
1929 auto emitAttrWithReturnType = [&](StringRef setterName, StringRef getterName,
1930 StringRef attrName, Attribute attr) {
1931 Attribute baseAttr = attr.getBaseAttr();
1932 if (!canUseUnwrappedRawValue(attr: baseAttr))
1933 return;
1934 FmtContext fctx;
1935 fctx.withBuilder(subst: "::mlir::Builder((*this)->getContext())");
1936 bool isUnitAttr = attr.getAttrDefName() == "UnitAttr";
1937 bool isOptional = attr.isOptional();
1938
1939 auto createMethod = [&](const Twine &paramType) {
1940 return opClass.addMethod(retType: "void", name&: setterName,
1941 args: MethodParameter(paramType.str(), "attrValue"));
1942 };
1943
1944 // Build the method using the correct parameter type depending on
1945 // optionality.
1946 Method *method = nullptr;
1947 if (isUnitAttr)
1948 method = createMethod("bool");
1949 else if (isOptional)
1950 method =
1951 createMethod("::std::optional<" + baseAttr.getReturnType() + ">");
1952 else
1953 method = createMethod(attr.getReturnType());
1954 if (!method)
1955 return;
1956
1957 // If the value isn't optional, just set it directly.
1958 if (!isOptional) {
1959 emitSetAttr(method, getterName, attrName,
1960 constBuildAttrFromParam(attr, fctx, paramName: "attrValue"));
1961 return;
1962 }
1963
1964 // Otherwise, we only set if the provided value is valid. If it isn't, we
1965 // remove the attribute.
1966
1967 // TODO: Handle unit attr parameters specially, given that it is treated as
1968 // optional but not in the same way as the others (i.e. it uses bool over
1969 // std::optional<>).
1970 StringRef paramStr = isUnitAttr ? "attrValue" : "*attrValue";
1971 const char *optionalCodeBody = R"(
1972 auto &odsProp = getProperties().{0};
1973 if (attrValue)
1974 odsProp = {1};
1975 else
1976 odsProp = nullptr;)";
1977 method->body() << formatv(
1978 Fmt: optionalCodeBody, Vals&: attrName,
1979 Vals: constBuildAttrFromParam(attr: baseAttr, fctx, paramName: paramStr));
1980 };
1981
1982 for (const NamedAttribute &namedAttr : op.getAttributes()) {
1983 if (namedAttr.attr.isDerivedAttr())
1984 continue;
1985 std::string setterName = op.getSetterName(name: namedAttr.name);
1986 std::string getterName = op.getGetterName(name: namedAttr.name);
1987 emitAttrWithStorageType(setterName, getterName, namedAttr.name,
1988 namedAttr.attr);
1989 emitAttrWithReturnType(setterName, getterName, namedAttr.name,
1990 namedAttr.attr);
1991 }
1992}
1993
1994void OpEmitter::genOptionalAttrRemovers() {
1995 // Generate methods for removing optional attributes, instead of having to
1996 // use the string interface. Enables better compile time verification.
1997 auto emitRemoveAttr = [&](StringRef name) {
1998 auto *method = opClass.addInlineMethod(retType: "::mlir::Attribute",
1999 name: op.getRemoverName(name) + "Attr");
2000 if (!method)
2001 return;
2002 method->body() << formatv(Fmt: R"(
2003 auto attr = getProperties().{0};
2004 getProperties().{0} = {{};
2005 return attr;
2006)",
2007 Vals&: name);
2008 };
2009
2010 for (const NamedAttribute &namedAttr : op.getAttributes())
2011 if (namedAttr.attr.isOptional())
2012 emitRemoveAttr(namedAttr.name);
2013}
2014
2015// Generates the code to compute the start and end index of an operand or result
2016// range.
2017template <typename RangeT>
2018static void generateValueRangeStartAndEnd(
2019 Class &opClass, bool isGenericAdaptorBase, StringRef methodName,
2020 int numVariadic, int numNonVariadic, StringRef rangeSizeCall,
2021 bool hasAttrSegmentSize, StringRef sizeAttrInit, RangeT &&odsValues) {
2022
2023 SmallVector<MethodParameter> parameters{MethodParameter("unsigned", "index")};
2024 if (isGenericAdaptorBase) {
2025 parameters.emplace_back(Args: "unsigned", Args: "odsOperandsSize");
2026 // The range size is passed per parameter for generic adaptor bases as
2027 // using the rangeSizeCall would require the operands, which are not
2028 // accessible in the base class.
2029 rangeSizeCall = "odsOperandsSize";
2030 }
2031
2032 // The method is trivial if the operation does not have any variadic operands.
2033 // In that case, make sure to generate it in-line.
2034 auto *method = opClass.addMethod(retType: "std::pair<unsigned, unsigned>", name&: methodName,
2035 properties: numVariadic == 0 ? Method::Properties::Inline
2036 : Method::Properties::None,
2037 args&: parameters);
2038 if (!method)
2039 return;
2040 auto &body = method->body();
2041 if (numVariadic == 0) {
2042 body << " return {index, 1};\n";
2043 } else if (hasAttrSegmentSize) {
2044 body << sizeAttrInit << attrSizedSegmentValueRangeCalcCode;
2045 } else {
2046 // Because the op can have arbitrarily interleaved variadic and non-variadic
2047 // operands, we need to embed a list in the "sink" getter method for
2048 // calculation at run-time.
2049 SmallVector<StringRef, 4> isVariadic;
2050 isVariadic.reserve(N: llvm::size(odsValues));
2051 for (auto &it : odsValues)
2052 isVariadic.push_back(Elt: it.isVariableLength() ? "true" : "false");
2053 std::string isVariadicList = llvm::join(R&: isVariadic, Separator: ", ");
2054 body << formatv(Fmt: sameVariadicSizeValueRangeCalcCode, Vals&: isVariadicList,
2055 Vals&: numNonVariadic, Vals&: numVariadic, Vals&: rangeSizeCall, Vals: "operand");
2056 }
2057}
2058
2059static std::string generateTypeForGetter(const NamedTypeConstraint &value) {
2060 return llvm::formatv(Fmt: "::mlir::TypedValue<{0}>", Vals: value.constraint.getCppType())
2061 .str();
2062}
2063
2064// Generates the named operand getter methods for the given Operator `op` and
2065// puts them in `opClass`. Uses `rangeType` as the return type of getters that
2066// return a range of operands (individual operands are `Value ` and each
2067// element in the range must also be `Value `); use `rangeBeginCall` to get
2068// an iterator to the beginning of the operand range; use `rangeSizeCall` to
2069// obtain the number of operands. `getOperandCallPattern` contains the code
2070// necessary to obtain a single operand whose position will be substituted
2071// instead of
2072// "{0}" marker in the pattern. Note that the pattern should work for any kind
2073// of ops, in particular for one-operand ops that may not have the
2074// `getOperand(unsigned)` method.
2075static void
2076generateNamedOperandGetters(const Operator &op, Class &opClass,
2077 Class *genericAdaptorBase, StringRef sizeAttrInit,
2078 StringRef rangeType, StringRef rangeElementType,
2079 StringRef rangeBeginCall, StringRef rangeSizeCall,
2080 StringRef getOperandCallPattern) {
2081 const int numOperands = op.getNumOperands();
2082 const int numVariadicOperands = op.getNumVariableLengthOperands();
2083 const int numNormalOperands = numOperands - numVariadicOperands;
2084
2085 const auto *sameVariadicSize =
2086 op.getTrait(trait: "::mlir::OpTrait::SameVariadicOperandSize");
2087 const auto *attrSizedOperands =
2088 op.getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments");
2089
2090 if (numVariadicOperands > 1 && !sameVariadicSize && !attrSizedOperands) {
2091 PrintFatalError(ErrorLoc: op.getLoc(), Msg: "op has multiple variadic operands but no "
2092 "specification over their sizes");
2093 }
2094
2095 if (numVariadicOperands < 2 && attrSizedOperands) {
2096 PrintFatalError(ErrorLoc: op.getLoc(), Msg: "op must have at least two variadic operands "
2097 "to use 'AttrSizedOperandSegments' trait");
2098 }
2099
2100 if (attrSizedOperands && sameVariadicSize) {
2101 PrintFatalError(ErrorLoc: op.getLoc(),
2102 Msg: "op cannot have both 'AttrSizedOperandSegments' and "
2103 "'SameVariadicOperandSize' traits");
2104 }
2105
2106 // Print the ODS indices of operands so they don't need to be hardcoded in the
2107 // source.
2108 for (int i = 0; i != numOperands; ++i) {
2109 const auto &operand = op.getOperand(index: i);
2110 if (operand.name.empty())
2111 continue;
2112
2113 opClass.declare<Field>(args: "static constexpr int", args: Twine("odsIndex_") +
2114 operand.name + " = " +
2115 Twine(i));
2116 }
2117
2118 // First emit a few "sink" getter methods upon which we layer all nicer named
2119 // getter methods.
2120 // If generating for an adaptor, the method is put into the non-templated
2121 // generic base class, to not require being defined in the header.
2122 // Since the operand size can't be determined from the base class however,
2123 // it has to be passed as an additional argument. The trampoline below
2124 // generates the function with the same signature as the Op in the generic
2125 // adaptor.
2126 bool isGenericAdaptorBase = genericAdaptorBase != nullptr;
2127 generateValueRangeStartAndEnd(
2128 /*opClass=*/isGenericAdaptorBase ? *genericAdaptorBase : opClass,
2129 isGenericAdaptorBase,
2130 /*methodName=*/"getODSOperandIndexAndLength", numVariadic: numVariadicOperands,
2131 numNonVariadic: numNormalOperands, rangeSizeCall, hasAttrSegmentSize: attrSizedOperands, sizeAttrInit,
2132 odsValues: const_cast<Operator &>(op).getOperands());
2133 if (isGenericAdaptorBase) {
2134 // Generate trampoline for calling 'getODSOperandIndexAndLength' with just
2135 // the index. This just calls the implementation in the base class but
2136 // passes the operand size as parameter.
2137 Method *method = opClass.addInlineMethod(
2138 retType: "std::pair<unsigned, unsigned>", name: "getODSOperandIndexAndLength",
2139 args: MethodParameter("unsigned", "index"));
2140 ERROR_IF_PRUNED(method, "getODSOperandIndexAndLength", op);
2141 MethodBody &body = method->body();
2142 body.indent() << formatv(
2143 Fmt: "return Base::getODSOperandIndexAndLength(index, {0});", Vals&: rangeSizeCall);
2144 }
2145
2146 // The implementation of this method is trivial and it is very load-bearing.
2147 // Generate it inline.
2148 auto *m = opClass.addInlineMethod(retType&: rangeType, name: "getODSOperands",
2149 args: MethodParameter("unsigned", "index"));
2150 ERROR_IF_PRUNED(m, "getODSOperands", op);
2151 auto &body = m->body();
2152 if (isGenericAdaptorBase) {
2153 body << formatv(Fmt: valueRangeReturnCode, Vals&: rangeBeginCall,
2154 Vals: "getODSOperandIndexAndLength(index)");
2155 } else {
2156 body << " auto [start, length] = getODSOperandIndexAndLength(index);\n"
2157 " return getOperation()->getOperands().slice(start, length);\n";
2158 }
2159
2160 // Then we emit nicer named getter methods by redirecting to the "sink" getter
2161 // method.
2162 for (int i = 0; i != numOperands; ++i) {
2163 const auto &operand = op.getOperand(index: i);
2164 if (operand.name.empty())
2165 continue;
2166 std::string name = op.getGetterName(name: operand.name);
2167 if (operand.isOptional()) {
2168 m = opClass.addInlineMethod(retType: isGenericAdaptorBase
2169 ? rangeElementType
2170 : generateTypeForGetter(value: operand),
2171 name);
2172 ERROR_IF_PRUNED(m, name, op);
2173 m->body().indent() << formatv(Fmt: "auto operands = getODSOperands({0});\n"
2174 "return operands.empty() ? {1}{{} : ",
2175 Vals&: i, Vals: m->getReturnType());
2176 if (!isGenericAdaptorBase)
2177 m->body() << llvm::formatv(Fmt: "::llvm::cast<{0}>", Vals: m->getReturnType());
2178 m->body() << "(*operands.begin());";
2179 } else if (operand.isVariadicOfVariadic()) {
2180 std::string segmentAttr = op.getGetterName(
2181 name: operand.constraint.getVariadicOfVariadicSegmentSizeAttr());
2182 if (genericAdaptorBase) {
2183 m = opClass.addMethod(retType: "::llvm::SmallVector<" + rangeType + ">", name);
2184 ERROR_IF_PRUNED(m, name, op);
2185 m->body() << llvm::formatv(Fmt: variadicOfVariadicAdaptorCalcCode,
2186 Vals&: segmentAttr, Vals&: i, Vals&: rangeType);
2187 continue;
2188 }
2189
2190 m = opClass.addInlineMethod(retType: "::mlir::OperandRangeRange", name);
2191 ERROR_IF_PRUNED(m, name, op);
2192 m->body() << " return getODSOperands(" << i << ").split(" << segmentAttr
2193 << "Attr());";
2194 } else if (operand.isVariadic()) {
2195 m = opClass.addInlineMethod(retType&: rangeType, name);
2196 ERROR_IF_PRUNED(m, name, op);
2197 m->body() << " return getODSOperands(" << i << ");";
2198 } else {
2199 m = opClass.addInlineMethod(retType: isGenericAdaptorBase
2200 ? rangeElementType
2201 : generateTypeForGetter(value: operand),
2202 name);
2203 ERROR_IF_PRUNED(m, name, op);
2204 m->body().indent() << "return ";
2205 if (!isGenericAdaptorBase)
2206 m->body() << llvm::formatv(Fmt: "::llvm::cast<{0}>", Vals: m->getReturnType());
2207 m->body() << llvm::formatv(Fmt: "(*getODSOperands({0}).begin());", Vals&: i);
2208 }
2209 }
2210}
2211
2212void OpEmitter::genNamedOperandGetters() {
2213 // Build the code snippet used for initializing the operand_segment_size)s
2214 // array.
2215 std::string attrSizeInitCode;
2216 if (op.getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments")) {
2217 attrSizeInitCode = formatv(Fmt: adapterSegmentSizeAttrInitCodeProperties,
2218 Vals: "getProperties().operandSegmentSizes");
2219 }
2220
2221 generateNamedOperandGetters(
2222 op, opClass,
2223 /*genericAdaptorBase=*/nullptr,
2224 /*sizeAttrInit=*/attrSizeInitCode,
2225 /*rangeType=*/"::mlir::Operation::operand_range",
2226 /*rangeElementType=*/"::mlir::Value",
2227 /*rangeBeginCall=*/"getOperation()->operand_begin()",
2228 /*rangeSizeCall=*/"getOperation()->getNumOperands()",
2229 /*getOperandCallPattern=*/"getOperation()->getOperand({0})");
2230}
2231
2232void OpEmitter::genNamedOperandSetters() {
2233 auto *attrSizedOperands =
2234 op.getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments");
2235 for (int i = 0, e = op.getNumOperands(); i != e; ++i) {
2236 const auto &operand = op.getOperand(index: i);
2237 if (operand.name.empty())
2238 continue;
2239 std::string name = op.getGetterName(name: operand.name);
2240
2241 StringRef returnType;
2242 if (operand.isVariadicOfVariadic()) {
2243 returnType = "::mlir::MutableOperandRangeRange";
2244 } else if (operand.isVariableLength()) {
2245 returnType = "::mlir::MutableOperandRange";
2246 } else {
2247 returnType = "::mlir::OpOperand &";
2248 }
2249 bool isVariadicOperand =
2250 operand.isVariadicOfVariadic() || operand.isVariableLength();
2251 auto *m = opClass.addMethod(retType&: returnType, name: name + "Mutable",
2252 properties: isVariadicOperand ? Method::Properties::None
2253 : Method::Properties::Inline);
2254 ERROR_IF_PRUNED(m, name, op);
2255 auto &body = m->body();
2256 body << " auto range = getODSOperandIndexAndLength(" << i << ");\n";
2257
2258 if (!isVariadicOperand) {
2259 // In case of a single operand, return a single OpOperand.
2260 body << " return getOperation()->getOpOperand(range.first);\n";
2261 continue;
2262 }
2263
2264 body << " auto mutableRange = "
2265 "::mlir::MutableOperandRange(getOperation(), "
2266 "range.first, range.second";
2267 if (attrSizedOperands)
2268 body << formatv(Fmt: ", ::mlir::MutableOperandRange::OperandSegment({0}u, "
2269 "{{getOperandSegmentSizesAttrName(), "
2270 "::mlir::DenseI32ArrayAttr::get(getContext(), "
2271 "getProperties().operandSegmentSizes)})",
2272 Vals&: i);
2273 body << ");\n";
2274
2275 // If this operand is a nested variadic, we split the range into a
2276 // MutableOperandRangeRange that provides a range over all of the
2277 // sub-ranges.
2278 if (operand.isVariadicOfVariadic()) {
2279 StringRef segmentAttr =
2280 operand.constraint.getVariadicOfVariadicSegmentSizeAttr();
2281 body << " return mutableRange.split({" << op.getGetterName(name: segmentAttr)
2282 << "AttrName(), " << op.getGetterName(name: segmentAttr) << "Attr()});\n";
2283 } else {
2284 // Otherwise, we use the full range directly.
2285 body << " return mutableRange;\n";
2286 }
2287 }
2288}
2289
2290void OpEmitter::genNamedResultGetters() {
2291 const int numResults = op.getNumResults();
2292 const int numVariadicResults = op.getNumVariableLengthResults();
2293 const int numNormalResults = numResults - numVariadicResults;
2294
2295 // If we have more than one variadic results, we need more complicated logic
2296 // to calculate the value range for each result.
2297
2298 const auto *sameVariadicSize =
2299 op.getTrait(trait: "::mlir::OpTrait::SameVariadicResultSize");
2300 const auto *attrSizedResults =
2301 op.getTrait(trait: "::mlir::OpTrait::AttrSizedResultSegments");
2302
2303 if (numVariadicResults > 1 && !sameVariadicSize && !attrSizedResults) {
2304 PrintFatalError(ErrorLoc: op.getLoc(), Msg: "op has multiple variadic results but no "
2305 "specification over their sizes");
2306 }
2307
2308 if (numVariadicResults < 2 && attrSizedResults) {
2309 PrintFatalError(ErrorLoc: op.getLoc(), Msg: "op must have at least two variadic results "
2310 "to use 'AttrSizedResultSegments' trait");
2311 }
2312
2313 if (attrSizedResults && sameVariadicSize) {
2314 PrintFatalError(ErrorLoc: op.getLoc(),
2315 Msg: "op cannot have both 'AttrSizedResultSegments' and "
2316 "'SameVariadicResultSize' traits");
2317 }
2318
2319 // Print the ODS indices of results so they don't need to be hardcoded in the
2320 // source.
2321 for (int i = 0; i != numResults; ++i) {
2322 const auto &result = op.getResult(index: i);
2323 if (result.name.empty())
2324 continue;
2325
2326 opClass.declare<Field>(args: "static constexpr int",
2327 args: Twine("odsIndex_") + result.name + " = " + Twine(i));
2328 }
2329
2330 // Build the initializer string for the result segment size attribute.
2331 std::string attrSizeInitCode;
2332 if (attrSizedResults) {
2333 attrSizeInitCode = formatv(Fmt: adapterSegmentSizeAttrInitCodeProperties,
2334 Vals: "getProperties().resultSegmentSizes");
2335 }
2336
2337 generateValueRangeStartAndEnd(
2338 opClass, /*isGenericAdaptorBase=*/false, methodName: "getODSResultIndexAndLength",
2339 numVariadic: numVariadicResults, numNonVariadic: numNormalResults, rangeSizeCall: "getOperation()->getNumResults()",
2340 hasAttrSegmentSize: attrSizedResults, sizeAttrInit: attrSizeInitCode, odsValues: op.getResults());
2341
2342 // The implementation of this method is trivial and it is very load-bearing.
2343 // Generate it inline.
2344 auto *m = opClass.addInlineMethod(retType: "::mlir::Operation::result_range",
2345 name: "getODSResults",
2346 args: MethodParameter("unsigned", "index"));
2347 ERROR_IF_PRUNED(m, "getODSResults", op);
2348 m->body() << formatv(Fmt: valueRangeReturnCode, Vals: "getOperation()->result_begin()",
2349 Vals: "getODSResultIndexAndLength(index)");
2350
2351 for (int i = 0; i != numResults; ++i) {
2352 const auto &result = op.getResult(index: i);
2353 if (result.name.empty())
2354 continue;
2355 std::string name = op.getGetterName(name: result.name);
2356 if (result.isOptional()) {
2357 m = opClass.addInlineMethod(retType: generateTypeForGetter(value: result), name);
2358 ERROR_IF_PRUNED(m, name, op);
2359 m->body() << " auto results = getODSResults(" << i << ");\n"
2360 << llvm::formatv(Fmt: " return results.empty()"
2361 " ? {0}()"
2362 " : ::llvm::cast<{0}>(*results.begin());",
2363 Vals: m->getReturnType());
2364 } else if (result.isVariadic()) {
2365 m = opClass.addInlineMethod(retType: "::mlir::Operation::result_range", name);
2366 ERROR_IF_PRUNED(m, name, op);
2367 m->body() << " return getODSResults(" << i << ");";
2368 } else {
2369 m = opClass.addInlineMethod(retType: generateTypeForGetter(value: result), name);
2370 ERROR_IF_PRUNED(m, name, op);
2371 m->body() << llvm::formatv(
2372 Fmt: " return ::llvm::cast<{0}>(*getODSResults({1}).begin());",
2373 Vals: m->getReturnType(), Vals&: i);
2374 }
2375 }
2376}
2377
2378void OpEmitter::genNamedRegionGetters() {
2379 unsigned numRegions = op.getNumRegions();
2380 for (unsigned i = 0; i < numRegions; ++i) {
2381 const auto &region = op.getRegion(index: i);
2382 if (region.name.empty())
2383 continue;
2384 std::string name = op.getGetterName(name: region.name);
2385
2386 // Generate the accessors for a variadic region.
2387 if (region.isVariadic()) {
2388 auto *m = opClass.addInlineMethod(
2389 retType: "::mlir::MutableArrayRef<::mlir::Region>", name);
2390 ERROR_IF_PRUNED(m, name, op);
2391 m->body() << formatv(Fmt: " return (*this)->getRegions().drop_front({0});",
2392 Vals&: i);
2393 continue;
2394 }
2395
2396 auto *m = opClass.addInlineMethod(retType: "::mlir::Region &", name);
2397 ERROR_IF_PRUNED(m, name, op);
2398 m->body() << formatv(Fmt: " return (*this)->getRegion({0});", Vals&: i);
2399 }
2400}
2401
2402void OpEmitter::genNamedSuccessorGetters() {
2403 unsigned numSuccessors = op.getNumSuccessors();
2404 for (unsigned i = 0; i < numSuccessors; ++i) {
2405 const NamedSuccessor &successor = op.getSuccessor(index: i);
2406 if (successor.name.empty())
2407 continue;
2408 std::string name = op.getGetterName(name: successor.name);
2409 // Generate the accessors for a variadic successor list.
2410 if (successor.isVariadic()) {
2411 auto *m = opClass.addInlineMethod(retType: "::mlir::SuccessorRange", name);
2412 ERROR_IF_PRUNED(m, name, op);
2413 m->body() << formatv(
2414 Fmt: " return {std::next((*this)->successor_begin(), {0}), "
2415 "(*this)->successor_end()};",
2416 Vals&: i);
2417 continue;
2418 }
2419
2420 auto *m = opClass.addInlineMethod(retType: "::mlir::Block *", name);
2421 ERROR_IF_PRUNED(m, name, op);
2422 m->body() << formatv(Fmt: " return (*this)->getSuccessor({0});", Vals&: i);
2423 }
2424}
2425
2426static bool canGenerateUnwrappedBuilder(const Operator &op) {
2427 // If this op does not have native attributes at all, return directly to avoid
2428 // redefining builders.
2429 if (op.getNumNativeAttributes() == 0)
2430 return false;
2431
2432 bool canGenerate = false;
2433 // We are generating builders that take raw values for attributes. We need to
2434 // make sure the native attributes have a meaningful "unwrapped" value type
2435 // different from the wrapped mlir::Attribute type to avoid redefining
2436 // builders. This checks for the op has at least one such native attribute.
2437 for (int i = 0, e = op.getNumNativeAttributes(); i < e; ++i) {
2438 const NamedAttribute &namedAttr = op.getAttribute(index: i);
2439 if (canUseUnwrappedRawValue(attr: namedAttr.attr)) {
2440 canGenerate = true;
2441 break;
2442 }
2443 }
2444 return canGenerate;
2445}
2446
2447static bool canInferType(const Operator &op) {
2448 return op.getTrait(trait: "::mlir::InferTypeOpInterface::Trait");
2449}
2450
2451void OpEmitter::genInlineCreateBody(
2452 const SmallVector<MethodParameter> &paramList, bool deprecated) {
2453 SmallVector<MethodParameter> createParamListOpBuilder;
2454 SmallVector<MethodParameter> createParamListImplicitLocOpBuilder;
2455 SmallVector<llvm::StringRef, 4> nonBuilderStateArgsList;
2456 createParamListOpBuilder.emplace_back(Args: "::mlir::OpBuilder &", Args: "builder");
2457 createParamListImplicitLocOpBuilder.emplace_back(
2458 Args: "::mlir::ImplicitLocOpBuilder &", Args: "builder");
2459 std::string locParamName = "location";
2460 while (llvm::find_if(Range: paramList, P: [&locParamName](const MethodParameter &p) {
2461 return p.getName() == locParamName;
2462 }) != paramList.end()) {
2463 locParamName += "_";
2464 }
2465 createParamListOpBuilder.emplace_back(Args: "::mlir::Location", Args&: locParamName);
2466
2467 for (auto &param : paramList) {
2468 if (param.getType() == "::mlir::OpBuilder &" ||
2469 param.getType() == "::mlir::OperationState &")
2470 continue;
2471 createParamListOpBuilder.emplace_back(Args: param.getType(), Args: param.getName(),
2472 Args: param.getDefaultValue(),
2473 Args: param.isOptional());
2474 createParamListImplicitLocOpBuilder.emplace_back(
2475 Args: param.getType(), Args: param.getName(), Args: param.getDefaultValue(),
2476 Args: param.isOptional());
2477 nonBuilderStateArgsList.push_back(Elt: param.getName());
2478 }
2479 auto *cWithLoc = opClass.addStaticMethod(retType: opClass.getClassName(), name: "create",
2480 args&: createParamListOpBuilder);
2481 auto *cImplicitLoc = opClass.addStaticMethod(
2482 retType: opClass.getClassName(), name: "create", args&: createParamListImplicitLocOpBuilder);
2483 if (deprecated) {
2484 if (cWithLoc)
2485 cWithLoc->setDeprecated(legacyBuilderDeprecation);
2486 if (cImplicitLoc)
2487 cImplicitLoc->setDeprecated(legacyBuilderDeprecation);
2488 }
2489 std::string nonBuilderStateArgs = "";
2490 if (!nonBuilderStateArgsList.empty()) {
2491 llvm::raw_string_ostream nonBuilderStateArgsOS(nonBuilderStateArgs);
2492 interleave(
2493 c: nonBuilderStateArgsList,
2494 each_fn: [&](StringRef name) {
2495 nonBuilderStateArgsOS << "std::forward<decltype(" << name << ")>("
2496 << name << ')';
2497 },
2498 between_fn: [&] { nonBuilderStateArgsOS << ", "; });
2499
2500 nonBuilderStateArgs = ", " + nonBuilderStateArgs;
2501 }
2502 // MSVC diagnoses deprecated calls even inside deprecated functions.
2503 // Keep compatibility wrappers quiet without hiding warnings at call sites.
2504 if (cWithLoc) {
2505 if (deprecated)
2506 cWithLoc->body() << "LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH\n";
2507 cWithLoc->body() << llvm::formatv(Fmt: inlineCreateBody, Vals&: locParamName,
2508 Vals&: nonBuilderStateArgs,
2509 Vals: opClass.getClassName());
2510 if (deprecated)
2511 cWithLoc->body() << "LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP\n";
2512 }
2513 if (cImplicitLoc) {
2514 if (deprecated)
2515 cImplicitLoc->body() << "LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_PUSH\n";
2516 cImplicitLoc->body() << llvm::formatv(Fmt: inlineCreateBodyImplicitLoc,
2517 Vals&: nonBuilderStateArgs);
2518 if (deprecated)
2519 cImplicitLoc->body() << "LLVM_SUPPRESS_DEPRECATED_DECLARATIONS_POP\n";
2520 }
2521}
2522
2523void OpEmitter::genSeparateArgParamBuilder() {
2524 SmallVector<AttrParamKind, 2> attrBuilderType;
2525 attrBuilderType.push_back(Elt: AttrParamKind::WrappedAttr);
2526 if (canGenerateUnwrappedBuilder(op))
2527 attrBuilderType.push_back(Elt: AttrParamKind::UnwrappedValue);
2528
2529 // Emit with separate builders with or without unwrapped attributes and/or
2530 // inferring result type.
2531 auto emit = [&](AttrParamKind attrType, TypeParamKind paramKind,
2532 bool inferType) {
2533 SmallVector<MethodParameter> paramList;
2534 SmallVector<std::string, 4> resultNames;
2535 llvm::StringSet<> inferredAttributes;
2536 buildParamList(paramList, inferredAttributes, resultTypeNames&: resultNames, typeParamKind: paramKind,
2537 attrParamKind: attrType);
2538
2539 auto *m = opClass.addStaticMethod(retType: "void", name: "build", args&: paramList);
2540 // If the builder is redundant, skip generating the method.
2541 if (!m)
2542 return;
2543 genInlineCreateBody(paramList);
2544
2545 auto &body = m->body();
2546 // The no-result builder can reuse the collective builder. Both forms
2547 // otherwise emit identical operand, attribute, and region handling.
2548 if (paramKind == TypeParamKind::Separate && op.getNumResults() == 0) {
2549 body << " build(odsBuilder, odsState, ::mlir::TypeRange{}";
2550 for (const MethodParameter &param : llvm::drop_begin(RangeOrContainer&: paramList, N: 2))
2551 body << ", " << param.getName();
2552 body << ");\n";
2553 return;
2554 }
2555 genCodeForAddingArgAndRegionForBuilder(body, inferredAttributes,
2556 /*isRawValueAttr=*/attrType ==
2557 AttrParamKind::UnwrappedValue);
2558
2559 // Push all result types to the operation state
2560
2561 if (inferType) {
2562 // Generate builder that infers type too.
2563 // TODO: Subsume this with general checking if type can be
2564 // inferred automatically.
2565 body << formatv(Fmt: R"(
2566 ::llvm::SmallVector<::mlir::Type, 2> inferredReturnTypes;
2567 if (::mlir::succeeded({0}::inferReturnTypes(odsBuilder.getContext(),
2568 {1}.location, {1}.operands,
2569 {1}.attributes.getDictionary({1}.getContext()),
2570 {1}.getRawProperties(),
2571 {1}.regions, inferredReturnTypes)))
2572 {1}.addTypes(inferredReturnTypes);
2573 else
2574 ::mlir::detail::reportFatalInferReturnTypesError({1});
2575 )",
2576 Vals: opClass.getClassName(), Vals: builderOpState);
2577 return;
2578 }
2579
2580 switch (paramKind) {
2581 case TypeParamKind::None:
2582 return;
2583 case TypeParamKind::Separate:
2584 for (int i = 0, e = op.getNumResults(); i < e; ++i) {
2585 if (op.getResult(index: i).isOptional())
2586 body << " if (" << resultNames[i] << ")\n ";
2587 body << " " << builderOpState << ".addTypes(" << resultNames[i]
2588 << ");\n";
2589 }
2590
2591 // Automatically create the 'resultSegmentSizes' attribute using
2592 // the length of the type ranges.
2593 if (op.getTrait(trait: "::mlir::OpTrait::AttrSizedResultSegments")) {
2594 body << " ::llvm::copy(::llvm::ArrayRef<int32_t>({";
2595 interleaveComma(
2596 c: llvm::seq<int>(Begin: 0, End: op.getNumResults()), os&: body, each_fn: [&](int i) {
2597 const NamedTypeConstraint &result = op.getResult(index: i);
2598 if (!result.isVariableLength()) {
2599 body << "1";
2600 } else if (result.isOptional()) {
2601 body << "(" << resultNames[i] << " ? 1 : 0)";
2602 } else {
2603 // VariadicOfVariadic of results are currently unsupported in
2604 // MLIR, hence it can only be a simple variadic.
2605 // TODO: Add implementation for VariadicOfVariadic results here
2606 // once supported.
2607 assert(result.isVariadic());
2608 body << "static_cast<int32_t>(" << resultNames[i] << ".size())";
2609 }
2610 });
2611 body << "}), " << builderOpStateProperties
2612 << ".resultSegmentSizes.begin());\n";
2613 }
2614
2615 return;
2616 case TypeParamKind::Collective: {
2617 int numResults = op.getNumResults();
2618 int numVariadicResults = op.getNumVariableLengthResults();
2619 int numNonVariadicResults = numResults - numVariadicResults;
2620 bool hasVariadicResult = numVariadicResults != 0;
2621
2622 // Avoid emitting "resultTypes.size() >= 0u" which is always true.
2623 if (!hasVariadicResult || numNonVariadicResults != 0)
2624 body << " " << "assert(resultTypes.size() "
2625 << (hasVariadicResult ? ">=" : "==") << " "
2626 << numNonVariadicResults
2627 << "u && \"mismatched number of results\");\n";
2628 body << " " << builderOpState << ".addTypes(resultTypes);\n";
2629 }
2630 return;
2631 }
2632 llvm_unreachable("unhandled TypeParamKind");
2633 };
2634
2635 // Some of the build methods generated here may be ambiguous, but TableGen's
2636 // ambiguous function detection will elide those ones.
2637 for (auto attrType : attrBuilderType) {
2638 emit(attrType, TypeParamKind::Separate, /*inferType=*/false);
2639 if (canInferType(op))
2640 emit(attrType, TypeParamKind::None, /*inferType=*/true);
2641 emit(attrType, TypeParamKind::Collective, /*inferType=*/false);
2642 }
2643}
2644
2645void OpEmitter::genLegacyPropertiesBuilderHelper() {
2646 if (!emitHelper.hasNonEmptyPropertiesStruct())
2647 return;
2648
2649 SmallVector<StringRef> inherentNames = op.getInherentAttrNames();
2650
2651 auto *method = opClass.addStaticMethod<Method::Private>(
2652 retType: "void", name: "buildPropertiesAndDiscardableAttributes",
2653 args: MethodParameter("::mlir::OperationState &", builderOpState),
2654 args: MethodParameter("::llvm::ArrayRef<::mlir::NamedAttribute>",
2655 "attributes"));
2656 ERROR_IF_PRUNED(method, "buildPropertiesAndDiscardableAttributes", op);
2657 MethodBody &body = method->body();
2658 body << " Properties &properties = " << builderOpStateProperties << ";\n"
2659 << " populateDefaultProperties(" << builderOpState
2660 << ".name, properties);\n";
2661 if (!inherentNames.empty()) {
2662 body << " const ::llvm::StringRef inherentNames[] = {";
2663 llvm::interleaveComma(c: inherentNames, os&: body,
2664 each_fn: [&](StringRef name) { body << '"' << name << '"'; });
2665 body << "};\n";
2666 }
2667 body << " ::mlir::detail::splitPropertiesAndDiscardableAttributes(\n"
2668 << " " << builderOpState << ", attributes, "
2669 << (inherentNames.empty() ? "::llvm::ArrayRef<::llvm::StringRef>{}"
2670 : "inherentNames")
2671 << ", [&](::mlir::DictionaryAttr inherentAttrs) {\n"
2672 << " return setPropertiesFromAttr(properties, inherentAttrs,\n"
2673 << " [&]() { return ::mlir::emitError(" << builderOpState
2674 << ".location); });\n"
2675 << " });\n";
2676}
2677
2678void OpEmitter::genCodeForAddingPropertiesAndAttributes(
2679 MethodBody &body, CollectiveBuilderKind kind, StringRef attributesName) {
2680 if (kind == CollectiveBuilderKind::PropStruct) {
2681 body << " " << builderOpState
2682 << ".useProperties(const_cast<Properties&>(properties));\n"
2683 << " " << builderOpState << ".addAttributes(" << attributesName
2684 << ");\n";
2685 return;
2686 }
2687 if (emitHelper.hasNonEmptyPropertiesStruct()) {
2688 body << " buildPropertiesAndDiscardableAttributes(" << builderOpState
2689 << ", " << attributesName << ");\n";
2690 return;
2691 }
2692 body << " " << builderOpState << ".addAttributes(" << attributesName
2693 << ");\n";
2694}
2695
2696void OpEmitter::genUseOperandAsResultTypeCollectiveParamBuilder(
2697 CollectiveBuilderKind kind) {
2698 int numResults = op.getNumResults();
2699
2700 // Signature
2701 SmallVector<MethodParameter> paramList;
2702 paramList.emplace_back(Args: "::mlir::OpBuilder &", Args: "odsBuilder");
2703 paramList.emplace_back(Args: "::mlir::OperationState &", Args: builderOpState);
2704 paramList.emplace_back(Args: "::mlir::ValueRange", Args: "operands");
2705 if (kind == CollectiveBuilderKind::PropStruct)
2706 paramList.emplace_back(Args: "const Properties &", Args: "properties");
2707 // Provide default value for `attributes` when its the last parameter
2708 StringRef attributesDefaultValue = op.getNumVariadicRegions() ? "" : "{}";
2709 StringRef attributesName = kind == CollectiveBuilderKind::PropStruct
2710 ? "discardableAttributes"
2711 : "attributes";
2712 paramList.emplace_back(Args: "::llvm::ArrayRef<::mlir::NamedAttribute>",
2713 Args&: attributesName, Args&: attributesDefaultValue);
2714 if (op.getNumVariadicRegions())
2715 paramList.emplace_back(Args: "unsigned", Args: "numRegions");
2716
2717 auto *m = opClass.addStaticMethod(retType: "void", name: "build", args&: paramList);
2718 // If the builder is redundant, skip generating the method
2719 if (!m)
2720 return;
2721 bool deprecated = kind == CollectiveBuilderKind::AttrDict &&
2722 emitHelper.hasNonEmptyPropertiesStruct();
2723 if (deprecated)
2724 m->setDeprecated(legacyBuilderDeprecation);
2725 genInlineCreateBody(paramList, deprecated);
2726 auto &body = m->body();
2727
2728 // Operands
2729 body << " " << builderOpState << ".addOperands(operands);\n";
2730
2731 genCodeForAddingPropertiesAndAttributes(body, kind, attributesName);
2732
2733 // Create the correct number of regions
2734 if (int numRegions = op.getNumRegions()) {
2735 body << llvm::formatv(
2736 Fmt: " for (unsigned i = 0; i != {0}; ++i)\n",
2737 Vals: (op.getNumVariadicRegions() ? "numRegions" : Twine(numRegions)));
2738 body << " (void)" << builderOpState << ".addRegion();\n";
2739 }
2740
2741 // Result types
2742 SmallVector<std::string, 2> resultTypes(numResults, "operands[0].getType()");
2743 body << " " << builderOpState << ".addTypes({"
2744 << llvm::join(R&: resultTypes, Separator: ", ") << "});\n\n";
2745}
2746
2747void OpEmitter::genPopulateDefaultAttributes() {
2748 // All done if no attributes, except optional ones, have default values.
2749 if (llvm::all_of(Range: op.getAttributes(), P: [](const NamedAttribute &named) {
2750 return !named.attr.hasDefaultValue() || named.attr.isOptional();
2751 }))
2752 return;
2753
2754 SmallVector<MethodParameter> paramList;
2755 paramList.emplace_back(Args: "::mlir::OperationName", Args: "opName");
2756 paramList.emplace_back(Args: "Properties &", Args: "properties");
2757 auto *m =
2758 opClass.addStaticMethod(retType: "void", name: "populateDefaultProperties", args&: paramList);
2759 ERROR_IF_PRUNED(m, "populateDefaultProperties", op);
2760 auto &body = m->body();
2761 body.indent();
2762 body << "::mlir::Builder " << odsBuilder << "(opName.getContext());\n";
2763 for (const NamedAttribute &namedAttr : op.getAttributes()) {
2764 auto &attr = namedAttr.attr;
2765 if (!attr.hasDefaultValue() || attr.isOptional())
2766 continue;
2767 StringRef name = namedAttr.name;
2768 FmtContext fctx;
2769 fctx.withBuilder(subst: odsBuilder);
2770 body << "if (!properties." << name << ")\n"
2771 << " properties." << name << " = "
2772 << std::string(tgfmt(fmt: attr.getConstBuilderTemplate(), ctx: &fctx,
2773 vals: tgfmt(fmt: attr.getDefaultValue(), ctx: &fctx)))
2774 << ";\n";
2775 }
2776}
2777
2778void OpEmitter::genInferredTypeCollectiveParamBuilder(
2779 CollectiveBuilderKind kind) {
2780 SmallVector<MethodParameter> paramList;
2781 paramList.emplace_back(Args: "::mlir::OpBuilder &", Args: "odsBuilder");
2782 paramList.emplace_back(Args: "::mlir::OperationState &", Args: builderOpState);
2783 paramList.emplace_back(Args: "::mlir::ValueRange", Args: "operands");
2784 if (kind == CollectiveBuilderKind::PropStruct)
2785 paramList.emplace_back(Args: "const Properties &", Args: "properties");
2786 StringRef attributesDefaultValue = op.getNumVariadicRegions() ? "" : "{}";
2787 StringRef attributesName = kind == CollectiveBuilderKind::PropStruct
2788 ? "discardableAttributes"
2789 : "attributes";
2790 paramList.emplace_back(Args: "::llvm::ArrayRef<::mlir::NamedAttribute>",
2791 Args&: attributesName, Args&: attributesDefaultValue);
2792 if (op.getNumVariadicRegions())
2793 paramList.emplace_back(Args: "unsigned", Args: "numRegions");
2794
2795 auto *m = opClass.addStaticMethod(retType: "void", name: "build", args&: paramList);
2796 // If the builder is redundant, skip generating the method
2797 if (!m)
2798 return;
2799 bool deprecated = kind == CollectiveBuilderKind::AttrDict &&
2800 emitHelper.hasNonEmptyPropertiesStruct();
2801 if (deprecated)
2802 m->setDeprecated(legacyBuilderDeprecation);
2803 genInlineCreateBody(paramList, deprecated);
2804 auto &body = m->body();
2805
2806 int numResults = op.getNumResults();
2807 int numVariadicResults = op.getNumVariableLengthResults();
2808 int numNonVariadicResults = numResults - numVariadicResults;
2809
2810 int numOperands = op.getNumOperands();
2811 int numVariadicOperands = op.getNumVariableLengthOperands();
2812 int numNonVariadicOperands = numOperands - numVariadicOperands;
2813
2814 // Operands
2815 if (numVariadicOperands == 0 || numNonVariadicOperands != 0)
2816 body << " assert(operands.size()"
2817 << (numVariadicOperands != 0 ? " >= " : " == ")
2818 << numNonVariadicOperands
2819 << "u && \"mismatched number of parameters\");\n";
2820 body << " " << builderOpState << ".addOperands(operands);\n";
2821 genCodeForAddingPropertiesAndAttributes(body, kind, attributesName);
2822
2823 // Create the correct number of regions
2824 if (int numRegions = op.getNumRegions()) {
2825 body << llvm::formatv(
2826 Fmt: " for (unsigned i = 0; i != {0}; ++i)\n",
2827 Vals: (op.getNumVariadicRegions() ? "numRegions" : Twine(numRegions)));
2828 body << " (void)" << builderOpState << ".addRegion();\n";
2829 }
2830
2831 // Result types
2832 body << formatv(Fmt: R"(
2833 ::llvm::SmallVector<::mlir::Type, 2> inferredReturnTypes;
2834 if (::mlir::succeeded({0}::inferReturnTypes(odsBuilder.getContext(),
2835 {1}.location, operands,
2836 {1}.attributes.getDictionary({1}.getContext()),
2837 {1}.getRawProperties(),
2838 {1}.regions, inferredReturnTypes))) {{)",
2839 Vals: opClass.getClassName(), Vals: builderOpState);
2840 if (numVariadicResults == 0 || numNonVariadicResults != 0)
2841 body << "\n assert(inferredReturnTypes.size()"
2842 << (numVariadicResults != 0 ? " >= " : " == ") << numNonVariadicResults
2843 << "u && \"mismatched number of return types\");";
2844 body << "\n " << builderOpState << ".addTypes(inferredReturnTypes);";
2845
2846 body << R"(
2847 } else {
2848 ::llvm::report_fatal_error("Failed to infer result type(s).");
2849 })";
2850}
2851
2852void OpEmitter::genUseOperandAsResultTypeSeparateParamBuilder() {
2853 auto emit = [&](AttrParamKind attrType) {
2854 SmallVector<MethodParameter> paramList;
2855 SmallVector<std::string, 4> resultNames;
2856 llvm::StringSet<> inferredAttributes;
2857 buildParamList(paramList, inferredAttributes, resultTypeNames&: resultNames,
2858 typeParamKind: TypeParamKind::None, attrParamKind: attrType);
2859
2860 auto *m = opClass.addStaticMethod(retType: "void", name: "build", args&: paramList);
2861 // If the builder is redundant, skip generating the method
2862 if (!m)
2863 return;
2864 genInlineCreateBody(paramList);
2865 auto &body = m->body();
2866 genCodeForAddingArgAndRegionForBuilder(body, inferredAttributes,
2867 /*isRawValueAttr=*/attrType ==
2868 AttrParamKind::UnwrappedValue);
2869
2870 auto numResults = op.getNumResults();
2871 if (numResults == 0)
2872 return;
2873
2874 // Push all result types to the operation state
2875 const char *index = op.getOperand(index: 0).isVariadic() ? ".front()" : "";
2876 std::string resultType =
2877 formatv(Fmt: "{0}{1}.getType()", Vals: getArgumentName(op, index: 0), Vals&: index).str();
2878 body << " " << builderOpState << ".addTypes({" << resultType;
2879 for (int i = 1; i != numResults; ++i)
2880 body << ", " << resultType;
2881 body << "});\n\n";
2882 };
2883
2884 emit(AttrParamKind::WrappedAttr);
2885 // Generate additional builder(s) if any attributes can be "unwrapped"
2886 if (canGenerateUnwrappedBuilder(op))
2887 emit(AttrParamKind::UnwrappedValue);
2888}
2889
2890void OpEmitter::genUseAttrAsResultTypeCollectiveParamBuilder(
2891 CollectiveBuilderKind kind) {
2892 SmallVector<MethodParameter> paramList;
2893 paramList.emplace_back(Args: "::mlir::OpBuilder &", Args: "odsBuilder");
2894 paramList.emplace_back(Args: "::mlir::OperationState &", Args: builderOpState);
2895 paramList.emplace_back(Args: "::mlir::ValueRange", Args: "operands");
2896 if (kind == CollectiveBuilderKind::PropStruct)
2897 paramList.emplace_back(Args: "const Properties &", Args: "properties");
2898 StringRef attributesName = kind == CollectiveBuilderKind::PropStruct
2899 ? "discardableAttributes"
2900 : "attributes";
2901 paramList.emplace_back(Args: "::llvm::ArrayRef<::mlir::NamedAttribute>",
2902 Args&: attributesName, Args: "{}");
2903 auto *m = opClass.addStaticMethod(retType: "void", name: "build", args&: paramList);
2904 // If the builder is redundant, skip generating the method
2905 if (!m)
2906 return;
2907 bool deprecated = kind == CollectiveBuilderKind::AttrDict &&
2908 emitHelper.hasNonEmptyPropertiesStruct();
2909 if (deprecated)
2910 m->setDeprecated(legacyBuilderDeprecation);
2911 genInlineCreateBody(paramList, deprecated);
2912
2913 auto &body = m->body();
2914
2915 // Push all result types to the operation state
2916 std::string resultType;
2917 const auto &namedAttr = op.getAttribute(index: 0);
2918
2919 if (namedAttr.attr.isTypeAttr()) {
2920 resultType = "::llvm::cast<::mlir::TypeAttr>(typeAttr).getValue()";
2921 } else {
2922 resultType = "::llvm::cast<::mlir::TypedAttr>(typeAttr).getType()";
2923 }
2924
2925 if (kind == CollectiveBuilderKind::PropStruct) {
2926 body << " ::mlir::Attribute typeAttr = properties."
2927 << op.getGetterName(name: namedAttr.name) << "();\n";
2928 } else if (emitHelper.hasNonEmptyPropertiesStruct()) {
2929 genCodeForAddingPropertiesAndAttributes(body, kind, attributesName);
2930 body << " ::mlir::Attribute typeAttr = " << builderOpStateProperties << "."
2931 << op.getGetterName(name: namedAttr.name) << "();\n";
2932 } else {
2933 body << " ::mlir::Attribute typeAttr;\n"
2934 << " auto attrName = " << op.getGetterName(name: namedAttr.name)
2935 << "AttrName(" << builderOpState
2936 << ".name);\n"
2937 " for (auto attr : attributes) {\n"
2938 " if (attr.getName() == attrName) {\n"
2939 " typeAttr = attr.getValue();\n"
2940 " break;\n"
2941 " }\n"
2942 " }\n";
2943 }
2944
2945 // Operands
2946 body << " " << builderOpState << ".addOperands(operands);\n";
2947
2948 if (kind == CollectiveBuilderKind::PropStruct ||
2949 !emitHelper.hasNonEmptyPropertiesStruct())
2950 genCodeForAddingPropertiesAndAttributes(body, kind, attributesName);
2951
2952 // Result types
2953 SmallVector<std::string, 2> resultTypes(op.getNumResults(), resultType);
2954 body << " " << builderOpState << ".addTypes({"
2955 << llvm::join(R&: resultTypes, Separator: ", ") << "});\n";
2956}
2957
2958/// Returns a signature of the builder. Updates the context `fctx` to enable
2959/// replacement of $_builder and $_state in the body.
2960static SmallVector<MethodParameter>
2961getBuilderSignature(const Builder &builder) {
2962 ArrayRef<Builder::Parameter> params(builder.getParameters());
2963
2964 // Inject builder and state arguments.
2965 SmallVector<MethodParameter> arguments;
2966 arguments.reserve(N: params.size() + 2);
2967 arguments.emplace_back(Args: "::mlir::OpBuilder &", Args: odsBuilder);
2968 arguments.emplace_back(Args: "::mlir::OperationState &", Args: builderOpState);
2969
2970 FmtContext fctx;
2971 fctx.withBuilder(subst: odsBuilder);
2972
2973 for (unsigned i = 0, e = params.size(); i < e; ++i) {
2974 // If no name is provided, generate one.
2975 std::optional<StringRef> paramName = params[i].getName();
2976 std::string name =
2977 paramName ? paramName->str() : "odsArg" + std::to_string(val: i);
2978
2979 StringRef defaultValue;
2980 if (std::optional<StringRef> defaultParamValue =
2981 params[i].getDefaultValue())
2982 defaultValue = *defaultParamValue;
2983
2984 arguments.emplace_back(Args: params[i].getCppType(), Args: std::move(name),
2985 Args: tgfmt(fmt: defaultValue, ctx: &fctx));
2986 }
2987
2988 return arguments;
2989}
2990
2991void OpEmitter::genBuilder() {
2992 // Handle custom builders if provided.
2993 for (const Builder &builder : op.getBuilders()) {
2994 SmallVector<MethodParameter> arguments = getBuilderSignature(builder);
2995
2996 std::optional<StringRef> body = builder.getBody();
2997 auto properties = body ? Method::Static : Method::StaticDeclaration;
2998 auto *method = opClass.addMethod(retType: "void", name: "build", properties, args&: arguments);
2999
3000 ERROR_IF_PRUNED(method, "build", op);
3001
3002 if (method)
3003 method->setDeprecated(builder.getDeprecatedMessage());
3004
3005 FmtContext fctx;
3006 fctx.withBuilder(subst: odsBuilder);
3007 fctx.addSubst(placeholder: "_state", subst: builderOpState);
3008 if (body)
3009 method->body() << tgfmt(fmt: *body, ctx: &fctx);
3010 genInlineCreateBody(paramList: arguments);
3011 }
3012
3013 // Generate default builders that requires all result type, operands, and
3014 // attributes as parameters.
3015 if (op.skipDefaultBuilders())
3016 return;
3017
3018 genLegacyPropertiesBuilderHelper();
3019
3020 // We generate three classes of builders here:
3021 // 1. one having a stand-alone parameter for each operand / attribute, and
3022 genSeparateArgParamBuilder();
3023 // 2. one having an aggregated parameter for all result types / operands /
3024 // [properties / discardable] attributes, and
3025 genCollectiveParamBuilder(kind: CollectiveBuilderKind::AttrDict);
3026 genCollectiveParamBuilder(kind: CollectiveBuilderKind::PropStruct);
3027 // 3. one having a stand-alone parameter for each operand and attribute,
3028 // use the first operand or attribute's type as all result types
3029 // to facilitate different call patterns.
3030 if (op.getNumVariableLengthResults() == 0) {
3031 if (op.getTrait(trait: "::mlir::OpTrait::SameOperandsAndResultType")) {
3032 genUseOperandAsResultTypeSeparateParamBuilder();
3033 genUseOperandAsResultTypeCollectiveParamBuilder(
3034 kind: CollectiveBuilderKind::AttrDict);
3035 genUseOperandAsResultTypeCollectiveParamBuilder(
3036 kind: CollectiveBuilderKind::PropStruct);
3037 }
3038 if (op.getTrait(trait: "::mlir::OpTrait::FirstAttrDerivedResultType")) {
3039 genUseAttrAsResultTypeCollectiveParamBuilder(
3040 kind: CollectiveBuilderKind::AttrDict);
3041 genUseAttrAsResultTypeCollectiveParamBuilder(
3042 kind: CollectiveBuilderKind::PropStruct);
3043 }
3044 }
3045}
3046
3047void OpEmitter::genCollectiveParamBuilder(CollectiveBuilderKind kind) {
3048 int numResults = op.getNumResults();
3049 int numVariadicResults = op.getNumVariableLengthResults();
3050 int numNonVariadicResults = numResults - numVariadicResults;
3051
3052 int numOperands = op.getNumOperands();
3053 int numVariadicOperands = op.getNumVariableLengthOperands();
3054 int numNonVariadicOperands = numOperands - numVariadicOperands;
3055
3056 SmallVector<MethodParameter> paramList;
3057 paramList.emplace_back(Args: "::mlir::OpBuilder &", Args: "");
3058 paramList.emplace_back(Args: "::mlir::OperationState &", Args: builderOpState);
3059 paramList.emplace_back(Args: "::mlir::TypeRange", Args: "resultTypes");
3060 paramList.emplace_back(Args: "::mlir::ValueRange", Args: "operands");
3061 if (kind == CollectiveBuilderKind::PropStruct)
3062 paramList.emplace_back(Args: "const Properties &", Args: "properties");
3063 // Provide default value for `attributes` when its the last parameter
3064 StringRef attributesDefaultValue = op.getNumVariadicRegions() ? "" : "{}";
3065 StringRef attributesName = kind == CollectiveBuilderKind::PropStruct
3066 ? "discardableAttributes"
3067 : "attributes";
3068 paramList.emplace_back(Args: "::llvm::ArrayRef<::mlir::NamedAttribute>",
3069 Args&: attributesName, Args&: attributesDefaultValue);
3070 if (op.getNumVariadicRegions())
3071 paramList.emplace_back(Args: "unsigned", Args: "numRegions");
3072
3073 auto *m = opClass.addStaticMethod(retType: "void", name: "build", args&: paramList);
3074 // If the builder is redundant, skip generating the method
3075 if (!m)
3076 return;
3077 bool deprecated = kind == CollectiveBuilderKind::AttrDict &&
3078 emitHelper.hasNonEmptyPropertiesStruct();
3079 if (deprecated)
3080 m->setDeprecated(legacyBuilderDeprecation);
3081 genInlineCreateBody(paramList, deprecated);
3082 auto &body = m->body();
3083
3084 // Operands
3085 if (numVariadicOperands == 0 || numNonVariadicOperands != 0)
3086 body << " assert(operands.size()"
3087 << (numVariadicOperands != 0 ? " >= " : " == ")
3088 << numNonVariadicOperands
3089 << "u && \"mismatched number of parameters\");\n";
3090 body << " " << builderOpState << ".addOperands(operands);\n";
3091
3092 genCodeForAddingPropertiesAndAttributes(body, kind, attributesName);
3093
3094 // Create the correct number of regions
3095 if (int numRegions = op.getNumRegions()) {
3096 body << llvm::formatv(
3097 Fmt: " for (unsigned i = 0; i != {0}; ++i)\n",
3098 Vals: (op.getNumVariadicRegions() ? "numRegions" : Twine(numRegions)));
3099 body << " (void)" << builderOpState << ".addRegion();\n";
3100 }
3101
3102 // Result types
3103 if (numVariadicResults == 0 || numNonVariadicResults != 0)
3104 body << " assert(resultTypes.size()"
3105 << (numVariadicResults != 0 ? " >= " : " == ") << numNonVariadicResults
3106 << "u && \"mismatched number of return types\");\n";
3107 body << " " << builderOpState << ".addTypes(resultTypes);\n";
3108
3109 // Generate builder that infers type too.
3110 // TODO: Expand to handle successors.
3111 if (canInferType(op) && op.getNumSuccessors() == 0)
3112 genInferredTypeCollectiveParamBuilder(kind);
3113}
3114
3115void OpEmitter::buildParamList(SmallVectorImpl<MethodParameter> &paramList,
3116 llvm::StringSet<> &inferredAttributes,
3117 SmallVectorImpl<std::string> &resultTypeNames,
3118 TypeParamKind typeParamKind,
3119 AttrParamKind attrParamKind) {
3120 resultTypeNames.clear();
3121 auto numResults = op.getNumResults();
3122 resultTypeNames.reserve(N: numResults);
3123
3124 paramList.emplace_back(Args: "::mlir::OpBuilder &", Args: odsBuilder);
3125 paramList.emplace_back(Args: "::mlir::OperationState &", Args: builderOpState);
3126
3127 switch (typeParamKind) {
3128 case TypeParamKind::None:
3129 break;
3130 case TypeParamKind::Separate: {
3131 // Add parameters for all return types
3132 for (int i = 0; i < numResults; ++i) {
3133 const auto &result = op.getResult(index: i);
3134 std::string resultName = std::string(result.name);
3135 if (resultName.empty())
3136 resultName = std::string(formatv(Fmt: "resultType{0}", Vals&: i));
3137
3138 StringRef type =
3139 result.isVariadic() ? "::mlir::TypeRange" : "::mlir::Type";
3140
3141 paramList.emplace_back(Args&: type, Args&: resultName, Args: result.isOptional());
3142 resultTypeNames.emplace_back(Args: std::move(resultName));
3143 }
3144 } break;
3145 case TypeParamKind::Collective: {
3146 paramList.emplace_back(Args: "::mlir::TypeRange", Args: "resultTypes");
3147 resultTypeNames.push_back(Elt: "resultTypes");
3148 } break;
3149 }
3150
3151 // Add parameters for all arguments (operands and attributes).
3152 // Track "attr-like" (property and attribute) optional values separate from
3153 // attributes themselves so that the disambiguation code can look at the first
3154 // attribute specifically when determining where to trim the optional-value
3155 // list to avoid ambiguity while preserving the ability of all-property ops to
3156 // use default parameters.
3157 int defaultValuedAttrLikeStartIndex = op.getNumArgs();
3158 int defaultValuedAttrStartIndex = op.getNumArgs();
3159 // Successors and variadic regions go at the end of the parameter list, so no
3160 // default arguments are possible.
3161 bool hasTrailingParams = op.getNumSuccessors() || op.getNumVariadicRegions();
3162 if (!hasTrailingParams) {
3163 // Calculate the start index from which we can attach default values in the
3164 // builder declaration.
3165 for (int i = op.getNumArgs() - 1; i >= 0; --i) {
3166 auto *namedAttr =
3167 llvm::dyn_cast_if_present<tblgen::NamedAttribute *>(Val: op.getArg(index: i));
3168 auto *namedProperty =
3169 llvm::dyn_cast_if_present<tblgen::NamedProperty *>(Val: op.getArg(index: i));
3170 if (namedProperty) {
3171 Property prop = namedProperty->prop;
3172 if (!prop.hasDefaultValue())
3173 break;
3174 defaultValuedAttrLikeStartIndex = i;
3175 continue;
3176 }
3177 if (!namedAttr)
3178 break;
3179
3180 Attribute attr = namedAttr->attr;
3181 // TODO: Currently we can't differentiate between optional meaning do not
3182 // verify/not always error if missing or optional meaning need not be
3183 // specified in builder. Expand isOptional once we can differentiate.
3184 if (!attr.hasDefaultValue() && !attr.isDerivedAttr())
3185 break;
3186
3187 // Creating an APInt requires us to provide bitwidth, value, and
3188 // signedness, which is complicated compared to others. Similarly
3189 // for APFloat.
3190 // TODO: Adjust the 'returnType' field of such attributes
3191 // to support them.
3192 StringRef retType = namedAttr->attr.getReturnType();
3193 if (retType == "::llvm::APInt" || retType == "::llvm::APFloat")
3194 break;
3195
3196 defaultValuedAttrLikeStartIndex = i;
3197 defaultValuedAttrStartIndex = i;
3198 }
3199 }
3200
3201 // Check if parameters besides default valued one are enough to distinguish
3202 // between builders with wrapped and unwrapped arguments.
3203 bool hasBuilderAmbiguity = true;
3204 for (auto *namedAttr : llvm::make_isa_range<NamedAttribute *>(Range: op.getArgs())) {
3205 Attribute attr = namedAttr->attr;
3206 if (attr.hasDefaultValue() || attr.isDerivedAttr())
3207 continue;
3208
3209 if (attrParamKind != AttrParamKind::WrappedAttr ||
3210 !canUseUnwrappedRawValue(attr))
3211 continue;
3212
3213 hasBuilderAmbiguity = false;
3214 break;
3215 }
3216
3217 // Avoid generating build methods that are ambiguous due to default values by
3218 // requiring at least one attribute.
3219 if (defaultValuedAttrStartIndex < op.getNumArgs()) {
3220 // TODO: This should have been possible as a cast<NamedAttribute> but
3221 // required template instantiations is not yet defined for the tblgen helper
3222 // classes.
3223 auto *namedAttr =
3224 cast<NamedAttribute *>(Val: op.getArg(index: defaultValuedAttrStartIndex));
3225 Attribute attr = namedAttr->attr;
3226 if ((attrParamKind == AttrParamKind::WrappedAttr &&
3227 canUseUnwrappedRawValue(attr) && hasBuilderAmbiguity) ||
3228 (attrParamKind == AttrParamKind::UnwrappedValue &&
3229 !canUseUnwrappedRawValue(attr) && hasBuilderAmbiguity)) {
3230 ++defaultValuedAttrStartIndex;
3231 defaultValuedAttrLikeStartIndex = defaultValuedAttrStartIndex;
3232 }
3233 }
3234
3235 /// Collect any inferred attributes.
3236 for (const NamedTypeConstraint &operand : op.getOperands()) {
3237 if (operand.isVariadicOfVariadic()) {
3238 inferredAttributes.insert(
3239 key: operand.constraint.getVariadicOfVariadicSegmentSizeAttr());
3240 }
3241 }
3242
3243 FmtContext fctx;
3244 fctx.withBuilder(subst: odsBuilder);
3245
3246 for (int i = 0, e = op.getNumArgs(), numOperands = 0; i < e; ++i) {
3247 Argument arg = op.getArg(index: i);
3248 if (const auto *operand =
3249 llvm::dyn_cast_if_present<NamedTypeConstraint *>(Val&: arg)) {
3250 StringRef type;
3251 if (operand->isVariadicOfVariadic())
3252 type = "::llvm::ArrayRef<::mlir::ValueRange>";
3253 else if (operand->isVariadic())
3254 type = "::mlir::ValueRange";
3255 else
3256 type = "::mlir::Value";
3257
3258 paramList.emplace_back(Args&: type, Args: getArgumentName(op, index: numOperands++),
3259 Args: operand->isOptional());
3260 continue;
3261 }
3262 if (auto *propArg = llvm::dyn_cast_if_present<NamedProperty *>(Val&: arg)) {
3263 const Property &prop = propArg->prop;
3264 StringRef type = prop.getInterfaceType();
3265 std::string defaultValue;
3266 if (prop.hasDefaultValue() && i >= defaultValuedAttrLikeStartIndex) {
3267 defaultValue = tgfmt(fmt: prop.getDefaultValue(), ctx: &fctx);
3268 }
3269 bool isOptional = prop.hasDefaultValue();
3270 paramList.emplace_back(Args&: type, Args&: propArg->name, Args: StringRef(defaultValue),
3271 Args&: isOptional);
3272 continue;
3273 }
3274 const NamedAttribute &namedAttr = *cast<NamedAttribute *>(Val&: arg);
3275 const Attribute &attr = namedAttr.attr;
3276
3277 // Inferred attributes don't need to be added to the param list.
3278 if (inferredAttributes.contains(key: namedAttr.name))
3279 continue;
3280
3281 StringRef type;
3282 switch (attrParamKind) {
3283 case AttrParamKind::WrappedAttr:
3284 type = attr.getStorageType();
3285 break;
3286 case AttrParamKind::UnwrappedValue:
3287 if (canUseUnwrappedRawValue(attr))
3288 type = attr.getReturnType();
3289 else
3290 type = attr.getStorageType();
3291 break;
3292 }
3293
3294 // Attach default value if requested and possible.
3295 std::string defaultValue;
3296 if (i >= defaultValuedAttrStartIndex) {
3297 if (attrParamKind == AttrParamKind::UnwrappedValue &&
3298 canUseUnwrappedRawValue(attr))
3299 defaultValue += tgfmt(fmt: attr.getDefaultValue(), ctx: &fctx);
3300 else
3301 defaultValue += "nullptr";
3302 }
3303 paramList.emplace_back(Args&: type, Args: namedAttr.name, Args: StringRef(defaultValue),
3304 Args: attr.isOptional());
3305 }
3306
3307 /// Insert parameters for each successor.
3308 for (const NamedSuccessor &succ : op.getSuccessors()) {
3309 StringRef type =
3310 succ.isVariadic() ? "::mlir::BlockRange" : "::mlir::Block *";
3311 paramList.emplace_back(Args&: type, Args: succ.name);
3312 }
3313
3314 /// Insert parameters for variadic regions.
3315 for (const NamedRegion &region : op.getRegions())
3316 if (region.isVariadic())
3317 paramList.emplace_back(Args: "unsigned",
3318 Args: llvm::formatv(Fmt: "{0}Count", Vals: region.name).str());
3319}
3320
3321void OpEmitter::genCodeForAddingArgAndRegionForBuilder(
3322 MethodBody &body, llvm::StringSet<> &inferredAttributes,
3323 bool isRawValueAttr) {
3324 bool needsProperties =
3325 !op.getProperties().empty() ||
3326 op.getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments");
3327 for (const NamedTypeConstraint &operand : op.getOperands())
3328 needsProperties |= operand.isVariadicOfVariadic();
3329 for (const NamedAttribute &attr : op.getAttributes())
3330 needsProperties |=
3331 !attr.attr.isDerivedAttr() && !inferredAttributes.contains(key: attr.name);
3332 if (needsProperties)
3333 body << " auto &odsProperties = " << builderOpStateProperties << ";\n";
3334
3335 // Push all operands to the result.
3336 for (int i = 0, e = op.getNumOperands(); i < e; ++i) {
3337 std::string argName = getArgumentName(op, index: i);
3338 const NamedTypeConstraint &operand = op.getOperand(index: i);
3339 if (operand.constraint.isVariadicOfVariadic()) {
3340 body << " for (::mlir::ValueRange range : " << argName << ")\n "
3341 << builderOpState << ".addOperands(range);\n";
3342
3343 // Add the segment attribute.
3344 body << " {\n"
3345 << " ::llvm::SmallVector<int32_t> rangeSegments;\n"
3346 << " for (::mlir::ValueRange range : " << argName << ")\n"
3347 << " rangeSegments.push_back(range.size());\n"
3348 << " auto rangeAttr = " << odsBuilder
3349 << ".getDenseI32ArrayAttr(rangeSegments);\n";
3350 body << " odsProperties."
3351 << operand.constraint.getVariadicOfVariadicSegmentSizeAttr()
3352 << " = rangeAttr;";
3353 body << " }\n";
3354 continue;
3355 }
3356
3357 if (operand.isOptional())
3358 body << " if (" << argName << ")\n ";
3359 body << " " << builderOpState << ".addOperands(" << argName << ");\n";
3360 }
3361
3362 // If the operation has the operand segment size attribute, add it here.
3363 auto emitSegment = [&]() {
3364 interleaveComma(c: llvm::seq<int>(Begin: 0, End: op.getNumOperands()), os&: body, each_fn: [&](int i) {
3365 const NamedTypeConstraint &operand = op.getOperand(index: i);
3366 if (!operand.isVariableLength()) {
3367 body << "1";
3368 return;
3369 }
3370
3371 std::string operandName = getArgumentName(op, index: i);
3372 if (operand.isOptional()) {
3373 body << "(" << operandName << " ? 1 : 0)";
3374 } else if (operand.isVariadicOfVariadic()) {
3375 body << llvm::formatv(
3376 Fmt: "llvm::accumulate({0}, int32_t(0), "
3377 "[](int32_t curSum, ::mlir::ValueRange range) {{ return curSum + "
3378 "static_cast<int32_t>(range.size()); })",
3379 Vals&: operandName);
3380 } else {
3381 body << "static_cast<int32_t>(" << getArgumentName(op, index: i) << ".size())";
3382 }
3383 });
3384 };
3385 if (op.getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments")) {
3386 body << " ::llvm::copy(::llvm::ArrayRef<int32_t>({";
3387 emitSegment();
3388 body << "}), odsProperties"
3389 << ".operandSegmentSizes.begin());\n";
3390 }
3391
3392 // Push all properties to the result.
3393 for (const auto &namedProp : op.getProperties()) {
3394 // Use the setter from the Properties struct since the conversion from the
3395 // interface type (used in the builder argument) to the storage type (used
3396 // in the state) is not necessarily trivial.
3397 std::string setterName = op.getSetterName(name: namedProp.name);
3398 body << formatv(Fmt: " odsProperties.{0}({1});\n", Vals&: setterName, Vals: namedProp.name);
3399 }
3400 // Push all attributes to the result.
3401 for (const auto &namedAttr : op.getAttributes()) {
3402 auto &attr = namedAttr.attr;
3403 if (attr.isDerivedAttr() || inferredAttributes.contains(key: namedAttr.name))
3404 continue;
3405
3406 // TODO: The wrapping of optional is different for default or not, so don't
3407 // unwrap for default ones that would fail below.
3408 bool emitNotNullCheck =
3409 (attr.isOptional() && !attr.hasDefaultValue()) ||
3410 (attr.hasDefaultValue() && !isRawValueAttr) ||
3411 // TODO: UnitAttr is optional, not wrapped, but needs to be guarded as
3412 // the constant materialization is only for true case.
3413 (isRawValueAttr && attr.getAttrDefName() == "UnitAttr");
3414 if (emitNotNullCheck)
3415 body.indent() << formatv(Fmt: "if ({0}) ", Vals: namedAttr.name) << "{\n";
3416
3417 if (isRawValueAttr && canUseUnwrappedRawValue(attr)) {
3418 // If this is a raw value, then we need to wrap it in an Attribute
3419 // instance.
3420 FmtContext fctx;
3421 fctx.withBuilder(subst: "odsBuilder");
3422 body << formatv(Fmt: " odsProperties.{0} = {1};\n", Vals: namedAttr.name,
3423 Vals: constBuildAttrFromParam(attr, fctx, paramName: namedAttr.name));
3424 } else {
3425 body << formatv(Fmt: " odsProperties.{0} = {0};\n", Vals: namedAttr.name);
3426 }
3427 if (emitNotNullCheck)
3428 body.unindent() << " }\n";
3429 }
3430
3431 // Create the correct number of regions.
3432 for (const NamedRegion &region : op.getRegions()) {
3433 if (region.isVariadic())
3434 body << formatv(Fmt: " for (unsigned i = 0; i < {0}Count; ++i)\n ",
3435 Vals: region.name);
3436
3437 body << " (void)" << builderOpState << ".addRegion();\n";
3438 }
3439
3440 // Push all successors to the result.
3441 for (const NamedSuccessor &namedSuccessor : op.getSuccessors()) {
3442 body << formatv(Fmt: " {0}.addSuccessors({1});\n", Vals: builderOpState,
3443 Vals: namedSuccessor.name);
3444 }
3445}
3446
3447void OpEmitter::genCanonicalizerDecls() {
3448 bool hasCanonicalizeMethod = def.getValueAsBit(FieldName: "hasCanonicalizeMethod");
3449 if (hasCanonicalizeMethod) {
3450 // static LogicResult FooOp::
3451 // canonicalize(FooOp op, PatternRewriter &rewriter);
3452 SmallVector<MethodParameter> paramList;
3453 paramList.emplace_back(Args: op.getCppClassName(), Args: "op");
3454 paramList.emplace_back(Args: "::mlir::PatternRewriter &", Args: "rewriter");
3455 auto *m = opClass.declareStaticMethod(retType: "::llvm::LogicalResult",
3456 name: "canonicalize", args: std::move(paramList));
3457 ERROR_IF_PRUNED(m, "canonicalize", op);
3458 }
3459
3460 // We get a prototype for 'getCanonicalizationPatterns' if requested directly
3461 // or if using a 'canonicalize' method.
3462 bool hasCanonicalizer = def.getValueAsBit(FieldName: "hasCanonicalizer");
3463 if (!hasCanonicalizeMethod && !hasCanonicalizer)
3464 return;
3465
3466 // We get a body for 'getCanonicalizationPatterns' when using a 'canonicalize'
3467 // method, but not implementing 'getCanonicalizationPatterns' manually.
3468 bool hasBody = hasCanonicalizeMethod && !hasCanonicalizer;
3469
3470 // Add a signature for getCanonicalizationPatterns if implemented by the
3471 // dialect or if synthesized to call 'canonicalize'.
3472 SmallVector<MethodParameter> paramList;
3473 paramList.emplace_back(Args: "::mlir::RewritePatternSet &", Args: "results");
3474 paramList.emplace_back(Args: "::mlir::MLIRContext *", Args: "context");
3475 auto kind = hasBody ? Method::Static : Method::StaticDeclaration;
3476 auto *method = opClass.addMethod(retType: "void", name: "getCanonicalizationPatterns", properties: kind,
3477 args: std::move(paramList));
3478
3479 // If synthesizing the method, fill it.
3480 if (hasBody) {
3481 ERROR_IF_PRUNED(method, "getCanonicalizationPatterns", op);
3482 method->body() << " results.add(canonicalize);\n";
3483 // `RewritePatternSet::add` requires the complete type.
3484 requiredDefIncludes.push_back(Elt: "mlir/IR/PatternMatch.h");
3485 }
3486}
3487
3488void OpEmitter::genFolderDecls() {
3489 if (!op.hasFolder())
3490 return;
3491
3492 SmallVector<MethodParameter> paramList;
3493 paramList.emplace_back(Args: "FoldAdaptor", Args: "adaptor");
3494
3495 StringRef retType;
3496 bool hasSingleResult =
3497 op.getNumResults() == 1 && op.getNumVariableLengthResults() == 0;
3498 if (hasSingleResult) {
3499 retType = "::mlir::OpFoldResult";
3500 } else {
3501 paramList.emplace_back(Args: "::llvm::SmallVectorImpl<::mlir::OpFoldResult> &",
3502 Args: "results");
3503 retType = "::llvm::LogicalResult";
3504 }
3505
3506 auto *m = opClass.declareMethod(retType, name: "fold", args: std::move(paramList));
3507 ERROR_IF_PRUNED(m, "fold", op);
3508}
3509
3510void OpEmitter::genOpInterfaceMethods(const tblgen::InterfaceTrait *opTrait) {
3511 Interface interface = opTrait->getInterface();
3512
3513 // Get the set of methods that should always be declared.
3514 auto alwaysDeclaredMethodsVec = opTrait->getAlwaysDeclaredMethods();
3515 llvm::StringSet<> alwaysDeclaredMethods;
3516 alwaysDeclaredMethods.insert_range(R&: alwaysDeclaredMethodsVec);
3517
3518 for (const InterfaceMethod &method : interface.getMethods()) {
3519 // Don't declare if the method has a body.
3520 if (method.getBody())
3521 continue;
3522 // Don't declare if the method has a default implementation and the op
3523 // didn't request that it always be declared.
3524 if (method.getDefaultImplementation() &&
3525 !alwaysDeclaredMethods.count(Key: method.getName())) {
3526 genOpInterfaceMethodUsingDecl(opTrait, method);
3527 continue;
3528 }
3529 // Interface methods are allowed to overlap with existing methods, so don't
3530 // check if pruned.
3531 (void)genOpInterfaceMethod(method);
3532 }
3533}
3534
3535Method *OpEmitter::genOpInterfaceMethod(const InterfaceMethod &method,
3536 bool declaration) {
3537 SmallVector<MethodParameter> paramList;
3538 for (const InterfaceMethod::Argument &arg : method.getArguments())
3539 paramList.emplace_back(Args: arg.type, Args: arg.name);
3540
3541 auto props = (method.isStatic() ? Method::Static : Method::None) |
3542 (declaration ? Method::Declaration : Method::None);
3543 return opClass.addMethod(retType: method.getReturnType(), name: method.getName(), properties: props,
3544 args: std::move(paramList));
3545}
3546
3547UsingDeclaration *
3548OpEmitter::genOpInterfaceMethodUsingDecl(const tblgen::InterfaceTrait *opTrait,
3549 const InterfaceMethod &method) {
3550 std::string name = (llvm::Twine(opTrait->getFullyQualifiedTraitName()) + "<" +
3551 op.getCppClassName() + ">::" + method.getName())
3552 .str();
3553 if (interfaceUsingNames.insert(key: name).second)
3554 return opClass.declare<UsingDeclaration>(args: std::move(name));
3555 return nullptr;
3556}
3557
3558void OpEmitter::genOpInterfaceMethods() {
3559 for (const auto &trait : op.getTraits()) {
3560 if (const auto *opTrait = dyn_cast<tblgen::InterfaceTrait>(Val: &trait))
3561 if (opTrait->shouldDeclareMethods())
3562 genOpInterfaceMethods(opTrait);
3563 }
3564}
3565
3566void OpEmitter::genSideEffectInterfaceMethods() {
3567 enum EffectKind { Operand, Result, Symbol, Static };
3568 struct EffectLocation {
3569 /// The effect applied.
3570 SideEffect effect;
3571
3572 /// The index if the kind is not static.
3573 unsigned index;
3574
3575 /// The kind of the location.
3576 unsigned kind;
3577 };
3578
3579 StringMap<SmallVector<EffectLocation, 1>> interfaceEffects;
3580 auto resolveDecorators = [&](Operator::var_decorator_range decorators,
3581 unsigned index, unsigned kind) {
3582 for (auto decorator : decorators)
3583 if (SideEffect *effect = dyn_cast<SideEffect>(Val: &decorator)) {
3584 addTrait(trait: effect->getInterfaceTrait(), cppIncludes: effect->getCppIncludes());
3585 interfaceEffects[effect->getBaseEffectName()].push_back(
3586 Elt: EffectLocation{.effect: *effect, .index: index, .kind: kind});
3587 }
3588 };
3589
3590 // Collect effects that were specified via:
3591 /// Traits.
3592 for (const auto &trait : op.getTraits()) {
3593 const auto *opTrait = dyn_cast<tblgen::SideEffectTrait>(Val: &trait);
3594 if (!opTrait)
3595 continue;
3596 auto &effects = interfaceEffects[opTrait->getBaseEffectName()];
3597 for (auto decorator : opTrait->getEffects())
3598 effects.push_back(Elt: EffectLocation{.effect: cast<SideEffect>(Val&: decorator),
3599 /*index=*/0, .kind: EffectKind::Static});
3600 }
3601 /// Attributes and Operands.
3602 for (unsigned i = 0, operandIt = 0, e = op.getNumArgs(); i != e; ++i) {
3603 Argument arg = op.getArg(index: i);
3604 if (isa<NamedTypeConstraint *>(Val: arg)) {
3605 resolveDecorators(op.getArgDecorators(index: i), operandIt, EffectKind::Operand);
3606 ++operandIt;
3607 continue;
3608 }
3609 if (isa<NamedProperty *>(Val: arg))
3610 continue;
3611 const NamedAttribute *attr = cast<NamedAttribute *>(Val&: arg);
3612 if (attr->attr.getBaseAttr().isSymbolRefAttr())
3613 resolveDecorators(op.getArgDecorators(index: i), i, EffectKind::Symbol);
3614 }
3615 /// Results.
3616 for (unsigned i = 0, e = op.getNumResults(); i != e; ++i)
3617 resolveDecorators(op.getResultDecorators(index: i), i, EffectKind::Result);
3618
3619 // The code used to add an effect instance.
3620 // {0}: The effect class.
3621 // {1}: Optional value or symbol reference.
3622 // {2}: Optional parameters attribute.
3623 // {3}: The side effect stage.
3624 // {4}: Does this side effect act on every single value of resource.
3625 // {5}: The resource class.
3626 const char *addEffectCode =
3627 " effects.emplace_back({0}::get(), {1}{2}{3}, {4}, {5}::get());\n";
3628
3629 for (auto &it : interfaceEffects) {
3630 // Generate the 'getEffects' method.
3631 std::string type = llvm::formatv(Fmt: "::llvm::SmallVectorImpl<::mlir::"
3632 "SideEffects::EffectInstance<{0}>> &",
3633 Vals: it.first())
3634 .str();
3635 auto *getEffects = opClass.addMethod(retType: "void", name: "getEffects",
3636 args: MethodParameter(type, "effects"));
3637 ERROR_IF_PRUNED(getEffects, "getEffects", op);
3638 auto &body = getEffects->body();
3639
3640 // Add effect instances for each of the locations marked on the operation.
3641 for (auto &location : it.second) {
3642 StringRef effect = location.effect.getName();
3643 StringRef resource = location.effect.getResource();
3644 std::string parameters = location.effect.getParameters().str();
3645 if (!parameters.empty())
3646 parameters += ", ";
3647 int stage = (int)location.effect.getStage();
3648 bool effectOnFullRegion = (int)location.effect.getEffectOnfullRegion();
3649 if (location.kind == EffectKind::Static) {
3650 // A static instance has no attached value.
3651 body << llvm::formatv(Fmt: addEffectCode, Vals&: effect, Vals: "", Vals&: parameters, Vals&: stage,
3652 Vals&: effectOnFullRegion, Vals&: resource)
3653 .str();
3654 } else if (location.kind == EffectKind::Symbol) {
3655 // A symbol reference requires adding the proper attribute.
3656 const auto *attr = cast<NamedAttribute *>(Val: op.getArg(index: location.index));
3657 std::string argName = op.getGetterName(name: attr->name);
3658 if (attr->attr.isOptional()) {
3659 body << " if (auto symbolRef = " << argName << "Attr())\n "
3660 << llvm::formatv(Fmt: addEffectCode, Vals&: effect, Vals: "symbolRef, ",
3661 Vals&: parameters, Vals&: stage, Vals&: effectOnFullRegion, Vals&: resource)
3662 .str();
3663 } else {
3664 body << llvm::formatv(Fmt: addEffectCode, Vals&: effect, Vals: argName + "Attr(), ",
3665 Vals&: parameters, Vals&: stage, Vals&: effectOnFullRegion, Vals&: resource)
3666 .str();
3667 }
3668 } else {
3669 // Otherwise this is an operand/result, so we need to attach the Value.
3670 body << " {\n auto valueRange = getODS"
3671 << (location.kind == EffectKind::Operand ? "Operand" : "Result")
3672 << "IndexAndLength(" << location.index << ");\n"
3673 << " for (unsigned idx = valueRange.first; idx < "
3674 "valueRange.first"
3675 << " + valueRange.second; idx++) {\n "
3676 << llvm::formatv(Fmt: addEffectCode, Vals&: effect,
3677 Vals: (location.kind == EffectKind::Operand
3678 ? "&getOperation()->getOpOperand(idx), "
3679 : "getOperation()->getOpResult(idx), "),
3680 Vals&: parameters, Vals&: stage, Vals&: effectOnFullRegion, Vals&: resource)
3681 << " }\n }\n";
3682 }
3683 }
3684 }
3685}
3686
3687void OpEmitter::genTypeInterfaceMethods() {
3688 if (!op.allResultTypesKnown())
3689 return;
3690 // Generate 'inferReturnTypes' method declaration using the interface method
3691 // declared in 'InferTypeOpInterface' op interface.
3692 const auto *trait =
3693 cast<InterfaceTrait>(Val: op.getTrait(trait: "::mlir::InferTypeOpInterface::Trait"));
3694 Interface interface = trait->getInterface();
3695 Method *method = [&]() -> Method * {
3696 for (const InterfaceMethod &interfaceMethod : interface.getMethods()) {
3697 if (interfaceMethod.getName() == "inferReturnTypes") {
3698 return genOpInterfaceMethod(method: interfaceMethod, /*declaration=*/false);
3699 }
3700 }
3701 assert(0 && "unable to find inferReturnTypes interface method");
3702 return nullptr;
3703 }();
3704 ERROR_IF_PRUNED(method, "inferReturnTypes", op);
3705 auto &body = method->body();
3706 body << " inferredReturnTypes.resize(" << op.getNumResults() << ");\n";
3707
3708 FmtContext fctx;
3709 fctx.withBuilder(subst: "odsBuilder");
3710 fctx.addSubst(placeholder: "_ctxt", subst: "context");
3711 body << " ::mlir::Builder odsBuilder(context);\n";
3712
3713 // Preprocessing stage to verify all accesses to operands are valid.
3714 int maxAccessedIndex = -1;
3715 for (int i = 0, e = op.getNumResults(); i != e; ++i) {
3716 const InferredResultType &infer = op.getInferredResultType(index: i);
3717 if (!infer.isArg())
3718 continue;
3719 Operator::OperandAttrOrProp arg =
3720 op.getArgToOperandAttrOrProp(index: infer.getIndex());
3721 if (arg.kind() == Operator::OperandAttrOrProp::Kind::Operand) {
3722 maxAccessedIndex =
3723 std::max(a: maxAccessedIndex, b: arg.operandOrAttributeIndex());
3724 }
3725 }
3726 if (maxAccessedIndex != -1) {
3727 body << " if (operands.size() <= " << Twine(maxAccessedIndex) << ")\n";
3728 body << " return ::mlir::failure();\n";
3729 }
3730
3731 // Process the type inference graph in topological order, starting from types
3732 // that are always fully-inferred: operands and results with constructible
3733 // types. The type inference graph here will always be a DAG, so this gives
3734 // us the correct order for generating the types. -1 is a placeholder to
3735 // indicate the type for a result has not been generated.
3736 SmallVector<int> constructedIndices(op.getNumResults(), -1);
3737 int inferredTypeIdx = 0;
3738 for (int numResults = op.getNumResults(); inferredTypeIdx != numResults;) {
3739 for (int i = 0, e = op.getNumResults(); i != e; ++i) {
3740 if (constructedIndices[i] >= 0)
3741 continue;
3742 const InferredResultType &infer = op.getInferredResultType(index: i);
3743 std::string typeStr;
3744 if (infer.isArg()) {
3745 // If this is an operand, just index into operand list to access the
3746 // type.
3747 Operator::OperandAttrOrProp arg =
3748 op.getArgToOperandAttrOrProp(index: infer.getIndex());
3749 if (arg.kind() == Operator::OperandAttrOrProp::Kind::Operand) {
3750 typeStr = ("operands[" + Twine(arg.operandOrAttributeIndex()) +
3751 "].getType()")
3752 .str();
3753
3754 // If this is an attribute, index into the attribute dictionary.
3755 } else if (auto *attr = dyn_cast<NamedAttribute *>(
3756 Val: op.getArg(index: arg.operandOrAttributeIndex()))) {
3757 body << " ::mlir::TypedAttr odsInferredTypeAttr" << inferredTypeIdx
3758 << " = (properties ? properties.as<Properties *>()->"
3759 << attr->name
3760 << " : ::llvm::dyn_cast_or_null<::mlir::TypedAttr>(attributes."
3761 "get(\""
3762 << attr->name << "\")));\n"
3763 << " if (!odsInferredTypeAttr" << inferredTypeIdx
3764 << ") return ::mlir::failure();\n";
3765 typeStr =
3766 ("odsInferredTypeAttr" + Twine(inferredTypeIdx) + ".getType()")
3767 .str();
3768 } else {
3769 llvm::PrintFatalError(Rec: &op.getDef(),
3770 Msg: "Properties cannot be used for type inference");
3771 }
3772 } else if (std::optional<StringRef> builder =
3773 op.getResult(index: infer.getResultIndex())
3774 .constraint.getBuilderCall()) {
3775 typeStr = tgfmt(fmt: *builder, ctx: &fctx).str();
3776 } else if (int index = constructedIndices[infer.getResultIndex()];
3777 index >= 0) {
3778 typeStr = ("odsInferredType" + Twine(index)).str();
3779 } else {
3780 continue;
3781 }
3782 body << " ::mlir::Type odsInferredType" << inferredTypeIdx++ << " = "
3783 << tgfmt(fmt: infer.getTransformer(), ctx: &fctx.withSelf(subst: typeStr)) << ";\n";
3784 constructedIndices[i] = inferredTypeIdx - 1;
3785 }
3786 }
3787 for (auto [i, index] : llvm::enumerate(First&: constructedIndices))
3788 body << " inferredReturnTypes[" << i << "] = odsInferredType" << index
3789 << ";\n";
3790 body << " return ::mlir::success();";
3791}
3792
3793void OpEmitter::genParser() {
3794 if (hasStringAttribute(record: def, fieldName: "assemblyFormat"))
3795 return;
3796
3797 if (!def.getValueAsBit(FieldName: "hasCustomAssemblyFormat"))
3798 return;
3799
3800 SmallVector<MethodParameter> paramList;
3801 paramList.emplace_back(Args: "::mlir::OpAsmParser &", Args: "parser");
3802 paramList.emplace_back(Args: "::mlir::OperationState &", Args: "result");
3803
3804 auto *method = opClass.declareStaticMethod(retType: "::mlir::ParseResult", name: "parse",
3805 args: std::move(paramList));
3806 ERROR_IF_PRUNED(method, "parse", op);
3807}
3808
3809void OpEmitter::genPrinter() {
3810 if (hasStringAttribute(record: def, fieldName: "assemblyFormat"))
3811 return;
3812
3813 // Check to see if this op uses a c++ format.
3814 if (!def.getValueAsBit(FieldName: "hasCustomAssemblyFormat"))
3815 return;
3816 auto *method = opClass.declareMethod(
3817 retType: "void", name: "print", args: MethodParameter("::mlir::OpAsmPrinter &", "p"));
3818 ERROR_IF_PRUNED(method, "print", op);
3819}
3820
3821void OpEmitter::genVerifier() {
3822 auto *implMethod =
3823 opClass.addMethod(retType: "::llvm::LogicalResult", name: "verifyInvariantsImpl");
3824 ERROR_IF_PRUNED(implMethod, "verifyInvariantsImpl", op);
3825 auto &implBody = implMethod->body();
3826 populateSubstitutions(emitHelper, ctx&: verifyCtx);
3827 genPropertyVerifier(emitHelper, ctx&: verifyCtx, body&: implBody, staticVerifierEmitter);
3828 genAttributeVerifier(emitHelper, ctx&: verifyCtx, body&: implBody, staticVerifierEmitter);
3829 genOperandResultVerifier(body&: implBody, values: op.getOperands(), valueKind: "operand");
3830 genOperandResultVerifier(body&: implBody, values: op.getResults(), valueKind: "result");
3831
3832 for (auto &trait : op.getTraits()) {
3833 if (auto *t = dyn_cast<tblgen::PredTrait>(Val: &trait)) {
3834 implBody << tgfmt(fmt: " if (!($0))\n "
3835 "return emitOpError(\"failed to verify that $1\");\n",
3836 ctx: &verifyCtx, vals: tgfmt(fmt: t->getPredTemplate(), ctx: &verifyCtx),
3837 vals: t->getSummary());
3838 }
3839 }
3840
3841 genRegionVerifier(body&: implBody);
3842 genSuccessorVerifier(body&: implBody);
3843
3844 implBody << " return ::mlir::success();\n";
3845
3846 // TODO: Some places use the `verifyInvariants` to do operation verification.
3847 // This may not act as their expectation because this doesn't call any
3848 // verifiers of native/interface traits. Needs to review those use cases and
3849 // see if we should use the mlir::verify() instead.
3850 auto *method = opClass.addMethod(retType: "::llvm::LogicalResult", name: "verifyInvariants");
3851 ERROR_IF_PRUNED(method, "verifyInvariants", op);
3852 auto &body = method->body();
3853 if (def.getValueAsBit(FieldName: "hasVerifier")) {
3854 body << " if(::mlir::succeeded(verifyInvariantsImpl()) && "
3855 "::mlir::succeeded(verify()))\n";
3856 body << " return ::mlir::success();\n";
3857 body << " return ::mlir::failure();";
3858 } else {
3859 body << " return verifyInvariantsImpl();";
3860 }
3861}
3862
3863void OpEmitter::genCustomVerifier() {
3864 if (def.getValueAsBit(FieldName: "hasVerifier")) {
3865 auto *method = opClass.declareMethod(retType: "::llvm::LogicalResult", name: "verify");
3866 ERROR_IF_PRUNED(method, "verify", op);
3867 }
3868
3869 if (def.getValueAsBit(FieldName: "hasRegionVerifier")) {
3870 auto *method =
3871 opClass.declareMethod(retType: "::llvm::LogicalResult", name: "verifyRegions");
3872 ERROR_IF_PRUNED(method, "verifyRegions", op);
3873 }
3874}
3875
3876void OpEmitter::genOperandResultVerifier(MethodBody &body,
3877 Operator::const_value_range values,
3878 StringRef valueKind) {
3879 // Check that an optional value is at most 1 element.
3880 //
3881 // {0}: Value index.
3882 // {1}: "operand" or "result"
3883 const char *const verifyOptional = R"(
3884 if (valueGroup{0}.size() > 1) {
3885 return emitOpError("{1} group starting at #") << index
3886 << " requires 0 or 1 element, but found " << valueGroup{0}.size();
3887 }
3888)";
3889 // Check the types of a range of values.
3890 //
3891 // {0}: Value index.
3892 // {1}: Type constraint function.
3893 // {2}: "operand" or "result"
3894 const char *const verifyValues = R"(
3895 for (auto v : valueGroup{0}) {
3896 if (::mlir::failed({1}(*this, v.getType(), "{2}", index++)))
3897 return ::mlir::failure();
3898 }
3899)";
3900
3901 const auto canSkip = [](const NamedTypeConstraint &value) {
3902 return !value.hasPredicate() && !value.isOptional() &&
3903 !value.isVariadicOfVariadic();
3904 };
3905 if (values.empty() || llvm::all_of(Range&: values, P: canSkip))
3906 return;
3907
3908 FmtContext fctx;
3909
3910 body << " {\n unsigned index = 0; (void)index;\n";
3911
3912 for (const auto &staticValue : llvm::enumerate(First&: values)) {
3913 const NamedTypeConstraint &value = staticValue.value();
3914
3915 bool hasPredicate = value.hasPredicate();
3916 bool isOptional = value.isOptional();
3917 bool isVariadicOfVariadic = value.isVariadicOfVariadic();
3918 if (!hasPredicate && !isOptional && !isVariadicOfVariadic)
3919 continue;
3920 body << formatv(Fmt: " auto valueGroup{2} = getODS{0}{1}s({2});\n",
3921 // Capitalize the first letter to match the function name
3922 Vals: valueKind.substr(Start: 0, N: 1).upper(), Vals: valueKind.substr(Start: 1),
3923 Vals: staticValue.index());
3924
3925 // If the constraint is optional check that the value group has at most 1
3926 // value.
3927 if (isOptional) {
3928 body << formatv(Fmt: verifyOptional, Vals: staticValue.index(), Vals&: valueKind);
3929 } else if (isVariadicOfVariadic) {
3930 body << formatv(
3931 Fmt: " if (::mlir::failed(::mlir::OpTrait::impl::verifyValueSizeAttr("
3932 "*this, \"{0}\", \"{1}\", valueGroup{2}.size())))\n"
3933 " return ::mlir::failure();\n",
3934 Vals: value.constraint.getVariadicOfVariadicSegmentSizeAttr(), Vals: value.name,
3935 Vals: staticValue.index());
3936 }
3937
3938 // Otherwise, if there is no predicate there is nothing left to do.
3939 if (!hasPredicate)
3940 continue;
3941 // Emit a loop to check all the dynamic values in the pack.
3942 StringRef constraintFn =
3943 staticVerifierEmitter.getTypeConstraintFn(constraint: value.constraint);
3944 body << formatv(Fmt: verifyValues, Vals: staticValue.index(), Vals&: constraintFn, Vals&: valueKind);
3945 }
3946
3947 body << " }\n";
3948}
3949
3950void OpEmitter::genRegionVerifier(MethodBody &body) {
3951 /// Code to verify a region.
3952 ///
3953 /// {0}: Getter for the regions.
3954 /// {1}: The region constraint.
3955 /// {2}: The region's name.
3956 /// {3}: The region description.
3957 const char *const verifyRegion = R"(
3958 for (auto &region : {0})
3959 if (::mlir::failed({1}(*this, region, "{2}", index++)))
3960 return ::mlir::failure();
3961)";
3962 /// Get a single region.
3963 ///
3964 /// {0}: The region's index.
3965 const char *const getSingleRegion =
3966 "::llvm::MutableArrayRef((*this)->getRegion({0}))";
3967
3968 // If we have no regions, there is nothing more to do.
3969 const auto canSkip = [](const NamedRegion &region) {
3970 return region.constraint.getPredicate().isNull();
3971 };
3972 auto regions = op.getRegions();
3973 if (regions.empty() && llvm::all_of(Range&: regions, P: canSkip))
3974 return;
3975
3976 body << " {\n unsigned index = 0; (void)index;\n";
3977 for (const auto &it : llvm::enumerate(First&: regions)) {
3978 const auto &region = it.value();
3979 if (canSkip(region))
3980 continue;
3981
3982 auto getRegion = region.isVariadic()
3983 ? formatv(Fmt: "{0}()", Vals: op.getGetterName(name: region.name)).str()
3984 : formatv(Fmt: getSingleRegion, Vals: it.index()).str();
3985 auto constraintFn =
3986 staticVerifierEmitter.getRegionConstraintFn(constraint: region.constraint);
3987 body << formatv(Fmt: verifyRegion, Vals&: getRegion, Vals&: constraintFn, Vals: region.name);
3988 }
3989 body << " }\n";
3990}
3991
3992void OpEmitter::genSuccessorVerifier(MethodBody &body) {
3993 // Code to verify a variadic successor.
3994 //
3995 // {0}: The name of the successor range accessor.
3996 // {1}: The successor constraint.
3997 // {2}: The successor's name.
3998 const char *const verifyVariadicSuccessor = R"(
3999 for (auto *successor : {0}())
4000 if (::mlir::failed({1}(*this, successor, "{2}", index++)))
4001 return ::mlir::failure();
4002)";
4003 // Code to verify a single successor. The accessor returns a `Block *` by
4004 // value, which can't be wrapped in a `MutableArrayRef`, so verify it
4005 // directly.
4006 //
4007 // {0}: The name of the successor accessor.
4008 // {1}: The successor constraint.
4009 // {2}: The successor's name.
4010 const char *const verifySingleSuccessor = R"(
4011 if (::mlir::failed({1}(*this, {0}(), "{2}", index++)))
4012 return ::mlir::failure();
4013)";
4014
4015 // If we have no successors, there is nothing more to do.
4016 const auto canSkip = [](const NamedSuccessor &successor) {
4017 return successor.constraint.getPredicate().isNull();
4018 };
4019 auto successors = op.getSuccessors();
4020 if (successors.empty() && llvm::all_of(Range&: successors, P: canSkip))
4021 return;
4022
4023 body << " {\n unsigned index = 0; (void)index;\n";
4024
4025 for (auto it : llvm::enumerate(First&: successors)) {
4026 const auto &successor = it.value();
4027 if (canSkip(successor))
4028 continue;
4029
4030 auto constraintFn =
4031 staticVerifierEmitter.getSuccessorConstraintFn(constraint: successor.constraint);
4032 body << formatv(Fmt: successor.isVariadic() ? verifyVariadicSuccessor
4033 : verifySingleSuccessor,
4034 Vals: op.getGetterName(name: successor.name), Vals&: constraintFn,
4035 Vals: successor.name);
4036 }
4037 body << " }\n";
4038}
4039
4040/// Add a size count trait to the given operation class.
4041static void addSizeCountTrait(OpClass &opClass, StringRef traitKind,
4042 int numTotal, int numVariadic) {
4043 if (numVariadic != 0) {
4044 if (numTotal == numVariadic)
4045 opClass.addTrait(trait: "::mlir::OpTrait::Variadic" + traitKind + "s");
4046 else
4047 opClass.addTrait(trait: "::mlir::OpTrait::AtLeastN" + traitKind + "s<" +
4048 Twine(numTotal - numVariadic) + ">::Impl");
4049 return;
4050 }
4051 switch (numTotal) {
4052 case 0:
4053 opClass.addTrait(trait: "::mlir::OpTrait::Zero" + traitKind + "s");
4054 break;
4055 case 1:
4056 opClass.addTrait(trait: "::mlir::OpTrait::One" + traitKind);
4057 break;
4058 default:
4059 opClass.addTrait(trait: "::mlir::OpTrait::N" + traitKind + "s<" + Twine(numTotal) +
4060 ">::Impl");
4061 break;
4062 }
4063}
4064
4065void OpEmitter::genTraits() {
4066 // Add region size trait.
4067 unsigned numRegions = op.getNumRegions();
4068 unsigned numVariadicRegions = op.getNumVariadicRegions();
4069 addSizeCountTrait(opClass, traitKind: "Region", numTotal: numRegions, numVariadic: numVariadicRegions);
4070
4071 // Add result size traits.
4072 int numResults = op.getNumResults();
4073 int numVariadicResults = op.getNumVariableLengthResults();
4074 addSizeCountTrait(opClass, traitKind: "Result", numTotal: numResults, numVariadic: numVariadicResults);
4075
4076 // For single result ops with a known specific type, generate a OneTypedResult
4077 // trait.
4078 if (numResults == 1 && numVariadicResults == 0) {
4079 auto cppName = op.getResults().begin()->constraint.getCppType();
4080 opClass.addTrait(trait: "::mlir::OpTrait::OneTypedResult<" + cppName + ">::Impl");
4081 }
4082
4083 // Add successor size trait.
4084 unsigned numSuccessors = op.getNumSuccessors();
4085 unsigned numVariadicSuccessors = op.getNumVariadicSuccessors();
4086 addSizeCountTrait(opClass, traitKind: "Successor", numTotal: numSuccessors, numVariadic: numVariadicSuccessors);
4087
4088 // Add variadic size trait and normal op traits.
4089 int numOperands = op.getNumOperands();
4090 int numVariadicOperands = op.getNumVariableLengthOperands();
4091
4092 // Add operand size trait.
4093 addSizeCountTrait(opClass, traitKind: "Operand", numTotal: numOperands, numVariadic: numVariadicOperands);
4094
4095 // The op traits defined internal are ensured that they can be verified
4096 // earlier.
4097 for (const auto &trait : op.getTraits()) {
4098 if (auto *opTrait = dyn_cast<tblgen::NativeTrait>(Val: &trait)) {
4099 if (opTrait->isStructuralOpTrait())
4100 addTrait(trait: *opTrait);
4101 }
4102 }
4103
4104 // OpInvariants wrapps the verifyInvariants which needs to be run before
4105 // native/interface traits and after all the traits with `StructuralOpTrait`.
4106 opClass.addTrait(trait: "::mlir::OpTrait::OpInvariants");
4107
4108 if (emitHelper.hasNonEmptyPropertiesStruct())
4109 addTrait(trait: "::mlir::BytecodeOpInterface::Trait",
4110 cppIncludes: {"mlir/Bytecode/BytecodeOpInterface.h"});
4111
4112 // Add the native and interface traits.
4113 for (const auto &trait : op.getTraits()) {
4114 if (auto *opTrait = dyn_cast<tblgen::NativeTrait>(Val: &trait)) {
4115 if (!opTrait->isStructuralOpTrait())
4116 addTrait(trait: *opTrait);
4117 } else if (auto *opTrait = dyn_cast<tblgen::InterfaceTrait>(Val: &trait)) {
4118 addTrait(trait: *opTrait);
4119 }
4120 }
4121
4122 // Auto-derive the builtin token producer/consumer traits whenever the op
4123 // statically declares a Token operand or result.
4124 constexpr llvm::StringLiteral kTokenCppType = "::mlir::TokenType";
4125 auto hasStaticTokenType = [&](auto &&values) {
4126 return llvm::any_of(values, [&](const tblgen::NamedTypeConstraint &v) {
4127 return v.constraint.getCppType() == kTokenCppType;
4128 });
4129 };
4130 if (hasStaticTokenType(op.getOperands()))
4131 opClass.addTrait(trait: "::mlir::OpTrait::TokenConsumerTrait");
4132 if (hasStaticTokenType(op.getResults()))
4133 opClass.addTrait(trait: "::mlir::OpTrait::TokenProducerTrait");
4134}
4135
4136void OpEmitter::genOpNameGetter() {
4137 auto *method = opClass.addStaticMethod<Method::Constexpr>(
4138 retType: "::llvm::StringLiteral", name: "getOperationName");
4139 ERROR_IF_PRUNED(method, "getOperationName", op);
4140 method->body() << " return ::llvm::StringLiteral(\"" << op.getOperationName()
4141 << "\");";
4142}
4143
4144void OpEmitter::genOpAsmInterface() {
4145 // If the user only has one results or specifically added the Asm trait,
4146 // then don't generate it for them. We specifically only handle multi result
4147 // operations, because the name of a single result in the common case is not
4148 // interesting(generally 'result'/'output'/etc.).
4149 // TODO: We could also add a flag to allow operations to opt in to this
4150 // generation, even if they only have a single operation.
4151 int numResults = op.getNumResults();
4152 if (numResults <= 1 || op.getTrait(trait: "::mlir::OpAsmOpInterface::Trait"))
4153 return;
4154
4155 SmallVector<StringRef, 4> resultNames(numResults);
4156 for (int i = 0; i != numResults; ++i)
4157 resultNames[i] = op.getResultName(index: i);
4158
4159 // Don't add the trait if none of the results have a valid name.
4160 if (llvm::all_of(Range&: resultNames, P: [](StringRef name) { return name.empty(); }))
4161 return;
4162 addTrait(trait: "::mlir::OpAsmOpInterface::Trait", cppIncludes: {"mlir/IR/OpImplementation.h"});
4163
4164 // Generate the right accessor for the number of results.
4165 auto *method = opClass.addMethod(
4166 retType: "void", name: "getAsmResultNames",
4167 args: MethodParameter("::mlir::OpAsmSetValueNameFn", "setNameFn"));
4168 ERROR_IF_PRUNED(method, "getAsmResultNames", op);
4169 auto &body = method->body();
4170 for (int i = 0; i != numResults; ++i) {
4171 body << " auto resultGroup" << i << " = getODSResults(" << i << ");\n"
4172 << " if (!resultGroup" << i << ".empty())\n"
4173 << " setNameFn(*resultGroup" << i << ".begin(), \""
4174 << resultNames[i] << "\");\n";
4175 }
4176}
4177
4178//===----------------------------------------------------------------------===//
4179// OpOperandAdaptor emitter
4180//===----------------------------------------------------------------------===//
4181
4182namespace {
4183// Helper class to emit Op operand adaptors to an output stream. Operand
4184// adaptors are wrappers around random access ranges that provide named operand
4185// getters identical to those defined in the Op.
4186// This currently generates 3 classes per Op:
4187// * A Base class within the 'detail' namespace, which contains all logic and
4188// members independent of the random access range that is indexed into.
4189// In other words, it contains all the attribute and region getters.
4190// * A templated class named '{OpName}GenericAdaptor' with a template parameter
4191// 'RangeT' that is indexed into by the getters to access the operands.
4192// It contains all getters to access operands and inherits from the previous
4193// class.
4194// * A class named '{OpName}Adaptor', which inherits from the 'GenericAdaptor'
4195// with 'mlir::ValueRange' as template parameter. It adds a constructor from
4196// an instance of the op type and a verify function.
4197class OpOperandAdaptorEmitter {
4198public:
4199 static void
4200 emitDecl(const Operator &op,
4201 const StaticVerifierFunctionEmitter &staticVerifierEmitter,
4202 raw_ostream &os);
4203 static void
4204 emitDef(const Operator &op,
4205 const StaticVerifierFunctionEmitter &staticVerifierEmitter,
4206 raw_ostream &os);
4207
4208private:
4209 explicit OpOperandAdaptorEmitter(
4210 const Operator &op,
4211 const StaticVerifierFunctionEmitter &staticVerifierEmitter);
4212
4213 // Add verification function. This generates a verify method for the adaptor
4214 // which verifies all the op-independent attribute constraints.
4215 void addVerification();
4216
4217 // The operation for which to emit an adaptor.
4218 const Operator &op;
4219
4220 // The generated adaptor classes.
4221 Class genericAdaptorBase;
4222 Class genericAdaptor;
4223 Class adaptor;
4224
4225 // The emitter containing all of the locally emitted verification functions.
4226 const StaticVerifierFunctionEmitter &staticVerifierEmitter;
4227
4228 // Helper for emitting adaptor code.
4229 OpOrAdaptorHelper emitHelper;
4230};
4231} // namespace
4232
4233OpOperandAdaptorEmitter::OpOperandAdaptorEmitter(
4234 const Operator &op,
4235 const StaticVerifierFunctionEmitter &staticVerifierEmitter)
4236 : op(op), genericAdaptorBase(op.getGenericAdaptorName() + "Base"),
4237 genericAdaptor(op.getGenericAdaptorName()), adaptor(op.getAdaptorName()),
4238 staticVerifierEmitter(staticVerifierEmitter),
4239 emitHelper(op, /*emitForOp=*/false) {
4240
4241 FmtContext fctx;
4242 fctx.withBuilder(subst: odsBuilder);
4243
4244 genericAdaptorBase.declare<VisibilityDeclaration>(args: Visibility::Public);
4245 {
4246 // Define the properties struct with multiple members.
4247 using ConstArgument =
4248 llvm::PointerUnion<const AttributeMetadata *, const NamedProperty *>;
4249 SmallVector<ConstArgument> attrOrProperties;
4250 for (const std::pair<StringRef, AttributeMetadata> &it :
4251 emitHelper.getAttrMetadata()) {
4252 if (!it.second.constraint || !it.second.constraint->isDerivedAttr())
4253 attrOrProperties.push_back(Elt: &it.second);
4254 }
4255 for (const NamedProperty &prop : op.getProperties())
4256 attrOrProperties.push_back(Elt: &prop);
4257 if (emitHelper.getOperandSegmentsSize())
4258 attrOrProperties.push_back(Elt: &emitHelper.getOperandSegmentsSize().value());
4259 if (emitHelper.getResultSegmentsSize())
4260 attrOrProperties.push_back(Elt: &emitHelper.getResultSegmentsSize().value());
4261 std::string declarations = " struct Properties {\n";
4262 llvm::raw_string_ostream os(declarations);
4263 std::string comparator =
4264 " bool operator==(const Properties &rhs) const {\n"
4265 " return \n";
4266 llvm::raw_string_ostream comparatorOs(comparator);
4267 for (const auto &attrOrProp : attrOrProperties) {
4268 if (const auto *namedProperty =
4269 llvm::dyn_cast_if_present<const NamedProperty *>(Val: attrOrProp)) {
4270 StringRef name = namedProperty->name;
4271 if (name.empty())
4272 report_fatal_error(reason: "missing name for property");
4273 std::string camelName =
4274 convertToCamelFromSnakeCase(input: name, /*capitalizeFirst=*/true);
4275 auto &prop = namedProperty->prop;
4276 // Generate the data member using the storage type.
4277 os << " using " << name << "Ty = " << prop.getStorageType() << ";\n"
4278 << " " << name << "Ty " << name;
4279 if (prop.hasStorageTypeValueOverride())
4280 os << " = " << prop.getStorageTypeValueOverride();
4281 else if (prop.hasDefaultValue())
4282 os << " = " << tgfmt(fmt: prop.getDefaultValue(), ctx: &fctx);
4283 comparatorOs << " rhs." << name << " == this->" << name
4284 << " &&\n";
4285 // Emit accessors using the interface type.
4286 const char *accessorFmt = R"decl(;
4287 {0} get{1}() const {
4288 auto &propStorage = this->{2};
4289 return {3};
4290 }
4291 void set{1}({0} propValue) {
4292 auto &propStorage = this->{2};
4293 {4};
4294 }
4295)decl";
4296 FmtContext fctx;
4297 os << formatv(Fmt: accessorFmt, Vals: prop.getInterfaceType(), Vals&: camelName, Vals&: name,
4298 Vals: tgfmt(fmt: prop.getConvertFromStorageCall(),
4299 ctx: &fctx.addSubst(placeholder: "_storage", subst: propertyStorage)),
4300 Vals: tgfmt(fmt: prop.getAssignToStorageCall(),
4301 ctx: &fctx.addSubst(placeholder: "_value", subst: propertyValue)
4302 .addSubst(placeholder: "_storage", subst: propertyStorage)));
4303 continue;
4304 }
4305 const auto *namedAttr =
4306 llvm::dyn_cast_if_present<const AttributeMetadata *>(Val: attrOrProp);
4307 const Attribute *attr = nullptr;
4308 if (namedAttr->constraint)
4309 attr = &*namedAttr->constraint;
4310 StringRef name = namedAttr->attrName;
4311 if (name.empty())
4312 report_fatal_error(reason: "missing name for property attr");
4313 std::string camelName =
4314 convertToCamelFromSnakeCase(input: name, /*capitalizeFirst=*/true);
4315 // Generate the data member using the storage type.
4316 StringRef storageType;
4317 if (attr) {
4318 storageType = attr->getStorageType();
4319 } else {
4320 if (name != operandSegmentAttrName && name != resultSegmentAttrName) {
4321 report_fatal_error(reason: "unexpected AttributeMetadata");
4322 }
4323 // TODO: update to use native integers.
4324 storageType = "::mlir::DenseI32ArrayAttr";
4325 }
4326 os << " using " << name << "Ty = " << storageType << ";\n"
4327 << " " << name << "Ty " << name << ";\n";
4328 comparatorOs << " rhs." << name << " == this->" << name << " &&\n";
4329
4330 // Emit accessors using the interface type.
4331 if (attr) {
4332 const char *accessorFmt = R"decl(
4333 auto get{0}() const {
4334 auto &propStorage = this->{1};
4335 return ::llvm::{2}<{3}>(propStorage);
4336 }
4337 void set{0}(const {3} &propValue) {
4338 this->{1} = propValue;
4339 }
4340)decl";
4341 os << formatv(Fmt: accessorFmt, Vals&: camelName, Vals&: name,
4342 Vals: attr->isOptional() || attr->hasDefaultValue()
4343 ? "dyn_cast_or_null"
4344 : "cast",
4345 Vals&: storageType);
4346 }
4347 }
4348 comparatorOs << " true;\n }\n"
4349 " bool operator!=(const Properties &rhs) const {\n"
4350 " return !(*this == rhs);\n"
4351 " }\n";
4352 os << comparator;
4353 os << " };\n";
4354
4355 if (attrOrProperties.empty())
4356 genericAdaptorBase.declare<UsingDeclaration>(args: "Properties",
4357 args: "::mlir::EmptyProperties");
4358 else
4359 genericAdaptorBase.declare<ExtraClassDeclaration>(
4360 args: std::move(declarations));
4361 }
4362
4363 genericAdaptorBase.declare<VisibilityDeclaration>(args: Visibility::Protected);
4364 genericAdaptorBase.declare<Field>(args: "::mlir::DictionaryAttr", args: "odsAttrs");
4365 genericAdaptorBase.declare<Field>(args: "::std::optional<::mlir::OperationName>",
4366 args: "odsOpName");
4367 genericAdaptorBase.declare<Field>(args: "Properties", args: "properties");
4368 genericAdaptorBase.declare<Field>(args: "::mlir::RegionRange", args: "odsRegions");
4369
4370 genericAdaptor.addTemplateParam(param: "RangeT");
4371 genericAdaptor.addField(type: "RangeT", name: "odsOperands");
4372 genericAdaptor.addParent(
4373 parent: ParentClass("detail::" + genericAdaptorBase.getClassName()));
4374 genericAdaptor.declare<UsingDeclaration>(
4375 args: "ValueT", args: "::llvm::detail::ValueOfRange<RangeT>");
4376 genericAdaptor.declare<UsingDeclaration>(
4377 args: "Base", args: "detail::" + genericAdaptorBase.getClassName());
4378
4379 const auto *attrSizedOperands =
4380 op.getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments");
4381 {
4382 SmallVector<MethodParameter> paramList;
4383 // Properties can't be given a default constructor here due to Properties
4384 // struct being defined in the enclosing class which isn't complete by
4385 // here.
4386 paramList.emplace_back(Args: "::mlir::DictionaryAttr", Args: "attrs");
4387 paramList.emplace_back(Args: "const Properties &", Args: "properties");
4388 paramList.emplace_back(Args: "::mlir::RegionRange", Args: "regions", Args: "{}");
4389 auto *baseConstructor =
4390 genericAdaptorBase.addConstructor<Method::Inline>(args&: paramList);
4391 baseConstructor->addMemberInitializer(name: "odsAttrs", value: "attrs");
4392 baseConstructor->addMemberInitializer(name: "properties", value: "properties");
4393 baseConstructor->addMemberInitializer(name: "odsRegions", value: "regions");
4394
4395 MethodBody &body = baseConstructor->body();
4396 body.indent() << "if (odsAttrs)\n";
4397 body.indent() << formatv(
4398 Fmt: "odsOpName.emplace(\"{0}\", odsAttrs.getContext());\n",
4399 Vals: op.getOperationName());
4400
4401 paramList.insert(I: paramList.begin(), Elt: MethodParameter("RangeT", "values"));
4402 auto *constructor = genericAdaptor.addConstructor(args&: paramList);
4403 constructor->addMemberInitializer(name: "Base", value: "attrs, properties, regions");
4404 constructor->addMemberInitializer(name: "odsOperands", value: "values");
4405
4406 // Add a forwarding constructor that accepts PropertyRef instead of a
4407 // concrete properties struct. It checks for null and casts to the actual
4408 // properties type.
4409 paramList[1] = MethodParameter("::mlir::DictionaryAttr", "attrs");
4410 paramList[2] = MethodParameter("::mlir::PropertyRef", "properties");
4411 auto *propertyRefConstructor =
4412 genericAdaptor.addConstructor(args: std::move(paramList));
4413 propertyRefConstructor->addMemberInitializer(
4414 name: genericAdaptor.getClassName(),
4415 value: "values, "
4416 "attrs, "
4417 "(properties ? *properties.as<Properties *>() : Properties{}), "
4418 "regions");
4419
4420 // Add forwarding constructor that constructs Properties.
4421 SmallVector<MethodParameter> noPropertiesParamList;
4422 noPropertiesParamList.emplace_back(Args: "RangeT", Args: "values");
4423 noPropertiesParamList.emplace_back(Args: "::mlir::DictionaryAttr", Args: "attrs",
4424 Args: attrSizedOperands ? "" : "nullptr");
4425 auto *noPropertiesConstructor =
4426 genericAdaptor.addConstructor(args: std::move(noPropertiesParamList));
4427 noPropertiesConstructor->addMemberInitializer(name: genericAdaptor.getClassName(),
4428 value: "values, "
4429 "attrs, "
4430 "Properties{}, "
4431 "{}");
4432 }
4433
4434 // Create a constructor that creates a new generic adaptor by copying
4435 // everything from another adaptor, except for the values.
4436 {
4437 SmallVector<MethodParameter> paramList;
4438 paramList.emplace_back(Args: "RangeT", Args: "values");
4439 paramList.emplace_back(Args: "const " + op.getGenericAdaptorName() + "Base &",
4440 Args: "base");
4441 auto *constructor =
4442 genericAdaptor.addConstructor<Method::Inline>(args&: paramList);
4443 constructor->addMemberInitializer(name: "Base", value: "base");
4444 constructor->addMemberInitializer(name: "odsOperands", value: "values");
4445 }
4446
4447 // Create constructors constructing the adaptor from an instance of the op.
4448 // This takes the attributes, properties and regions from the op instance
4449 // and the value range from the parameter.
4450 {
4451 // Base class is in the cpp file and can simply access the members of the op
4452 // class to initialize the template independent fields. Emit it out-of-line
4453 // because we need the op to be defined to access its properties.
4454 Constructor *constructor = genericAdaptorBase.addConstructor(
4455 args: MethodParameter(op.getCppClassName(), "op"));
4456 constructor->addMemberInitializer(name: "odsAttrs",
4457 value: "op->getRawDictionaryAttrs()");
4458 // Retrieve the operation name from the op directly.
4459 constructor->addMemberInitializer(name: "odsOpName", value: "op->getName()");
4460 constructor->addMemberInitializer(name: "properties", value: "op.getProperties()");
4461 constructor->addMemberInitializer(name: "odsRegions", value: "op->getRegions()");
4462
4463 // Generic adaptor is templated and therefore defined inline in the header.
4464 // We cannot use the Op class here as it is an incomplete type (we have a
4465 // circular reference between the two).
4466 // Use a template trick to make the constructor be instantiated at call site
4467 // when the op class is complete.
4468 constructor = genericAdaptor.addConstructor(
4469 args: MethodParameter("RangeT", "values"), args: MethodParameter("LateInst", "op"));
4470 constructor->addTemplateParam(param: "LateInst = " + op.getCppClassName());
4471 constructor->addTemplateParam(
4472 param: "= std::enable_if_t<std::is_same_v<LateInst, " + op.getCppClassName() +
4473 ">>");
4474 constructor->addMemberInitializer(name: "Base", value: "op");
4475 constructor->addMemberInitializer(name: "odsOperands", value: "values");
4476 }
4477
4478 std::string sizeAttrInit;
4479 if (op.getTrait(trait: "::mlir::OpTrait::AttrSizedOperandSegments")) {
4480 sizeAttrInit = formatv(Fmt: adapterSegmentSizeAttrInitCodeProperties,
4481 Vals: "getProperties().operandSegmentSizes");
4482 }
4483 generateNamedOperandGetters(op, opClass&: genericAdaptor,
4484 /*genericAdaptorBase=*/&genericAdaptorBase,
4485 /*sizeAttrInit=*/sizeAttrInit,
4486 /*rangeType=*/"RangeT",
4487 /*rangeElementType=*/"ValueT",
4488 /*rangeBeginCall=*/"odsOperands.begin()",
4489 /*rangeSizeCall=*/"odsOperands.size()",
4490 /*getOperandCallPattern=*/"odsOperands[{0}]");
4491
4492 // Any invalid overlap for `getOperands` will have been diagnosed before
4493 // here already.
4494 if (auto *m = genericAdaptor.addMethod(retType: "RangeT", name: "getOperands"))
4495 m->body() << " return odsOperands;";
4496
4497 fctx.withBuilder(subst: "::mlir::Builder(odsAttrs.getContext())");
4498
4499 // Generate named accessor with Attribute return type.
4500 auto emitAttrWithStorageType = [&](StringRef name, StringRef emitName,
4501 Attribute attr) {
4502 // The method body is trivial if the attribute does not have a default
4503 // value, in which case the default value may be arbitrary code.
4504 auto *method = genericAdaptorBase.addMethod(
4505 retType: attr.getStorageType(), name: emitName + "Attr",
4506 properties: attr.hasDefaultValue() ? Method::Properties::None
4507 : Method::Properties::Inline);
4508 ERROR_IF_PRUNED(method, "Adaptor::" + emitName + "Attr", op);
4509 auto &body = method->body().indent();
4510 body << formatv(
4511 Fmt: "auto attr = ::llvm::{1}<{2}>({0});\n", Vals: emitHelper.getAttr(attrName: name),
4512 Vals: attr.hasDefaultValue() || attr.isOptional() ? "dyn_cast_or_null"
4513 : "cast",
4514 Vals: attr.getStorageType());
4515
4516 if (attr.hasDefaultValue() && attr.isOptional()) {
4517 // Use the default value if attribute is not set.
4518 // TODO: this is inefficient, we are recreating the attribute for every
4519 // call. This should be set instead.
4520 std::string defaultValue =
4521 std::string(tgfmt(fmt: attr.getConstBuilderTemplate(), ctx: &fctx,
4522 vals: tgfmt(fmt: attr.getDefaultValue(), ctx: &fctx)));
4523 body << "if (!attr)\n attr = " << defaultValue << ";\n";
4524 }
4525 body << "return attr;\n";
4526 };
4527
4528 auto *m =
4529 genericAdaptorBase.addInlineMethod(retType: "const Properties &", name: "getProperties");
4530 ERROR_IF_PRUNED(m, "Adaptor::getProperties", op);
4531 m->body() << " return properties;";
4532 {
4533 auto *m = genericAdaptorBase.addInlineMethod(retType: "::mlir::DictionaryAttr",
4534 name: "getAttributes");
4535 ERROR_IF_PRUNED(m, "Adaptor::getAttributes", op);
4536 m->body() << " return odsAttrs;";
4537 }
4538 for (auto &namedProp : op.getProperties()) {
4539 std::string name = op.getGetterName(name: namedProp.name);
4540 emitPropGetter(opClass&: genericAdaptorBase, op, name, prop: namedProp.prop);
4541 }
4542
4543 for (auto &namedAttr : op.getAttributes()) {
4544 const auto &name = namedAttr.name;
4545 const auto &attr = namedAttr.attr;
4546 if (attr.isDerivedAttr())
4547 continue;
4548 std::string emitName = op.getGetterName(name);
4549 emitAttrWithStorageType(name, emitName, attr);
4550 emitAttrGetterWithReturnType(fctx, opClass&: genericAdaptorBase, op, name: emitName, attr);
4551 }
4552
4553 unsigned numRegions = op.getNumRegions();
4554 for (unsigned i = 0; i < numRegions; ++i) {
4555 const auto &region = op.getRegion(index: i);
4556 if (region.name.empty())
4557 continue;
4558
4559 // Generate the accessors for a variadic region.
4560 std::string name = op.getGetterName(name: region.name);
4561 if (region.isVariadic()) {
4562 auto *m = genericAdaptorBase.addInlineMethod(retType: "::mlir::RegionRange", name);
4563 ERROR_IF_PRUNED(m, "Adaptor::" + name, op);
4564 m->body() << formatv(Fmt: " return odsRegions.drop_front({0});", Vals&: i);
4565 continue;
4566 }
4567
4568 auto *m = genericAdaptorBase.addInlineMethod(retType: "::mlir::Region &", name);
4569 ERROR_IF_PRUNED(m, "Adaptor::" + name, op);
4570 m->body() << formatv(Fmt: " return *odsRegions[{0}];", Vals&: i);
4571 }
4572 if (numRegions > 0) {
4573 // Any invalid overlap for `getRegions` will have been diagnosed before
4574 // here already.
4575 if (auto *m = genericAdaptorBase.addInlineMethod(retType: "::mlir::RegionRange",
4576 name: "getRegions"))
4577 m->body() << " return odsRegions;";
4578 }
4579
4580 StringRef genericAdaptorClassName = genericAdaptor.getClassName();
4581 adaptor.addParent(parent: ParentClass(genericAdaptorClassName))
4582 .addTemplateParam(param: "::mlir::ValueRange");
4583 adaptor.declare<VisibilityDeclaration>(args: Visibility::Public);
4584 adaptor.declare<UsingDeclaration>(args: genericAdaptorClassName +
4585 "::" + genericAdaptorClassName);
4586 {
4587 // Constructor taking the Op as single parameter.
4588 auto *constructor =
4589 adaptor.addConstructor(args: MethodParameter(op.getCppClassName(), "op"));
4590 constructor->addMemberInitializer(name&: genericAdaptorClassName,
4591 value: "op->getOperands(), op");
4592 }
4593
4594 // Add verification function.
4595 addVerification();
4596
4597 genericAdaptorBase.finalize();
4598 genericAdaptor.finalize();
4599 adaptor.finalize();
4600}
4601
4602void OpOperandAdaptorEmitter::addVerification() {
4603 auto *method = adaptor.addMethod(retType: "::llvm::LogicalResult", name: "verify",
4604 args: MethodParameter("::mlir::Location", "loc"));
4605 ERROR_IF_PRUNED(method, "verify", op);
4606 auto &body = method->body();
4607
4608 FmtContext verifyCtx;
4609 populateSubstitutions(emitHelper, ctx&: verifyCtx);
4610 genPropertyVerifier(emitHelper, ctx&: verifyCtx, body, staticVerifierEmitter);
4611 genAttributeVerifier(emitHelper, ctx&: verifyCtx, body, staticVerifierEmitter);
4612
4613 body << " return ::mlir::success();";
4614}
4615
4616void OpOperandAdaptorEmitter::emitDecl(
4617 const Operator &op,
4618 const StaticVerifierFunctionEmitter &staticVerifierEmitter,
4619 raw_ostream &os) {
4620 OpOperandAdaptorEmitter emitter(op, staticVerifierEmitter);
4621 {
4622 NamespaceEmitter ns(os, "detail");
4623 emitter.genericAdaptorBase.writeDeclTo(rawOs&: os);
4624 }
4625 emitter.genericAdaptor.writeDeclTo(rawOs&: os);
4626 emitter.adaptor.writeDeclTo(rawOs&: os);
4627}
4628
4629void OpOperandAdaptorEmitter::emitDef(
4630 const Operator &op,
4631 const StaticVerifierFunctionEmitter &staticVerifierEmitter,
4632 raw_ostream &os) {
4633 OpOperandAdaptorEmitter emitter(op, staticVerifierEmitter);
4634 {
4635 NamespaceEmitter ns(os, "detail");
4636 emitter.genericAdaptorBase.writeDefTo(rawOs&: os);
4637 }
4638 emitter.genericAdaptor.writeDefTo(rawOs&: os);
4639 emitter.adaptor.writeDefTo(rawOs&: os);
4640}
4641
4642/// Headers declaring the C++ constructs used by every generated op declaration.
4643static constexpr StringRef opDeclBaseIncludes[] = {
4644 "mlir/IR/OpDefinition.h",
4645};
4646
4647/// Emit the declarations for the provided op classes.
4648static void emitOpClassDecls(const RecordKeeper &records,
4649 ArrayRef<const Record *> defs, raw_ostream &os) {
4650 // First emit forward declaration for each class, this allows them to refer
4651 // to each others in traits for example.
4652 for (const Record *def : defs) {
4653 Operator op(*def);
4654 NamespaceEmitter emitter(os, op.getCppNamespace());
4655 tblgen::emitSummaryAndDescComments(os, summary: op.getSummary(),
4656 description: op.getDescription());
4657 os << "class " << op.getCppClassName() << ";\n";
4658 }
4659
4660 // Emit the op class declarations.
4661 IfDefEmitter scope(os, "GET_OP_CLASSES");
4662 if (defs.empty())
4663 return;
4664 StaticVerifierFunctionEmitter staticVerifierEmitter(os, records);
4665 staticVerifierEmitter.collectOpConstraints(opDefs: defs);
4666
4667 // The required includes are only known once all declarations were
4668 // generated, so buffer the declarations until the includes are emitted.
4669 SmallVector<StringRef> includes(std::begin(arr: opDeclBaseIncludes),
4670 std::end(arr: opDeclBaseIncludes));
4671 std::string declarations;
4672 raw_string_ostream declOs(declarations);
4673 for (const Record *def : defs) {
4674 Operator op(*def);
4675 OpOrAdaptorHelper emitHelper(op, /*emitForOp=*/true);
4676 {
4677 NamespaceEmitter emitter(declOs, op.getCppNamespace());
4678 declOs << formatv(Fmt: opCommentHeader, Vals: op.getQualCppClassName(),
4679 Vals: "declarations");
4680 OpOperandAdaptorEmitter::emitDecl(op, staticVerifierEmitter, os&: declOs);
4681 OpEmitter::emitDecl(op, os&: declOs, staticVerifierEmitter, includes);
4682 }
4683 // Emit the TypeID explicit specialization to have a single definition.
4684 if (!op.getCppNamespace().empty()) {
4685 declOs << "MLIR_DECLARE_EXPLICIT_TYPE_ID(" << op.getCppNamespace()
4686 << "::" << op.getCppClassName() << ")\n";
4687 if (emitHelper.hasNonEmptyPropertiesStruct())
4688 declOs << "MLIR_DECLARE_EXPLICIT_TYPE_ID(" << op.getCppNamespace()
4689 << "::detail::" << op.getCppClassName()
4690 << "GenericAdaptorBase::Properties)\n";
4691 declOs << "\n";
4692 }
4693 }
4694 tblgen::emitIncludes(os, headers: includes);
4695 os << declarations;
4696}
4697
4698/// Headers declaring the C++ constructs used by every generated op definition,
4699/// beyond those already included by the op declarations.
4700static constexpr StringRef opDefBaseIncludes[] = {
4701 // `::mlir::Builder`, `::mlir::OpBuilder`, `::mlir::ImplicitLocOpBuilder`.
4702 "mlir/IR/Builders.h",
4703 // `::mlir::DenseI32ArrayAttr`, `::mlir::DictionaryAttr`,
4704 // `::mlir::StringAttr`, `::mlir::TypeAttr`.
4705 "mlir/IR/BuiltinAttributes.h",
4706 // `::mlir::FunctionType`.
4707 "mlir/IR/BuiltinTypes.h",
4708 // `::mlir::OpAsmParser`, `::mlir::OpAsmParser::UnresolvedOperand`,
4709 // `::mlir::OpAsmPrinter`, `::mlir::detail::OperandParserStorage`.
4710 "mlir/IR/OpImplementation.h",
4711};
4712
4713/// Emit the definitions for the provided op classes.
4714static void emitOpClassDefs(const RecordKeeper &records,
4715 ArrayRef<const Record *> defs, raw_ostream &os,
4716 StringRef constraintPrefix = "") {
4717 if (defs.empty())
4718 return;
4719
4720 // The required includes are only known once all definitions were generated,
4721 // so buffer the definitions until the includes are emitted.
4722 SmallVector<StringRef> includes(std::begin(arr: opDefBaseIncludes),
4723 std::end(arr: opDefBaseIncludes));
4724 std::string definitions;
4725 raw_string_ostream defOs(definitions);
4726
4727 // Generate all of the locally instantiated methods first.
4728 StaticVerifierFunctionEmitter staticVerifierEmitter(defOs, records,
4729 constraintPrefix);
4730 defOs << formatv(Fmt: opCommentHeader, Vals: "Local Utility Method", Vals: "Definitions");
4731 staticVerifierEmitter.collectOpConstraints(opDefs: defs);
4732 staticVerifierEmitter.emitOpConstraints();
4733
4734 // Emit the classes.
4735 for (const Record *def : defs) {
4736 Operator op(*def);
4737 OpOrAdaptorHelper emitHelper(op, /*emitForOp=*/true);
4738 {
4739 NamespaceEmitter emitter(defOs, op.getCppNamespace());
4740 defOs << formatv(Fmt: opCommentHeader, Vals: op.getQualCppClassName(),
4741 Vals: "definitions");
4742 OpOperandAdaptorEmitter::emitDef(op, staticVerifierEmitter, os&: defOs);
4743 OpEmitter::emitDef(op, os&: defOs, staticVerifierEmitter, includes);
4744 }
4745 // Emit the TypeID explicit specialization to have a single definition.
4746 if (!op.getCppNamespace().empty()) {
4747 defOs << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" << op.getCppNamespace()
4748 << "::" << op.getCppClassName() << ")\n";
4749 if (emitHelper.hasNonEmptyPropertiesStruct())
4750 defOs << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" << op.getCppNamespace()
4751 << "::detail::" << op.getCppClassName()
4752 << "GenericAdaptorBase::Properties)\n";
4753 defOs << "\n";
4754 }
4755 }
4756 tblgen::emitIncludes(os, headers: includes);
4757 os << definitions;
4758}
4759
4760/// When generating the ops of an op partition, forward declare the ops of the
4761/// other partitions, so that the declarations can refer to ops of any
4762/// partition, like they could without partitions.
4763static void
4764emitOtherOpPartitionForwardDecls(ArrayRef<const Record *> otherPartitionDefs,
4765 raw_ostream &os) {
4766 for (const Record *def : otherPartitionDefs) {
4767 Operator op(def);
4768 NamespaceEmitter emitter(os, op.getCppNamespace());
4769 os << "class " << op.getCppClassName() << ";\n";
4770 }
4771}
4772
4773/// Returns the dialects of the ops in `defs`, in order of first appearance.
4774static SmallVector<Dialect> getOpDialects(ArrayRef<const Record *> defs) {
4775 llvm::SetVector<const Record *> dialectDefs;
4776 for (const Record *def : defs)
4777 dialectDefs.insert(X: def->getValueAsDef(FieldName: "opDialect"));
4778 return llvm::to_vector_of<Dialect>(Range&: dialectDefs);
4779}
4780
4781/// Returns the sorted names of the op partitions within `dialect` where
4782/// `defs` and `otherPartitionDefs` are all the dialect's operations.
4783static SmallVector<StringRef>
4784getAllOpPartitions(const Dialect &dialect, ArrayRef<const Record *> defs,
4785 ArrayRef<const Record *> otherPartitionDefs) {
4786 llvm::SetVector<StringRef> partitions;
4787 for (const Record *def :
4788 concat<const Record *const>(Ranges&: defs, Ranges&: otherPartitionDefs))
4789 if (def->getValueAsDef(FieldName: "opDialect") == dialect.getDef())
4790 partitions.insert(X: getOpPartitionName(def: *def));
4791
4792 SmallVector<StringRef> result = partitions.takeVector();
4793 llvm::sort(C&: result);
4794 return result;
4795}
4796
4797/// Emit the declarations of the op registration hooks of every dialect with
4798/// ops in the specified op partition: the hooks of all partitions of the
4799/// dialect and the hook that calls them.
4800static void emitOpPartitionRegistrationHookDecls(
4801 ArrayRef<const Record *> defs, ArrayRef<const Record *> otherPartitionDefs,
4802 raw_ostream &os) {
4803 const char *const opRegistrationHook = "void {0}({1}::{2} *dialect);\n";
4804 for (const Dialect &dialect : getOpDialects(defs)) {
4805 DialectNamespaceEmitter ns(os, dialect);
4806 os << formatv(Fmt: opRegistrationHook, Vals: getOpRegistrationHookName(dialect),
4807 Vals: dialect.getCppNamespace(), Vals: dialect.getCppClassName());
4808 for (StringRef partition :
4809 getAllOpPartitions(dialect, defs, otherPartitionDefs))
4810 os << formatv(Fmt: opRegistrationHook,
4811 Vals: getOpRegistrationHookName(dialect, partition),
4812 Vals: dialect.getCppNamespace(), Vals: dialect.getCppClassName());
4813 }
4814}
4815
4816/// Emit op declarations for all op records.
4817static bool emitOpDecls(const RecordKeeper &records, raw_ostream &os) {
4818 emitSourceFileHeader(Desc: "Op Declarations", OS&: os, Record: records);
4819
4820 std::vector<const Record *> otherPartitionDefs;
4821 std::vector<const Record *> defs =
4822 getRequestedOpDefinitions(records, otherPartitionDefs: &otherPartitionDefs);
4823 emitOtherOpPartitionForwardDecls(otherPartitionDefs, os);
4824 emitOpClassDecls(records, defs, os);
4825
4826 if (!getSpecifiedOpPartition().empty()) {
4827 // Also emit partition registration hooks if partitons were requested.
4828 emitOpPartitionRegistrationHookDecls(defs, otherPartitionDefs, os);
4829 return false;
4830 }
4831
4832 // If we are generating sharded op definitions, emit the sharded op
4833 // registration hooks.
4834 SmallVector<ArrayRef<const Record *>, 4> shardedDefs;
4835 shardOpDefinitions(defs, shardedDefs);
4836 if (defs.empty() || shardedDefs.size() <= 1)
4837 return false;
4838
4839 Dialect dialect = Operator(defs.front()).getDialect();
4840 DialectNamespaceEmitter ns(os, dialect);
4841
4842 const char *const opRegistrationHook =
4843 "void register{0}Operations{1}({2}::{0} *dialect);\n";
4844 os << formatv(Fmt: opRegistrationHook, Vals: dialect.getCppClassName(), Vals: "",
4845 Vals: dialect.getCppNamespace());
4846 for (unsigned i = 0; i < shardedDefs.size(); ++i) {
4847 os << formatv(Fmt: opRegistrationHook, Vals: dialect.getCppClassName(), Vals&: i,
4848 Vals: dialect.getCppNamespace());
4849 }
4850
4851 return false;
4852}
4853
4854/// Generate the dialect op registration hook and the op class definitions for a
4855/// shard of ops.
4856static void emitOpDefShard(const RecordKeeper &records,
4857 ArrayRef<const Record *> defs,
4858 const Dialect &dialect, unsigned shardIndex,
4859 unsigned shardCount, raw_ostream &os) {
4860 std::string shardGuard = "GET_OP_DEFS_";
4861 std::string indexStr = std::to_string(val: shardIndex);
4862 shardGuard += indexStr;
4863 IfDefEmitter scope(os, shardGuard);
4864
4865 // Emit the op registration hook in the first shard.
4866 const char *const opRegistrationHook =
4867 "void {0}::register{1}Operations{2}({0}::{1} *dialect) {{\n";
4868 if (shardIndex == 0) {
4869 os << formatv(Fmt: opRegistrationHook, Vals: dialect.getCppNamespace(),
4870 Vals: dialect.getCppClassName(), Vals: "");
4871 for (unsigned i = 0; i < shardCount; ++i) {
4872 os << formatv(Fmt: " {0}::register{1}Operations{2}(dialect);\n",
4873 Vals: dialect.getCppNamespace(), Vals: dialect.getCppClassName(), Vals&: i);
4874 }
4875 os << "}\n";
4876 }
4877
4878 // Generate the per-shard op registration hook.
4879 os << formatv(Fmt: opCommentHeader, Vals: dialect.getCppClassName(),
4880 Vals: "Op Registration Hook")
4881 << formatv(Fmt: opRegistrationHook, Vals: dialect.getCppNamespace(),
4882 Vals: dialect.getCppClassName(), Vals&: shardIndex);
4883 for (const Record *def : defs) {
4884 os << formatv(Fmt: " ::mlir::RegisteredOperationName::insert<{0}>(*dialect);\n",
4885 Vals: Operator(def).getQualCppClassName());
4886 }
4887 os << "}\n";
4888
4889 // Generate the per-shard op definitions.
4890 emitOpClassDefs(records, defs, os, constraintPrefix: indexStr);
4891}
4892
4893/// Emit the definitions of the op registration hooks of the specified op
4894/// partition for every dialect with ops in the partition. The first partition
4895/// (sorted by name Lexicographically) is additionally responsible for emitting
4896/// the dialect's top level 'register<Dialect>Operations' hook that dispatches
4897/// to all other partitions' op registration hooks.
4898static void
4899emitOpPartitionRegistrationHookDefs(ArrayRef<const Record *> defs,
4900 ArrayRef<const Record *> otherPartitionDefs,
4901 raw_ostream &os) {
4902 StringRef specifiedPartition = getSpecifiedOpPartition();
4903 const char *const opRegistrationHook =
4904 "void {0}::{1}({0}::{2} *dialect) {{\n";
4905 for (const Dialect &dialect : getOpDialects(defs)) {
4906 SmallVector<StringRef> partitions =
4907 getAllOpPartitions(dialect, defs, otherPartitionDefs);
4908
4909 // Dialect's top-level registration hook. Should be called by users in the
4910 // 'initialize' method.
4911 if (partitions.front() == specifiedPartition) {
4912 os << formatv(Fmt: opCommentHeader, Vals: dialect.getCppClassName(),
4913 Vals: "Op Registration Hook")
4914 << formatv(Fmt: opRegistrationHook, Vals: dialect.getCppNamespace(),
4915 Vals: getOpRegistrationHookName(dialect),
4916 Vals: dialect.getCppClassName());
4917
4918 for (StringRef partition : partitions)
4919 os << formatv(Fmt: " {0}::{1}(dialect);\n", Vals: dialect.getCppNamespace(),
4920 Vals: getOpRegistrationHookName(dialect, partition));
4921 os << "}\n";
4922 }
4923
4924 os << formatv(Fmt: opCommentHeader, Vals: dialect.getCppClassName(),
4925 Vals: (specifiedPartition + " Op Registration Hook").str())
4926 << formatv(Fmt: opRegistrationHook, Vals: dialect.getCppNamespace(),
4927 Vals: getOpRegistrationHookName(dialect, partition: specifiedPartition),
4928 Vals: dialect.getCppClassName());
4929
4930 // The hooks register each op with a separate call instead of
4931 // `addOperations<...>()`, whose single instantiation over all ops of a
4932 // partition takes noticeably longer to compile.
4933 for (const Record *def : defs)
4934 if (def->getValueAsDef(FieldName: "opDialect") == dialect.getDef())
4935 os << formatv(
4936 Fmt: " ::mlir::RegisteredOperationName::insert<{0}>(*dialect);\n",
4937 Vals: Operator(def).getQualCppClassName());
4938 os << "}\n";
4939 }
4940}
4941
4942/// Returns the tag that makes the static verifier functions generated for the
4943/// specified op partition unique, so that the definitions of several
4944/// partitions can be included in one compilation unit. The partition name is
4945/// prefixed with its length and the tag ends with `_`, so that the partition
4946/// name and the file name that follows the tag cannot run together.
4947static std::string getOpPartitionConstraintTag() {
4948 StringRef partition = getSpecifiedOpPartition();
4949 return std::to_string(val: partition.size()) + partition.str() + "_";
4950}
4951
4952/// Emit op definitions for all op records.
4953static bool emitOpDefs(const RecordKeeper &records, raw_ostream &os) {
4954 emitSourceFileHeader(Desc: "Op Definitions", OS&: os, Record: records);
4955
4956 std::vector<const Record *> otherPartitionDefs;
4957 std::vector<const Record *> defs =
4958 getRequestedOpDefinitions(records, otherPartitionDefs: &otherPartitionDefs);
4959
4960 // If an op partition was specified, emit its class definitions and
4961 // registration hooks.
4962 if (!getSpecifiedOpPartition().empty()) {
4963 IfDefEmitter scope(os, "GET_OP_CLASSES");
4964 emitOpClassDefs(records, defs, os, constraintPrefix: getOpPartitionConstraintTag());
4965 emitOpPartitionRegistrationHookDefs(defs, otherPartitionDefs, os);
4966 return false;
4967 }
4968
4969 SmallVector<ArrayRef<const Record *>, 4> shardedDefs;
4970 shardOpDefinitions(defs, shardedDefs);
4971
4972 // If no shard was requested, emit the regular op list and class definitions.
4973 if (shardedDefs.size() == 1) {
4974 {
4975 IfDefEmitter scope(os, "GET_OP_LIST");
4976 interleave(
4977 c: defs, os,
4978 each_fn: [&](const Record *def) { os << Operator(def).getQualCppClassName(); },
4979 separator: ",\n");
4980 }
4981 {
4982 IfDefEmitter scope(os, "GET_OP_CLASSES");
4983 emitOpClassDefs(records, defs, os);
4984 }
4985 return false;
4986 }
4987
4988 if (defs.empty())
4989 return false;
4990 Dialect dialect = Operator(defs.front()).getDialect();
4991 for (auto [idx, value] : llvm::enumerate(First&: shardedDefs)) {
4992 emitOpDefShard(records, defs: value, dialect, shardIndex: idx, shardCount: shardedDefs.size(), os);
4993 }
4994 return false;
4995}
4996
4997static mlir::GenRegistration
4998 genOpDecls("gen-op-decls", "Generate op declarations",
4999 [](const RecordKeeper &records, raw_ostream &os) {
5000 return emitOpDecls(records, os);
5001 });
5002
5003static mlir::GenRegistration genOpDefs("gen-op-defs", "Generate op definitions",
5004 [](const RecordKeeper &records,
5005 raw_ostream &os) {
5006 return emitOpDefs(records, os);
5007 });
5008