[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
This commit is contained in:
John Demme 2021-07-15 19:03:48 -07:00
parent b1ffa8fc61
commit b4c93ece8e
3 changed files with 46 additions and 39 deletions

View File

@ -31,21 +31,21 @@ def AttrSizedOperandsOp : TestOp<"attr_sized_operands",
// CHECK: attributes=attributes, results=results, operands=operands, // CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip)) // CHECK: loc=loc, ip=ip))
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic1(self): // CHECK: def variadic1(self):
// CHECK: operand_range = _ods_segmented_accessor( // CHECK: operand_range = _ods_segmented_accessor(
// CHECK: self.operation.operands, // CHECK: self.operation.operands,
// CHECK: self.operation.attributes["operand_segment_sizes"], 0) // CHECK: self.operation.attributes["operand_segment_sizes"], 0)
// CHECK: return operand_range // CHECK: return operand_range
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def non_variadic(self): // CHECK: def non_variadic(self):
// CHECK: operand_range = _ods_segmented_accessor( // CHECK: operand_range = _ods_segmented_accessor(
// CHECK: self.operation.operands, // CHECK: self.operation.operands,
// CHECK: self.operation.attributes["operand_segment_sizes"], 1) // CHECK: self.operation.attributes["operand_segment_sizes"], 1)
// CHECK: return operand_range[0] // CHECK: return operand_range[0]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic2(self): // CHECK: def variadic2(self):
// CHECK: operand_range = _ods_segmented_accessor( // CHECK: operand_range = _ods_segmented_accessor(
// CHECK: self.operation.operands, // CHECK: self.operation.operands,
@ -72,21 +72,21 @@ def AttrSizedResultsOp : TestOp<"attr_sized_results",
// CHECK: attributes=attributes, results=results, operands=operands, // CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip)) // CHECK: loc=loc, ip=ip))
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic1(self): // CHECK: def variadic1(self):
// CHECK: result_range = _ods_segmented_accessor( // CHECK: result_range = _ods_segmented_accessor(
// CHECK: self.operation.results, // CHECK: self.operation.results,
// CHECK: self.operation.attributes["result_segment_sizes"], 0) // CHECK: self.operation.attributes["result_segment_sizes"], 0)
// CHECK: return result_range[0] if len(result_range) > 0 else None // CHECK: return result_range[0] if len(result_range) > 0 else None
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def non_variadic(self): // CHECK: def non_variadic(self):
// CHECK: result_range = _ods_segmented_accessor( // CHECK: result_range = _ods_segmented_accessor(
// CHECK: self.operation.results, // CHECK: self.operation.results,
// CHECK: self.operation.attributes["result_segment_sizes"], 1) // CHECK: self.operation.attributes["result_segment_sizes"], 1)
// CHECK: return result_range[0] // CHECK: return result_range[0]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic2(self): // CHECK: def variadic2(self):
// CHECK: result_range = _ods_segmented_accessor( // CHECK: result_range = _ods_segmented_accessor(
// CHECK: self.operation.results, // CHECK: self.operation.results,
@ -116,21 +116,21 @@ def AttributedOp : TestOp<"attributed_op"> {
// CHECK: attributes=attributes, results=results, operands=operands, // CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip)) // CHECK: loc=loc, ip=ip))
// CHECK: @property // CHECK: @builtins.property
// CHECK: def i32attr(self): // CHECK: def i32attr(self):
// CHECK: return _ods_ir.IntegerAttr(self.operation.attributes["i32attr"]) // CHECK: return _ods_ir.IntegerAttr(self.operation.attributes["i32attr"])
// CHECK: @property // CHECK: @builtins.property
// CHECK: def optionalF32Attr(self): // CHECK: def optionalF32Attr(self):
// CHECK: if "optionalF32Attr" not in self.operation.attributes: // CHECK: if "optionalF32Attr" not in self.operation.attributes:
// CHECK: return None // CHECK: return None
// CHECK: return _ods_ir.FloatAttr(self.operation.attributes["optionalF32Attr"]) // CHECK: return _ods_ir.FloatAttr(self.operation.attributes["optionalF32Attr"])
// CHECK: @property // CHECK: @builtins.property
// CHECK: def unitAttr(self): // CHECK: def unitAttr(self):
// CHECK: return "unitAttr" in self.operation.attributes // CHECK: return "unitAttr" in self.operation.attributes
// CHECK: @property // CHECK: @builtins.property
// CHECK: def in_(self): // CHECK: def in_(self):
// CHECK: return _ods_ir.IntegerAttr(self.operation.attributes["in"]) // CHECK: return _ods_ir.IntegerAttr(self.operation.attributes["in"])
let arguments = (ins I32Attr:$i32attr, OptionalAttr<F32Attr>:$optionalF32Attr, 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: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip)) // CHECK: loc=loc, ip=ip))
// CHECK: @property // CHECK: @builtins.property
// CHECK: def in_(self): // CHECK: def in_(self):
// CHECK: return "in" in self.operation.attributes // CHECK: return "in" in self.operation.attributes
// CHECK: @property // CHECK: @builtins.property
// CHECK: def is_(self): // CHECK: def is_(self):
// CHECK: if "is" not in self.operation.attributes: // CHECK: if "is" not in self.operation.attributes:
// CHECK: return None // CHECK: return None
@ -199,16 +199,16 @@ def MissingNamesOp : TestOp<"missing_names"> {
// CHECK: attributes=attributes, results=results, operands=operands, // CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip)) // CHECK: loc=loc, ip=ip))
// CHECK: @property // CHECK: @builtins.property
// CHECK: def f32(self): // CHECK: def f32(self):
// CHECK: return self.operation.operands[1] // CHECK: return self.operation.operands[1]
let arguments = (ins I32, F32:$f32, I64); let arguments = (ins I32, F32:$f32, I64);
// CHECK: @property // CHECK: @builtins.property
// CHECK: def i32(self): // CHECK: def i32(self):
// CHECK: return self.operation.results[0] // CHECK: return self.operation.results[0]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def i64(self): // CHECK: def i64(self):
// CHECK: return self.operation.results[2] // CHECK: return self.operation.results[2]
let results = (outs I32:$i32, F32, I64:$i64); 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: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip)) // CHECK: loc=loc, ip=ip))
// CHECK: @property // CHECK: @builtins.property
// CHECK: def non_variadic(self): // CHECK: def non_variadic(self):
// CHECK: return self.operation.operands[0] // CHECK: return self.operation.operands[0]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic(self): // CHECK: def variadic(self):
// CHECK: _ods_variadic_group_length = len(self.operation.operands) - 2 + 1 // CHECK: _ods_variadic_group_length = len(self.operation.operands) - 2 + 1
// CHECK: return self.operation.operands[1:1 + _ods_variadic_group_length] // 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: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip)) // CHECK: loc=loc, ip=ip))
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic(self): // CHECK: def variadic(self):
// CHECK: _ods_variadic_group_length = len(self.operation.results) - 2 + 1 // CHECK: _ods_variadic_group_length = len(self.operation.results) - 2 + 1
// CHECK: return self.operation.results[0:0 + _ods_variadic_group_length] // CHECK: return self.operation.results[0:0 + _ods_variadic_group_length]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def non_variadic(self): // CHECK: def non_variadic(self):
// CHECK: _ods_variadic_group_length = len(self.operation.results) - 2 + 1 // CHECK: _ods_variadic_group_length = len(self.operation.results) - 2 + 1
// CHECK: return self.operation.results[1 + _ods_variadic_group_length - 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: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip)) // CHECK: loc=loc, ip=ip))
// CHECK: @property // CHECK: @builtins.property
// CHECK: def in_(self): // CHECK: def in_(self):
// CHECK: return self.operation.operands[0] // CHECK: return self.operation.operands[0]
let arguments = (ins AnyType:$in); let arguments = (ins AnyType:$in);
@ -293,17 +293,17 @@ def PythonKeywordOp : TestOp<"python_keyword"> {
// CHECK-LABEL: OPERATION_NAME = "test.same_variadic_operand" // CHECK-LABEL: OPERATION_NAME = "test.same_variadic_operand"
def SameVariadicOperandSizeOp : TestOp<"same_variadic_operand", def SameVariadicOperandSizeOp : TestOp<"same_variadic_operand",
[SameVariadicOperandSize]> { [SameVariadicOperandSize]> {
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic1(self): // CHECK: def variadic1(self):
// CHECK: start, pg = _ods_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: return self.operation.operands[start:start + pg]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def non_variadic(self): // CHECK: def non_variadic(self):
// CHECK: start, pg = _ods_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: return self.operation.operands[start]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic2(self): // CHECK: def variadic2(self):
// CHECK: start, pg = _ods_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] // 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" // CHECK-LABEL: OPERATION_NAME = "test.same_variadic_result"
def SameVariadicResultSizeOp : TestOp<"same_variadic_result", def SameVariadicResultSizeOp : TestOp<"same_variadic_result",
[SameVariadicResultSize]> { [SameVariadicResultSize]> {
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic1(self): // CHECK: def variadic1(self):
// CHECK: start, pg = _ods_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: return self.operation.results[start:start + pg]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def non_variadic(self): // CHECK: def non_variadic(self):
// CHECK: start, pg = _ods_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: return self.operation.results[start]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def variadic2(self): // CHECK: def variadic2(self):
// CHECK: start, pg = _ods_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] // CHECK: return self.operation.results[start:start + pg]
@ -350,20 +350,20 @@ def SimpleOp : TestOp<"simple"> {
// CHECK: attributes=attributes, results=results, operands=operands, // CHECK: attributes=attributes, results=results, operands=operands,
// CHECK: loc=loc, ip=ip)) // CHECK: loc=loc, ip=ip))
// CHECK: @property // CHECK: @builtins.property
// CHECK: def i32(self): // CHECK: def i32(self):
// CHECK: return self.operation.operands[0] // CHECK: return self.operation.operands[0]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def f32(self): // CHECK: def f32(self):
// CHECK: return self.operation.operands[1] // CHECK: return self.operation.operands[1]
let arguments = (ins I32:$i32, F32:$f32); let arguments = (ins I32:$i32, F32:$f32);
// CHECK: @property // CHECK: @builtins.property
// CHECK: def i64(self): // CHECK: def i64(self):
// CHECK: return self.operation.results[0] // CHECK: return self.operation.results[0]
// //
// CHECK: @property // CHECK: @builtins.property
// CHECK: def f64(self): // CHECK: def f64(self):
// CHECK: return self.operation.results[1] // CHECK: return self.operation.results[1]
let results = (outs I64:$i64, F64:$f64); let results = (outs I64:$i64, F64:$f64);

View File

@ -25,4 +25,9 @@ def AttributedOp : TestOp<"attributed_op"> {
UnitAttr:$unit); UnitAttr:$unit);
} }
def PropertyOp : TestOp<"property_op"> {
let arguments = (ins I32Attr:$property,
I32:$idx);
}
#endif // PYTHON_TEST_OPS #endif // PYTHON_TEST_OPS

View File

@ -36,6 +36,8 @@ try:
except ImportError: except ImportError:
_ods_ext_module = None _ods_ext_module = None
import builtins
)Py"; )Py";
/// Template for dialect class: /// Template for dialect class:
@ -82,7 +84,7 @@ constexpr const char *opClassRegionSpecTemplate = R"Py(
/// {1} is either 'operand' or 'result'; /// {1} is either 'operand' or 'result';
/// {2} is the position in the element list. /// {2} is the position in the element list.
constexpr const char *opSingleTemplate = R"Py( constexpr const char *opSingleTemplate = R"Py(
@property @builtins.property
def {0}(self): def {0}(self):
return self.operation.{1}s[{2}] return self.operation.{1}s[{2}]
)Py"; )Py";
@ -95,7 +97,7 @@ constexpr const char *opSingleTemplate = R"Py(
/// This works for both a single variadic group (non-negative length) and an /// This works for both a single variadic group (non-negative length) and an
/// single optional element (zero length if the element is absent). /// single optional element (zero length if the element is absent).
constexpr const char *opSingleAfterVariableTemplate = R"Py( constexpr const char *opSingleAfterVariableTemplate = R"Py(
@property @builtins.property
def {0}(self): def {0}(self):
_ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1 _ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1
return self.operation.{1}s[{3} + _ods_variadic_group_length - 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; /// {2} is the total number of element groups;
/// {3} is the position of the current group in the group list. /// {3} is the position of the current group in the group list.
constexpr const char *opOneOptionalTemplate = R"Py( constexpr const char *opOneOptionalTemplate = R"Py(
@property @builtins.property
def {0}(self): def {0}(self):
return self.operation.{1}s[{3}] if len(self.operation.{1}s) > {2} else None return self.operation.{1}s[{3}] if len(self.operation.{1}s) > {2} else None
)Py"; )Py";
@ -118,7 +120,7 @@ constexpr const char *opOneOptionalTemplate = R"Py(
/// {2} is the total number of element groups; /// {2} is the total number of element groups;
/// {3} is the position of the current group in the group list. /// {3} is the position of the current group in the group list.
constexpr const char *opOneVariadicTemplate = R"Py( constexpr const char *opOneVariadicTemplate = R"Py(
@property @builtins.property
def {0}(self): def {0}(self):
_ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1 _ods_variadic_group_length = len(self.operation.{1}s) - {2} + 1
return self.operation.{1}s[{3}:{3} + _ods_variadic_group_length] 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 non-variadic groups preceding the current group;
/// {3} is the number of variadic groups preceding the current group. /// {3} is the number of variadic groups preceding the current group.
constexpr const char *opVariadicEqualPrefixTemplate = R"Py( constexpr const char *opVariadicEqualPrefixTemplate = R"Py(
@property @builtins.property
def {0}(self): def {0}(self):
start, pg = _ods_equally_sized_accessor(operation.{1}s, {2}, {3}, {4}))Py"; 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 /// {3} is a return suffix (expected [0] for single-element, empty for
/// variadic, and opVariadicSegmentOptionalTrailingTemplate for optional). /// variadic, and opVariadicSegmentOptionalTrailingTemplate for optional).
constexpr const char *opVariadicSegmentTemplate = R"Py( constexpr const char *opVariadicSegmentTemplate = R"Py(
@property @builtins.property
def {0}(self): def {0}(self):
{1}_range = _ods_segmented_accessor( {1}_range = _ods_segmented_accessor(
self.operation.{1}s, self.operation.{1}s,
@ -175,7 +177,7 @@ constexpr const char *opVariadicSegmentOptionalTrailingTemplate =
/// {1} is the Python type of the attribute; /// {1} is the Python type of the attribute;
/// {2} os the original name of the attribute. /// {2} os the original name of the attribute.
constexpr const char *attributeGetterTemplate = R"Py( constexpr const char *attributeGetterTemplate = R"Py(
@property @builtins.property
def {0}(self): def {0}(self):
return {1}(self.operation.attributes["{2}"]) return {1}(self.operation.attributes["{2}"])
)Py"; )Py";
@ -185,7 +187,7 @@ constexpr const char *attributeGetterTemplate = R"Py(
/// {1} is the Python type of the attribute; /// {1} is the Python type of the attribute;
/// {2} is the original name of the attribute. /// {2} is the original name of the attribute.
constexpr const char *optionalAttributeGetterTemplate = R"Py( constexpr const char *optionalAttributeGetterTemplate = R"Py(
@property @builtins.property
def {0}(self): def {0}(self):
if "{2}" not in self.operation.attributes: if "{2}" not in self.operation.attributes:
return None return None
@ -198,7 +200,7 @@ constexpr const char *optionalAttributeGetterTemplate = R"Py(
/// {0} is the name of the attribute sanitized for Python, /// {0} is the name of the attribute sanitized for Python,
/// {1} is the original name of the attribute. /// {1} is the original name of the attribute.
constexpr const char *unitAttributeGetterTemplate = R"Py( constexpr const char *unitAttributeGetterTemplate = R"Py(
@property @builtins.property
def {0}(self): def {0}(self):
return "{1}" in self.operation.attributes return "{1}" in self.operation.attributes
)Py"; )Py";