Issue 238526
Summary [HLSL][DirectX][SPIRV] Implement Load with a Status parameter on multisampled textures
Labels backend:DirectX, HLSL, backend:SPIR-V
Assignees
Reporter Icohedron
    Clang declares `Load` with a `Status` parameter on no multisampled texture. A call such as `Texture2DMS<float4>::Load(int2, int, int2, out uint)` fails with "no matching member function for call to 'Load'". Add the `Load` overloads with a `Status` parameter to `Texture2DMS`, `Texture2DMSArray`, `RWTexture2DMS` and `RWTexture2DMSArray`. Lower them to the DXIL `TextureLoad` and `CheckAccessFullyMapped` operations. Set the `TiledResources` shader flag for them. Lower them to SPIR-V `OpImageSparseFetch` and `OpImageSparseRead` with the `Sample` image operand. Store in `Status` the value that each target stores for `Load` with a `Status` parameter on `Texture` and `RWTexture` resources.

A Vulkan device runs these shaders only when it enables the `shaderResourceResidency` feature.

## Acceptance Criteria

### HLSL

- [ ] Add the `Load` overloads with a `Status` parameter to the multisampled `Texture` and `RWTexture` types in [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp].
- [ ] Add the `__builtin_hlsl_resource_load_ms_with_status` builtin to [clang/include/clang/Basic/Builtins.td] and check its arguments in [clang/lib/Sema/SemaHLSL.cpp].
- [ ] Generate `llvm.dx.resource.load.ms.status` and `llvm.spv.resource.load.ms.status` calls for `__builtin_hlsl_resource_load_ms_with_status` and store the status in `Status` in [clang/lib/CodeGen/CGHLSLBuiltins.cpp] and [clang/lib/CodeGen/CGHLSLRuntime.h].
- [ ] Add AST tests of the `Load` overloads with a `Status` parameter to [clang/test/AST/HLSL/MultiSampledTextures-AST.hlsl].
- [ ] Add Sema tests to [clang/test/SemaHLSL/Resources/MultiSampledTextures-Sema.hlsl].
- [ ] Add HLSL codegen tests to [clang/test/CodeGenHLSL/resources/MultiSampledTextures-Load.hlsl].
- [ ] Add HLSL codegen tests to `clang/test/CodeGenHLSL/resources/RWMultiSampledTextures.hlsl`.

### DirectX

- [ ] Add the `llvm.dx.resource.load.ms.status` intrinsic to [llvm/include/llvm/IR/IntrinsicsDirectX.td].
- [ ] Lower `llvm.dx.resource.load.ms.status` to the DXIL `TextureLoad` and `CheckAccessFullyMapped` operations in [llvm/lib/Target/DirectX/DXILOpLowering.cpp].
- [ ] Split `llvm.dx.resource.load.ms.status` calls with 64-bit elements into 32-bit loads in [llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp].
- [ ] Set the `TiledResources` shader flag when code uses the status result of `llvm.dx.resource.load.ms.status` in [llvm/lib/Target/DirectX/DXILShaderFlags.cpp].
- [ ] Set the `Typed UAV Load Additional Formats` shader flag for `llvm.dx.resource.load.ms.status` on `RWTexture2DMS` and `RWTexture2DMSArray` resources with multi-component elements in [llvm/lib/Target/DirectX/DXILShaderFlags.cpp].
- [ ] Add DXIL lowering tests to [llvm/test/CodeGen/DirectX/TextureLoad.ll].
- [ ] Add DXIL lowering tests of the 64-bit `llvm.dx.resource.load.ms.status` split to `llvm/test/CodeGen/DirectX/TextureLoad-64bit.ll`.
- [ ] Add shader flag tests to `llvm/test/CodeGen/DirectX/ShaderFlags/tiled-resources.ll` that set `TiledResources` for multisampled texture loads whose status result is used.
- [ ] Add shader flag tests of `llvm.dx.resource.load.ms.status` to [llvm/test/CodeGen/DirectX/ShaderFlags/typed-uav-load-additional-formats.ll].
- [ ] Document the `llvm.dx.resource.load.ms.status` intrinsic and its lowering in the "Loads, Samples, and Gathers" section of [llvm/docs/DirectX/DXILResources.md].

### SPIR-V

