Skip to content

Commit a1623fb

Browse files
committed
[MLIR][Python] demo finding live ops via nanobind internals
1 parent 157b81a commit a1623fb

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

mlir/examples/standalone/python/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,6 @@ if(NOT EXTERNAL_PROJECT_BUILD)
152152
add_dependencies(StandalonePythonModules "${_mlir_typestub_gen_target}")
153153
endif()
154154
add_dependencies(StandalonePythonModules "${_standaloneDialectsNanobind_typestub_gen_target}")
155+
156+
target_include_directories(StandalonePythonModules.extension._mlir.dso PRIVATE ${NB_DIR}/ext/robin_map/include)
157+
target_include_directories(StandalonePythonModules.extension._mlir.dso PRIVATE ${NB_DIR}/src)

mlir/lib/Bindings/Python/IRCore.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2897,6 +2897,36 @@ maybeGetTracebackLocation(const std::optional<PyLocation> &location) {
28972897
// Populates the core exports of the 'ir' submodule.
28982898
//------------------------------------------------------------------------------
28992899

2900+
#include "nb_internals.h"
2901+
2902+
auto print_live_op = [](void *k, PyObject *v) {
2903+
nb::handle op_py_type = nb::type<PyOperation>();
2904+
nb::handle maybe_op_inst{v};
2905+
nb::str end{" "};
2906+
if (maybe_op_inst.type().is(op_py_type)) {
2907+
nb::print("found live operation:", end);
2908+
nb::print(maybe_op_inst);
2909+
}
2910+
};
2911+
2912+
void print_live_ops() noexcept {
2913+
nanobind::detail::nb_internals *p = nanobind::detail::internals;
2914+
for (size_t i = 0; i < p->shard_count; ++i) {
2915+
nanobind::detail::nb_shard &s = p->shards[i];
2916+
nanobind::detail::lock_shard lock(s);
2917+
for (auto [k, v] : s.inst_c2p) {
2918+
if (NB_UNLIKELY(nanobind::detail::nb_is_seq(v))) {
2919+
nanobind::detail::nb_inst_seq *seq = nanobind::detail::nb_get_seq(v);
2920+
for (; seq != nullptr; seq = seq->next) {
2921+
print_live_op(k, seq->inst);
2922+
}
2923+
} else {
2924+
print_live_op(k, (PyObject *)v);
2925+
}
2926+
}
2927+
}
2928+
}
2929+
29002930
void mlir::python::populateIRCore(nb::module_ &m) {
29012931
// disable leak warnings which tend to be false positives.
29022932
nb::set_leak_warnings(false);
@@ -4475,4 +4505,6 @@ void mlir::python::populateIRCore(nb::module_ &m) {
44754505
PyErr_SetObject(PyExc_Exception, obj.ptr());
44764506
}
44774507
});
4508+
4509+
m.def("print_live_ops", &print_live_ops);
44784510
}

mlir/python/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,3 +974,6 @@ add_dependencies(MLIRPythonModules "${_mlir_typestub_gen_target}")
974974
if(MLIR_INCLUDE_TESTS)
975975
add_dependencies(MLIRPythonModules "${_mlirPythonTestNanobind_typestub_gen_target}")
976976
endif()
977+
message(STATUS "NB_DIR=${NB_DIR}")
978+
target_include_directories(MLIRPythonModules.extension._mlir.dso PRIVATE ${NB_DIR}/ext/robin_map/include)
979+
target_include_directories(MLIRPythonModules.extension._mlir.dso PRIVATE ${NB_DIR}/src)

mlir/test/python/dialects/memref.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ def testSubViewOpInferReturnTypeSemantics():
179179
# CHECK: %{{.*}} = memref.subview %[[DYNAMICALLOC]][1, 1] [3, 3] [1, 1] : memref<10x10xi32, strided<[10, 1], offset: ?>> to memref<3x3xi32, strided<[10, 1], offset: ?>>
180180
print(y.owner)
181181

182+
# CHECK: %subview_10 = memref.subview
183+
# CHECK: %0 = arith.addi %c3_2, %c4_3 : index
184+
print_live_ops()
185+
182186

183187
# CHECK-LABEL: TEST: testSubViewOpInferReturnTypeExtensiveSlicing
184188
@run

0 commit comments

Comments
 (0)