https://github.com/xgupta updated 
https://github.com/llvm/llvm-project/pull/201157

>From 5488aea662b8dfdf227130e83a20f0880daf941f Mon Sep 17 00:00:00 2001
From: Shivam Gupta <[email protected]>
Date: Tue, 2 Jun 2026 22:26:55 +0530
Subject: [PATCH 1/3] [OpenMP] Mark critical region lock variables dso_local
 for static relocation model

OpenMP named critical regions use lock variables of the form
`.gomp_critical_user_<name>.var`, which are created through
CGOpenMPRuntime::getCriticalRegionLock().

On X86, accesses to these variables currently use GOT-based relocations
even when compiling with a static relocation model (-fno-pic). This
results in relocations such as R_X86_64_REX_GOTPCRELX being emitted for
OpenMP critical lock variables.

Mark these lock variables as dso_local when the relocation model is
static. This allows LLVM to generate direct accesses to the lock
variable while preserving existing PIC/shared-library behavior.

This matches the GCC behaviour for `gomp_critical_user_*` lock
variables.

Rework of https://github.com/llvm/llvm-project/pull/75564.
---
 clang/lib/CodeGen/CGOpenMPRuntime.cpp    |  9 ++++++++-
 clang/test/OpenMP/critical-dso-local.cpp | 17 +++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/OpenMP/critical-dso-local.cpp

diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 87e9824e677ba..a844f123df56d 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -2069,7 +2069,14 @@ 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);
+
+  if (CGM.getCodeGenOpts().RelocationModel == llvm::Reloc::Static)
+    GV->setDSOLocal(true);
+
+  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..ec19a678c489f
--- /dev/null
+++ b/clang/test/OpenMP/critical-dso-local.cpp
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu \
+// RUN:   -fopenmp \
+// RUN:   -mrelocation-model static \
+// RUN:   -emit-llvm %s -o - | FileCheck %s --check-prefix=STATIC
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu \
+// RUN:   -fopenmp \
+// RUN:   -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)
+  ;
+}

>From 601a8ba34e38502741ea1b6bf7482ed1c3192185 Mon Sep 17 00:00:00 2001
From: Shivam Gupta <[email protected]>
Date: Wed, 3 Jun 2026 23:51:02 +0530
Subject: [PATCH 2/3] [OpenMP] Apply normal dso_local inference to critical
 lock variables

---
 clang/lib/CodeGen/CGOpenMPRuntime.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index a844f123df56d..47a4faa9eef6c 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -2072,9 +2072,7 @@ llvm::Value 
*CGOpenMPRuntime::getCriticalRegionLock(StringRef CriticalName) {
 
   llvm::GlobalVariable *GV =
       OMPBuilder.getOrCreateInternalVariable(KmpCriticalNameTy, Name);
-
-  if (CGM.getCodeGenOpts().RelocationModel == llvm::Reloc::Static)
-    GV->setDSOLocal(true);
+  CGM.setDSOLocal(GV);
 
   return GV;
 }

>From d26cedd2394626ba5536950189ef402b78b5d09b Mon Sep 17 00:00:00 2001
From: Shivam Gupta <[email protected]>
Date: Mon, 8 Jun 2026 16:47:14 +0530
Subject: [PATCH 3/3] Address review comment and failed test cases

---
 clang/lib/CodeGen/CGOpenMPRuntime.cpp       | 2 --
 clang/test/OpenMP/critical-dso-local.cpp    | 8 ++------
 clang/test/OpenMP/critical_codegen.cpp      | 6 +++---
 clang/test/OpenMP/critical_codegen_attr.cpp | 6 +++---
 4 files changed, 8 insertions(+), 14 deletions(-)

diff --git a/clang/lib/CodeGen/CGOpenMPRuntime.cpp 
b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
index 47a4faa9eef6c..3ac41a3108ff7 100644
--- a/clang/lib/CodeGen/CGOpenMPRuntime.cpp
+++ b/clang/lib/CodeGen/CGOpenMPRuntime.cpp
@@ -2069,11 +2069,9 @@ 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"});
-
   llvm::GlobalVariable *GV =
       OMPBuilder.getOrCreateInternalVariable(KmpCriticalNameTy, Name);
   CGM.setDSOLocal(GV);
-
   return GV;
 }
 
diff --git a/clang/test/OpenMP/critical-dso-local.cpp 
b/clang/test/OpenMP/critical-dso-local.cpp
index ec19a678c489f..8895f8f460d6e 100644
--- a/clang/test/OpenMP/critical-dso-local.cpp
+++ b/clang/test/OpenMP/critical-dso-local.cpp
@@ -1,11 +1,7 @@
-// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu \
-// RUN:   -fopenmp \
-// RUN:   -mrelocation-model static \
+// 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 \
-// RUN:   -fopenmp \
-// RUN:   -mrelocation-model pic \
+// 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
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:@.+]]()
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to