[Openmp-commits] [openmp] [Libomptarget] Remove global ctor and use reference counting (PR #80499)

Joseph Huber via Openmp-commits openmp-commits at lists.llvm.org
Tue Feb 6 08:42:54 PST 2024


================
@@ -20,25 +20,37 @@
 extern void llvm::omp::target::ompt::connectLibrary();
 #endif
 
-__attribute__((constructor(101))) void init() {
+static std::mutex PluginMtx;
+static std::atomic<uint32_t> RefCount = 0;
+
+void initRuntime() {
+  std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
   Profiler::get();
   TIMESCOPE();
 
-  DP("Init offload library!\n");
-
-  PM = new PluginManager();
+  if (PM == nullptr) {
+    DP("Init offload library!\n");
+    PM = new PluginManager();
 
 #ifdef OMPT_SUPPORT
-  // Initialize OMPT first
-  llvm::omp::target::ompt::connectLibrary();
+    // Initialize OMPT first
+    llvm::omp::target::ompt::connectLibrary();
 #endif
 
-  PM->init();
+    PM->init();
+    PM->registerDelayedLibraries();
+  }
 
-  PM->registerDelayedLibraries();
+  RefCount++;
 }
 
-__attribute__((destructor(101))) void deinit() {
-  DP("Deinit offload library!\n");
-  delete PM;
+void deinitRuntime() {
+  std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
+  if (PM == nullptr)
----------------
jhuber6 wrote:

Yeah, honestly it's probably correct to just assert that `deinit` should never be called without initializing it first, rather than handling it. We expect these things to be paired up, so we should assert as such.

https://github.com/llvm/llvm-project/pull/80499


More information about the Openmp-commits mailing list