[all-commits] [llvm/llvm-project] b2a736: [MLIR][Python] remove `liveOperations` (#155114)

Maksim Levental via All-commits all-commits at lists.llvm.org
Mon Sep 1 21:53:54 PDT 2025


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: b2a7369631d70df8bcd0c4ee304afa30a0657ba7
      https://github.com/llvm/llvm-project/commit/b2a7369631d70df8bcd0c4ee304afa30a0657ba7
  Author: Maksim Levental <maksim.levental at gmail.com>
  Date:   2025-09-01 (Mon, 01 Sep 2025)

  Changed paths:
    M mlir/docs/Bindings/Python.md
    M mlir/include/mlir-c/IR.h
    M mlir/lib/Bindings/Python/IRCore.cpp
    M mlir/lib/Bindings/Python/IRModule.h
    M mlir/lib/Bindings/Python/Pass.cpp
    M mlir/lib/Bindings/Python/TransformInterpreter.cpp
    M mlir/lib/CAPI/IR/IR.cpp
    M mlir/test/python/ir/module.py
    M mlir/test/python/ir/operation.py
    M mlir/test/python/ir/symbol_table.py
    M mlir/test/python/pass_manager.py

  Log Message:
  -----------
  [MLIR][Python] remove `liveOperations` (#155114)

Historical context: `PyMlirContext::liveOperations` was an optimization
meant to cut down on the number of Python object allocations and
(partially) a mechanism for updating validity of ops after
transformation. E.g. during walking/transforming the AST. See original
patch [here](https://reviews.llvm.org/D87958).

Inspired by a
[renewed](https://github.com/llvm/llvm-project/pull/139721#issuecomment-3217131918)
interest in https://github.com/llvm/llvm-project/pull/139721 (which has
become a little stale...)

<p align="center">
<img width="504" height="375" alt="image"
src="https://github.com/user-attachments/assets/0daad562-d3d1-4876-8d01-5dba382ab186"
/>
</p>

In the previous go-around
(https://github.com/llvm/llvm-project/pull/92631) there were two issues
which have been resolved

1. ops that were "fetched" under a root op which has been transformed
are no longer reported as invalid. We simply "[formally
forbid](https://github.com/llvm/llvm-project/pull/92631#issuecomment-2119397018)"
this;
2. `Module._CAPICreate(module_capsule)` must now be followed by a
`module._clear_mlir_module()` to prevent double-freeing of the actual
`ModuleOp` object (i.e. calling the dtor on the
`OwningOpRef<ModuleOp>`):

     ```python
    module = ...
    module_dup = Module._CAPICreate(module._CAPIPtr)
    module._clear_mlir_module()
    ```
- **the alternative choice** here is to remove the `Module._CAPICreate`
API altogether and replace it with something like `Module._move(module)`
which will do both `Module._CAPICreate` and `module._clear_mlir_module`.

Note, the other approach I explored last year was a [weakref
system](https://github.com/llvm/llvm-project/pull/97340) for
`mlir::Operation` which would effectively hoist this `liveOperations`
thing into MLIR core. Possibly doable but I now believe it's a bad idea.

The other potentially breaking change is `is`, which checks object
equality rather than value equality, will now report `False` because we
are always allocating `new` Python objects (ie that's the whole point of
this change). Users wanting to check equality for `Operation` and
`Module` should use `==`.



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list