[Mlir-commits] [mlir] b4c93ec - [MLIR] [Python ODS] Use @builtins.property for cases where 'property' is already defined
John Demme
llvmlistbot at llvm.org
Thu Jul 15 19:39:46 PDT 2021
Author: John Demme
Date: 2021-07-15T19:39:03-07:00
New Revision: b4c93ece8e4f6e98a15daca10c8a3db33cf8c195
URL: https://github.com/llvm/llvm-project/commit/b4c93ece8e4f6e98a15daca10c8a3db33cf8c195
DIFF: https://github.com/llvm/llvm-project/commit/b4c93ece8e4f6e98a15daca10c8a3db33cf8c195.diff
LOG: [MLIR] [Python ODS] Use @builtins.property for cases where 'property' is already defined
In cases where an operation has an argument or result named 'property', the
ODS-generated python fails on import because the `@property` resolves to the
`property` operation argument instead of the builtin `@property` decorator. We
should always use the fully qualified decorator name.
Reviewed By: mikeurbach
Differential Revision: https://reviews.llvm.org/D106106
Added:
Modified:
mlir/test/mlir-tblgen/op-python-bindings.td
mlir/test/python/python_test_ops.td
mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
Removed:
################################################################################
diff --git a/mlir/test/mlir-tblgen/op-python-bindings.td b/mlir/test/mlir-tblgen/op-python-bindings.td
index cdfc87add0690..95c58d5c3c25b 100644
--- a/mlir/test/mlir-tblgen/op-python-bindings.td
+++ b/mlir/test/mlir-tblgen/op-python-bindings.td
@@ -31,21 +31,21 @@ def AttrSizedOperandsOp : TestOp<"attr_sized_operands",
// CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip))
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def variadic1(self):
// 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: @builtins.property
// CHECK: def non_variadic(self):
// 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: @builtins.property
// CHECK: def variadic2(self):
// CHECK: operand_range = _ods_segmented_accessor(
// CHECK: self.operation.operands,
@@ -72,21 +72,21 @@ def AttrSizedResultsOp : TestOp<"attr_sized_results",
// CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip))
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def variadic1(self):
// 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: @builtins.property
// CHECK: def non_variadic(self):
// 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: @builtins.property
// CHECK: def variadic2(self):
// CHECK: result_range = _ods_segmented_accessor(
// CHECK: self.operation.results,
@@ -116,21 +116,21 @@ def AttributedOp : TestOp<"attributed_op"> {
// CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip))
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def i32attr(self):
// CHECK: return _ods_ir.IntegerAttr(self.operation.attributes["i32attr"])
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def optionalF32Attr(self):
// CHECK: if "optionalF32Attr" not in self.operation.attributes:
// CHECK: return None
// CHECK: return _ods_ir.FloatAttr(self.operation.attributes["optionalF32Attr"])
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def unitAttr(self):
// CHECK: return "unitAttr" in self.operation.attributes
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def in_(self):
// CHECK: return _ods_ir.IntegerAttr(self.operation.attributes["in"])
let arguments = (ins I32Attr:$i32attr, OptionalAttr<F32Attr>:$optionalF32Attr,
@@ -156,11 +156,11 @@ def AttributedOpWithOperands : TestOp<"attributed_op_with_operands"> {
// CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip))
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def in_(self):
// CHECK: return "in" in self.operation.attributes
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def is_(self):
// CHECK: if "is" not in self.operation.attributes:
// CHECK: return None
@@ -199,16 +199,16 @@ def MissingNamesOp : TestOp<"missing_names"> {
// CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip))
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def f32(self):
// CHECK: return self.operation.operands[1]
let arguments = (ins I32, F32:$f32, I64);
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def i32(self):
// CHECK: return self.operation.results[0]
//
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def i64(self):
// CHECK: return self.operation.results[2]
let results = (outs I32:$i32, F32, I64:$i64);
@@ -230,11 +230,11 @@ def OneVariadicOperandOp : TestOp<"one_variadic_operand"> {
// CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip))
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def non_variadic(self):
// CHECK: return self.operation.operands[0]
//
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def variadic(self):
// CHECK: _ods_variadic_group_length = len(self.operation.operands) - 2 + 1
// CHECK: return self.operation.operands[1:1 + _ods_variadic_group_length]
@@ -257,12 +257,12 @@ def OneVariadicResultOp : TestOp<"one_variadic_result"> {
// CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip))
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def variadic(self):
// 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: @builtins.property
// CHECK: def non_variadic(self):
// CHECK: _ods_variadic_group_length = len(self.operation.results) - 2 + 1
// CHECK: return self.operation.results[1 + _ods_variadic_group_length - 1]
@@ -282,7 +282,7 @@ def PythonKeywordOp : TestOp<"python_keyword"> {
// CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip))
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def in_(self):
// CHECK: return self.operation.operands[0]
let arguments = (ins AnyType:$in);
@@ -293,17 +293,17 @@ def PythonKeywordOp : TestOp<"python_keyword"> {
// CHECK-LABEL: OPERATION_NAME = "test.same_variadic_operand"
def SameVariadicOperandSizeOp : TestOp<"same_variadic_operand",
[SameVariadicOperandSize]> {
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def variadic1(self):
// CHECK: start, pg = _ods_equally_sized_accessor(operation.operands, 2, 0, 0)
// CHECK: return self.operation.operands[start:start + pg]
//
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def non_variadic(self):
// CHECK: start, pg = _ods_equally_sized_accessor(operation.operands, 2, 0, 1)
// CHECK: return self.operation.operands[start]
//
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def variadic2(self):
// CHECK: start, pg = _ods_equally_sized_accessor(operation.operands, 2, 1, 1)
// CHECK: return self.operation.operands[start:start + pg]
@@ -316,17 +316,17 @@ def SameVariadicOperandSizeOp : TestOp<"same_variadic_operand",
// CHECK-LABEL: OPERATION_NAME = "test.same_variadic_result"
def SameVariadicResultSizeOp : TestOp<"same_variadic_result",
[SameVariadicResultSize]> {
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def variadic1(self):
// CHECK: start, pg = _ods_equally_sized_accessor(operation.results, 2, 0, 0)
// CHECK: return self.operation.results[start:start + pg]
//
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def non_variadic(self):
// CHECK: start, pg = _ods_equally_sized_accessor(operation.results, 2, 0, 1)
// CHECK: return self.operation.results[start]
//
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def variadic2(self):
// CHECK: start, pg = _ods_equally_sized_accessor(operation.results, 2, 1, 1)
// CHECK: return self.operation.results[start:start + pg]
@@ -350,20 +350,20 @@ def SimpleOp : TestOp<"simple"> {
// CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip))
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def i32(self):
// CHECK: return self.operation.operands[0]
//
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def f32(self):
// CHECK: return self.operation.operands[1]
let arguments = (ins I32:$i32, F32:$f32);
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def i64(self):
// CHECK: return self.operation.results[0]
//
- // CHECK: @property
+ // CHECK: @builtins.property
// CHECK: def f64(self):
// CHECK: return self.operation.results[1]
let results = (outs I64:$i64, F64:$f64);
diff --git a/mlir/test/python/python_test_ops.td b/mlir/test/python/python_test_ops.td
index 34c7e37ef4b2e..d3d49395ad45a 100644
--- a/mlir/test/python/python_test_ops.td
+++ b/mlir/test/python/python_test_ops.td
@@ -25,4 +25,9 @@ def AttributedOp : TestOp<"attributed_op"> {
UnitAttr:$unit);
}
+def PropertyOp : TestOp<"property_op"> {
+ let arguments = (ins I32Attr:$property,
+ I32:$idx);
+}
+
#endif // PYTHON_TEST_OPS
diff --git a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
index d00555dc1c098..1c1123cdc2f94 100644
--- a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
@@ -36,6 +36,8 @@ _ods_ir = _ods_cext.ir
except ImportError:
_ods_ext_module = None
+import builtins
+
)Py";
/// Template for dialect class:
@@ -82,7 +84,7 @@ constexpr const char *opClassRegionSpecTemplate = R"Py(
/// {1} is either 'operand' or 'result';
/// {2} is the position in the element list.
constexpr const char *opSingleTemplate = R"Py(
- @property
+ @builtins.property
def {0}(self):
return self.operation.{1}s[{2}]
)Py";
@@ -95,7 +97,7 @@ constexpr const char *opSingleTemplate = R"Py(
/// This works for both a single variadic group (non-negative length) and an
/// single optional element (zero length if the element is absent).
constexpr const char *opSingleAfterVariableTemplate = R"Py(
- @property
+ @builtins.property
def {0}(self):
_ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1
return self.operation.{1}s[{3} + _ods_variadic_group_length - 1]
@@ -107,7 +109,7 @@ constexpr const char *opSingleAfterVariableTemplate = R"Py(
/// {2} is the total number of element groups;
/// {3} is the position of the current group in the group list.
constexpr const char *opOneOptionalTemplate = R"Py(
- @property
+ @builtins.property
def {0}(self):
return self.operation.{1}s[{3}] if len(self.operation.{1}s) > {2} else None
)Py";
@@ -118,7 +120,7 @@ constexpr const char *opOneOptionalTemplate = R"Py(
/// {2} is the total number of element groups;
/// {3} is the position of the current group in the group list.
constexpr const char *opOneVariadicTemplate = R"Py(
- @property
+ @builtins.property
def {0}(self):
_ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1
return self.operation.{1}s[{3}:{3} + _ods_variadic_group_length]
@@ -131,7 +133,7 @@ constexpr const char *opOneVariadicTemplate = R"Py(
/// {3} is the number of non-variadic groups preceding the current group;
/// {3} is the number of variadic groups preceding the current group.
constexpr const char *opVariadicEqualPrefixTemplate = R"Py(
- @property
+ @builtins.property
def {0}(self):
start, pg = _ods_equally_sized_accessor(operation.{1}s, {2}, {3}, {4}))Py";
@@ -156,7 +158,7 @@ constexpr const char *opVariadicEqualVariadicTemplate = R"Py(
/// {3} is a return suffix (expected [0] for single-element, empty for
/// variadic, and opVariadicSegmentOptionalTrailingTemplate for optional).
constexpr const char *opVariadicSegmentTemplate = R"Py(
- @property
+ @builtins.property
def {0}(self):
{1}_range = _ods_segmented_accessor(
self.operation.{1}s,
@@ -175,7 +177,7 @@ constexpr const char *opVariadicSegmentOptionalTrailingTemplate =
/// {1} is the Python type of the attribute;
/// {2} os the original name of the attribute.
constexpr const char *attributeGetterTemplate = R"Py(
- @property
+ @builtins.property
def {0}(self):
return {1}(self.operation.attributes["{2}"])
)Py";
@@ -185,7 +187,7 @@ constexpr const char *attributeGetterTemplate = R"Py(
/// {1} is the Python type of the attribute;
/// {2} is the original name of the attribute.
constexpr const char *optionalAttributeGetterTemplate = R"Py(
- @property
+ @builtins.property
def {0}(self):
if "{2}" not in self.operation.attributes:
return None
@@ -198,7 +200,7 @@ constexpr const char *optionalAttributeGetterTemplate = R"Py(
/// {0} is the name of the attribute sanitized for Python,
/// {1} is the original name of the attribute.
constexpr const char *unitAttributeGetterTemplate = R"Py(
- @property
+ @builtins.property
def {0}(self):
return "{1}" in self.operation.attributes
)Py";
More information about the Mlir-commits
mailing list