Issue 238558
Summary [HLSL][DirectX] Implement `WriteSamplerFeedbackBias`
Labels backend:DirectX, HLSL
Assignees
Reporter Icohedron
    Add the `WriteSamplerFeedbackBias` method and its clamp overload to `FeedbackTexture2D` and `FeedbackTexture2DArray`. Make the method available from Shader Model 6.5 in pixel shaders only. Lower it to the DXIL `WriteSamplerFeedbackBias` operation. Clamp a constant bias to the range of that operation. Set the `SamplerFeedback` shader flag for every `WriteSamplerFeedbackBias` call when the validator version is 1.9 or later.

## Acceptance Criteria

### `WriteSamplerFeedbackBias`

- [ ] Declare `WriteSamplerFeedbackBias`, with and without a clamp argument, on `FeedbackTexture2D` and `FeedbackTexture2DArray`, available from SM6.5 in pixel shaders only, in [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp] and [clang/lib/Sema/HLSLExternalSemaSource.cpp].
- [ ] Add the `__builtin_hlsl_resource_write_sampler_feedback_bias` builtin and check its arguments in [clang/include/clang/Basic/Builtins.td] and [clang/lib/Sema/SemaHLSL.cpp].
- [ ] Emit the `llvm.dx.resource.writesamplerfeedbackbias` and `llvm.dx.resource.writesamplerfeedbackbias.clamp` intrinsics for `WriteSamplerFeedbackBias` calls in [clang/lib/CodeGen/CGHLSLBuiltins.cpp], and declare them in [llvm/include/llvm/IR/IntrinsicsDirectX.td].
- [ ] Add Sema tests to `clang/test/SemaHLSL/Resources/FeedbackTextures-WriteSamplerFeedbackBias.hlsl` (new).
- [ ] Add HLSL codegen tests to `clang/test/CodeGenHLSL/resources/FeedbackTextures-WriteSamplerFeedbackBias.hlsl` (new).

### DirectX

- [ ] Lower `llvm.dx.resource.writesamplerfeedbackbias` and `llvm.dx.resource.writesamplerfeedbackbias.clamp` to the DXIL `WriteSamplerFeedbackBias` operation in [llvm/lib/Target/DirectX/DXIL.td] and [llvm/lib/Target/DirectX/DXILOpLowering.cpp].
- [ ] Clamp a finite constant bias outside −16.0 to 15.99 to the nearest endpoint, and pass a nonconstant bias unchanged, when lowering `llvm.dx.resource.writesamplerfeedbackbias` and `llvm.dx.resource.writesamplerfeedbackbias.clamp` in [llvm/lib/Target/DirectX/DXILOpLowering.cpp].
- [ ] Set the `SamplerFeedback` shader flag for every `WriteSamplerFeedbackBias` call when the validator version is 1.9 or later in [llvm/lib/Target/DirectX/DXILShaderFlags.cpp].
- [ ] Add DXIL lowering tests to `llvm/test/CodeGen/DirectX/WriteSamplerFeedbackBias.ll` (new).
- [ ] Extend the DXIL operation error tests in `llvm/test/CodeGen/DirectX/WriteSamplerFeedback-errors.ll`, which llvm/llvm-project#238555 adds, to reject `WriteSamplerFeedbackBias` below SM6.5 and outside pixel and library modules.
- [ ] Extend the shader flag tests in `llvm/test/CodeGen/DirectX/ShaderFlags/sampler-feedback.ll`, which llvm/llvm-project#238555 adds, to check that `WriteSamplerFeedbackBias` sets `SamplerFeedback` with validator 1.9 and not with validator 1.8.
- [ ] Document the `llvm.dx.resource.writesamplerfeedbackbias` and `llvm.dx.resource.writesamplerfeedbackbias.clamp` intrinsics and their lowering to the DXIL `WriteSamplerFeedbackBias` operation in the "Resource Operations" section of [llvm/docs/DirectX/DXILResources.md].

## Notes

- The feedback specification gives the bias range −16.0 to 15.99 but defines no behavior outside it. Clamping finite constants matches DXC ([microsoft/DirectXShaderCompiler:lib/HLSL/HLOperationLower.cpp:L3293-3304]); DXC also clamps ±inf. The clamp is a deliberate Clang choice, not a language rule. Add no Sema diagnostic for an out-of-range bias, and do not change ordinary `SampleBias` lowering.
- `addDerivativeAvailability` ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L1922-1933]) makes implicit-derivative methods available in compute, mesh and amplification shaders from SM6.6. `WriteSamplerFeedbackBias` is pixel-only at every shader model. Do not use that helper.
- The DXIL operation computes the implicit derivatives itself. Do not emit `ddx` or `ddy`, and do not convert the call to `WriteSamplerFeedbackGrad` or `WriteSamplerFeedback`.
- `CheckSamplingBuiltin` rejects integer element types below SM6.7 ([clang/lib/Sema/SemaHLSL.cpp:L4449-4543]). `WriteSamplerFeedbackBias` accepts every element type of the paired texture at SM6.5. Do not reuse that check.
- LLVM reads the validator version from the `!dx.valver` module metadata. Without that metadata, opt and llc use 0.0 ([llvm/lib/Analysis/DXILMetadataAnalysis.cpp:L43-50]), so an LLVM test without `!dx.valver` 1.9 gets no `SamplerFeedback` flag.
- Below DXIL 1.6, LLVM lowers resource handles to `createHandle` ([llvm/lib/Target/DirectX/DXILOpLowering.cpp:L432-436]). SM6.5 output has no `annotateHandle`. Check handle annotations only at SM6.6 or later.

### Suggested Implementation

<details>
<summary>Show details</summary>

#### HLSL Interface

```hlsl
void WriteSamplerFeedbackBias(Texture2D<T> Tex, SamplerState S, float2 Location, float Bias);                   // FeedbackTexture2D
void WriteSamplerFeedbackBias(Texture2D<T> Tex, SamplerState S, float2 Location, float Bias, float Clamp);      // FeedbackTexture2D
void WriteSamplerFeedbackBias(Texture2DArray<T> Tex, SamplerState S, float3 Location, float Bias);              // FeedbackTexture2DArray
void WriteSamplerFeedbackBias(Texture2DArray<T> Tex, SamplerState S, float3 Location, float Bias, float Clamp); // FeedbackTexture2DArray
```

Sources: [Sampler Feedback: HLSL constructs], [microsoft/DirectXShaderCompiler:utils/hct/gen_intrin_main.txt:L1040-1041] and [microsoft/DirectXShaderCompiler:utils/hct/gen_intrin_main.txt:L1052-1053] (the clamp overloads).

- `T` is any element type that `Texture2D` and `Texture2DArray` accept, including integer and `half` types. The `z` component of the `FeedbackTexture2DArray` location is the array layer.
- No offset overload and no status overload exist.

- In [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp], add both overloads beside `WriteSamplerFeedbackLevel`, with the same member template over the element type of the paired texture. Model the parameters on `addSampleBiasMethods` ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L2004-2077]): the bias follows the location, and the clamp is the last builtin argument. Install the overloads from the feedback setup function in [clang/lib/Sema/HLSLExternalSemaSource.cpp].
- Mark each overload with one implicit `AvailabilityAttr` for the `shadermodel` platform, the `pixel` environment and version 6.5, built as in `addDerivativeAvailabilityAttrs` ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L120-139]). `FindAvailabilityAttr` falls back to this attribute in other stages ([clang/lib/Sema/SemaHLSL.cpp:L3435-3458]). `CheckDeclAvailability` then reports the method as unavailable ([clang/lib/Sema/SemaHLSL.cpp:L3492-3520]). Library targets use the existing reachability-based diagnostics.
- Add the pixel-only helper beside `addDerivativeAvailability`. {{121}} needs the same helper, and neither issue blocks the other. The issue that lands first adds the helper; the other reuses it.
- Add `__builtin_hlsl_resource_write_sampler_feedback_bias(FeedbackHandle, TextureHandle, SamplerHandle, Location, Bias[, Clamp])` to [clang/include/clang/Basic/Builtins.td], modelled on `__builtin_hlsl_resource_sample_bias` ([clang/include/clang/Basic/Builtins.td:L5444-5448]).
- In [clang/lib/Sema/SemaHLSL.cpp], reuse the feedback handle and location checks that llvm/llvm-project#238555 adds. Accept five or six arguments. Check a scalar `float` bias and an optional scalar `float` clamp, as `CheckSamplingBuiltin` does ([clang/lib/Sema/SemaHLSL.cpp:L4484-4490], [clang/lib/Sema/SemaHLSL.cpp:L4514-4519]). Set the result type to `void`.
- In [clang/lib/CodeGen/CGHLSLBuiltins.cpp], emit `llvm.dx.resource.writesamplerfeedbackbias(feedback, texture, sampler, location, bias)` without a clamp argument and `llvm.dx.resource.writesamplerfeedbackbias.clamp(feedback, texture, sampler, location, bias, clamp)` with one, as `__builtin_hlsl_resource_sample_bias` selects `samplebias` or `samplebias.clamp` ([clang/lib/CodeGen/CGHLSLBuiltins.cpp:L834-858]). Pass the bias unchanged.
- Declare `int_dx_resource_writesamplerfeedbackbias` (operands `llvm_any_ty` × 4 and `llvm_float_ty`) and `int_dx_resource_writesamplerfeedbackbias_clamp` (one more `llvm_float_ty`) in [llvm/include/llvm/IR/IntrinsicsDirectX.td] with no result and `[IntrWriteMem, IntrConvergent]`. `int_dx_resource_samplebias` ([llvm/include/llvm/IR/IntrinsicsDirectX.td:L157-167]) is the convergent model. `int_dx_resource_store_texture` ([llvm/include/llvm/IR/IntrinsicsDirectX.td:L198-200]) is the write-side-effect model. The names follow [llvm/wg-hlsl:proposals/0014-consistent-naming-for-dx-intrinsics.md:L76-79].

