diff options
author | max <maksim.levental@gmail.com> | 2023-05-31 15:52:46 -0500 |
---|---|---|
committer | max <maksim.levental@gmail.com> | 2023-06-07 12:01:00 -0500 |
commit | 9566ee280607d91fa2e5eca730a6765ac84dfd0f (patch) | |
tree | cbd97bd49e47ce5632818c4a155a90c723c85ba2 /mlir/lib/Bindings/Python/IRModule.h | |
parent | 31fbfa57e7126804180c85b9299ed7c9fd2be9b9 (diff) | |
download | llvm-9566ee280607d91fa2e5eca730a6765ac84dfd0f.zip llvm-9566ee280607d91fa2e5eca730a6765ac84dfd0f.tar.gz llvm-9566ee280607d91fa2e5eca730a6765ac84dfd0f.tar.bz2 |
[MLIR][python bindings] TypeCasters for Attributes
Differential Revision: https://reviews.llvm.org/D151840
Diffstat (limited to 'mlir/lib/Bindings/Python/IRModule.h')
-rw-r--r-- | mlir/lib/Bindings/Python/IRModule.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h index 013bb7b..225580f 100644 --- a/mlir/lib/Bindings/Python/IRModule.h +++ b/mlir/lib/Bindings/Python/IRModule.h @@ -986,6 +986,8 @@ public: // const char *pyClassName using ClassTy = pybind11::class_<DerivedTy, BaseTy>; using IsAFunctionTy = bool (*)(MlirAttribute); + using GetTypeIDFunctionTy = MlirTypeID (*)(); + static constexpr GetTypeIDFunctionTy getTypeIdFunction = nullptr; PyConcreteAttribute() = default; PyConcreteAttribute(PyMlirContextRef contextRef, MlirAttribute attr) @@ -1017,6 +1019,34 @@ public: pybind11::arg("other")); cls.def_property_readonly( "type", [](PyAttribute &attr) { return mlirAttributeGetType(attr); }); + cls.def_property_readonly_static( + "static_typeid", [](py::object & /*class*/) -> MlirTypeID { + if (DerivedTy::getTypeIdFunction) + return DerivedTy::getTypeIdFunction(); + throw py::attribute_error( + (DerivedTy::pyClassName + llvm::Twine(" has no typeid.")).str()); + }); + cls.def_property_readonly("typeid", [](PyAttribute &self) { + return py::cast(self).attr("typeid").cast<MlirTypeID>(); + }); + cls.def("__repr__", [](DerivedTy &self) { + PyPrintAccumulator printAccum; + printAccum.parts.append(DerivedTy::pyClassName); + printAccum.parts.append("("); + mlirAttributePrint(self, printAccum.getCallback(), + printAccum.getUserData()); + printAccum.parts.append(")"); + return printAccum.join(); + }); + + if (DerivedTy::getTypeIdFunction) { + PyGlobals::get().registerTypeCaster( + DerivedTy::getTypeIdFunction(), + pybind11::cpp_function([](PyAttribute pyAttribute) -> DerivedTy { + return pyAttribute; + })); + } + DerivedTy::bindDerived(cls); } |