Issue 234053
Summary [HLSL][DirectX][SPIRV] Add SampleCmpLevel, SampleCmpGrad and SampleCmpBias in Clang
Labels backend:DirectX, HLSL, backend:SPIR-V
Assignees Icohedron
Reporter Icohedron
    Add SampleCmpLevel, SampleCmpGrad and SampleCmpBias to Clang with DirectX and SPIR-V lowering and DXIL sampling feature flags. Test Level and Grad in vertex and pixel shaders, and Bias in pixel shaders.

## Acceptance Criteria

### Implement methods

- [ ] Add the methods in `Overloads` to the six comparison-capable textures, starting in [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp], [clang/lib/Sema/HLSLExternalSemaSource.cpp] and [clang/include/clang/Basic/Builtins.td].
- [ ] Emit the methods as intrinsics in [clang/lib/CodeGen/CGHLSLBuiltins.cpp], declaring them in [llvm/include/llvm/IR/IntrinsicsDirectX.td] and [llvm/include/llvm/IR/IntrinsicsSPIRV.td]; mark the `SampleCmpBias` intrinsics `IntrConvergent`.
- [ ] Lower the intrinsics to the DXIL operations in `DirectX`, starting in [llvm/lib/Target/DirectX/DXIL.td] and [llvm/lib/Target/DirectX/DXILOpLowering.cpp].
- [ ] Lower the intrinsics to the SPIR-V operations in `SPIR-V` in [llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp].

### Feature flags

- [ ] Set `AdvancedTextureOps` and `SampleCmpWithGradientOrBias` by the rules in `Sampling feature triggers` in [llvm/lib/Target/DirectX/DXILShaderFlags.cpp].

### Compiler tests

- [ ] Test accepted overloads and the rejections in `Availability and limits` in `clang/test/SemaHLSL/Resources/Textures-SampleCmpLevel.hlsl`, `clang/test/SemaHLSL/Resources/Textures-SampleCmpGrad.hlsl` and `clang/test/SemaHLSL/Resources/Textures-SampleCmpBias.hlsl` (new).
- [ ] Write CodeGen tests for the new methods, including the `SampleCmpBias` convergent call attribute, in `clang/test/CodeGenHLSL/resources/Textures-SampleCmpLevel.hlsl`, `clang/test/CodeGenHLSL/resources/Textures-SampleCmpGrad.hlsl` and `clang/test/CodeGenHLSL/resources/Textures-SampleCmpBias.hlsl` (new).
- [ ] Test DXIL lowering for each `Overloads` shape in `llvm/test/CodeGen/DirectX/SampleCmpLevel.ll`, `llvm/test/CodeGen/DirectX/SampleCmpGrad.ll` and `llvm/test/CodeGen/DirectX/SampleCmpBias.ll` (new).
- [ ] Test SPIR-V lowering for each `Overloads` shape in `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmpLevel.ll`, `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmpGrad.ll` and `llvm/test/CodeGen/SPIRV/hlsl-resources/SampleCmpBias.ll` (new).
- [ ] Test `SampleCmpBias` pixel acceptance and unsupported noncompute-stage rejection in [clang/test/SemaHLSL/Resources/Textures-derivative-availability.hlsl].
- [ ] Test every row of `Sampling feature triggers` in [llvm/test/CodeGen/DirectX/ShaderFlags/advanced-texture-ops.ll].

## DirectX

[docs/DXIL.rst][dxil-expanded-opcodes] identifies `SampleCmpLevel` (opcode 224), `SampleCmpGrad` (opcode 254) and `SampleCmpBias` (opcode 255), and [proposals/0014-expanded-comparison-sampling.md][expanded-dxil] specifies the `sampleCmpGrad` and `sampleCmpBias` operands.

### Sampling feature triggers

Bits are shader-property/SFI0. Check analysis output, `dx.entryPoints` tag 0 and SFI0.

| Flag | Set when | Not set by |
| --- | --- | --- |
| `AdvancedTextureOps` (34/29) | Every `SampleCmpLevel`, with or without offsets. | — |
| `AdvancedTextureOps` (34/29) | `SampleCmp`, `SampleCmpLevelZero`, `SampleCmpGrad` or `SampleCmpBias` has an offset that stays nonconstant after simplification. The existing `Sample`, `SampleBias`, `SampleLevel` and `SampleGrad` rule already does this. | Omitted, constant or folded offsets; dynamic non-offset operands. |
| `SampleCmpWithGradientOrBias` (37/31) | Every `SampleCmpGrad` and `SampleCmpBias`, including constant gradients, bias and offsets. | Ordinary `SampleGrad` and `SampleBias`; other comparison methods. |

[docs/DXIL.rst][dxil-flags] defines the shader-flag encodings.

## SPIR-V

[OpImageSampleDrefExplicitLod] with `Lod` or `Grad` implements `SampleCmpLevel` and `SampleCmpGrad`, and [OpImageSampleDrefImplicitLod] with `Bias` implements `SampleCmpBias`; [SPIR-V image operands] defines those operands.

## HLSL

### Overloads

`s` is `SamplerComparisonState`, `p` is the location, `r` is the float comparison reference, `o` is the constant spatial offset, `c` is the float minimum-LOD clamp, `lod` is the float LOD, `gx` and `gy` are spatial gradients and `b` is the float bias. Every overload returns scalar `float`. `Receiver shapes` gives the location, gradient and offset types.

