| Issue |
238137
|
| Summary |
[SPIRV] Lower programmable offsets in Sample, SampleBias, SampleGrad, SampleLevel, SampleCmp and SampleCmpLevelZero
|
| Labels |
HLSL,
backend:SPIR-V
|
| Assignees |
|
| Reporter |
Icohedron
|
The SPIR-V backend reports "Non-constant offsets are not supported in sample instructions." for a programmable offset in `Sample`, `SampleBias`, `SampleGrad`, `SampleLevel`, `SampleCmp` and `SampleCmpLevelZero` (https://godbolt.org/z/Go45bd7jo). Lower these offsets to the `Offset` image operand. Make the driver's `spirv-val` job accept the `Offset` image operand on non-gather image instructions.
A Vulkan device runs these shaders only when it enables the `maintenance8` feature of `VK_KHR_maintenance8`.
## Acceptance Criteria
### SPIR-V
- [ ] Lower programmable offsets of the sample instructions to the `Offset` image operand in [llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp].
- [ ] Remove the tests of the non-constant offset error, which this change makes obsolete: delete [llvm/test/CodeGen/SPIRV/hlsl-resources/SampleErrors.ll] and [llvm/test/CodeGen/SPIRV/hlsl-resources/SampleErrorsDebug.ll].
- [ ] Add SPIR-V lowering tests of programmable offsets to [llvm/test/CodeGen/SPIRV/hlsl-resources/Sample.ll].
- [ ] Add SPIR-V lowering tests of programmable offsets to [llvm/test/CodeGen/SPIRV/hlsl-resources/SampleBias.ll].
- [ ] Add SPIR-V lowering tests of programmable offsets to [llvm/test/CodeGen/SPIRV/hlsl-resources/SampleGrad.ll].
- [ ] Add SPIR-V lowering tests of programmable offsets to [llvm/test/CodeGen/SPIRV/hlsl-resources/SampleLevel.ll].
- [ ] Add SPIR-V lowering tests of programmable offsets to [llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmp.ll].
- [ ] Add SPIR-V lowering tests of programmable offsets to [llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmpLevelZero.ll].
### Driver
- [ ] Pass `--allow-offset-texture-operand` to `spirv-val` for Vulkan targets in [clang/lib/Driver/ToolChains/HLSL.cpp].
- [ ] Add driver tests that check the `--allow-offset-texture-operand` argument of the `spirv-val` command to [clang/test/Driver/dxc_spirv-val_path.hlsl].
## Notes
- `Load` with a programmable offset already lowers to `OpImageFetch` with `Offset`. Today the driver's `spirv-val` job rejects it with `VUID-RuntimeSpirv-Offset-10213`.
- `spirv-val` checks `VUID-RuntimeSpirv-Offset-10213` only with a Vulkan `--target-env`. The existing `spirv-val` RUN lines of the sample tests pass no target environment, so they do not catch an `Offset` on a sample instruction.
- Unlike DXIL, SPIR-V has no shader-model or range rule for texel offsets. Do not add one.
### Suggested Implementation
<details>
<summary>Show details</summary>
#### SPIR-V
- `generateSampleImage` ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6155-6164]) sets no operand for a zero offset and `ConstOffset` (0x8) for a constant offset. For any other offset it reports the error and fails. Set `Offset` (0x10) there instead. The operand emission at [llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6168-6183] already accepts both bits.
- `generateImageReadOrFetch` already makes this choice for `OpImageFetch` ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6615-6623]).
- `generateSampleImage` serves `llvm.spv.resource.sample`, `sample.clamp`, `samplebias`, `samplebias.clamp`, `samplegrad`, `samplegrad.clamp`, `samplelevel`, `samplecmp`, `samplecmp.clamp` and `samplecmplevelzero`.
- The selector adds no capability. The `Offset` image operand requires `ImageGatherExtended` ([llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td:L1184-1186]). `addImageOperandReqs` adds the requirements of each set mask bit ([llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp:L1577-1587]) for the four `OpImageSample*` opcodes ([llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp:L2469-2483]).
- Delete `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleErrors.ll` and `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleErrorsDebug.ll`. They check only this error, without and with a debug location, and no other error remains in `generateSampleImage` for them to check.
#### Driver
- `Validator::ConstructJob` ([clang/lib/Driver/ToolChains/HLSL.cpp:L298-331]) runs `spirv-val --target-env <os> --scalar-block-layout <file>` for SPIR-V targets. Add `--allow-offset-texture-operand` to that command.
#### Test Cases
##### `llvm/test/CodeGen/SPIRV/hlsl-resources/Sample.ll`
- `llvm.spv.resource.sample` with a programmable offset: `OpImageSampleImplicitLod` with `Offset`.
- `llvm.spv.resource.sample.clamp` with a programmable offset: `Offset|MinLod`, offset before the minimum LOD.
- `OpCapability ImageGatherExtended`.
- A Vulkan `spirv-val` RUN line: `spirv-val --target-env vulkan1.3 --allow-offset-texture-operand`. Vulkan rejects the `Linkage` capability, so the module needs an entry point: an `"hlsl.shader"` attribute, as in [llvm/test/CodeGen/SPIRV/hlsl-resources/LoadLevel.ll]. Implicit-LOD files (`Sample.ll`, `SampleBias.ll`, `SampleCmp.ll`) use a `spirv1.6-vulkan1.3-pixel` triple and `"hlsl.shader"="pixel"`. Explicit-LOD files (`SampleGrad.ll`, `SampleLevel.ll`, `SampleCmpLevelZero.ll`) use `spirv1.6-vulkan1.3-compute` with `"hlsl.shader"="compute"` and `"hlsl.numthreads"`. An entry point has no parameters, so it reads the offset from a buffer.
##### `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleBias.ll`
- `llvm.spv.resource.samplebias` with a programmable offset: `Bias|Offset`.
- `llvm.spv.resource.samplebias.clamp` with a programmable offset: `Bias|Offset|MinLod`.
- `OpCapability ImageGatherExtended` and the Vulkan `spirv-val` RUN line.
##### `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleGrad.ll`
- `llvm.spv.resource.samplegrad` with a programmable offset: `OpImageSampleExplicitLod` with `Grad|Offset`, both gradients before the offset.
- `llvm.spv.resource.samplegrad.clamp` with a programmable offset: `Grad|Offset|MinLod`.
- `OpCapability ImageGatherExtended` and the Vulkan `spirv-val` RUN line.
##### `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleLevel.ll`
- `llvm.spv.resource.samplelevel` with a programmable offset: `Lod|Offset`.
- `OpCapability ImageGatherExtended` and the Vulkan `spirv-val` RUN line.
##### `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmp.ll`
- `llvm.spv.resource.samplecmp` with a programmable offset: `OpImageSampleDrefImplicitLod` with `Offset` after the reference value.
- `llvm.spv.resource.samplecmp.clamp` with a programmable offset: `Offset|MinLod`.
- `OpCapability ImageGatherExtended` and the Vulkan `spirv-val` RUN line.
##### `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmpLevelZero.ll`
- `llvm.spv.resource.samplecmplevelzero` with a programmable offset: `OpImageSampleDrefExplicitLod` with `Lod|Offset`.
- `OpCapability ImageGatherExtended` and the Vulkan `spirv-val` RUN line.
##### `clang/test/Driver/dxc_spirv-val_path.hlsl`
- The `SPIRV_VAL_PATH` and `SPIRV_VAL_VK12` checks expect `--allow-offset-texture-operand` in the `spirv-val` command.
</details>
## References
<details>
<summary>HLSL</summary>
- [Texture2D::Sample] — HLSL method reference.
- [Texture2D::SampleBias] — HLSL method reference.
- [Texture2D::SampleGrad] — HLSL method reference.
- [Texture2D::SampleLevel] — HLSL method reference.
- [Texture2D::SampleCmp] — HLSL method reference.
- [Texture2D::SampleCmpLevelZero] — HLSL method reference.
</details>
<details>
<summary>SPIR-V</summary>
- [SPIR-V Image Operands] — `Offset` and `ConstOffset` image operands.
- [OpImageSampleImplicitLod] — SPIR-V implicit-LOD sample instruction.
- [OpImageSampleDrefImplicitLod] — SPIR-V implicit-LOD depth-comparison sample instruction.
- [OpImageSampleExplicitLod] — SPIR-V explicit-LOD sample instruction.
- [OpImageSampleDrefExplicitLod] — SPIR-V explicit-LOD depth-comparison sample instruction.
- [VUID-RuntimeSpirv-Offset-10213] — Vulkan rule: the `Offset` image operand is allowed on non-gather instructions only with `maintenance8`.
- [VK_KHR_maintenance8] — Vulkan extension that provides the `maintenance8` feature.
</details>
_Disclaimer: This issue was written with the help of AI and may contain some inaccuracies._
[llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
[llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6155-6164]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp#L6155-L6164
[llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6168-6183]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp#L6168-L6183
[llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6615-6623]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp#L6615-L6623
[llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td:L1184-1186]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVSymbolicOperands.td#L1184-L1186
[llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp:L1577-1587]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp#L1577-L1587
[llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp:L2469-2483]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp#L2469-L2483
[llvm/test/CodeGen/SPIRV/hlsl-resources/SampleErrors.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/SampleErrors.ll
[llvm/test/CodeGen/SPIRV/hlsl-resources/SampleErrorsDebug.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/SampleErrorsDebug.ll
[llvm/test/CodeGen/SPIRV/hlsl-resources/Sample.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/Sample.ll
[llvm/test/CodeGen/SPIRV/hlsl-resources/SampleBias.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/SampleBias.ll
[llvm/test/CodeGen/SPIRV/hlsl-resources/SampleGrad.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/SampleGrad.ll
[llvm/test/CodeGen/SPIRV/hlsl-resources/SampleLevel.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/SampleLevel.ll
[llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmp.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmp.ll
[llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmpLevelZero.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmpLevelZero.ll
[llvm/test/CodeGen/SPIRV/hlsl-resources/LoadLevel.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/LoadLevel.ll
[clang/lib/Driver/ToolChains/HLSL.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Driver/ToolChains/HLSL.cpp
[clang/lib/Driver/ToolChains/HLSL.cpp:L298-331]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Driver/ToolChains/HLSL.cpp#L298-L331
[clang/test/Driver/dxc_spirv-val_path.hlsl]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/test/Driver/dxc_spirv-val_path.hlsl
[Texture2D::Sample]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/texture-sample-overload
[Texture2D::SampleBias]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/texture2d-samplebias
[Texture2D::SampleGrad]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/texture2d-samplegrad
[Texture2D::SampleLevel]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/texture2d-samplelevel
[Texture2D::SampleCmp]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/texture2d-samplecmp
[Texture2D::SampleCmpLevelZero]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/texture2d-samplecmplevelzero
[SPIR-V Image Operands]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Image_Operands
[OpImageSampleImplicitLod]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageSampleImplicitLod
[OpImageSampleDrefImplicitLod]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageSampleDrefImplicitLod
[OpImageSampleExplicitLod]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageSampleExplicitLod
[OpImageSampleDrefExplicitLod]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageSampleDrefExplicitLod
[VUID-RuntimeSpirv-Offset-10213]: https://docs.vulkan.org/spec/latest/appendices/spirvenv.html#VUID-RuntimeSpirv-Offset-10213
[VK_KHR_maintenance8]: https://docs.vulkan.org/refpages/latest/refpages/source/VK_KHR_maintenance8.html
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs