aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/Bindings/Python/IRModule.h
diff options
context:
space:
mode:
authorMaksim Levental <maksim.levental@gmail.com>2025-09-15 00:45:30 -0400
committerGitHub <noreply@github.com>2025-09-15 06:45:30 +0200
commit6a4f66476ff59a32898891345bc07547e71028ec (patch)
treee9fd9bf7d9ab756b032949fe0f4515d367aa1fd5 /mlir/lib/Bindings/Python/IRModule.h
parent65ad21d730d25789454d18e811f8ff5db79cb5d4 (diff)
downloadllvm-6a4f66476ff59a32898891345bc07547e71028ec.zip
llvm-6a4f66476ff59a32898891345bc07547e71028ec.tar.gz
llvm-6a4f66476ff59a32898891345bc07547e71028ec.tar.bz2
[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/lib/Bindings/Python/IRModule.h')
-rw-r--r--mlir/lib/Bindings/Python/IRModule.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/mlir/lib/Bindings/Python/IRModule.h b/mlir/lib/Bindings/Python/IRModule.h
index 1d1ff29..28b885f 100644
--- a/mlir/lib/Bindings/Python/IRModule.h
+++ b/mlir/lib/Bindings/Python/IRModule.h
@@ -218,6 +218,10 @@ public:
/// Gets the count of live context objects. Used for testing.
static size_t getLiveCount();
+ /// Gets the count of live modules associated with this context.
+ /// Used for testing.
+ size_t getLiveModuleCount();
+
/// Enter and exit the context manager.
static nanobind::object contextEnter(nanobind::object context);
void contextExit(const nanobind::object &excType,
@@ -244,6 +248,14 @@ private:
static nanobind::ft_mutex live_contexts_mutex;
static LiveContextMap &getLiveContexts();
+ // Interns all live modules associated with this context. Modules tracked
+ // in this map are valid. When a module is invalidated, it is removed
+ // from this map, and while it still exists as an instance, any
+ // attempt to access it will raise an error.
+ using LiveModuleMap =
+ llvm::DenseMap<const void *, std::pair<nanobind::handle, PyModule *>>;
+ LiveModuleMap liveModules;
+
bool emitErrorDiagnostics = false;
MlirContext context;