[Mlir-commits] [mlir] [MLIR][Docs] Add docs about Python-defined dialects (PR #181372)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Fri Feb 13 07:21:19 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: Twice (PragmaTwice)
<details>
<summary>Changes</summary>
This 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:
- (modified) mlir/docs/Bindings/Python.md (+35-1)
``````````diff
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).
``````````
</details>
https://github.com/llvm/llvm-project/pull/181372
More information about the Mlir-commits
mailing list