Skip to content

Conversation

PragmaTwice
Copy link
Member

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.

@llvmbot
Copy link
Member

llvmbot commented Oct 10, 2025

@llvm/pr-subscribers-mlir

Author: Twice (PragmaTwice)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/162833.diff

1 Files Affected:

  • (modified) mlir/docs/Bindings/Python.md (+20)
diff --git a/mlir/docs/Bindings/Python.md b/mlir/docs/Bindings/Python.md
index 98ac635aa4ee2..893c6d48a88d2 100644
--- a/mlir/docs/Bindings/Python.md
+++ b/mlir/docs/Bindings/Python.md
@@ -1188,6 +1188,26 @@ which can be `import`ed  from the main dialect file, i.e.
 `python/mlir/dialects/<dialect-namespace>/passes.py` if it is undesirable to
 make the passes available along with the dialect.
 
+Passes can be defined as Python callables via the `PassManager.add` API.
+In such case, the callable is wrapped as an `mlir::Pass` internally and
+executed as part of the pass pipeline when `PassManager.run` is invoked.
+In the callable, the `op` parameter represents the current operation being transformed,
+while the `pass_` parameter provides access to the current `Pass` object,
+allowing actions such as `signalPassFailure()`.
+The lifetime of the callable is extended at least until the `PassManager` is destroyed.
+The following example code demonstrates how to define Python passes.
+
+```python
+def demo_pass(op, pass_):
+    # do something with the given op
+    pass
+
+pm = PassManager('any')
+pm.add(demo_pass)
+pm.add('some-cpp-defined-passes')
+...
+pm.run(some_op)
+```
 
 ### Other functionality
 

Copy link
Contributor

@makslevental makslevental left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@PragmaTwice
Copy link
Member Author

Merging...

@PragmaTwice PragmaTwice merged commit 19b9b54 into llvm:main Oct 11, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants