1 //===- OpPythonBindingGen.cpp - Generator of Python API for MLIR Ops ------===//
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 // OpPythonBindingGen uses ODS specification of MLIR ops to generate Python
10 // binding classes wrapping a generic operation API.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/TableGen/GenInfo.h"
15 #include "mlir/TableGen/Operator.h"
16 #include "llvm/ADT/StringSet.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/FormatVariadic.h"
19 #include "llvm/TableGen/Error.h"
20 #include "llvm/TableGen/Record.h"
21 
22 using namespace mlir;
23 using namespace mlir::tblgen;
24 
25 /// File header and includes.
26 ///   {0} is the dialect namespace.
27 constexpr const char *fileHeader = R"Py(
28 # Autogenerated by mlir-tblgen; don't manually edit.
29 
30 from ._ods_common import _cext as _ods_cext
31 from ._ods_common import extend_opview_class as _ods_extend_opview_class, segmented_accessor as _ods_segmented_accessor, equally_sized_accessor as _ods_equally_sized_accessor, get_default_loc_context as _ods_get_default_loc_context, get_op_result_or_value as _get_op_result_or_value, get_op_results_or_values as _get_op_results_or_values
32 _ods_ir = _ods_cext.ir
33 
34 try:
35   from . import _{0}_ops_ext as _ods_ext_module
36 except ImportError:
37   _ods_ext_module = None
38 
39 import builtins
40 
41 )Py";
42 
43 /// Template for dialect class:
44 ///   {0} is the dialect namespace.
45 constexpr const char *dialectClassTemplate = R"Py(
46 @_ods_cext.register_dialect
47 class _Dialect(_ods_ir.Dialect):
48   DIALECT_NAMESPACE = "{0}"
49   pass
50 
51 )Py";
52 
53 /// Template for operation class:
54 ///   {0} is the Python class name;
55 ///   {1} is the operation name.
56 constexpr const char *opClassTemplate = R"Py(
57 @_ods_cext.register_operation(_Dialect)
58 @_ods_extend_opview_class(_ods_ext_module)
59 class {0}(_ods_ir.OpView):
60   OPERATION_NAME = "{1}"
61 )Py";
62 
63 /// Template for class level declarations of operand and result
64 /// segment specs.
65 ///   {0} is either "OPERAND" or "RESULT"
66 ///   {1} is the segment spec
67 /// Each segment spec is either None (default) or an array of integers
68 /// where:
69 ///   1 = single element (expect non sequence operand/result)
70 ///   -1 = operand/result is a sequence corresponding to a variadic
71 constexpr const char *opClassSizedSegmentsTemplate = R"Py(
72   _ODS_{0}_SEGMENTS = {1}
73 )Py";
74 
75 /// Template for class level declarations of the _ODS_REGIONS spec:
76 ///   {0} is the minimum number of regions
77 ///   {1} is the Python bool literal for hasNoVariadicRegions
78 constexpr const char *opClassRegionSpecTemplate = R"Py(
79   _ODS_REGIONS = ({0}, {1})
80 )Py";
81 
82 /// Template for single-element accessor:
83 ///   {0} is the name of the accessor;
84 ///   {1} is either 'operand' or 'result';
85 ///   {2} is the position in the element list.
86 constexpr const char *opSingleTemplate = R"Py(
87   @builtins.property
88   def {0}(self):
89     return self.operation.{1}s[{2}]
90 )Py";
91 
92 /// Template for single-element accessor after a variable-length group:
93 ///   {0} is the name of the accessor;
94 ///   {1} is either 'operand' or 'result';
95 ///   {2} is the total number of element groups;
96 ///   {3} is the position of the current group in the group list.
97 /// This works for both a single variadic group (non-negative length) and an
98 /// single optional element (zero length if the element is absent).
99 constexpr const char *opSingleAfterVariableTemplate = R"Py(
100   @builtins.property
101   def {0}(self):
102     _ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1
103     return self.operation.{1}s[{3} + _ods_variadic_group_length - 1]
104 )Py";
105 
106 /// Template for an optional element accessor:
107 ///   {0} is the name of the accessor;
108 ///   {1} is either 'operand' or 'result';
109 ///   {2} is the total number of element groups;
110 ///   {3} is the position of the current group in the group list.
111 constexpr const char *opOneOptionalTemplate = R"Py(
112   @builtins.property
113   def {0}(self):
114     return self.operation.{1}s[{3}] if len(self.operation.{1}s) > {2} else None
115 )Py";
116 
117 /// Template for the variadic group accessor in the single variadic group case:
118 ///   {0} is the name of the accessor;
119 ///   {1} is either 'operand' or 'result';
120 ///   {2} is the total number of element groups;
121 ///   {3} is the position of the current group in the group list.
122 constexpr const char *opOneVariadicTemplate = R"Py(
123   @builtins.property
124   def {0}(self):
125     _ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1
126     return self.operation.{1}s[{3}:{3} + _ods_variadic_group_length]
127 )Py";
128 
129 /// First part of the template for equally-sized variadic group accessor:
130 ///   {0} is the name of the accessor;
131 ///   {1} is either 'operand' or 'result';
132 ///   {2} is the total number of variadic groups;
133 ///   {3} is the number of non-variadic groups preceding the current group;
134 ///   {3} is the number of variadic groups preceding the current group.
135 constexpr const char *opVariadicEqualPrefixTemplate = R"Py(
136   @builtins.property
137   def {0}(self):
138     start, pg = _ods_equally_sized_accessor(operation.{1}s, {2}, {3}, {4}))Py";
139 
140 /// Second part of the template for equally-sized case, accessing a single
141 /// element:
142 ///   {0} is either 'operand' or 'result'.
143 constexpr const char *opVariadicEqualSimpleTemplate = R"Py(
144     return self.operation.{0}s[start]
145 )Py";
146 
147 /// Second part of the template for equally-sized case, accessing a variadic
148 /// group:
149 ///   {0} is either 'operand' or 'result'.
150 constexpr const char *opVariadicEqualVariadicTemplate = R"Py(
151     return self.operation.{0}s[start:start + pg]
152 )Py";
153 
154 /// Template for an attribute-sized group accessor:
155 ///   {0} is the name of the accessor;
156 ///   {1} is either 'operand' or 'result';
157 ///   {2} is the position of the group in the group list;
158 ///   {3} is a return suffix (expected [0] for single-element, empty for
159 ///       variadic, and opVariadicSegmentOptionalTrailingTemplate for optional).
160 constexpr const char *opVariadicSegmentTemplate = R"Py(
161   @builtins.property
162   def {0}(self):
163     {1}_range = _ods_segmented_accessor(
164          self.operation.{1}s,
165          self.operation.attributes["{1}_segment_sizes"], {2})
166     return {1}_range{3}
167 )Py";
168 
169 /// Template for a suffix when accessing an optional element in the
170 /// attribute-sized case:
171 ///   {0} is either 'operand' or 'result';
172 constexpr const char *opVariadicSegmentOptionalTrailingTemplate =
173     R"Py([0] if len({0}_range) > 0 else None)Py";
174 
175 /// Template for an operation attribute getter:
176 ///   {0} is the name of the attribute sanitized for Python;
177 ///   {1} is the Python type of the attribute;
178 ///   {2} os the original name of the attribute.
179 constexpr const char *attributeGetterTemplate = R"Py(
180   @builtins.property
181   def {0}(self):
182     return {1}(self.operation.attributes["{2}"])
183 )Py";
184 
185 /// Template for an optional operation attribute getter:
186 ///   {0} is the name of the attribute sanitized for Python;
187 ///   {1} is the Python type of the attribute;
188 ///   {2} is the original name of the attribute.
189 constexpr const char *optionalAttributeGetterTemplate = R"Py(
190   @builtins.property
191   def {0}(self):
192     if "{2}" not in self.operation.attributes:
193       return None
194     return {1}(self.operation.attributes["{2}"])
195 )Py";
196 
197 /// Template for a getter of a unit operation attribute, returns True of the
198 /// unit attribute is present, False otherwise (unit attributes have meaning
199 /// by mere presence):
200 ///    {0} is the name of the attribute sanitized for Python,
201 ///    {1} is the original name of the attribute.
202 constexpr const char *unitAttributeGetterTemplate = R"Py(
203   @builtins.property
204   def {0}(self):
205     return "{1}" in self.operation.attributes
206 )Py";
207 
208 /// Template for an operation attribute setter:
209 ///    {0} is the name of the attribute sanitized for Python;
210 ///    {1} is the original name of the attribute.
211 constexpr const char *attributeSetterTemplate = R"Py(
212   @{0}.setter
213   def {0}(self, value):
214     if value is None:
215       raise ValueError("'None' not allowed as value for mandatory attributes")
216     self.operation.attributes["{1}"] = value
217 )Py";
218 
219 /// Template for a setter of an optional operation attribute, setting to None
220 /// removes the attribute:
221 ///    {0} is the name of the attribute sanitized for Python;
222 ///    {1} is the original name of the attribute.
223 constexpr const char *optionalAttributeSetterTemplate = R"Py(
224   @{0}.setter
225   def {0}(self, value):
226     if value is not None:
227       self.operation.attributes["{1}"] = value
228     elif "{1}" in self.operation.attributes:
229       del self.operation.attributes["{1}"]
230 )Py";
231 
232 /// Template for a setter of a unit operation attribute, setting to None or
233 /// False removes the attribute:
234 ///    {0} is the name of the attribute sanitized for Python;
235 ///    {1} is the original name of the attribute.
236 constexpr const char *unitAttributeSetterTemplate = R"Py(
237   @{0}.setter
238   def {0}(self, value):
239     if bool(value):
240       self.operation.attributes["{1}"] = _ods_ir.UnitAttr.get()
241     elif "{1}" in self.operation.attributes:
242       del self.operation.attributes["{1}"]
243 )Py";
244 
245 /// Template for a deleter of an optional or a unit operation attribute, removes
246 /// the attribute from the operation:
247 ///    {0} is the name of the attribute sanitized for Python;
248 ///    {1} is the original name of the attribute.
249 constexpr const char *attributeDeleterTemplate = R"Py(
250   @{0}.deleter
251   def {0}(self):
252     del self.operation.attributes["{1}"]
253 )Py";
254 
255 constexpr const char *regionAccessorTemplate = R"PY(
256   @builtins.property
257   def {0}():
258     return self.regions[{1}]
259 )PY";
260 
261 static llvm::cl::OptionCategory
262     clOpPythonBindingCat("Options for -gen-python-op-bindings");
263 
264 static llvm::cl::opt<std::string>
265     clDialectName("bind-dialect",
266                   llvm::cl::desc("The dialect to run the generator for"),
267                   llvm::cl::init(""), llvm::cl::cat(clOpPythonBindingCat));
268 
269 using AttributeClasses = DenseMap<StringRef, StringRef>;
270 
271 /// Checks whether `str` is a Python keyword.
272 static bool isPythonKeyword(StringRef str) {
273   static llvm::StringSet<> keywords(
274       {"and",   "as",     "assert",   "break", "class",  "continue",
275        "def",   "del",    "elif",     "else",  "except", "finally",
276        "for",   "from",   "global",   "if",    "import", "in",
277        "is",    "lambda", "nonlocal", "not",   "or",     "pass",
278        "raise", "return", "try",      "while", "with",   "yield"});
279   return keywords.contains(str);
280 }
281 
282 /// Checks whether `str` would shadow a generated variable or attribute
283 /// part of the OpView API.
284 static bool isODSReserved(StringRef str) {
285   static llvm::StringSet<> reserved(
286       {"attributes", "create", "context", "ip", "operands", "print", "get_asm",
287        "loc", "verify", "regions", "results", "self", "operation",
288        "DIALECT_NAMESPACE", "OPERATION_NAME"});
289   return str.startswith("_ods_") || str.endswith("_ods") ||
290          reserved.contains(str);
291 }
292 
293 /// Modifies the `name` in a way that it becomes suitable for Python bindings
294 /// (does not change the `name` if it already is suitable) and returns the
295 /// modified version.
296 static std::string sanitizeName(StringRef name) {
297   if (isPythonKeyword(name) || isODSReserved(name))
298     return (name + "_").str();
299   return name.str();
300 }
301 
302 static std::string attrSizedTraitForKind(const char *kind) {
303   return llvm::formatv("::mlir::OpTrait::AttrSized{0}{1}Segments",
304                        llvm::StringRef(kind).take_front().upper(),
305                        llvm::StringRef(kind).drop_front());
306 }
307 
308 /// Emits accessors to "elements" of an Op definition. Currently, the supported
309 /// elements are operands and results, indicated by `kind`, which must be either
310 /// `operand` or `result` and is used verbatim in the emitted code.
311 static void emitElementAccessors(
312     const Operator &op, raw_ostream &os, const char *kind,
313     llvm::function_ref<unsigned(const Operator &)> getNumVariadic,
314     llvm::function_ref<int(const Operator &)> getNumElements,
315     llvm::function_ref<const NamedTypeConstraint &(const Operator &, int)>
316         getElement) {
317   assert(llvm::is_contained(
318              llvm::SmallVector<StringRef, 2>{"operand", "result"}, kind) &&
319          "unsupported kind");
320 
321   // Traits indicating how to process variadic elements.
322   std::string sameSizeTrait =
323       llvm::formatv("::mlir::OpTrait::SameVariadic{0}{1}Size",
324                     llvm::StringRef(kind).take_front().upper(),
325                     llvm::StringRef(kind).drop_front());
326   std::string attrSizedTrait = attrSizedTraitForKind(kind);
327 
328   unsigned numVariadic = getNumVariadic(op);
329 
330   // If there is only one variadic element group, its size can be inferred from
331   // the total number of elements. If there are none, the generation is
332   // straightforward.
333   if (numVariadic <= 1) {
334     bool seenVariableLength = false;
335     for (int i = 0, e = getNumElements(op); i < e; ++i) {
336       const NamedTypeConstraint &element = getElement(op, i);
337       if (element.isVariableLength())
338         seenVariableLength = true;
339       if (element.name.empty())
340         continue;
341       if (element.isVariableLength()) {
342         os << llvm::formatv(element.isOptional() ? opOneOptionalTemplate
343                                                  : opOneVariadicTemplate,
344                             sanitizeName(element.name), kind,
345                             getNumElements(op), i);
346       } else if (seenVariableLength) {
347         os << llvm::formatv(opSingleAfterVariableTemplate,
348                             sanitizeName(element.name), kind,
349                             getNumElements(op), i);
350       } else {
351         os << llvm::formatv(opSingleTemplate, sanitizeName(element.name), kind,
352                             i);
353       }
354     }
355     return;
356   }
357 
358   // Handle the operations where variadic groups have the same size.
359   if (op.getTrait(sameSizeTrait)) {
360     int numPrecedingSimple = 0;
361     int numPrecedingVariadic = 0;
362     for (int i = 0, e = getNumElements(op); i < e; ++i) {
363       const NamedTypeConstraint &element = getElement(op, i);
364       if (!element.name.empty()) {
365         os << llvm::formatv(opVariadicEqualPrefixTemplate,
366                             sanitizeName(element.name), kind, numVariadic,
367                             numPrecedingSimple, numPrecedingVariadic);
368         os << llvm::formatv(element.isVariableLength()
369                                 ? opVariadicEqualVariadicTemplate
370                                 : opVariadicEqualSimpleTemplate,
371                             kind);
372       }
373       if (element.isVariableLength())
374         ++numPrecedingVariadic;
375       else
376         ++numPrecedingSimple;
377     }
378     return;
379   }
380 
381   // Handle the operations where the size of groups (variadic or not) is
382   // provided as an attribute. For non-variadic elements, make sure to return
383   // an element rather than a singleton container.
384   if (op.getTrait(attrSizedTrait)) {
385     for (int i = 0, e = getNumElements(op); i < e; ++i) {
386       const NamedTypeConstraint &element = getElement(op, i);
387       if (element.name.empty())
388         continue;
389       std::string trailing;
390       if (!element.isVariableLength())
391         trailing = "[0]";
392       else if (element.isOptional())
393         trailing = std::string(
394             llvm::formatv(opVariadicSegmentOptionalTrailingTemplate, kind));
395       os << llvm::formatv(opVariadicSegmentTemplate, sanitizeName(element.name),
396                           kind, i, trailing);
397     }
398     return;
399   }
400 
401   llvm::PrintFatalError("unsupported " + llvm::Twine(kind) + " structure");
402 }
403 
404 /// Free function helpers accessing Operator components.
405 static int getNumOperands(const Operator &op) { return op.getNumOperands(); }
406 static const NamedTypeConstraint &getOperand(const Operator &op, int i) {
407   return op.getOperand(i);
408 }
409 static int getNumResults(const Operator &op) { return op.getNumResults(); }
410 static const NamedTypeConstraint &getResult(const Operator &op, int i) {
411   return op.getResult(i);
412 }
413 
414 /// Emits accessors to Op operands.
415 static void emitOperandAccessors(const Operator &op, raw_ostream &os) {
416   auto getNumVariadic = [](const Operator &oper) {
417     return oper.getNumVariableLengthOperands();
418   };
419   emitElementAccessors(op, os, "operand", getNumVariadic, getNumOperands,
420                        getOperand);
421 }
422 
423 /// Emits accessors Op results.
424 static void emitResultAccessors(const Operator &op, raw_ostream &os) {
425   auto getNumVariadic = [](const Operator &oper) {
426     return oper.getNumVariableLengthResults();
427   };
428   emitElementAccessors(op, os, "result", getNumVariadic, getNumResults,
429                        getResult);
430 }
431 
432 /// Emits accessors to Op attributes.
433 static void emitAttributeAccessors(const Operator &op,
434                                    const AttributeClasses &attributeClasses,
435                                    raw_ostream &os) {
436   for (const auto &namedAttr : op.getAttributes()) {
437     // Skip "derived" attributes because they are just C++ functions that we
438     // don't currently expose.
439     if (namedAttr.attr.isDerivedAttr())
440       continue;
441 
442     if (namedAttr.name.empty())
443       continue;
444 
445     std::string sanitizedName = sanitizeName(namedAttr.name);
446 
447     // Unit attributes are handled specially.
448     if (namedAttr.attr.getStorageType().trim().equals("::mlir::UnitAttr")) {
449       os << llvm::formatv(unitAttributeGetterTemplate, sanitizedName,
450                           namedAttr.name);
451       os << llvm::formatv(unitAttributeSetterTemplate, sanitizedName,
452                           namedAttr.name);
453       os << llvm::formatv(attributeDeleterTemplate, sanitizedName,
454                           namedAttr.name);
455       continue;
456     }
457 
458     // Other kinds of attributes need a mapping to a Python type.
459     if (!attributeClasses.count(namedAttr.attr.getStorageType().trim()))
460       continue;
461 
462     StringRef pythonType =
463         attributeClasses.lookup(namedAttr.attr.getStorageType());
464     if (namedAttr.attr.isOptional()) {
465       os << llvm::formatv(optionalAttributeGetterTemplate, sanitizedName,
466                           pythonType, namedAttr.name);
467       os << llvm::formatv(optionalAttributeSetterTemplate, sanitizedName,
468                           namedAttr.name);
469       os << llvm::formatv(attributeDeleterTemplate, sanitizedName,
470                           namedAttr.name);
471     } else {
472       os << llvm::formatv(attributeGetterTemplate, sanitizedName, pythonType,
473                           namedAttr.name);
474       os << llvm::formatv(attributeSetterTemplate, sanitizedName,
475                           namedAttr.name);
476       // Non-optional attributes cannot be deleted.
477     }
478   }
479 }
480 
481 /// Template for the default auto-generated builder.
482 ///   {0} is a comma-separated list of builder arguments, including the trailing
483 ///       `loc` and `ip`;
484 ///   {1} is the code populating `operands`, `results` and `attributes`,
485 ///       `successors` fields.
486 constexpr const char *initTemplate = R"Py(
487   def __init__(self, {0}):
488     operands = []
489     results = []
490     attributes = {{}
491     regions = None
492     {1}
493     super().__init__(self.build_generic(
494       attributes=attributes, results=results, operands=operands,
495       successors=_ods_successors, regions=regions, loc=loc, ip=ip))
496 )Py";
497 
498 /// Template for appending a single element to the operand/result list.
499 ///   {0} is the field name.
500 constexpr const char *singleOperandAppendTemplate =
501     "operands.append(_get_op_result_or_value({0}))";
502 constexpr const char *singleResultAppendTemplate = "results.append({0})";
503 
504 /// Template for appending an optional element to the operand/result list.
505 ///   {0} is the field name.
506 constexpr const char *optionalAppendOperandTemplate =
507     "if {0} is not None: operands.append(_get_op_result_or_value({0}))";
508 constexpr const char *optionalAppendResultTemplate =
509     "if {0} is not None: results.append({0})";
510 
511 /// Template for appending a list of elements to the operand/result list.
512 ///   {0} is the field name.
513 constexpr const char *multiOperandAppendTemplate =
514     "operands.extend(_get_op_results_or_values({0}))";
515 constexpr const char *multiOperandAppendPackTemplate =
516     "operands.append(_get_op_results_or_values({0}))";
517 constexpr const char *multiResultAppendTemplate = "results.extend({0})";
518 
519 /// Template for setting an attribute in the operation builder.
520 ///   {0} is the attribute name;
521 ///   {1} is the builder argument name.
522 constexpr const char *initAttributeTemplate = R"Py(attributes["{0}"] = {1})Py";
523 
524 /// Template for setting an optional attribute in the operation builder.
525 ///   {0} is the attribute name;
526 ///   {1} is the builder argument name.
527 constexpr const char *initOptionalAttributeTemplate =
528     R"Py(if {1} is not None: attributes["{0}"] = {1})Py";
529 
530 constexpr const char *initUnitAttributeTemplate =
531     R"Py(if bool({1}): attributes["{0}"] = _ods_ir.UnitAttr.get(
532       _ods_get_default_loc_context(loc)))Py";
533 
534 /// Template to initialize the successors list in the builder if there are any
535 /// successors.
536 ///   {0} is the value to initialize the successors list to.
537 constexpr const char *initSuccessorsTemplate = R"Py(_ods_successors = {0})Py";
538 
539 /// Template to append or extend the list of successors in the builder.
540 ///   {0} is the list method ('append' or 'extend');
541 ///   {1} is the value to add.
542 constexpr const char *addSuccessorTemplate = R"Py(_ods_successors.{0}({1}))Py";
543 
544 /// Populates `builderArgs` with the Python-compatible names of builder function
545 /// arguments, first the results, then the intermixed attributes and operands in
546 /// the same order as they appear in the `arguments` field of the op definition.
547 /// Additionally, `operandNames` is populated with names of operands in their
548 /// order of appearance.
549 static void
550 populateBuilderArgs(const Operator &op,
551                     llvm::SmallVectorImpl<std::string> &builderArgs,
552                     llvm::SmallVectorImpl<std::string> &operandNames,
553                     llvm::SmallVectorImpl<std::string> &successorArgNames) {
554   for (int i = 0, e = op.getNumResults(); i < e; ++i) {
555     std::string name = op.getResultName(i).str();
556     if (name.empty()) {
557       if (op.getNumResults() == 1) {
558         // Special case for one result, make the default name be 'result'
559         // to properly match the built-in result accessor.
560         name = "result";
561       } else {
562         name = llvm::formatv("_gen_res_{0}", i);
563       }
564     }
565     name = sanitizeName(name);
566     builderArgs.push_back(name);
567   }
568   for (int i = 0, e = op.getNumArgs(); i < e; ++i) {
569     std::string name = op.getArgName(i).str();
570     if (name.empty())
571       name = llvm::formatv("_gen_arg_{0}", i);
572     name = sanitizeName(name);
573     builderArgs.push_back(name);
574     if (!op.getArg(i).is<NamedAttribute *>())
575       operandNames.push_back(name);
576   }
577 
578   for (int i = 0, e = op.getNumSuccessors(); i < e; ++i) {
579     NamedSuccessor successor = op.getSuccessor(i);
580     std::string name = std::string(successor.name);
581     if (name.empty())
582       name = llvm::formatv("_gen_successor_{0}", i);
583     name = sanitizeName(name);
584     builderArgs.push_back(name);
585     successorArgNames.push_back(name);
586   }
587 }
588 
589 /// Populates `builderLines` with additional lines that are required in the
590 /// builder to set up operation attributes. `argNames` is expected to contain
591 /// the names of builder arguments that correspond to op arguments, i.e. to the
592 /// operands and attributes in the same order as they appear in the `arguments`
593 /// field.
594 static void
595 populateBuilderLinesAttr(const Operator &op,
596                          llvm::ArrayRef<std::string> argNames,
597                          llvm::SmallVectorImpl<std::string> &builderLines) {
598   for (int i = 0, e = op.getNumArgs(); i < e; ++i) {
599     Argument arg = op.getArg(i);
600     auto *attribute = arg.dyn_cast<NamedAttribute *>();
601     if (!attribute)
602       continue;
603 
604     // Unit attributes are handled specially.
605     if (attribute->attr.getStorageType().trim().equals("::mlir::UnitAttr")) {
606       builderLines.push_back(llvm::formatv(initUnitAttributeTemplate,
607                                            attribute->name, argNames[i]));
608       continue;
609     }
610 
611     builderLines.push_back(llvm::formatv(attribute->attr.isOptional()
612                                              ? initOptionalAttributeTemplate
613                                              : initAttributeTemplate,
614                                          attribute->name, argNames[i]));
615   }
616 }
617 
618 /// Populates `builderLines` with additional lines that are required in the
619 /// builder to set up successors. successorArgNames is expected to correspond
620 /// to the Python argument name for each successor on the op.
621 static void populateBuilderLinesSuccessors(
622     const Operator &op, llvm::ArrayRef<std::string> successorArgNames,
623     llvm::SmallVectorImpl<std::string> &builderLines) {
624   if (successorArgNames.empty()) {
625     builderLines.push_back(llvm::formatv(initSuccessorsTemplate, "None"));
626     return;
627   }
628 
629   builderLines.push_back(llvm::formatv(initSuccessorsTemplate, "[]"));
630   for (int i = 0, e = successorArgNames.size(); i < e; ++i) {
631     auto &argName = successorArgNames[i];
632     const NamedSuccessor &successor = op.getSuccessor(i);
633     builderLines.push_back(
634         llvm::formatv(addSuccessorTemplate,
635                       successor.isVariadic() ? "extend" : "append", argName));
636   }
637 }
638 
639 /// Populates `builderLines` with additional lines that are required in the
640 /// builder to set up op operands.
641 static void
642 populateBuilderLinesOperand(const Operator &op,
643                             llvm::ArrayRef<std::string> names,
644                             llvm::SmallVectorImpl<std::string> &builderLines) {
645   bool sizedSegments = op.getTrait(attrSizedTraitForKind("operand")) != nullptr;
646 
647   // For each element, find or generate a name.
648   for (int i = 0, e = op.getNumOperands(); i < e; ++i) {
649     const NamedTypeConstraint &element = op.getOperand(i);
650     std::string name = names[i];
651 
652     // Choose the formatting string based on the element kind.
653     llvm::StringRef formatString;
654     if (!element.isVariableLength()) {
655       formatString = singleOperandAppendTemplate;
656     } else if (element.isOptional()) {
657       formatString = optionalAppendOperandTemplate;
658     } else {
659       assert(element.isVariadic() && "unhandled element group type");
660       // If emitting with sizedSegments, then we add the actual list-typed
661       // element. Otherwise, we extend the actual operands.
662       if (sizedSegments) {
663         formatString = multiOperandAppendPackTemplate;
664       } else {
665         formatString = multiOperandAppendTemplate;
666       }
667     }
668 
669     builderLines.push_back(llvm::formatv(formatString.data(), name));
670   }
671 }
672 
673 /// Populates `builderLines` with additional lines that are required in the
674 /// builder to set up op results.
675 static void
676 populateBuilderLinesResult(const Operator &op,
677                            llvm::ArrayRef<std::string> names,
678                            llvm::SmallVectorImpl<std::string> &builderLines) {
679   bool sizedSegments = op.getTrait(attrSizedTraitForKind("result")) != nullptr;
680 
681   // For each element, find or generate a name.
682   for (int i = 0, e = op.getNumResults(); i < e; ++i) {
683     const NamedTypeConstraint &element = op.getResult(i);
684     std::string name = names[i];
685 
686     // Choose the formatting string based on the element kind.
687     llvm::StringRef formatString;
688     if (!element.isVariableLength()) {
689       formatString = singleResultAppendTemplate;
690     } else if (element.isOptional()) {
691       formatString = optionalAppendResultTemplate;
692     } else {
693       assert(element.isVariadic() && "unhandled element group type");
694       // If emitting with sizedSegments, then we add the actual list-typed
695       // element. Otherwise, we extend the actual operands.
696       if (sizedSegments) {
697         formatString = singleResultAppendTemplate;
698       } else {
699         formatString = multiResultAppendTemplate;
700       }
701     }
702 
703     builderLines.push_back(llvm::formatv(formatString.data(), name));
704   }
705 }
706 
707 /// If the operation has variadic regions, adds a builder argument to specify
708 /// the number of those regions and builder lines to forward it to the generic
709 /// constructor.
710 static void
711 populateBuilderRegions(const Operator &op,
712                        llvm::SmallVectorImpl<std::string> &builderArgs,
713                        llvm::SmallVectorImpl<std::string> &builderLines) {
714   if (op.hasNoVariadicRegions())
715     return;
716 
717   // This is currently enforced when Operator is constructed.
718   assert(op.getNumVariadicRegions() == 1 &&
719          op.getRegion(op.getNumRegions() - 1).isVariadic() &&
720          "expected the last region to be varidic");
721 
722   const NamedRegion &region = op.getRegion(op.getNumRegions() - 1);
723   std::string name =
724       ("num_" + region.name.take_front().lower() + region.name.drop_front())
725           .str();
726   builderArgs.push_back(name);
727   builderLines.push_back(
728       llvm::formatv("regions = {0} + {1}", op.getNumRegions() - 1, name));
729 }
730 
731 /// Emits a default builder constructing an operation from the list of its
732 /// result types, followed by a list of its operands.
733 static void emitDefaultOpBuilder(const Operator &op, raw_ostream &os) {
734   // If we are asked to skip default builders, comply.
735   if (op.skipDefaultBuilders())
736     return;
737 
738   llvm::SmallVector<std::string> builderArgs;
739   llvm::SmallVector<std::string> builderLines;
740   llvm::SmallVector<std::string> operandArgNames;
741   llvm::SmallVector<std::string> successorArgNames;
742   builderArgs.reserve(op.getNumOperands() + op.getNumResults() +
743                       op.getNumNativeAttributes() + op.getNumSuccessors());
744   populateBuilderArgs(op, builderArgs, operandArgNames, successorArgNames);
745 
746   populateBuilderLinesResult(
747       op, llvm::makeArrayRef(builderArgs).take_front(op.getNumResults()),
748       builderLines);
749   populateBuilderLinesOperand(op, operandArgNames, builderLines);
750   populateBuilderLinesAttr(
751       op, llvm::makeArrayRef(builderArgs).drop_front(op.getNumResults()),
752       builderLines);
753   populateBuilderLinesSuccessors(op, successorArgNames, builderLines);
754   populateBuilderRegions(op, builderArgs, builderLines);
755 
756   builderArgs.push_back("*");
757   builderArgs.push_back("loc=None");
758   builderArgs.push_back("ip=None");
759   os << llvm::formatv(initTemplate, llvm::join(builderArgs, ", "),
760                       llvm::join(builderLines, "\n    "));
761 }
762 
763 static void constructAttributeMapping(const llvm::RecordKeeper &records,
764                                       AttributeClasses &attributeClasses) {
765   for (const llvm::Record *rec :
766        records.getAllDerivedDefinitions("PythonAttr")) {
767     attributeClasses.try_emplace(rec->getValueAsString("cppStorageType").trim(),
768                                  rec->getValueAsString("pythonType").trim());
769   }
770 }
771 
772 static void emitSegmentSpec(
773     const Operator &op, const char *kind,
774     llvm::function_ref<int(const Operator &)> getNumElements,
775     llvm::function_ref<const NamedTypeConstraint &(const Operator &, int)>
776         getElement,
777     raw_ostream &os) {
778   std::string segmentSpec("[");
779   for (int i = 0, e = getNumElements(op); i < e; ++i) {
780     const NamedTypeConstraint &element = getElement(op, i);
781     if (element.isVariableLength()) {
782       segmentSpec.append("-1,");
783     } else if (element.isOptional()) {
784       segmentSpec.append("0,");
785     } else {
786       segmentSpec.append("1,");
787     }
788   }
789   segmentSpec.append("]");
790 
791   os << llvm::formatv(opClassSizedSegmentsTemplate, kind, segmentSpec);
792 }
793 
794 static void emitRegionAttributes(const Operator &op, raw_ostream &os) {
795   // Emit _ODS_REGIONS = (min_region_count, has_no_variadic_regions).
796   // Note that the base OpView class defines this as (0, True).
797   unsigned minRegionCount = op.getNumRegions() - op.getNumVariadicRegions();
798   os << llvm::formatv(opClassRegionSpecTemplate, minRegionCount,
799                       op.hasNoVariadicRegions() ? "True" : "False");
800 }
801 
802 /// Emits named accessors to regions.
803 static void emitRegionAccessors(const Operator &op, raw_ostream &os) {
804   for (auto en : llvm::enumerate(op.getRegions())) {
805     const NamedRegion &region = en.value();
806     if (region.name.empty())
807       continue;
808 
809     assert((!region.isVariadic() || en.index() == op.getNumRegions() - 1) &&
810            "expected only the last region to be variadic");
811     os << llvm::formatv(regionAccessorTemplate, sanitizeName(region.name),
812                         std::to_string(en.index()) +
813                             (region.isVariadic() ? ":" : ""));
814   }
815 }
816 
817 /// Emits bindings for a specific Op to the given output stream.
818 static void emitOpBindings(const Operator &op,
819                            const AttributeClasses &attributeClasses,
820                            raw_ostream &os) {
821   os << llvm::formatv(opClassTemplate, op.getCppClassName(),
822                       op.getOperationName());
823 
824   // Sized segments.
825   if (op.getTrait(attrSizedTraitForKind("operand")) != nullptr) {
826     emitSegmentSpec(op, "OPERAND", getNumOperands, getOperand, os);
827   }
828   if (op.getTrait(attrSizedTraitForKind("result")) != nullptr) {
829     emitSegmentSpec(op, "RESULT", getNumResults, getResult, os);
830   }
831 
832   emitRegionAttributes(op, os);
833   emitDefaultOpBuilder(op, os);
834   emitOperandAccessors(op, os);
835   emitAttributeAccessors(op, attributeClasses, os);
836   emitResultAccessors(op, os);
837   emitRegionAccessors(op, os);
838 }
839 
840 /// Emits bindings for the dialect specified in the command line, including file
841 /// headers and utilities. Returns `false` on success to comply with Tablegen
842 /// registration requirements.
843 static bool emitAllOps(const llvm::RecordKeeper &records, raw_ostream &os) {
844   if (clDialectName.empty())
845     llvm::PrintFatalError("dialect name not provided");
846 
847   AttributeClasses attributeClasses;
848   constructAttributeMapping(records, attributeClasses);
849 
850   os << llvm::formatv(fileHeader, clDialectName.getValue());
851   os << llvm::formatv(dialectClassTemplate, clDialectName.getValue());
852 
853   for (const llvm::Record *rec : records.getAllDerivedDefinitions("Op")) {
854     Operator op(rec);
855     if (op.getDialectName() == clDialectName.getValue())
856       emitOpBindings(op, attributeClasses, os);
857   }
858   return false;
859 }
860 
861 static GenRegistration
862     genPythonBindings("gen-python-op-bindings",
863                       "Generate Python bindings for MLIR Ops", &emitAllOps);
864