[MLIR][python bindings] Add TypeCaster for returning refined types from python APIs
authormax <maksim.levental@gmail.com>
Fri, 26 May 2023 15:23:17 +0000 (10:23 -0500)
committermax <maksim.levental@gmail.com>
Fri, 26 May 2023 16:02:05 +0000 (11:02 -0500)
commitbfb1ba752655bf09b35c486f6cc9817dbedfb1bb
tree0eeac7a4de2ececeabbf6deeb386de30f9d5bf85
parent5310be521db2aa8c05a1c1adb7e108fc2c7c9ddc
[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
26 files changed:
mlir/include/mlir-c/Bindings/Python/Interop.h
mlir/include/mlir-c/Dialect/Transform.h
mlir/include/mlir-c/IR.h
mlir/include/mlir/Bindings/Python/PybindAdaptors.h
mlir/include/mlir/CAPI/Support.h
mlir/lib/Bindings/Python/DialectTransform.cpp
mlir/lib/Bindings/Python/Globals.h
mlir/lib/Bindings/Python/IRAttributes.cpp
mlir/lib/Bindings/Python/IRCore.cpp
mlir/lib/Bindings/Python/IRInterfaces.cpp
mlir/lib/Bindings/Python/IRModule.cpp
mlir/lib/Bindings/Python/IRModule.h
mlir/lib/Bindings/Python/IRTypes.cpp
mlir/lib/Bindings/Python/MainModule.cpp
mlir/lib/CAPI/Dialect/Transform.cpp
mlir/lib/CAPI/IR/BuiltinTypes.cpp
mlir/lib/CAPI/IR/IR.cpp
mlir/lib/CAPI/IR/Support.cpp
mlir/python/mlir/dialects/python_test.py
mlir/python/mlir/ir.py
mlir/test/python/dialects/python_test.py
mlir/test/python/ir/attributes.py
mlir/test/python/ir/builtin_types.py
mlir/test/python/lib/PythonTestCAPI.cpp
mlir/test/python/lib/PythonTestCAPI.h
mlir/test/python/lib/PythonTestModule.cpp