[clang] [HIP][CUDA] Apply protected visibility to kernels and globals (PR #187784)

Dmitry Sidorov via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 26 06:30:32 PDT 2026


https://github.com/MrSidims updated https://github.com/llvm/llvm-project/pull/187784

>From b2cd41aa330a6527795fc8a14642dfe801c542da Mon Sep 17 00:00:00 2001
From: Dmitry Sidorov <Dmitry.Sidorov at amd.com>
Date: Fri, 20 Mar 2026 16:46:39 +0100
Subject: [PATCH 1/3] [HIP][SPIR-V] Apply AMDGPU protected visibility to SPIRV
 AMDGCN target

On AMDGCN, device kernels and variables get protected visibility.
AMDGPU target already does this in AMDGPUTargetCodeGenInfo::
setTargetAttributes(), but the SPIRV target was missing the same
override.
---
 clang/lib/CodeGen/Targets/SPIR.cpp            | 23 ++++++++++++
 .../CodeGenHIP/amdgcnspirv-visibility.cpp     | 37 +++++++++++++++++++
 2 files changed, 60 insertions(+)
 create mode 100644 clang/test/CodeGenHIP/amdgcnspirv-visibility.cpp

diff --git a/clang/lib/CodeGen/Targets/SPIR.cpp b/clang/lib/CodeGen/Targets/SPIR.cpp
index 4d902fe2d6e3e..8b7cd5fb3882d 100644
--- a/clang/lib/CodeGen/Targets/SPIR.cpp
+++ b/clang/lib/CodeGen/Targets/SPIR.cpp
@@ -497,8 +497,31 @@ SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
   return DefaultGlobalAS;
 }
 
+// Copied from Targets/AMDGPU.cpp to match AMDGPUTargetCodeGenInfo behavior.
+// Device kernels and variables with hidden visibility need protected
+// visibility.
+// TODO: unify this with AMDGPU.cpp.
+static bool requiresAMDGPUProtectedVisibility(const Decl *D,
+                                              llvm::GlobalValue *GV) {
+  if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
+    return false;
+
+  return !D->hasAttr<OMPDeclareTargetDeclAttr>() &&
+         (D->hasAttr<DeviceKernelAttr>() ||
+          (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
+          (isa<VarDecl>(D) &&
+           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
+            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
+            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType())));
+}
+
 void SPIRVTargetCodeGenInfo::setTargetAttributes(
     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
+  if (requiresAMDGPUProtectedVisibility(D, GV)) {
+    GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
+    GV->setDSOLocal(true);
+  }
+
   if (GV->isDeclaration())
     return;
 
diff --git a/clang/test/CodeGenHIP/amdgcnspirv-visibility.cpp b/clang/test/CodeGenHIP/amdgcnspirv-visibility.cpp
new file mode 100644
index 0000000000000..cf6c2984498ea
--- /dev/null
+++ b/clang/test/CodeGenHIP/amdgcnspirv-visibility.cpp
@@ -0,0 +1,37 @@
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip -fcuda-is-device -fapply-global-visibility-to-externs -fvisibility=default -emit-llvm -o - %s | FileCheck --check-prefix=CHECK-DEFAULT %s
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip -fcuda-is-device -fapply-global-visibility-to-externs -fvisibility=protected -emit-llvm -o - %s | FileCheck --check-prefix=CHECK-PROTECTED %s
+// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -x hip -fcuda-is-device -fapply-global-visibility-to-externs -fvisibility=hidden -emit-llvm -o - %s | FileCheck --check-prefix=CHECK-HIDDEN %s
+
+// Mirrors clang/test/CodeGenCUDA/amdgpu-visibility.cu for the SPIR-V AMDGCN
+// target. Verifies that device kernels and variables with hidden visibility get
+// upgraded to protected, matching native AMDGPU behavior.
+
+#define __device__ __attribute__((device))
+#define __constant__ __attribute__((constant))
+#define __global__ __attribute__((global))
+
+// CHECK-DEFAULT: @c ={{.*}} addrspace(1) externally_initialized constant
+// CHECK-DEFAULT: @g ={{.*}} addrspace(1) externally_initialized global
+// CHECK-PROTECTED: @c = protected addrspace(1) externally_initialized constant
+// CHECK-PROTECTED: @g = protected addrspace(1) externally_initialized global
+// CHECK-HIDDEN: @c = protected addrspace(1) externally_initialized constant
+// CHECK-HIDDEN: @g = protected addrspace(1) externally_initialized global
+__constant__ int c;
+__device__ int g;
+
+// CHECK-DEFAULT: @e = external addrspace(1) global
+// CHECK-PROTECTED: @e = external protected addrspace(1) global
+// CHECK-HIDDEN: @e = external protected addrspace(1) global
+extern __device__ int e;
+
+// dummy one to hold reference to `e`.
+__device__ int f() {
+  return e;
+}
+
+// CHECK-DEFAULT: define{{.*}} spir_kernel void @_Z3foov()
+// CHECK-PROTECTED: define protected spir_kernel void @_Z3foov()
+// CHECK-HIDDEN: define protected spir_kernel void @_Z3foov()
+__global__ void foo() {
+  g = c;
+}

>From d3594b0687a241942ed44609ab3db3e3b8f38d62 Mon Sep 17 00:00:00 2001
From: Dmitry Sidorov <Dmitry.Sidorov at amd.com>
Date: Sat, 21 Mar 2026 00:14:36 +0100
Subject: [PATCH 2/3] Generalize the solution

---
 clang/lib/CodeGen/CodeGenModule.cpp | 22 ++++++++++++++++++++++
 clang/lib/CodeGen/Targets/SPIR.cpp  | 23 -----------------------
 2 files changed, 22 insertions(+), 23 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index daaa846bf42bc..a32e6c7aeb7a6 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1899,6 +1899,28 @@ void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
     return;
   }
 
