[llvm] [Offload] Avoid global mutex destructors (PR #221471)
Vedran Miletić via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 7 02:21:15 PDT 2026
https://github.com/vedranmiletic updated https://github.com/llvm/llvm-project/pull/221471
>From ffc1d8c4dc046fda2b722e280befdda62af3e5a3 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vedran=20Mileti=C4=87?= <vedran at miletic.net>
Date: Sat, 5 Sep 2026 17:49:50 +0200
Subject: [PATCH] [Offload] Avoid global mutex destructors
Replace namespace-scope std::mutex instances that trigger
-Wglobal-constructors with destruction-free storage.
This allows the offload libraries to build with
-Werror=global-constructors.
---
offload/liboffload/src/OffloadImpl.cpp | 13 ++++++++-----
offload/libomptarget/OffloadRTL.cpp | 9 ++++++---
2 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/offload/liboffload/src/OffloadImpl.cpp b/offload/liboffload/src/OffloadImpl.cpp
index e79c1e2d8832e7..9be0629faa4b8b 100644
--- a/offload/liboffload/src/OffloadImpl.cpp
+++ b/offload/liboffload/src/OffloadImpl.cpp
@@ -295,9 +295,12 @@ struct OffloadContext;
// This pointer is non-null if and only if the context is valid and fully
// initialized
static std::atomic<OffloadContext *> OffloadContextVal;
-std::mutex OffloadContextValMutex;
+static std::mutex &getOffloadContextMutex() {
+ static std::mutex Mutex;
+ return Mutex;
+}
// Set once the vendor runtimes may have been torn down at process exit.
-// Guarded by OffloadContextValMutex.
+// Guarded by getOffloadContextMutex().
static bool ShutDownAtExit = false;
struct OffloadContext {
OffloadContext(OffloadContext &) = delete;
@@ -372,7 +375,7 @@ Error initPlugins(OffloadContext &Context, const ol_init_args_t *InitArgs) {
}
Error olInit_impl(const ol_init_args_t *InitArgs) {
- std::lock_guard<std::mutex> Lock(OffloadContextValMutex);
+ std::lock_guard<std::mutex> Lock(getOffloadContextMutex());
if (ShutDownAtExit)
return createOffloadError(ErrorCode::UNINITIALIZED,
@@ -413,7 +416,7 @@ static Error destroyContext(OffloadContext *Context) {
}
Error olShutDown_impl() {
- std::lock_guard<std::mutex> Lock(OffloadContextValMutex);
+ std::lock_guard<std::mutex> Lock(getOffloadContextMutex());
// The context may already have been released at process exit, calls that
// balance an earlier olInit are still valid.
@@ -434,7 +437,7 @@ Error olShutDown_impl() {
// In these cases we register a specific handler to shut it down with the proper
// ordering. If this is necessary than the shutdown implementation is skipped.
static void shutDownAtExit() {
- std::lock_guard<std::mutex> Lock(OffloadContextValMutex);
+ std::lock_guard<std::mutex> Lock(getOffloadContextMutex());
ShutDownAtExit = true;
if (OffloadContext *Context = OffloadContextVal.exchange(nullptr))
consumeError(destroyContext(Context));
diff --git a/offload/libomptarget/OffloadRTL.cpp b/offload/libomptarget/OffloadRTL.cpp
index 96e4027dfc99f0..74c4b77d141b88 100644
--- a/offload/libomptarget/OffloadRTL.cpp
+++ b/offload/libomptarget/OffloadRTL.cpp
@@ -18,7 +18,10 @@
using namespace llvm::omp::target::debug;
-static std::mutex PluginMtx;
+static std::mutex &getPluginMutex() {
+ static std::mutex Mutex;
+ return Mutex;
+}
static uint32_t RefCount = 0;
std::atomic<bool> RTLAlive{false};
std::atomic<int> RTLOngoingSyncs{0};
@@ -33,7 +36,7 @@ static void checkRuntimeEnvironment() {
}
void initRuntime() {
- std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
+ std::scoped_lock<std::mutex> Lock(getPluginMutex());
Profiler::get();
TIMESCOPE();
@@ -59,7 +62,7 @@ void initRuntime() {
}
void deinitRuntime() {
- std::scoped_lock<decltype(PluginMtx)> Lock(PluginMtx);
+ std::scoped_lock<std::mutex> Lock(getPluginMutex());
assert(PM && "Runtime not initialized");
if (RefCount == 1) {
More information about the llvm-commits
mailing list