[MLIR][Docs] Add docs about Python-defined dialects#181372
Merged
Merged
Conversation
Member
|
@llvm/pr-subscribers-mlir Author: Twice (PragmaTwice) ChangesThis PR adds documentation to the MLIR Python bindings introducing support for Python-defined dialects (initially introduced in #169045). Full diff: https://github.com/llvm/llvm-project/pull/181372.diff 1 Files Affected:
diff --git a/mlir/docs/Bindings/Python.md b/mlir/docs/Bindings/Python.md
index f75febd1a06e0..3af91e7a4cba8 100644
--- a/mlir/docs/Bindings/Python.md
+++ b/mlir/docs/Bindings/Python.md
@@ -1293,7 +1293,41 @@ The following sections outline how each of these can be implemented.
### Dialects
-Dialects can be defined through the IRDL dialect bindings in Python.
+The `mlir.dialects.ext` module provides support for defining Python-defined dialects.
+Users can define a new dialect (e.g., `MyDialect`) by subclassing the `Dialect` class,
+and define operations in that dialect by subclassing `MyDialect.Operation`.
+The dialect can then be loaded into MLIR by calling `MyDialect.load()` within a valid `Context`.
+After loading, these operations can be used just like other `OpView` subclasses.
+
+The following example shows how to define a dialect and construct IR using the newly defined ops.
+
+```python
+class MyInt(Dialect, name="myint"):
+ pass
+
+class ConstantOp(MyInt.Operation, name="constant"):
+ value: IntegerAttr
+ cst: Result[IntegerType[32]]
+
+class AddOp(MyInt.Operation, name="add"):
+ lhs: Operand[IntegerType[32]]
+ rhs: Operand[IntegerType[32]]
+ res: Result[IntegerType[32]]
+
+# The code below requires an available MLIR context and location.
+
+MyInt.load()
+
+module = Module.create()
+with InsertionPoint(module.body):
+ two = ConstantOp(IntegerAttr.get(i32, 2))
+ three = ConstantOp(IntegerAttr.get(i32, 3))
+ add1 = AddOp(two, three)
+ add2 = AddOp(add1, two)
+ add3 = AddOp(add2, three)
+```
+
+Dialects can also be defined through the IRDL dialect bindings in Python.
The IRDL bindings offer a `load_dialects` function that
converts an MLIR module containing `irdl.dialect` ops into MLIR dialects.
For further details, see the documentation of [the IRDL dialect](../Dialects/IRDL.md).
|
makslevental
approved these changes
Feb 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR adds documentation to the MLIR Python bindings introducing support for Python-defined dialects (initially introduced in #169045).