[Mlir-commits] [mlir] [MLIR][Python] remove `liveOperations` (PR #155114)

Maksim Levental llvmlistbot at llvm.org
Tue Sep 23 22:03:56 PDT 2025


makslevental wrote:

> We'd just have to track the set of all `Operation`s which have been created (either constructed from Python or by the bindings while crawling the IR) and haven't been deconstructed yet. Seems like that would be easy in the C++ bindings, wouldn't take up more space than `liveOperations`, and invalidations would be the responsibility of the user.

The whole reason for removing `liveOperations` was because keeping it up to date caused lots of issues: https://github.com/llvm/llvm-project/pull/139721, https://github.com/llvm/llvm-project/pull/93339, https://github.com/llvm/llvm-project/issues/69730, https://github.com/llvm/llvm-project/issues/92344, https://github.com/llvm/llvm-project/issues/63916. So while I'm generally supportive of sharp edges which are the user's responsibility to be cautious about but this one seems just fundamentally unsustainable. 

I can suggest two workarounds:

1. You should actually be able to recover all currently live operations/opviews using something like
    ```python
    import gc
    for obj in gc.get_objects():
        if isinstance(obj, (Operation, OpView)):
            print(obj)
     ```
     but actually it's not working for me right now (`gc.is_tracked` returns `False` for all ops/opviews and I don't understand why). I will try again tomorrow.
2. You can monkey-patch `OpView.__init__` to both init your opview *and* add it to a global dictionary:
    ```python
    my_live_operation_map = set()
    old_init = OpView.__init__
    def __init__(self, *args, **kwargs):
        old_init(self, *args, **kwargs)
        my_live_operation_map.add(self)
     OpView.__init__ = __init__
    ```
    Assuming you're loading [dialects correctly](https://github.com/llvm/llvm-project/blob/a40f47c9725f3aed6752f050f3010f3701d0cff7/mlir/lib/Bindings/Python/IRModule.cpp#L40) this will work both for `OpView`s you create in python yourself (i.e., `from circt_ops_gen import Op1; op = Op1()`) and opviews created for you by the bindings (by doing `op.operation.opview`).

Now I don't want to insist because I/we broke you and that's bad. So if these workarounds aren't sufficient then I can keep pondering this and if worse comes to worst we can restore `liveOperationsMap` in some limited form/fashion. But just reiterating: I would very much prefer to keep it fully removed.

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


More information about the Mlir-commits mailing list