[Mlir-commits] [mlir] [MLIR][Python] Add a DSL for defining IRDL dialects in Python bindings (PR #169045)

Rolf Morel llvmlistbot at llvm.org
Fri Dec 5 11:49:00 PST 2025


================
@@ -0,0 +1,308 @@
+# RUN: %PYTHON %s 2>&1 | FileCheck %s
+
+from mlir.ir import *
+from mlir.dialects.irdl import dsl as irdsl
+from mlir.dialects import arith
+import sys
+
+
+def run(f):
+    print("\nTEST:", f.__name__, file=sys.stderr)
+    with Context():
+        f()
+
+
+# CHECK: TEST: testMyInt
+ at run
+def testMyInt():
+    myint = irdsl.Dialect("myint")
+    iattr = irdsl.BaseName("#builtin.integer")
+    i32 = irdsl.IsType(IntegerType.get_signless(32))
+
+    @myint.op("constant")
+    class ConstantOp:
+        value = irdsl.Attribute(iattr)
+        cst = irdsl.Result(i32)
----------------
rolfmorel wrote:

I am just trying to think through if there's any value in making use of inheritance. E.g.
```python
    @myint.register_operation
    class ConstantOp(irdsl.Operation):
        name = "constant"
        value = irdsl.Attribute(iattr)
        cst = irdsl.Result(i32)
```
or even terser:
```python
    class ConstantOp(myint.Operation):
        name = "constant"
        value = irdsl.Attribute(iattr)
        cst = irdsl.Result(i32)
```
The latter is pretty bad as now it's not obvious that the `myint` dialect object will learn about the new `ConstantOp` class (through some meta-programming magic).

Not suggesting either option here. Just wanted to check if you have any opinion. Also in the context of these ops hopefully being able to implement interfaces soon (and presumably asserting that they do through subclassing the relevant interface classes).

https://github.com/llvm/llvm-project/pull/169045


More information about the Mlir-commits mailing list