[all-commits] [llvm/llvm-project] bfb1ba: [MLIR][python bindings] Add TypeCaster for returni...

Maksim Levental via All-commits all-commits at lists.llvm.org
Fri May 26 09:02:22 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: bfb1ba752655bf09b35c486f6cc9817dbedfb1bb
      https://github.com/llvm/llvm-project/commit/bfb1ba752655bf09b35c486f6cc9817dbedfb1bb
  Author: max <maksim.levental at gmail.com>
  Date:   2023-05-26 (Fri, 26 May 2023)

  Changed paths:
    M mlir/include/mlir-c/Bindings/Python/Interop.h
    M mlir/include/mlir-c/Dialect/Transform.h
    M mlir/include/mlir-c/IR.h
    M mlir/include/mlir/Bindings/Python/PybindAdaptors.h
    M mlir/include/mlir/CAPI/Support.h
    M mlir/lib/Bindings/Python/DialectTransform.cpp
    M mlir/lib/Bindings/Python/Globals.h
    M mlir/lib/Bindings/Python/IRAttributes.cpp
    M mlir/lib/Bindings/Python/IRCore.cpp
    M mlir/lib/Bindings/Python/IRInterfaces.cpp
    M mlir/lib/Bindings/Python/IRModule.cpp
    M mlir/lib/Bindings/Python/IRModule.h
    M mlir/lib/Bindings/Python/IRTypes.cpp
    M mlir/lib/Bindings/Python/MainModule.cpp
    M mlir/lib/CAPI/Dialect/Transform.cpp
    M mlir/lib/CAPI/IR/BuiltinTypes.cpp
    M mlir/lib/CAPI/IR/IR.cpp
    M mlir/lib/CAPI/IR/Support.cpp
    M mlir/python/mlir/dialects/python_test.py
    M mlir/python/mlir/ir.py
    M mlir/test/python/dialects/python_test.py
    M mlir/test/python/ir/attributes.py
    M mlir/test/python/ir/builtin_types.py
    M mlir/test/python/lib/PythonTestCAPI.cpp
    M mlir/test/python/lib/PythonTestCAPI.h
    M mlir/test/python/lib/PythonTestModule.cpp

  Log Message:
  -----------
  [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




More information about the All-commits mailing list