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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Dec 8 07:03:10 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:
----------------
PragmaTwice wrote:

> versus just inserting AViewBase in the MRO of A?

Sorry, there was an error in my previous example. The `lhs` in class `A` was redefined, which causes the `Operand` to be lost. Similarly, if we simply use `AViewBase` as the base class of `A`, then `lhs`, `rhs`, etc. in `A` seem to override the properties generated in `AViewBase`. Note that I’m not a Python expert, so please feel free to correct me.

> Use of super() on classes which do not extend any classes is confusing, IMO.

I understand your point; explicitly inheriting from a base class does seem to reduce some confusion (though not all of it). I’ve actually been thinking about reimplementing this DSL using a metaclass instead of a class decorator, but I’m not really sure what benefits that would bring in terms of implementation and user experience respectively (one obvious benefit is that no decorator is needed, and a base class alone would suffice). This is probably something that would need some Python experts to evaluate.


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


More information about the Mlir-commits mailing list