[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:35:54 PST 2026
rj123-nv wrote:
> Thanks for fixing this.
>
> Could you make sure the offload-test-suite `ResourcesInStructs` tests still behave as expected with this change?
>
> OK with the above changes.
with current PR i get:
array-of-structs-with-res.test : pass
res-in-struct-local-arg.test : fail (compiler error)
res-in-struct-simple-array.test : pass
res-in-struct-this-access.test : fail (compiler error)
res-array-of-matrix-in-struct.test : fail (runtime diff)
res-in-struct-mix.test : pass
res-in-struct-simple-one.test : pass
res-of-matrix-in-struct.test : pass
all the ones passing here fail on main in spirv validation.
res-in-struct-this-access seems out of scope here.
res-in-struct-local-arg is something I realize I should address here. Basically the issue is that when you have
```
struct S {
RWBuffer<float> a;
RWBuffer<float> b;
};
S s;
[numthreads(1, 1, 1)]
void main() {
S tmp = s;
tmp.a[0] = tmp.b[0] + 1.0f;
}
```
We effectively split 's' into the actual globals 's.a' and 's.b' (assume '.' can be used in names)
according to https://github.com/llvm/wg-hlsl/blob/main/proposals/0038-resources-in-structs.md
When the whole struct 's' is used by value, like assignment or as a function arg, we need to reconstruct a struct type from the 2 globals s.a and s.b.
Effectively doing
```
struct S {
RWBuffer<float> a;
RWBuffer<float> b;
};
RWBuffer<float> s_a;
RWBuffer<float> s_b;
...
S tmp = { s_a, s_b }; // SROA should remove this eventually
```
There is logic that exists for mixed structs to do this (globals structs with both numeric members and resources, which are given hlsl_constant addr space) that I think can be adapted here.
https://github.com/llvm/llvm-project/blob/0dfd901f3757/clang/lib/CodeGen/CGHLSLRuntime.cpp#L538
https://github.com/llvm/llvm-project/pull/234797
More information about the cfe-commits
mailing list