
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
66 lines
2.5 KiB
C++
66 lines
2.5 KiB
C++
//===- DialectTransform.cpp - 'transform' dialect submodule ---------------===//
|
|
//
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir-c/Dialect/Transform.h"
|
|
#include "mlir-c/IR.h"
|
|
#include "mlir-c/Support.h"
|
|
#include "mlir/Bindings/Python/PybindAdaptors.h"
|
|
|
|
namespace py = pybind11;
|
|
using namespace mlir;
|
|
using namespace mlir::python;
|
|
using namespace mlir::python::adaptors;
|
|
|
|
void populateDialectTransformSubmodule(const pybind11::module &m) {
|
|
//===-------------------------------------------------------------------===//
|
|
// AnyOpType
|
|
//===-------------------------------------------------------------------===//
|
|
|
|
auto anyOpType =
|
|
mlir_type_subclass(m, "AnyOpType", mlirTypeIsATransformAnyOpType);
|
|
anyOpType.def_classmethod(
|
|
"get",
|
|
[](py::object cls, MlirContext ctx) {
|
|
return cls(mlirTransformAnyOpTypeGet(ctx));
|
|
},
|
|
"Get an instance of AnyOpType in the given context.", py::arg("cls"),
|
|
py::arg("context") = py::none());
|
|
|
|
//===-------------------------------------------------------------------===//
|
|
// OperationType
|
|
//===-------------------------------------------------------------------===//
|
|
|
|
auto operationType =
|
|
mlir_type_subclass(m, "OperationType", mlirTypeIsATransformOperationType,
|
|
mlirTransformOperationTypeGetTypeID);
|
|
operationType.def_classmethod(
|
|
"get",
|
|
[](py::object cls, const std::string &operationName, MlirContext ctx) {
|
|
MlirStringRef cOperationName =
|
|
mlirStringRefCreate(operationName.data(), operationName.size());
|
|
return cls(mlirTransformOperationTypeGet(ctx, cOperationName));
|
|
},
|
|
"Get an instance of OperationType for the given kind in the given "
|
|
"context",
|
|
py::arg("cls"), py::arg("operation_name"),
|
|
py::arg("context") = py::none());
|
|
operationType.def_property_readonly(
|
|
"operation_name",
|
|
[](MlirType type) {
|
|
MlirStringRef operationName =
|
|
mlirTransformOperationTypeGetOperationName(type);
|
|
return py::str(operationName.data, operationName.length);
|
|
},
|
|
"Get the name of the payload operation accepted by the handle.");
|
|
}
|
|
|
|
PYBIND11_MODULE(_mlirDialectsTransform, m) {
|
|
m.doc() = "MLIR Transform dialect.";
|
|
populateDialectTransformSubmodule(m);
|
|
}
|