- [ ] Add the `llvm.spv.resource.load.ms.status` intrinsic to [llvm/include/llvm/IR/IntrinsicsSPIRV.td].
- [ ] Lower `llvm.spv.resource.load.ms.status` to `OpImageSparseFetch` on sampled images and to `OpImageSparseRead` on storage images, each with the `Sample` image operand, in [llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp].
- [ ] Add SPIR-V lowering tests to `llvm/test/CodeGen/SPIRV/hlsl-resources/LoadMSStatus.ll` (new).

## Notes

- llvm/llvm-project#238061 adds `clang/test/CodeGenHLSL/resources/RWMultiSampledTextures.hlsl`. llvm/llvm-project#238522 adds `llvm/test/CodeGen/DirectX/ShaderFlags/tiled-resources.ll`. llvm/llvm-project#235930 adds `llvm/test/CodeGen/DirectX/TextureLoad-64bit.ll`.
- `Texture2DMSArray`, `RWTexture2DMS` and `RWTexture2DMSArray` do not exist in Clang at the linked revision. Their `Load` overloads without a `Status` parameter come from llvm/llvm-project#231777 and llvm/llvm-project#238061.
- The SPIR-V `Status` value and `CheckAccessFullyMapped` come from llvm/llvm-project#238524, which follows llvm/wg-hlsl#411. This issue reuses that code.
- `SparseResidency` and the other capabilities of `OpImageSparseFetch` and `OpImageSparseRead` also come from that issue. The `ImageMSArray` rule of llvm/llvm-project#231777 depends on the image type, not on the instruction, so it already covers the sparse instructions.

### Suggested Implementation

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

#### HLSL Interface

| Receiver | New overload |
| --- | --- |
| `Texture2DMS` | `T Load(int2 Location, int SampleIndex, int2 Offset, out uint Status)` |
| `Texture2DMSArray` | `T Load(int3 Location, int SampleIndex, int2 Offset, out uint Status)` |
| `RWTexture2DMS` | `T Load(int2 Location, int SampleIndex, out uint Status)` |
| `RWTexture2DMSArray` | `T Load(int3 Location, int SampleIndex, out uint Status)` |

Sources: [Texture2DMS::Load(int,int,int,uint)], [Texture2DMSArray::Load(int,int,int,uint)], [microsoft/DirectXShaderCompiler:utils/hct/gen_intrin_main.txt:L616], [microsoft/DirectXShaderCompiler:utils/hct/gen_intrin_main.txt:L908].

- A read-only multisampled texture has no status overload without an offset. A writable one has no offset.
- `T` is the element type of the texture, including `half`, `int16_t` and `uint16_t` with 16-bit types, and `double` and `double2` on the DXIL target. `int64_t`, `uint64_t` and their two-component vectors are valid on both targets.

#### HLSL

1. In `addTextureLoadMSMethods` ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L1746-1779]), add the read-only overload after `Load(Location, SampleIndex, Offset)`, and the writable overload where the `RWTexture2DMS` change gives the writable types `Load(Location, SampleIndex)`. Add `Status` with `addParam("Status", AST.UnsignedIntTy, HLSLParamModifierAttr::Keyword_out)`, as the `Texture` status overloads do. Call `__builtin_hlsl_resource_load_ms_with_status` with the handle, the location, the sample index, the offset on read-only textures, and `Status`. Give the writable overload the Shader Model 6.7 availability attribute that the `RWTexture2DMS` change gives every other member of the writable types.
2. Declare `__builtin_hlsl_resource_load_ms_with_status` in [clang/include/clang/Basic/Builtins.td] beside `HLSLResourceLoadMS` ([clang/include/clang/Basic/Builtins.td:L5432-5436]): `NoThrow`, `CustomTypeChecking`, `void(...)`.
3. In [clang/lib/Sema/SemaHLSL.cpp], reuse `CheckLoadMSBuiltin` ([clang/lib/Sema/SemaHLSL.cpp:L4409-4447]) with a status flag. With the flag, require exactly five arguments on an SRV handle and four on a UAV handle. Check the last argument against `unsigned int` and with `CheckModifiableLValue`, as the `Texture` status builtin does.
4. In [clang/lib/CodeGen/CGHLSLRuntime.h], add `GENERATE_HLSL_INTRINSIC_FUNCTION(LoadMSStatus, resource_load_ms_status)` beside `LoadMS` ([clang/lib/CodeGen/CGHLSLRuntime.h:L224]).
5. In [clang/lib/CodeGen/CGHLSLBuiltins.cpp], share the operand code of `__builtin_hlsl_resource_load_ms` ([clang/lib/CodeGen/CGHLSLBuiltins.cpp:L954-969]). That code takes the offset from argument 3 whenever the call has more than three arguments. On a writable texture, argument 3 is `Status`. Take the offset from argument 3 only on read-only textures, and emit a zero offset on writable ones. `Status` is argument 4 on read-only textures and argument 3 on writable ones. Call `getLoadMSStatusIntrinsic()`. Extract element 0 as the result. Store element 1 in `Status` with the code of `__builtin_hlsl_resource_load_level_with_status`: a `zext` of the `i1` on DXIL, and the SPIR-V value that llvm/wg-hlsl#411 defines.

#### DirectX

- Declare `int_dx_resource_load_ms_status` in [llvm/include/llvm/IR/IntrinsicsDirectX.td] with the operands of `int_dx_resource_load_ms` ([llvm/include/llvm/IR/IntrinsicsDirectX.td:L193-196]) and the result `[llvm_any_ty, llvm_i1_ty]`.

| Operand | Value |
| --- | --- |
| 0 | Multisampled texture handle |
| 1 | Coordinates: `<2 x i32>`, or `<3 x i32>` with the layer last |
| 2 | Sample index, `i32` |
| 3 | Offsets, `<2 x i32>`: zero for `RWTexture2DMS` and `RWTexture2DMSArray` |
| Result | `{ T, i1 }`: the element and the check bit |

- In [llvm/lib/Target/DirectX/DXILOpLowering.cpp], dispatch `dx_resource_load_ms_status` to `lowerTextureLoad` ([llvm/lib/Target/DirectX/DXILOpLowering.cpp:L710-756]) with `HasCheckBit` true, beside `dx_resource_load_ms`. The sample index fills the mip-level slot of `TextureLoad`, as for `load.ms`.
- In [llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp], add the intrinsic to `isIntrinsicExpansion` and `expandIntrinsic` beside `dx_resource_load_ms` and `dx_resource_load_level_status`. Use the status split of `load.level.status`: one `<2 x i32>` or `<4 x i32>` call, whose check bit replaces the original status.
- In [llvm/lib/Target/DirectX/DXILShaderFlags.cpp], call `checkIfStatusIsExtracted` for `dx_resource_load_ms_status`. Add the intrinsic to the `TypedUAVLoadAdditionalFormats` case that holds `dx_resource_load_ms`. `WriteableMSAATextures` depends on the UAV handle, not on the intrinsic, so it needs no change.

| Flag | Shader flag bit | `SFI0` bit | Set when |
| --- | --- | --- | --- |
| `TiledResources` | 12 (`0x1000`) | 8 (`0x100`) | Code uses element 1 of the struct |
| `Typed UAV Load Additional Formats` | 13 (`0x2000`) | 11 (`0x800`) | A load from a writable multisampled texture whose element has more than one component |