+  // CUDA/HIP device kernels and global variables must be visible to the host
+  // so they can be registered / initialized. We require protected visibility
+  // unless the user explicitly requested hidden via an attribute.
+  if (Context.getLangOpts().CUDAIsDevice &&
+      LV.getVisibility() == HiddenVisibility &&
+      !LV.isVisibilityExplicit() &&
+      !D->hasAttr<OMPDeclareTargetDeclAttr>()) {
+    bool NeedsProtected = false;
+    if (isa<FunctionDecl>(D))
+      NeedsProtected =
+          D->hasAttr<CUDAGlobalAttr>() || D->hasAttr<DeviceKernelAttr>();
+    else if (const auto *VD = dyn_cast<VarDecl>(D))
+      NeedsProtected =
+          VD->hasAttr<CUDADeviceAttr>() || VD->hasAttr<CUDAConstantAttr>() ||
+          VD->getType()->isCUDADeviceBuiltinSurfaceType() ||
+          VD->getType()->isCUDADeviceBuiltinTextureType();
+    if (NeedsProtected) {
+      GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
+      return;
+    }
+  }
+
   if (Context.getLangOpts().HLSL && !D->isInExportDeclContext()) {
     GV->setVisibility(llvm::GlobalValue::HiddenVisibility);
     return;
diff --git a/clang/lib/CodeGen/Targets/SPIR.cpp b/clang/lib/CodeGen/Targets/SPIR.cpp
index 8b7cd5fb3882d..4d902fe2d6e3e 100644
--- a/clang/lib/CodeGen/Targets/SPIR.cpp
+++ b/clang/lib/CodeGen/Targets/SPIR.cpp
@@ -497,31 +497,8 @@ SPIRVTargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM,
   return DefaultGlobalAS;
 }
 
