[llvm-branch-commits] [mlir] 8a1f1a1 - [mlir][python] Aggressively avoid name collisions in generated python ODS code.
Stella Laurenzo via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Dec 29 17:51:25 PST 2020
Author: Stella Laurenzo
Date: 2020-12-29T17:43:04-08:00
New Revision: 8a1f1a100cce6ed9b52aedbbf03da887833508be
URL: https://github.com/llvm/llvm-project/commit/8a1f1a100cce6ed9b52aedbbf03da887833508be
DIFF: https://github.com/llvm/llvm-project/commit/8a1f1a100cce6ed9b52aedbbf03da887833508be.diff
LOG: [mlir][python] Aggressively avoid name collisions in generated python ODS code.
* When porting npcomp to use these bindings, I ran into enough patterns of collisions that I decided to be somewhat draconian about not polluting the namespace.
* With these changes all of the npcomp dialects generate and pass what tests we have.
Reviewed By: mehdi_amini
Differential Revision: https://reviews.llvm.org/D93920
Added:
Modified:
mlir/include/mlir/Bindings/Python/Attributes.td
mlir/test/mlir-tblgen/op-python-bindings.td
mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
Removed:
################################################################################
diff --git a/mlir/include/mlir/Bindings/Python/Attributes.td b/mlir/include/mlir/Bindings/Python/Attributes.td
index 4fe746d2a737..f9a7fa703f50 100644
--- a/mlir/include/mlir/Bindings/Python/Attributes.td
+++ b/mlir/include/mlir/Bindings/Python/Attributes.td
@@ -22,13 +22,13 @@ class PythonAttr<string c, string p> {
}
// Mappings between supported builtin attribtues and Python types.
-def : PythonAttr<"::mlir::Attribute", "_ir.Attribute">;
-def : PythonAttr<"::mlir::BoolAttr", "_ir.BoolAttr">;
-def : PythonAttr<"::mlir::IntegerAttr", "_ir.IntegerAttr">;
-def : PythonAttr<"::mlir::FloatAttr", "_ir.FloatAttr">;
-def : PythonAttr<"::mlir::StringAttr", "_ir.StringAttr">;
-def : PythonAttr<"::mlir::DenseElementsAttr", "_ir.DenseElementsAttr">;
-def : PythonAttr<"::mlir::DenseIntElementsAttr", "_ir.DenseIntElementsAttr">;
-def : PythonAttr<"::mlir::DenseFPElementsAttr", "_ir.DenseFPElementsAttr">;
+def : PythonAttr<"::mlir::Attribute", "_ods_ir.Attribute">;
+def : PythonAttr<"::mlir::BoolAttr", "_ods_ir.BoolAttr">;
+def : PythonAttr<"::mlir::IntegerAttr", "_ods_ir.IntegerAttr">;
+def : PythonAttr<"::mlir::FloatAttr", "_ods_ir.FloatAttr">;
+def : PythonAttr<"::mlir::StringAttr", "_ods_ir.StringAttr">;
+def : PythonAttr<"::mlir::DenseElementsAttr", "_ods_ir.DenseElementsAttr">;
+def : PythonAttr<"::mlir::DenseIntElementsAttr", "_ods_ir.DenseIntElementsAttr">;
+def : PythonAttr<"::mlir::DenseFPElementsAttr", "_ods_ir.DenseFPElementsAttr">;
#endif
diff --git a/mlir/test/mlir-tblgen/op-python-bindings.td b/mlir/test/mlir-tblgen/op-python-bindings.td
index ef8b20fa2ccc..722cf9fb7e40 100644
--- a/mlir/test/mlir-tblgen/op-python-bindings.td
+++ b/mlir/test/mlir-tblgen/op-python-bindings.td
@@ -3,8 +3,8 @@
include "mlir/IR/OpBase.td"
include "mlir/Bindings/Python/Attributes.td"
-// CHECK: @_cext.register_dialect
-// CHECK: class _Dialect(_ir.Dialect):
+// CHECK: @_ods_cext.register_dialect
+// CHECK: class _Dialect(_ods_ir.Dialect):
// CHECK: DIALECT_NAMESPACE = "test"
// CHECK: pass
def Test_Dialect : Dialect {
@@ -14,8 +14,8 @@ def Test_Dialect : Dialect {
class TestOp<string mnemonic, list<OpTrait> traits = []> :
Op<Test_Dialect, mnemonic, traits>;
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class AttrSizedOperandsOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class AttrSizedOperandsOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.attr_sized_operands"
def AttrSizedOperandsOp : TestOp<"attr_sized_operands",
[AttrSizedOperandSegments]> {
@@ -23,36 +23,36 @@ def AttrSizedOperandsOp : TestOp<"attr_sized_operands",
// CHECK: operands = []
// CHECK: results = []
// CHECK: attributes = {}
- // CHECK: operand_segment_sizes = array.array('L')
+ // CHECK: operand_segment_sizes_ods = _ods_array.array('L')
// CHECK: operands += [*variadic1]
- // CHECK: operand_segment_sizes.append(len(variadic1))
+ // CHECK: operand_segment_sizes_ods.append(len(variadic1))
// CHECK: operands.append(non_variadic)
- // CHECK: operand_segment_sizes.append(1)
+ // CHECK: operand_segment_sizes_ods.append(1)
// CHECK: if variadic2 is not None: operands.append(variadic2)
- // CHECK: operand_segment_sizes.append(0 if variadic2 is None else 1)
- // CHECK: attributes["operand_segment_sizes"] = _ir.DenseElementsAttr.get(operand_segment_sizes,
- // CHECK: context=_get_default_loc_context(loc))
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: operand_segment_sizes_ods.append(0 if variadic2 is None else 1)
+ // CHECK: attributes["operand_segment_sizes"] = _ods_ir.DenseElementsAttr.get(operand_segment_sizes_ods,
+ // CHECK: context=_ods_get_default_loc_context(loc))
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.attr_sized_operands", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
// CHECK: @property
// CHECK: def variadic1(self):
- // CHECK: operand_range = _segmented_accessor(
+ // CHECK: operand_range = _ods_segmented_accessor(
// CHECK: self.operation.operands,
// CHECK: self.operation.attributes["operand_segment_sizes"], 0)
// CHECK: return operand_range
//
// CHECK: @property
// CHECK: def non_variadic(self):
- // CHECK: operand_range = _segmented_accessor(
+ // CHECK: operand_range = _ods_segmented_accessor(
// CHECK: self.operation.operands,
// CHECK: self.operation.attributes["operand_segment_sizes"], 1)
// CHECK: return operand_range[0]
//
// CHECK: @property
// CHECK: def variadic2(self):
- // CHECK: operand_range = _segmented_accessor(
+ // CHECK: operand_range = _ods_segmented_accessor(
// CHECK: self.operation.operands,
// CHECK: self.operation.attributes["operand_segment_sizes"], 2)
// CHECK: return operand_range[0] if len(operand_range) > 0 else None
@@ -60,8 +60,8 @@ def AttrSizedOperandsOp : TestOp<"attr_sized_operands",
Optional<AnyType>:$variadic2);
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class AttrSizedResultsOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class AttrSizedResultsOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.attr_sized_results"
def AttrSizedResultsOp : TestOp<"attr_sized_results",
[AttrSizedResultSegments]> {
@@ -69,36 +69,36 @@ def AttrSizedResultsOp : TestOp<"attr_sized_results",
// CHECK: operands = []
// CHECK: results = []
// CHECK: attributes = {}
- // CHECK: result_segment_sizes = array.array('L')
+ // CHECK: result_segment_sizes_ods = _ods_array.array('L')
// CHECK: if variadic1 is not None: results.append(variadic1)
- // CHECK: result_segment_sizes.append(0 if variadic1 is None else 1)
+ // CHECK: result_segment_sizes_ods.append(0 if variadic1 is None else 1)
// CHECK: results.append(non_variadic)
- // CHECK: result_segment_sizes.append(1) # non_variadic
+ // CHECK: result_segment_sizes_ods.append(1) # non_variadic
// CHECK: if variadic2 is not None: results.append(variadic2)
- // CHECK: result_segment_sizes.append(0 if variadic2 is None else 1)
- // CHECK: attributes["result_segment_sizes"] = _ir.DenseElementsAttr.get(result_segment_sizes,
- // CHECK: context=_get_default_loc_context(loc))
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: result_segment_sizes_ods.append(0 if variadic2 is None else 1)
+ // CHECK: attributes["result_segment_sizes"] = _ods_ir.DenseElementsAttr.get(result_segment_sizes_ods,
+ // CHECK: context=_ods_get_default_loc_context(loc))
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.attr_sized_results", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
// CHECK: @property
// CHECK: def variadic1(self):
- // CHECK: result_range = _segmented_accessor(
+ // CHECK: result_range = _ods_segmented_accessor(
// CHECK: self.operation.results,
// CHECK: self.operation.attributes["result_segment_sizes"], 0)
// CHECK: return result_range[0] if len(result_range) > 0 else None
//
// CHECK: @property
// CHECK: def non_variadic(self):
- // CHECK: result_range = _segmented_accessor(
+ // CHECK: result_range = _ods_segmented_accessor(
// CHECK: self.operation.results,
// CHECK: self.operation.attributes["result_segment_sizes"], 1)
// CHECK: return result_range[0]
//
// CHECK: @property
// CHECK: def variadic2(self):
- // CHECK: result_range = _segmented_accessor(
+ // CHECK: result_range = _ods_segmented_accessor(
// CHECK: self.operation.results,
// CHECK: self.operation.attributes["result_segment_sizes"], 2)
// CHECK: return result_range
@@ -107,8 +107,8 @@ def AttrSizedResultsOp : TestOp<"attr_sized_results",
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class AttributedOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class AttributedOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.attributed_op"
def AttributedOp : TestOp<"attributed_op"> {
// CHECK: def __init__(self, i32attr, optionalF32Attr, unitAttr, in_, loc=None, ip=None):
@@ -117,22 +117,22 @@ def AttributedOp : TestOp<"attributed_op"> {
// CHECK: attributes = {}
// CHECK: attributes["i32attr"] = i32attr
// CHECK: if optionalF32Attr is not None: attributes["optionalF32Attr"] = optionalF32Attr
- // CHECK: if bool(unitAttr): attributes["unitAttr"] = _ir.UnitAttr.get(
- // CHECK: _get_default_loc_context(loc))
+ // CHECK: if bool(unitAttr): attributes["unitAttr"] = _ods_ir.UnitAttr.get(
+ // CHECK: _ods_get_default_loc_context(loc))
// CHECK: attributes["in"] = in_
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.attributed_op", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
// CHECK: @property
// CHECK: def i32attr(self):
- // CHECK: return _ir.IntegerAttr(self.operation.attributes["i32attr"])
+ // CHECK: return _ods_ir.IntegerAttr(self.operation.attributes["i32attr"])
// CHECK: @property
// CHECK: def optionalF32Attr(self):
// CHECK: if "optionalF32Attr" not in self.operation.attributes:
// CHECK: return None
- // CHECK: return _ir.FloatAttr(self.operation.attributes["optionalF32Attr"])
+ // CHECK: return _ods_ir.FloatAttr(self.operation.attributes["optionalF32Attr"])
// CHECK: @property
// CHECK: def unitAttr(self):
@@ -140,13 +140,13 @@ def AttributedOp : TestOp<"attributed_op"> {
// CHECK: @property
// CHECK: def in_(self):
- // CHECK: return _ir.IntegerAttr(self.operation.attributes["in"])
+ // CHECK: return _ods_ir.IntegerAttr(self.operation.attributes["in"])
let arguments = (ins I32Attr:$i32attr, OptionalAttr<F32Attr>:$optionalF32Attr,
UnitAttr:$unitAttr, I32Attr:$in);
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class AttributedOpWithOperands(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class AttributedOpWithOperands(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.attributed_op_with_operands"
def AttributedOpWithOperands : TestOp<"attributed_op_with_operands"> {
// CHECK: def __init__(self, _gen_arg_0, in_, _gen_arg_2, is_, loc=None, ip=None):
@@ -155,10 +155,10 @@ def AttributedOpWithOperands : TestOp<"attributed_op_with_operands"> {
// CHECK: attributes = {}
// CHECK: operands.append(_gen_arg_0)
// CHECK: operands.append(_gen_arg_2)
- // CHECK: if bool(in_): attributes["in"] = _ir.UnitAttr.get(
- // CHECK: _get_default_loc_context(loc))
+ // CHECK: if bool(in_): attributes["in"] = _ods_ir.UnitAttr.get(
+ // CHECK: _ods_get_default_loc_context(loc))
// CHECK: if is_ is not None: attributes["is"] = is_
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.attributed_op_with_operands", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
@@ -170,25 +170,25 @@ def AttributedOpWithOperands : TestOp<"attributed_op_with_operands"> {
// CHECK: def is_(self):
// CHECK: if "is" not in self.operation.attributes:
// CHECK: return None
- // CHECK: return _ir.FloatAttr(self.operation.attributes["is"])
+ // CHECK: return _ods_ir.FloatAttr(self.operation.attributes["is"])
let arguments = (ins I32, UnitAttr:$in, F32, OptionalAttr<F32Attr>:$is);
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class EmptyOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class EmptyOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.empty"
def EmptyOp : TestOp<"empty">;
// CHECK: def __init__(self, loc=None, ip=None):
// CHECK: operands = []
// CHECK: results = []
// CHECK: attributes = {}
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.empty", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class MissingNamesOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class MissingNamesOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.missing_names"
def MissingNamesOp : TestOp<"missing_names"> {
// CHECK: def __init__(self, i32, _gen_res_1, i64, _gen_arg_0, f32, _gen_arg_2, loc=None, ip=None):
@@ -201,7 +201,7 @@ def MissingNamesOp : TestOp<"missing_names"> {
// CHECK: operands.append(_gen_arg_0)
// CHECK: operands.append(f32)
// CHECK: operands.append(_gen_arg_2)
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.missing_names", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
@@ -220,8 +220,8 @@ def MissingNamesOp : TestOp<"missing_names"> {
let results = (outs I32:$i32, F32, I64:$i64);
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class OneVariadicOperandOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class OneVariadicOperandOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.one_variadic_operand"
def OneVariadicOperandOp : TestOp<"one_variadic_operand"> {
// CHECK: def __init__(self, non_variadic, variadic, loc=None, ip=None):
@@ -230,7 +230,7 @@ def OneVariadicOperandOp : TestOp<"one_variadic_operand"> {
// CHECK: attributes = {}
// CHECK: operands.append(non_variadic)
// CHECK: operands += [*variadic]
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.one_variadic_operand", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
@@ -240,13 +240,13 @@ def OneVariadicOperandOp : TestOp<"one_variadic_operand"> {
//
// CHECK: @property
// CHECK: def variadic(self):
- // CHECK: variadic_group_length = len(self.operation.operands) - 2 + 1
- // CHECK: return self.operation.operands[1:1 + variadic_group_length]
+ // CHECK: _ods_variadic_group_length = len(self.operation.operands) - 2 + 1
+ // CHECK: return self.operation.operands[1:1 + _ods_variadic_group_length]
let arguments = (ins AnyType:$non_variadic, Variadic<AnyType>:$variadic);
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class OneVariadicResultOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class OneVariadicResultOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.one_variadic_result"
def OneVariadicResultOp : TestOp<"one_variadic_result"> {
// CHECK: def __init__(self, variadic, non_variadic, loc=None, ip=None):
@@ -255,24 +255,24 @@ def OneVariadicResultOp : TestOp<"one_variadic_result"> {
// CHECK: attributes = {}
// CHECK: results += [*variadic]
// CHECK: results.append(non_variadic)
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.one_variadic_result", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
// CHECK: @property
// CHECK: def variadic(self):
- // CHECK: variadic_group_length = len(self.operation.results) - 2 + 1
- // CHECK: return self.operation.results[0:0 + variadic_group_length]
+ // CHECK: _ods_variadic_group_length = len(self.operation.results) - 2 + 1
+ // CHECK: return self.operation.results[0:0 + _ods_variadic_group_length]
//
// CHECK: @property
// CHECK: def non_variadic(self):
- // CHECK: variadic_group_length = len(self.operation.results) - 2 + 1
- // CHECK: return self.operation.results[1 + variadic_group_length - 1]
+ // CHECK: _ods_variadic_group_length = len(self.operation.results) - 2 + 1
+ // CHECK: return self.operation.results[1 + _ods_variadic_group_length - 1]
let results = (outs Variadic<AnyType>:$variadic, AnyType:$non_variadic);
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class PythonKeywordOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class PythonKeywordOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.python_keyword"
def PythonKeywordOp : TestOp<"python_keyword"> {
// CHECK: def __init__(self, in_, loc=None, ip=None):
@@ -280,7 +280,7 @@ def PythonKeywordOp : TestOp<"python_keyword"> {
// CHECK: results = []
// CHECK: attributes = {}
// CHECK: operands.append(in_)
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.python_keyword", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
@@ -290,54 +290,54 @@ def PythonKeywordOp : TestOp<"python_keyword"> {
let arguments = (ins AnyType:$in);
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class SameVariadicOperandSizeOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class SameVariadicOperandSizeOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.same_variadic_operand"
def SameVariadicOperandSizeOp : TestOp<"same_variadic_operand",
[SameVariadicOperandSize]> {
// CHECK: @property
// CHECK: def variadic1(self):
- // CHECK: start, pg = _equally_sized_accessor(operation.operands, 2, 0, 0)
+ // CHECK: start, pg = _ods_equally_sized_accessor(operation.operands, 2, 0, 0)
// CHECK: return self.operation.operands[start:start + pg]
//
// CHECK: @property
// CHECK: def non_variadic(self):
- // CHECK: start, pg = _equally_sized_accessor(operation.operands, 2, 0, 1)
+ // CHECK: start, pg = _ods_equally_sized_accessor(operation.operands, 2, 0, 1)
// CHECK: return self.operation.operands[start]
//
// CHECK: @property
// CHECK: def variadic2(self):
- // CHECK: start, pg = _equally_sized_accessor(operation.operands, 2, 1, 1)
+ // CHECK: start, pg = _ods_equally_sized_accessor(operation.operands, 2, 1, 1)
// CHECK: return self.operation.operands[start:start + pg]
let arguments = (ins Variadic<AnyType>:$variadic1, AnyType:$non_variadic,
Variadic<AnyType>:$variadic2);
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class SameVariadicResultSizeOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class SameVariadicResultSizeOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.same_variadic_result"
def SameVariadicResultSizeOp : TestOp<"same_variadic_result",
[SameVariadicResultSize]> {
// CHECK: @property
// CHECK: def variadic1(self):
- // CHECK: start, pg = _equally_sized_accessor(operation.results, 2, 0, 0)
+ // CHECK: start, pg = _ods_equally_sized_accessor(operation.results, 2, 0, 0)
// CHECK: return self.operation.results[start:start + pg]
//
// CHECK: @property
// CHECK: def non_variadic(self):
- // CHECK: start, pg = _equally_sized_accessor(operation.results, 2, 0, 1)
+ // CHECK: start, pg = _ods_equally_sized_accessor(operation.results, 2, 0, 1)
// CHECK: return self.operation.results[start]
//
// CHECK: @property
// CHECK: def variadic2(self):
- // CHECK: start, pg = _equally_sized_accessor(operation.results, 2, 1, 1)
+ // CHECK: start, pg = _ods_equally_sized_accessor(operation.results, 2, 1, 1)
// CHECK: return self.operation.results[start:start + pg]
let results = (outs Variadic<AnyType>:$variadic1, AnyType:$non_variadic,
Variadic<AnyType>:$variadic2);
}
-// CHECK: @_cext.register_operation(_Dialect)
-// CHECK: class SimpleOp(_ir.OpView):
+// CHECK: @_ods_cext.register_operation(_Dialect)
+// CHECK: class SimpleOp(_ods_ir.OpView):
// CHECK-LABEL: OPERATION_NAME = "test.simple"
def SimpleOp : TestOp<"simple"> {
// CHECK: def __init__(self, i64, f64, i32, f32, loc=None, ip=None):
@@ -348,7 +348,7 @@ def SimpleOp : TestOp<"simple"> {
// CHECK: results.append(f64)
// CHECK: operands.append(i32)
// CHECK: operands.append(f32)
- // CHECK: super().__init__(_ir.Operation.create(
+ // CHECK: super().__init__(_ods_ir.Operation.create(
// CHECK: "test.simple", attributes=attributes, operands=operands, results=results,
// CHECK: loc=loc, ip=ip))
diff --git a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
index ddc1d928a970..64afc747351a 100644
--- a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
@@ -26,17 +26,17 @@ using namespace mlir::tblgen;
constexpr const char *fileHeader = R"Py(
# Autogenerated by mlir-tblgen; don't manually edit.
-import array
-from . import _cext
-from . import _segmented_accessor, _equally_sized_accessor, _get_default_loc_context
-_ir = _cext.ir
+import array as _ods_array
+from . import _cext as _ods_cext
+from . import _segmented_accessor as _ods_segmented_accessor, _equally_sized_accessor as _ods_equally_sized_accessor, _get_default_loc_context as _ods_get_default_loc_context
+_ods_ir = _ods_cext.ir
)Py";
/// Template for dialect class:
/// {0} is the dialect namespace.
constexpr const char *dialectClassTemplate = R"Py(
- at _cext.register_dialect
-class _Dialect(_ir.Dialect):
+ at _ods_cext.register_dialect
+class _Dialect(_ods_ir.Dialect):
DIALECT_NAMESPACE = "{0}"
pass
@@ -46,8 +46,8 @@ class _Dialect(_ir.Dialect):
/// {0} is the Python class name;
/// {1} is the operation name.
constexpr const char *opClassTemplate = R"Py(
- at _cext.register_operation(_Dialect)
-class {0}(_ir.OpView):
+ at _ods_cext.register_operation(_Dialect)
+class {0}(_ods_ir.OpView):
OPERATION_NAME = "{1}"
)Py";
@@ -71,8 +71,8 @@ constexpr const char *opSingleTemplate = R"Py(
constexpr const char *opSingleAfterVariableTemplate = R"Py(
@property
def {0}(self):
- variadic_group_length = len(self.operation.{1}s) - {2} + 1
- return self.operation.{1}s[{3} + variadic_group_length - 1]
+ _ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1
+ return self.operation.{1}s[{3} + _ods_variadic_group_length - 1]
)Py";
/// Template for an optional element accessor:
@@ -95,8 +95,8 @@ constexpr const char *opOneOptionalTemplate = R"Py(
constexpr const char *opOneVariadicTemplate = R"Py(
@property
def {0}(self):
- variadic_group_length = len(self.operation.{1}s) - {2} + 1
- return self.operation.{1}s[{3}:{3} + variadic_group_length]
+ _ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1
+ return self.operation.{1}s[{3}:{3} + _ods_variadic_group_length]
)Py";
/// First part of the template for equally-sized variadic group accessor:
@@ -108,7 +108,7 @@ constexpr const char *opOneVariadicTemplate = R"Py(
constexpr const char *opVariadicEqualPrefixTemplate = R"Py(
@property
def {0}(self):
- start, pg = _equally_sized_accessor(operation.{1}s, {2}, {3}, {4}))Py";
+ start, pg = _ods_equally_sized_accessor(operation.{1}s, {2}, {3}, {4}))Py";
/// Second part of the template for equally-sized case, accessing a single
/// element:
@@ -133,7 +133,7 @@ constexpr const char *opVariadicEqualVariadicTemplate = R"Py(
constexpr const char *opVariadicSegmentTemplate = R"Py(
@property
def {0}(self):
- {1}_range = _segmented_accessor(
+ {1}_range = _ods_segmented_accessor(
self.operation.{1}s,
self.operation.attributes["{1}_segment_sizes"], {2})
return {1}_range{3}
@@ -210,7 +210,7 @@ constexpr const char *unitAttributeSetterTemplate = R"Py(
@{0}.setter
def {0}(self, value):
if bool(value):
- self.operation.attributes["{1}"] = _ir.UnitAttr.get()
+ self.operation.attributes["{1}"] = _ods_ir.UnitAttr.get()
elif "{1}" in self.operation.attributes:
del self.operation.attributes["{1}"]
)Py";
@@ -246,11 +246,22 @@ static bool isPythonKeyword(StringRef str) {
return keywords.contains(str);
};
+/// Checks whether `str` would shadow a generated variable or attribute
+/// part of the OpView API.
+static bool isODSReserved(StringRef str) {
+ static llvm::StringSet<> reserved(
+ {"attributes", "create", "context", "ip", "operands", "print", "get_asm",
+ "loc", "verify", "regions", "result", "results", "self", "operation",
+ "DIALECT_NAMESPACE", "OPERATION_NAME"});
+ return str.startswith("_ods_") || str.endswith("_ods") ||
+ reserved.contains(str);
+}
+
/// Modifies the `name` in a way that it becomes suitable for Python bindings
/// (does not change the `name` if it already is suitable) and returns the
/// modified version.
static std::string sanitizeName(StringRef name) {
- if (isPythonKeyword(name))
+ if (isPythonKeyword(name) || isODSReserved(name))
return (name + "_").str();
return name.str();
}
@@ -445,7 +456,7 @@ constexpr const char *initTemplate = R"Py(
results = []
attributes = {{}
{2}
- super().__init__(_ir.Operation.create(
+ super().__init__(_ods_ir.Operation.create(
"{0}", attributes=attributes, operands=operands, results=results,
loc=loc, ip=ip))
)Py";
@@ -468,30 +479,30 @@ constexpr const char *variadicAppendTemplate = "{0}s += [*{1}]";
/// Template for setting up the segment sizes buffer.
constexpr const char *segmentDeclarationTemplate =
- "{0}_segment_sizes = array.array('L')";
+ "{0}_segment_sizes_ods = _ods_array.array('L')";
/// Template for attaching segment sizes to the attribute list.
constexpr const char *segmentAttributeTemplate =
- R"Py(attributes["{0}_segment_sizes"] = _ir.DenseElementsAttr.get({0}_segment_sizes,
- context=_get_default_loc_context(loc)))Py";
+ R"Py(attributes["{0}_segment_sizes"] = _ods_ir.DenseElementsAttr.get({0}_segment_sizes_ods,
+ context=_ods_get_default_loc_context(loc)))Py";
/// Template for appending the unit size to the segment sizes.
/// {0} is either 'operand' or 'result';
/// {1} is the field name.
constexpr const char *singleElementSegmentTemplate =
- "{0}_segment_sizes.append(1) # {1}";
+ "{0}_segment_sizes_ods.append(1) # {1}";
/// Template for appending 0/1 for an optional element to the segment sizes.
/// {0} is either 'operand' or 'result';
/// {1} is the field name.
constexpr const char *optionalSegmentTemplate =
- "{0}_segment_sizes.append(0 if {1} is None else 1)";
+ "{0}_segment_sizes_ods.append(0 if {1} is None else 1)";
/// Template for appending the length of a variadic group to the segment sizes.
/// {0} is either 'operand' or 'result';
/// {1} is the field name.
constexpr const char *variadicSegmentTemplate =
- "{0}_segment_sizes.append(len({1}))";
+ "{0}_segment_sizes_ods.append(len({1}))";
/// Template for setting an attribute in the operation builder.
/// {0} is the attribute name;
@@ -505,8 +516,8 @@ constexpr const char *initOptionalAttributeTemplate =
R"Py(if {1} is not None: attributes["{0}"] = {1})Py";
constexpr const char *initUnitAttributeTemplate =
- R"Py(if bool({1}): attributes["{0}"] = _ir.UnitAttr.get(
- _get_default_loc_context(loc)))Py";
+ R"Py(if bool({1}): attributes["{0}"] = _ods_ir.UnitAttr.get(
+ _ods_get_default_loc_context(loc)))Py";
/// Populates `builderArgs` with the Python-compatible names of builder function
/// arguments, first the results, then the intermixed attributes and operands in
More information about the llvm-branch-commits
mailing list