Skip to content

[MLIR][Python] Add method for getting the live operation objects #78663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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