[clang] [Hlsl][Spirv] Don't emit global variables for structs containing resources (PR #234797)

via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 5 23:48:42 PST 2026


https://github.com/rj123-nv updated https://github.com/llvm/llvm-project/pull/234797

>From 763811e7404fc7e9a422d1ee5464867bb72ed2f0 Mon Sep 17 00:00:00 2001
From: rjennings <rjennings at nvidia.com>
Date: Wed, 21 Oct 2026 16:57:17 -0700
Subject: [PATCH 1/3] [Hlsl][Spirv] Don't emit global variables for structs
 containing resources

For global, non-static instances of a struct containing
resources, we end up splitting these recursively into separate global
variables for each resource, and for numeric members these go into the
default $globals cbuffer.

For this change, when there's a instance of a struct (or array of)
containing only resources, skip emitting a global variable for the
struct. Before, it would emit external linkage globals of these which
would not get removed.

based on https://github.com/llvm/wg-hlsl/blob/main/proposals/0038-resources-in-structs.md

improves #194435
---
 clang/include/clang/AST/HLSLResource.h        |  5 ++++
 clang/lib/CodeGen/CodeGenModule.cpp           | 11 +++++++--
 clang/lib/Sema/SemaHLSL.cpp                   |  5 ++--
 .../resources-in-structs-no-global.hlsl       | 24 +++++++++++++++++++
 4 files changed, 40 insertions(+), 5 deletions(-)
 create mode 100644 clang/test/CodeGenHLSL/resources/resources-in-structs-no-global.hlsl

diff --git a/clang/include/clang/AST/HLSLResource.h b/clang/include/clang/AST/HLSLResource.h
index 03152d7e0bfaea..f7a00951dd3c0b 100644
--- a/clang/include/clang/AST/HLSLResource.h
+++ b/clang/include/clang/AST/HLSLResource.h
@@ -165,6 +165,11 @@ class EmbeddedResourceNameBuilder {
   void pushName(llvm::StringRef N, llvm::StringRef Delim);
 };
 
+inline bool isStructOrArrayOfWithResources(QualType Ty) {
+  return Ty->isHLSLIntangibleType() && !Ty->isHLSLResourceRecord() &&
+         !Ty->isHLSLResourceRecordArray();
+}
+
 } // namespace hlsl
 
 } // namespace clang
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 88129acfe2dabc..750e05352ffdd7 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -35,6 +35,7 @@
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/DeclTemplate.h"
+#include "clang/AST/HLSLResource.h"
 #include "clang/AST/Mangle.h"
 #include "clang/AST/RecursiveASTVisitor.h"
 #include "clang/AST/StmtVisitor.h"
@@ -6572,10 +6573,16 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
 
   // TODO(Reflection): add support for consteval-only types.
 
-  // HLSL default buffer constants will be emitted during HLSLBufferDecl codegen
+  // HLSL default buffer constants will be emitted during HLSLBufferDecl codegen.
+  // Also skip emitting globals for structs or arrays of structs containing only
+  // resource constants, which should have already been flattened into separate
+  // variables.
   if (getLangOpts().HLSL &&
-      D->getType().getAddressSpace() == LangAS::hlsl_constant)
+      (ASTTy.getAddressSpace() == LangAS::hlsl_constant ||
+       (D->getStorageClass() != SC_Static &&
+        hlsl::isStructOrArrayOfWithResources(ASTTy)))) {
     return;
+  }
 
   // If this is OpenMP device, check if it is legal to emit this global
   // normally.
diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp
index eb6fce49d87083..040676b6417185 100644
--- a/clang/lib/Sema/SemaHLSL.cpp
+++ b/clang/lib/Sema/SemaHLSL.cpp
@@ -6380,9 +6380,8 @@ void SemaHLSL::ActOnVariableDeclarator(VarDecl *VD) {
     }
 
     // Process resources in user-defined structs, or arrays of such structs.
-    const Type *VDTy = VD->getType().getTypePtr();
-    if (VD->getStorageClass() != SC_Static && VDTy->isHLSLIntangibleType() &&
-        !isResourceRecordTypeOrArrayOf(VD))
+    if (VD->getStorageClass() != SC_Static &&
+        isStructOrArrayOfWithResources(VD->getType()))
       handleGlobalStructOrArrayOfWithResources(VD);
 
     // Mark groupshared variables as extern so they will have
