Skip to content

[MLIR][Docs] Add docs about Python-defined dialects#181372

Merged
PragmaTwice merged 3 commits into
mainfrom
users/pragmatwice/mlir-docs-python-dialects
Feb 13, 2026
Merged

[MLIR][Docs] Add docs about Python-defined dialects#181372
PragmaTwice merged 3 commits into
mainfrom
users/pragmatwice/mlir-docs-python-dialects

Conversation

@PragmaTwice
Copy link
Copy Markdown
Member

This PR adds documentation to the MLIR Python bindings introducing support for Python-defined dialects (initially introduced in #169045).

@llvmbot
Copy link
Copy Markdown
Member

llvmbot commented Feb 13, 2026

@llvm/pr-subscribers-mlir

Author: Twice (PragmaTwice)

Changes

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 --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).

@PragmaTwice PragmaTwice merged commit 424686a into main Feb 13, 2026
8 of 9 checks passed
@PragmaTwice PragmaTwice deleted the users/pragmatwice/mlir-docs-python-dialects branch February 13, 2026 15:49
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