Skip to content

Conversation

@Ritanya-B-Bharadwaj
Copy link
Contributor

Patch 1 - #158134

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:codegen IR generation bugs: mangling, exceptions, etc. clang:openmp OpenMP related changes to Clang labels Nov 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 21, 2025

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: None (Ritanya-B-Bharadwaj)

Changes

Patch 1 - #158134


Full diff: https://github.com/llvm/llvm-project/pull/169018.diff

4 Files Affected:

  • (modified) clang/lib/CodeGen/CGDecl.cpp (+5-1)
  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+11)
  • (modified) clang/lib/Sema/SemaOpenMP.cpp (+1-1)
  • (added) clang/test/OpenMP/groupprivate_codegen.cpp (+25)
diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index 8b1cd83af2396..e08efed1cefc0 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -212,7 +212,11 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) {
     // Static sampler variables translated to function calls.
     if (D.getType()->isSamplerT())
       return;
-
+    if (D.hasAttr<OMPGroupPrivateDeclAttr>()) {
+      llvm::GlobalValue::LinkageTypes Linkage =
+          CGM.getLLVMLinkageVarDefinition(&D);
+      return EmitStaticVarDecl(D, Linkage);
+    }
     llvm::GlobalValue::LinkageTypes Linkage =
         CGM.getLLVMLinkageVarDefinition(&D);
 
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 3eeb1718e455a..57f9734604362 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -5354,6 +5354,11 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
   // Lookup the entry, lazily creating it if necessary.
   llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
   unsigned TargetAS = getContext().getTargetAddressSpace(AddrSpace);
+  if (D && D->hasAttr<OMPGroupPrivateDeclAttr>() && getLangOpts().OpenMP &&
+      getTarget().getTriple().isGPU()) {
+    Entry->setLinkage(llvm::GlobalValue::InternalLinkage);
+    AddrSpace = LangAS::cuda_shared;
+  }
   if (Entry) {
     if (WeakRefReferences.erase(Entry)) {
       if (D && !D->hasAttr<WeakAttr>())
@@ -5734,6 +5739,9 @@ LangAS CodeGenModule::GetGlobalVarAddressSpace(const VarDecl *D) {
     LangAS AS;
     if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
       return AS;
+    if (D && D->hasAttr<OMPGroupPrivateDeclAttr>()) {
+      return LangAS::cuda_shared; // maps to target addressspace 3 on NVPTX/AMD
+    }
   }
   return getTargetCodeGenInfo().getGlobalVarAddressSpace(*this, D);
 }
@@ -7623,6 +7631,9 @@ void CodeGenModule::EmitTopLevelDecl(Decl *D) {
     EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D));
     break;
 
+  case Decl::OMPGroupPrivate:
+    break;
+
   case Decl::OMPAllocate:
     EmitOMPAllocateDecl(cast<OMPAllocateDecl>(D));
     break;
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 81c591a00cfc6..1725e24f4ad06 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -3275,7 +3275,7 @@ SemaOpenMP::ActOnOpenMPGroupPrivateDirective(SourceLocation Loc,
                                              ArrayRef<Expr *> VarList) {
   if (!getLangOpts().OpenMP || getLangOpts().OpenMP < 60) {
     Diag(Loc, diag::err_omp_unexpected_directive)
-        << getOpenMPDirectiveName(OMPD_groupprivate, getLangOpts().OpenMP);
+        << 1 << getOpenMPDirectiveName(OMPD_groupprivate, getLangOpts().OpenMP);
     return nullptr;
   }
   if (OMPGroupPrivateDecl *D = CheckOMPGroupPrivateDecl(Loc, VarList)) {
diff --git a/clang/test/OpenMP/groupprivate_codegen.cpp b/clang/test/OpenMP/groupprivate_codegen.cpp
new file mode 100644
index 0000000000000..1a29fae6eb6d6
--- /dev/null
+++ b/clang/test/OpenMP/groupprivate_codegen.cpp
@@ -0,0 +1,25 @@
+// RUN: %clang_cc1 -fopenmp -fopenmp-version=60 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-is-device -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK
+// RUN: %clang_cc1 -fopenmp -fopenmp-version=60 -triple x86_64-unknown-unknown -fopenmp-targets=nvptx64-nvidia-cuda -fopenmp-is-device -emit-llvm -o - %s | FileCheck %s --check-prefix=CHECK
+//
+// This test ensures that variables marked 'groupprivate' are emitted as
+// device globals in the GPU shared address space (addrspace(3)).
+// The test is GPU-only and checks the LLVM IR for addrspace(3).
+//
+
+int group_var;
+
+#pragma omp groupprivate(group_var)
+
+void foo() {
+#pragma omp target teams num_teams(4) thread_limit(100)
+{
+  // simple use so the var is referenced in device codegen
+  group_var = group_var + 1;
+}
+}
+
+// CHECK: @group_var = global i32 0, align 4, addrspace(3)
+// CHECK: store i32 %{{.*}}, i32 addrspace(3)* @group_var, align 4
+
+// CHECK: @group_var = global i32 0, align 4, addrspace(3)
+// CHECK: store i32 %{{.*}}, i32 addrspace(3)* @group_var, align 4

@github-actions
Copy link

🐧 Linux x64 Test Results

  • 84704 tests passed
  • 1095 tests skipped
  • 1 test failed

Failed Tests

(click on a test name to see its output)

Clang

Clang.OpenMP/groupprivate_codegen.cpp
Exit Code: 1

Command Output (stdout):
--
# RUN: at line 1
/home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang -cc1 -internal-isystem /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lib/clang/22/include -nostdsysteminc -fopenmp -fopenmp-version=60 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-is-device -emit-llvm -o - /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp | /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/FileCheck --allow-unused-prefixes /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp --check-prefix=CHECK
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/clang -cc1 -internal-isystem /home/gha/actions-runner/_work/llvm-project/llvm-project/build/lib/clang/22/include -nostdsysteminc -fopenmp -fopenmp-version=60 -triple x86_64-unknown-unknown -fopenmp-targets=amdgcn-amd-amdhsa -fopenmp-is-device -emit-llvm -o - /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp
# note: command had no output on stdout or stderr
# executed command: /home/gha/actions-runner/_work/llvm-project/llvm-project/build/bin/FileCheck --allow-unused-prefixes /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp --check-prefix=CHECK
# .---command stderr------------
# | /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp:21:11: error: CHECK: expected string not found in input
# | // CHECK: @group_var = global i32 0, align 4, addrspace(3)
# |           ^
# | <stdin>:1:1: note: scanning from here
# | ; ModuleID = '/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp'
# | ^
# | <stdin>:1:89: note: possible intended match here
# | ; ModuleID = '/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp'
# |                                                                                         ^
# | 
# | Input file: <stdin>
# | Check file: /home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp
# | 
# | -dump-input=help explains the following input dump.
# | 
# | Input was:
# | <<<<<<
# |             1: ; ModuleID = '/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp' 
# | check:21'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
# | check:21'1                                                                                             ?                           possible intended match
# |             2: source_filename = "/home/gha/actions-runner/_work/llvm-project/llvm-project/clang/test/OpenMP/groupprivate_codegen.cpp" 
# | check:21'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             3: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" 
# | check:21'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             4: target triple = "x86_64-unknown-unknown" 
# | check:21'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             5:  
# | check:21'0     ~
# |             6: !llvm.module.flags = !{!0, !1, !2} 
# | check:21'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |             .
# |             .
# |             .
# | >>>>>>
# `-----------------------------
# error: command failed with exit status: 1

--

If these failures are unrelated to your changes (for example tests are broken or flaky at HEAD), please open an issue at https://github.com/llvm/llvm-project/issues and add the infrastructure label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:codegen IR generation bugs: mangling, exceptions, etc. clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:openmp OpenMP related changes to Clang clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants