Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-06 23:05:46 -07:00
|
|
|
//===- MainModule.cpp - Main pybind module --------------------------------===//
|
|
|
|
|
//
|
|
|
|
|
// 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
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2020-10-21 23:34:01 -07:00
|
|
|
#include "PybindUtils.h"
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-06 23:05:46 -07:00
|
|
|
|
2020-10-21 23:34:01 -07:00
|
|
|
#include "Globals.h"
|
2021-03-19 11:57:01 -07:00
|
|
|
#include "IRModule.h"
|
2020-11-10 18:39:12 +00:00
|
|
|
#include "Pass.h"
|
2024-06-11 07:45:12 -07:00
|
|
|
#include "Rewrite.h"
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-06 23:05:46 -07:00
|
|
|
|
2020-08-16 20:53:45 -07:00
|
|
|
namespace py = pybind11;
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-06 23:05:46 -07:00
|
|
|
using namespace mlir;
|
2023-05-26 10:23:17 -05:00
|
|
|
using namespace py::literals;
|
2020-08-16 20:53:45 -07:00
|
|
|
using namespace mlir::python;
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-06 23:05:46 -07:00
|
|
|
|
2020-10-21 23:34:01 -07:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
// Module initialization.
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
|
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-06 23:05:46 -07:00
|
|
|
PYBIND11_MODULE(_mlir, m) {
|
|
|
|
|
m.doc() = "MLIR Python Native Extension";
|
|
|
|
|
|
2021-08-23 20:01:07 -07:00
|
|
|
py::class_<PyGlobals>(m, "_Globals", py::module_local())
|
2020-10-21 23:34:01 -07:00
|
|
|
.def_property("dialect_search_modules",
|
|
|
|
|
&PyGlobals::getDialectSearchPrefixes,
|
|
|
|
|
&PyGlobals::setDialectSearchPrefixes)
|
2021-11-28 14:08:06 -08:00
|
|
|
.def(
|
|
|
|
|
"append_dialect_search_prefix",
|
|
|
|
|
[](PyGlobals &self, std::string moduleName) {
|
|
|
|
|
self.getDialectSearchPrefixes().push_back(std::move(moduleName));
|
|
|
|
|
},
|
2023-05-26 10:23:17 -05:00
|
|
|
"module_name"_a)
|
2023-11-03 13:28:20 -05:00
|
|
|
.def(
|
|
|
|
|
"_check_dialect_module_loaded",
|
|
|
|
|
[](PyGlobals &self, const std::string &dialectNamespace) {
|
|
|
|
|
return self.loadDialectModule(dialectNamespace);
|
|
|
|
|
},
|
|
|
|
|
"dialect_namespace"_a)
|
2020-10-21 23:34:01 -07:00
|
|
|
.def("_register_dialect_impl", &PyGlobals::registerDialectImpl,
|
2023-05-26 10:23:17 -05:00
|
|
|
"dialect_namespace"_a, "dialect_class"_a,
|
2020-10-21 23:34:01 -07:00
|
|
|
"Testing hook for directly registering a dialect")
|
|
|
|
|
.def("_register_operation_impl", &PyGlobals::registerOperationImpl,
|
[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
|
|
|
"operation_name"_a, "operation_class"_a, py::kw_only(),
|
|
|
|
|
"replace"_a = false,
|
2020-10-21 23:34:01 -07:00
|
|
|
"Testing hook for directly registering an operation");
|
|
|
|
|
|
|
|
|
|
// Aside from making the globals accessible to python, having python manage
|
|
|
|
|
// it is necessary to make sure it is destroyed (and releases its python
|
|
|
|
|
// resources) properly.
|
|
|
|
|
m.attr("globals") =
|
|
|
|
|
py::cast(new PyGlobals, py::return_value_policy::take_ownership);
|
|
|
|
|
|
|
|
|
|
// Registration decorators.
|
|
|
|
|
m.def(
|
|
|
|
|
"register_dialect",
|
2024-11-11 09:26:15 +01:00
|
|
|
[](py::type pyClass) {
|
2020-10-21 23:34:01 -07:00
|
|
|
std::string dialectNamespace =
|
|
|
|
|
pyClass.attr("DIALECT_NAMESPACE").cast<std::string>();
|
|
|
|
|
PyGlobals::get().registerDialectImpl(dialectNamespace, pyClass);
|
|
|
|
|
return pyClass;
|
|
|
|
|
},
|
2023-05-26 10:23:17 -05:00
|
|
|
"dialect_class"_a,
|
2020-10-21 23:34:01 -07:00
|
|
|
"Class decorator for registering a custom Dialect wrapper");
|
|
|
|
|
m.def(
|
|
|
|
|
"register_operation",
|
2024-11-11 09:26:15 +01:00
|
|
|
[](const py::type &dialectClass, bool replace) -> py::cpp_function {
|
2020-10-21 23:34:01 -07:00
|
|
|
return py::cpp_function(
|
2024-11-11 09:26:15 +01:00
|
|
|
[dialectClass, replace](py::type opClass) -> py::type {
|
2020-10-21 23:34:01 -07:00
|
|
|
std::string operationName =
|
|
|
|
|
opClass.attr("OPERATION_NAME").cast<std::string>();
|
[mlir][python] remove mixins (#68853)
This PR replaces the mixin `OpView` extension mechanism with the
standard inheritance mechanism.
Why? Firstly, mixins are not very pythonic (inheritance is usually used
for this), a little convoluted, and too "tight" (can only be used in the
immediately adjacent `_ext.py`). Secondly, it (mixins) are now blocking
are correct implementation of "value builders" (see
[here](https://github.com/llvm/llvm-project/pull/68764)) where the
problem becomes how to choose the correct base class that the value
builder should call.
This PR looks big/complicated but appearances are deceiving; 4 things
were needed to make this work:
1. Drop `skipDefaultBuilders` in
`OpPythonBindingGen::emitDefaultOpBuilders`
2. Former mixin extension classes are converted to inherit from the
generated `OpView` instead of being "mixins"
a. extension classes that simply were calling into an already generated
`super().__init__` continue to do so
b. (almost all) extension classes that were calling `self.build_generic`
because of a lack of default builder being generated can now also just
call `super().__init__`
3. To handle the [lone single
use-case](https://sourcegraph.com/search?q=context%3Aglobal+select_opview_mixin&patternType=standard&sm=1&groupBy=repo)
of `select_opview_mixin`, namely
[linalg](https://github.com/llvm/llvm-project/blob/main/mlir/python/mlir/dialects/_linalg_ops_ext.py#L38),
only a small change was necessary in `opdsl/lang/emitter.py` (thanks to
the emission/generation of default builders/`__init__`s)
4. since the `extend_opview_class` decorator is removed, we need a way
to register extension classes as the desired `OpView` that `op.opview`
conjures into existence; so we do the standard thing and just enable
replacing the existing registered `OpView` i.e.,
`register_operation(_Dialect, replace=True)`.
Note, the upgrade path for the common case is to change an extension to
inherit from the generated builder and decorate it with
`register_operation(_Dialect, replace=True)`. In the slightly more
complicated case where `super().__init(self.build_generic(...))` is
called in the extension's `__init__`, this needs to be updated to call
`__init__` in `OpView`, i.e., the grandparent (see updated docs).
Note, also `<DIALECT>_ext.py` files/modules will no longer be automatically loaded.
Note, the PR has 3 base commits that look funny but this was done for
the purpose of tracking the line history of moving the
`<DIALECT>_ops_ext.py` class into `<DIALECT>.py` and updating (commit
labeled "fix").
2023-10-19 16:20:14 -05:00
|
|
|
PyGlobals::get().registerOperationImpl(operationName, opClass,
|
|
|
|
|
replace);
|
2020-10-21 23:34:01 -07:00
|
|
|
|
|
|
|
|
// Dict-stuff the new opClass by name onto the dialect class.
|
|
|
|
|
py::object opClassName = opClass.attr("__name__");
|
|
|
|
|
dialectClass.attr(opClassName) = opClass;
|
|
|
|
|
return opClass;
|
|
|
|
|
});
|
|
|
|
|
},
|
[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
|
|
|
"dialect_class"_a, py::kw_only(), "replace"_a = false,
|
2021-11-28 14:08:06 -08:00
|
|
|
"Produce a class decorator for registering an Operation class as part of "
|
|
|
|
|
"a dialect");
|
2023-05-26 10:23:17 -05:00
|
|
|
m.def(
|
|
|
|
|
MLIR_PYTHON_CAPI_TYPE_CASTER_REGISTER_ATTR,
|
[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 mlirTypeID, bool replace) -> py::cpp_function {
|
|
|
|
|
return py::cpp_function([mlirTypeID,
|
|
|
|
|
replace](py::object typeCaster) -> py::object {
|
|
|
|
|
PyGlobals::get().registerTypeCaster(mlirTypeID, typeCaster, replace);
|
|
|
|
|
return typeCaster;
|
|
|
|
|
});
|
2023-05-26 10:23:17 -05:00
|
|
|
},
|
[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
|
|
|
"typeid"_a, py::kw_only(), "replace"_a = false,
|
2023-05-26 10:23:17 -05:00
|
|
|
"Register a type caster for casting MLIR types to custom user types.");
|
[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
|
|
|
m.def(
|
|
|
|
|
MLIR_PYTHON_CAPI_VALUE_CASTER_REGISTER_ATTR,
|
|
|
|
|
[](MlirTypeID mlirTypeID, bool replace) -> py::cpp_function {
|
|
|
|
|
return py::cpp_function(
|
|
|
|
|
[mlirTypeID, replace](py::object valueCaster) -> py::object {
|
|
|
|
|
PyGlobals::get().registerValueCaster(mlirTypeID, valueCaster,
|
|
|
|
|
replace);
|
|
|
|
|
return valueCaster;
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
"typeid"_a, py::kw_only(), "replace"_a = false,
|
|
|
|
|
"Register a value caster for casting MLIR values to custom user values.");
|
2020-10-21 23:34:01 -07:00
|
|
|
|
2020-08-16 18:49:28 -07:00
|
|
|
// Define and populate IR submodule.
|
|
|
|
|
auto irModule = m.def_submodule("ir", "MLIR IR Bindings");
|
2021-03-19 11:57:01 -07:00
|
|
|
populateIRCore(irModule);
|
|
|
|
|
populateIRAffine(irModule);
|
|
|
|
|
populateIRAttributes(irModule);
|
2021-10-14 17:18:28 +02:00
|
|
|
populateIRInterfaces(irModule);
|
2021-03-19 11:57:01 -07:00
|
|
|
populateIRTypes(irModule);
|
2020-11-10 18:39:12 +00:00
|
|
|
|
2024-06-11 07:45:12 -07:00
|
|
|
auto rewriteModule = m.def_submodule("rewrite", "MLIR Rewrite Bindings");
|
|
|
|
|
populateRewriteSubmodule(rewriteModule);
|
|
|
|
|
|
2020-11-10 18:39:12 +00:00
|
|
|
// Define and populate PassManager submodule.
|
|
|
|
|
auto passModule =
|
|
|
|
|
m.def_submodule("passmanager", "MLIR Pass Management Bindings");
|
|
|
|
|
populatePassManagerSubmodule(passModule);
|
Initial boiler-plate for python bindings.
Summary:
* Native '_mlir' extension module.
* Python mlir/__init__.py trampoline module.
* Lit test that checks a message.
* Uses some cmake configurations that have worked for me in the past but likely needs further elaboration.
Subscribers: mgorny, mehdi_amini, rriddle, jpienaar, shauheen, antiagainst, nicolasvasilache, arpith-jacob, mgester, lucyrfox, aartbik, liufengdb, stephenneuendorffer, Joonsoo, grosul1, Kayjukh, jurahul, msifontes
Tags: #mlir
Differential Revision: https://reviews.llvm.org/D83279
2020-07-06 23:05:46 -07:00
|
|
|
}
|