[clang] [HIP][SPIR-V] Apply AMDGPU protected visibility to SPIRV AMDGCN target (PR #187784)

Dmitry Sidorov via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 20 13:10:37 PDT 2026


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

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.

>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] [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;
+}



More information about the cfe-commits mailing list