Skip to content

Commit d1fdb41

Browse files
authored
[MLIR][Python] Add method for getting the live operation objects (#78663)
Currently, a method exists to get the count of the operation objects which are still alive. This helps for sanity checking, but isn't terribly useful for debugging. This new method returns the actual operation objects which are still alive. This allows Python code like the following: ``` gc.collect() live_ops = ir.Context.current._get_live_operation_objects() for op in live_ops: print(f"Warning: {op} is still live. Referrers:") for referrer in gc.get_referrers(op)[0]: print(f" {referrer}") ```
1 parent e5924d6 commit d1fdb41

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,13 @@ size_t PyMlirContext::getLiveCount() { return getLiveContexts().size(); }
636636

637637
size_t PyMlirContext::getLiveOperationCount() { return liveOperations.size(); }
638638

639+
std::vector<PyOperation *> PyMlirContext::getLiveOperationObjects() {
640+
std::vector<PyOperation *> liveObjects;
641+
for (auto &entry : liveOperations)
642+
liveObjects.push_back(entry.second.second);
643+
return liveObjects;
644+
}
645+
639646
size_t PyMlirContext::clearLiveOperations() {
640647
for (auto &op : liveOperations)
641648
op.second.second->setInvalid();
@@ -2546,6 +2553,8 @@ void mlir::python::populateIRCore(py::module &m) {
25462553
return ref.releaseObject();
25472554
})
25482555
.def("_get_live_operation_count", &PyMlirContext::getLiveOperationCount)
2556+
.def("_get_live_operation_objects",
2557+
&PyMlirContext::getLiveOperationObjects)
25492558
.def("_clear_live_operations", &PyMlirContext::clearLiveOperations)
25502559
.def("_get_live_module_count", &PyMlirContext::getLiveModuleCount)
25512560
.def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,

mlir/lib/Bindings/Python/IRModule.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ class PyMlirContext {
201201
/// Gets the count of live context objects. Used for testing.
202202
static size_t getLiveCount();
203203

204+
/// Get a list of Python objects which are still in the live context map.
205+
std::vector<PyOperation *> getLiveOperationObjects();
206+
204207
/// Gets the count of live operations associated with this context.
205208
/// Used for testing.
206209
size_t getLiveOperationCount();

mlir/python/mlir/_mlir_libs/_mlir/ir.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,7 @@ class Context:
985985
def _get_context_again(self) -> Context: ...
986986
def _get_live_module_count(self) -> int: ...
987987
def _get_live_operation_count(self) -> int: ...
988+
def _get_live_operation_objects(self) -> List[Operation]: ...
988989
def append_dialect_registry(self, registry: DialectRegistry) -> None: ...
989990
def attach_diagnostic_handler(
990991
self, callback: Callable[[Diagnostic], bool]

mlir/test/python/ir/module.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ def testModuleOperation():
105105
assert ctx._get_live_module_count() == 1
106106
op1 = module.operation
107107
assert ctx._get_live_operation_count() == 1
108+
live_ops = ctx._get_live_operation_objects()
109+
assert len(live_ops) == 1
110+
assert live_ops[0] is op1
111+
live_ops = None
108112
# CHECK: module @successfulParse
109113
print(op1)
110114

0 commit comments

Comments
 (0)