#### DirectX

[microsoft/DirectXShaderCompiler:utils/hct/hctdb.py:L4263-4284] defines opcode 175 with this operand layout:

| Operand | Value |
| --- | --- |
| 0 | `i32 175` |
| 1 | Feedback texture handle |
| 2 | Paired texture handle |
| 3 | Sampler handle |
| 4–7 | Coordinates c0–c3. `c2` holds the array layer for `FeedbackTexture2DArray`. Set each unused coordinate to `undef`. |
| 8 | Bias, `float`, in −16.0 to 15.99 |
| 9 | Clamp, `float`. Set it to `undef` when the call has no clamp, never to 0. |

The result is `void`. The operation has no overloads and no offsets. It needs SM6.5 and is legal only in pixel shaders and libraries ([microsoft/DirectXShaderCompiler:utils/hct/hctdb.py:L961-969]).

1. Add a `WriteSamplerFeedbackBias : DXILOp<175, writeSamplerFeedbackBias>` record to [llvm/lib/Target/DirectX/DXIL.td]. The `writeSamplerFeedbackBias` class already exists ([llvm/lib/Target/DirectX/DXIL.td:L246]). Set `arguments` to the operands in the operand table: three `HandleTy`, then four `FloatTy` coordinates, a `FloatTy` bias and a `FloatTy` clamp. Use a `VoidTy` result, no `overloads`, no attributes and `Stages<DXIL1_5, [library, pixel]>`. `SampleBias` ([llvm/lib/Target/DirectX/DXIL.td:L945-960]) is the stage-list model. `DXILOpBuilder::tryCreateOp` then reports "No valid stage for DXIL version 1.4" below SM6.5 and "Invalid stage" in other stages ([llvm/lib/Target/DirectX/DXILOpBuilder.cpp:L565-583]).
2. In [llvm/lib/Target/DirectX/DXILOpLowering.cpp], call `lowerWriteSamplerFeedbackOp(F, OpCode::WriteSamplerFeedbackBias, EmitExtraArgs)`, which llvm/llvm-project#238555 adds. The callback appends the bias, then the clamp operand or `undef` for the non-clamp intrinsic, as `lowerSampleBias` does ([llvm/lib/Target/DirectX/DXILOpLowering.cpp:L817-828]). Dispatch both intrinsics beside `dx_resource_samplebias` ([llvm/lib/Target/DirectX/DXILOpLowering.cpp:L1444-1449]).
3. In the same callback, clamp the bias as the bias table below shows. Add no runtime `minnum` or `maxnum`. For finite constants, DXC applies the same rule in `SetBias` with `kMinMipLodBias` and `kMaxMipLodBias` ([microsoft/DirectXShaderCompiler:include/dxc/DXIL/DxilConstants.h:L163-164]); DXC also clamps ±inf.
4. In `updateFunctionFlags` in [llvm/lib/Target/DirectX/DXILShaderFlags.cpp], add both intrinsics to the `SamplerFeedback` detection that llvm/llvm-project#238555 adds. That detection applies the validator 1.9 rule. The existing call-graph merge propagates the flag to callers and the module.

| Bias operand | Lowered bias |
| --- | --- |
| Constant above 15.99, such as `27.0` | `15.99f` (`0x402FFAE140000000`) |
| Constant below −16.0, such as `-27.0` | `-16.0` |
| Constant from −16.0 to 15.99 | Unchanged |
| Not a constant | Unchanged |
| An infinite or NaN constant | Unchanged |

