| Issue |
234940
|
| Summary |
[HLSL][SPIR-V] Proposal: legalize resource handles in the SPIR-V backend
|
| Labels |
backend:SPIR-V,
SPIR-V,
clang:HLSL:SPIRV
|
| Assignees |
|
| Reporter |
dnovillo
|
When a resource handle stays in a global, the SPIR-V backend emits an `OpVariable` of resource type in the `Function` or `Private` storage class, and spirv-val rejects the module:
```
error: line 68: In Logical addressing, variables can only allocate a pointer to the StorageBuffer or Workgroup storage classes
%_ZL9Transform_0 = OpVariable %_ptr_Function_spirv_VulkanBuffer Function
```
Our internal testing hits this on many shaders that DXC compiles successfully.
Reproducer: the pixel entry point of BigWheels' [`assets/basic/shaders/VertexColors.hlsl`](https://github.com/google/bigwheels/blob/ae412e248a7c9e13e54b36c86a08ef8057d5d790/assets/basic/shaders/VertexColors.hlsl), compiled with `-spirv -T ps_6_6 -E psmain`. Only `vsmain` reads `Transform`, so in `psmain` the global holding its handle is stored and never read:
```hlsl
struct TransformData { float4x4 M; };
ConstantBuffer<TransformData> Transform : register(b0);
struct VSOutput {
float4 Position : SV_POSITION;
float3 Color : COLOR;
};
VSOutput vsmain(float4 Position : POSITION, float3 Color : COLOR) {
VSOutput result;
result.Position = mul(Transform.M, Position);
result.Color = Color;
return result;
}
float4 psmain(VSOutput input) : SV_TARGET {
return float4(input.Color, 1);
}
```
Deleting the unused global fixes this shader, but the same cause affects resources that are used. The SPIR-V backend expects every handle to come directly from its creation call. Clang stores each handle in a global and loads it at every use, and the backend relies on the middle end to replace those loads with the handle. From reading the code, at `-O0` the loads stay, and handles reaching isel through a load, `phi` or `select` trigger an assertion instead of a diagnostic.
#134749, #162841 and #192562 are earlier symptoms of the same gap. DXIL addressed part of it in #135378, whose review deferred a SPIR-V version. I'd like to propose an explicit resource-handle legalization step in the SPIR-V backend, at every optimization level, before implicit bindings are assigned:
1. Move handles out of memory, touching only handle-typed fields.
2. Resolve a `phi` or `select` of handles to a single binding. When the handles have different bindings, report the error from [wg-hlsl 0048](https://github.com/llvm/wg-hlsl/blob/main/proposals/0048-local-resources-behavior.md), as DXIL does: "Resource access is not guaranteed to map to a unique global resource".
3. Remove dead handle-creation calls, their stores and their globals.
4. Report an error for any handle not rooted in a creation call.
@rj123-nv has step 3 in #225270. The other steps would follow as separate PRs, and could later be shared with the DXIL passes.
@hekota @bogner, does this match where you want resource legalization to go?
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs