| Issue |
238511
|
| Summary |
[DirectX] Report an error for texel offsets that are not constants in [-8, 7] below Shader Model 6.7
|
| Labels |
backend:DirectX,
HLSL
|
| Assignees |
|
| Reporter |
Icohedron
|
Below Shader Model 6.7, each texel offset of a DXIL sample or load operation must be a constant in [-8, 7]. Clang does not check this rule; only the DXIL validator reports a programmable offset or an out-of-range constant offset. Today Clang emits DXIL for a `Load` with an offset of 8 and for a `Load` with a run-time offset at Shader Model 6.6 (https://godbolt.org/z/8vaEo64x9). Report an error, after optimization, when an offset of `Load`, `Sample`, `SampleBias`, `SampleGrad` or `SampleLevel` breaks this rule.
## Acceptance Criteria
### DirectX
- [ ] Report an error in [llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp] when, below Shader Model 6.7, an offset component of a `Load`, `Sample`, `SampleBias`, `SampleGrad` or `SampleLevel` operation is not a constant in [-8, 7] after optimization.
### Tests
- [ ] Add DXIL error tests that check each offset error at Shader Model 6.6 and accept the same calls at Shader Model 6.7 to `llvm/test/CodeGen/DirectX/texel-offset-errors.ll` (new).
- [ ] Add DXIL tests that accept constant offsets in [-8, 7] at Shader Model 6.6 to `llvm/test/CodeGen/DirectX/texel-offset-valid.ll` (new).
## Notes
- The check must run after optimization. With optimization enabled (`-O1` and above), a source offset that folds to a legal constant, such as an `int2` local variable that holds constants, is accepted. Do not add a Sema rule.
- At `-O0`, Clang does not promote the offset parameter of texture methods to an SSA value. Every offset, including a literal, reaches the check as a load, so the check reports an error. DXC accepts these shaders at `-Od`. At this revision, `-O0` code that uses resources fails before this pass.
- Gather operations have different offset rules. Do not check `llvm.dx.resource.gather` or `llvm.dx.resource.gather.cmp`.
### Suggested Implementation
<details>
<summary>Show details</summary>
#### DirectX
- Put the check in `DXILPostOptimizationValidation`. In `addCodeGenPrepare` this pass runs after the last IR transforms (scalarizer, dead store elimination, `DXILLegalize`) and before `DXILOpLowering` ([llvm/lib/Target/DirectX/DirectXTargetMachine.cpp:L137-152]). It sees each offset as one intrinsic operand, before op lowering splits it into scalar operands.
- The pass already has `ModuleMetadataInfo` ([llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp:L332-342]). `ShaderModelVersion` comes from the target triple ([llvm/lib/Analysis/DXILMetadataAnalysis.cpp:L41]). Add a static `reportInvalidTexelOffsets` function and call it from `reportErrors` ([llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp:L314-330]) when `ShaderModelVersion` is less than 6.7. It checks the calls of the intrinsics in the operand table below: `llvm.dx.resource.load.level` and the ordinary sample intrinsics with their clamp forms.
- Report each error with `DiagnosticInfoGenericWithLoc` on the call, as `reportInvalidDirection` does ([llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp:L46-70]). Report one error per call. A non-constant component takes priority over an out-of-range component.
- Check each component with `findScalarElement` ([llvm/include/llvm/Analysis/VectorUtils.h:L219]). After optimization, an offset with run-time and constant components is an `insertelement` chain, such as `insertelement <2 x i32> %x, i32 9, i32 1`. A 1D offset is a scalar `i32`.
| Intrinsic | Offset operand index |
| --- | --- |
| `llvm.dx.resource.load.level` | 3 |
| `llvm.dx.resource.sample`, `llvm.dx.resource.sample.clamp` | 3 |
| `llvm.dx.resource.samplebias`, `llvm.dx.resource.samplebias.clamp` | 4 |
| `llvm.dx.resource.samplelevel` | 4 |
| `llvm.dx.resource.samplegrad`, `llvm.dx.resource.samplegrad.clamp` | 5 |
Source: [llvm/include/llvm/IR/IntrinsicsDirectX.td:L146-191].
| Component value | Diagnostic |
| --- | --- |
| Not a `ConstantInt` (run-time value, `undef` or `poison`) | `Offsets to texture access operations must be immediate values below Shader Model 6.7.` |
| `ConstantInt` with a sign-extended value less than -8 or greater than 7 | `Offsets to texture access operations must be between -8 and 7 below Shader Model 6.7.` |
The texts follow DXC's `DxilLegalizeSampleOffsetPass` errors ([microsoft/DirectXShaderCompiler:lib/HLSL/DxilLegalizeSampleOffsetPass.cpp:L191-222]) and add the shader model. DXC skips the check from Shader Model 6.7 ([microsoft/DirectXShaderCompiler:lib/HLSL/DxilLegalizeSampleOffsetPass.cpp:L84-91]). The validator rule `Instr.TextureOffset` applies the same limits ([microsoft/DirectXShaderCompiler:lib/DxilValidation/DxilValidation.cpp:L364-390]).
#### Test Cases
##### `llvm/test/CodeGen/DirectX/texel-offset-errors.ll`
- No `target triple` in the module. RUN 1: `not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.6-compute %s 2>&1 | FileCheck %s`, as in [llvm/test/CodeGen/DirectX/resource_counter_error.ll]. RUN 2: `opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.7-compute %s 2>&1 | FileCheck %s --check-prefix=SM67 --implicit-check-not='error:'`, with at least one `SM67:` line, such as one on a `define`. The `opt -S` output holds the file name, so `--implicit-check-not=error` without the colon always fails.
- `opt` prints every error of the module and then exits with status 1, so one module can hold all error cases. Without debug information, each error starts with `error: <unknown>:0:0:`.
- A run-time offset (a function argument) for each intrinsic in the operand table.
- Constant offsets with 8 on the x component and with -9 on the y component.
- Mixed components: a run-time x component with a constant y component, and a constant x component with a run-time y component.
- A 1D `i32` offset of 8 and a 3D `<3 x i32>` offset with a z component of -9.
- A constant vector with a `poison` component.
##### `llvm/test/CodeGen/DirectX/texel-offset-valid.ll`
- RUN: `opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.6-compute %s 2>&1 | FileCheck %s --implicit-check-not='error:'`.
- Offsets of -8 and 7 on each component, for `load.level` and each sample intrinsic.
- `zeroinitializer` offsets, which Clang emits for an omitted offset.
- A 1D `i32` offset and a 3D `<3 x i32>` offset.
</details>
## References
<details>
<summary>HLSL</summary>
- [Texture2D::Load] — HLSL method reference.
- [Texture2D::Sample] — HLSL method reference.
- [Texture2D::SampleBias] — HLSL method reference.
- [Texture2D::SampleGrad] — HLSL method reference.
- [Texture2D::SampleLevel] — HLSL method reference.
- [Programmable offsets] — Shader Model 6.7 programmable offsets for sample and load operations.
</details>
<details>
<summary>DirectX</summary>
- [microsoft/DirectXShaderCompiler:docs/DXIL.rst:L3332] — DXIL validation rule `INSTR.TEXTUREOFFSET`.
</details>
_Disclaimer: This issue was written with the help of AI and may contain some inaccuracies._
[llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp
[llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp:L46-70]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp#L46-L70
[llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp:L314-330]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp#L314-L330
[llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp:L332-342]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp#L332-L342
[llvm/lib/Target/DirectX/DirectXTargetMachine.cpp:L137-152]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DirectXTargetMachine.cpp#L137-L152
[llvm/lib/Analysis/DXILMetadataAnalysis.cpp:L41]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Analysis/DXILMetadataAnalysis.cpp#L41
[llvm/include/llvm/Analysis/VectorUtils.h:L219]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/include/llvm/Analysis/VectorUtils.h#L219
[llvm/include/llvm/IR/IntrinsicsDirectX.td:L146-191]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/include/llvm/IR/IntrinsicsDirectX.td#L146-L191
[llvm/test/CodeGen/DirectX/resource_counter_error.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/DirectX/resource_counter_error.ll
[microsoft/DirectXShaderCompiler:lib/HLSL/DxilLegalizeSampleOffsetPass.cpp:L84-91]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/lib/HLSL/DxilLegalizeSampleOffsetPass.cpp#L84-L91
[microsoft/DirectXShaderCompiler:lib/HLSL/DxilLegalizeSampleOffsetPass.cpp:L191-222]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/lib/HLSL/DxilLegalizeSampleOffsetPass.cpp#L191-L222
[microsoft/DirectXShaderCompiler:lib/DxilValidation/DxilValidation.cpp:L364-390]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/lib/DxilValidation/DxilValidation.cpp#L364-L390
[microsoft/DirectXShaderCompiler:docs/DXIL.rst:L3332]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/docs/DXIL.rst#L3332
[Texture2D::Load]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/texture2d-load
[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
[Programmable offsets]: https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_7_Advanced_Texture_Ops.html#programmable-offsets
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs