Skip to content

[mlir][python] auto attribute casting #97786

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions mlir/include/mlir/Bindings/Python/PybindAdaptors.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,21 +406,25 @@ class pure_subclass {
class mlir_attribute_subclass : public pure_subclass {
public:
using IsAFunctionTy = bool (*)(MlirAttribute);
using GetTypeIDFunctionTy = MlirTypeID (*)();

/// Subclasses by looking up the super-class dynamically.
mlir_attribute_subclass(py::handle scope, const char *attrClassName,
IsAFunctionTy isaFunction)
IsAFunctionTy isaFunction,
GetTypeIDFunctionTy getTypeIDFunction = nullptr)
: mlir_attribute_subclass(
scope, attrClassName, isaFunction,
py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
.attr("Attribute")) {}
.attr("Attribute"),
getTypeIDFunction) {}

/// Subclasses with a provided mlir.ir.Attribute super-class. This must
/// be used if the subclass is being defined in the same extension module
/// as the mlir.ir class (otherwise, it will trigger a recursive
/// initialization).
mlir_attribute_subclass(py::handle scope, const char *typeClassName,
IsAFunctionTy isaFunction, const py::object &superCls)
IsAFunctionTy isaFunction, const py::object &superCls,
GetTypeIDFunctionTy getTypeIDFunction = nullptr)
: pure_subclass(scope, typeClassName, superCls) {
// Casting constructor. Note that it hard, if not impossible, to properly
// call chain to parent `__init__` in pybind11 due to its special handling
Expand Down Expand Up @@ -454,6 +458,20 @@ class mlir_attribute_subclass : public pure_subclass {
"isinstance",
[isaFunction](MlirAttribute other) { return isaFunction(other); },
py::arg("other_attribute"));
def("__repr__", [superCls, captureTypeName](py::object self) {
return py::repr(superCls(self))
.attr("replace")(superCls.attr("__name__"), captureTypeName);
});
if (getTypeIDFunction) {
def_staticmethod("get_static_typeid",
[getTypeIDFunction]() { return getTypeIDFunction(); });
py::module::import(MAKE_MLIR_PYTHON_QUALNAME("ir"))
.attr(MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR)(
getTypeIDFunction())(pybind11::cpp_function(
[thisClass = thisClass](const py::object &mlirAttribute) {
return thisClass(mlirAttribute);
}));
}
}
};

Expand Down
14 changes: 13 additions & 1 deletion mlir/test/python/dialects/python_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,23 @@ def testOptionalOperandOp():
# CHECK-LABEL: TEST: testCustomAttribute
@run
def testCustomAttribute():
with Context() as ctx:
with Context() as ctx, Location.unknown():
a = test.TestAttr.get()
# CHECK: #python_test.test_attr
print(a)

# CHECK: python_test.custom_attributed_op {
# CHECK: #python_test.test_attr
# CHECK: }
op2 = test.CustomAttributedOp(a)
print(f"{op2}")

# CHECK: #python_test.test_attr
print(f"{op2.test_attr}")

# CHECK: TestAttr(#python_test.test_attr)
print(repr(op2.test_attr))

# The following cast must not assert.
b = test.TestAttr(a)

Expand Down
4 changes: 4 additions & 0 deletions mlir/test/python/lib/PythonTestCAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ MlirAttribute mlirPythonTestTestAttributeGet(MlirContext context) {
return wrap(python_test::TestAttrAttr::get(unwrap(context)));
}

MlirTypeID mlirPythonTestTestAttributeGetTypeID(void) {
return wrap(python_test::TestAttrAttr::getTypeID());
}

bool mlirTypeIsAPythonTestTestType(MlirType type) {
return llvm::isa<python_test::TestTypeType>(unwrap(type));
}
Expand Down
2 changes: 2 additions & 0 deletions mlir/test/python/lib/PythonTestCAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ mlirAttributeIsAPythonTestTestAttribute(MlirAttribute attr);
MLIR_CAPI_EXPORTED MlirAttribute
mlirPythonTestTestAttributeGet(MlirContext context);

MLIR_CAPI_EXPORTED MlirTypeID mlirPythonTestTestAttributeGetTypeID(void);

MLIR_CAPI_EXPORTED bool mlirTypeIsAPythonTestTestType(MlirType type);

MLIR_CAPI_EXPORTED MlirType mlirPythonTestTestTypeGet(MlirContext context);
Expand Down
6 changes: 3 additions & 3 deletions mlir/test/python/lib/PythonTestDialect.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

#include "PythonTestDialect.h.inc"

#define GET_OP_CLASSES
#include "PythonTestOps.h.inc"

#define GET_ATTRDEF_CLASSES
#include "PythonTestAttributes.h.inc"

#define GET_TYPEDEF_CLASSES
#include "PythonTestTypes.h.inc"

#define GET_OP_CLASSES
#include "PythonTestOps.h.inc"

#endif // MLIR_TEST_PYTHON_LIB_PYTHONTESTDIALECT_H
7 changes: 4 additions & 3 deletions mlir/test/python/lib/PythonTestModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ PYBIND11_MODULE(_mlirPythonTest, m) {
py::arg("registry"));

mlir_attribute_subclass(m, "TestAttr",
mlirAttributeIsAPythonTestTestAttribute)
mlirAttributeIsAPythonTestTestAttribute,
mlirPythonTestTestAttributeGetTypeID)
.def_classmethod(
"get",
[](py::object cls, MlirContext ctx) {
[](const py::object &cls, MlirContext ctx) {
return cls(mlirPythonTestTestAttributeGet(ctx));
},
py::arg("cls"), py::arg("context") = py::none());
Expand All @@ -56,7 +57,7 @@ PYBIND11_MODULE(_mlirPythonTest, m) {
mlirPythonTestTestTypeGetTypeID)
.def_classmethod(
"get",
[](py::object cls, MlirContext ctx) {
[](const py::object &cls, MlirContext ctx) {
return cls(mlirPythonTestTestTypeGet(ctx));
},
py::arg("cls"), py::arg("context") = py::none());
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/python/python_test_ops.td
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def AttributedOp : TestOp<"attributed_op"> {
UnitAttr:$unit);
}

def CustomAttributedOp : TestOp<"custom_attributed_op"> {
let arguments = (ins TestAttr:$test_attr);
}

def AttributesOp : TestOp<"attributes_op"> {
let arguments = (ins
AffineMapArrayAttr:$x_affinemaparr,
Expand Down
Loading