[clang] [HLSL] Static resources (PR #166880)
https://github.com/hekota closed https://github.com/llvm/llvm-project/pull/166880 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL] Static resources (PR #166880)
https://github.com/hekota auto_merge_enabled https://github.com/llvm/llvm-project/pull/166880 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL] Static resources (PR #166880)
https://github.com/hekota updated
https://github.com/llvm/llvm-project/pull/166880
>From 883df7bce01ddaedd133813b7e79397b974e6835 Mon Sep 17 00:00:00 2001
From: Helena Kotas
Date: Thu, 6 Nov 2025 12:37:29 -0800
Subject: [PATCH 1/7] [HLSL] Add internal linkage attribute to resources
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 is moves from making all resources `static` to adding Clang internal
linkage attribute to all non-static resource declarations as a global scope.
Existing tests verify that there is no change in how the resource globals are
emitted: `internal global`.
---
clang/lib/Sema/SemaHLSL.cpp | 13 -
clang/test/AST/HLSL/cbuffer.hlsl | 2 +-
clang/test/AST/HLSL/private.hlsl | 2 +-
3 files changed, 10 insertions(+), 7 deletions(-)
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()) {
- // 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())
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':'hlsl::RWBuffer'
static callinit
+ // CHECK: VarDecl {{.*}} Buf 'RWBuffer':'hlsl::RWBuffer'
callinit
RWBuffer 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':'hlsl::RWBuffer' static callinit
+// CHECK: VarDecl {{.*}} global_buffer
'RWBuffer':'hlsl::RWBuffer' callinit
RWBuffer global_buffer;
class A {
>From 58161a23f0e4241e679c4d0bcbd0c8d55cb9682d Mon Sep 17 00:00:00 2001
From: Helena Kotas
Date: Thu, 6 Nov 2025 17:40:22 -0800
Subject: [PATCH 2/7] [HLSL] Fix static resources
- Enable assignment to static resource variables (fixes #166458)
- Initialize static resources and resource arrays with default constructor that
sets the handle to poison
---
clang/lib/CodeGen/CGHLSLRuntime.cpp | 5 +-
clang/lib/CodeGen/CodeGenModule.cpp | 3 +-
clang/lib/Sema/SemaHLSL.cpp | 12 ++--
clang/test/SemaHLSL/static_resources.hlsl | 86 +++
4 files changed, 98 insertions(+), 8 deletions(-)
create mode 100644 clang/test/SemaHLSL/static_resources.hlsl
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index e392a12044a39..2cf601ca6a424 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1025,12 +1025,13 @@ std::optional
CGHLSLRuntime::emitResourceArraySubscriptExpr(
ArraySubsExpr->getType()->isHLSLResourceRecordArray()) &&
"expected resource array subscript expression");
- // Let clang codegen handle local resource array subscripts,
+ // Let Clang codegen handle local and static resource array subscripts,
// or when the subscript references on opaque expression (as part of
// ArrayInitLoopExpr AST node).
const VarDecl *ArrayDecl =
dyn_cast_or_null(getArrayDecl(ArraySubsExpr));
- if (!ArrayDecl || !ArrayDecl->hasGlobalStorage())
+ if (!ArrayDecl || !ArrayDecl->hasGlobalStorage() ||
+ ArrayDecl->getStorageClass() == SC_Static)
return std::nullopt;
// get the resource array type
[clang] [HLSL] Static resources (PR #166880)
@@ -1272,6 +1280,49 @@ std::optional
CGHLSLRuntime::emitResourceArraySubscriptExpr(
return CGF.MakeAddrLValue(TmpVar, ResultTy, AlignmentSource::Decl);
}
+// If RHSExpr is a global resource array, initialize all of its resources and
+// set them into LHS. Returns false if no copy has been performed and the
+// array copy should be handled by Clang codegen.
+bool CGHLSLRuntime::emitResourceArrayCopy(LValue &LHS, Expr *RHSExpr,
+ CodeGenFunction &CGF) {
+ QualType ResultTy = RHSExpr->getType();
+ assert((ResultTy->isHLSLResourceRecordArray()) && "expected resource array");
bogner wrote:
```suggestion
assert(ResultTy->isHLSLResourceRecordArray() && "expected resource array");
```
https://github.com/llvm/llvm-project/pull/166880
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL] Static resources (PR #166880)
@@ -1272,6 +1280,49 @@ std::optional
CGHLSLRuntime::emitResourceArraySubscriptExpr(
return CGF.MakeAddrLValue(TmpVar, ResultTy, AlignmentSource::Decl);
}
+// If RHSExpr is a global resource array, initialize all of its resources and
+// set them into LHS. Returns false if no copy has been performed and the
+// array copy should be handled by Clang codegen.
+bool CGHLSLRuntime::emitResourceArrayCopy(LValue &LHS, Expr *RHSExpr,
+ CodeGenFunction &CGF) {
+ QualType ResultTy = RHSExpr->getType();
+ assert((ResultTy->isHLSLResourceRecordArray()) && "expected resource array");
+
+ // Let Clang codegen handle local and static resource array copies.
+ const VarDecl *ArrayDecl = dyn_cast_or_null(getArrayDecl(RHSExpr));
+ if (!ArrayDecl || !ArrayDecl->hasGlobalStorage() ||
+ ArrayDecl->getStorageClass() == SC_Static)
+return false;
+
+ // Find binding info for the resource array. For implicit binding
+ // the HLSLResourceBindingAttr should have been added by SemaHLSL.
+ ResourceBindingAttrs Binding(ArrayDecl);
+ assert((Binding.hasBinding()) &&
bogner wrote:
```suggestion
assert(Binding.hasBinding() &&
```
https://github.com/llvm/llvm-project/pull/166880
___
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL] Static resources (PR #166880)
https://github.com/bogner approved this pull request. https://github.com/llvm/llvm-project/pull/166880 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL] Static resources (PR #166880)
https://github.com/hekota updated
https://github.com/llvm/llvm-project/pull/166880
>From 883df7bce01ddaedd133813b7e79397b974e6835 Mon Sep 17 00:00:00 2001
From: Helena Kotas
Date: Thu, 6 Nov 2025 12:37:29 -0800
Subject: [PATCH 1/6] [HLSL] Add internal linkage attribute to resources
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 is moves from making all resources `static` to adding Clang internal
linkage attribute to all non-static resource declarations as a global scope.
Existing tests verify that there is no change in how the resource globals are
emitted: `internal global`.
---
clang/lib/Sema/SemaHLSL.cpp | 13 -
clang/test/AST/HLSL/cbuffer.hlsl | 2 +-
clang/test/AST/HLSL/private.hlsl | 2 +-
3 files changed, 10 insertions(+), 7 deletions(-)
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()) {
- // 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())
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':'hlsl::RWBuffer'
static callinit
+ // CHECK: VarDecl {{.*}} Buf 'RWBuffer':'hlsl::RWBuffer'
callinit
RWBuffer 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':'hlsl::RWBuffer' static callinit
+// CHECK: VarDecl {{.*}} global_buffer
'RWBuffer':'hlsl::RWBuffer' callinit
RWBuffer global_buffer;
class A {
>From 58161a23f0e4241e679c4d0bcbd0c8d55cb9682d Mon Sep 17 00:00:00 2001
From: Helena Kotas
Date: Thu, 6 Nov 2025 17:40:22 -0800
Subject: [PATCH 2/6] [HLSL] Fix static resources
- Enable assignment to static resource variables (fixes #166458)
- Initialize static resources and resource arrays with default constructor that
sets the handle to poison
---
clang/lib/CodeGen/CGHLSLRuntime.cpp | 5 +-
clang/lib/CodeGen/CodeGenModule.cpp | 3 +-
clang/lib/Sema/SemaHLSL.cpp | 12 ++--
clang/test/SemaHLSL/static_resources.hlsl | 86 +++
4 files changed, 98 insertions(+), 8 deletions(-)
create mode 100644 clang/test/SemaHLSL/static_resources.hlsl
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index e392a12044a39..2cf601ca6a424 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1025,12 +1025,13 @@ std::optional
CGHLSLRuntime::emitResourceArraySubscriptExpr(
ArraySubsExpr->getType()->isHLSLResourceRecordArray()) &&
"expected resource array subscript expression");
- // Let clang codegen handle local resource array subscripts,
+ // Let Clang codegen handle local and static resource array subscripts,
// or when the subscript references on opaque expression (as part of
// ArrayInitLoopExpr AST node).
const VarDecl *ArrayDecl =
dyn_cast_or_null(getArrayDecl(ArraySubsExpr));
- if (!ArrayDecl || !ArrayDecl->hasGlobalStorage())
+ if (!ArrayDecl || !ArrayDecl->hasGlobalStorage() ||
+ ArrayDecl->getStorageClass() == SC_Static)
return std::nullopt;
// get the resource array type
[clang] [HLSL] Static resources (PR #166880)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff origin/main HEAD --extensions h,cpp -- clang/lib/CodeGen/CGExpr.cpp clang/lib/CodeGen/CGHLSLRuntime.cpp clang/lib/CodeGen/CGHLSLRuntime.h clang/lib/CodeGen/CodeGenModule.cpp clang/lib/Sema/SemaHLSL.cpp --diff_from_common_commit `` :warning: The reproduction instructions above might return results for more than one PR in a stack if you are using a stacked PR workflow. You can limit the results by changing `origin/main` to the base branch/commit you want to compare against. :warning: View the diff from clang-format here. ``diff diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h index fa7b9c468..dbcc19317 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.h +++ b/clang/lib/CodeGen/CGHLSLRuntime.h @@ -258,7 +258,7 @@ public: emitResourceArraySubscriptExpr(const ArraySubscriptExpr *E, CodeGenFunction &CGF); bool emitResourceArrayCopy(LValue &LHS, Expr *RHSExpr, CodeGenFunction &CGF); - + std::optional emitBufferArraySubscriptExpr( const ArraySubscriptExpr *E, CodeGenFunction &CGF, llvm::function_ref EmitIdxAfterBase); `` https://github.com/llvm/llvm-project/pull/166880 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [HLSL] Static resources (PR #166880)
https://github.com/hekota updated
https://github.com/llvm/llvm-project/pull/166880
>From 883df7bce01ddaedd133813b7e79397b974e6835 Mon Sep 17 00:00:00 2001
From: Helena Kotas
Date: Thu, 6 Nov 2025 12:37:29 -0800
Subject: [PATCH 1/5] [HLSL] Add internal linkage attribute to resources
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 is moves from making all resources `static` to adding Clang internal
linkage attribute to all non-static resource declarations as a global scope.
Existing tests verify that there is no change in how the resource globals are
emitted: `internal global`.
---
clang/lib/Sema/SemaHLSL.cpp | 13 -
clang/test/AST/HLSL/cbuffer.hlsl | 2 +-
clang/test/AST/HLSL/private.hlsl | 2 +-
3 files changed, 10 insertions(+), 7 deletions(-)
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()) {
- // 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())
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':'hlsl::RWBuffer'
static callinit
+ // CHECK: VarDecl {{.*}} Buf 'RWBuffer':'hlsl::RWBuffer'
callinit
RWBuffer 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':'hlsl::RWBuffer' static callinit
+// CHECK: VarDecl {{.*}} global_buffer
'RWBuffer':'hlsl::RWBuffer' callinit
RWBuffer global_buffer;
class A {
>From 58161a23f0e4241e679c4d0bcbd0c8d55cb9682d Mon Sep 17 00:00:00 2001
From: Helena Kotas
Date: Thu, 6 Nov 2025 17:40:22 -0800
Subject: [PATCH 2/5] [HLSL] Fix static resources
- Enable assignment to static resource variables (fixes #166458)
- Initialize static resources and resource arrays with default constructor that
sets the handle to poison
---
clang/lib/CodeGen/CGHLSLRuntime.cpp | 5 +-
clang/lib/CodeGen/CodeGenModule.cpp | 3 +-
clang/lib/Sema/SemaHLSL.cpp | 12 ++--
clang/test/SemaHLSL/static_resources.hlsl | 86 +++
4 files changed, 98 insertions(+), 8 deletions(-)
create mode 100644 clang/test/SemaHLSL/static_resources.hlsl
diff --git a/clang/lib/CodeGen/CGHLSLRuntime.cpp
b/clang/lib/CodeGen/CGHLSLRuntime.cpp
index e392a12044a39..2cf601ca6a424 100644
--- a/clang/lib/CodeGen/CGHLSLRuntime.cpp
+++ b/clang/lib/CodeGen/CGHLSLRuntime.cpp
@@ -1025,12 +1025,13 @@ std::optional
CGHLSLRuntime::emitResourceArraySubscriptExpr(
ArraySubsExpr->getType()->isHLSLResourceRecordArray()) &&
"expected resource array subscript expression");
- // Let clang codegen handle local resource array subscripts,
+ // Let Clang codegen handle local and static resource array subscripts,
// or when the subscript references on opaque expression (as part of
// ArrayInitLoopExpr AST node).
const VarDecl *ArrayDecl =
dyn_cast_or_null(getArrayDecl(ArraySubsExpr));
- if (!ArrayDecl || !ArrayDecl->hasGlobalStorage())
+ if (!ArrayDecl || !ArrayDecl->hasGlobalStorage() ||
+ ArrayDecl->getStorageClass() == SC_Static)
return std::nullopt;
// get the resource array type
[clang] [HLSL] Static resources (PR #166880)
github-actions[bot] wrote: # :penguin: Linux x64 Test Results * 111270 tests passed * 4418 tests skipped https://github.com/llvm/llvm-project/pull/166880 ___ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