-// Copied from Targets/AMDGPU.cpp to match AMDGPUTargetCodeGenInfo behavior.
-// Device kernels and variables with hidden visibility need protected
-// visibility.
-// TODO: unify this with AMDGPU.cpp.
-static bool requiresAMDGPUProtectedVisibility(const Decl *D,
-                                              llvm::GlobalValue *GV) {
-  if (GV->getVisibility() != llvm::GlobalValue::HiddenVisibility)
-    return false;
-
-  return !D->hasAttr<OMPDeclareTargetDeclAttr>() &&
-         (D->hasAttr<DeviceKernelAttr>() ||
-          (isa<FunctionDecl>(D) && D->hasAttr<CUDAGlobalAttr>()) ||
-          (isa<VarDecl>(D) &&
-           (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
-            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinSurfaceType() ||
-            cast<VarDecl>(D)->getType()->isCUDADeviceBuiltinTextureType())));
-}
-
 void SPIRVTargetCodeGenInfo::setTargetAttributes(
     const Decl *D, llvm::GlobalValue *GV, CodeGen::CodeGenModule &M) const {
-  if (requiresAMDGPUProtectedVisibility(D, GV)) {
-    GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
-    GV->setDSOLocal(true);
-  }
-
   if (GV->isDeclaration())
     return;
 

>From 77902970b5e8c6dee817f2848c1381948d17a36c Mon Sep 17 00:00:00 2001
From: Dmitry Sidorov <Dmitry.Sidorov at amd.com>
Date: Sat, 21 Mar 2026 00:22:05 +0100
Subject: [PATCH 3/3] add test and format

---
 clang/lib/CodeGen/CodeGenModule.cpp           | 11 +++----
 .../CodeGenHIP/amdgcnspirv-visibility.cpp     | 33 +++++++++++++------
 2 files changed, 28 insertions(+), 16 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index a32e6c7aeb7a6..382087bf37c42 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -1903,18 +1903,17 @@ void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
   // so they can be registered / initialized. We require protected visibility
   // unless the user explicitly requested hidden via an attribute.
   if (Context.getLangOpts().CUDAIsDevice &&
-      LV.getVisibility() == HiddenVisibility &&
-      !LV.isVisibilityExplicit() &&
+      LV.getVisibility() == HiddenVisibility && !LV.isVisibilityExplicit() &&
       !D->hasAttr<OMPDeclareTargetDeclAttr>()) {
     bool NeedsProtected = false;
     if (isa<FunctionDecl>(D))
       NeedsProtected =
           D->hasAttr<CUDAGlobalAttr>() || D->hasAttr<DeviceKernelAttr>();
     else if (const auto *VD = dyn_cast<VarDecl>(D))
-      NeedsProtected =
-          VD->hasAttr<CUDADeviceAttr>() || VD->hasAttr<CUDAConstantAttr>() ||
-          VD->getType()->isCUDADeviceBuiltinSurfaceType() ||
-          VD->getType()->isCUDADeviceBuiltinTextureType();
+      NeedsProtected = VD->hasAttr<CUDADeviceAttr>() ||
+                       VD->hasAttr<CUDAConstantAttr>() ||
+                       VD->getType()->isCUDADeviceBuiltinSurfaceType() ||
+                       VD->getType()->isCUDADeviceBuiltinTextureType();
     if (NeedsProtected) {
       GV->setVisibility(llvm::GlobalValue::ProtectedVisibility);
       return;
diff --git a/clang/test/CodeGenHIP/amdgcnspirv-visibility.cpp b/clang/test/CodeGenHIP/amdgcnspirv-visibility.cpp
index cf6c2984498ea..d1b42e2368978 100644
--- a/clang/test/CodeGenHIP/amdgcnspirv-visibility.cpp
+++ b/clang/test/CodeGenHIP/amdgcnspirv-visibility.cpp
@@ -10,20 +10,26 @@
 #define __constant__ __attribute__((constant))
 #define __global__ __attribute__((global))
 
-// CHECK-DEFAULT: @c ={{.*}} addrspace(1) externally_initialized constant
-// CHECK-DEFAULT: @g ={{.*}} addrspace(1) externally_initialized global
-// CHECK-PROTECTED: @c = protected addrspace(1) externally_initialized constant
-// CHECK-PROTECTED: @g = protected addrspace(1) externally_initialized global
-// CHECK-HIDDEN: @c = protected addrspace(1) externally_initialized constant
-// CHECK-HIDDEN: @g = protected addrspace(1) externally_initialized global
+// CHECK-DEFAULT-DAG: @c ={{.*}} addrspace(1) externally_initialized constant
+// CHECK-DEFAULT-DAG: @g ={{.*}} addrspace(1) externally_initialized global
+// CHECK-DEFAULT-DAG: @e = external addrspace(1) global
+// CHECK-PROTECTED-DAG: @c = protected addrspace(1) externally_initialized constant
+// CHECK-PROTECTED-DAG: @g = protected addrspace(1) externally_initialized global
+// CHECK-PROTECTED-DAG: @e = external protected addrspace(1) global
+// CHECK-HIDDEN-DAG: @c = protected addrspace(1) externally_initialized constant
+// CHECK-HIDDEN-DAG: @g = protected addrspace(1) externally_initialized global
+// CHECK-HIDDEN-DAG: @e = external protected addrspace(1) global
 __constant__ int c;
 __device__ int g;
-
-// CHECK-DEFAULT: @e = external addrspace(1) global
-// CHECK-PROTECTED: @e = external protected addrspace(1) global
-// CHECK-HIDDEN: @e = external protected addrspace(1) global
 extern __device__ int e;
 
+// Explicit [[gnu::visibility("hidden")]] must be respected (not upgraded to
+// protected), unlike the implicit -fvisibility=hidden flag.
+// CHECK-DEFAULT-DAG: @h = hidden addrspace(1) externally_initialized global
+// CHECK-PROTECTED-DAG: @h = hidden addrspace(1) externally_initialized global
+// CHECK-HIDDEN-DAG: @h = hidden addrspace(1) externally_initialized global
+__attribute__((visibility("hidden"))) __device__ int h;
+
 // dummy one to hold reference to `e`.
 __device__ int f() {
   return e;
@@ -35,3 +41,10 @@ __device__ int f() {
 __global__ void foo() {
   g = c;
 }
+
+// CHECK-DEFAULT: define hidden spir_kernel void @_Z3barv()
+// CHECK-PROTECTED: define hidden spir_kernel void @_Z3barv()
+// CHECK-HIDDEN: define hidden spir_kernel void @_Z3barv()
+__attribute__((visibility("hidden"))) __global__ void bar() {
+  h = 1;
+}



More information about the cfe-commits mailing list