| Issue |
238548
|
| Summary |
[HLSL][SPIRV] Implement Sample, SampleBias, SampleCmp and SampleCmpBias with a Status parameter in compute shaders
|
| Labels |
HLSL,
backend:SPIR-V
|
| Assignees |
|
| Reporter |
Icohedron
|
The compute thread-group shape error and the SPIR-V derivative-group execution mode cover only the overloads of `Sample`, `SampleBias`, `SampleCmp` and `SampleCmpBias` without a `Status` parameter. Report the shape error for the overloads with a `Status` parameter. Emit the derivative-group execution mode for compute entry points that call them. Test that the overloads with a `Status` parameter have the stage availability of the overloads without it.
A compute shader has a 1D quad layout when `numthreads` X is divisible by 4 and Y and Z are 1. It has a 2D quad layout when X and Y are divisible by 2.
## Acceptance Criteria
### HLSL
- [ ] Report the shape error for each compute entry point that calls the `Status` overloads of `Sample`, `SampleBias`, `SampleCmp` or `SampleCmpBias`, directly or through other functions, when its `numthreads` has no quad layout, in [clang/lib/Sema/SemaHLSL.cpp].
- [ ] Add Sema tests to [clang/test/SemaHLSL/Resources/Textures-derivative-availability.hlsl] that check the stage availability and the shape error of the `Status` overloads of `Sample`, `SampleBias`, `SampleCmp` and `SampleCmpBias`.
### SPIR-V
- [ ] Emit `SPV_KHR_compute_shader_derivatives` and the derivative-group capabilities in the module, and the execution mode of the quad layout on each compute entry point that reaches `OpImageSparseSampleImplicitLod` or `OpImageSparseSampleDrefImplicitLod`, directly or through other functions, in [llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp].
- [ ] Add compute-shader SPIR-V lowering tests to `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleStatusCompute.ll` (new).
## Notes
- Clang SPIR-V output for the `Status` overloads in a compute shader has `OpImageSparseSampleImplicitLod` or `OpImageSparseSampleDrefImplicitLod` without a derivative-group execution mode. `spirv-val` rejects it: "ImplicitLod instructions require DerivativeGroupQuadsKHR or DerivativeGroupLinearKHR execution mode for GLCompute".
- The DXIL `Sample` and `SampleBias` operations allow compute shaders from DXIL 1.6 ([llvm/lib/Target/DirectX/DXIL.td:L928-960]). llvm/llvm-project#231729 and llvm/llvm-project#234053 add `SampleCmp` and `SampleCmpBias` with the same stages. The `Status` forms use these operations, so DXIL needs no change.
- `__builtin_hlsl_resource_sample_with_status`, `__builtin_hlsl_resource_sample_bias_with_status`, `__builtin_hlsl_resource_sample_cmp_with_status`, `__builtin_hlsl_resource_sample_cmp_bias_with_status` and the intrinsics `llvm.spv.resource.sample.clamp.status`, `llvm.spv.resource.samplebias.clamp.status`, `llvm.spv.resource.samplecmp.clamp.status` and `llvm.spv.resource.samplecmpbias.clamp.status` do not exist yet. llvm/llvm-project#238527 adds those of `Sample` and `SampleBias`. llvm/llvm-project#238542 adds those of `SampleCmp` and `SampleCmpBias`. Use the names that those issues define.
- The shape check, the derivative-instruction reachability and their trigger lists do not exist yet. llvm/llvm-project#236218 adds them. llvm/llvm-project#236233 and llvm/llvm-project#236249 extend them to the overloads without a `Status` parameter.
### Suggested Implementation
<details>
<summary>Show details</summary>
#### HLSL Interface
```hlsl
uint s;
float4 a = tex.Sample(samp, location, offset, clamp, s);
float4 b = tex.SampleBias(samp, location, bias, offset, clamp, s);
float c = depth.SampleCmp(cmpSamp, location, compareValue, offset, clamp, s);
float d = depth.SampleCmpBias(cmpSamp, location, compareValue, bias, offset, clamp, s);
```
| Method | Compute availability on DirectX | Clang builtin | SPIR-V instruction |
| --- | --- | --- | --- |
| `Sample` | SM6.6 | `__builtin_hlsl_resource_sample_with_status` | `OpImageSparseSampleImplicitLod` |
| `SampleBias` | SM6.6 | `__builtin_hlsl_resource_sample_bias_with_status` | `OpImageSparseSampleImplicitLod` with `Bias` |
| `SampleCmp` | SM6.6 | `__builtin_hlsl_resource_sample_cmp_with_status` | `OpImageSparseSampleDrefImplicitLod` |
| `SampleCmpBias` | SM6.8 | `__builtin_hlsl_resource_sample_cmp_bias_with_status` | `OpImageSparseSampleDrefImplicitLod` with `Bias` |
Sources: [SM6.6 texture sample methods], [microsoft/DirectXShaderCompiler:docs/SPIR-V.rst:L2993-3090].
- Stage availability needs no code change. `addDerivativeAvailability` adds the derivative stage attributes to every method of the given name that the record already has ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L1922-1933]). The sample builders call it after they add the last overload ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L1974], [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L2001]). The `Status` overloads are added in the same builder functions, so they get the same attributes.
- In [clang/lib/Sema/SemaHLSL.cpp], add the four builtins of the HLSL interface table to the derivative operations of the shape check. The check runs in the `DiagnoseHLSLAvailability` walk, which visits each called function in `HandleFunctionOrMethodRef` ([clang/lib/Sema/SemaHLSL.cpp:L3301-3315]).
#### SPIR-V
- `addInstrRequirements` handles `OpImageSampleImplicitLod` and `OpImageSampleDrefImplicitLod` in [llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp:L2469-2482]. In [llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp], add `OpImageSparseSampleImplicitLod` and `OpImageSparseSampleDrefImplicitLod` to the derivative instructions that the compute-shader reachability finds.
- The `SparseResidency` and `MinLod` capabilities of these instructions and the `Status` value are not part of this issue.
| `numthreads` | Quad layout | Capability | Execution mode |
| --- | --- | --- | --- |
| X divisible by 4, Y = 1, Z = 1 | 1D quads | `ComputeDerivativeGroupLinearKHR` | `DerivativeGroupLinearKHR` |
| X and Y divisible by 2 | 2D quads | `ComputeDerivativeGroupQuadsKHR` | `DerivativeGroupQuadsKHR` |
Source: [SPV_KHR_compute_shader_derivatives].
#### Test Cases
##### `clang/test/SemaHLSL/Resources/Textures-derivative-availability.hlsl`
- The `Status` overloads of `Sample`, `SampleBias` and `SampleCmp` at `cs_6_6`, and of `SampleCmpBias` at `cs_6_8`, with `numthreads(4, 1, 1)` and `numthreads(2, 2, 2)`: accepted.
- The same calls with `numthreads(1, 1, 1)` and `numthreads(3, 1, 1)`: shape error.
- The calls through a helper function.
- The calls in a vertex shader on the DXIL and SPIR-V targets: unavailable.
- The calls at `cs_6_5`, and `SampleCmpBias` at `cs_6_7`, on the DXIL target: unavailable.
##### `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleStatusCompute.ll`
- `llc -O0 -mtriple=spirv1.6-vulkan1.3-compute` and `spirv-val --target-env vulkan1.3`, with an entry point.
- `llvm.spv.resource.sample.clamp.status`, `llvm.spv.resource.samplebias.clamp.status`, `llvm.spv.resource.samplecmp.clamp.status` and `llvm.spv.resource.samplecmpbias.clamp.status` in a 1D-quad and in a 2D-quad compute entry point, each in its own module, with the extension, capability and execution mode.
- A call in a function that a compute entry point calls.
- A second compute entry point without sparse sampling in the same module: no derivative-group execution mode on that entry point.
</details>
## References
<details>
<summary>HLSL</summary>
- [SM6.6 texture sample methods] — Compute availability of the implicit-LOD methods.
- [SM6.6 thread groups and quads] — 1D and 2D quad layouts.
- [HLSL tiled resources exposure] — `Status` parameters on HLSL resource methods.
- [microsoft/hlsl-specs:proposals/0014-expanded-comparison-sampling.md#hlsl-additions] — `SampleCmpBias` method.
</details>
<details>
<summary>SPIR-V</summary>
- [OpImageSparseSampleImplicitLod] — SPIR-V sparse sample with an implicit LOD.
- [OpImageSparseSampleDrefImplicitLod] — SPIR-V sparse depth-comparison sample with an implicit LOD.
- [SPV_KHR_compute_shader_derivatives] — Implicit-LOD instructions in compute shaders need a derivative-group execution mode.
- [microsoft/DirectXShaderCompiler:docs/SPIR-V.rst:L2993-3090] — DXC SPIR-V lowering of the sample methods with a `Status` parameter.
</details>
_Disclaimer: This issue was written with the help of AI and may contain some inaccuracies._
[clang/lib/Sema/SemaHLSL.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/SemaHLSL.cpp
[clang/lib/Sema/SemaHLSL.cpp:L3301-3315]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/SemaHLSL.cpp#L3301-L3315
[llvm/lib/Target/DirectX/DXIL.td:L928-960]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXIL.td#L928-L960
[clang/test/SemaHLSL/Resources/Textures-derivative-availability.hlsl]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/test/SemaHLSL/Resources/Textures-derivative-availability.hlsl
[clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L1922-1933]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp#L1922-L1933
[clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L1974]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp#L1974
[clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L2001]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp#L2001
[llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
[llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp:L2469-2482]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp#L2469-L2482
[microsoft/DirectXShaderCompiler:docs/SPIR-V.rst:L2993-3090]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/docs/SPIR-V.rst#L2993-L3090
[microsoft/hlsl-specs:proposals/0014-expanded-comparison-sampling.md#hlsl-additions]: https://github.com/microsoft/hlsl-specs/blob/5c1aae7ef249efba8a993ca50d69430c379ac1a1/proposals/0014-expanded-comparison-sampling.md#hlsl-additions
[SM6.6 texture sample methods]: https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_Derivatives.html#texture-sample-methods
[SM6.6 thread groups and quads]: https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_6_Derivatives.html#thread-groups-and-quads
[HLSL tiled resources exposure]: https://learn.microsoft.com/en-us/windows/win32/direct3d11/hlsl-tiled-resources-exposure
[OpImageSparseSampleImplicitLod]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageSparseSampleImplicitLod
[OpImageSparseSampleDrefImplicitLod]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageSparseSampleDrefImplicitLod
[SPV_KHR_compute_shader_derivatives]: https://github.khronos.org/SPIRV-Registry/extensions/KHR/SPV_KHR_compute_shader_derivatives.html
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs