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 "OpGenHelpers.h"
15
16#include "mlir/Support/IndentedOstream.h"
17#include "mlir/TableGen/GenInfo.h"
18#include "mlir/TableGen/Operator.h"
19#include "llvm/ADT/SmallVectorExtras.h"
20#include "llvm/ADT/StringMap.h"
21#include "llvm/ADT/StringSet.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/FormatVariadic.h"
24#include "llvm/TableGen/Error.h"
25#include "llvm/TableGen/Record.h"
26#include <regex>
27
28using namespace mlir;
29using namespace mlir::tblgen;
30using llvm::formatv;
31using llvm::Record;
32using llvm::RecordKeeper;
33
34/// Built-in C++ type to Python type mappings.
35static constexpr std::pair<StringRef, StringRef> builtinTypeMappings[] = {
36 {"::mlir::MemRefType", "_ods_ir.MemRefType"},
37 {"::mlir::UnrankedMemRefType", "_ods_ir.UnrankedMemRefType"},
38 {"::mlir::RankedTensorType", "_ods_ir.RankedTensorType"},
39 {"::mlir::UnrankedTensorType", "_ods_ir.UnrankedTensorType"},
40 {"::mlir::VectorType", "_ods_ir.VectorType"},
41 {"::mlir::IntegerType", "_ods_ir.IntegerType"},
42 {"::mlir::FloatType", "_ods_ir.FloatType"},
43 {"::mlir::IndexType", "_ods_ir.IndexType"},
44 {"::mlir::ComplexType", "_ods_ir.ComplexType"},
45 {"::mlir::TupleType", "_ods_ir.TupleType"},
46 {"::mlir::NoneType", "_ods_ir.NoneType"},
47};
48
49/// Built-in TableGen attribute def name to Python type mappings.
50static constexpr std::pair<StringRef, StringRef> builtinAttrTypeMappings[] = {
51 {"BoolAttr", "bool"},
52 {"I1Attr", "bool"},
53 {"I8Attr", "int"},
54 {"I16Attr", "int"},
55 {"I32Attr", "int"},
56 {"I64Attr", "int"},
57 {"SI1Attr", "int"},
58 {"SI8Attr", "int"},
59 {"SI16Attr", "int"},
60 {"SI32Attr", "int"},
61 {"SI64Attr", "int"},
62 {"UI1Attr", "int"},
63 {"UI8Attr", "int"},
64 {"UI16Attr", "int"},
65 {"UI32Attr", "int"},
66 {"UI64Attr", "int"},
67 {"IndexAttr", "int"},
68 {"F32Attr", "float"},
69 {"F64Attr", "float"},
70 {"StrAttr", "str"},
71 {"SymbolNameAttr", "str"},
72 {"FlatSymbolRefAttr", "str"},
73 {"SymbolRefAttr", "str"},
74 {"TypeAttr", "_ods_ir.Type"},
75 {"AffineMapAttr", "_ods_ir.AffineMap"},
76 {"IntegerSetAttr", "_ods_ir.IntegerSet"},
77 {"DictionaryAttr", "dict"},
78 {"ArrayAttr", "_Sequence[_ods_ir.Attribute]"},
79 {"I32ArrayAttr", "_Sequence[int]"},
80 {"I64ArrayAttr", "_Sequence[int]"},
81 {"I64SmallVectorArrayAttr", "_Sequence[int]"},
82 {"F32ArrayAttr", "_Sequence[float]"},
83 {"F64ArrayAttr", "_Sequence[float]"},
84 {"BoolArrayAttr", "_Sequence[bool]"},
85 {"DenseBoolArrayAttr", "_Sequence[bool]"},
86 {"StrArrayAttr", "_Sequence[str]"},
87 {"FlatSymbolRefArrayAttr", "_Sequence[str]"},
88 {"DenseI8ArrayAttr", "_Sequence[int]"},
89 {"DenseI16ArrayAttr", "_Sequence[int]"},
90 {"DenseI32ArrayAttr", "_Sequence[int]"},
91 {"DenseI64ArrayAttr", "_Sequence[int]"},
92 {"DenseF32ArrayAttr", "_Sequence[float]"},
93 {"DenseF64ArrayAttr", "_Sequence[float]"},
94 {"I32ElementsAttr", "_Union[_Sequence[int], _Buffer]"},
95 {"I64ElementsAttr", "_Union[_Sequence[int], _Buffer]"},
96 {"IndexElementsAttr", "_Union[_Sequence[int], _Buffer]"},
97 {"F64ElementsAttr", "_Union[_Sequence[float], _Buffer]"},
98};
99
100/// Maps from C++ type names to Python type annotations.
101static llvm::StringMap<std::string> pythonTypeMap;
102
103/// Maps from TableGen attribute def names to Python types.
104static llvm::StringMap<std::string> pythonAttrTypeMap;
105
106/// File header and includes.
107/// {0} is the dialect namespace.
108constexpr const char *fileHeader = R"Py(
109# Autogenerated by mlir-tblgen; don't manually edit.
110
111from ._ods_common import _cext as _ods_cext
112from ._ods_common import (
113 equally_sized_accessor as _ods_equally_sized_accessor,
114 get_default_loc_context as _ods_get_default_loc_context,
115 get_op_results_or_values as _get_op_results_or_values,
116 segmented_accessor as _ods_segmented_accessor,
117)
118_ods_ir = _ods_cext.ir
119_ods_cext.globals.register_traceback_file_exclusion(__file__)
120
121import builtins
122from typing import Any as _Any, Sequence as _Sequence, Union as _Union, Optional as _Optional
123import sys as _sys
124if _sys.version_info >= (3, 12):
125 from collections.abc import Buffer as _Buffer # pytype: disable=not-supported-yet
126else:
127 try:
128 from typing_extensions import Buffer as _Buffer
129 except ImportError:
130 _Buffer = _Any
131
132)Py";
133
134/// Template for dialect class:
135/// {0} is the dialect namespace.
136constexpr const char *dialectClassTemplate = R"Py(
137@_ods_cext.register_dialect
138class _Dialect(_ods_ir.Dialect):
139 DIALECT_NAMESPACE = "{0}"
140)Py";
141
142constexpr const char *dialectExtensionTemplate = R"Py(
143from ._{0}_ops_gen import _Dialect
144)Py";
145
146/// Template for operation class:
147/// {0} is the Python class name;
148/// {1} is the operation name;
149/// {2} is the docstring for this operation.
150constexpr const char *opClassTemplate = R"Py(
151@_ods_cext.register_operation(_Dialect)
152class {0}(_ods_ir.OpView):{2}
153 OPERATION_NAME = "{1}"
154)Py";
155
156/// Template for operation class:
157/// {0} is the Python class name;
158/// {1} is the operation name。
159constexpr const char *opAdaptorClassTemplate = R"Py(
160@_ods_cext.register_op_adaptor({0})
161class {0}Adaptor(_ods_ir.OpAdaptor):
162 OPERATION_NAME = "{1}"
163)Py";
164
165/// Template for class level declarations of operand and result
166/// segment specs.
167/// {0} is either "OPERAND" or "RESULT"
168/// {1} is the segment spec
169/// Each segment spec is either None (default) or an array of integers
170/// where:
171/// 1 = single element (expect non sequence operand/result)
172/// 0 = optional element (expect a value or std::nullopt)
173/// -1 = operand/result is a sequence corresponding to a variadic
174constexpr const char *opClassSizedSegmentsTemplate = R"Py(
175 _ODS_{0}_SEGMENTS = {1}
176)Py";
177
178/// Template for class level declarations of the _ODS_REGIONS spec:
179/// {0} is the minimum number of regions
180/// {1} is the Python bool literal for hasNoVariadicRegions
181constexpr const char *opClassRegionSpecTemplate = R"Py(
182 _ODS_REGIONS = ({0}, {1})
183)Py";
184
185/// Template for single-element accessor:
186/// {0} is the name of the accessor;
187/// {1} is either 'operand' or 'result';
188/// {2} is the position in the element list.
189/// {3} is the type hint.
190constexpr const char *opSingleTemplate = R"Py(
191 @builtins.property
192 def {0}(self) -> {3}:
193 return self.{1}s[{2}]
194)Py";
195
196/// Template for single-element accessor after a variable-length group:
197/// {0} is the name of the accessor;
198/// {1} is either 'operand' or 'result';
199/// {2} is the total number of element groups;
200/// {3} is the position of the current group in the group list.
201/// {4} is the type hint.
202/// This works for both a single variadic group (non-negative length) and an
203/// single optional element (zero length if the element is absent).
204constexpr const char *opSingleAfterVariableTemplate = R"Py(
205 @builtins.property
206 def {0}(self) -> {4}:
207 _ods_variadic_group_length = len(self.{1}s) - {2} + 1
208 return self.{1}s[{3} + _ods_variadic_group_length - 1]
209)Py";
210
211/// Template for an optional element accessor:
212/// {0} is the name of the accessor;
213/// {1} is either 'operand' or 'result';
214/// {2} is the total number of element groups;
215/// {3} is the position of the current group in the group list.
216/// {4} is the type hint.
217/// This works if we have only one variable-length group (and it's the optional
218/// operand/result): we can deduce it's absent if the `len(operation.{1}s)` is
219/// smaller than the total number of groups.
220constexpr const char *opOneOptionalTemplate = R"Py(
221 @builtins.property
222 def {0}(self) -> _Optional[{4}]:
223 return None if len(self.{1}s) < {2} else self.{1}s[{3}]
224)Py";
225
226/// Template for the variadic group accessor in the single variadic group case:
227/// {0} is the name of the accessor;
228/// {1} is either 'operand' or 'result';
229/// {2} is the total number of element groups;
230/// {3} is the position of the current group in the group list.
231/// {4} is the type hint.
232constexpr const char *opOneVariadicTemplate = R"Py(
233 @builtins.property
234 def {0}(self) -> {4}:
235 _ods_variadic_group_length = len(self.{1}s) - {2} + 1
236 return self.{1}s[{3}:{3} + _ods_variadic_group_length]
237)Py";
238
239/// First part of the template for equally-sized variadic group accessor:
240/// {0} is the name of the accessor;
241/// {1} is either 'operand' or 'result';
242/// {2} is the total number of non-variadic groups;
243/// {3} is the total number of variadic groups;
244/// {4} is the number of non-variadic groups preceding the current group;
245/// {5} is the number of variadic groups preceding the current group.
246/// {6} is the type hint.
247constexpr const char *opVariadicEqualPrefixTemplate = R"Py(
248 @builtins.property
249 def {0}(self) -> {6}:
250 start, elements_per_group = _ods_equally_sized_accessor(self.{1}s, {2}, {3}, {4}, {5}))Py";
251
252/// Second part of the template for equally-sized case, accessing a single
253/// element:
254/// {0} is either 'operand' or 'result'.
255constexpr const char *opVariadicEqualSimpleTemplate = R"Py(
256 return self.{0}s[start]
257)Py";
258
259/// Second part of the template for equally-sized case, accessing a variadic
260/// group:
261/// {0} is either 'operand' or 'result'.
262constexpr const char *opVariadicEqualVariadicTemplate = R"Py(
263 return self.{0}s[start:start + elements_per_group]
264)Py";
265
266/// Template for an attribute-sized group accessor:
267/// {0} is the name of the accessor;
268/// {1} is either 'operand' or 'result';
269/// {2} is the position of the group in the group list;
270/// {3} is a return suffix (expected [0] for single-element, empty for
271/// variadic, and opVariadicSegmentOptionalTrailingTemplate for optional);
272/// {4} is the type hint;
273/// {5} is the instance variable name in python;
274/// {6} is the instance variable name for attributes in python.
275constexpr const char *opVariadicSegmentTemplate = R"Py(
276 @builtins.property
277 def {0}(self) -> {4}:
278 {1}_range = _ods_segmented_accessor(
279 self.{5}s,
280 self.{6}["{1}SegmentSizes"], {2})
281 return {1}_range{3}
282)Py";
283
284/// Template for a suffix when accessing an optional element in the
285/// attribute-sized case:
286/// {0} is either 'operand' or 'result';
287constexpr const char *opVariadicSegmentOptionalTrailingTemplate =
288 R"Py([0] if len({0}_range) > 0 else None)Py";
289
290/// Template for an operation attribute getter:
291/// {0} is the name of the attribute sanitized for Python;
292/// {1} is the original name of the attribute.
293/// {2} is the type hint.
294constexpr const char *attributeGetterTemplate = R"Py(
295 @builtins.property
296 def {0}(self) -> {2}:
297 return self.operation.attributes["{1}"]
298)Py";
299
300/// Template for an optional operation attribute getter:
301/// {0} is the name of the attribute sanitized for Python;
302/// {1} is the original name of the attribute.
303/// {2} is the type hint.
304constexpr const char *optionalAttributeGetterTemplate = R"Py(
305 @builtins.property
306 def {0}(self) -> _Optional[{2}]:
307 if "{1}" not in self.operation.attributes:
308 return None
309 return self.operation.attributes["{1}"]
310)Py";
311
312/// Template for an operation attribute getter for adaptors:
313/// {0} is the name of the attribute sanitized for Python;
314/// {1} is the original name of the attribute.
315/// {2} is the type hint.
316constexpr const char *adaptorAttributeGetterTemplate = R"Py(
317 @builtins.property
318 def {0}(self) -> {2}:
319 return self.attributes["{1}"]
320)Py";
321
322/// Template for an optional operation attribute getter for adaptors:
323/// {0} is the name of the attribute sanitized for Python;
324/// {1} is the original name of the attribute.
325/// {2} is the type hint.
326constexpr const char *adaptorOptionalAttributeGetterTemplate = R"Py(
327 @builtins.property
328 def {0}(self) -> _Optional[{2}]:
329 if "{1}" not in self.attributes:
330 return None
331 return self.attributes["{1}"]
332)Py";
333
334/// Template for a getter of a unit operation attribute, returns True of the
335/// unit attribute is present, False otherwise (unit attributes have meaning
336/// by mere presence):
337/// {0} is the name of the attribute sanitized for Python,
338/// {1} is the original name of the attribute.
339constexpr const char *unitAttributeGetterTemplate = R"Py(
340 @builtins.property
341 def {0}(self) -> bool:
342 return "{1}" in self.operation.attributes
343)Py";
344
345/// Template for a getter of a unit operation attribute for adaptors, returns
346/// True of the unit attribute is present, False otherwise (unit attributes have
347/// meaning by mere presence):
348/// {0} is the name of the attribute sanitized for Python,
349/// {1} is the original name of the attribute.
350constexpr const char *adaptorUnitAttributeGetterTemplate = R"Py(
351 @builtins.property
352 def {0}(self) -> bool:
353 return "{1}" in self.attributes
354)Py";
355
356/// Template for an operation attribute setter:
357/// {0} is the name of the attribute sanitized for Python;
358/// {1} is the original name of the attribute.
359/// {2} is the type hint.
360constexpr const char *attributeSetterTemplate = R"Py(
361 @{0}.setter
362 def {0}(self, value: {2}):
363 if value is None:
364 raise ValueError("'None' not allowed as value for mandatory attributes")
365 self.operation.attributes["{1}"] = value
366)Py";
367
368/// Template for a setter of an optional operation attribute, setting to None
369/// removes the attribute:
370/// {0} is the name of the attribute sanitized for Python;
371/// {1} is the original name of the attribute.
372/// {2} is the type hint.
373constexpr const char *optionalAttributeSetterTemplate = R"Py(
374 @{0}.setter
375 def {0}(self, value: _Optional[{2}]):
376 if value is not None:
377 self.operation.attributes["{1}"] = value
378 elif "{1}" in self.operation.attributes:
379 del self.operation.attributes["{1}"]
380)Py";
381
382/// Template for a setter of a unit operation attribute, setting to None or
383/// False removes the attribute:
384/// {0} is the name of the attribute sanitized for Python;
385/// {1} is the original name of the attribute.
386constexpr const char *unitAttributeSetterTemplate = R"Py(
387 @{0}.setter
388 def {0}(self, value):
389 if bool(value):
390 self.operation.attributes["{1}"] = _ods_ir.UnitAttr.get()
391 elif "{1}" in self.operation.attributes:
392 del self.operation.attributes["{1}"]
393)Py";
394
395/// Template for a deleter of an optional or a unit operation attribute, removes
396/// the attribute from the operation:
397/// {0} is the name of the attribute sanitized for Python;
398/// {1} is the original name of the attribute.
399constexpr const char *attributeDeleterTemplate = R"Py(
400 @{0}.deleter
401 def {0}(self):
402 del self.operation.attributes["{1}"]
403)Py";
404
405constexpr const char *regionAccessorTemplate = R"Py(
406 @builtins.property
407 def {0}(self) -> {2}:
408 return self.regions[{1}]
409)Py";
410
411constexpr const char *valueBuilderTemplate = R"Py(
412def {0}({2}) -> {4}:
413 return {1}({3}){5}
414)Py";
415
416constexpr const char *valueBuilderVariadicTemplate = R"Py(
417def {0}({2}) -> _Union[_ods_ir.OpResult, _ods_ir.OpResultList, {1}]:
418 op = {1}({3}); results = op.results
419 return results if len(results) > 1 else (results[0] if len(results) == 1 else op)
420)Py";
421
422static llvm::cl::OptionCategory
423 clOpPythonBindingCat("Options for -gen-python-op-bindings");
424
425std::string dialectNameStorage;
426
427llvm::cl::opt<std::string, /*ExternalStorage=*/true>
428 clDialectName("bind-dialect",
429 llvm::cl::desc("The dialect to run the generator for"),
430 llvm::cl::location(L&: dialectNameStorage),
431 llvm::cl::cat(clOpPythonBindingCat));
432
433static llvm::cl::opt<std::string> clDialectExtensionName(
434 "dialect-extension", llvm::cl::desc("The prefix of the dialect extension"),
435 llvm::cl::init(Val: ""), llvm::cl::cat(clOpPythonBindingCat));
436
437using AttributeClasses = DenseMap<StringRef, StringRef>;
438
439/// Checks whether `str` would shadow a generated variable or attribute
440/// part of the OpView API.
441static bool isODSReserved(StringRef str) {
442 static llvm::StringSet<> reserved(
443 {"attributes", "create", "context", "ip", "operands", "print", "get_asm",
444 "loc", "verify", "regions", "results", "self", "operation",
445 "DIALECT_NAMESPACE", "OPERATION_NAME"});
446 return str.starts_with(Prefix: "_ods_") || str.ends_with(Suffix: "_ods") ||
447 reserved.contains(key: str);
448}
449
450/// Modifies the `name` in a way that it becomes suitable for Python bindings
451/// (does not change the `name` if it already is suitable) and returns the
452/// modified version.
453static std::string sanitizeName(StringRef name) {
454 std::string processedStr = name.str();
455 std::replace_if(
456 first: processedStr.begin(), last: processedStr.end(),
457 pred: [](char c) { return !llvm::isAlnum(C: c); }, new_value: '_');
458
459 if (llvm::isDigit(C: *processedStr.begin()))
460 return "_" + processedStr;
461
462 if (isPythonReserved(str: processedStr) || isODSReserved(str: processedStr))
463 return processedStr + "_";
464 return processedStr;
465}
466
467static std::string attrSizedTraitForKind(const char *kind) {
468 return formatv(Fmt: "::mlir::OpTrait::AttrSized{0}{1}Segments",
469 Vals: StringRef(kind).take_front().upper(),
470 Vals: StringRef(kind).drop_front());
471}
472
473/// Returns the Python type annotation for a given type constraint.
474/// Returns empty StringRef if no mapping is known.
475static StringRef getPythonType(const tblgen::TypeConstraint &constraint) {
476 auto it = pythonTypeMap.find(Key: constraint.getCppType());
477 if (it != pythonTypeMap.end())
478 return it->second;
479 return StringRef();
480}
481
482/// Emits accessors to "elements" of an Op definition. Currently, the supported
483/// elements are operands and results, indicated by `kind`, which must be either
484/// `operand` or `result` and is used verbatim in the emitted code.
485static void emitElementAccessors(
486 const Operator &op, raw_ostream &os, const char *kind,
487 unsigned numVariadicGroups, unsigned numElements,
488 llvm::function_ref<const NamedTypeConstraint &(const Operator &, int)>
489 getElement,
490 bool isAdaptor = false) {
491 assert(llvm::is_contained(SmallVector<StringRef, 2>{"operand", "result"},
492 kind) &&
493 "unsupported kind");
494
495 // Traits indicating how to process variadic elements.
496 std::string sameSizeTrait = formatv(Fmt: "::mlir::OpTrait::SameVariadic{0}{1}Size",
497 Vals: StringRef(kind).take_front().upper(),
498 Vals: StringRef(kind).drop_front());
499 std::string attrSizedTrait = attrSizedTraitForKind(kind);
500
501 std::string pyAttrName = isAdaptor ? kind : std::string("operation.") + kind;
502
503 // If there is only one variable-length element group, its size can be
504 // inferred from the total number of elements. If there are none, the
505 // generation is straightforward.
506 if (numVariadicGroups <= 1) {
507 bool seenVariableLength = false;
508 for (unsigned i = 0; i < numElements; ++i) {
509 const NamedTypeConstraint &element = getElement(op, i);
510 if (element.isVariableLength())
511 seenVariableLength = true;
512 if (element.name.empty())
513 continue;
514 std::string type = std::strcmp(s1: kind, s2: "operand") == 0 ? "_ods_ir.Value"
515 : "_ods_ir.OpResult";
516 if (StringRef pythonType = getPythonType(constraint: element.constraint);
517 !pythonType.empty())
518 type = llvm::formatv(Fmt: "{0}[{1}]", Vals&: type, Vals&: pythonType);
519 if (element.isVariableLength()) {
520 if (element.isOptional()) {
521 os << formatv(Fmt: opOneOptionalTemplate, Vals: sanitizeName(name: element.name),
522 Vals&: pyAttrName, Vals&: numElements, Vals&: i, Vals&: type);
523 } else {
524 type = std::strcmp(s1: kind, s2: "operand") == 0 ? "_ods_ir.OpOperandList"
525 : "_ods_ir.OpResultList";
526 if (StringRef pythonType = getPythonType(constraint: element.constraint);
527 !pythonType.empty())
528 type = llvm::formatv(Fmt: "{0}[{1}]", Vals&: type, Vals&: pythonType);
529 os << formatv(Fmt: opOneVariadicTemplate, Vals: sanitizeName(name: element.name),
530 Vals&: pyAttrName, Vals&: numElements, Vals&: i, Vals&: type);
531 }
532 } else if (seenVariableLength) {
533 os << formatv(Fmt: opSingleAfterVariableTemplate, Vals: sanitizeName(name: element.name),
534 Vals&: pyAttrName, Vals&: numElements, Vals&: i, Vals&: type);
535 } else {
536 os << formatv(Fmt: opSingleTemplate, Vals: sanitizeName(name: element.name), Vals&: pyAttrName,
537 Vals&: i, Vals&: type);
538 }
539 }
540 return;
541 }
542
543 // Handle the operations where variadic groups have the same size.
544 if (op.getTrait(trait: sameSizeTrait)) {
545 // Count the number of simple elements
546 unsigned numSimpleLength = 0;
547 for (unsigned i = 0; i < numElements; ++i) {
548 const NamedTypeConstraint &element = getElement(op, i);
549 if (!element.isVariableLength()) {
550 ++numSimpleLength;
551 }
552 }
553
554 // Generate the accessors
555 int numPrecedingSimple = 0;
556 int numPrecedingVariadic = 0;
557 for (unsigned i = 0; i < numElements; ++i) {
558 const NamedTypeConstraint &element = getElement(op, i);
559 if (!element.name.empty()) {
560 std::string type;
561 if (element.isVariableLength()) {
562 type = std::strcmp(s1: kind, s2: "operand") == 0 ? "_ods_ir.OpOperandList"
563 : "_ods_ir.OpResultList";
564 } else {
565 type = std::strcmp(s1: kind, s2: "operand") == 0 ? "_ods_ir.Value"
566 : "_ods_ir.OpResult";
567 }
568 if (StringRef pythonType = getPythonType(constraint: element.constraint);
569 !pythonType.empty()) {
570 type = llvm::formatv(Fmt: "{0}[{1}]", Vals&: type, Vals&: pythonType);
571 }
572 os << formatv(Fmt: opVariadicEqualPrefixTemplate, Vals: sanitizeName(name: element.name),
573 Vals&: pyAttrName, Vals&: numSimpleLength, Vals&: numVariadicGroups,
574 Vals&: numPrecedingSimple, Vals&: numPrecedingVariadic, Vals&: type);
575 os << formatv(Fmt: element.isVariableLength()
576 ? opVariadicEqualVariadicTemplate
577 : opVariadicEqualSimpleTemplate,
578 Vals&: pyAttrName);
579 }
580 if (element.isVariableLength())
581 ++numPrecedingVariadic;
582 else
583 ++numPrecedingSimple;
584 }
585 return;
586 }
587
588 // Handle the operations where the size of groups (variadic or not) is
589 // provided as an attribute. For non-variadic elements, make sure to return
590 // an element rather than a singleton container.
591 if (op.getTrait(trait: attrSizedTrait)) {
592 for (unsigned i = 0; i < numElements; ++i) {
593 const NamedTypeConstraint &element = getElement(op, i);
594 if (element.name.empty())
595 continue;
596 std::string trailing;
597 std::string type = std::strcmp(s1: kind, s2: "operand") == 0
598 ? "_ods_ir.OpOperandList"
599 : "_ods_ir.OpResultList";
600 if (!element.isVariableLength() || element.isOptional()) {
601 type = std::strcmp(s1: kind, s2: "operand") == 0 ? "_ods_ir.Value"
602 : "_ods_ir.OpResult";
603 if (StringRef pythonType = getPythonType(constraint: element.constraint);
604 !pythonType.empty()) {
605 type = llvm::formatv(Fmt: "{0}[{1}]", Vals&: type, Vals&: pythonType);
606 }
607 if (!element.isVariableLength()) {
608 trailing = "[0]";
609 } else if (element.isOptional()) {
610 type = "_Optional[" + type + "]";
611 trailing = std::string(
612 formatv(Fmt: opVariadicSegmentOptionalTrailingTemplate, Vals&: kind));
613 }
614 } else {
615 if (StringRef pythonType = getPythonType(constraint: element.constraint);
616 !pythonType.empty()) {
617 type = llvm::formatv(Fmt: "{0}[{1}]", Vals&: type, Vals&: pythonType);
618 }
619 }
620
621 os << formatv(Fmt: opVariadicSegmentTemplate, Vals: sanitizeName(name: element.name), Vals&: kind,
622 Vals&: i, Vals&: trailing, Vals&: type, Vals&: pyAttrName,
623 Vals: isAdaptor ? "attributes" : "operation.attributes");
624 }
625 return;
626 }
627
628 llvm::PrintFatalError(Msg: "unsupported " + llvm::Twine(kind) + " structure");
629}
630
631/// Free function helpers accessing Operator components.
632static int getNumOperands(const Operator &op) { return op.getNumOperands(); }
633static const NamedTypeConstraint &getOperand(const Operator &op, int i) {
634 return op.getOperand(index: i);
635}
636static int getNumResults(const Operator &op) { return op.getNumResults(); }
637static const NamedTypeConstraint &getResult(const Operator &op, int i) {
638 return op.getResult(index: i);
639}
640
641/// Emits accessors to Op operands.
642static void emitOperandAccessors(const Operator &op, raw_ostream &os) {
643 emitElementAccessors(op, os, kind: "operand", numVariadicGroups: op.getNumVariableLengthOperands(),
644 numElements: getNumOperands(op), getElement: getOperand);
645}
646
647/// Emits accessors Op results.
648static void emitResultAccessors(const Operator &op, raw_ostream &os) {
649 emitElementAccessors(op, os, kind: "result", numVariadicGroups: op.getNumVariableLengthResults(),
650 numElements: getNumResults(op), getElement: getResult);
651}
652
653static std::string getPythonAttrName(mlir::tblgen::Attribute attr) {
654 auto storageTypeStr = attr.getStorageType();
655 if (storageTypeStr == "::mlir::AffineMapAttr")
656 return "AffineMapAttr";
657 if (storageTypeStr == "::mlir::ArrayAttr")
658 return "ArrayAttr";
659 if (storageTypeStr == "::mlir::BoolAttr")
660 return "BoolAttr";
661 if (storageTypeStr == "::mlir::DenseBoolArrayAttr")
662 return "DenseBoolArrayAttr";
663 if (storageTypeStr == "::mlir::DenseElementsAttr") {
664 llvm::StringSet<> superClasses;
665 for (const Record *sc : attr.getDef().getSuperClasses())
666 superClasses.insert(key: sc->getNameInitAsString());
667 if (superClasses.contains(key: "FloatElementsAttr") ||
668 superClasses.contains(key: "RankedFloatElementsAttr")) {
669 return "DenseFPElementsAttr";
670 }
671 return "DenseElementsAttr";
672 }
673 if (storageTypeStr == "::mlir::DenseF32ArrayAttr")
674 return "DenseF32ArrayAttr";
675 if (storageTypeStr == "::mlir::DenseF64ArrayAttr")
676 return "DenseF64ArrayAttr";
677 if (storageTypeStr == "::mlir::DenseFPElementsAttr")
678 return "DenseFPElementsAttr";
679 if (storageTypeStr == "::mlir::DenseI16ArrayAttr")
680 return "DenseI16ArrayAttr";
681 if (storageTypeStr == "::mlir::DenseI32ArrayAttr")
682 return "DenseI32ArrayAttr";
683 if (storageTypeStr == "::mlir::DenseI64ArrayAttr")
684 return "DenseI64ArrayAttr";
685 if (storageTypeStr == "::mlir::DenseI8ArrayAttr")
686 return "DenseI8ArrayAttr";
687 if (storageTypeStr == "::mlir::DenseIntElementsAttr")
688 return "DenseIntElementsAttr";
689 if (storageTypeStr == "::mlir::DenseResourceElementsAttr")
690 return "DenseResourceElementsAttr";
691 if (storageTypeStr == "::mlir::DictionaryAttr")
692 return "DictAttr";
693 if (storageTypeStr == "::mlir::FlatSymbolRefAttr")
694 return "FlatSymbolRefAttr";
695 if (storageTypeStr == "::mlir::FloatAttr")
696 return "FloatAttr";
697 if (storageTypeStr == "::mlir::IntegerAttr") {
698 if (attr.getAttrDefName().str() == "I1Attr")
699 return "BoolAttr";
700 return "IntegerAttr";
701 }
702 if (storageTypeStr == "::mlir::IntegerSetAttr")
703 return "IntegerSetAttr";
704 if (storageTypeStr == "::mlir::OpaqueAttr")
705 return "OpaqueAttr";
706 if (storageTypeStr == "::mlir::StridedLayoutAttr")
707 return "StridedLayoutAttr";
708 if (storageTypeStr == "::mlir::StringAttr")
709 return "StringAttr";
710 if (storageTypeStr == "::mlir::SymbolRefAttr")
711 return "SymbolRefAttr";
712 if (storageTypeStr == "::mlir::TypeAttr")
713 return "TypeAttr";
714 if (storageTypeStr == "::mlir::UnitAttr")
715 return "UnitAttr";
716 return "Attribute";
717}
718
719/// Returns the Python value type accepted by the AttrBuilder for the given
720/// attribute. Returns empty StringRef if no mapping is known.
721static StringRef getPythonAttrType(mlir::tblgen::Attribute attr) {
722 auto it = pythonAttrTypeMap.find(Key: attr.getAttrDefName());
723 if (it != pythonAttrTypeMap.end())
724 return it->second;
725 return StringRef();
726}
727
728/// Emits accessors to Op attributes.
729static void emitAttributeAccessors(const Operator &op, raw_ostream &os) {
730 for (const auto &namedAttr : op.getAttributes()) {
731 // Skip "derived" attributes because they are just C++ functions that we
732 // don't currently expose.
733 if (namedAttr.attr.isDerivedAttr())
734 continue;
735
736 if (namedAttr.name.empty())
737 continue;
738
739 std::string sanitizedName = sanitizeName(name: namedAttr.name);
740
741 // Unit attributes are handled specially.
742 if (namedAttr.attr.getStorageType().trim() == "::mlir::UnitAttr") {
743 os << formatv(Fmt: unitAttributeGetterTemplate, Vals&: sanitizedName, Vals: namedAttr.name);
744 os << formatv(Fmt: unitAttributeSetterTemplate, Vals&: sanitizedName, Vals: namedAttr.name);
745 os << formatv(Fmt: attributeDeleterTemplate, Vals&: sanitizedName, Vals: namedAttr.name);
746 continue;
747 }
748
749 std::string type = "_ods_ir." + getPythonAttrName(attr: namedAttr.attr);
750 if (namedAttr.attr.isOptional()) {
751 os << formatv(Fmt: optionalAttributeGetterTemplate, Vals&: sanitizedName,
752 Vals: namedAttr.name, Vals&: type);
753 os << formatv(Fmt: optionalAttributeSetterTemplate, Vals&: sanitizedName,
754 Vals: namedAttr.name, Vals&: type);
755 os << formatv(Fmt: attributeDeleterTemplate, Vals&: sanitizedName, Vals: namedAttr.name);
756 } else {
757 os << formatv(Fmt: attributeGetterTemplate, Vals&: sanitizedName, Vals: namedAttr.name,
758 Vals&: type);
759 os << formatv(Fmt: attributeSetterTemplate, Vals&: sanitizedName, Vals: namedAttr.name,
760 Vals&: type);
761 // Non-optional attributes cannot be deleted.
762 }
763 }
764}
765
766/// Emits accessors to Op attributes for adaptors.
767static void emitAdaptorAttributeAccessors(const Operator &op, raw_ostream &os) {
768 for (const auto &namedAttr : op.getAttributes()) {
769 // Skip "derived" attributes because they are just C++ functions that we
770 // don't currently expose.
771 if (namedAttr.attr.isDerivedAttr())
772 continue;
773
774 if (namedAttr.name.empty())
775 continue;
776
777 std::string sanitizedName = sanitizeName(name: namedAttr.name);
778
779 // Unit attributes are handled specially.
780 if (namedAttr.attr.getStorageType().trim() == "::mlir::UnitAttr") {
781 os << formatv(Fmt: adaptorUnitAttributeGetterTemplate, Vals&: sanitizedName,
782 Vals: namedAttr.name);
783 continue;
784 }
785
786 std::string type = "_ods_ir." + getPythonAttrName(attr: namedAttr.attr);
787 os << formatv(Fmt: namedAttr.attr.isOptional()
788 ? adaptorOptionalAttributeGetterTemplate
789 : adaptorAttributeGetterTemplate,
790 Vals&: sanitizedName, Vals: namedAttr.name, Vals&: type);
791 }
792}
793
794/// Template for the default auto-generated builder.
795/// {0} is a comma-separated list of builder arguments, including the trailing
796/// `loc` and `ip`;
797/// {1} is the code populating `operands`, `results` and `attributes`,
798/// `successors` fields.
799constexpr const char *initTemplate = R"Py(
800 def __init__(self, {0}):
801 operands = []
802 attributes = {{}
803 regions = None
804 {1}
805 super().__init__({2})
806)Py";
807
808/// Template for appending a single element to the operand/result list.
809/// {0} is the field name.
810constexpr const char *singleOperandAppendTemplate = "operands.append({0})";
811constexpr const char *singleResultAppendTemplate = "results.append({0})";
812
813/// Template for appending an optional element to the operand/result list.
814/// {0} is the field name.
815constexpr const char *optionalAppendOperandTemplate =
816 "if {0} is not None: operands.append({0})";
817constexpr const char *optionalAppendAttrSizedOperandsTemplate =
818 "operands.append({0})";
819constexpr const char *optionalAppendResultTemplate =
820 "if {0} is not None: results.append({0})";
821
822/// Template for appending a list of elements to the operand/result list.
823/// {0} is the field name.
824constexpr const char *multiOperandAppendTemplate =
825 "operands.extend(_get_op_results_or_values({0}))";
826constexpr const char *multiOperandAppendPackTemplate =
827 "operands.append(_get_op_results_or_values({0}))";
828constexpr const char *multiResultAppendTemplate = "results.extend({0})";
829
830/// Template for attribute builder from raw input in the operation builder.
831/// {0} is the builder argument name;
832/// {1} is the attribute builder from raw;
833/// {2} is the attribute builder from raw.
834/// Use the value the user passed in if either it is already an Attribute or
835/// there is no method registered to make it an Attribute.
836constexpr const char *initAttributeWithBuilderTemplate =
837 R"Py(attributes["{1}"] = ({0} if (
838 isinstance({0}, _ods_ir.Attribute) or
839 not _ods_ir.AttrBuilder.contains('{2}')) else
840 _ods_ir.AttrBuilder.get('{2}')({0}, context=_ods_context)))Py";
841
842/// Template for attribute builder from raw input for optional attribute in the
843/// operation builder.
844/// {0} is the builder argument name;
845/// {1} is the attribute builder from raw;
846/// {2} is the attribute builder from raw.
847/// Use the value the user passed in if either it is already an Attribute or
848/// there is no method registered to make it an Attribute.
849constexpr const char *initOptionalAttributeWithBuilderTemplate =
850 R"Py(if {0} is not None: attributes["{1}"] = ({0} if (
851 isinstance({0}, _ods_ir.Attribute) or
852 not _ods_ir.AttrBuilder.contains('{2}')) else
853 _ods_ir.AttrBuilder.get('{2}')({0}, context=_ods_context)))Py";
854
855constexpr const char *initUnitAttributeTemplate =
856 R"Py(if bool({1}): attributes["{0}"] = _ods_ir.UnitAttr.get(
857 _ods_get_default_loc_context(loc)))Py";
858
859/// Template to initialize the successors list in the builder if there are any
860/// successors.
861/// {0} is the value to initialize the successors list to.
862constexpr const char *initSuccessorsTemplate = R"Py(_ods_successors = {0})Py";
863
864/// Template to append or extend the list of successors in the builder.
865/// {0} is the list method ('append' or 'extend');
866/// {1} is the value to add.
867constexpr const char *addSuccessorTemplate = R"Py(_ods_successors.{0}({1}))Py";
868
869/// Returns true if the SameArgumentAndResultTypes trait can be used to infer
870/// result types of the given operation.
871static bool hasSameArgumentAndResultTypes(const Operator &op) {
872 return op.getTrait(trait: "::mlir::OpTrait::SameOperandsAndResultType") &&
873 op.getNumVariableLengthResults() == 0;
874}
875
876/// Returns true if the FirstAttrDerivedResultType trait can be used to infer
877/// result types of the given operation.
878static bool hasFirstAttrDerivedResultTypes(const Operator &op) {
879 return op.getTrait(trait: "::mlir::OpTrait::FirstAttrDerivedResultType") &&
880 op.getNumVariableLengthResults() == 0;
881}
882
883/// Returns true if the InferTypeOpInterface can be used to infer result types
884/// of the given operation.
885static bool hasInferTypeInterface(const Operator &op) {
886 return op.getTrait(trait: "::mlir::InferTypeOpInterface::Trait") &&
887 op.getNumRegions() == 0;
888}
889
890/// Returns true if there is a trait or interface that can be used to infer
891/// result types of the given operation.
892static bool canInferType(const Operator &op) {
893 return hasSameArgumentAndResultTypes(op) ||
894 hasFirstAttrDerivedResultTypes(op) || hasInferTypeInterface(op);
895}
896
897/// Populates `builderArgs` with result names if the builder is expected to
898/// accept them as arguments.
899static void
900populateBuilderArgsResults(const Operator &op,
901 SmallVectorImpl<std::string> &builderArgs) {
902 if (canInferType(op))
903 return;
904
905 for (int i = 0, e = op.getNumResults(); i < e; ++i) {
906 std::string name = op.getResultName(index: i).str();
907 if (name.empty()) {
908 if (op.getNumResults() == 1) {
909 // Special case for one result, make the default name be 'result'
910 // to properly match the built-in result accessor.
911 name = "result";
912 } else {
913 name = formatv(Fmt: "_gen_res_{0}", Vals&: i);
914 }
915 }
916 name = sanitizeName(name);
917 builderArgs.push_back(Elt: name);
918 }
919}
920
921/// Populates `builderArgs` with the Python-compatible names of builder function
922/// arguments using intermixed attributes and operands in the same order as they
923/// appear in the `arguments` field of the op definition. Additionally,
924/// `operandNames` is populated with names of operands in their order of
925/// appearance.
926static void populateBuilderArgs(const Operator &op,
927 SmallVectorImpl<std::string> &builderArgs,
928 SmallVectorImpl<std::string> &operandNames) {
929 for (int i = 0, e = op.getNumArgs(); i < e; ++i) {
930 std::string name = op.getArgName(index: i).str();
931 if (name.empty())
932 name = formatv(Fmt: "_gen_arg_{0}", Vals&: i);
933 name = sanitizeName(name);
934 builderArgs.push_back(Elt: name);
935 if (!isa<NamedAttribute *>(Val: op.getArg(index: i)))
936 operandNames.push_back(Elt: name);
937 }
938}
939
940/// Populates `builderArgs` with the Python-compatible names of builder function
941/// successor arguments. Additionally, `successorArgNames` is also populated.
942static void
943populateBuilderArgsSuccessors(const Operator &op,
944 SmallVectorImpl<std::string> &builderArgs,
945 SmallVectorImpl<std::string> &successorArgNames) {
946
947 for (int i = 0, e = op.getNumSuccessors(); i < e; ++i) {
948 NamedSuccessor successor = op.getSuccessor(index: i);
949 std::string name = std::string(successor.name);
950 if (name.empty())
951 name = formatv(Fmt: "_gen_successor_{0}", Vals&: i);
952 name = sanitizeName(name);
953 builderArgs.push_back(Elt: name);
954 successorArgNames.push_back(Elt: name);
955 }
956}
957
958/// Populates `builderLines` with additional lines that are required in the
959/// builder to set up operation attributes. `argNames` is expected to contain
960/// the names of builder arguments that correspond to op arguments, i.e. to the
961/// operands and attributes in the same order as they appear in the `arguments`
962/// field.
963static void
964populateBuilderLinesAttr(const Operator &op, ArrayRef<std::string> argNames,
965 SmallVectorImpl<std::string> &builderLines) {
966 builderLines.push_back(Elt: "_ods_context = _ods_get_default_loc_context(loc)");
967 for (int i = 0, e = op.getNumArgs(); i < e; ++i) {
968 Argument arg = op.getArg(index: i);
969 auto *attribute = llvm::dyn_cast_if_present<NamedAttribute *>(Val&: arg);
970 if (!attribute)
971 continue;
972
973 // Unit attributes are handled specially.
974 if (attribute->attr.getStorageType().trim() == "::mlir::UnitAttr") {
975 builderLines.push_back(
976 Elt: formatv(Fmt: initUnitAttributeTemplate, Vals&: attribute->name, Vals: argNames[i]));
977 continue;
978 }
979
980 // For EnumAttr-style attributes (those defined as EnumAttr<Dialect, ...>
981 // in tablegen), use a dialect-qualified key ("dialect.AttrName") so the
982 // lookup matches the registration emitted by EnumPythonBindingGen with
983 // -bind-dialect. For all other attributes (plain attrs like I32Attr,
984 // custom AttrDef, etc.), keep the unqualified name to match their
985 // registrations in ir.py or dialect-specific Python files.
986 Attribute baseAttr = attribute->attr.getBaseAttr();
987 Dialect attrDialect = baseAttr.isSubClassOf(className: "EnumAttr")
988 ? baseAttr.getDialect()
989 : Dialect(nullptr);
990 std::string attrBuilderKey = attrDialect
991 ? formatv(Fmt: "{0}.{1}", Vals: attrDialect.getName(),
992 Vals: attribute->attr.getAttrDefName())
993 .str()
994 : attribute->attr.getAttrDefName().str();
995
996 builderLines.push_back(Elt: formatv(
997 Fmt: attribute->attr.isOptional() || attribute->attr.hasDefaultValue()
998 ? initOptionalAttributeWithBuilderTemplate
999 : initAttributeWithBuilderTemplate,
1000 Vals: argNames[i], Vals&: attribute->name, Vals&: attrBuilderKey));
1001 }
1002}
1003
1004/// Populates `builderLines` with additional lines that are required in the
1005/// builder to set up successors. successorArgNames is expected to correspond
1006/// to the Python argument name for each successor on the op.
1007static void
1008populateBuilderLinesSuccessors(const Operator &op,
1009 ArrayRef<std::string> successorArgNames,
1010 SmallVectorImpl<std::string> &builderLines) {
1011 if (successorArgNames.empty()) {
1012 builderLines.push_back(Elt: formatv(Fmt: initSuccessorsTemplate, Vals: "None"));
1013 return;
1014 }
1015
1016 builderLines.push_back(Elt: formatv(Fmt: initSuccessorsTemplate, Vals: "[]"));
1017 for (int i = 0, e = successorArgNames.size(); i < e; ++i) {
1018 auto &argName = successorArgNames[i];
1019 const NamedSuccessor &successor = op.getSuccessor(index: i);
1020 builderLines.push_back(Elt: formatv(Fmt: addSuccessorTemplate,
1021 Vals: successor.isVariadic() ? "extend" : "append",
1022 Vals: argName));
1023 }
1024}
1025
1026/// Populates `builderLines` with additional lines that are required in the
1027/// builder to set up op operands.
1028static void
1029populateBuilderLinesOperand(const Operator &op, ArrayRef<std::string> names,
1030 SmallVectorImpl<std::string> &builderLines) {
1031 bool sizedSegments = op.getTrait(trait: attrSizedTraitForKind(kind: "operand")) != nullptr;
1032
1033 // For each element, find or generate a name.
1034 for (int i = 0, e = op.getNumOperands(); i < e; ++i) {
1035 const NamedTypeConstraint &element = op.getOperand(index: i);
1036 std::string name = names[i];
1037
1038 // Choose the formatting string based on the element kind.
1039 StringRef formatString;
1040 if (!element.isVariableLength()) {
1041 formatString = singleOperandAppendTemplate;
1042 } else if (element.isOptional()) {
1043 if (sizedSegments) {
1044 formatString = optionalAppendAttrSizedOperandsTemplate;
1045 } else {
1046 formatString = optionalAppendOperandTemplate;
1047 }
1048 } else {
1049 assert(element.isVariadic() && "unhandled element group type");
1050 // If emitting with sizedSegments, then we add the actual list-typed
1051 // element. Otherwise, we extend the actual operands.
1052 if (sizedSegments) {
1053 formatString = multiOperandAppendPackTemplate;
1054 } else {
1055 formatString = multiOperandAppendTemplate;
1056 }
1057 }
1058
1059 builderLines.push_back(Elt: formatv(Fmt: formatString.data(), Vals&: name));
1060 }
1061}
1062
1063/// Python code template of generating result types for
1064/// FirstAttrDerivedResultType trait
1065/// - {0} is the name of the attribute from which to derive the types.
1066/// - {1} is the number of results.
1067constexpr const char *firstAttrDerivedResultTypeTemplate =
1068 R"Py(if results is None:
1069 _ods_result_type_source_attr = attributes["{0}"]
1070 _ods_derived_result_type = (
1071 _ods_ir.TypeAttr(_ods_result_type_source_attr).value
1072 if isinstance(_ods_result_type_source_attr, _ods_ir.TypeAttr) else
1073 _ods_result_type_source_attr.type)
1074 results = [_ods_derived_result_type] * {1})Py";
1075
1076/// Python code template of generating result types for
1077/// SameOperandsAndResultType trait
1078/// - {0} is the number of results.
1079constexpr const char *sameOperandsAndResultTypeTemplate =
1080 R"Py(if results is None: results = [operands[0].type] * {0})Py";
1081
1082/// Appends the given multiline string as individual strings into
1083/// `builderLines`.
1084static void appendLineByLine(StringRef string,
1085 SmallVectorImpl<std::string> &builderLines) {
1086
1087 std::pair<StringRef, StringRef> split = std::make_pair(x&: string, y&: string);
1088 do {
1089 split = split.second.split(Separator: '\n');
1090 builderLines.push_back(Elt: split.first.str());
1091 } while (!split.second.empty());
1092}
1093
1094/// Populates `builderLines` with additional lines that are required in the
1095/// builder to set up op results.
1096static void
1097populateBuilderLinesResult(const Operator &op, ArrayRef<std::string> names,
1098 SmallVectorImpl<std::string> &builderLines) {
1099 if (hasSameArgumentAndResultTypes(op)) {
1100 appendLineByLine(
1101 string: formatv(Fmt: sameOperandsAndResultTypeTemplate, Vals: op.getNumResults()).str(),
1102 builderLines);
1103 return;
1104 }
1105
1106 if (hasFirstAttrDerivedResultTypes(op)) {
1107 const NamedAttribute &firstAttr = op.getAttribute(index: 0);
1108 assert(!firstAttr.name.empty() && "unexpected empty name for the attribute "
1109 "from which the type is derived");
1110 appendLineByLine(string: formatv(Fmt: firstAttrDerivedResultTypeTemplate, Vals: firstAttr.name,
1111 Vals: op.getNumResults())
1112 .str(),
1113 builderLines);
1114 return;
1115 }
1116
1117 if (hasInferTypeInterface(op))
1118 return;
1119
1120 bool sizedSegments = op.getTrait(trait: attrSizedTraitForKind(kind: "result")) != nullptr;
1121 builderLines.push_back(Elt: "results = []");
1122
1123 // For each element, find or generate a name.
1124 for (int i = 0, e = op.getNumResults(); i < e; ++i) {
1125 const NamedTypeConstraint &element = op.getResult(index: i);
1126 std::string name = names[i];
1127
1128 // Choose the formatting string based on the element kind.
1129 StringRef formatString;
1130 if (!element.isVariableLength()) {
1131 formatString = singleResultAppendTemplate;
1132 } else if (element.isOptional()) {
1133 formatString = optionalAppendResultTemplate;
1134 } else {
1135 assert(element.isVariadic() && "unhandled element group type");
1136 // If emitting with sizedSegments, then we add the actual list-typed
1137 // element. Otherwise, we extend the actual operands.
1138 if (sizedSegments) {
1139 formatString = singleResultAppendTemplate;
1140 } else {
1141 formatString = multiResultAppendTemplate;
1142 }
1143 }
1144
1145 builderLines.push_back(Elt: formatv(Fmt: formatString.data(), Vals&: name));
1146 }
1147}
1148
1149/// If the operation has variadic regions, adds a builder argument to specify
1150/// the number of those regions and builder lines to forward it to the generic
1151/// constructor.
1152static void populateBuilderRegions(const Operator &op,
1153 SmallVectorImpl<std::string> &builderArgs,
1154 SmallVectorImpl<std::string> &builderLines) {
1155 if (op.hasNoVariadicRegions())
1156 return;
1157
1158 // This is currently enforced when Operator is constructed.
1159 assert(op.getNumVariadicRegions() == 1 &&
1160 op.getRegion(op.getNumRegions() - 1).isVariadic() &&
1161 "expected the last region to be varidic");
1162
1163 const NamedRegion &region = op.getRegion(index: op.getNumRegions() - 1);
1164 std::string name =
1165 ("num_" + region.name.take_front().lower() + region.name.drop_front())
1166 .str();
1167 builderArgs.push_back(Elt: name);
1168 builderLines.push_back(
1169 Elt: formatv(Fmt: "regions = {0} + {1}", Vals: op.getNumRegions() - 1, Vals&: name));
1170}
1171
1172/// Emits a default builder constructing an operation from the list of its
1173/// result types, followed by a list of its operands. Returns vector
1174/// of fully built functionArgs for downstream users (to save having to
1175/// rebuild anew).
1176static SmallVector<std::string> emitDefaultOpBuilder(const Operator &op,
1177 raw_ostream &os) {
1178 SmallVector<std::string> builderArgs;
1179 SmallVector<std::string> builderLines;
1180 SmallVector<std::string> operandArgNames;
1181 SmallVector<std::string> successorArgNames;
1182 builderArgs.reserve(N: op.getNumOperands() + op.getNumResults() +
1183 op.getNumNativeAttributes() + op.getNumSuccessors());
1184 populateBuilderArgsResults(op, builderArgs);
1185 size_t numResultArgs = builderArgs.size();
1186 populateBuilderArgs(op, builderArgs, operandNames&: operandArgNames);
1187 size_t numOperandAttrArgs = builderArgs.size() - numResultArgs;
1188 populateBuilderArgsSuccessors(op, builderArgs, successorArgNames);
1189 size_t numSuccessorArgs = successorArgNames.size();
1190
1191 populateBuilderLinesOperand(op, names: operandArgNames, builderLines);
1192 populateBuilderLinesAttr(op, argNames: ArrayRef(builderArgs).drop_front(N: numResultArgs),
1193 builderLines);
1194 populateBuilderLinesResult(
1195 op, names: ArrayRef(builderArgs).take_front(N: numResultArgs), builderLines);
1196 populateBuilderLinesSuccessors(op, successorArgNames, builderLines);
1197 populateBuilderRegions(op, builderArgs, builderLines);
1198
1199 // Compute type annotations for each builder arg.
1200 SmallVector<std::string> argTypes(builderArgs.size());
1201
1202 // Result args: user passes Type objects.
1203 for (size_t i = 0; i < numResultArgs; ++i) {
1204 const NamedTypeConstraint &result = op.getResult(index: i);
1205 if (result.isVariadic())
1206 argTypes[i] = "_Sequence[_ods_ir.Type]";
1207 else if (result.isOptional())
1208 argTypes[i] = "_Optional[_ods_ir.Type]";
1209 else
1210 argTypes[i] = "_ods_ir.Type";
1211 }
1212
1213 // Operand and attribute args.
1214 for (size_t i = 0; i < numOperandAttrArgs; ++i) {
1215 size_t idx = numResultArgs + i;
1216 Argument arg = op.getArg(index: i);
1217 if (auto *nattr = llvm::dyn_cast_if_present<NamedAttribute *>(Val&: arg)) {
1218 if (nattr->attr.getStorageType().trim() == "::mlir::UnitAttr") {
1219 argTypes[idx] = "bool";
1220 } else {
1221 std::string attrType = "_ods_ir." + getPythonAttrName(attr: nattr->attr);
1222 StringRef rawType = getPythonAttrType(attr: nattr->attr);
1223 argTypes[idx] =
1224 llvm::formatv(Fmt: "_Union[{0}, {1}]",
1225 Vals: rawType.empty() ? "_Any" : rawType, Vals&: attrType)
1226 .str();
1227 }
1228 } else if (auto *ntype =
1229 llvm::dyn_cast_if_present<NamedTypeConstraint *>(Val&: arg)) {
1230 std::string type = "_ods_ir.Value";
1231 if (StringRef pythonType = getPythonType(constraint: ntype->constraint);
1232 !pythonType.empty()) {
1233 type = llvm::formatv(Fmt: "{0}[{1}]", Vals&: type, Vals&: pythonType);
1234 }
1235 if (ntype->isVariadic())
1236 type = llvm::formatv(Fmt: "_Sequence[{0}]", Vals&: type);
1237 argTypes[idx] = type;
1238 }
1239 // NamedProperty args are skipped (no type hint).
1240 }
1241
1242 // Successor args.
1243 for (size_t i = 0; i < numSuccessorArgs; ++i) {
1244 size_t idx = numResultArgs + numOperandAttrArgs + i;
1245 const NamedSuccessor &successor = op.getSuccessor(index: i);
1246 argTypes[idx] =
1247 successor.isVariadic() ? "_Sequence[_ods_ir.Block]" : "_ods_ir.Block";
1248 }
1249
1250 // Region args (variadic region count).
1251 for (size_t i = numResultArgs + numOperandAttrArgs + numSuccessorArgs;
1252 i < builderArgs.size(); ++i) {
1253 argTypes[i] = "int";
1254 }
1255
1256 // Determine whether the argument corresponding to a given index into the
1257 // builderArgs vector is a python keyword argument or not.
1258 auto isKeywordArgFn = [&](size_t builderArgIndex) -> bool {
1259 // All result, successor, and region arguments are positional arguments.
1260 if (builderArgIndex < numResultArgs ||
1261 builderArgIndex >= numResultArgs + numOperandAttrArgs)
1262 return false;
1263 // Keyword arguments:
1264 // - optional named attributes (including unit attributes)
1265 // - default-valued named attributes
1266 // - optional operands
1267 Argument a = op.getArg(index: builderArgIndex - numResultArgs);
1268 if (auto *nattr = llvm::dyn_cast_if_present<NamedAttribute *>(Val&: a))
1269 return (nattr->attr.isOptional() || nattr->attr.hasDefaultValue());
1270 if (auto *ntype = llvm::dyn_cast_if_present<NamedTypeConstraint *>(Val&: a))
1271 return ntype->isOptional();
1272 return false;
1273 };
1274
1275 // Format a single function argument with optional type hint and default.
1276 auto formatArg = [](StringRef name, StringRef typeHint,
1277 bool isKeyword) -> std::string {
1278 std::string result = name.str();
1279 if (isKeyword && !typeHint.empty())
1280 result += ": _Optional[" + typeHint.str() + "] = None";
1281 else if (isKeyword)
1282 result += "=None";
1283 else if (!typeHint.empty())
1284 result += ": " + typeHint.str();
1285 return result;
1286 };
1287
1288 // Build the function argument list: positional args, *, keyword args.
1289 SmallVector<std::string> functionArgs;
1290 for (size_t i = 0, cnt = builderArgs.size(); i < cnt; ++i)
1291 if (!isKeywordArgFn(i))
1292 functionArgs.push_back(Elt: formatArg(builderArgs[i], argTypes[i], false));
1293
1294 // Add a bare '*' to indicate that all following arguments must be keyword
1295 // arguments.
1296 functionArgs.push_back(Elt: "*");
1297
1298 for (size_t i = 0, cnt = builderArgs.size(); i < cnt; ++i)
1299 if (isKeywordArgFn(i))
1300 functionArgs.push_back(Elt: formatArg(builderArgs[i], argTypes[i], true));
1301
1302 if (canInferType(op))
1303 functionArgs.push_back(
1304 Elt: "results: _Optional[_Sequence[_ods_ir.Type]] = None");
1305 functionArgs.push_back(Elt: "loc: _Optional[_ods_ir.Location] = None");
1306 functionArgs.push_back(Elt: "ip: _Optional[_ods_ir.InsertionPoint] = None");
1307
1308 SmallVector<std::string> initArgs;
1309 initArgs.push_back(Elt: "self.OPERATION_NAME");
1310 initArgs.push_back(Elt: "self._ODS_REGIONS");
1311 initArgs.push_back(Elt: "self._ODS_OPERAND_SEGMENTS");
1312 initArgs.push_back(Elt: "self._ODS_RESULT_SEGMENTS");
1313 initArgs.push_back(Elt: "attributes=attributes");
1314 initArgs.push_back(Elt: "results=results");
1315 initArgs.push_back(Elt: "operands=operands");
1316 initArgs.push_back(Elt: "successors=_ods_successors");
1317 initArgs.push_back(Elt: "regions=regions");
1318 initArgs.push_back(Elt: "loc=loc");
1319 initArgs.push_back(Elt: "ip=ip");
1320
1321 os << formatv(Fmt: initTemplate, Vals: llvm::join(R&: functionArgs, Separator: ", "),
1322 Vals: llvm::join(R&: builderLines, Separator: "\n "), Vals: llvm::join(R&: initArgs, Separator: ", "));
1323 return functionArgs;
1324}
1325
1326static void emitSegmentSpec(
1327 const Operator &op, const char *kind,
1328 llvm::function_ref<int(const Operator &)> getNumElements,
1329 llvm::function_ref<const NamedTypeConstraint &(const Operator &, int)>
1330 getElement,
1331 raw_ostream &os) {
1332 std::string segmentSpec("[");
1333 for (int i = 0, e = getNumElements(op); i < e; ++i) {
1334 const NamedTypeConstraint &element = getElement(op, i);
1335 if (element.isOptional()) {
1336 segmentSpec.append(s: "0,");
1337 } else if (element.isVariadic()) {
1338 segmentSpec.append(s: "-1,");
1339 } else {
1340 segmentSpec.append(s: "1,");
1341 }
1342 }
1343 segmentSpec.append(s: "]");
1344
1345 os << formatv(Fmt: opClassSizedSegmentsTemplate, Vals&: kind, Vals&: segmentSpec);
1346}
1347
1348static void emitRegionAttributes(const Operator &op, raw_ostream &os) {
1349 // Emit _ODS_REGIONS = (min_region_count, has_no_variadic_regions).
1350 // Note that the base OpView class defines this as (0, True).
1351 unsigned minRegionCount = op.getNumRegions() - op.getNumVariadicRegions();
1352 os << formatv(Fmt: opClassRegionSpecTemplate, Vals&: minRegionCount,
1353 Vals: op.hasNoVariadicRegions() ? "True" : "False");
1354}
1355
1356/// Emits named accessors to regions.
1357static void emitRegionAccessors(const Operator &op, raw_ostream &os) {
1358 for (const auto &en : llvm::enumerate(First: op.getRegions())) {
1359 const NamedRegion &region = en.value();
1360 if (region.name.empty())
1361 continue;
1362
1363 assert((!region.isVariadic() || en.index() == op.getNumRegions() - 1) &&
1364 "expected only the last region to be variadic");
1365 os << formatv(Fmt: regionAccessorTemplate, Vals: sanitizeName(name: region.name),
1366 Vals: std::to_string(val: en.index()) + (region.isVariadic() ? ":" : ""),
1367 Vals: region.isVariadic() ? "_ods_ir.RegionSequence"
1368 : "_ods_ir.Region");
1369 }
1370}
1371
1372/// Emits builder that extracts results from op
1373static void emitValueBuilder(const Operator &op,
1374 SmallVector<std::string> functionArgs,
1375 raw_ostream &os) {
1376 // Parse a formatted function arg "name[: type][ = default]" into
1377 // (name, type, defaultVal) with whitespace trimmed.
1378 auto parseFunctionArg =
1379 [](StringRef arg) -> std::tuple<StringRef, StringRef, StringRef> {
1380 auto [nameAndType, defaultVal] = arg.split(Separator: '=');
1381 auto [name, type] = nameAndType.split(Separator: ':');
1382 return {name.trim(), type.trim(), defaultVal.trim()};
1383 };
1384
1385 // Params with (possibly) default args.
1386 auto valueBuilderParams =
1387 llvm::map_range(C&: functionArgs, F: [&](const std::string &arg) {
1388 auto [name, type, defaultVal] = parseFunctionArg(arg);
1389 std::string result = llvm::convertToSnakeFromCamelCase(input: name);
1390 if (!type.empty())
1391 result += ": " + type.str();
1392 if (!defaultVal.empty())
1393 result += " = " + defaultVal.str();
1394 return result;
1395 });
1396 // Actual args passed to op builder (e.g., opParam=op_param).
1397 auto opBuilderArgs = llvm::map_range(
1398 C: llvm::make_filter_range(Range&: functionArgs,
1399 Pred: [](const std::string &s) { return s != "*"; }),
1400 F: [&](const std::string &arg) {
1401 auto [name, type, defaultVal] = parseFunctionArg(arg);
1402 return (name + "=" + llvm::convertToSnakeFromCamelCase(input: name)).str();
1403 });
1404 std::string nameWithoutDialect = sanitizeName(
1405 name: op.getOperationName().substr(pos: op.getOperationName().find(c: '.') + 1));
1406 if (nameWithoutDialect == op.getCppClassName())
1407 nameWithoutDialect += "_";
1408 std::string params = llvm::join(R&: valueBuilderParams, Separator: ", ");
1409 std::string args = llvm::join(R&: opBuilderArgs, Separator: ", ");
1410 if (op.getNumVariableLengthResults()) {
1411 os << formatv(Fmt: valueBuilderVariadicTemplate, Vals&: nameWithoutDialect,
1412 Vals: op.getCppClassName(), Vals&: params, Vals&: args);
1413 } else {
1414 std::string type = op.getCppClassName().str();
1415 const char *results = "";
1416 if (op.getNumResults() > 1) {
1417 type = "_ods_ir.OpResultList";
1418 results = ".results";
1419 } else if (op.getNumResults() == 1) {
1420 type = "_ods_ir.OpResult";
1421 if (StringRef pythonType = getPythonType(constraint: op.getResult(index: 0).constraint);
1422 !pythonType.empty())
1423 type = llvm::formatv(Fmt: "{0}[{1}]", Vals&: type, Vals&: pythonType);
1424 results = ".result";
1425 }
1426 os << formatv(Fmt: valueBuilderTemplate, Vals&: nameWithoutDialect,
1427 Vals: op.getCppClassName(), Vals&: params, Vals&: args, Vals&: type, Vals&: results);
1428 }
1429}
1430
1431/// Retrieve the description of the given op and generate a docstring for it.
1432static std::string makeDocStringForOp(const Operator &op) {
1433 if (!op.hasDescription())
1434 return "";
1435
1436 auto desc = op.getDescription().rtrim(Chars: " \t").str();
1437 // Replace all """ with \"\"\" to avoid early termination of the literal.
1438 desc = std::regex_replace(s: desc, e: std::regex(R"(""")"), fmt: R"(\"\"\")");
1439
1440 std::string docString = "\n";
1441 llvm::raw_string_ostream os(docString);
1442 raw_indented_ostream identedOs(os);
1443 os << R"( r""")" << "\n";
1444 identedOs.printReindented(str: desc, extraPrefix: " ");
1445 if (!StringRef(desc).ends_with(Suffix: "\n"))
1446 os << "\n";
1447 os << R"( """)" << "\n";
1448
1449 return docString;
1450}
1451
1452static void emitAdaptorOperandAccessors(const Operator &op, raw_ostream &os) {
1453 emitElementAccessors(op, os, kind: "operand", numVariadicGroups: op.getNumVariableLengthOperands(),
1454 numElements: getNumOperands(op), getElement: getOperand, /*isAdaptor=*/true);
1455}
1456
1457/// Emits bindings for a specific Op to the given output stream.
1458static void emitOpBindings(const Operator &op, raw_ostream &os) {
1459 os << formatv(Fmt: opClassTemplate, Vals: op.getCppClassName(), Vals: op.getOperationName(),
1460 Vals: makeDocStringForOp(op));
1461
1462 // Sized segments.
1463 if (op.getTrait(trait: attrSizedTraitForKind(kind: "operand")) != nullptr) {
1464 emitSegmentSpec(op, kind: "OPERAND", getNumElements: getNumOperands, getElement: getOperand, os);
1465 }
1466 if (op.getTrait(trait: attrSizedTraitForKind(kind: "result")) != nullptr) {
1467 emitSegmentSpec(op, kind: "RESULT", getNumElements: getNumResults, getElement: getResult, os);
1468 }
1469
1470 emitRegionAttributes(op, os);
1471 SmallVector<std::string> functionArgs = emitDefaultOpBuilder(op, os);
1472 emitOperandAccessors(op, os);
1473 emitAttributeAccessors(op, os);
1474 emitResultAccessors(op, os);
1475 emitRegionAccessors(op, os);
1476
1477 os << formatv(Fmt: opAdaptorClassTemplate, Vals: op.getCppClassName(),
1478 Vals: op.getOperationName());
1479 emitAdaptorOperandAccessors(op, os);
1480 emitAdaptorAttributeAccessors(op, os);
1481
1482 emitValueBuilder(op, functionArgs, os);
1483}
1484
1485static void populateTypeMap(llvm::StringMap<std::string> &map,
1486 ArrayRef<std::pair<StringRef, StringRef>> builtins,
1487 const RecordKeeper &records, StringRef recordClass,
1488 StringRef keyField, StringRef valueField) {
1489 map.clear();
1490 for (auto [key, value] : builtins)
1491 map[key] = value.str();
1492 for (const Record *rec :
1493 records.getAllDerivedDefinitionsIfDefined(ClassName: recordClass)) {
1494 StringRef key = rec->getValueAsString(FieldName: keyField);
1495 std::string value = rec->getValueAsString(FieldName: valueField).str();
1496 auto [it, inserted] = map.try_emplace(Key: key, Args: std::move(value));
1497 if (!inserted && it->second != value)
1498 llvm::PrintFatalError(ErrorLoc: rec->getLoc(),
1499 Msg: "conflicting " + recordClass + " for '" + key +
1500 "': '" + it->second + "' vs '" + value + "'");
1501 }
1502}
1503
1504/// Emits bindings for the dialect specified in the command line, including file
1505/// headers and utilities. Returns `false` on success to comply with Tablegen
1506/// registration requirements.
1507static bool emitAllOps(const RecordKeeper &records, raw_ostream &os) {
1508 if (dialectNameStorage.empty())
1509 llvm::PrintFatalError(Msg: "dialect name not provided");
1510
1511 populateTypeMap(map&: pythonTypeMap, builtins: builtinTypeMappings, records, recordClass: "PythonTypeName",
1512 keyField: "cppName", valueField: "pyName");
1513 populateTypeMap(map&: pythonAttrTypeMap, builtins: builtinAttrTypeMappings, records,
1514 recordClass: "PythonAttrType", keyField: "defName", valueField: "pyType");
1515
1516 os << fileHeader;
1517 if (!clDialectExtensionName.empty())
1518 os << formatv(Fmt: dialectExtensionTemplate, Vals&: dialectNameStorage);
1519 else
1520 os << formatv(Fmt: dialectClassTemplate, Vals&: dialectNameStorage);
1521
1522 for (const Record *rec : records.getAllDerivedDefinitions(ClassName: "Op")) {
1523 Operator op(rec);
1524 if (op.getDialectName() == dialectNameStorage)
1525 emitOpBindings(op, os);
1526 }
1527 return false;
1528}
1529
1530static GenRegistration
1531 genPythonBindings("gen-python-op-bindings",
1532 "Generate Python bindings for MLIR Ops", &emitAllOps);
1533