| Issue |
238060
|
| Summary |
[DirectX] Add `llvm.dx.resource.store.ms` and lower it to `TextureStoreSample`
|
| Labels |
backend:DirectX,
HLSL
|
| Assignees |
|
| Reporter |
Icohedron
|
The DirectX backend has no intrinsic that stores to one sample of a writable multisampled texture, and no record for the DXIL `TextureStoreSample` operation (opcode 225). Add the `llvm.dx.resource.store.ms` intrinsic. Lower it to `TextureStoreSample`. Split elements with 64-bit components into pairs of 32-bit components before the `TextureStoreSample` operation. Set the shader flags that `TextureStoreSample` requires.
On the DirectX target, `TextureStoreSample` requires Shader Model 6.7.
## Acceptance Criteria
- [ ] Add the `llvm.dx.resource.store.ms` intrinsic and lower it to the DXIL `TextureStoreSample` operation, with elements that have 64-bit components split into pairs of 32-bit components, in [llvm/include/llvm/IR/IntrinsicsDirectX.td], [llvm/lib/Target/DirectX/DXIL.td], [llvm/lib/Target/DirectX/DXILOpLowering.cpp] and [llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp].
- [ ] Set the `AdvancedTextureOps` and `WriteableMSAATextures` shader flags for `llvm.dx.resource.store.ms` in [llvm/lib/Target/DirectX/DXILShaderFlags.cpp].
- [ ] Add DXIL lowering tests to `llvm/test/CodeGen/DirectX/TextureStoreSample.ll` (new).
- [ ] Add DXIL lowering tests that check the split of 64-bit components into 32-bit pairs for `llvm.dx.resource.store.ms` to `llvm/test/CodeGen/DirectX/TextureStore-64bit.ll`.
- [ ] Add shader flag tests of `llvm.dx.resource.store.ms` to [llvm/test/CodeGen/DirectX/ShaderFlags/advanced-texture-ops.ll].
- [ ] Document the `llvm.dx.resource.store.ms` intrinsic and its lowering to the DXIL `TextureStoreSample` operation in the "Stores" section of [llvm/docs/DirectX/DXILResources.md].
## Notes
- DXC sets `AdvancedTextureOps` for every `TextureStoreSample` ([microsoft/DirectXShaderCompiler:lib/DXIL/DxilShaderFlags.cpp:L720-727]), although [microsoft/DirectXShaderCompiler:docs/DXIL.rst:L558-559] lists only `WriteableMSAATextures` for it. The DXIL validator rejects a module whose flags differ from the flags that it computes.
- Clang does not declare `RWTexture2DMS` or `RWTexture2DMSArray` yet. Write the tests in LLVM IR with `target("dx.MSTexture", T, 1, N, IsSigned, Kind)` handles. Kind 3 is `Texture2DMS` and kind 8 is `Texture2DMSArray`.
- llvm/llvm-project#235930 creates `llvm/test/CodeGen/DirectX/TextureStore-64bit.ll`. The file does not exist at the linked revision.
### Suggested Implementation
<details>
<summary>Show details</summary>
#### DirectX
- In [llvm/include/llvm/IR/IntrinsicsDirectX.td], define `int_dx_resource_store_ms : DefaultAttrsIntrinsic<[], [llvm_any_ty, llvm_any_ty, llvm_i32_ty, llvm_any_ty], [IntrWriteMem]>` beside `int_dx_resource_store_texture` ([llvm/include/llvm/IR/IntrinsicsDirectX.td:L193-200]). The operands are the handle, the coordinates, the sample index and the element value: `llvm.dx.resource.store.ms(handle, coords, i32 sample, value)`.
| `TextureStoreSample` operand | Value |
| --- | --- |
| 0 | `i32 225` |
| 1 | Texture handle |
| 2–4 | Coordinates c0–c2. c2 is the array layer for kind 8. Set each unused coordinate to `undef`. |
| 5–8 | Values v0–v3 |
| 9 | Write mask, `i8` constant 15 |
| 10 | Sample index |
Sources: [microsoft/DirectXShaderCompiler:utils/hct/hctdb.py:L5101-5121], [microsoft/DirectXShaderCompiler:docs/DXIL.rst:L2364]. Overloads: `half`, `float`, `i16`, `i32`. Shader model 6.7 ([microsoft/DirectXShaderCompiler:utils/hct/hctdb.py:L994-995]).
- In [llvm/lib/Target/DirectX/DXIL.td], add `TextureStoreSample : DXILOp<225, textureStoreSample>` with `Overloads<DXIL1_7, [HalfTy, FloatTy, Int16Ty, Int32Ty]>` and `Stages<DXIL1_7, [all_stages]>`. The class exists ([llvm/lib/Target/DirectX/DXIL.td:L208]); the record does not. `TextureStore` is the model ([llvm/lib/Target/DirectX/DXIL.td:L1007-1017]).
- In [llvm/lib/Target/DirectX/DXILOpLowering.cpp], dispatch `dx_resource_store_ms` beside `dx_resource_store_texture` ([llvm/lib/Target/DirectX/DXILOpLowering.cpp:L1462-1464]). Model the lowering on `lowerTextureStore` ([llvm/lib/Target/DirectX/DXILOpLowering.cpp:L1116-1163]), and append the sample index after the mask.
- In [llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp], add `dx_resource_store_ms` (data operand 3) to `isIntrinsicExpansion` ([llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp:L246-259]) and `expandIntrinsic` ([llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp:L1361-1376]), beside the `store.texture` case of llvm/llvm-project#235930. Use the same split: `i64` and `double` become `<2 x i32>`, two-component elements become `<4 x i32>`. Forward the coordinates and the sample index unchanged.
| Flag | Shader flag bit | `SFI0` bit (mask) | Set when |
| --- | --- | --- | --- |
| `AdvancedTextureOps` | 34 | 29 (`0x20000000`) | Any `llvm.dx.resource.store.ms` |
| `WriteableMSAATextures` | 35 | 30 (`0x40000000`) | Any `llvm.dx.resource.store.ms` |
Sources: [microsoft/DirectXShaderCompiler:lib/DXIL/DxilShaderFlags.cpp:L720-727], [llvm/include/llvm/BinaryFormat/DXContainerConstants.def:L53-54].
- In [llvm/lib/Target/DirectX/DXILShaderFlags.cpp], `checkAdvancedTextureOps` names `TextureStoreSample` as a future unconditional case ([llvm/lib/Target/DirectX/DXILShaderFlags.cpp:L131-164]). Set both flags in the intrinsic switch of `updateFunctionFlags`. Keep `WriteableMSAATextures` on the store: DXC sets it for every `TextureStoreSample` at every validator version ([microsoft/DirectXShaderCompiler:lib/DXIL/DxilShaderFlags.cpp:L720-722]). From validator 1.8, the handle rule of llvm/llvm-project#116203 also sets it, so the store rule is redundant there. It is still required for flag parity with the validator.
- [llvm/docs/DirectX/DXILResources.md] has a "Stores" section that documents `llvm.dx.resource.store.typedbuffer` and `llvm.dx.resource.store.texture` ([llvm/docs/DirectX/DXILResources.md:L497-600]). Add an `@llvm.dx.resource.store.ms` table with its operands and its lowering to `dx.op.textureStoreSample`.
#### Test Cases
##### `llvm/test/CodeGen/DirectX/TextureStoreSample.ll`
- `store.ms` on kind 3 and kind 8 UAV handles: two or three coordinates, `undef` past them, the sample in operand 10, mask 15.
- A constant and a run-time sample index.
- `<4 x float>`, `float`, `<4 x i32>`, `<4 x half>` and `<4 x i16>` data (`f32`, `i32`, `f16` and `i16` overloads).
- An SM6.6 module: the operation is rejected.
- Follow [llvm/test/CodeGen/DirectX/TextureStore.ll].
##### `llvm/test/CodeGen/DirectX/TextureStore-64bit.ll`
- `store.ms` with `i64`, `<2 x i64>`, `double` and `<2 x double>` data, split into `<2 x i32>` or `<4 x i32>` `store.ms` calls with the coordinates and the sample forwarded.
- A `Texture2DMSArray` handle (kind 8) with three coordinates.
##### `llvm/test/CodeGen/DirectX/ShaderFlags/advanced-texture-ops.ll`
- A function with `store.ms`: `AdvancedTextureOps` and `WriteableMSAATextures` in the function mask (`0xC00000000`), the combined mask and `SFI0`.
</details>
## References
<details>
<summary>DirectX</summary>
- [microsoft/DirectXShaderCompiler:docs/DXIL.rst:L2364] — DXIL `TextureStoreSample` operation, opcode 225.
- [microsoft/DirectXShaderCompiler:docs/DXIL.rst#shader-flags] — DXIL shader flags.
- [SM6.7 Writable MSAA Textures] — Writable multisampled textures and `TextureStoreSample`.
</details>
_Disclaimer: This issue was written with the help of AI and may contain some inaccuracies._
[llvm/include/llvm/IR/IntrinsicsDirectX.td]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/include/llvm/IR/IntrinsicsDirectX.td
[llvm/include/llvm/IR/IntrinsicsDirectX.td:L193-200]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/include/llvm/IR/IntrinsicsDirectX.td#L193-L200
[llvm/include/llvm/BinaryFormat/DXContainerConstants.def:L53-54]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/include/llvm/BinaryFormat/DXContainerConstants.def#L53-L54
[llvm/lib/Target/DirectX/DXIL.td]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXIL.td
[llvm/lib/Target/DirectX/DXIL.td:L208]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXIL.td#L208
[llvm/lib/Target/DirectX/DXIL.td:L1007-1017]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXIL.td#L1007-L1017
[llvm/lib/Target/DirectX/DXILOpLowering.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILOpLowering.cpp
[llvm/lib/Target/DirectX/DXILOpLowering.cpp:L1116-1163]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILOpLowering.cpp#L1116-L1163
[llvm/lib/Target/DirectX/DXILOpLowering.cpp:L1462-1464]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILOpLowering.cpp#L1462-L1464
[llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp
[llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp:L246-259]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp#L246-L259
[llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp:L1361-1376]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILIntrinsicExpansion.cpp#L1361-L1376
[llvm/lib/Target/DirectX/DXILShaderFlags.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILShaderFlags.cpp
[llvm/lib/Target/DirectX/DXILShaderFlags.cpp:L131-164]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILShaderFlags.cpp#L131-L164
[llvm/docs/DirectX/DXILResources.md]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/docs/DirectX/DXILResources.md
[llvm/docs/DirectX/DXILResources.md:L497-600]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/docs/DirectX/DXILResources.md#L497-L600
[llvm/test/CodeGen/DirectX/TextureStore.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/DirectX/TextureStore.ll
[llvm/test/CodeGen/DirectX/ShaderFlags/advanced-texture-ops.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/DirectX/ShaderFlags/advanced-texture-ops.ll
[microsoft/DirectXShaderCompiler:docs/DXIL.rst:L2364]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/docs/DXIL.rst#L2364
[microsoft/DirectXShaderCompiler:docs/DXIL.rst:L558-559]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/docs/DXIL.rst#L558-L559
[microsoft/DirectXShaderCompiler:docs/DXIL.rst#shader-flags]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/docs/DXIL.rst#shader-flags
[microsoft/DirectXShaderCompiler:utils/hct/hctdb.py:L994-995]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/utils/hct/hctdb.py#L994-L995
[microsoft/DirectXShaderCompiler:utils/hct/hctdb.py:L5101-5121]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/utils/hct/hctdb.py#L5101-L5121
[microsoft/DirectXShaderCompiler:lib/DXIL/DxilShaderFlags.cpp:L720-727]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/lib/DXIL/DxilShaderFlags.cpp#L720-L727
[SM6.7 Writable MSAA Textures]: https://microsoft.github.io/DirectX-Specs/d3d/HLSL_SM_6_7_Advanced_Texture_Ops.html#writable-msaa-textures
[microsoft/DirectXShaderCompiler:lib/DXIL/DxilShaderFlags.cpp:L720-722]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/lib/DXIL/DxilShaderFlags.cpp#L720-L722
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs