2024-12-09 16:37:43 -05:00
|
|
|
//===- PythonTestModuleNanobind.cpp - PythonTest dialect extension --------===//
|
2021-10-14 17:18:28 +02:00
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2024-12-09 16:37:43 -05:00
|
|
|
// This is the nanobind edition of the PythonTest dialect module.
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2021-10-14 17:18:28 +02:00
|
|
|
#include "PythonTestCAPI.h"
|
2023-05-26 10:23:17 -05:00
|
|
|
#include "mlir-c/BuiltinAttributes.h"
|
2023-05-22 11:12:53 -05:00
|
|
|
#include "mlir-c/BuiltinTypes.h"
|
2025-03-10 15:59:47 -07:00
|
|
|
#include "mlir-c/Diagnostics.h"
|
2023-05-26 10:23:17 -05:00
|
|
|
#include "mlir-c/IR.h"
|
2025-03-10 15:59:47 -07:00
|
|
|
#include "mlir/Bindings/Python/Diagnostics.h"
|
2024-12-20 23:32:32 -05:00
|
|
|
#include "mlir/Bindings/Python/Nanobind.h"
|
2024-12-09 16:37:43 -05:00
|
|
|
#include "mlir/Bindings/Python/NanobindAdaptors.h"
|
2025-03-10 15:59:47 -07:00
|
|
|
#include "nanobind/nanobind.h"
|
2021-10-14 17:18:28 +02:00
|
|
|
|
2024-12-09 16:37:43 -05:00
|
|
|
namespace nb = nanobind;
|
|
|
|
|
using namespace mlir::python::nanobind_adaptors;
|
2023-05-26 10:23:17 -05:00
|
|
|
|
|
|
|
|
static bool mlirTypeIsARankedIntegerTensor(MlirType t) {
|
|
|
|
|
return mlirTypeIsARankedTensor(t) &&
|
|
|
|
|
mlirTypeIsAInteger(mlirShapedTypeGetElementType(t));
|
|
|
|
|
}
|
2021-10-14 17:18:28 +02:00
|
|
|
|
2024-12-09 16:37:43 -05:00
|
|
|
NB_MODULE(_mlirPythonTestNanobind, m) {
|
2021-10-14 17:18:28 +02:00
|
|
|
m.def(
|
|
|
|
|
"register_python_test_dialect",
|
|
|
|
|
[](MlirContext context, bool load) {
|
|
|
|
|
MlirDialectHandle pythonTestDialect =
|
|
|
|
|
mlirGetDialectHandle__python_test__();
|
|
|
|
|
mlirDialectHandleRegisterDialect(pythonTestDialect, context);
|
|
|
|
|
if (load) {
|
|
|
|
|
mlirDialectHandleLoadDialect(pythonTestDialect, context);
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-12-09 16:37:43 -05:00
|
|
|
nb::arg("context"), nb::arg("load") = true);
|
2022-01-19 12:21:42 +01:00
|
|
|
|
2023-11-27 19:26:05 -06:00
|
|
|
m.def(
|
|
|
|
|
"register_dialect",
|
|
|
|
|
[](MlirDialectRegistry registry) {
|
|
|
|
|
MlirDialectHandle pythonTestDialect =
|
|
|
|
|
mlirGetDialectHandle__python_test__();
|
|
|
|
|
mlirDialectHandleInsertDialect(pythonTestDialect, registry);
|
|
|
|
|
},
|
2024-12-09 16:37:43 -05:00
|
|
|
nb::arg("registry"));
|
2023-11-27 19:26:05 -06:00
|
|
|
|
2025-03-10 15:59:47 -07:00
|
|
|
m.def("test_diagnostics_with_errors_and_notes", [](MlirContext ctx) {
|
|
|
|
|
mlir::python::CollectDiagnosticsToStringScope handler(ctx);
|
|
|
|
|
|
|
|
|
|
mlirPythonTestEmitDiagnosticWithNote(ctx);
|
|
|
|
|
throw nb::value_error(handler.takeMessage().c_str());
|
|
|
|
|
});
|
|
|
|
|
|
2022-01-19 12:21:42 +01:00
|
|
|
mlir_attribute_subclass(m, "TestAttr",
|
2024-07-05 10:43:51 -05:00
|
|
|
mlirAttributeIsAPythonTestTestAttribute,
|
|
|
|
|
mlirPythonTestTestAttributeGetTypeID)
|
2022-01-19 12:21:42 +01:00
|
|
|
.def_classmethod(
|
|
|
|
|
"get",
|
2024-12-09 16:37:43 -05:00
|
|
|
[](const nb::object &cls, MlirContext ctx) {
|
2022-01-19 12:21:42 +01:00
|
|
|
return cls(mlirPythonTestTestAttributeGet(ctx));
|
|
|
|
|
},
|
2024-12-09 16:37:43 -05:00
|
|
|
nb::arg("cls"), nb::arg("context").none() = nb::none());
|
[mlir][python] value casting (#69644)
This PR adds "value casting", i.e., a mechanism to wrap `ir.Value` in a
proxy class that overloads dunders such as `__add__`, `__sub__`, and
`__mul__` for fun and great profit.
This is thematically similar to
https://github.com/llvm/llvm-project/commit/bfb1ba752655bf09b35c486f6cc9817dbedfb1bb
and
https://github.com/llvm/llvm-project/commit/9566ee280607d91fa2e5eca730a6765ac84dfd0f.
The example in the test demonstrates the value of the feature (no pun
intended):
```python
@register_value_caster(F16Type.static_typeid)
@register_value_caster(F32Type.static_typeid)
@register_value_caster(F64Type.static_typeid)
@register_value_caster(IntegerType.static_typeid)
class ArithValue(Value):
__add__ = partialmethod(_binary_op, op="add")
__sub__ = partialmethod(_binary_op, op="sub")
__mul__ = partialmethod(_binary_op, op="mul")
a = arith.constant(value=FloatAttr.get(f16_t, 42.42))
b = a + a
# CHECK: ArithValue(%0 = arith.addf %cst, %cst : f16)
print(b)
a = arith.constant(value=FloatAttr.get(f32_t, 42.42))
b = a - a
# CHECK: ArithValue(%1 = arith.subf %cst_0, %cst_0 : f32)
print(b)
a = arith.constant(value=FloatAttr.get(f64_t, 42.42))
b = a * a
# CHECK: ArithValue(%2 = arith.mulf %cst_1, %cst_1 : f64)
print(b)
```
**EDIT**: this now goes through the bindings and thus supports automatic
casting of `OpResult` (including as an element of `OpResultList`),
`BlockArgument` (including as an element of `BlockArgumentList`), as
well as `Value`.
2023-11-07 10:49:41 -06:00
|
|
|
|
2023-05-26 10:23:17 -05:00
|
|
|
mlir_type_subclass(m, "TestType", mlirTypeIsAPythonTestTestType,
|
|
|
|
|
mlirPythonTestTestTypeGetTypeID)
|
2022-01-19 12:21:42 +01:00
|
|
|
.def_classmethod(
|
|
|
|
|
"get",
|
2024-12-09 16:37:43 -05:00
|
|
|
[](const nb::object &cls, MlirContext ctx) {
|
2022-01-19 12:21:42 +01:00
|
|
|
return cls(mlirPythonTestTestTypeGet(ctx));
|
|
|
|
|
},
|
2024-12-09 16:37:43 -05:00
|
|
|
nb::arg("cls"), nb::arg("context").none() = nb::none());
|
[mlir][python] value casting (#69644)
This PR adds "value casting", i.e., a mechanism to wrap `ir.Value` in a
proxy class that overloads dunders such as `__add__`, `__sub__`, and
`__mul__` for fun and great profit.
This is thematically similar to
https://github.com/llvm/llvm-project/commit/bfb1ba752655bf09b35c486f6cc9817dbedfb1bb
and
https://github.com/llvm/llvm-project/commit/9566ee280607d91fa2e5eca730a6765ac84dfd0f.
The example in the test demonstrates the value of the feature (no pun
intended):
```python
@register_value_caster(F16Type.static_typeid)
@register_value_caster(F32Type.static_typeid)
@register_value_caster(F64Type.static_typeid)
@register_value_caster(IntegerType.static_typeid)
class ArithValue(Value):
__add__ = partialmethod(_binary_op, op="add")
__sub__ = partialmethod(_binary_op, op="sub")
__mul__ = partialmethod(_binary_op, op="mul")
a = arith.constant(value=FloatAttr.get(f16_t, 42.42))
b = a + a
# CHECK: ArithValue(%0 = arith.addf %cst, %cst : f16)
print(b)
a = arith.constant(value=FloatAttr.get(f32_t, 42.42))
b = a - a
# CHECK: ArithValue(%1 = arith.subf %cst_0, %cst_0 : f32)
print(b)
a = arith.constant(value=FloatAttr.get(f64_t, 42.42))
b = a * a
# CHECK: ArithValue(%2 = arith.mulf %cst_1, %cst_1 : f64)
print(b)
```
**EDIT**: this now goes through the bindings and thus supports automatic
casting of `OpResult` (including as an element of `OpResultList`),
`BlockArgument` (including as an element of `BlockArgumentList`), as
well as `Value`.
2023-11-07 10:49:41 -06:00
|
|
|
|
|
|
|
|
auto typeCls =
|
2023-05-26 10:23:17 -05:00
|
|
|
mlir_type_subclass(m, "TestIntegerRankedTensorType",
|
|
|
|
|
mlirTypeIsARankedIntegerTensor,
|
2024-12-09 16:37:43 -05:00
|
|
|
nb::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
|
2023-05-26 10:23:17 -05:00
|
|
|
.attr("RankedTensorType"))
|
|
|
|
|
.def_classmethod(
|
|
|
|
|
"get",
|
2024-12-09 16:37:43 -05:00
|
|
|
[](const nb::object &cls, std::vector<int64_t> shape,
|
2023-05-26 10:23:17 -05:00
|
|
|
unsigned width, MlirContext ctx) {
|
|
|
|
|
MlirAttribute encoding = mlirAttributeGetNull();
|
|
|
|
|
return cls(mlirRankedTensorTypeGet(
|
|
|
|
|
shape.size(), shape.data(), mlirIntegerTypeGet(ctx, width),
|
|
|
|
|
encoding));
|
|
|
|
|
},
|
2024-12-09 16:37:43 -05:00
|
|
|
nb::arg("cls"), nb::arg("shape"), nb::arg("width"),
|
|
|
|
|
nb::arg("context").none() = nb::none());
|
[mlir][python] value casting (#69644)
This PR adds "value casting", i.e., a mechanism to wrap `ir.Value` in a
proxy class that overloads dunders such as `__add__`, `__sub__`, and
`__mul__` for fun and great profit.
This is thematically similar to
https://github.com/llvm/llvm-project/commit/bfb1ba752655bf09b35c486f6cc9817dbedfb1bb
and
https://github.com/llvm/llvm-project/commit/9566ee280607d91fa2e5eca730a6765ac84dfd0f.
The example in the test demonstrates the value of the feature (no pun
intended):
```python
@register_value_caster(F16Type.static_typeid)
@register_value_caster(F32Type.static_typeid)
@register_value_caster(F64Type.static_typeid)
@register_value_caster(IntegerType.static_typeid)
class ArithValue(Value):
__add__ = partialmethod(_binary_op, op="add")
__sub__ = partialmethod(_binary_op, op="sub")
__mul__ = partialmethod(_binary_op, op="mul")
a = arith.constant(value=FloatAttr.get(f16_t, 42.42))
b = a + a
# CHECK: ArithValue(%0 = arith.addf %cst, %cst : f16)
print(b)
a = arith.constant(value=FloatAttr.get(f32_t, 42.42))
b = a - a
# CHECK: ArithValue(%1 = arith.subf %cst_0, %cst_0 : f32)
print(b)
a = arith.constant(value=FloatAttr.get(f64_t, 42.42))
b = a * a
# CHECK: ArithValue(%2 = arith.mulf %cst_1, %cst_1 : f64)
print(b)
```
**EDIT**: this now goes through the bindings and thus supports automatic
casting of `OpResult` (including as an element of `OpResultList`),
`BlockArgument` (including as an element of `BlockArgumentList`), as
well as `Value`.
2023-11-07 10:49:41 -06:00
|
|
|
|
2024-12-09 16:37:43 -05:00
|
|
|
assert(nb::hasattr(typeCls.get_class(), "static_typeid") &&
|
2023-05-26 10:23:17 -05:00
|
|
|
"TestIntegerRankedTensorType has no static_typeid");
|
[mlir][python] value casting (#69644)
This PR adds "value casting", i.e., a mechanism to wrap `ir.Value` in a
proxy class that overloads dunders such as `__add__`, `__sub__`, and
`__mul__` for fun and great profit.
This is thematically similar to
https://github.com/llvm/llvm-project/commit/bfb1ba752655bf09b35c486f6cc9817dbedfb1bb
and
https://github.com/llvm/llvm-project/commit/9566ee280607d91fa2e5eca730a6765ac84dfd0f.
The example in the test demonstrates the value of the feature (no pun
intended):
```python
@register_value_caster(F16Type.static_typeid)
@register_value_caster(F32Type.static_typeid)
@register_value_caster(F64Type.static_typeid)
@register_value_caster(IntegerType.static_typeid)
class ArithValue(Value):
__add__ = partialmethod(_binary_op, op="add")
__sub__ = partialmethod(_binary_op, op="sub")
__mul__ = partialmethod(_binary_op, op="mul")
a = arith.constant(value=FloatAttr.get(f16_t, 42.42))
b = a + a
# CHECK: ArithValue(%0 = arith.addf %cst, %cst : f16)
print(b)
a = arith.constant(value=FloatAttr.get(f32_t, 42.42))
b = a - a
# CHECK: ArithValue(%1 = arith.subf %cst_0, %cst_0 : f32)
print(b)
a = arith.constant(value=FloatAttr.get(f64_t, 42.42))
b = a * a
# CHECK: ArithValue(%2 = arith.mulf %cst_1, %cst_1 : f64)
print(b)
```
**EDIT**: this now goes through the bindings and thus supports automatic
casting of `OpResult` (including as an element of `OpResultList`),
`BlockArgument` (including as an element of `BlockArgumentList`), as
well as `Value`.
2023-11-07 10:49:41 -06:00
|
|
|
|
|
|
|
|
MlirTypeID mlirRankedTensorTypeID = mlirRankedTensorTypeGetTypeID();
|
|
|
|
|
|
2024-12-09 16:37:43 -05:00
|
|
|
nb::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
|
|
|
|
|
.attr(MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR)(
|
|
|
|
|
mlirRankedTensorTypeID, nb::arg("replace") = true)(
|
|
|
|
|
nanobind::cpp_function([typeCls](const nb::object &mlirType) {
|
[mlir][python] value casting (#69644)
This PR adds "value casting", i.e., a mechanism to wrap `ir.Value` in a
proxy class that overloads dunders such as `__add__`, `__sub__`, and
`__mul__` for fun and great profit.
This is thematically similar to
https://github.com/llvm/llvm-project/commit/bfb1ba752655bf09b35c486f6cc9817dbedfb1bb
and
https://github.com/llvm/llvm-project/commit/9566ee280607d91fa2e5eca730a6765ac84dfd0f.
The example in the test demonstrates the value of the feature (no pun
intended):
```python
@register_value_caster(F16Type.static_typeid)
@register_value_caster(F32Type.static_typeid)
@register_value_caster(F64Type.static_typeid)
@register_value_caster(IntegerType.static_typeid)
class ArithValue(Value):
__add__ = partialmethod(_binary_op, op="add")
__sub__ = partialmethod(_binary_op, op="sub")
__mul__ = partialmethod(_binary_op, op="mul")
a = arith.constant(value=FloatAttr.get(f16_t, 42.42))
b = a + a
# CHECK: ArithValue(%0 = arith.addf %cst, %cst : f16)
print(b)
a = arith.constant(value=FloatAttr.get(f32_t, 42.42))
b = a - a
# CHECK: ArithValue(%1 = arith.subf %cst_0, %cst_0 : f32)
print(b)
a = arith.constant(value=FloatAttr.get(f64_t, 42.42))
b = a * a
# CHECK: ArithValue(%2 = arith.mulf %cst_1, %cst_1 : f64)
print(b)
```
**EDIT**: this now goes through the bindings and thus supports automatic
casting of `OpResult` (including as an element of `OpResultList`),
`BlockArgument` (including as an element of `BlockArgumentList`), as
well as `Value`.
2023-11-07 10:49:41 -06:00
|
|
|
return typeCls.get_class()(mlirType);
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
auto valueCls = mlir_value_subclass(m, "TestTensorValue",
|
|
|
|
|
mlirTypeIsAPythonTestTestTensorValue)
|
|
|
|
|
.def("is_null", [](MlirValue &self) {
|
|
|
|
|
return mlirValueIsNull(self);
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-09 16:37:43 -05:00
|
|
|
nb::module_::import_(MAKE_MLIR_PYTHON_QUALNAME("ir"))
|
[mlir][python] value casting (#69644)
This PR adds "value casting", i.e., a mechanism to wrap `ir.Value` in a
proxy class that overloads dunders such as `__add__`, `__sub__`, and
`__mul__` for fun and great profit.
This is thematically similar to
https://github.com/llvm/llvm-project/commit/bfb1ba752655bf09b35c486f6cc9817dbedfb1bb
and
https://github.com/llvm/llvm-project/commit/9566ee280607d91fa2e5eca730a6765ac84dfd0f.
The example in the test demonstrates the value of the feature (no pun
intended):
```python
@register_value_caster(F16Type.static_typeid)
@register_value_caster(F32Type.static_typeid)
@register_value_caster(F64Type.static_typeid)
@register_value_caster(IntegerType.static_typeid)
class ArithValue(Value):
__add__ = partialmethod(_binary_op, op="add")
__sub__ = partialmethod(_binary_op, op="sub")
__mul__ = partialmethod(_binary_op, op="mul")
a = arith.constant(value=FloatAttr.get(f16_t, 42.42))
b = a + a
# CHECK: ArithValue(%0 = arith.addf %cst, %cst : f16)
print(b)
a = arith.constant(value=FloatAttr.get(f32_t, 42.42))
b = a - a
# CHECK: ArithValue(%1 = arith.subf %cst_0, %cst_0 : f32)
print(b)
a = arith.constant(value=FloatAttr.get(f64_t, 42.42))
b = a * a
# CHECK: ArithValue(%2 = arith.mulf %cst_1, %cst_1 : f64)
print(b)
```
**EDIT**: this now goes through the bindings and thus supports automatic
casting of `OpResult` (including as an element of `OpResultList`),
`BlockArgument` (including as an element of `BlockArgumentList`), as
well as `Value`.
2023-11-07 10:49:41 -06:00
|
|
|
.attr(MLIR_PYTHON_CAPI_VALUE_CASTER_REGISTER_ATTR)(
|
|
|
|
|
mlirRankedTensorTypeID)(
|
2024-12-09 16:37:43 -05:00
|
|
|
nanobind::cpp_function([valueCls](const nb::object &valueObj) {
|
|
|
|
|
nb::object capsule = mlirApiObjectToCapsule(valueObj);
|
[mlir][python] value casting (#69644)
This PR adds "value casting", i.e., a mechanism to wrap `ir.Value` in a
proxy class that overloads dunders such as `__add__`, `__sub__`, and
`__mul__` for fun and great profit.
This is thematically similar to
https://github.com/llvm/llvm-project/commit/bfb1ba752655bf09b35c486f6cc9817dbedfb1bb
and
https://github.com/llvm/llvm-project/commit/9566ee280607d91fa2e5eca730a6765ac84dfd0f.
The example in the test demonstrates the value of the feature (no pun
intended):
```python
@register_value_caster(F16Type.static_typeid)
@register_value_caster(F32Type.static_typeid)
@register_value_caster(F64Type.static_typeid)
@register_value_caster(IntegerType.static_typeid)
class ArithValue(Value):
__add__ = partialmethod(_binary_op, op="add")
__sub__ = partialmethod(_binary_op, op="sub")
__mul__ = partialmethod(_binary_op, op="mul")
a = arith.constant(value=FloatAttr.get(f16_t, 42.42))
b = a + a
# CHECK: ArithValue(%0 = arith.addf %cst, %cst : f16)
print(b)
a = arith.constant(value=FloatAttr.get(f32_t, 42.42))
b = a - a
# CHECK: ArithValue(%1 = arith.subf %cst_0, %cst_0 : f32)
print(b)
a = arith.constant(value=FloatAttr.get(f64_t, 42.42))
b = a * a
# CHECK: ArithValue(%2 = arith.mulf %cst_1, %cst_1 : f64)
print(b)
```
**EDIT**: this now goes through the bindings and thus supports automatic
casting of `OpResult` (including as an element of `OpResultList`),
`BlockArgument` (including as an element of `BlockArgumentList`), as
well as `Value`.
2023-11-07 10:49:41 -06:00
|
|
|
MlirValue v = mlirPythonCapsuleToValue(capsule.ptr());
|
|
|
|
|
MlirType t = mlirValueGetType(v);
|
|
|
|
|
// This is hyper-specific in order to exercise/test registering a
|
|
|
|
|
// value caster from cpp (but only for a single test case; see
|
|
|
|
|
// testTensorValue python_test.py).
|
|
|
|
|
if (mlirShapedTypeHasStaticShape(t) &&
|
|
|
|
|
mlirShapedTypeGetDimSize(t, 0) == 1 &&
|
|
|
|
|
mlirShapedTypeGetDimSize(t, 1) == 2 &&
|
|
|
|
|
mlirShapedTypeGetDimSize(t, 2) == 3)
|
|
|
|
|
return valueCls.get_class()(valueObj);
|
|
|
|
|
return valueObj;
|
|
|
|
|
}));
|
2021-10-14 17:18:28 +02:00
|
|
|
}
|