PSA: Breaking change for Python bindings `OpView` extensions

See [mlir][python] remove mixins by makslevental · Pull Request #68853 · llvm/llvm-project · GitHub

TLDR: the “mixin” mechanism for extending generated builders has been replaced with a (hopefully) simpler inheritance mechanism.

If you’re just using upstream extensions you don’t need to do anything.

If you have some of your own extensions that look like

class ConstantOpExt:
    def __init__(
        self, result: Type, value: Union[int, float], *, loc=None, ip=None
    ):
        if isinstance(value, int):
            super().__init__(IntegerAttr.get(result, value), loc=loc, ip=ip)

(i.e., extending arith.ConstantOp) then you only need to

  1. inherit directly from the upstream builder;
  2. decorate your extension class with @_cext.register_operation(_Dialect, replace=True).

I.e., your new extension will need to look like this:

from mlir.dialects._arith_ops_gen import _Dialect, ConstantOp
from mlir.dialects._ods_common import _cext

@_cext.register_operation(_Dialect, replace=True)
class ConstantOpExt(ConstantOp):
    def __init__(
        self, result: Type, value: Union[int, float], *, loc=None, ip=None
    ):
        if isinstance(value, int):
            super().__init__(IntegerAttr.get(result, value), loc=loc, ip=ip)

As a benefit of this refactor, you no longer need to put this extension class in a special <DIALECT>_ext.py file - you can put it anywhere. Note, as a corollary, <DIALECT>_ext.py files/modules will no longer be automatically loaded.

3 Likes

Thank you for this. As an aside, did you ever manage to figure out how to make new members abd constructors on extension classes appear in auto complete UIs?