[Mlir-commits] [mlir] [MLIR][Python] Add a function to register python-defined passes (PR #157850)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Sep 10 05:56:43 PDT 2025
PragmaTwice wrote:
While simple test cases work well for the current `register_pass`, there are still something we should consider:
Currently the python callable is actually "shared" between all pipelines (in all instances of the pass registered with the callable). For example:
```python
def my_pass(op, pass_):
...
register_pass("my-pass", my_pass)
pm1.add("my-pass ... my-pass")
...
pm2.add("my-pass")
```
In the example above, the object `my_pass` is shared between the three instance of that pass in these two pass managers. For simple passes (e.g. passes with no state) it should be fine, but for complicated passes, e.g.
```python
class AdvancedPass:
def __init__(self):
self.count = 0
def __call__(self, op, pass_):
self.count += 1
```
In this example the `self.count` will be increased to 2 and 3 rather than always be 1. And this is not expected for most users since each time we created a new ExternalPass by the factory lambda. Hence from my side, the python object need to be deepcopied before constructing the ExternalPass.
https://github.com/llvm/llvm-project/pull/157850
More information about the Mlir-commits
mailing list