summaryrefslogtreecommitdiff
path: root/mlir/lib/Bindings/Python/MainModule.cpp
diff options
context:
space:
mode:
authormax <maksim.levental@gmail.com>2023-05-26 10:23:17 -0500
committermax <maksim.levental@gmail.com>2023-05-26 11:02:05 -0500
commitbfb1ba752655bf09b35c486f6cc9817dbedfb1bb (patch)
tree0eeac7a4de2ececeabbf6deeb386de30f9d5bf85 /mlir/lib/Bindings/Python/MainModule.cpp
parent5310be521db2aa8c05a1c1adb7e108fc2c7c9ddc (diff)
[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/MainModule.cpp')
-rw-r--r--mlir/lib/Bindings/Python/MainModule.cpp21
1 files changed, 15 insertions, 6 deletions
diff --git a/mlir/lib/Bindings/Python/MainModule.cpp b/mlir/lib/Bindings/Python/MainModule.cpp
index b32b4186fcb9..cdddfbe50606 100644
--- a/mlir/lib/Bindings/Python/MainModule.cpp
+++ b/mlir/lib/Bindings/Python/MainModule.cpp
@@ -16,6 +16,7 @@
namespace py = pybind11;
using namespace mlir;
+using namespace py::literals;
using namespace mlir::python;
// -----------------------------------------------------------------------------
@@ -35,12 +36,12 @@ PYBIND11_MODULE(_mlir, m) {
self.getDialectSearchPrefixes().push_back(std::move(moduleName));
self.clearImportCache();
},
- py::arg("module_name"))
+ "module_name"_a)
.def("_register_dialect_impl", &PyGlobals::registerDialectImpl,
- py::arg("dialect_namespace"), py::arg("dialect_class"),
+ "dialect_namespace"_a, "dialect_class"_a,
"Testing hook for directly registering a dialect")
.def("_register_operation_impl", &PyGlobals::registerOperationImpl,
- py::arg("operation_name"), py::arg("operation_class"),
+ "operation_name"_a, "operation_class"_a,
"Testing hook for directly registering an operation");
// Aside from making the globals accessible to python, having python manage
@@ -58,11 +59,11 @@ PYBIND11_MODULE(_mlir, m) {
PyGlobals::get().registerDialectImpl(dialectNamespace, pyClass);
return pyClass;
},
- py::arg("dialect_class"),
+ "dialect_class"_a,
"Class decorator for registering a custom Dialect wrapper");
m.def(
"register_operation",
- [](py::object dialectClass) -> py::cpp_function {
+ [](const py::object &dialectClass) -> py::cpp_function {
return py::cpp_function(
[dialectClass](py::object opClass) -> py::object {
std::string operationName =
@@ -75,9 +76,17 @@ PYBIND11_MODULE(_mlir, m) {
return opClass;
});
},
- py::arg("dialect_class"),
+ "dialect_class"_a,
"Produce a class decorator for registering an Operation class as part of "
"a dialect");
+ m.def(
+ MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR,
+ [](MlirTypeID mlirTypeID, py::function typeCaster, bool replace) {
+ PyGlobals::get().registerTypeCaster(mlirTypeID, std::move(typeCaster),
+ replace);
+ },
+ "typeid"_a, "type_caster"_a, "replace"_a = false,
+ "Register a type caster for casting MLIR types to custom user types.");
// Define and populate IR submodule.
auto irModule = m.def_submodule("ir", "MLIR IR Bindings");