[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
Wed Dec 10 04:01:52 PST 2025


PragmaTwice wrote:

Demo in the PR description after the refactor:
```python
    myint = irdsl.Dialect("myint")
    iattr = irdsl.BaseName("#builtin.integer")
    i32 = irdsl.Is(IntegerType.get_signless(32))

    class ConstantOp(myint.Operation, name="constant"):
        value = irdsl.Attribute(iattr)
        cst = irdsl.Result(i32)

    class AddOp(myint.Operation, name="add"):
        lhs = irdsl.Operand(i32)
        rhs = irdsl.Operand(i32)
        res = irdsl.Result(i32)

    with Location.unknown():
        myint.load()

    i32 = IntegerType.get_signless(32)
    with Location.unknown():
        module = Module.create()
        with InsertionPoint(module.body):
            two = ConstantOp(i32, IntegerAttr.get(i32, 2))
            three = ConstantOp(i32, IntegerAttr.get(i32, 3))
            add1 = AddOp(i32, two, three)
            add2 = AddOp(i32, add1, two)
            add3 = AddOp(i32, add2, three)
```

This implementation doesn’t use decorators or metaclasses; instead, it relies on `__init_subclass__`.
I briefly experimented with a metaclass, but found it unsuitable: `ir.OpView` already has a metaclass `nb_type_0` (defined by nanobind), which conflicts with the metaclass defined in `irdsl` (and we also can’t inherit from that metaclass due to constraints imposed by nanobind).

I previously used `name` as a class attribute, but that turned out to shadow the existing `name` property on `OpView`, so the current approach is to treat `name` as a “class parameter” (I’m not sure if that’s the right term).

All in all, I think the current approach may be easier to extend than the previous one—for example, you can add custom methods and attributes to the `Operation` class.


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


More information about the Mlir-commits mailing list