[clang] 6d18313 - [HLSL] Add internal linkage attribute to resources (#166844)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Nov 10 10:26:03 PST 2025
Author: Helena Kotas
Date: 2025-11-10T10:25:59-08:00
New Revision: 6d1831361fad0df0b1a36e4ff028de54cb05a6bb
URL: https://github.com/llvm/llvm-project/commit/6d1831361fad0df0b1a36e4ff028de54cb05a6bb
DIFF: https://github.com/llvm/llvm-project/commit/6d1831361fad0df0b1a36e4ff028de54cb05a6bb.diff
LOG: [HLSL] Add internal linkage attribute to resources (#166844)
HLSL resources should not be externally visible from the module. We made sure of this by marking them `static` as soon as they were declared. However, this prevents us fixing issue #166458 because there is no way to know if a resource has been explicitly marked `static` by the user, and can therefore be assigned to.
This change moves from making all resources `static` to adding Clang internal linkage attribute to all non-static resource declarations as a global scope.
No explicit test added this change. There is a number of existing HLSL codegen tests that already verify that the resource globals are emitted as `internal global`.
Added:
Modified:
clang/lib/Sema/SemaHLSL.cpp
clang/test/AST/HLSL/cbuffer.hlsl
clang/test/AST/HLSL/private.hlsl
Removed:
################################################################################
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index a06c57b15c585..e95fe16e6cb6c 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -3910,12 +3910,15 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
if (VD->getType()->isHLSLIntangibleType())
collectResourceBindingsOnVarDecl(VD);
- if (isResourceRecordTypeOrArrayOf(VD) ||
- VD->hasAttr<HLSLVkConstantIdAttr>()) {
- // Make the variable for resources static. The global externally visible
- // storage is accessed through the handle, which is a member. The variable
- // itself is not externally visible.
+ if (VD->hasAttr<HLSLVkConstantIdAttr>())
VD->setStorageClass(StorageClass::SC_Static);
+
+ if (isResourceRecordTypeOrArrayOf(VD) &&
+ VD->getStorageClass() != SC_Static) {
+ // Add internal linkage attribute to non-static resource variables. The
+ // global externally visible storage is accessed through the handle, which
+ // is a member. The variable itself is not externally visible.
+ VD->addAttr(InternalLinkageAttr::CreateImplicit(getASTContext()));
}
// process explicit bindings
diff --git a/clang/test/AST/HLSL/cbuffer.hlsl b/clang/test/AST/HLSL/cbuffer.hlsl
index f3c6636232798..b0b5b989e36c2 100644
--- a/clang/test/AST/HLSL/cbuffer.hlsl
+++ b/clang/test/AST/HLSL/cbuffer.hlsl
@@ -153,7 +153,7 @@ cbuffer CB {
static float SV;
// CHECK: VarDecl {{.*}} s7 'EmptyStruct' callinit
EmptyStruct s7;
- // CHECK: VarDecl {{.*}} Buf 'RWBuffer<float>':'hlsl::RWBuffer<float>' static callinit
+ // CHECK: VarDecl {{.*}} Buf 'RWBuffer<float>':'hlsl::RWBuffer<float>' callinit
RWBuffer<float> Buf;
// CHECK: VarDecl {{.*}} ea 'EmptyArrayTypedef':'float[10][0]'
EmptyArrayTypedef ea;
diff --git a/clang/test/AST/HLSL/private.hlsl b/clang/test/AST/HLSL/private.hlsl
index e00afb8f5cbd8..ba7380ec3cfda 100644
--- a/clang/test/AST/HLSL/private.hlsl
+++ b/clang/test/AST/HLSL/private.hlsl
@@ -3,7 +3,7 @@
// CHECK: VarDecl {{.*}} global_scalar 'hlsl_private int' static cinit
static int global_scalar = 0;
-// CHECK: VarDecl {{.*}} global_buffer 'RWBuffer<float>':'hlsl::RWBuffer<float>' static callinit
+// CHECK: VarDecl {{.*}} global_buffer 'RWBuffer<float>':'hlsl::RWBuffer<float>' callinit
RWBuffer<float> global_buffer;
class A {
More information about the cfe-commits
mailing list