diff options
author | max <maksim.levental@gmail.com> | 2023-05-26 10:23:17 -0500 |
---|---|---|
committer | max <maksim.levental@gmail.com> | 2023-05-26 11:02:05 -0500 |
commit | bfb1ba752655bf09b35c486f6cc9817dbedfb1bb (patch) | |
tree | 0eeac7a4de2ececeabbf6deeb386de30f9d5bf85 /mlir/lib/Bindings/Python/IRModule.h | |
parent | 5310be521db2aa8c05a1c1adb7e108fc2c7c9ddc (diff) | |
download | llvm-bfb1ba752655bf09b35c486f6cc9817dbedfb1bb.zip llvm-bfb1ba752655bf09b35c486f6cc9817dbedfb1bb.tar.gz llvm-bfb1ba752655bf09b35c486f6cc9817dbedfb1bb.tar.bz2 |
[MLIR][python bindings] Add TypeCaster for returning refined types from python APIs
depends on D150839
This diff uses `MlirTypeID` to register `TypeCaster`s (i.e., `[](PyType pyType) -> DerivedTy { return pyType; }`) for all concrete types (i.e., `PyConcrete<...>`) that are then queried for (by `MlirTypeID`) and called in `struct type_caster<MlirType>::cast`. The result is that anywhere an `MlirType mlirType` is returned from a python binding, that `mlirType` is automatically cast to the correct concrete type. For example:
```
c0 = arith.ConstantOp(f32, 0.0)
# CHECK: F32Type(f32)
print(repr(c0.result.type))
unranked_tensor_type = UnrankedTensorType.get(f32)
unranked_tensor = tensor.FromElementsOp(unranked_tensor_type, [c0]).result
# CHECK: UnrankedTensorType
print(type(unranked_tensor.type).__name__)
# CHECK: UnrankedTensorType(tensor<*xf32>)
print(repr(unranked_tensor.type))
```
This functionality immediately extends to typed attributes (i.e., `attr.type`).
The diff also implements similar functionality for `mlir_type_subclass`es but in a slightly different way - for such types (which have no cpp corresponding `class` or `struct`) the user must provide a type caster in python (similar to how `AttrBuilder` works) or in cpp as a `py::cpp_function`.
Reviewed By: ftynse
Differential Revision: https://reviews.llvm.org/D150927
Diffstat (limited to 'mlir/lib/Bindings/Python/IRModule.h')
-rw-r--r-- | mlir/lib/Bindings/Python/IRModule.h | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h index cfa3737..013bb7b 100644 --- a/mlir/lib/Bindings/Python/IRModule.h +++ b/mlir/lib/Bindings/Python/IRModule.h @@ -13,6 +13,7 @@ #include <utility> #include <vector> +#include "Globals.h" #include "PybindUtils.h" #include "mlir-c/AffineExpr.h" @@ -868,9 +869,7 @@ public: PyConcreteType() = default; PyConcreteType(PyMlirContextRef contextRef, MlirType t) - : BaseTy(std::move(contextRef), t) { - pybind11::implicitly_convertible<PyType, DerivedTy>(); - } + : BaseTy(std::move(contextRef), t) {} PyConcreteType(PyType &orig) : PyConcreteType(orig.getContext(), castFrom(orig)) {} @@ -914,6 +913,13 @@ public: return printAccum.join(); }); + if (DerivedTy::getTypeIdFunction) { + PyGlobals::get().registerTypeCaster( + DerivedTy::getTypeIdFunction(), + pybind11::cpp_function( + [](PyType pyType) -> DerivedTy { return pyType; })); + } + DerivedTy::bindDerived(cls); } @@ -1009,9 +1015,8 @@ public: return DerivedTy::isaFunction(otherAttr); }, pybind11::arg("other")); - cls.def_property_readonly("type", [](PyAttribute &attr) { - return PyType(attr.getContext(), mlirAttributeGetType(attr)); - }); + cls.def_property_readonly( + "type", [](PyAttribute &attr) { return mlirAttributeGetType(attr); }); DerivedTy::bindDerived(cls); } |