[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 02:42:32 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:
This is the approach I have in mind so far, and I’d like to ask for everyone’s comments:
Given an input class like
```python
class A:
lhs = Operand()
rhs = Operand()
res = Result()
def __init__(self, x, y):
super().__init__(f32, x, y)
@property
def lhs(self):
# do some check and return lhs
```
We would first generate a class `AViewBase`, which does not contain any user-defined methods:
```python
class AViewBase(OpView):
def __init__(res, lhs, rhs):
...
# call OpView.build_generic(...)
@property
def lhs(self): ...
@property
def rhs(self): ...
```
Then, we generate another class `AView` that includes the user-defined methods, and this class will replace the original `A`:
```python
class AView(AViewBase):
def __init__(self, x, y):
super().__init__(f32, x, y)
@property
def lhs(self):
# do some check and return lhs
```
I think the benefit of this design is that users can very easily customize `__init__` or properties (while still taking advantage of the automatically generated methods inside them).
However, I think this feature can probably be deferred to a later PR, since this change might introduce more code and make the current PR even larger. For now, we can focus on the design issues of this PR rather than adding new features.
https://github.com/llvm/llvm-project/pull/169045
More information about the Mlir-commits
mailing list