diff --git a/clang/test/CodeGenHLSL/resources/resources-in-structs-no-global.hlsl b/clang/test/CodeGenHLSL/resources/resources-in-structs-no-global.hlsl
new file mode 100644
index 00000000000000..9574d6d7a8e604
--- /dev/null
+++ b/clang/test/CodeGenHLSL/resources/resources-in-structs-no-global.hlsl
@@ -0,0 +1,24 @@
+// When processing a struct containing resources, we should split members recursively into
+// their own global variables, and not emit a global of struct or array of
+// struct type which contains a resource
+
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --implicit-check-not='@g =' --implicit-check-not='@array ='
+// RUN: %clang_cc1 -triple spirv1.6-unknown-vulkan1.3-compute -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s --implicit-check-not='@g =' --implicit-check-not='@array ='
+// RUN: %if spirv-registered-target && spirv-val %{ %clang_cc1 -triple spirv1.6-unknown-vulkan1.3-compute -O3 -emit-obj -o %t.spv %s && spirv-val --target-env vulkan1.3 %t.spv %}
+
+// CHECK: @_ZL5g.Buf = internal global
+// CHECK: @_ZL11array.0.Buf = internal global
+// CHECK: @_ZL11array.1.Buf = internal global
+
+struct A {
+  RWBuffer<float> Buf;
+};
+A g;
+A array[2];
+
+[numthreads(1, 1, 1)]
+void main() {
+  g.Buf[0] = 1.0f;
+  array[0].Buf[0] = 2.0f;
+  array[1].Buf[0] = 3.0f;
+}

>From c295c29eb943a2940eb7df89c0bef4ed7a07bb2a Mon Sep 17 00:00:00 2001
From: rjennings <rjennings at nvidia.com>
Date: Thu, 5 Nov 2026 23:43:54 -0800
Subject: [PATCH 2/3] Add negative test for static struct with buffer

---
 .../resources-in-structs-static-global.hlsl   | 23 +++++++++++++++++++
 1 file changed, 23 insertions(+)
 create mode 100644 clang/test/CodeGenHLSL/resources/resources-in-structs-static-global.hlsl

diff --git a/clang/test/CodeGenHLSL/resources/resources-in-structs-static-global.hlsl b/clang/test/CodeGenHLSL/resources/resources-in-structs-static-global.hlsl
new file mode 100644
index 00000000000000..7818ffce9027ec
--- /dev/null
+++ b/clang/test/CodeGenHLSL/resources/resources-in-structs-static-global.hlsl
@@ -0,0 +1,23 @@
+// We should not split global struct instances containing resources when they are
+// static
+
+// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-compute -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+// RUN: %clang_cc1 -triple spirv1.6-unknown-vulkan1.3-compute -emit-llvm -disable-llvm-passes -o - %s | FileCheck %s
+
+// CHECK: @_ZL6cached = internal {{.*}}global %struct.A
+
+struct A {
+  RWBuffer<float> Buf;
+};
+
+RWBuffer<float> buffer;
+static A cached = {buffer};
+
+void write(A value) {
+  value.Buf[0] = 1.0f;
+}
+
+[numthreads(1, 1, 1)]
+void main() {
+  write(cached);
+}

>From 9a88cc6886472ebe92fdba6aa772d4fe187e6583 Mon Sep 17 00:00:00 2001
From: rjennings <rjennings at nvidia.com>
Date: Thu, 5 Nov 2026 23:46:34 -0800
Subject: [PATCH 3/3] clang format

---
 clang/lib/CodeGen/CodeGenModule.cpp | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 750e05352ffdd7..bdfbdeb1059384 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -6573,14 +6573,13 @@ void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
 
   // TODO(Reflection): add support for consteval-only types.
 
-  // HLSL default buffer constants will be emitted during HLSLBufferDecl codegen.
-  // Also skip emitting globals for structs or arrays of structs containing only
-  // resource constants, which should have already been flattened into separate
-  // variables.
-  if (getLangOpts().HLSL &&
-      (ASTTy.getAddressSpace() == LangAS::hlsl_constant ||
-       (D->getStorageClass() != SC_Static &&
-        hlsl::isStructOrArrayOfWithResources(ASTTy)))) {
+  // HLSL default buffer constants will be emitted during HLSLBufferDecl
+  // codegen. Also skip emitting globals for structs or arrays of structs
+  // containing only resource constants, which should have already been
+  // flattened into separate variables.
+  if (getLangOpts().HLSL && (ASTTy.getAddressSpace() == LangAS::hlsl_constant ||
+                             (D->getStorageClass() != SC_Static &&
+                              hlsl::isStructOrArrayOfWithResources(ASTTy)))) {
     return;
   }
 



More information about the cfe-commits mailing list