[Mlir-commits] [mlir] [MLIR][Python] Add a DSL for defining IRDL dialects in Python bindings (PR #169045)
Rolf Morel
llvmlistbot at llvm.org
Sat Dec 13 08:52:20 PST 2025
rolfmorel wrote:
Based on @superlopuh reference, should we maybe be adhering more closely to the syntax used by Python's `@dataclass` classes? That is, state the constraints as _types_ of the fields rather than the fields' _values_. Given that types are essentially constraints, this makes quite a bit of sense to me actually.
For example (note the `:`s rather than `=`s):
```python
class ConstantOp(myint.Operation, name="constant"):
value: irdsl.Attribute(iattr)
cst: irdsl.Result(i32)
```
Default values could then look like:
```python
class ConstantOp(myint.Operation, name="constant"):
value: irdsl.Attribute(iattr) = 0
cst: irdsl.Result(i32)
```
here we would need to rely on that the class's `__init__` is able to coerce the Python value to the appropriate `mlir.ir.Attribute`.
More generally, we could adopt the `dataclasses`'s `field` mechanism to allow for appropriate "just-in-time" construction of the default value, i.e. when the class's `__init__` is actually called (which will be within an appropriate `mlir.ir.Context`):
```python
from mlir.dialects.irdl import field, dsl as irdsl # field is just a re-export of `dataclasses.field`
class ConstantOp(myint.Operation, name="constant"):
value: irdsl.Attribute(iattr) = field(default_factory=lambda: ir.IntegerAttr.get(ir.i32Type.get(), 0))
cst: irdsl.Result(i32)
```
https://github.com/llvm/llvm-project/pull/169045
More information about the Mlir-commits
mailing list