[Mlir-commits] [mlir] [MLIR][Python] Support Python-defined passes in MLIR (PR #156000)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Sep 8 01:42:11 PDT 2025


PragmaTwice wrote:

> We should preserve the ability of the system to generate reproducer: that is a pass is more than a callable and should be registered in a way that we can print a pass pipeline and replay it (that implies the ability to register it and a stable mnemnonic). Registering a pass is something you need to do independently from the pass manager, since you can build a pass manager from a pipeline. This is one of the important aspect of fitting into the pass manager, otherwise if you're just running functions on operations without more structure, you can just write it all in python without hooking into the MLIR pass management infrastructure.

Yeah I see your concern — a pass needs to be registered with a stable mnemonic and interoperate with the pass pipeline (for replay, etc.), rather than just being a bare function. That said, in Python a callable can just as well be a class implementing the `__call__` method, which makes it very easy to extend. For example, such a class could also provide other methods to integrate with the pass infrastructure (as optional callbacks in `ExternalPass`). What we currently have is just a minimal working entity, but it’s not difficult to extend it further.

Already, we can mix Python passes with C++ passes in the same pipeline (as shown in `test/python/python_pass.py`), and use `enable_ir_printing` to dump the IR before and after each pass. We’ve also had some discussions around pass cloning; it’s not implemented yet, but it’s certainly possible to add in Python.

https://github.com/llvm/llvm-project/pull/156000


More information about the Mlir-commits mailing list