Author: Arseniy Obolenskiy
Date: 2026-11-12T05:49:51+01:00
New Revision: 01c460448069b3e596f869158f6ab64fba11bf3a

URL: 
https://github.com/llvm/llvm-project/commit/01c460448069b3e596f869158f6ab64fba11bf3a
DIFF: 
https://github.com/llvm/llvm-project/commit/01c460448069b3e596f869158f6ab64fba11bf3a.diff

LOG: [CIR][OpenMP] Add registerTargetGlobalVariable for global variables 
(#234077)

Limit the NYI to declare target variables so other globals emit under
-fopenmp

Added: 
    clang/test/CIR/CodeGenOpenMP/global-var.cpp

Modified: 
    clang/lib/CIR/CodeGen/CIRGenModule.cpp
    clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.cpp
    clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp 
b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
index aac3211ef02e7..f331f4ef88b7e 100644
--- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp
@@ -1316,7 +1316,9 @@ cir::GlobalOp CIRGenModule::getOrCreateCIRGlobal(
       entry.setLinkage(cir::GlobalLinkageKind::ExternalLinkage);
 
     assert(!cir::MissingFeatures::setDLLStorageClass());
-    assert(!cir::MissingFeatures::openMP());
+
+    if (langOpts.OpenMP && !langOpts.OpenMPSimd && d)
+      getOpenMPRuntime().registerTargetGlobalVariable(d);
 
     if (entry.getSymType() == ty &&
         cir::normalizeDefaultAddressSpace(entryCIRAS) ==
@@ -1389,8 +1391,7 @@ cir::GlobalOp CIRGenModule::getOrCreateCIRGlobal(
   // Handle things which are present even on external declarations.
   if (d) {
     if (langOpts.OpenMP && !langOpts.OpenMPSimd)
-      errorNYI(d->getSourceRange(),
-               "getOrCreateCIRGlobal: OpenMP target global variable");
+      getOpenMPRuntime().registerTargetGlobalVariable(d);
 
     gv.setAlignmentAttr(getSize(astContext.getDeclAlign(d)));
 

diff  --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.cpp 
b/clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.cpp
index d3e99488a6da8..8087be04ca7c8 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.cpp
@@ -173,6 +173,32 @@ bool CIRGenOpenMPRuntime::markAsGlobalTarget(GlobalDecl 
gd) {
   return !alreadyEmittedTargetDecls.insert(d).second;
 }
 
+// Mirrors CGOpenMPRuntime::registerTargetGlobalVariable.
+void CIRGenOpenMPRuntime::registerTargetGlobalVariable(const VarDecl *vd) {
+  if (cgm.getLangOpts().OMPTargetTriples.empty() &&
+      !cgm.getLangOpts().OpenMPIsTargetDevice)
+    return;
+
+  std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> res =
+      OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(vd);
+
+  if (res && *res != OMPDeclareTargetDeclAttr::MT_Link &&
+      vd->hasExternalStorage())
+    return;
+
+  if (res && *res == OMPDeclareTargetDeclAttr::MT_Local)
+    return;
+
+  if (!res) {
+    // OGCG records non-target variables emitted in device code.
+    assert(!cir::MissingFeatures::openMP());
+    return;
+  }
+
+  cgm.errorNYI(vd->getSourceRange(),
+               "registerTargetGlobalVariable: declare target variable");
+}
+
 bool CIRGenOpenMPRuntime::hasRequiresUnifiedSharedMemory() const {
   // OGCG also sets this from '#pragma omp requires unified_shared_memory'.
   assert(!cir::MissingFeatures::openMPRequiresUnifiedSharedMemory());

diff  --git a/clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.h 
b/clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.h
index b57537defbfbd..fb368e06c5b2a 100644
--- a/clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.h
+++ b/clang/lib/CIR/CodeGen/CIRGenOpenMPRuntime.h
@@ -48,6 +48,9 @@ class CIRGenOpenMPRuntime {
   /// handled and should be skipped.
   bool markAsGlobalTarget(GlobalDecl gd);
 
+  /// Register a global variable that may need an offload entry.
+  void registerTargetGlobalVariable(const VarDecl *vd);
+
   /// Return whether unified shared memory is required.
   bool hasRequiresUnifiedSharedMemory() const;
 

diff  --git a/clang/test/CIR/CodeGenOpenMP/global-var.cpp 
b/clang/test/CIR/CodeGenOpenMP/global-var.cpp
new file mode 100644
index 0000000000000..8945e21b35ce5
--- /dev/null
+++ b/clang/test/CIR/CodeGenOpenMP/global-var.cpp
@@ -0,0 +1,40 @@
+// RUN: %clang_cc1 -fopenmp -triple x86_64-unknown-linux-gnu -fclangir 
-emit-cir %s -o %t.cir
+// RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -fopenmp -fopenmp-targets=amdgcn-amd-amdhsa -triple 
x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t-offload.cir
+// RUN: FileCheck --input-file=%t-offload.cir %s -check-prefix=CIR
+// RUN: %clang_cc1 -fopenmp -triple x86_64-unknown-linux-gnu -fclangir 
-emit-llvm %s -o %t-cir.ll
+// RUN: FileCheck --input-file=%t-cir.ll %s -check-prefix=LLVM
+// RUN: %clang_cc1 -fopenmp -triple x86_64-unknown-linux-gnu -emit-llvm %s -o 
%t.ll
+// RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM
+
+int g;
+extern int e;
+int *use_e = &e;
+
+// CIR: cir.global external @g = #cir.int<0> : !s32i
+// CIR: cir.global external @use_e = #cir.global_view<@e> : !cir.ptr<!s32i>
+// CIR: cir.global "private" external @e : !s32i
+
+// LLVM: @g = global i32 0
+// LLVM: @use_e = global ptr @e
+// LLVM: @e = external global i32
+
+int global_ref(int v) {
+  g = v;
+  return g + e;
+}
+
+// CIR: cir.func{{.*}} @_Z10global_refi
+// CIR:   %[[G:.*]] = cir.get_global @g : !cir.ptr<!s32i>
+// CIR:   cir.store{{.*}} %{{.*}}, %[[G]] : !s32i, !cir.ptr<!s32i>
+// CIR:   %[[G1:.*]] = cir.get_global @g : !cir.ptr<!s32i>
+// CIR:   %[[G_VAL:.*]] = cir.load{{.*}} %[[G1]] : !cir.ptr<!s32i>, !s32i
+// CIR:   %[[E:.*]] = cir.get_global @e : !cir.ptr<!s32i>
+// CIR:   %[[E_VAL:.*]] = cir.load{{.*}} %[[E]] : !cir.ptr<!s32i>, !s32i
+// CIR:   %{{.*}} = cir.add nsw %[[G_VAL]], %[[E_VAL]] : !s32i
+
+// LLVM: define{{.*}} i32 @_Z10global_refi
+// LLVM:   store i32 %{{.*}}, ptr @g
+// LLVM:   %[[G:.*]] = load i32, ptr @g
+// LLVM:   %[[E:.*]] = load i32, ptr @e
+// LLVM:   %{{.*}} = add nsw i32 %[[G]], %[[E]]


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

Reply via email to