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

Rolf Morel llvmlistbot at llvm.org
Mon Dec 8 02:21:15 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:
----------------
rolfmorel wrote:

Supposing these op definitions inherited from `OpView`, could we have the decorator synthesize the `__init__` (and maybe `build`) method(s) directly on the op definition class? This is even a pattern in Python's stdlib, e.g. `@dataclass`, so scores low on the magic meter for me.

Alternatively, synthesize a class that inherits from `OpView` and has the appropriate default `__init__` method (and methods for properties etc.) and put it first in the MRO of the op definition class. The advantage of this is that users' implementation of a `method(...)` can call `super().method(...)` in their implementation in case they only want to depart from default behaviour a little.

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


More information about the Mlir-commits mailing list