Skip to content

Commit 6d18313

Browse files
authored
[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`.
1 parent 5aec174 commit 6d18313

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

clang/lib/Sema/SemaHLSL.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3910,12 +3910,15 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
39103910
if (VD->getType()->isHLSLIntangibleType())
39113911
collectResourceBindingsOnVarDecl(VD);
39123912

3913-
if (isResourceRecordTypeOrArrayOf(VD) ||
3914-
VD->hasAttr<HLSLVkConstantIdAttr>()) {
3915-
// Make the variable for resources static. The global externally visible
3916-
// storage is accessed through the handle, which is a member. The variable
3917-
// itself is not externally visible.
3913+
if (VD->hasAttr<HLSLVkConstantIdAttr>())
39183914
VD->setStorageClass(StorageClass::SC_Static);
3915+
3916+
if (isResourceRecordTypeOrArrayOf(VD) &&
3917+
VD->getStorageClass() != SC_Static) {
3918+
// Add internal linkage attribute to non-static resource variables. The
3919+
// global externally visible storage is accessed through the handle, which
3920+
// is a member. The variable itself is not externally visible.
3921+
VD->addAttr(InternalLinkageAttr::CreateImplicit(getASTContext()));
39193922
}
39203923

39213924
// process explicit bindings

clang/test/AST/HLSL/cbuffer.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ cbuffer CB {
153153
static float SV;
154154
// CHECK: VarDecl {{.*}} s7 'EmptyStruct' callinit
155155
EmptyStruct s7;
156-
// CHECK: VarDecl {{.*}} Buf 'RWBuffer<float>':'hlsl::RWBuffer<float>' static callinit
156+
// CHECK: VarDecl {{.*}} Buf 'RWBuffer<float>':'hlsl::RWBuffer<float>' callinit
157157
RWBuffer<float> Buf;
158158
// CHECK: VarDecl {{.*}} ea 'EmptyArrayTypedef':'float[10][0]'
159159
EmptyArrayTypedef ea;

clang/test/AST/HLSL/private.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// CHECK: VarDecl {{.*}} global_scalar 'hlsl_private int' static cinit
44
static int global_scalar = 0;
55

6-
// CHECK: VarDecl {{.*}} global_buffer 'RWBuffer<float>':'hlsl::RWBuffer<float>' static callinit
6+
// CHECK: VarDecl {{.*}} global_buffer 'RWBuffer<float>':'hlsl::RWBuffer<float>' callinit
77
RWBuffer<float> global_buffer;
88

99
class A {

0 commit comments

Comments
 (0)