[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 12 08:46:05 PST 2025
================
@@ -0,0 +1,300 @@
+# 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.Is(IntegerType.get_signless(32))
+
+ class ConstantOp(myint.Operation, name="constant"):
----------------
rolfmorel wrote:
A "further out" suggestion is to push on dialects as namespaces and thereby potentially help IDEs/type checkers:
```python
from mlir.dialects.irdl import dsl as irdsl
class MyIntDialect(irdsl.Dialect, name="my_int"):
def verifyOperationAttribute(self, op, attr):
...
class ConstantOp(__class__.Operation, name="constant"):
value = irdsl.Attribute(iattr)
cst = irdsl.Result(i32)
my_int = MyIntDialect()
with ir.Context(), ir.Location.unknown():
my_int.load()
assert(isinstance(my_int.constant(...), MyIntDialect.ConstantOp))
```
Note that this still doesn't help with inferring the signature of `my_int.constant`. 😥
https://github.com/llvm/llvm-project/pull/169045
More information about the Mlir-commits
mailing list