[mlir][python] Remove PythonAttr mapping functionality

This functionality has been replaced by TypeCasters (see D151840)

depends on D154468

Reviewed By: ftynse

Differential Revision: https://reviews.llvm.org/D154469
This commit is contained in:
Rahul Kayaith
2023-07-04 22:21:26 -04:00
parent fe22b9050c
commit 67a910bbff
31 changed files with 26 additions and 102 deletions

View File

@@ -271,6 +271,11 @@ endfunction()
# SOURCES: Same as declare_mlir_python_sources().
# SOURCES_GLOB: Same as declare_mlir_python_sources().
# DEPENDS: Additional dependency targets.
#
# TODO: Right now `TD_FILE` can't be the actual dialect tablegen file, since we
# use its path to determine where to place the generated python file. If
# we made the output path an additional argument here we could remove the
# need for the separate "wrapper" .td files
function(declare_mlir_dialect_python_bindings)
cmake_parse_arguments(ARG
""

View File

@@ -919,7 +919,6 @@ the `Func` (which is assigned the namespace `func` as a special case):
#ifndef PYTHON_BINDINGS_FUNC_OPS
#define PYTHON_BINDINGS_FUNC_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Func/IR/FuncOps.td"
#endif // PYTHON_BINDINGS_FUNC_OPS
@@ -1125,14 +1124,10 @@ Dialect operations are provided in Python by wrapping the generic
properties. Therefore, there is no need to implement a separate C API for them.
For operations defined in ODS, `mlir-tblgen -gen-python-op-bindings
-bind-dialect=<dialect-namespace>` generates the Python API from the declarative
description. If the build API uses specific attribute types, such as
`::mlir::IntegerAttr` or `::mlir::DenseIntElementsAttr`, for its arguments, the
mapping to the corresponding Python types should be provided in ODS definition.
For built-in attribute types, this mapping is available in
[`include/mlir/Bindings/Python/Attributes.td`](https://github.com/llvm/llvm-project/blob/main/mlir/include/mlir/Bindings/Python/Attributes.td);
it is sufficient to create a new `.td` file that includes this file and the
original ODS definition and use it as source for the `mlir-tblgen` call. Such
`.td` files reside in
description.
It is sufficient to create a new `.td` file that includes the original ODS
definition and use it as source for the `mlir-tblgen` call.
Such `.td` files reside in
[`python/mlir/dialects/`](https://github.com/llvm/llvm-project/tree/main/mlir/python/mlir/dialects).
The results of `mlir-tblgen` are expected to produce a file named
`_<dialect-namespace>_ops_gen.py` by convention. The generated operation classes

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_STANDALONE_OPS
#define PYTHON_BINDINGS_STANDALONE_OPS
include "mlir/Bindings/Python/Attributes.td"
include "Standalone/StandaloneOps.td"
#endif

View File

@@ -1,24 +0,0 @@
//===-- Attributes.td - Attribute mapping for Python -------*- tablegen -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This defines the mapping between MLIR ODS attributes and the corresponding
// Python binding classes.
//
//===----------------------------------------------------------------------===//
#ifndef PYTHON_BINDINGS_ATTRIBUTES
#define PYTHON_BINDINGS_ATTRIBUTES
// A mapping between the attribute storage type and the corresponding Python
// type. There is not necessarily a 1-1 match for non-builtin attributes.
class PythonAttr<string c, string p> {
string cppStorageType = c;
string pythonType = p;
}
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_ARITH_OPS
#define PYTHON_BINDINGS_ARITH_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Arith/IR/ArithOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_ASYNC_OPS
#define PYTHON_BINDINGS_ASYNC_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Async/IR/AsyncOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_BUFFERIZATION_OPS
#define PYTHON_BINDINGS_BUFFERIZATION_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Bufferization/IR/BufferizationOps.td"
#endif

View File

@@ -15,7 +15,6 @@
#ifndef PYTHON_BINDINGS_BUFFERIZATION_TRANSFORM_OPS
#define PYTHON_BINDINGS_BUFFERIZATION_TRANSFORM_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Bufferization/TransformOps/BufferizationTransformOps.td"
#endif // PYTHON_BINDINGS_BUFFERIZATION_TRANSFORM_OPS

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_BUILTIN_OPS
#define PYTHON_BINDINGS_BUILTIN_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/IR/BuiltinOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_COMPLEX_OPS
#define PYTHON_BINDINGS_COMPLEX_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Complex/IR/ComplexOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_CONTROL_FLOW_OPS
#define PYTHON_BINDINGS_CONTROL_FLOW_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.td"
#endif

View File

@@ -14,7 +14,6 @@
#ifndef PYTHON_BINDINGS_FUNC
#define PYTHON_BINDINGS_FUNC
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Func/IR/FuncOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_GPU_OPS
#define PYTHON_BINDINGS_GPU_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/GPU/IR/GPUOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_LINALG_OPS
#define PYTHON_BINDINGS_LINALG_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Linalg/IR/LinalgOps.td"
include "mlir/Dialect/Linalg/IR/LinalgStructuredOps.td"

View File

@@ -15,7 +15,6 @@
#ifndef PYTHON_BINDINGS_LINALG_STRUCTURED_TRANSFORM_OPS
#define PYTHON_BINDINGS_LINALG_STRUCTURED_TRANSFORM_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Linalg/TransformOps/LinalgTransformOps.td"
#endif // PYTHON_BINDINGS_LINALG_STRUCTURED_TRANSFORM_OPS

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_MLPROGRAM_OPS
#define PYTHON_BINDINGS_MLPROGRAM_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/MLProgram/IR/MLProgramOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_MATH_OPS
#define PYTHON_BINDINGS_MATH_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Math/IR/MathOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_MEMREF_OPS
#define PYTHON_BINDINGS_MEMREF_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/MemRef/IR/MemRefOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_PDL_OPS
#define PYTHON_BINDINGS_PDL_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/PDL/IR/PDLOps.td"
#endif

View File

@@ -15,7 +15,6 @@
#ifndef PYTHON_BINDINGS_SCF_LOOP_TRANSFORM_OPS
#define PYTHON_BINDINGS_SCF_LOOP_TRANSFORM_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/SCF/TransformOps/SCFTransformOps.td"
#endif // PYTHON_BINDINGS_SCF_LOOP_TRANSFORM_OPS

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_SCF_OPS
#define PYTHON_BINDINGS_SCF_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/SCF/IR/SCFOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_SHAPE_OPS
#define PYTHON_BINDINGS_SHAPE_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Shape/IR/ShapeOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_SPARSE_TENSOR_OPS
#define PYTHON_BINDINGS_SPARSE_TENSOR_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/SparseTensor/IR/SparseTensorOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_TENSOR_OPS
#define PYTHON_BINDINGS_TENSOR_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Tensor/IR/TensorOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_TOSA_OPS
#define PYTHON_BINDINGS_TOSA_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Tosa/IR/TosaOps.td"
#endif

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_TRANSFORM_OPS
#define PYTHON_BINDINGS_TRANSFORM_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Transform/IR/TransformOps.td"
#endif // PYTHON_BINDINGS_TRANSFORM_OPS

View File

@@ -14,7 +14,6 @@
#ifndef PYTHON_BINDINGS_TRANSFORM_PDL_EXTENSION_OPS
#define PYTHON_BINDINGS_TRANSFORM_PDL_EXTENSION_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Transform/PDLExtension/PDLExtensionOps.td"
#endif // PYTHON_BINDINGS_TRANSFORM_PDL_EXTENSION_OPS

View File

@@ -9,7 +9,6 @@
#ifndef PYTHON_BINDINGS_VECTOR_OPS
#define PYTHON_BINDINGS_VECTOR_OPS
include "mlir/Bindings/Python/Attributes.td"
include "mlir/Dialect/Vector/IR/VectorOps.td"
#endif

View File

@@ -3,7 +3,6 @@
include "mlir/IR/OpBase.td"
include "mlir/IR/AttrTypeBase.td"
include "mlir/Interfaces/InferTypeOpInterface.td"
include "mlir/Bindings/Python/Attributes.td"
// CHECK: @_ods_cext.register_dialect
// CHECK: class _Dialect(_ods_ir.Dialect):
@@ -13,8 +12,6 @@ def Test_Dialect : Dialect {
let name = "test";
let cppNamespace = "Test";
}
def TestAttr : AttrDef<Test_Dialect, "TestAttr">;
def : PythonAttr<TestAttr.cppType, "<TestAttr hook>">;
class TestOp<string mnemonic, list<Trait> traits = []> :
Op<Test_Dialect, mnemonic, traits>;
@@ -114,7 +111,7 @@ def AttrSizedResultsOp : TestOp<"attr_sized_results",
// CHECK-NOT: _ODS_OPERAND_SEGMENTS
// CHECK-NOT: _ODS_RESULT_SEGMENTS
def AttributedOp : TestOp<"attributed_op"> {
// CHECK: def __init__(self, i32attr, in_, test_attr, *, optionalF32Attr=None, unitAttr=None, loc=None, ip=None):
// CHECK: def __init__(self, i32attr, in_, *, optionalF32Attr=None, unitAttr=None, loc=None, ip=None):
// CHECK: operands = []
// CHECK: results = []
// CHECK: attributes = {}
@@ -134,13 +131,13 @@ def AttributedOp : TestOp<"attributed_op"> {
// CHECK: @builtins.property
// CHECK: def i32attr(self):
// CHECK: return (self.operation.attributes["i32attr"])
// CHECK: return self.operation.attributes["i32attr"]
// CHECK: @builtins.property
// CHECK: def optionalF32Attr(self):
// CHECK: if "optionalF32Attr" not in self.operation.attributes:
// CHECK: return None
// CHECK: return (self.operation.attributes["optionalF32Attr"])
// CHECK: return self.operation.attributes["optionalF32Attr"]
// CHECK: @builtins.property
// CHECK: def unitAttr(self):
@@ -148,13 +145,10 @@ def AttributedOp : TestOp<"attributed_op"> {
// CHECK: @builtins.property
// CHECK: def in_(self):
// CHECK: return (self.operation.attributes["in"])
// CHECK: return self.operation.attributes["in"]
// CHECK: @builtins.property
// CHECK: def test_attr(self):
// CHECK: return <TestAttr hook>(self.operation.attributes["test_attr"])
let arguments = (ins I32Attr:$i32attr, OptionalAttr<F32Attr>:$optionalF32Attr,
UnitAttr:$unitAttr, I32Attr:$in, TestAttr:$test_attr);
UnitAttr:$unitAttr, I32Attr:$in);
}
// CHECK: @_ods_cext.register_operation(_Dialect)
@@ -186,7 +180,7 @@ def AttributedOpWithOperands : TestOp<"attributed_op_with_operands"> {
// CHECK: def is_(self):
// CHECK: if "is" not in self.operation.attributes:
// CHECK: return None
// CHECK: return (self.operation.attributes["is"])
// CHECK: return self.operation.attributes["is"]
let arguments = (ins I32, UnitAttr:$in, F32, OptionalAttr<F32Attr>:$is);
}

View File

@@ -10,7 +10,6 @@
#define PYTHON_TEST_OPS
include "mlir/IR/AttrTypeBase.td"
include "mlir/Bindings/Python/Attributes.td"
include "mlir/IR/OpBase.td"
include "mlir/Interfaces/InferTypeOpInterface.td"

View File

@@ -182,24 +182,22 @@ constexpr const char *opVariadicSegmentOptionalTrailingTemplate =
/// Template for an operation attribute getter:
/// {0} is the name of the attribute sanitized for Python;
/// {1} is the Python type of the attribute;
/// {2} os the original name of the attribute.
/// {1} is the original name of the attribute.
constexpr const char *attributeGetterTemplate = R"Py(
@builtins.property
def {0}(self):
return {1}(self.operation.attributes["{2}"])
return self.operation.attributes["{1}"]
)Py";
/// Template for an optional operation attribute getter:
/// {0} is the name of the attribute sanitized for Python;
/// {1} is the Python type of the attribute;
/// {2} is the original name of the attribute.
/// {1} is the original name of the attribute.
constexpr const char *optionalAttributeGetterTemplate = R"Py(
@builtins.property
def {0}(self):
if "{2}" not in self.operation.attributes:
if "{1}" not in self.operation.attributes:
return None
return {1}(self.operation.attributes["{2}"])
return self.operation.attributes["{1}"]
)Py";
/// Template for a getter of a unit operation attribute, returns True of the
@@ -443,9 +441,7 @@ static void emitResultAccessors(const Operator &op, raw_ostream &os) {
}
/// Emits accessors to Op attributes.
static void emitAttributeAccessors(const Operator &op,
const AttributeClasses &attributeClasses,
raw_ostream &os) {
static void emitAttributeAccessors(const Operator &op, raw_ostream &os) {
for (const auto &namedAttr : op.getAttributes()) {
// Skip "derived" attributes because they are just C++ functions that we
// don't currently expose.
@@ -468,17 +464,15 @@ static void emitAttributeAccessors(const Operator &op,
continue;
}
StringRef pythonType =
attributeClasses.lookup(namedAttr.attr.getStorageType());
if (namedAttr.attr.isOptional()) {
os << llvm::formatv(optionalAttributeGetterTemplate, sanitizedName,
pythonType, namedAttr.name);
namedAttr.name);
os << llvm::formatv(optionalAttributeSetterTemplate, sanitizedName,
namedAttr.name);
os << llvm::formatv(attributeDeleterTemplate, sanitizedName,
namedAttr.name);
} else {
os << llvm::formatv(attributeGetterTemplate, sanitizedName, pythonType,
os << llvm::formatv(attributeGetterTemplate, sanitizedName,
namedAttr.name);
os << llvm::formatv(attributeSetterTemplate, sanitizedName,
namedAttr.name);
@@ -944,15 +938,6 @@ static void emitDefaultOpBuilder(const Operator &op, raw_ostream &os) {
llvm::join(builderLines, "\n "));
}
static void constructAttributeMapping(const llvm::RecordKeeper &records,
AttributeClasses &attributeClasses) {
for (const llvm::Record *rec :
records.getAllDerivedDefinitions("PythonAttr")) {
attributeClasses.try_emplace(rec->getValueAsString("cppStorageType").trim(),
rec->getValueAsString("pythonType").trim());
}
}
static void emitSegmentSpec(
const Operator &op, const char *kind,
llvm::function_ref<int(const Operator &)> getNumElements,
@@ -999,9 +984,7 @@ static void emitRegionAccessors(const Operator &op, raw_ostream &os) {
}
/// Emits bindings for a specific Op to the given output stream.
static void emitOpBindings(const Operator &op,
const AttributeClasses &attributeClasses,
raw_ostream &os) {
static void emitOpBindings(const Operator &op, raw_ostream &os) {
os << llvm::formatv(opClassTemplate, op.getCppClassName(),
op.getOperationName());
@@ -1016,7 +999,7 @@ static void emitOpBindings(const Operator &op,
emitRegionAttributes(op, os);
emitDefaultOpBuilder(op, os);
emitOperandAccessors(op, os);
emitAttributeAccessors(op, attributeClasses, os);
emitAttributeAccessors(op, os);
emitResultAccessors(op, os);
emitRegionAccessors(op, os);
}
@@ -1028,9 +1011,6 @@ static bool emitAllOps(const llvm::RecordKeeper &records, raw_ostream &os) {
if (clDialectName.empty())
llvm::PrintFatalError("dialect name not provided");
AttributeClasses attributeClasses;
constructAttributeMapping(records, attributeClasses);
bool isExtension = !clDialectExtensionName.empty();
os << llvm::formatv(fileHeader, isExtension
? clDialectExtensionName.getValue()
@@ -1043,7 +1023,7 @@ static bool emitAllOps(const llvm::RecordKeeper &records, raw_ostream &os) {
for (const llvm::Record *rec : records.getAllDerivedDefinitions("Op")) {
Operator op(rec);
if (op.getDialectName() == clDialectName.getValue())
emitOpBindings(op, attributeClasses, os);
emitOpBindings(op, os);
}
return false;
}