diff options
| author | Maksim Levental <maksim.levental@gmail.com> | 2025-09-15 00:45:30 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-09-15 06:45:30 +0200 |
| commit | 6a4f66476ff59a32898891345bc07547e71028ec (patch) | |
| tree | e9fd9bf7d9ab756b032949fe0f4515d367aa1fd5 /mlir/test/python | |
| parent | 65ad21d730d25789454d18e811f8ff5db79cb5d4 (diff) | |
[MLIR][Python] restore `liveModuleMap` (#158506)
There are cases where the same module can have multiple references (via
`PyModule::forModule` via `PyModule::createFromCapsule`) and thus when
`PyModule`s get gc'd `mlirModuleDestroy` can get called multiple times
for the same actual underlying `mlir::Module` (i.e., double free). So we
do actually need a "liveness map" for modules.
Note, if `type_caster<MlirModule>::from_cpp` weren't a thing we could guarantree
this never happened except explicitly when users called `PyModule::createFromCapsule`.
Diffstat (limited to 'mlir/test/python')
| -rw-r--r-- | mlir/test/python/ir/module.py | 8 |
1 files changed, 5 insertions, 3 deletions
diff --git a/mlir/test/python/ir/module.py b/mlir/test/python/ir/module.py index ad4c9340a6c8..33959bea9ffb 100644 --- a/mlir/test/python/ir/module.py +++ b/mlir/test/python/ir/module.py @@ -121,6 +121,7 @@ def testRoundtripBinary(): def testModuleOperation(): ctx = Context() module = Module.parse(r"""module @successfulParse {}""", ctx) + assert ctx._get_live_module_count() == 1 op1 = module.operation # CHECK: module @successfulParse print(op1) @@ -145,6 +146,7 @@ def testModuleOperation(): op1 = None op2 = None gc.collect() + assert ctx._get_live_module_count() == 0 # CHECK-LABEL: TEST: testModuleCapsule @@ -152,17 +154,17 @@ def testModuleOperation(): def testModuleCapsule(): ctx = Context() module = Module.parse(r"""module @successfulParse {}""", ctx) + assert ctx._get_live_module_count() == 1 # CHECK: "mlir.ir.Module._CAPIPtr" module_capsule = module._CAPIPtr print(module_capsule) module_dup = Module._CAPICreate(module_capsule) - assert module is not module_dup + assert module is module_dup assert module == module_dup - module._clear_mlir_module() - assert module != module_dup assert module_dup.context is ctx # Gc and verify destructed. module = None module_capsule = None module_dup = None gc.collect() + assert ctx._get_live_module_count() == 0 |
