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 "OpFormatGen.h"
15 #include "mlir/TableGen/Format.h"
16 #include "mlir/TableGen/GenInfo.h"
17 #include "mlir/TableGen/Interfaces.h"
18 #include "mlir/TableGen/OpClass.h"
19 #include "mlir/TableGen/OpTrait.h"
20 #include "mlir/TableGen/Operator.h"
21 #include "mlir/TableGen/SideEffects.h"
22 #include "llvm/ADT/Sequence.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/Regex.h"
26 #include "llvm/Support/Signals.h"
27 #include "llvm/TableGen/Error.h"
28 #include "llvm/TableGen/Record.h"
29 #include "llvm/TableGen/TableGenBackend.h"
30 
31 #define DEBUG_TYPE "mlir-tblgen-opdefgen"
32 
33 using namespace llvm;
34 using namespace mlir;
35 using namespace mlir::tblgen;
36 
37 cl::OptionCategory opDefGenCat("Options for -gen-op-defs and -gen-op-decls");
38 
39 static cl::opt<std::string> opIncFilter(
40     "op-include-regex",
41     cl::desc("Regex of name of op's to include (no filter if empty)"),
42     cl::cat(opDefGenCat));
43 static cl::opt<std::string> opExcFilter(
44     "op-exclude-regex",
45     cl::desc("Regex of name of op's to exclude (no filter if empty)"),
46     cl::cat(opDefGenCat));
47 
48 static const char *const tblgenNamePrefix = "tblgen_";
49 static const char *const generatedArgName = "odsArg";
50 static const char *const builderOpState = "odsState";
51 
52 // The logic to calculate the actual value range for a declared operand/result
53 // of an op with variadic operands/results. Note that this logic is not for
54 // general use; it assumes all variadic operands/results must have the same
55 // number of values.
56 //
57 // {0}: The list of whether each declared operand/result is variadic.
58 // {1}: The total number of non-variadic operands/results.
59 // {2}: The total number of variadic operands/results.
60 // {3}: The total number of actual values.
61 // {4}: "operand" or "result".
62 const char *sameVariadicSizeValueRangeCalcCode = R"(
63   bool isVariadic[] = {{{0}};
64   int prevVariadicCount = 0;
65   for (unsigned i = 0; i < index; ++i)
66     if (isVariadic[i]) ++prevVariadicCount;
67 
68   // Calculate how many dynamic values a static variadic {4} corresponds to.
69   // This assumes all static variadic {4}s have the same dynamic value count.
70   int variadicSize = ({3} - {1}) / {2};
71   // `index` passed in as the parameter is the static index which counts each
72   // {4} (variadic or not) as size 1. So here for each previous static variadic
73   // {4}, we need to offset by (variadicSize - 1) to get where the dynamic
74   // value pack for this static {4} starts.
75   int start = index + (variadicSize - 1) * prevVariadicCount;
76   int size = isVariadic[index] ? variadicSize : 1;
77   return {{start, size};
78 )";
79 
80 // The logic to calculate the actual value range for a declared operand/result
81 // of an op with variadic operands/results. Note that this logic is assumes
82 // the op has an attribute specifying the size of each operand/result segment
83 // (variadic or not).
84 //
85 // {0}: The name of the attribute specifying the segment sizes.
86 const char *adapterSegmentSizeAttrInitCode = R"(
87   assert(odsAttrs && "missing segment size attribute for op");
88   auto sizeAttr = odsAttrs.get("{0}").cast<::mlir::DenseIntElementsAttr>();
89 )";
90 const char *opSegmentSizeAttrInitCode = R"(
91   auto sizeAttr = getAttrOfType<::mlir::DenseIntElementsAttr>("{0}");
92 )";
93 const char *attrSizedSegmentValueRangeCalcCode = R"(
94   unsigned start = 0;
95   for (unsigned i = 0; i < index; ++i)
96     start += (*(sizeAttr.begin() + i)).getZExtValue();
97   unsigned size = (*(sizeAttr.begin() + index)).getZExtValue();
98   return {start, size};
99 )";
100 
101 // The logic to build a range of either operand or result values.
102 //
103 // {0}: The begin iterator of the actual values.
104 // {1}: The call to generate the start and length of the value range.
105 const char *valueRangeReturnCode = R"(
106   auto valueRange = {1};
107   return {{std::next({0}, valueRange.first),
108            std::next({0}, valueRange.first + valueRange.second)};
109 )";
110 
111 static const char *const opCommentHeader = R"(
112 //===----------------------------------------------------------------------===//
113 // {0} {1}
114 //===----------------------------------------------------------------------===//
115 
116 )";
117 
118 //===----------------------------------------------------------------------===//
119 // Utility structs and functions
120 //===----------------------------------------------------------------------===//
121 
122 // Replaces all occurrences of `match` in `str` with `substitute`.
123 static std::string replaceAllSubstrs(std::string str, const std::string &match,
124                                      const std::string &substitute) {
125   std::string::size_type scanLoc = 0, matchLoc = std::string::npos;
126   while ((matchLoc = str.find(match, scanLoc)) != std::string::npos) {
127     str = str.replace(matchLoc, match.size(), substitute);
128     scanLoc = matchLoc + substitute.size();
129   }
130   return str;
131 }
132 
133 // Returns whether the record has a value of the given name that can be returned
134 // via getValueAsString.
135 static inline bool hasStringAttribute(const Record &record,
136                                       StringRef fieldName) {
137   auto valueInit = record.getValueInit(fieldName);
138   return isa<CodeInit, StringInit>(valueInit);
139 }
140 
141 static std::string getArgumentName(const Operator &op, int index) {
142   const auto &operand = op.getOperand(index);
143   if (!operand.name.empty())
144     return std::string(operand.name);
145   else
146     return std::string(formatv("{0}_{1}", generatedArgName, index));
147 }
148 
149 // Returns true if we can use unwrapped value for the given `attr` in builders.
150 static bool canUseUnwrappedRawValue(const tblgen::Attribute &attr) {
151   return attr.getReturnType() != attr.getStorageType() &&
152          // We need to wrap the raw value into an attribute in the builder impl
153          // so we need to make sure that the attribute specifies how to do that.
154          !attr.getConstBuilderTemplate().empty();
155 }
156 
157 //===----------------------------------------------------------------------===//
158 // Op emitter
159 //===----------------------------------------------------------------------===//
160 
161 namespace {
162 // Simple RAII helper for defining ifdef-undef-endif scopes.
163 class IfDefScope {
164 public:
165   IfDefScope(StringRef name, raw_ostream &os) : name(name), os(os) {
166     os << "#ifdef " << name << "\n"
167        << "#undef " << name << "\n\n";
168   }
169 
170   ~IfDefScope() { os << "\n#endif  // " << name << "\n\n"; }
171 
172 private:
173   StringRef name;
174   raw_ostream &os;
175 };
176 } // end anonymous namespace
177 
178 namespace {
179 // Helper class to emit a record into the given output stream.
180 class OpEmitter {
181 public:
182   static void emitDecl(const Operator &op, raw_ostream &os);
183   static void emitDef(const Operator &op, raw_ostream &os);
184 
185 private:
186   OpEmitter(const Operator &op);
187 
188   void emitDecl(raw_ostream &os);
189   void emitDef(raw_ostream &os);
190 
191   // Generates the OpAsmOpInterface for this operation if possible.
192   void genOpAsmInterface();
193 
194   // Generates the `getOperationName` method for this op.
195   void genOpNameGetter();
196 
197   // Generates getters for the attributes.
198   void genAttrGetters();
199 
200   // Generates setter for the attributes.
201   void genAttrSetters();
202 
203   // Generates getters for named operands.
204   void genNamedOperandGetters();
205 
206   // Generates setters for named operands.
207   void genNamedOperandSetters();
208 
209   // Generates getters for named results.
210   void genNamedResultGetters();
211 
212   // Generates getters for named regions.
213   void genNamedRegionGetters();
214 
215   // Generates getters for named successors.
216   void genNamedSuccessorGetters();
217 
218   // Generates builder methods for the operation.
219   void genBuilder();
220 
221   // Generates the build() method that takes each operand/attribute
222   // as a stand-alone parameter.
223   void genSeparateArgParamBuilder();
224 
225   // Generates the build() method that takes each operand/attribute as a
226   // stand-alone parameter. The generated build() method uses first operand's
227   // type as all results' types.
228   void genUseOperandAsResultTypeSeparateParamBuilder();
229 
230   // Generates the build() method that takes all operands/attributes
231   // collectively as one parameter. The generated build() method uses first
232   // operand's type as all results' types.
233   void genUseOperandAsResultTypeCollectiveParamBuilder();
234 
235   // Generates the build() method that takes aggregate operands/attributes
236   // parameters. This build() method uses inferred types as result types.
237   // Requires: The type needs to be inferable via InferTypeOpInterface.
238   void genInferredTypeCollectiveParamBuilder();
239 
240   // Generates the build() method that takes each operand/attribute as a
241   // stand-alone parameter. The generated build() method uses first attribute's
242   // type as all result's types.
243   void genUseAttrAsResultTypeBuilder();
244 
245   // Generates the build() method that takes all result types collectively as
246   // one parameter. Similarly for operands and attributes.
247   void genCollectiveParamBuilder();
248 
249   // The kind of parameter to generate for result types in builders.
250   enum class TypeParamKind {
251     None,       // No result type in parameter list.
252     Separate,   // A separate parameter for each result type.
253     Collective, // An ArrayRef<Type> for all result types.
254   };
255 
256   // The kind of parameter to generate for attributes in builders.
257   enum class AttrParamKind {
258     WrappedAttr,    // A wrapped MLIR Attribute instance.
259     UnwrappedValue, // A raw value without MLIR Attribute wrapper.
260   };
261 
262   // Builds the parameter list for build() method of this op. This method writes
263   // to `paramList` the comma-separated parameter list and updates
264   // `resultTypeNames` with the names for parameters for specifying result
265   // types. The given `typeParamKind` and `attrParamKind` controls how result
266   // types and attributes are placed in the parameter list.
267   void buildParamList(std::string &paramList,
268                       SmallVectorImpl<std::string> &resultTypeNames,
269                       TypeParamKind typeParamKind,
270                       AttrParamKind attrParamKind = AttrParamKind::WrappedAttr);
271 
272   // Adds op arguments and regions into operation state for build() methods.
273   void genCodeForAddingArgAndRegionForBuilder(OpMethodBody &body,
274                                               bool isRawValueAttr = false);
275 
276   // Generates canonicalizer declaration for the operation.
277   void genCanonicalizerDecls();
278 
279   // Generates the folder declaration for the operation.
280   void genFolderDecls();
281 
282   // Generates the parser for the operation.
283   void genParser();
284 
285   // Generates the printer for the operation.
286   void genPrinter();
287 
288   // Generates verify method for the operation.
289   void genVerifier();
290 
291   // Generates verify statements for operands and results in the operation.
292   // The generated code will be attached to `body`.
293   void genOperandResultVerifier(OpMethodBody &body,
294                                 Operator::value_range values,
295                                 StringRef valueKind);
296 
297   // Generates verify statements for regions in the operation.
298   // The generated code will be attached to `body`.
299   void genRegionVerifier(OpMethodBody &body);
300 
301   // Generates verify statements for successors in the operation.
302   // The generated code will be attached to `body`.
303   void genSuccessorVerifier(OpMethodBody &body);
304 
305   // Generates the traits used by the object.
306   void genTraits();
307 
308   // Generate the OpInterface methods.
309   void genOpInterfaceMethods();
310 
311   // Generate op interface method.
312   void genOpInterfaceMethod(const tblgen::InterfaceOpTrait *trait);
313 
314   // Generate the side effect interface methods.
315   void genSideEffectInterfaceMethods();
316 
317   // Generate the type inference interface methods.
318   void genTypeInterfaceMethods();
319 
320 private:
321   // The TableGen record for this op.
322   // TODO: OpEmitter should not have a Record directly,
323   // it should rather go through the Operator for better abstraction.
324   const Record &def;
325 
326   // The wrapper operator class for querying information from this op.
327   Operator op;
328 
329   // The C++ code builder for this op
330   OpClass opClass;
331 
332   // The format context for verification code generation.
333   FmtContext verifyCtx;
334 };
335 } // end anonymous namespace
336 
337 // Populate the format context `ctx` with substitutions of attributes, operands
338 // and results.
339 // - attrGet corresponds to the name of the function to call to get value of
340 //   attribute (the generated function call returns an Attribute);
341 // - operandGet corresponds to the name of the function with which to retrieve
342 //   an operand (the generaed function call returns an OperandRange);
343 // - reultGet corresponds to the name of the function to get an result (the
344 //   generated function call returns a ValueRange);
345 static void populateSubstitutions(const Operator &op, const char *attrGet,
346                                   const char *operandGet, const char *resultGet,
347                                   FmtContext &ctx) {
348   // Populate substitutions for attributes and named operands.
349   for (const auto &namedAttr : op.getAttributes())
350     ctx.addSubst(namedAttr.name,
351                  formatv("{0}(\"{1}\")", attrGet, namedAttr.name));
352   for (int i = 0, e = op.getNumOperands(); i < e; ++i) {
353     auto &value = op.getOperand(i);
354     if (value.name.empty())
355       continue;
356 
357     if (value.isVariadic())
358       ctx.addSubst(value.name, formatv("{0}({1})", operandGet, i));
359     else
360       ctx.addSubst(value.name, formatv("(*{0}({1}).begin())", operandGet, i));
361   }
362 
363   // Populate substitutions for results.
364   for (int i = 0, e = op.getNumResults(); i < e; ++i) {
365     auto &value = op.getResult(i);
366     if (value.name.empty())
367       continue;
368 
369     if (value.isVariadic())
370       ctx.addSubst(value.name, formatv("{0}({1})", resultGet, i));
371     else
372       ctx.addSubst(value.name, formatv("(*{0}({1}).begin())", resultGet, i));
373   }
374 }
375 
376 // Generate attribute verification. If emitVerificationRequiringOp is set then
377 // only verification for attributes whose value depend on op being known are
378 // emitted, else only verification that doesn't depend on the op being known are
379 // generated.
380 // - emitErrorPrefix is the prefix for the error emitting call which consists
381 //   of the entire function call up to start of error message fragment;
382 // - emitVerificationRequiringOp specifies whether verification should be
383 //   emitted for verification that require the op to exist;
384 static void genAttributeVerifier(const Operator &op, const char *attrGet,
385                                  const Twine &emitErrorPrefix,
386                                  bool emitVerificationRequiringOp,
387                                  FmtContext &ctx, OpMethodBody &body) {
388   for (const auto &namedAttr : op.getAttributes()) {
389     const auto &attr = namedAttr.attr;
390     if (attr.isDerivedAttr())
391       continue;
392 
393     auto attrName = namedAttr.name;
394     bool allowMissingAttr = attr.hasDefaultValue() || attr.isOptional();
395     auto attrPred = attr.getPredicate();
396     auto condition = attrPred.isNull() ? "" : attrPred.getCondition();
397     // There is a condition to emit only if the use of $_op and whether to
398     // emit verifications for op matches.
399     bool hasConditionToEmit = (!(condition.find("$_op") != StringRef::npos) ^
400                                emitVerificationRequiringOp);
401 
402     // Prefix with `tblgen_` to avoid hiding the attribute accessor.
403     auto varName = tblgenNamePrefix + attrName;
404 
405     // If the attribute is
406     //  1. Required (not allowed missing) and not in op verification, or
407     //  2. Has a condition that will get verified
408     // then the variable will be used.
409     //
410     // Therefore, for optional attributes whose verification requires that an
411     // op already exists for verification/emitVerificationRequiringOp is set
412     // has nothing that can be verified here.
413     if ((allowMissingAttr || emitVerificationRequiringOp) &&
414         !hasConditionToEmit)
415       continue;
416 
417     body << formatv("  {\n  auto {0} = {1}(\"{2}\");\n", varName, attrGet,
418                     attrName);
419 
420     if (!emitVerificationRequiringOp && !allowMissingAttr) {
421       body << "  if (!" << varName << ") return " << emitErrorPrefix
422            << "\"requires attribute '" << attrName << "'\");\n";
423     }
424 
425     if (!hasConditionToEmit) {
426       body << "  }\n";
427       continue;
428     }
429 
430     if (allowMissingAttr) {
431       // If the attribute has a default value, then only verify the predicate if
432       // set. This does effectively assume that the default value is valid.
433       // TODO: verify the debug value is valid (perhaps in debug mode only).
434       body << "  if (" << varName << ") {\n";
435     }
436 
437     body << tgfmt("    if (!($0)) return $1\"attribute '$2' "
438                   "failed to satisfy constraint: $3\");\n",
439                   /*ctx=*/nullptr, tgfmt(condition, &ctx.withSelf(varName)),
440                   emitErrorPrefix, attrName, attr.getDescription());
441     if (allowMissingAttr)
442       body << "  }\n";
443     body << "  }\n";
444   }
445 }
446 
447 OpEmitter::OpEmitter(const Operator &op)
448     : def(op.getDef()), op(op),
449       opClass(op.getCppClassName(), op.getExtraClassDeclaration()) {
450   verifyCtx.withOp("(*this->getOperation())");
451 
452   genTraits();
453 
454   // Generate C++ code for various op methods. The order here determines the
455   // methods in the generated file.
456   genOpAsmInterface();
457   genOpNameGetter();
458   genNamedOperandGetters();
459   genNamedOperandSetters();
460   genNamedResultGetters();
461   genNamedRegionGetters();
462   genNamedSuccessorGetters();
463   genAttrGetters();
464   genAttrSetters();
465   genBuilder();
466   genParser();
467   genPrinter();
468   genVerifier();
469   genCanonicalizerDecls();
470   genFolderDecls();
471   genOpInterfaceMethods();
472   generateOpFormat(op, opClass);
473   genSideEffectInterfaceMethods();
474   genTypeInterfaceMethods();
475 }
476 
477 void OpEmitter::emitDecl(const Operator &op, raw_ostream &os) {
478   OpEmitter(op).emitDecl(os);
479 }
480 
481 void OpEmitter::emitDef(const Operator &op, raw_ostream &os) {
482   OpEmitter(op).emitDef(os);
483 }
484 
485 void OpEmitter::emitDecl(raw_ostream &os) { opClass.writeDeclTo(os); }
486 
487 void OpEmitter::emitDef(raw_ostream &os) { opClass.writeDefTo(os); }
488 
489 void OpEmitter::genAttrGetters() {
490   FmtContext fctx;
491   fctx.withBuilder("::mlir::Builder(this->getContext())");
492 
493   // Emit the derived attribute body.
494   auto emitDerivedAttr = [&](StringRef name, Attribute attr) {
495     auto &method = opClass.newMethod(attr.getReturnType(), name);
496     auto &body = method.body();
497     body << "  " << attr.getDerivedCodeBody() << "\n";
498   };
499 
500   // Emit with return type specified.
501   auto emitAttrWithReturnType = [&](StringRef name, Attribute attr) {
502     auto &method = opClass.newMethod(attr.getReturnType(), name);
503     auto &body = method.body();
504     body << "  auto attr = " << name << "Attr();\n";
505     if (attr.hasDefaultValue()) {
506       // Returns the default value if not set.
507       // TODO: this is inefficient, we are recreating the attribute for every
508       // call. This should be set instead.
509       std::string defaultValue = std::string(
510           tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue()));
511       body << "    if (!attr)\n      return "
512            << tgfmt(attr.getConvertFromStorageCall(),
513                     &fctx.withSelf(defaultValue))
514            << ";\n";
515     }
516     body << "  return "
517          << tgfmt(attr.getConvertFromStorageCall(), &fctx.withSelf("attr"))
518          << ";\n";
519   };
520 
521   // Generate raw named accessor type. This is a wrapper class that allows
522   // referring to the attributes via accessors instead of having to use
523   // the string interface for better compile time verification.
524   auto emitAttrWithStorageType = [&](StringRef name, Attribute attr) {
525     auto &method =
526         opClass.newMethod(attr.getStorageType(), (name + "Attr").str());
527     auto &body = method.body();
528     body << "  return this->getAttr(\"" << name << "\").";
529     if (attr.isOptional() || attr.hasDefaultValue())
530       body << "dyn_cast_or_null<";
531     else
532       body << "cast<";
533     body << attr.getStorageType() << ">();";
534   };
535 
536   for (auto &namedAttr : op.getAttributes()) {
537     const auto &name = namedAttr.name;
538     const auto &attr = namedAttr.attr;
539     if (attr.isDerivedAttr()) {
540       emitDerivedAttr(name, attr);
541     } else {
542       emitAttrWithStorageType(name, attr);
543       emitAttrWithReturnType(name, attr);
544     }
545   }
546 
547   auto derivedAttrs = make_filter_range(op.getAttributes(),
548                                         [](const NamedAttribute &namedAttr) {
549                                           return namedAttr.attr.isDerivedAttr();
550                                         });
551   if (!derivedAttrs.empty()) {
552     opClass.addTrait("::mlir::DerivedAttributeOpInterface::Trait");
553     // Generate helper method to query whether a named attribute is a derived
554     // attribute. This enables, for example, avoiding adding an attribute that
555     // overlaps with a derived attribute.
556     {
557       auto &method =
558           opClass.newMethod("bool", "isDerivedAttribute",
559                             "::llvm::StringRef name", OpMethod::MP_Static);
560       auto &body = method.body();
561       for (auto namedAttr : derivedAttrs)
562         body << "  if (name == \"" << namedAttr.name << "\") return true;\n";
563       body << " return false;";
564     }
565     // Generate method to materialize derived attributes as a DictionaryAttr.
566     {
567       OpMethod &method = opClass.newMethod("::mlir::DictionaryAttr",
568                                            "materializeDerivedAttributes");
569       auto &body = method.body();
570 
571       auto nonMaterializable =
572           make_filter_range(derivedAttrs, [](const NamedAttribute &namedAttr) {
573             return namedAttr.attr.getConvertFromStorageCall().empty();
574           });
575       if (!nonMaterializable.empty()) {
576         std::string attrs;
577         llvm::raw_string_ostream os(attrs);
578         interleaveComma(nonMaterializable, os,
579                         [&](const NamedAttribute &attr) { os << attr.name; });
580         PrintWarning(
581             op.getLoc(),
582             formatv(
583                 "op has non-materialzable derived attributes '{0}', skipping",
584                 os.str()));
585         body << formatv("  emitOpError(\"op has non-materializable derived "
586                         "attributes '{0}'\");\n",
587                         attrs);
588         body << "  return nullptr;";
589         return;
590       }
591 
592       body << "  ::mlir::MLIRContext* ctx = getContext();\n";
593       body << "  ::mlir::Builder odsBuilder(ctx); (void)odsBuilder;\n";
594       body << "  return ::mlir::DictionaryAttr::get({\n";
595       interleave(
596           derivedAttrs, body,
597           [&](const NamedAttribute &namedAttr) {
598             auto tmpl = namedAttr.attr.getConvertFromStorageCall();
599             body << "    {::mlir::Identifier::get(\"" << namedAttr.name
600                  << "\", ctx),\n"
601                  << tgfmt(tmpl, &fctx.withSelf(namedAttr.name + "()")
602                                      .withBuilder("odsBuilder")
603                                      .addSubst("_ctx", "ctx"))
604                  << "}";
605           },
606           ",\n");
607       body << "\n    }, ctx);";
608     }
609   }
610 }
611 
612 void OpEmitter::genAttrSetters() {
613   // Generate raw named setter type. This is a wrapper class that allows setting
614   // to the attributes via setters instead of having to use the string interface
615   // for better compile time verification.
616   auto emitAttrWithStorageType = [&](StringRef name, Attribute attr) {
617     auto &method = opClass.newMethod("void", (name + "Attr").str(),
618                                      (attr.getStorageType() + " attr").str());
619     auto &body = method.body();
620     body << "  this->getOperation()->setAttr(\"" << name << "\", attr);";
621   };
622 
623   for (auto &namedAttr : op.getAttributes()) {
624     const auto &name = namedAttr.name;
625     const auto &attr = namedAttr.attr;
626     if (!attr.isDerivedAttr())
627       emitAttrWithStorageType(name, attr);
628   }
629 }
630 
631 // Generates the code to compute the start and end index of an operand or result
632 // range.
633 template <typename RangeT>
634 static void
635 generateValueRangeStartAndEnd(Class &opClass, StringRef methodName,
636                               int numVariadic, int numNonVariadic,
637                               StringRef rangeSizeCall, bool hasAttrSegmentSize,
638                               StringRef sizeAttrInit, RangeT &&odsValues) {
639   auto &method = opClass.newMethod("std::pair<unsigned, unsigned>", methodName,
640                                    "unsigned index");
641 
642   if (numVariadic == 0) {
643     method.body() << "  return {index, 1};\n";
644   } else if (hasAttrSegmentSize) {
645     method.body() << sizeAttrInit << attrSizedSegmentValueRangeCalcCode;
646   } else {
647     // Because the op can have arbitrarily interleaved variadic and non-variadic
648     // operands, we need to embed a list in the "sink" getter method for
649     // calculation at run-time.
650     llvm::SmallVector<StringRef, 4> isVariadic;
651     isVariadic.reserve(llvm::size(odsValues));
652     for (auto &it : odsValues)
653       isVariadic.push_back(it.isVariableLength() ? "true" : "false");
654     std::string isVariadicList = llvm::join(isVariadic, ", ");
655     method.body() << formatv(sameVariadicSizeValueRangeCalcCode, isVariadicList,
656                              numNonVariadic, numVariadic, rangeSizeCall,
657                              "operand");
658   }
659 }
660 
661 // Generates the named operand getter methods for the given Operator `op` and
662 // puts them in `opClass`.  Uses `rangeType` as the return type of getters that
663 // return a range of operands (individual operands are `Value ` and each
664 // element in the range must also be `Value `); use `rangeBeginCall` to get
665 // an iterator to the beginning of the operand range; use `rangeSizeCall` to
666 // obtain the number of operands. `getOperandCallPattern` contains the code
667 // necessary to obtain a single operand whose position will be substituted
668 // instead of
669 // "{0}" marker in the pattern.  Note that the pattern should work for any kind
670 // of ops, in particular for one-operand ops that may not have the
671 // `getOperand(unsigned)` method.
672 static void generateNamedOperandGetters(const Operator &op, Class &opClass,
673                                         StringRef sizeAttrInit,
674                                         StringRef rangeType,
675                                         StringRef rangeBeginCall,
676                                         StringRef rangeSizeCall,
677                                         StringRef getOperandCallPattern) {
678   const int numOperands = op.getNumOperands();
679   const int numVariadicOperands = op.getNumVariableLengthOperands();
680   const int numNormalOperands = numOperands - numVariadicOperands;
681 
682   const auto *sameVariadicSize =
683       op.getTrait("OpTrait::SameVariadicOperandSize");
684   const auto *attrSizedOperands =
685       op.getTrait("OpTrait::AttrSizedOperandSegments");
686 
687   if (numVariadicOperands > 1 && !sameVariadicSize && !attrSizedOperands) {
688     PrintFatalError(op.getLoc(), "op has multiple variadic operands but no "
689                                  "specification over their sizes");
690   }
691 
692   if (numVariadicOperands < 2 && attrSizedOperands) {
693     PrintFatalError(op.getLoc(), "op must have at least two variadic operands "
694                                  "to use 'AttrSizedOperandSegments' trait");
695   }
696 
697   if (attrSizedOperands && sameVariadicSize) {
698     PrintFatalError(op.getLoc(),
699                     "op cannot have both 'AttrSizedOperandSegments' and "
700                     "'SameVariadicOperandSize' traits");
701   }
702 
703   // First emit a few "sink" getter methods upon which we layer all nicer named
704   // getter methods.
705   generateValueRangeStartAndEnd(opClass, "getODSOperandIndexAndLength",
706                                 numVariadicOperands, numNormalOperands,
707                                 rangeSizeCall, attrSizedOperands, sizeAttrInit,
708                                 const_cast<Operator &>(op).getOperands());
709 
710   auto &m = opClass.newMethod(rangeType, "getODSOperands", "unsigned index");
711   m.body() << formatv(valueRangeReturnCode, rangeBeginCall,
712                       "getODSOperandIndexAndLength(index)");
713 
714   // Then we emit nicer named getter methods by redirecting to the "sink" getter
715   // method.
716   for (int i = 0; i != numOperands; ++i) {
717     const auto &operand = op.getOperand(i);
718     if (operand.name.empty())
719       continue;
720 
721     if (operand.isOptional()) {
722       auto &m = opClass.newMethod("::mlir::Value", operand.name);
723       m.body() << "  auto operands = getODSOperands(" << i << ");\n"
724                << "  return operands.empty() ? Value() : *operands.begin();";
725     } else if (operand.isVariadic()) {
726       auto &m = opClass.newMethod(rangeType, operand.name);
727       m.body() << "  return getODSOperands(" << i << ");";
728     } else {
729       auto &m = opClass.newMethod("::mlir::Value", operand.name);
730       m.body() << "  return *getODSOperands(" << i << ").begin();";
731     }
732   }
733 }
734 
735 void OpEmitter::genNamedOperandGetters() {
736   generateNamedOperandGetters(
737       op, opClass,
738       /*sizeAttrInit=*/
739       formatv(opSegmentSizeAttrInitCode, "operand_segment_sizes").str(),
740       /*rangeType=*/"::mlir::Operation::operand_range",
741       /*rangeBeginCall=*/"getOperation()->operand_begin()",
742       /*rangeSizeCall=*/"getOperation()->getNumOperands()",
743       /*getOperandCallPattern=*/"getOperation()->getOperand({0})");
744 }
745 
746 void OpEmitter::genNamedOperandSetters() {
747   auto *attrSizedOperands = op.getTrait("OpTrait::AttrSizedOperandSegments");
748   for (int i = 0, e = op.getNumOperands(); i != e; ++i) {
749     const auto &operand = op.getOperand(i);
750     if (operand.name.empty())
751       continue;
752     auto &m = opClass.newMethod("::mlir::MutableOperandRange",
753                                 (operand.name + "Mutable").str());
754     auto &body = m.body();
755     body << "  auto range = getODSOperandIndexAndLength(" << i << ");\n"
756          << "  return ::mlir::MutableOperandRange(getOperation(), "
757             "range.first, range.second";
758     if (attrSizedOperands)
759       body << ", ::mlir::MutableOperandRange::OperandSegment(" << i
760            << "u, *getOperation()->getMutableAttrDict().getNamed("
761               "\"operand_segment_sizes\"))";
762     body << ");\n";
763   }
764 }
765 
766 void OpEmitter::genNamedResultGetters() {
767   const int numResults = op.getNumResults();
768   const int numVariadicResults = op.getNumVariableLengthResults();
769   const int numNormalResults = numResults - numVariadicResults;
770 
771   // If we have more than one variadic results, we need more complicated logic
772   // to calculate the value range for each result.
773 
774   const auto *sameVariadicSize = op.getTrait("OpTrait::SameVariadicResultSize");
775   const auto *attrSizedResults =
776       op.getTrait("OpTrait::AttrSizedResultSegments");
777 
778   if (numVariadicResults > 1 && !sameVariadicSize && !attrSizedResults) {
779     PrintFatalError(op.getLoc(), "op has multiple variadic results but no "
780                                  "specification over their sizes");
781   }
782 
783   if (numVariadicResults < 2 && attrSizedResults) {
784     PrintFatalError(op.getLoc(), "op must have at least two variadic results "
785                                  "to use 'AttrSizedResultSegments' trait");
786   }
787 
788   if (attrSizedResults && sameVariadicSize) {
789     PrintFatalError(op.getLoc(),
790                     "op cannot have both 'AttrSizedResultSegments' and "
791                     "'SameVariadicResultSize' traits");
792   }
793 
794   generateValueRangeStartAndEnd(
795       opClass, "getODSResultIndexAndLength", numVariadicResults,
796       numNormalResults, "getOperation()->getNumResults()", attrSizedResults,
797       formatv(opSegmentSizeAttrInitCode, "result_segment_sizes").str(),
798       op.getResults());
799   auto &m = opClass.newMethod("::mlir::Operation::result_range",
800                               "getODSResults", "unsigned index");
801   m.body() << formatv(valueRangeReturnCode, "getOperation()->result_begin()",
802                       "getODSResultIndexAndLength(index)");
803 
804   for (int i = 0; i != numResults; ++i) {
805     const auto &result = op.getResult(i);
806     if (result.name.empty())
807       continue;
808 
809     if (result.isOptional()) {
810       auto &m = opClass.newMethod("::mlir::Value", result.name);
811       m.body()
812           << "  auto results = getODSResults(" << i << ");\n"
813           << "  return results.empty() ? ::mlir::Value() : *results.begin();";
814     } else if (result.isVariadic()) {
815       auto &m =
816           opClass.newMethod("::mlir::Operation::result_range", result.name);
817       m.body() << "  return getODSResults(" << i << ");";
818     } else {
819       auto &m = opClass.newMethod("::mlir::Value", result.name);
820       m.body() << "  return *getODSResults(" << i << ").begin();";
821     }
822   }
823 }
824 
825 void OpEmitter::genNamedRegionGetters() {
826   unsigned numRegions = op.getNumRegions();
827   for (unsigned i = 0; i < numRegions; ++i) {
828     const auto &region = op.getRegion(i);
829     if (region.name.empty())
830       continue;
831 
832     // Generate the accessors for a varidiadic region.
833     if (region.isVariadic()) {
834       auto &m =
835           opClass.newMethod("::mlir::MutableArrayRef<Region>", region.name);
836       m.body() << formatv(
837           "  return this->getOperation()->getRegions().drop_front({0});", i);
838       continue;
839     }
840 
841     auto &m = opClass.newMethod("::mlir::Region &", region.name);
842     m.body() << formatv("  return this->getOperation()->getRegion({0});", i);
843   }
844 }
845 
846 void OpEmitter::genNamedSuccessorGetters() {
847   unsigned numSuccessors = op.getNumSuccessors();
848   for (unsigned i = 0; i < numSuccessors; ++i) {
849     const NamedSuccessor &successor = op.getSuccessor(i);
850     if (successor.name.empty())
851       continue;
852 
853     // Generate the accessors for a variadic successor list.
854     if (successor.isVariadic()) {
855       auto &m = opClass.newMethod("::mlir::SuccessorRange", successor.name);
856       m.body() << formatv(
857           "  return {std::next(this->getOperation()->successor_begin(), {0}), "
858           "this->getOperation()->successor_end()};",
859           i);
860       continue;
861     }
862 
863     auto &m = opClass.newMethod("::mlir::Block *", successor.name);
864     m.body() << formatv("  return this->getOperation()->getSuccessor({0});", i);
865   }
866 }
867 
868 static bool canGenerateUnwrappedBuilder(Operator &op) {
869   // If this op does not have native attributes at all, return directly to avoid
870   // redefining builders.
871   if (op.getNumNativeAttributes() == 0)
872     return false;
873 
874   bool canGenerate = false;
875   // We are generating builders that take raw values for attributes. We need to
876   // make sure the native attributes have a meaningful "unwrapped" value type
877   // different from the wrapped mlir::Attribute type to avoid redefining
878   // builders. This checks for the op has at least one such native attribute.
879   for (int i = 0, e = op.getNumNativeAttributes(); i < e; ++i) {
880     NamedAttribute &namedAttr = op.getAttribute(i);
881     if (canUseUnwrappedRawValue(namedAttr.attr)) {
882       canGenerate = true;
883       break;
884     }
885   }
886   return canGenerate;
887 }
888 
889 static bool canInferType(Operator &op) {
890   return op.getTrait("::mlir::InferTypeOpInterface::Trait") &&
891          op.getNumRegions() == 0;
892 }
893 
894 void OpEmitter::genSeparateArgParamBuilder() {
895   SmallVector<AttrParamKind, 2> attrBuilderType;
896   attrBuilderType.push_back(AttrParamKind::WrappedAttr);
897   if (canGenerateUnwrappedBuilder(op))
898     attrBuilderType.push_back(AttrParamKind::UnwrappedValue);
899 
900   // Emit with separate builders with or without unwrapped attributes and/or
901   // inferring result type.
902   auto emit = [&](AttrParamKind attrType, TypeParamKind paramKind,
903                   bool inferType) {
904     std::string paramList;
905     llvm::SmallVector<std::string, 4> resultNames;
906     buildParamList(paramList, resultNames, paramKind, attrType);
907 
908     auto &m =
909         opClass.newMethod("void", "build", paramList, OpMethod::MP_Static);
910     auto &body = m.body();
911 
912     genCodeForAddingArgAndRegionForBuilder(
913         body, /*isRawValueAttr=*/attrType == AttrParamKind::UnwrappedValue);
914 
915     // Push all result types to the operation state
916 
917     if (inferType) {
918       // Generate builder that infers type too.
919       // TODO: Subsume this with general checking if type can be
920       // inferred automatically.
921       // TODO: Expand to handle regions.
922       body << formatv(R"(
923         ::llvm::SmallVector<::mlir::Type, 2> inferredReturnTypes;
924         if (succeeded({0}::inferReturnTypes(odsBuilder.getContext(),
925                       {1}.location, {1}.operands,
926                       {1}.attributes.getDictionary({1}.getContext()),
927                       /*regions=*/{{}, inferredReturnTypes)))
928           {1}.addTypes(inferredReturnTypes);
929         else
930           ::llvm::report_fatal_error("Failed to infer result type(s).");)",
931                       opClass.getClassName(), builderOpState);
932       return;
933     }
934 
935     switch (paramKind) {
936     case TypeParamKind::None:
937       return;
938     case TypeParamKind::Separate:
939       for (int i = 0, e = op.getNumResults(); i < e; ++i) {
940         if (op.getResult(i).isOptional())
941           body << "  if (" << resultNames[i] << ")\n  ";
942         body << "  " << builderOpState << ".addTypes(" << resultNames[i]
943              << ");\n";
944       }
945       return;
946     case TypeParamKind::Collective:
947       body << "  "
948            << "assert(resultTypes.size() "
949            << (op.getNumVariableLengthResults() == 0 ? "==" : ">=") << " "
950            << (op.getNumResults() - op.getNumVariableLengthResults())
951            << "u && \"mismatched number of results\");\n";
952       body << "  " << builderOpState << ".addTypes(resultTypes);\n";
953       return;
954     }
955     llvm_unreachable("unhandled TypeParamKind");
956   };
957 
958   // A separate arg param builder method will have a signature which is
959   // ambiguous with the collective params build method (generated in
960   // `genCollectiveParamBuilder` function below) if it has a single
961   // `ArrayReg<Type>` parameter for result types and a single `ArrayRef<Value>`
962   // parameter for the operands, no parameters after that, and the collective
963   // params build method has `attributes` as its last parameter (with
964   // a default value). This will happen when all of the following are true:
965   // 1. [`attributes` as last parameter in collective params build method]:
966   //    getNumVariadicRegions must be 0 (otherwise the collective params build
967   //    method ends with a `numRegions` param, and we don't specify default
968   //    value for attributes).
969   // 2. [single `ArrayRef<Value>` parameter for operands, and no parameters
970   //    after that]: numArgs() must be 1 (if not, each arg gets a separate param
971   //    in the build methods generated here) and the single arg must be a
972   //    non-attribute variadic argument.
973   // 3. [single `ArrayReg<Type>` parameter for result types]:
974   //      3a. paramKind should be Collective, or
975   //      3b. paramKind should be Separate and there should be a single variadic
976   //          result
977   //
978   // In that case, skip generating such ambiguous build methods here.
979   bool hasSingleVariadicResult =
980       op.getNumResults() == 1 && op.getResult(0).isVariadic();
981 
982   bool hasSingleVariadicArg =
983       op.getNumArgs() == 1 &&
984       op.getArg(0).is<tblgen::NamedTypeConstraint *>() &&
985       op.getOperand(0).isVariadic();
986   bool hasNoVariadicRegions = op.getNumVariadicRegions() == 0;
987 
988   for (auto attrType : attrBuilderType) {
989     // Case 3b above.
990     if (!(hasNoVariadicRegions && hasSingleVariadicArg &&
991           hasSingleVariadicResult))
992       emit(attrType, TypeParamKind::Separate, /*inferType=*/false);
993     if (canInferType(op))
994       emit(attrType, TypeParamKind::None, /*inferType=*/true);
995     // The separate arg + collective param kind method will be:
996     // (a) Same as the separate arg + separate param kind method if there is
997     //     only one variadic result.
998     // (b) Ambiguous with the collective params method under conditions in (3a)
999     //     above.
1000     // In either case, skip generating such build method.
1001     if (!hasSingleVariadicResult &&
1002         !(hasNoVariadicRegions && hasSingleVariadicArg))
1003       emit(attrType, TypeParamKind::Collective, /*inferType=*/false);
1004   }
1005 }
1006 
1007 void OpEmitter::genUseOperandAsResultTypeCollectiveParamBuilder() {
1008   // If this op has a variadic result, we cannot generate this builder because
1009   // we don't know how many results to create.
1010   if (op.getNumVariableLengthResults() != 0)
1011     return;
1012 
1013   int numResults = op.getNumResults();
1014 
1015   // Signature
1016   std::string params =
1017       std::string("::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &") +
1018       builderOpState +
1019       ", ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> "
1020       "attributes";
1021   if (op.getNumVariadicRegions())
1022     params += ", unsigned numRegions";
1023   auto &m = opClass.newMethod("void", "build", params, OpMethod::MP_Static);
1024   auto &body = m.body();
1025 
1026   // Operands
1027   body << "  " << builderOpState << ".addOperands(operands);\n";
1028 
1029   // Attributes
1030   body << "  " << builderOpState << ".addAttributes(attributes);\n";
1031 
1032   // Create the correct number of regions
1033   if (int numRegions = op.getNumRegions()) {
1034     body << llvm::formatv(
1035         "  for (unsigned i = 0; i != {0}; ++i)\n",
1036         (op.getNumVariadicRegions() ? "numRegions" : Twine(numRegions)));
1037     body << "    (void)" << builderOpState << ".addRegion();\n";
1038   }
1039 
1040   // Result types
1041   SmallVector<std::string, 2> resultTypes(numResults, "operands[0].getType()");
1042   body << "  " << builderOpState << ".addTypes({"
1043        << llvm::join(resultTypes, ", ") << "});\n\n";
1044 }
1045 
1046 void OpEmitter::genInferredTypeCollectiveParamBuilder() {
1047   // TODO: Expand to support regions.
1048   const char *params =
1049       "::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &{0}, "
1050       "::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> "
1051       "attributes";
1052   auto &m =
1053       opClass.newMethod("void", "build", formatv(params, builderOpState).str(),
1054                         OpMethod::MP_Static);
1055   auto &body = m.body();
1056 
1057   int numResults = op.getNumResults();
1058   int numVariadicResults = op.getNumVariableLengthResults();
1059   int numNonVariadicResults = numResults - numVariadicResults;
1060 
1061   int numOperands = op.getNumOperands();
1062   int numVariadicOperands = op.getNumVariableLengthOperands();
1063   int numNonVariadicOperands = numOperands - numVariadicOperands;
1064 
1065   // Operands
1066   if (numVariadicOperands == 0 || numNonVariadicOperands != 0)
1067     body << "  assert(operands.size()"
1068          << (numVariadicOperands != 0 ? " >= " : " == ")
1069          << numNonVariadicOperands
1070          << "u && \"mismatched number of parameters\");\n";
1071   body << "  " << builderOpState << ".addOperands(operands);\n";
1072   body << "  " << builderOpState << ".addAttributes(attributes);\n";
1073 
1074   // Create the correct number of regions
1075   if (int numRegions = op.getNumRegions()) {
1076     body << llvm::formatv(
1077         "  for (unsigned i = 0; i != {0}; ++i)\n",
1078         (op.getNumVariadicRegions() ? "numRegions" : Twine(numRegions)));
1079     body << "    (void)" << builderOpState << ".addRegion();\n";
1080   }
1081 
1082   // Result types
1083   body << formatv(R"(
1084     ::mlir::SmallVector<::mlir::Type, 2> inferredReturnTypes;
1085     if (succeeded({0}::inferReturnTypes(odsBuilder.getContext(),
1086                   {1}.location, operands,
1087                   {1}.attributes.getDictionary({1}.getContext()),
1088                   /*regions=*/{{}, inferredReturnTypes))) {{)",
1089                   opClass.getClassName(), builderOpState);
1090   if (numVariadicResults == 0 || numNonVariadicResults != 0)
1091     body << "  assert(inferredReturnTypes.size()"
1092          << (numVariadicResults != 0 ? " >= " : " == ") << numNonVariadicResults
1093          << "u && \"mismatched number of return types\");\n";
1094   body << "      " << builderOpState << ".addTypes(inferredReturnTypes);";
1095 
1096   body << formatv(R"(
1097     } else
1098       ::llvm::report_fatal_error("Failed to infer result type(s).");)",
1099                   opClass.getClassName(), builderOpState);
1100 }
1101 
1102 void OpEmitter::genUseOperandAsResultTypeSeparateParamBuilder() {
1103   std::string paramList;
1104   llvm::SmallVector<std::string, 4> resultNames;
1105   buildParamList(paramList, resultNames, TypeParamKind::None);
1106 
1107   auto &m = opClass.newMethod("void", "build", paramList, OpMethod::MP_Static);
1108   genCodeForAddingArgAndRegionForBuilder(m.body());
1109 
1110   auto numResults = op.getNumResults();
1111   if (numResults == 0)
1112     return;
1113 
1114   // Push all result types to the operation state
1115   const char *index = op.getOperand(0).isVariadic() ? ".front()" : "";
1116   std::string resultType =
1117       formatv("{0}{1}.getType()", getArgumentName(op, 0), index).str();
1118   m.body() << "  " << builderOpState << ".addTypes({" << resultType;
1119   for (int i = 1; i != numResults; ++i)
1120     m.body() << ", " << resultType;
1121   m.body() << "});\n\n";
1122 }
1123 
1124 void OpEmitter::genUseAttrAsResultTypeBuilder() {
1125   std::string params =
1126       std::string("::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &") +
1127       builderOpState +
1128       ", ::mlir::ValueRange operands, ::llvm::ArrayRef<::mlir::NamedAttribute> "
1129       "attributes";
1130   auto &m = opClass.newMethod("void", "build", params, OpMethod::MP_Static);
1131   auto &body = m.body();
1132 
1133   // Push all result types to the operation state
1134   std::string resultType;
1135   const auto &namedAttr = op.getAttribute(0);
1136 
1137   body << "  for (auto attr : attributes) {\n";
1138   body << "    if (attr.first != \"" << namedAttr.name << "\") continue;\n";
1139   if (namedAttr.attr.isTypeAttr()) {
1140     resultType = "attr.second.cast<::mlir::TypeAttr>().getValue()";
1141   } else {
1142     resultType = "attr.second.getType()";
1143   }
1144 
1145   // Operands
1146   body << "  " << builderOpState << ".addOperands(operands);\n";
1147 
1148   // Attributes
1149   body << "  " << builderOpState << ".addAttributes(attributes);\n";
1150 
1151   // Result types
1152   SmallVector<std::string, 2> resultTypes(op.getNumResults(), resultType);
1153   body << "    " << builderOpState << ".addTypes({"
1154        << llvm::join(resultTypes, ", ") << "});\n";
1155   body << "  }\n";
1156 }
1157 
1158 void OpEmitter::genBuilder() {
1159   // Handle custom builders if provided.
1160   // TODO: Create wrapper class for OpBuilder to hide the native
1161   // TableGen API calls here.
1162   {
1163     auto *listInit = dyn_cast_or_null<ListInit>(def.getValueInit("builders"));
1164     if (listInit) {
1165       for (Init *init : listInit->getValues()) {
1166         Record *builderDef = cast<DefInit>(init)->getDef();
1167         StringRef params = builderDef->getValueAsString("params");
1168         StringRef body = builderDef->getValueAsString("body");
1169         bool hasBody = !body.empty();
1170 
1171         auto &method =
1172             opClass.newMethod("void", "build", params, OpMethod::MP_Static,
1173                               /*declOnly=*/!hasBody);
1174         if (hasBody)
1175           method.body() << body;
1176       }
1177     }
1178     if (op.skipDefaultBuilders()) {
1179       if (!listInit || listInit->empty())
1180         PrintFatalError(
1181             op.getLoc(),
1182             "default builders are skipped and no custom builders provided");
1183       return;
1184     }
1185   }
1186 
1187   // Generate default builders that requires all result type, operands, and
1188   // attributes as parameters.
1189 
1190   // We generate three classes of builders here:
1191   // 1. one having a stand-alone parameter for each operand / attribute, and
1192   genSeparateArgParamBuilder();
1193   // 2. one having an aggregated parameter for all result types / operands /
1194   //    attributes, and
1195   genCollectiveParamBuilder();
1196   // 3. one having a stand-alone parameter for each operand and attribute,
1197   //    use the first operand or attribute's type as all result types
1198   //    to facilitate different call patterns.
1199   if (op.getNumVariableLengthResults() == 0) {
1200     if (op.getTrait("OpTrait::SameOperandsAndResultType")) {
1201       genUseOperandAsResultTypeSeparateParamBuilder();
1202       genUseOperandAsResultTypeCollectiveParamBuilder();
1203     }
1204     if (op.getTrait("OpTrait::FirstAttrDerivedResultType"))
1205       genUseAttrAsResultTypeBuilder();
1206   }
1207 }
1208 
1209 void OpEmitter::genCollectiveParamBuilder() {
1210   int numResults = op.getNumResults();
1211   int numVariadicResults = op.getNumVariableLengthResults();
1212   int numNonVariadicResults = numResults - numVariadicResults;
1213 
1214   int numOperands = op.getNumOperands();
1215   int numVariadicOperands = op.getNumVariableLengthOperands();
1216   int numNonVariadicOperands = numOperands - numVariadicOperands;
1217   // Signature
1218   std::string params =
1219       std::string("::mlir::OpBuilder &, ::mlir::OperationState &") +
1220       builderOpState +
1221       ", ::llvm::ArrayRef<::mlir::Type> resultTypes, ::mlir::ValueRange "
1222       "operands, "
1223       "::llvm::ArrayRef<::mlir::NamedAttribute> attributes";
1224   if (op.getNumVariadicRegions()) {
1225     params += ", unsigned numRegions";
1226   } else {
1227     // Provide default value for `attributes` since its the last parameter
1228     params += " = {}";
1229   }
1230   auto &m = opClass.newMethod("void", "build", params, OpMethod::MP_Static);
1231   auto &body = m.body();
1232 
1233   // Operands
1234   if (numVariadicOperands == 0 || numNonVariadicOperands != 0)
1235     body << "  assert(operands.size()"
1236          << (numVariadicOperands != 0 ? " >= " : " == ")
1237          << numNonVariadicOperands
1238          << "u && \"mismatched number of parameters\");\n";
1239   body << "  " << builderOpState << ".addOperands(operands);\n";
1240 
1241   // Attributes
1242   body << "  " << builderOpState << ".addAttributes(attributes);\n";
1243 
1244   // Create the correct number of regions
1245   if (int numRegions = op.getNumRegions()) {
1246     body << llvm::formatv(
1247         "  for (unsigned i = 0; i != {0}; ++i)\n",
1248         (op.getNumVariadicRegions() ? "numRegions" : Twine(numRegions)));
1249     body << "    (void)" << builderOpState << ".addRegion();\n";
1250   }
1251 
1252   // Result types
1253   if (numVariadicResults == 0 || numNonVariadicResults != 0)
1254     body << "  assert(resultTypes.size()"
1255          << (numVariadicResults != 0 ? " >= " : " == ") << numNonVariadicResults
1256          << "u && \"mismatched number of return types\");\n";
1257   body << "  " << builderOpState << ".addTypes(resultTypes);\n";
1258 
1259   // Generate builder that infers type too.
1260   // TODO: Expand to handle regions and successors.
1261   if (canInferType(op) && op.getNumSuccessors() == 0)
1262     genInferredTypeCollectiveParamBuilder();
1263 }
1264 
1265 void OpEmitter::buildParamList(std::string &paramList,
1266                                SmallVectorImpl<std::string> &resultTypeNames,
1267                                TypeParamKind typeParamKind,
1268                                AttrParamKind attrParamKind) {
1269   resultTypeNames.clear();
1270   auto numResults = op.getNumResults();
1271   resultTypeNames.reserve(numResults);
1272 
1273   paramList = "::mlir::OpBuilder &odsBuilder, ::mlir::OperationState &";
1274   paramList.append(builderOpState);
1275 
1276   switch (typeParamKind) {
1277   case TypeParamKind::None:
1278     break;
1279   case TypeParamKind::Separate: {
1280     // Add parameters for all return types
1281     for (int i = 0; i < numResults; ++i) {
1282       const auto &result = op.getResult(i);
1283       std::string resultName = std::string(result.name);
1284       if (resultName.empty())
1285         resultName = std::string(formatv("resultType{0}", i));
1286 
1287       if (result.isOptional())
1288         paramList.append(", /*optional*/::mlir::Type ");
1289       else if (result.isVariadic())
1290         paramList.append(", ::llvm::ArrayRef<::mlir::Type> ");
1291       else
1292         paramList.append(", ::mlir::Type ");
1293       paramList.append(resultName);
1294 
1295       resultTypeNames.emplace_back(std::move(resultName));
1296     }
1297   } break;
1298   case TypeParamKind::Collective: {
1299     paramList.append(", ::llvm::ArrayRef<::mlir::Type> resultTypes");
1300     resultTypeNames.push_back("resultTypes");
1301   } break;
1302   }
1303 
1304   // Add parameters for all arguments (operands and attributes).
1305 
1306   int numOperands = 0;
1307   int numAttrs = 0;
1308 
1309   int defaultValuedAttrStartIndex = op.getNumArgs();
1310   if (attrParamKind == AttrParamKind::UnwrappedValue) {
1311     // Calculate the start index from which we can attach default values in the
1312     // builder declaration.
1313     for (int i = op.getNumArgs() - 1; i >= 0; --i) {
1314       auto *namedAttr = op.getArg(i).dyn_cast<tblgen::NamedAttribute *>();
1315       if (!namedAttr || !namedAttr->attr.hasDefaultValue())
1316         break;
1317 
1318       if (!canUseUnwrappedRawValue(namedAttr->attr))
1319         break;
1320 
1321       // Creating an APInt requires us to provide bitwidth, value, and
1322       // signedness, which is complicated compared to others. Similarly
1323       // for APFloat.
1324       // TODO: Adjust the 'returnType' field of such attributes
1325       // to support them.
1326       StringRef retType = namedAttr->attr.getReturnType();
1327       if (retType == "::llvm::APInt" || retType == "::llvm::APFloat")
1328         break;
1329 
1330       defaultValuedAttrStartIndex = i;
1331     }
1332   }
1333 
1334   for (int i = 0, e = op.getNumArgs(); i < e; ++i) {
1335     auto argument = op.getArg(i);
1336     if (argument.is<tblgen::NamedTypeConstraint *>()) {
1337       const auto &operand = op.getOperand(numOperands);
1338       if (operand.isOptional())
1339         paramList.append(", /*optional*/::mlir::Value ");
1340       else if (operand.isVariadic())
1341         paramList.append(", ::mlir::ValueRange ");
1342       else
1343         paramList.append(", ::mlir::Value ");
1344       paramList.append(getArgumentName(op, numOperands));
1345       ++numOperands;
1346     } else {
1347       const auto &namedAttr = op.getAttribute(numAttrs);
1348       const auto &attr = namedAttr.attr;
1349       paramList.append(", ");
1350 
1351       if (attr.isOptional())
1352         paramList.append("/*optional*/");
1353 
1354       switch (attrParamKind) {
1355       case AttrParamKind::WrappedAttr:
1356         paramList.append(std::string(attr.getStorageType()));
1357         break;
1358       case AttrParamKind::UnwrappedValue:
1359         if (canUseUnwrappedRawValue(attr)) {
1360           paramList.append(std::string(attr.getReturnType()));
1361         } else {
1362           paramList.append(std::string(attr.getStorageType()));
1363         }
1364         break;
1365       }
1366       paramList.append(" ");
1367       paramList.append(std::string(namedAttr.name));
1368 
1369       // Attach default value if requested and possible.
1370       if (attrParamKind == AttrParamKind::UnwrappedValue &&
1371           i >= defaultValuedAttrStartIndex) {
1372         bool isString = attr.getReturnType() == "::llvm::StringRef";
1373         paramList.append(" = ");
1374         if (isString)
1375           paramList.append("\"");
1376         paramList.append(std::string(attr.getDefaultValue()));
1377         if (isString)
1378           paramList.append("\"");
1379       }
1380       ++numAttrs;
1381     }
1382   }
1383 
1384   /// Insert parameters for each successor.
1385   for (const NamedSuccessor &succ : op.getSuccessors()) {
1386     paramList += (succ.isVariadic() ? ", ::llvm::ArrayRef<::mlir::Block *> "
1387                                     : ", ::mlir::Block *");
1388     paramList += succ.name;
1389   }
1390 
1391   /// Insert parameters for variadic regions.
1392   for (const NamedRegion &region : op.getRegions()) {
1393     if (region.isVariadic())
1394       paramList += llvm::formatv(", unsigned {0}Count", region.name).str();
1395   }
1396 }
1397 
1398 void OpEmitter::genCodeForAddingArgAndRegionForBuilder(OpMethodBody &body,
1399                                                        bool isRawValueAttr) {
1400   // Push all operands to the result.
1401   for (int i = 0, e = op.getNumOperands(); i < e; ++i) {
1402     std::string argName = getArgumentName(op, i);
1403     if (op.getOperand(i).isOptional())
1404       body << "  if (" << argName << ")\n  ";
1405     body << "  " << builderOpState << ".addOperands(" << argName << ");\n";
1406   }
1407 
1408   // If the operation has the operand segment size attribute, add it here.
1409   if (op.getTrait("OpTrait::AttrSizedOperandSegments")) {
1410     body << "  " << builderOpState
1411          << ".addAttribute(\"operand_segment_sizes\", "
1412             "odsBuilder.getI32VectorAttr({";
1413     interleaveComma(llvm::seq<int>(0, op.getNumOperands()), body, [&](int i) {
1414       if (op.getOperand(i).isOptional())
1415         body << "(" << getArgumentName(op, i) << " ? 1 : 0)";
1416       else if (op.getOperand(i).isVariadic())
1417         body << "static_cast<int32_t>(" << getArgumentName(op, i) << ".size())";
1418       else
1419         body << "1";
1420     });
1421     body << "}));\n";
1422   }
1423 
1424   // Push all attributes to the result.
1425   for (const auto &namedAttr : op.getAttributes()) {
1426     auto &attr = namedAttr.attr;
1427     if (!attr.isDerivedAttr()) {
1428       bool emitNotNullCheck = attr.isOptional();
1429       if (emitNotNullCheck) {
1430         body << formatv("  if ({0}) ", namedAttr.name) << "{\n";
1431       }
1432       if (isRawValueAttr && canUseUnwrappedRawValue(attr)) {
1433         // If this is a raw value, then we need to wrap it in an Attribute
1434         // instance.
1435         FmtContext fctx;
1436         fctx.withBuilder("odsBuilder");
1437 
1438         std::string builderTemplate =
1439             std::string(attr.getConstBuilderTemplate());
1440 
1441         // For StringAttr, its constant builder call will wrap the input in
1442         // quotes, which is correct for normal string literals, but incorrect
1443         // here given we use function arguments. So we need to strip the
1444         // wrapping quotes.
1445         if (StringRef(builderTemplate).contains("\"$0\""))
1446           builderTemplate = replaceAllSubstrs(builderTemplate, "\"$0\"", "$0");
1447 
1448         std::string value =
1449             std::string(tgfmt(builderTemplate, &fctx, namedAttr.name));
1450         body << formatv("  {0}.addAttribute(\"{1}\", {2});\n", builderOpState,
1451                         namedAttr.name, value);
1452       } else {
1453         body << formatv("  {0}.addAttribute(\"{1}\", {1});\n", builderOpState,
1454                         namedAttr.name);
1455       }
1456       if (emitNotNullCheck) {
1457         body << "  }\n";
1458       }
1459     }
1460   }
1461 
1462   // Create the correct number of regions.
1463   for (const NamedRegion &region : op.getRegions()) {
1464     if (region.isVariadic())
1465       body << formatv("  for (unsigned i = 0; i < {0}Count; ++i)\n  ",
1466                       region.name);
1467 
1468     body << "  (void)" << builderOpState << ".addRegion();\n";
1469   }
1470 
1471   // Push all successors to the result.
1472   for (const NamedSuccessor &namedSuccessor : op.getSuccessors()) {
1473     body << formatv("  {0}.addSuccessors({1});\n", builderOpState,
1474                     namedSuccessor.name);
1475   }
1476 }
1477 
1478 void OpEmitter::genCanonicalizerDecls() {
1479   if (!def.getValueAsBit("hasCanonicalizer"))
1480     return;
1481 
1482   const char *const params =
1483       "::mlir::OwningRewritePatternList &results, ::mlir::MLIRContext *context";
1484   opClass.newMethod("void", "getCanonicalizationPatterns", params,
1485                     OpMethod::MP_Static, /*declOnly=*/true);
1486 }
1487 
1488 void OpEmitter::genFolderDecls() {
1489   bool hasSingleResult =
1490       op.getNumResults() == 1 && op.getNumVariableLengthResults() == 0;
1491 
1492   if (def.getValueAsBit("hasFolder")) {
1493     if (hasSingleResult) {
1494       const char *const params = "::llvm::ArrayRef<::mlir::Attribute> operands";
1495       opClass.newMethod("::mlir::OpFoldResult", "fold", params,
1496                         OpMethod::MP_None,
1497                         /*declOnly=*/true);
1498     } else {
1499       const char *const params =
1500           "::llvm::ArrayRef<::mlir::Attribute> operands, "
1501           "::llvm::SmallVectorImpl<::mlir::OpFoldResult> &results";
1502       opClass.newMethod("::mlir::LogicalResult", "fold", params,
1503                         OpMethod::MP_None,
1504                         /*declOnly=*/true);
1505     }
1506   }
1507 }
1508 
1509 void OpEmitter::genOpInterfaceMethod(const tblgen::InterfaceOpTrait *opTrait) {
1510   auto interface = opTrait->getOpInterface();
1511 
1512   // Get the set of methods that should always be declared.
1513   auto alwaysDeclaredMethodsVec = opTrait->getAlwaysDeclaredMethods();
1514   llvm::StringSet<> alwaysDeclaredMethods;
1515   alwaysDeclaredMethods.insert(alwaysDeclaredMethodsVec.begin(),
1516                                alwaysDeclaredMethodsVec.end());
1517 
1518   for (const InterfaceMethod &method : interface.getMethods()) {
1519     // Don't declare if the method has a body.
1520     if (method.getBody())
1521       continue;
1522     // Don't declare if the method has a default implementation and the op
1523     // didn't request that it always be declared.
1524     if (method.getDefaultImplementation() &&
1525         !alwaysDeclaredMethods.count(method.getName()))
1526       continue;
1527 
1528     std::string args;
1529     llvm::raw_string_ostream os(args);
1530     interleaveComma(method.getArguments(), os,
1531                     [&](const InterfaceMethod::Argument &arg) {
1532                       os << arg.type << " " << arg.name;
1533                     });
1534     opClass.newMethod(method.getReturnType(), method.getName(), os.str(),
1535                       method.isStatic() ? OpMethod::MP_Static
1536                                         : OpMethod::MP_None,
1537                       /*declOnly=*/true);
1538   }
1539 }
1540 
1541 void OpEmitter::genOpInterfaceMethods() {
1542   for (const auto &trait : op.getTraits()) {
1543     if (const auto *opTrait = dyn_cast<tblgen::InterfaceOpTrait>(&trait))
1544       if (opTrait->shouldDeclareMethods())
1545         genOpInterfaceMethod(opTrait);
1546   }
1547 }
1548 
1549 void OpEmitter::genSideEffectInterfaceMethods() {
1550   enum EffectKind { Operand, Result, Static };
1551   struct EffectLocation {
1552     /// The effect applied.
1553     SideEffect effect;
1554 
1555     /// The index if the kind is either operand or result.
1556     unsigned index : 30;
1557 
1558     /// The kind of the location.
1559     unsigned kind : 2;
1560   };
1561 
1562   StringMap<SmallVector<EffectLocation, 1>> interfaceEffects;
1563   auto resolveDecorators = [&](Operator::var_decorator_range decorators,
1564                                unsigned index, unsigned kind) {
1565     for (auto decorator : decorators)
1566       if (SideEffect *effect = dyn_cast<SideEffect>(&decorator)) {
1567         opClass.addTrait(effect->getInterfaceTrait());
1568         interfaceEffects[effect->getBaseEffectName()].push_back(
1569             EffectLocation{*effect, index, kind});
1570       }
1571   };
1572 
1573   // Collect effects that were specified via:
1574   /// Traits.
1575   for (const auto &trait : op.getTraits()) {
1576     const auto *opTrait = dyn_cast<tblgen::SideEffectTrait>(&trait);
1577     if (!opTrait)
1578       continue;
1579     auto &effects = interfaceEffects[opTrait->getBaseEffectName()];
1580     for (auto decorator : opTrait->getEffects())
1581       effects.push_back(EffectLocation{cast<SideEffect>(decorator),
1582                                        /*index=*/0, EffectKind::Static});
1583   }
1584   /// Operands.
1585   for (unsigned i = 0, operandIt = 0, e = op.getNumArgs(); i != e; ++i) {
1586     if (op.getArg(i).is<NamedTypeConstraint *>()) {
1587       resolveDecorators(op.getArgDecorators(i), operandIt, EffectKind::Operand);
1588       ++operandIt;
1589     }
1590   }
1591   /// Results.
1592   for (unsigned i = 0, e = op.getNumResults(); i != e; ++i)
1593     resolveDecorators(op.getResultDecorators(i), i, EffectKind::Result);
1594 
1595   for (auto &it : interfaceEffects) {
1596     auto effectsParam =
1597         llvm::formatv("::mlir::SmallVectorImpl<::mlir::SideEffects::"
1598                       "EffectInstance<{0}>> &effects",
1599                       it.first())
1600             .str();
1601 
1602     // Generate the 'getEffects' method.
1603     auto &getEffects = opClass.newMethod("void", "getEffects", effectsParam);
1604     auto &body = getEffects.body();
1605 
1606     // Add effect instances for each of the locations marked on the operation.
1607     for (auto &location : it.second) {
1608       if (location.kind != EffectKind::Static) {
1609         body << "  for (::mlir::Value value : getODS"
1610              << (location.kind == EffectKind::Operand ? "Operands" : "Results")
1611              << "(" << location.index << "))\n  ";
1612       }
1613 
1614       body << "  effects.emplace_back(" << location.effect.getName()
1615            << "::get()";
1616 
1617       // If the effect isn't static, it has a specific value attached to it.
1618       if (location.kind != EffectKind::Static)
1619         body << ", value";
1620       body << ", " << location.effect.getResource() << "::get());\n";
1621     }
1622   }
1623 }
1624 
1625 void OpEmitter::genTypeInterfaceMethods() {
1626   if (!op.allResultTypesKnown())
1627     return;
1628 
1629   auto &method = opClass.newMethod(
1630       "::mlir::LogicalResult", "inferReturnTypes",
1631       "::mlir::MLIRContext* context, "
1632       "::llvm::Optional<::mlir::Location> location, "
1633       "::mlir::ValueRange operands, ::mlir::DictionaryAttr attributes, "
1634       "::mlir::RegionRange regions, "
1635       "::llvm::SmallVectorImpl<::mlir::Type>& inferredReturnTypes",
1636       OpMethod::MP_Static,
1637       /*declOnly=*/false);
1638   auto &os = method.body();
1639   os << "  inferredReturnTypes.resize(" << op.getNumResults() << ");\n";
1640 
1641   FmtContext fctx;
1642   fctx.withBuilder("odsBuilder");
1643   os << "  ::mlir::Builder odsBuilder(context);\n";
1644 
1645   auto emitType =
1646       [&](const tblgen::Operator::ArgOrType &type) -> OpMethodBody & {
1647     if (type.isArg()) {
1648       auto argIndex = type.getArg();
1649       assert(!op.getArg(argIndex).is<NamedAttribute *>());
1650       auto arg = op.getArgToOperandOrAttribute(argIndex);
1651       if (arg.kind() == Operator::OperandOrAttribute::Kind::Operand)
1652         return os << "operands[" << arg.operandOrAttributeIndex()
1653                   << "].getType()";
1654       return os << "attributes[" << arg.operandOrAttributeIndex()
1655                 << "].getType()";
1656     } else {
1657       return os << tgfmt(*type.getType().getBuilderCall(), &fctx);
1658     }
1659   };
1660 
1661   for (int i = 0, e = op.getNumResults(); i != e; ++i) {
1662     os << "  inferredReturnTypes[" << i << "] = ";
1663     auto types = op.getSameTypeAsResult(i);
1664     emitType(types[0]) << ";\n";
1665     if (types.size() == 1)
1666       continue;
1667     // TODO: We could verify equality here, but skipping that for verification.
1668   }
1669   os << "  return success();";
1670 }
1671 
1672 void OpEmitter::genParser() {
1673   if (!hasStringAttribute(def, "parser") ||
1674       hasStringAttribute(def, "assemblyFormat"))
1675     return;
1676 
1677   auto &method = opClass.newMethod(
1678       "::mlir::ParseResult", "parse",
1679       "::mlir::OpAsmParser &parser, ::mlir::OperationState &result",
1680       OpMethod::MP_Static);
1681   FmtContext fctx;
1682   fctx.addSubst("cppClass", opClass.getClassName());
1683   auto parser = def.getValueAsString("parser").ltrim().rtrim(" \t\v\f\r");
1684   method.body() << "  " << tgfmt(parser, &fctx);
1685 }
1686 
1687 void OpEmitter::genPrinter() {
1688   if (hasStringAttribute(def, "assemblyFormat"))
1689     return;
1690 
1691   auto valueInit = def.getValueInit("printer");
1692   CodeInit *codeInit = dyn_cast<CodeInit>(valueInit);
1693   if (!codeInit)
1694     return;
1695 
1696   auto &method = opClass.newMethod("void", "print", "::mlir::OpAsmPrinter &p");
1697   FmtContext fctx;
1698   fctx.addSubst("cppClass", opClass.getClassName());
1699   auto printer = codeInit->getValue().ltrim().rtrim(" \t\v\f\r");
1700   method.body() << "  " << tgfmt(printer, &fctx);
1701 }
1702 
1703 void OpEmitter::genVerifier() {
1704   auto &method =
1705       opClass.newMethod("::mlir::LogicalResult", "verify", /*params=*/"");
1706   auto &body = method.body();
1707   body << "  if (failed(" << op.getAdaptorName()
1708        << "(*this).verify(this->getLoc()))) "
1709        << "return failure();\n";
1710 
1711   auto *valueInit = def.getValueInit("verifier");
1712   CodeInit *codeInit = dyn_cast<CodeInit>(valueInit);
1713   bool hasCustomVerify = codeInit && !codeInit->getValue().empty();
1714   populateSubstitutions(op, "this->getAttr", "this->getODSOperands",
1715                         "this->getODSResults", verifyCtx);
1716 
1717   genAttributeVerifier(op, "this->getAttr", "emitOpError(",
1718                        /*emitVerificationRequiringOp=*/true, verifyCtx, body);
1719   genOperandResultVerifier(body, op.getOperands(), "operand");
1720   genOperandResultVerifier(body, op.getResults(), "result");
1721 
1722   for (auto &trait : op.getTraits()) {
1723     if (auto *t = dyn_cast<tblgen::PredOpTrait>(&trait)) {
1724       body << tgfmt("  if (!($0))\n    "
1725                     "return emitOpError(\"failed to verify that $1\");\n",
1726                     &verifyCtx, tgfmt(t->getPredTemplate(), &verifyCtx),
1727                     t->getDescription());
1728     }
1729   }
1730 
1731   genRegionVerifier(body);
1732   genSuccessorVerifier(body);
1733 
1734   if (hasCustomVerify) {
1735     FmtContext fctx;
1736     fctx.addSubst("cppClass", opClass.getClassName());
1737     auto printer = codeInit->getValue().ltrim().rtrim(" \t\v\f\r");
1738     body << "  " << tgfmt(printer, &fctx);
1739   } else {
1740     body << "  return ::mlir::success();\n";
1741   }
1742 }
1743 
1744 void OpEmitter::genOperandResultVerifier(OpMethodBody &body,
1745                                          Operator::value_range values,
1746                                          StringRef valueKind) {
1747   FmtContext fctx;
1748 
1749   body << "  {\n";
1750   body << "    unsigned index = 0; (void)index;\n";
1751 
1752   for (auto staticValue : llvm::enumerate(values)) {
1753     bool hasPredicate = staticValue.value().hasPredicate();
1754     bool isOptional = staticValue.value().isOptional();
1755     if (!hasPredicate && !isOptional)
1756       continue;
1757     body << formatv("    auto valueGroup{2} = getODS{0}{1}s({2});\n",
1758                     // Capitalize the first letter to match the function name
1759                     valueKind.substr(0, 1).upper(), valueKind.substr(1),
1760                     staticValue.index());
1761 
1762     // If the constraint is optional check that the value group has at most 1
1763     // value.
1764     if (isOptional) {
1765       body << formatv("    if (valueGroup{0}.size() > 1)\n"
1766                       "      return emitOpError(\"{1} group starting at #\") "
1767                       "<< index << \" requires 0 or 1 element, but found \" << "
1768                       "valueGroup{0}.size();\n",
1769                       staticValue.index(), valueKind);
1770     }
1771 
1772     // Otherwise, if there is no predicate there is nothing left to do.
1773     if (!hasPredicate)
1774       continue;
1775 
1776     // Emit a loop to check all the dynamic values in the pack.
1777     body << "    for (::mlir::Value v : valueGroup" << staticValue.index()
1778          << ") {\n";
1779 
1780     auto constraint = staticValue.value().constraint;
1781     body << "      (void)v;\n"
1782          << "      if (!("
1783          << tgfmt(constraint.getConditionTemplate(),
1784                   &fctx.withSelf("v.getType()"))
1785          << ")) {\n"
1786          << formatv("        return emitOpError(\"{0} #\") << index "
1787                     "<< \" must be {1}, but got \" << v.getType();\n",
1788                     valueKind, constraint.getDescription())
1789          << "      }\n" // if
1790          << "      ++index;\n"
1791          << "    }\n"; // for
1792   }
1793 
1794   body << "  }\n";
1795 }
1796 
1797 void OpEmitter::genRegionVerifier(OpMethodBody &body) {
1798   // If we have no regions, there is nothing more to do.
1799   unsigned numRegions = op.getNumRegions();
1800   if (numRegions == 0)
1801     return;
1802 
1803   body << "{\n";
1804   body << "    unsigned index = 0; (void)index;\n";
1805 
1806   for (unsigned i = 0; i < numRegions; ++i) {
1807     const auto &region = op.getRegion(i);
1808     if (region.constraint.getPredicate().isNull())
1809       continue;
1810 
1811     body << "    for (::mlir::Region &region : ";
1812     body << formatv(region.isVariadic()
1813                         ? "{0}()"
1814                         : "::mlir::MutableArrayRef<::mlir::Region>(this->"
1815                           "getOperation()->getRegion({1}))",
1816                     region.name, i);
1817     body << ") {\n";
1818     auto constraint = tgfmt(region.constraint.getConditionTemplate(),
1819                             &verifyCtx.withSelf("region"))
1820                           .str();
1821 
1822     body << formatv("      (void)region;\n"
1823                     "      if (!({0})) {\n        "
1824                     "return emitOpError(\"region #\") << index << \" {1}"
1825                     "failed to "
1826                     "verify constraint: {2}\";\n      }\n",
1827                     constraint,
1828                     region.name.empty() ? "" : "('" + region.name + "') ",
1829                     region.constraint.getDescription())
1830          << "      ++index;\n"
1831          << "    }\n";
1832   }
1833   body << "  }\n";
1834 }
1835 
1836 void OpEmitter::genSuccessorVerifier(OpMethodBody &body) {
1837   // If we have no successors, there is nothing more to do.
1838   unsigned numSuccessors = op.getNumSuccessors();
1839   if (numSuccessors == 0)
1840     return;
1841 
1842   body << "{\n";
1843   body << "    unsigned index = 0; (void)index;\n";
1844 
1845   for (unsigned i = 0; i < numSuccessors; ++i) {
1846     const auto &successor = op.getSuccessor(i);
1847     if (successor.constraint.getPredicate().isNull())
1848       continue;
1849 
1850     body << "    for (::mlir::Block *successor : ";
1851     body << formatv(successor.isVariadic()
1852                         ? "{0}()"
1853                         : "::llvm::ArrayRef<::mlir::Block *>({0}())",
1854                     successor.name);
1855     body << ") {\n";
1856     auto constraint = tgfmt(successor.constraint.getConditionTemplate(),
1857                             &verifyCtx.withSelf("successor"))
1858                           .str();
1859 
1860     body << formatv("      (void)successor;\n"
1861                     "      if (!({0})) {\n        "
1862                     "return emitOpError(\"successor #\") << index << \"('{1}') "
1863                     "failed to "
1864                     "verify constraint: {2}\";\n      }\n",
1865                     constraint, successor.name,
1866                     successor.constraint.getDescription())
1867          << "      ++index;\n"
1868          << "    }\n";
1869   }
1870   body << "  }\n";
1871 }
1872 
1873 /// Add a size count trait to the given operation class.
1874 static void addSizeCountTrait(OpClass &opClass, StringRef traitKind,
1875                               int numTotal, int numVariadic) {
1876   if (numVariadic != 0) {
1877     if (numTotal == numVariadic)
1878       opClass.addTrait("OpTrait::Variadic" + traitKind + "s");
1879     else
1880       opClass.addTrait("OpTrait::AtLeastN" + traitKind + "s<" +
1881                        Twine(numTotal - numVariadic) + ">::Impl");
1882     return;
1883   }
1884   switch (numTotal) {
1885   case 0:
1886     opClass.addTrait("OpTrait::Zero" + traitKind);
1887     break;
1888   case 1:
1889     opClass.addTrait("OpTrait::One" + traitKind);
1890     break;
1891   default:
1892     opClass.addTrait("OpTrait::N" + traitKind + "s<" + Twine(numTotal) +
1893                      ">::Impl");
1894     break;
1895   }
1896 }
1897 
1898 void OpEmitter::genTraits() {
1899   // Add region size trait.
1900   unsigned numRegions = op.getNumRegions();
1901   unsigned numVariadicRegions = op.getNumVariadicRegions();
1902   addSizeCountTrait(opClass, "Region", numRegions, numVariadicRegions);
1903 
1904   // Add result size trait.
1905   int numResults = op.getNumResults();
1906   int numVariadicResults = op.getNumVariableLengthResults();
1907   addSizeCountTrait(opClass, "Result", numResults, numVariadicResults);
1908 
1909   // Add successor size trait.
1910   unsigned numSuccessors = op.getNumSuccessors();
1911   unsigned numVariadicSuccessors = op.getNumVariadicSuccessors();
1912   addSizeCountTrait(opClass, "Successor", numSuccessors, numVariadicSuccessors);
1913 
1914   // Add variadic size trait and normal op traits.
1915   int numOperands = op.getNumOperands();
1916   int numVariadicOperands = op.getNumVariableLengthOperands();
1917 
1918   // Add operand size trait.
1919   if (numVariadicOperands != 0) {
1920     if (numOperands == numVariadicOperands)
1921       opClass.addTrait("OpTrait::VariadicOperands");
1922     else
1923       opClass.addTrait("OpTrait::AtLeastNOperands<" +
1924                        Twine(numOperands - numVariadicOperands) + ">::Impl");
1925   } else {
1926     switch (numOperands) {
1927     case 0:
1928       opClass.addTrait("OpTrait::ZeroOperands");
1929       break;
1930     case 1:
1931       opClass.addTrait("OpTrait::OneOperand");
1932       break;
1933     default:
1934       opClass.addTrait("OpTrait::NOperands<" + Twine(numOperands) + ">::Impl");
1935       break;
1936     }
1937   }
1938 
1939   // Add the native and interface traits.
1940   for (const auto &trait : op.getTraits()) {
1941     if (auto opTrait = dyn_cast<tblgen::NativeOpTrait>(&trait))
1942       opClass.addTrait(opTrait->getTrait());
1943     else if (auto opTrait = dyn_cast<tblgen::InterfaceOpTrait>(&trait))
1944       opClass.addTrait(opTrait->getTrait());
1945   }
1946 }
1947 
1948 void OpEmitter::genOpNameGetter() {
1949   auto &method = opClass.newMethod("::llvm::StringRef", "getOperationName",
1950                                    /*params=*/"", OpMethod::MP_Static);
1951   method.body() << "  return \"" << op.getOperationName() << "\";\n";
1952 }
1953 
1954 void OpEmitter::genOpAsmInterface() {
1955   // If the user only has one results or specifically added the Asm trait,
1956   // then don't generate it for them. We specifically only handle multi result
1957   // operations, because the name of a single result in the common case is not
1958   // interesting(generally 'result'/'output'/etc.).
1959   // TODO: We could also add a flag to allow operations to opt in to this
1960   // generation, even if they only have a single operation.
1961   int numResults = op.getNumResults();
1962   if (numResults <= 1 || op.getTrait("::mlir::OpAsmOpInterface::Trait"))
1963     return;
1964 
1965   SmallVector<StringRef, 4> resultNames(numResults);
1966   for (int i = 0; i != numResults; ++i)
1967     resultNames[i] = op.getResultName(i);
1968 
1969   // Don't add the trait if none of the results have a valid name.
1970   if (llvm::all_of(resultNames, [](StringRef name) { return name.empty(); }))
1971     return;
1972   opClass.addTrait("::mlir::OpAsmOpInterface::Trait");
1973 
1974   // Generate the right accessor for the number of results.
1975   auto &method = opClass.newMethod("void", "getAsmResultNames",
1976                                    "OpAsmSetValueNameFn setNameFn");
1977   auto &body = method.body();
1978   for (int i = 0; i != numResults; ++i) {
1979     body << "  auto resultGroup" << i << " = getODSResults(" << i << ");\n"
1980          << "  if (!llvm::empty(resultGroup" << i << "))\n"
1981          << "    setNameFn(*resultGroup" << i << ".begin(), \""
1982          << resultNames[i] << "\");\n";
1983   }
1984 }
1985 
1986 //===----------------------------------------------------------------------===//
1987 // OpOperandAdaptor emitter
1988 //===----------------------------------------------------------------------===//
1989 
1990 namespace {
1991 // Helper class to emit Op operand adaptors to an output stream.  Operand
1992 // adaptors are wrappers around ArrayRef<Value> that provide named operand
1993 // getters identical to those defined in the Op.
1994 class OpOperandAdaptorEmitter {
1995 public:
1996   static void emitDecl(const Operator &op, raw_ostream &os);
1997   static void emitDef(const Operator &op, raw_ostream &os);
1998 
1999 private:
2000   explicit OpOperandAdaptorEmitter(const Operator &op);
2001 
2002   // Add verification function. This generates a verify method for the adaptor
2003   // which verifies all the op-independent attribute constraints.
2004   void addVerification();
2005 
2006   const Operator &op;
2007   Class adaptor;
2008 };
2009 } // end namespace
2010 
2011 OpOperandAdaptorEmitter::OpOperandAdaptorEmitter(const Operator &op)
2012     : op(op), adaptor(op.getAdaptorName()) {
2013   adaptor.newField("::mlir::ValueRange", "odsOperands");
2014   adaptor.newField("::mlir::DictionaryAttr", "odsAttrs");
2015   const auto *attrSizedOperands =
2016       op.getTrait("OpTrait::AttrSizedOperandSegments");
2017   {
2018     auto &constructor = adaptor.newConstructor(
2019         attrSizedOperands
2020             ? "::mlir::ValueRange values, ::mlir::DictionaryAttr attrs"
2021             : "::mlir::ValueRange values, ::mlir::DictionaryAttr attrs = "
2022               "nullptr");
2023     constructor.addMemberInitializer("odsOperands", "values");
2024     constructor.addMemberInitializer("odsAttrs", "attrs");
2025   }
2026 
2027   {
2028     auto &constructor = adaptor.newConstructor(
2029         llvm::formatv("{0}& op", op.getCppClassName()).str());
2030     constructor.addMemberInitializer("odsOperands",
2031                                      "op.getOperation()->getOperands()");
2032     constructor.addMemberInitializer("odsAttrs",
2033                                      "op.getOperation()->getAttrDictionary()");
2034   }
2035 
2036   std::string sizeAttrInit =
2037       formatv(adapterSegmentSizeAttrInitCode, "operand_segment_sizes");
2038   generateNamedOperandGetters(op, adaptor, sizeAttrInit,
2039                               /*rangeType=*/"::mlir::ValueRange",
2040                               /*rangeBeginCall=*/"odsOperands.begin()",
2041                               /*rangeSizeCall=*/"odsOperands.size()",
2042                               /*getOperandCallPattern=*/"odsOperands[{0}]");
2043 
2044   FmtContext fctx;
2045   fctx.withBuilder("::mlir::Builder(odsAttrs.getContext())");
2046 
2047   auto emitAttr = [&](StringRef name, Attribute attr) {
2048     auto &body = adaptor.newMethod(attr.getStorageType(), name).body();
2049     body << "  assert(odsAttrs && \"no attributes when constructing adapter\");"
2050          << "\n  " << attr.getStorageType() << " attr = "
2051          << "odsAttrs.get(\"" << name << "\").";
2052     if (attr.hasDefaultValue() || attr.isOptional())
2053       body << "dyn_cast_or_null<";
2054     else
2055       body << "cast<";
2056     body << attr.getStorageType() << ">();\n";
2057 
2058     if (attr.hasDefaultValue()) {
2059       // Use the default value if attribute is not set.
2060       // TODO: this is inefficient, we are recreating the attribute for every
2061       // call. This should be set instead.
2062       std::string defaultValue = std::string(
2063           tgfmt(attr.getConstBuilderTemplate(), &fctx, attr.getDefaultValue()));
2064       body << "  if (!attr)\n    attr = " << defaultValue << ";\n";
2065     }
2066     body << "  return attr;\n";
2067   };
2068 
2069   for (auto &namedAttr : op.getAttributes()) {
2070     const auto &name = namedAttr.name;
2071     const auto &attr = namedAttr.attr;
2072     if (!attr.isDerivedAttr())
2073       emitAttr(name, attr);
2074   }
2075 
2076   // Add verification function.
2077   addVerification();
2078 }
2079 
2080 void OpOperandAdaptorEmitter::addVerification() {
2081   auto &method = adaptor.newMethod("::mlir::LogicalResult", "verify",
2082                                    /*params=*/"::mlir::Location loc");
2083   auto &body = method.body();
2084 
2085   const char *checkAttrSizedValueSegmentsCode = R"(
2086   {
2087     auto sizeAttr = odsAttrs.get("{0}").cast<::mlir::DenseIntElementsAttr>();
2088     auto numElements = sizeAttr.getType().cast<::mlir::ShapedType>().getNumElements();
2089     if (numElements != {1})
2090       return emitError(loc, "'{0}' attribute for specifying {2} segments "
2091                        "must have {1} elements");
2092   }
2093   )";
2094 
2095   // Verify a few traits first so that we can use
2096   // getODSOperands()/getODSResults() in the rest of the verifier.
2097   for (auto &trait : op.getTraits()) {
2098     if (auto *t = dyn_cast<tblgen::NativeOpTrait>(&trait)) {
2099       if (t->getTrait() == "OpTrait::AttrSizedOperandSegments") {
2100         body << formatv(checkAttrSizedValueSegmentsCode,
2101                         "operand_segment_sizes", op.getNumOperands(),
2102                         "operand");
2103       } else if (t->getTrait() == "OpTrait::AttrSizedResultSegments") {
2104         body << formatv(checkAttrSizedValueSegmentsCode, "result_segment_sizes",
2105                         op.getNumResults(), "result");
2106       }
2107     }
2108   }
2109 
2110   FmtContext verifyCtx;
2111   populateSubstitutions(op, "odsAttrs.get", "getODSOperands",
2112                         "<no results should be genarated>", verifyCtx);
2113   genAttributeVerifier(op, "odsAttrs.get",
2114                        Twine("emitError(loc, \"'") + op.getOperationName() +
2115                            "' op \"",
2116                        /*emitVerificationRequiringOp*/ false, verifyCtx, body);
2117 
2118   body << "  return success();";
2119 }
2120 
2121 void OpOperandAdaptorEmitter::emitDecl(const Operator &op, raw_ostream &os) {
2122   OpOperandAdaptorEmitter(op).adaptor.writeDeclTo(os);
2123 }
2124 
2125 void OpOperandAdaptorEmitter::emitDef(const Operator &op, raw_ostream &os) {
2126   OpOperandAdaptorEmitter(op).adaptor.writeDefTo(os);
2127 }
2128 
2129 // Emits the opcode enum and op classes.
2130 static void emitOpClasses(const std::vector<Record *> &defs, raw_ostream &os,
2131                           bool emitDecl) {
2132   IfDefScope scope("GET_OP_CLASSES", os);
2133   // First emit forward declaration for each class, this allows them to refer
2134   // to each others in traits for example.
2135   if (emitDecl) {
2136     for (auto *def : defs) {
2137       Operator op(*def);
2138       os << "class " << op.getCppClassName() << ";\n";
2139     }
2140   }
2141   for (auto *def : defs) {
2142     Operator op(*def);
2143     if (emitDecl) {
2144       os << formatv(opCommentHeader, op.getQualCppClassName(), "declarations");
2145       OpOperandAdaptorEmitter::emitDecl(op, os);
2146       OpEmitter::emitDecl(op, os);
2147     } else {
2148       os << formatv(opCommentHeader, op.getQualCppClassName(), "definitions");
2149       OpOperandAdaptorEmitter::emitDef(op, os);
2150       OpEmitter::emitDef(op, os);
2151     }
2152   }
2153 }
2154 
2155 // Emits a comma-separated list of the ops.
2156 static void emitOpList(const std::vector<Record *> &defs, raw_ostream &os) {
2157   IfDefScope scope("GET_OP_LIST", os);
2158 
2159   interleave(
2160       // TODO: We are constructing the Operator wrapper instance just for
2161       // getting it's qualified class name here. Reduce the overhead by having a
2162       // lightweight version of Operator class just for that purpose.
2163       defs, [&os](Record *def) { os << Operator(def).getQualCppClassName(); },
2164       [&os]() { os << ",\n"; });
2165 }
2166 
2167 static std::string getOperationName(const Record &def) {
2168   auto prefix = def.getValueAsDef("opDialect")->getValueAsString("name");
2169   auto opName = def.getValueAsString("opName");
2170   if (prefix.empty())
2171     return std::string(opName);
2172   return std::string(llvm::formatv("{0}.{1}", prefix, opName));
2173 }
2174 
2175 static std::vector<Record *>
2176 getAllDerivedDefinitions(const RecordKeeper &recordKeeper,
2177                          StringRef className) {
2178   Record *classDef = recordKeeper.getClass(className);
2179   if (!classDef)
2180     PrintFatalError("ERROR: Couldn't find the `" + className + "' class!\n");
2181 
2182   llvm::Regex includeRegex(opIncFilter), excludeRegex(opExcFilter);
2183   std::vector<Record *> defs;
2184   for (const auto &def : recordKeeper.getDefs()) {
2185     if (!def.second->isSubClassOf(classDef))
2186       continue;
2187     // Include if no include filter or include filter matches.
2188     if (!opIncFilter.empty() &&
2189         !includeRegex.match(getOperationName(*def.second)))
2190       continue;
2191     // Unless there is an exclude filter and it matches.
2192     if (!opExcFilter.empty() &&
2193         excludeRegex.match(getOperationName(*def.second)))
2194       continue;
2195     defs.push_back(def.second.get());
2196   }
2197 
2198   return defs;
2199 }
2200 
2201 static bool emitOpDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
2202   emitSourceFileHeader("Op Declarations", os);
2203 
2204   const auto &defs = getAllDerivedDefinitions(recordKeeper, "Op");
2205   emitOpClasses(defs, os, /*emitDecl=*/true);
2206 
2207   return false;
2208 }
2209 
2210 static bool emitOpDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
2211   emitSourceFileHeader("Op Definitions", os);
2212 
2213   const auto &defs = getAllDerivedDefinitions(recordKeeper, "Op");
2214   emitOpList(defs, os);
2215   emitOpClasses(defs, os, /*emitDecl=*/false);
2216 
2217   return false;
2218 }
2219 
2220 static mlir::GenRegistration
2221     genOpDecls("gen-op-decls", "Generate op declarations",
2222                [](const RecordKeeper &records, raw_ostream &os) {
2223                  return emitOpDecls(records, os);
2224                });
2225 
2226 static mlir::GenRegistration genOpDefs("gen-op-defs", "Generate op definitions",
2227                                        [](const RecordKeeper &records,
2228                                           raw_ostream &os) {
2229                                          return emitOpDefs(records, os);
2230                                        });
2231