[clang] bf14b9f - [OpenMP] Mark critical region lock variables as dso_local (#201157)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 8 22:13:04 PDT 2026
Author: Shivam Gupta
Date: 2026-06-09T10:42:59+05:30
New Revision: bf14b9ff2a61daed7a4bae710908f12b467568d6
URL: https://github.com/llvm/llvm-project/commit/bf14b9ff2a61daed7a4bae710908f12b467568d6
DIFF: https://github.com/llvm/llvm-project/commit/bf14b9ff2a61daed7a4bae710908f12b467568d6.diff
LOG: [OpenMP] Mark critical region lock variables as dso_local (#201157)
OpenMP named critical regions use lock variables of the form
.gomp_critical_user_<name>.var, which are created through
CGOpenMPRuntime::getCriticalRegionLock().
These variables are created via
OpenMPIRBuilder::getOrCreateInternalVariable() and bypass the normal
CodeGenModule::setDSOLocal() path used for other Clang-generated
globals. As a result, OpenMP critical lock variables do not receive the
usual frontend dso_local inference.
Apply CodeGenModule::setDSOLocal() to critical lock variables after
creation. This matches the existing frontend
dso_local inference logic.
On ELF targets with a static relocation model, this results in direct
accesses to the lock variable instead of GOT-based accesses. For
example, x86-64 code generation changes from R_X86_64_REX_GOTPCRELX
relocations
to direct relocations for .gomp_critical_user_<name>.var, while PIC code
generation remains unchanged.
Rework of #75564.
Added:
clang/test/OpenMP/critical-dso-local.cpp
Modified:
clang/lib/CodeGen/CGOpenMPRuntime.cpp
clang/test/OpenMP/critical_codegen.cpp
clang/test/OpenMP/critical_codegen_attr.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index f3158f48e7944..92763d72877f4 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -2070,7 +2070,10 @@ Address CGOpenMPRuntime::emitThreadIDAddress(CodeGenFunction &CGF,
llvm::Value *CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) {
std::string Prefix = Twine("gomp_critical_user_", CriticalName).str();
std::string Name = getName({Prefix, "var"});
- return OMPBuilder.getOrCreateInternalVariable(KmpCriticalNameTy, Name);
+ llvm::GlobalVariable *GV =
+ OMPBuilder.getOrCreateInternalVariable(KmpCriticalNameTy, Name);
+ CGM.setDSOLocal(GV);
+ return GV;
}
namespace {
diff --git a/clang/test/OpenMP/critical-dso-local.cpp b/clang/test/OpenMP/critical-dso-local.cpp
new file mode 100644
index 0000000000000..8895f8f460d6e
--- /dev/null
+++ b/clang/test/OpenMP/critical-dso-local.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -mrelocation-model static \
+// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=STATIC
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fopenmp -mrelocation-model pic \
+// RUN: -emit-llvm %s -o - | FileCheck %s --check-prefix=PIC
+
+// STATIC: @.gomp_critical_user_foo.var = common dso_local global [8 x i32] zeroinitializer
+// PIC: @.gomp_critical_user_foo.var = common global [8 x i32] zeroinitializer
+
+void f() {
+#pragma omp critical(foo)
+ ;
+}
diff --git a/clang/test/OpenMP/critical_codegen.cpp b/clang/test/OpenMP/critical_codegen.cpp
index 9620613dfdb87..c40ed4e632deb 100644
--- a/clang/test/OpenMP/critical_codegen.cpp
+++ b/clang/test/OpenMP/critical_codegen.cpp
@@ -16,9 +16,9 @@
#define HEADER
// ALL: [[IDENT_T_TY:%.+]] = type { i32, i32, i32, i32, ptr }
-// ALL: [[UNNAMED_LOCK:@.+]] = common global [8 x i32] zeroinitializer
-// ALL: [[THE_NAME_LOCK:@.+]] = common global [8 x i32] zeroinitializer
-// ALL: [[THE_NAME_LOCK1:@.+]] = common global [8 x i32] zeroinitializer
+// ALL: [[UNNAMED_LOCK:@.+]] = common {{(dso_local )?}}global [8 x i32] zeroinitializer
+// ALL: [[THE_NAME_LOCK:@.+]] = common {{(dso_local )?}}global [8 x i32] zeroinitializer
+// ALL: [[THE_NAME_LOCK1:@.+]] = common {{(dso_local )?}}global [8 x i32] zeroinitializer
// ALL: define {{.*}}void [[FOO:@.+]]()
diff --git a/clang/test/OpenMP/critical_codegen_attr.cpp b/clang/test/OpenMP/critical_codegen_attr.cpp
index 50b0b04fcfd4a..0014d16b2000f 100644
--- a/clang/test/OpenMP/critical_codegen_attr.cpp
+++ b/clang/test/OpenMP/critical_codegen_attr.cpp
@@ -16,9 +16,9 @@
#define HEADER
// ALL: [[IDENT_T_TY:%.+]] = type { i32, i32, i32, i32, ptr }
-// ALL: [[UNNAMED_LOCK:@.+]] = common global [8 x i32] zeroinitializer
-// ALL: [[THE_NAME_LOCK:@.+]] = common global [8 x i32] zeroinitializer
-// ALL: [[THE_NAME_LOCK1:@.+]] = common global [8 x i32] zeroinitializer
+// ALL: [[UNNAMED_LOCK:@.+]] = common {{(dso_local )?}}global [8 x i32] zeroinitializer
+// ALL: [[THE_NAME_LOCK:@.+]] = common {{(dso_local )?}}global [8 x i32] zeroinitializer
+// ALL: [[THE_NAME_LOCK1:@.+]] = common {{(dso_local )?}}global [8 x i32] zeroinitializer
// ALL: define {{.*}}void [[FOO:@.+]]()
More information about the cfe-commits
mailing list