Skip to content

Commit

Permalink
[MLIR][Python] Add method for getting the live operation objects (llv…
Browse files Browse the repository at this point in the history
…m#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}")
```
  • Loading branch information
teqdruid authored Feb 8, 2024
1 parent e5924d6 commit d1fdb41
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions mlir/lib/Bindings/Python/IRCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,13 @@ size_t PyMlirContext::getLiveCount() { return getLiveContexts().size(); }

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

std::vector<PyOperation *> PyMlirContext::getLiveOperationObjects() {
std::vector<PyOperation *> liveObjects;
for (auto &entry : liveOperations)
liveObjects.push_back(entry.second.second);
return liveObjects;
}

size_t PyMlirContext::clearLiveOperations() {
for (auto &op : liveOperations)
op.second.second->setInvalid();
Expand Down Expand Up @@ -2546,6 +2553,8 @@ void mlir::python::populateIRCore(py::module &m) {
return ref.releaseObject();
})
.def("_get_live_operation_count", &PyMlirContext::getLiveOperationCount)
.def("_get_live_operation_objects",
&PyMlirContext::getLiveOperationObjects)
.def("_clear_live_operations", &PyMlirContext::clearLiveOperations)
.def("_get_live_module_count", &PyMlirContext::getLiveModuleCount)
.def_property_readonly(MLIR_PYTHON_CAPI_PTR_ATTR,
Expand Down
3 changes: 3 additions & 0 deletions mlir/lib/Bindings/Python/IRModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ class PyMlirContext {
/// Gets the count of live context objects. Used for testing.
static size_t getLiveCount();

/// Get a list of Python objects which are still in the live context map.
std::vector<PyOperation *> getLiveOperationObjects();

/// Gets the count of live operations associated with this context.
/// Used for testing.
size_t getLiveOperationCount();
Expand Down
1 change: 1 addition & 0 deletions mlir/python/mlir/_mlir_libs/_mlir/ir.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,7 @@ class Context:
def _get_context_again(self) -> Context: ...
def _get_live_module_count(self) -> int: ...
def _get_live_operation_count(self) -> int: ...
def _get_live_operation_objects(self) -> List[Operation]: ...
def append_dialect_registry(self, registry: DialectRegistry) -> None: ...
def attach_diagnostic_handler(
self, callback: Callable[[Diagnostic], bool]
Expand Down
4 changes: 4 additions & 0 deletions mlir/test/python/ir/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def testModuleOperation():
assert ctx._get_live_module_count() == 1
op1 = module.operation
assert ctx._get_live_operation_count() == 1
live_ops = ctx._get_live_operation_objects()
assert len(live_ops) == 1
assert live_ops[0] is op1
live_ops = None
# CHECK: module @successfulParse
print(op1)

Expand Down

0 comments on commit d1fdb41

Please sign in to comment.