Sources: [microsoft/DirectXShaderCompiler:docs/DXIL.rst#shader-flags], [llvm/include/llvm/BinaryFormat/DXContainerConstants.def:L32-35].

- In [llvm/docs/DirectX/DXILResources.md], add a `@llvm.dx.resource.load.ms.status` table beside the `load.ms` and `load.level.status` tables, with its lowering to `dx.op.textureLoad` and `dx.op.checkAccessFullyMapped`.

#### SPIR-V

- Declare `int_spv_resource_load_ms_status` in [llvm/include/llvm/IR/IntrinsicsSPIRV.td] with the operands of `int_spv_resource_load_ms` ([llvm/include/llvm/IR/IntrinsicsSPIRV.td:L609-612]) and the result `[llvm_any_ty, llvm_i32_ty]`. Element 1 is the Residency Code.
- In [llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp], dispatch `spv_resource_load_ms_status` to `selectLoadMSIntrinsic` with the sparse mode of `generateImageReadOrFetch`. The intrinsic has two definitions, so its operands start one index later than those of `load.ms`. Pass `selectLoadMSIntrinsic` the first operand index, or compute it with `I.getNumExplicitDefs()`, as the `Texture` status change does for `selectLoadLevelIntrinsic`. The sparse mode emits `OpImageSparseFetch` on a sampled image and `OpImageSparseRead` on a storage image with an `OpTypeStruct` result. The `Sample` operand follows any `ConstOffset` operand. On a signed image the mask is `SignExtend|Sample` or `ConstOffset|SignExtend|Sample`.

| Instruction | Image operands | Result |
| --- | --- | --- |
| `OpImageSparseFetch` | `Sample`, and `ConstOffset` for a nonzero constant offset | `{ i32 Residency Code, vec4 }` |
| `OpImageSparseRead` | `Sample` | `{ i32 Residency Code, vec4 }` |

Source: [OpImageSparseFetch], [OpImageSparseRead], [SPIR-V Image Operands].

#### Test Cases

##### `clang/test/AST/HLSL/MultiSampledTextures-AST.hlsl`

- The read-only overload on the `Texture2DMS` and `Texture2DMSArray` RUN lines: `Location`, `SampleIndex`, `Offset`, `Status` as `unsigned int &__restrict`, and the `__builtin_hlsl_resource_load_ms_with_status` call.
- The writable overload on the `RWTexture2DMS` and `RWTexture2DMSArray` RUN lines with three parameters.

##### `clang/test/SemaHLSL/Resources/MultiSampledTextures-Sema.hlsl`

- A status argument that is a constant, an `int` _expression_ or a `float` variable: rejected.
- `Load(Location, SampleIndex, Status)` on `Texture2DMS` with a `uint` `Status`: the call binds `Status` as the offset of `Load(Location, SampleIndex, Offset)`. With `-Wconversion`, check the warning "implicit conversion changes signedness: 'uint' to 'vector<int, 2>'".
- `Load(Location, SampleIndex, Offset, Status)` on `RWTexture2DMS`: no matching overload.
- `RWTexture2DMS` `Load(Location, SampleIndex, Status)` at `cs_6_6` on the DXIL target: the shader model error. At `cs_6_7` and on the SPIR-V target: no error.
- The existing `Load` arity cases, such as `bad_load`, with the new candidate note of the status overload.

##### `clang/test/CodeGenHLSL/resources/MultiSampledTextures-Load.hlsl`

- `Texture2DMS` and `Texture2DMSArray` on DXIL: the `{ <4 x float>, i1 }` `llvm.dx.resource.load.ms.status` call, `extractvalue` 0 and 1, the `zext` to `i32` and the store to `Status`.
- The same on SPIR-V with `llvm.spv.resource.load.ms.status` and a `{ <4 x float>, i32 }` result.
- A zero and a nonzero constant offset, and a sample index in a variable.
- `half4`, `int16_t` and `uint64_t2` elements on the DXIL target.

##### `clang/test/CodeGenHLSL/resources/RWMultiSampledTextures.hlsl`

- `RWTexture2DMS` and `RWTexture2DMSArray` on DXIL and SPIR-V: the status call with a zero offset and the store to `Status`.

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

- `load.ms.status` on `Texture2DMS` and `Texture2DMSArray` SRV handles and on a `Texture2DMS` UAV handle: `dx.op.textureLoad` with the operands of `load.ms`, element 4 of the `ResRet` and `dx.op.checkAccessFullyMapped.i32`.
- A call whose status is not used: no `CheckAccessFullyMapped` operation.
- `<4 x half>` and `<4 x i16>` results.

##### `llvm/test/CodeGen/DirectX/TextureLoad-64bit.ll`

- `load.ms.status` with `i64`, `<2 x i64>`, `double` and `<2 x double>` elements: one `<2 x i32>` or `<4 x i32>` call with the coordinates, sample and offset forwarded, and the recombined value.
- The status of the 32-bit call replaces every use of the original status.

##### `llvm/test/CodeGen/DirectX/ShaderFlags/tiled-resources.ll`

- `load.ms.status` on a `Texture2DMS` SRV handle and on a `Texture2DMSArray` UAV handle with a used status: shader flag bit 12 and `TiledResources: true` in `SFI0`.

##### `llvm/test/CodeGen/DirectX/ShaderFlags/typed-uav-load-additional-formats.ll`

- `load.ms.status` of `<4 x float>` on a `Texture2DMS` UAV handle.

##### `llvm/test/CodeGen/SPIRV/hlsl-resources/LoadMSStatus.ll`

- `llc -O0 -mtriple=spirv1.6-vulkan1.3-compute` and `spirv-val --target-env vulkan1.3`, with an entry point, as in [llvm/test/CodeGen/SPIRV/hlsl-resources/LoadLevel.ll].
- `OpImageSparseFetch` on a 2D and an arrayed 2D multisampled sampled image: `Sample` and `ConstOffset|Sample`; `SignExtend|Sample` on a signed integer image.
- `OpImageSparseRead` with `Sample` on a 2D and an arrayed 2D multisampled storage image, with `ImageMSArray` for the arrayed one.
- A constant sample i<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