[Mlir-commits] [mlir] [MLIR][Python] Support Python-defined passes in MLIR (PR #156000)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Mon Sep 1 20:29:31 PDT 2025
================
@@ -52,6 +129,26 @@ class PyPassManager {
} // namespace
+void mlir::python::populatePassSubmodule(nanobind::module_ &m) {
+ //----------------------------------------------------------------------------
+ // Mapping of the Python-defined Pass interface
+ //----------------------------------------------------------------------------
+ nb::class_<PyPassBase, PyPass>(m, "Pass")
----------------
PragmaTwice wrote:
Ahh this is a quite interesting case that I just did some research on : )
Refer to the nanobind documentation (https://nanobind.readthedocs.io/en/latest/api_core.html#_CPPv4I0DpEN8nanobind6class_E):
> template<typename T, typename ...Ts>
class class_ : public object
>
> The variable length parameter [Ts](https://nanobind.readthedocs.io/en/latest/api_core.html#_CPPv4I0DpEN8nanobind6class_E) is optional and can be used to specify the base class of [T](https://nanobind.readthedocs.io/en/latest/api_core.html#_CPPv4I0DpEN8nanobind6class_E) and/or an alias needed to realize [trampoline classes](https://nanobind.readthedocs.io/en/latest/classes.html#trampolines).
So in the case you mentioned ([`<PyOperation, PyOperationBase>`](https://github.com/llvm/llvm-project/blob/7cc8a50c03f2c139070358fb55db925b8b6ceb9d/mlir/lib/Bindings/Python/IRCore.cpp#L3495)), the parameter `Ts` (`PyOperationBase`) is a base class of `T` (`PyOperation`); and for the case here (`<PyPassBase, PyPass>`), the `Ts` (`PyPass`) is a trampoline class of `T` (`PyPassBase`). So I think both of them is correct here.
For example, in the documentation of trampoline classes (https://nanobind.readthedocs.io/en/latest/classes.html#overriding-virtual-functions-in-python), we can see such an instance of `nb::class_`:
```
nb::class_<Dog, PyDog /* <-- trampoline */>(m, "Dog")
```
And here `PyDog` is a derived class of `Dog` but also a trampoline class of `Dog` : )
https://github.com/llvm/llvm-project/pull/156000
More information about the Mlir-commits
mailing list