Skip to content

Commit 19b9b54

Browse files
authored
[MLIR][Docs] Add docs for Python-defined pass in Python bindings (#162833)
Python-defined passes have been merged into the main branch for some time now. I believe adding a corresponding section in the documentation will help more users learn about this feature and understand how to use it. This PR adds such a section to the docs of Python bindings, summarizing the feature and providing an example.
1 parent cb4fdc0 commit 19b9b54

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

mlir/docs/Bindings/Python.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,26 @@ which can be `import`ed from the main dialect file, i.e.
11881188
`python/mlir/dialects/<dialect-namespace>/passes.py` if it is undesirable to
11891189
make the passes available along with the dialect.
11901190

1191+
Passes can be defined as Python callables via the `PassManager.add` API.
1192+
In such case, the callable is wrapped as an `mlir::Pass` internally and
1193+
executed as part of the pass pipeline when `PassManager.run` is invoked.
1194+
In the callable, the `op` parameter represents the current operation being transformed,
1195+
while the `pass_` parameter provides access to the current `Pass` object,
1196+
allowing actions such as `signalPassFailure()`.
1197+
The lifetime of the callable is extended at least until the `PassManager` is destroyed.
1198+
The following example code demonstrates how to define Python passes.
1199+
1200+
```python
1201+
def demo_pass(op, pass_):
1202+
# do something with the given op
1203+
pass
1204+
1205+
pm = PassManager('any')
1206+
pm.add(demo_pass)
1207+
pm.add('some-cpp-defined-passes')
1208+
...
1209+
pm.run(some_op)
1210+
```
11911211

11921212
### Other functionality
11931213

0 commit comments

Comments
 (0)