| Method | `Texture1D`, `Texture1DArray`, `Texture2D`, `Texture2DArray` | `TextureCube`, `TextureCubeArray` |
| --- | --- | --- |
| `SampleCmpLevel` | `(s,p,r,lod)`, `(s,p,r,lod,o)` | `(s,p,r,lod)` |
| `SampleCmpGrad` | `(s,p,r,gx,gy)`, `(s,p,r,gx,gy,o)`, `(s,p,r,gx,gy,o,c)` | `(s,p,r,gx,gy)`, `(s,p,r,gx,gy,c)` |
| `SampleCmpBias` | `(s,p,r,b)`, `(s,p,r,b,o)`, `(s,p,r,b,o,c)` | `(s,p,r,b)`, `(s,p,r,b,c)` |

### Receiver shapes

| Receivers | Location | Spatial gradient | Constant spatial offset |
| --- | --- | --- | --- |
| `Texture1D<T>`, `Texture1DArray<T>` | `float`, `float2` | `float` | `int` |
| `Texture2D<T>`, `Texture2DArray<T>` | `float2`, `float3` | `float2` | `int2` |
| `TextureCube<T>`, `TextureCubeArray<T>` | `float3`, `float4` | `float3` | None |

### Availability and limits

| Subject | Rule |
| --- | --- |
| `SampleCmpLevel` | SM6.7/DXIL1.7; all stages. |
| `SampleCmpGrad`, `SampleCmpBias` | SM6.8/DXIL1.8; `SampleCmpGrad` in all stages, `SampleCmpBias` in pixel shaders. |
| Stages exercised here | `SampleCmpLevel` and `SampleCmpGrad` in vertex and pixel shaders; `SampleCmpBias` in pixel shaders. |
| Element types | Floating scalar or vector elements; native-half elements need SM6.2/DXIL1.2. |
| Bias and offsets | Shader bias may be dynamic, and DXIL limits it to [-16,15.99]; before SM6.7, offsets are immediates in [-8,7]. |
| Rejected sources | Ordinary samplers; integer, unsigned, double, bool and aggregate elements; `Texture3D`, multisampled and read-write receivers; status outputs. |
| Compatibility | Support scalar-float `TextureCubeArray.SampleCmpGrad`, although the proposal excludes it. |

[SM6.7 SampleCmpLevel] defines `SampleCmpLevel`; [proposals/0014-expanded-comparison-sampling.md][expanded-hlsl] defines `SampleCmpGrad` and `SampleCmpBias`, and its validation section, [proposals/0014-expanded-comparison-sampling.md][expanded-validation], gives the model and bias rules.

[clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp
[clang/include/clang/Basic/Builtins.td]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/clang/include/clang/Basic/Builtins.td
[clang/lib/Sema/HLSLExternalSemaSource.cpp]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/clang/lib/Sema/HLSLExternalSemaSource.cpp
[clang/lib/CodeGen/CGHLSLBuiltins.cpp]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/clang/lib/CodeGen/CGHLSLBuiltins.cpp
[llvm/include/llvm/IR/IntrinsicsDirectX.td]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/llvm/include/llvm/IR/IntrinsicsDirectX.td
[llvm/include/llvm/IR/IntrinsicsSPIRV.td]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/llvm/include/llvm/IR/IntrinsicsSPIRV.td
[llvm/lib/Target/DirectX/DXILOpLowering.cpp]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/llvm/lib/Target/DirectX/DXILOpLowering.cpp
[llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
[clang/test/SemaHLSL/Resources/Textures-derivative-availability.hlsl]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/clang/test/SemaHLSL/Resources/Textures-derivative-availability.hlsl
[llvm/lib/Target/DirectX/DXILShaderFlags.cpp]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
[llvm/test/CodeGen/DirectX/ShaderFlags/advanced-texture-ops.ll]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/llvm/test/CodeGen/DirectX/ShaderFlags/advanced-texture-ops.ll
[dxil-expanded-opcodes]: https://github.com/microsoft/DirectXShaderCompiler/blob/791e06620e6eccad215440ff2af6f37620589115/docs/DXIL.rst#L2363-L2394
[expanded-dxil]: https://github.com/microsoft/hlsl-specs/blob/5c1aae7ef249efba8a993ca50d69430c379ac1a1/proposals/0014-expanded-comparison-sampling.md#dxil-additions
[dxil-flags]: https://github.com/microsoft/DirectXShaderCompiler/blob/791e06620e6eccad215440ff2af6f37620589115/docs/DXIL.rst#shader-flags
[OpImageSampleDrefImplicitLod]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageSampleDrefImplicitLod
[OpImageSampleDrefExplicitLod]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageSampleDrefExplicitLod
[SPIR-V image operands]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Image_Operands
[SM6.7 SampleCmpLevel]: https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_7_Advanced_Texture_Ops.html#samplecmplevel
[expanded-hlsl]: https://github.com/microsoft/hlsl-specs/blob/5c1aae7ef249efba8a993ca50d69430c379ac1a1/proposals/0014-expanded-comparison-sampling.md#hlsl-additions
[expanded-validation]: https://github.com/microsoft/hlsl-specs/blob/5c1aae7ef249efba8a993ca50d69430c379ac1a1/proposals/0014-expanded-comparison-sampling.md#validation-changes
[llvm/lib/Target/DirectX/DXIL.td]: https://github.com/llvm/llvm-project/blob/1b4e7f41268eb3db5300e79256764140e509e3f5/llvm/lib/Target/DirectX/DXIL.td

_Disclaimer: This issue was written with the help of AI and may contain some inaccuracies._

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to