Sources: [microsoft/DirectXShaderCompiler:tools/clang/test/HLSLFileCheck/hlsl/objects/FeedbackTexture/feedback.hlsl:L67-74], [microsoft/DirectXShaderCompiler:tools/clang/test/HLSLFileCheck/hlsl/objects/FeedbackTexture/feedback_array.hlsl:L60-67].

| Flag | Shader flag bit | SFI0 bit | Set when |
| --- | --- | --- | --- |
| `SamplerFeedback` | 26 | 21 | Any `WriteSamplerFeedbackBias` call, validator 1.9 or later |

Sources: [microsoft/DirectXShaderCompiler:docs/DXIL.rst:L550], [llvm/include/llvm/BinaryFormat/DXContainerConstants.def:L45], [microsoft/DirectXShaderCompiler:lib/DXIL/DxilShaderFlags.cpp:L831-835].

- [llvm/docs/DirectX/DXILResources.md] has no text about `WriteSamplerFeedbackBias`. llvm/llvm-project#238555 adds a sampler feedback subsection under "Resource Operations". Add to it the signatures and operands of `llvm.dx.resource.writesamplerfeedbackbias` and `llvm.dx.resource.writesamplerfeedbackbias.clamp`, their lowering to `dx.op.writeSamplerFeedbackBias`, the constant-bias clamping and the `undef` clamp of the non-clamp form.

#### Test Cases

##### `clang/test/SemaHLSL/Resources/FeedbackTextures-WriteSamplerFeedbackBias.hlsl`

- Both types and both overloads in a `ps_6_5` pixel shader, with `float`, `float4`, `int`, `uint4`, `half`, `float16_t`, `int16_t`, `uint64_t` and `double` paired textures.
- A `Texture2DArray` paired with `FeedbackTexture2D`, and a `Texture2D` paired with `FeedbackTexture2DArray`: rejected.
- A `Texture2DMS`, a `RWTexture2D` and a `SamplerComparisonState`: rejected.
- Wrong location size, wrong bias and clamp types, a missing bias and wrong argument counts. No offset and no status overload.
- Constant biases `27.0` and `-27.0`: accepted without a diagnostic.
- `ps_6_4`: rejected.
- `numthreads(1, 1, 1)` compute entry points at `cs_6_5` and `cs_6_6`: rejected, as unavailable.
- Vertex at SM6.5, and mesh and amplification at SM6.6: rejected.
- A library with a helper function that calls the method, reached from a pixel entry point (accepted) and from a compute entry point (rejected). [clang/test/SemaHLSL/Resources/Textures-derivative-availability.hlsl] is the model.

##### `clang/test/CodeGenHLSL/resources/FeedbackTextures-WriteSamplerFeedbackBias.hlsl`

- Both types in both modes.
- A call without a clamp: `llvm.dx.resource.writesamplerfeedbackbias`. A call with a clamp: `llvm.dx.resource.writesamplerfeedbackbias.clamp`.
- The intrinsic operands: feedback, texture and sampler handles, the location, the bias and the clamp.
- A constant bias `27.0`, unchanged in the intrinsic call.
- An optimized compilation in which both calls stay.

##### `llvm/test/CodeGen/DirectX/WriteSamplerFeedbackBias.ll`

- Both types and both intrinsics: three handles, the coordinates, the bias and the clamp.
- The array layer in `c2` for `FeedbackTexture2DArray`; `c3` is `undef`.
- An explicit clamp, and an `undef` clamp for `llvm.dx.resource.writesamplerfeedbackbias`.
- Each row of the bias table under DirectX: `27.0`, `-27.0`, `15.99`, `-16.0`, `0.5`, a function argument, an infinite constant and a NaN constant.
- A clamped bias next to an explicit clamp: the clamp operand is unchanged.
- An SM6.5 pixel module and a library module.

##### `llvm/test/CodeGen/DirectX/WriteSamplerFeedback-errors.ll`

- A `ps_6_4` module: "No valid stage for DXIL version 1.4".
- `cs_6_5`, `cs_6_6` and `vs_6_5` modules: "Invalid stage". [llvm/test/CodeGen/DirectX/ddx_coarse-errors.ll] is the model.

##### `llvm/test/CodeGen/DirectX/ShaderFlags/sampler-feedback.ll`

- A function that calls `llvm.dx.resource.writesamplerfeedbackbias` in the validator 1.9 section: `SamplerFeedback` in the analysis output and in S<truncated>Please see the issue for the entire body.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to