| Issue |
238597
|
| Summary |
[HLSL][DirectX][SPIRV] Test Interlocked operations on RasterizerOrderedTexture elements
|
| Labels |
backend:DirectX,
HLSL,
backend:SPIR-V
|
| Assignees |
|
| Reporter |
Icohedron
|
The `Interlocked*` functions already lower to DXIL atomic operations on rasterizer-ordered view (ROV) handles, but no test covers them on the `RasterizerOrderedTexture` types. After llvm/llvm-project#238587, Clang codegen brackets each member call on a `RasterizerOrderedTexture` other than `GetDimensions`, `operator[]` included, with fragment shader interlock markers when the target is SPIR-V. So the atomic operations of the `Interlocked*` functions will sit inside the interlock region, but no test will check this. Test the DXIL and SPIR-V output of the `Interlocked*` functions on `RasterizerOrderedTexture` elements with 32-bit and 64-bit integer elements. Test that the SPIR-V target rejects the `FloatBitwise` functions on `float` `RasterizerOrderedTexture` elements.
## Acceptance Criteria
- [ ] Add HLSL codegen tests to `clang/test/CodeGenHLSL/builtins/RasterizerOrderedTexture-Interlocked.hlsl` (new).
- [ ] Add Sema tests to `clang/test/SemaHLSL/BuiltIns/InterlockedFloatBitwise-spirv.hlsl` that reject `InterlockedCompareExchangeFloatBitwise` and `InterlockedCompareStoreFloatBitwise` on `float` `RasterizerOrderedTexture` elements when the target is SPIR-V.
- [ ] Add DXIL lowering tests to `llvm/test/CodeGen/DirectX/ResourceAtomic-texture-rov.ll` (new) that check the rasterizer-ordered bit in the handle properties of `AtomicBinOp` and `AtomicCompareExchange` operations on `RasterizerOrderedTexture` handles.
- [ ] Add SPIR-V lowering tests to `llvm/test/CodeGen/SPIRV/extensions/SPV_EXT_fragment_shader_interlock/interlock-image-atomics.ll` (new) that check the `OpImageTexelPointer` atomic operations stay between `OpBeginInvocationInterlockEXT` and `OpEndInvocationInterlockEXT`.
## Notes
- llvm/llvm-project#238587 adds the fragment shader interlock markers around member calls on `RasterizerOrderedTexture` types other than `GetDimensions`.
- DXC emits no fragment shader interlock for atomic operations on ROV textures. Clang treats them as ROV accesses, because overlapping pixel-shader invocations must apply `InterlockedExchange` and the compare operations in primitive order.
- llvm/llvm-project#238585 rejects rasterizer-ordered resources outside pixel shaders. Use a pixel or library triple in every test file this issue edits, not the compute triple of [clang/test/CodeGenHLSL/builtins/RWTexture-Interlocked.hlsl].
- The SPIR-V `cmpxchg` case reaches instruction selection only after [llvm/llvm-project#221370] is fixed.
- llvm/llvm-project#235837 creates `clang/test/SemaHLSL/BuiltIns/InterlockedFloatBitwise-spirv.hlsl`.
- llvm/llvm-project#238587 creates the `llvm/test/CodeGen/SPIRV/extensions/SPV_EXT_fragment_shader_interlock/` folder through its blocker llvm/llvm-project#238586.
### Suggested Implementation
<details>
<summary>Show details</summary>
Use the marker and mode-attribute spellings adopted by llvm/wg-hlsl#518 through the blocking interlock issues; the names below are illustrative only. Apply the adopted non-entry-function policy to helper-marker expectations without adding resource-helper legalization work. Generic full-_expression_ markers, `GLSL450` `Coherent` and this issue's tests-only scope stay required.
#### HLSL Interface
- The `Interlocked*` free functions take a `RasterizerOrderedTexture` element through `operator[]` as their destination, as for the `RWTexture` types: `InterlockedAdd`, `InterlockedAnd`, `InterlockedOr`, `InterlockedXor`, `InterlockedMax`, `InterlockedMin`, `InterlockedExchange`, `InterlockedCompareExchange`, `InterlockedCompareStore`, `InterlockedCompareExchangeFloatBitwise` and `InterlockedCompareStoreFloatBitwise`.
- Element types: 32-bit and 64-bit integers; `float` for `InterlockedExchange` and the two `FloatBitwise` functions.
#### Clang
- No code change. After llvm/llvm-project#238587, Clang emits the begin marker before each member call on a `RasterizerOrderedTexture` other than `GetDimensions`, `operator[]` included, and the end marker at the end of the full-_expression_ of the caller. So `InterlockedAdd(T[p], 1)` will be bracketed. The markers make the entry function a ROV-dependent entry point, also when the atomic operation is its only ROV access.
- `handleInterlockedOp` ([clang/lib/CodeGen/CGHLSLBuiltins.cpp:L382-411]) emits the `atomicrmw` and `handleInterlockedCompareOp` (L415-453) emits the `cmpxchg` inside that full-_expression_.
#### DirectX
- No code change. The atomic switches of the DXIL resource access pass test only the resource kind ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L516-586]), so ROV texture handles lower like `RWTexture` handles. The pass bitcasts a `float` exchange value to `i32` itself.
- Probe results with `opt -dxil-resource-access -dxil-op-lower` on a pixel module. The first properties word has bit 13 (`IsROV`) set:
| Handle | Operation | `annotateHandle` properties |
| --- | --- | --- |
| `RasterizerOrderedTexture1D<float>` | `atomicBinOp.i32` (exchange) | `{ i32 12289, i32 265 }` |
| `RasterizerOrderedTexture2D<int>` | `atomicBinOp.i32` (add) | `{ i32 12290, i32 260 }` |
| `RasterizerOrderedTexture3D<uint>` | `atomicCompareExchange.i32` | `{ i32 12292, i32 261 }` |
| `RasterizerOrderedTexture1DArray<uint64_t>` | `atomicBinOp.i64` (or) | `{ i32 12294, i32 263 }` |
| `RasterizerOrderedTexture2DArray<int64_t>` | `atomicCompareExchange.i64` | `{ i32 12295, i32 262 }` |
Source: [llvm/lib/Analysis/DXILResource.cpp:L729-743].
#### SPIR-V
- No backend change. The backend treats every instruction between the markers as a ROV access, places one Begin/End pair per entry point and decorates the accessed image variable `Coherent`.
- llvm/llvm-project#225543 selects `OpImageTexelPointer` for `atomicrmw`. llvm/llvm-project#235929 selects it for `cmpxchg`.
- 64-bit image atomics need `SPV_EXT_shader_image_int64` (`Int64ImageEXT`) and `Int64Atomics`.
#### Test Cases
##### `clang/test/CodeGenHLSL/builtins/RasterizerOrderedTexture-Interlocked.hlsl`
- Follow [clang/test/CodeGenHLSL/builtins/RWTexture-Interlocked.hlsl] with `dxil-pc-shadermodel6.6-pixel` and `spirv-pc-vulkan1.3-pixel` RUN lines and a `[shader("pixel")]` entry function.
- The calls of that file on `RasterizerOrderedTexture2D<int>`, `RasterizerOrderedTexture2DArray<uint>` and `RasterizerOrderedTexture2D<float>`.
- `InterlockedAdd` on `RasterizerOrderedTexture1DArray<uint>`.
- 64-bit `InterlockedOr`, `InterlockedMax` and `InterlockedCompareExchange` on `RasterizerOrderedTexture1D<uint64_t>` and `RasterizerOrderedTexture3D<int64_t>`.
- DXIL: `llvm.dx.resource.getpointer` on `target("dx.Texture", T, 1, 1, IsSigned, Kind)` and the atomic operation.
- SPIR-V: for each call, the begin marker, `llvm.spv.resource.getpointer`, the atomic operation and the end marker, in this order.
- The two `FloatBitwise` calls on DXIL only.
- A `RWTexture2D<uint>` atomic operation in the same entry function: no markers.
- A `[[clang::noinline]]` helper with an atomic operation on a ROV element: check Clang's generic full-_expression_ markers in the helper at the IR level. Backend helper realization follows the adopted non-entry-function policy; no general resource-helper legalization is assigned here.
##### `clang/test/SemaHLSL/BuiltIns/InterlockedFloatBitwise-spirv.hlsl`
- `InterlockedCompareExchangeFloatBitwise` and `InterlockedCompareStoreFloatBitwise` on a `RasterizerOrderedTexture2D<float>` element: the error of the file's other `float` destinations.
##### `llvm/test/CodeGen/DirectX/ResourceAtomic-texture-rov.ll`
- Follow [llvm/test/CodeGen/DirectX/ResourceAtomicBinOp-texture.ll] with a `dxil-pc-shadermodel6.6-pixel` triple.
- The five handles of the table under DirectX, with their properties and operations.
- The `bitcast` of the `float` exchange value to `i32`.
##### `llvm/test/CodeGen/SPIRV/extensions/SPV_EXT_fragment_shader_interlock/interlock-image-atomics.ll`
- `llc -O0` with a `spirv-unknown-vulkan1.3-pixel` triple and `--spirv-ext=+SPV_EXT_fragment_shader_interlock,+SPV_EXT_shader_image_int64`, and a `spirv-val` RUN line.
- 32-bit `atomicrmw add`, 64-bit `atomicrmw umax` and 32-bit `cmpxchg` between the markers: `OpImageTexelPointer` and `OpAtomicIAdd`, `OpAtomicUMax` and `OpAtomicCompareExchange` between `OpBeginInvocationInterlockEXT` and `OpEndInvocationInterlockEXT`.
- `PixelInterlockOrderedEXT` on an entry point whose only ROV access is an atomic operation.
- `Coherent` on the image variable.
</details>
## References
<details>
<summary>HLSL</summary>
- [Rasterizer order views] — Direct3D 12 rasterizer-ordered views and their ordering guarantee.
- [InterlockedCompareExchange] — HLSL `Interlocked*` function reference.
</details>
<details>
<summary>DirectX</summary>
- [microsoft/DirectXShaderCompiler:docs/DXIL.rst#atomicbinop] — DXIL `AtomicBinOp` operation, opcode 78.
- [llvm/wg-hlsl:proposals/0015-resource-attributes-in-dxil-and-spirv.md#dxil-binding-annotation] — ROV bit of the resource properties.
</details>
<details>
<summary>SPIR-V</summary>
- [SPV_EXT_fragment_shader_interlock] — `OpBeginInvocationInterlockEXT`, `OpEndInvocationInterlockEXT` and the interlock execution modes.
- [OpImageTexelPointer] — Pointer to a texel for atomic operations.
- [SPV_EXT_shader_image_int64] — 64-bit integer image atomics.
</details>
_Disclaimer: This issue was written with the help of AI and may contain some inaccuracies._
[clang/lib/CodeGen/CGHLSLBuiltins.cpp:L382-411]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/CodeGen/CGHLSLBuiltins.cpp#L382-L411
[clang/test/CodeGenHLSL/builtins/RWTexture-Interlocked.hlsl]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/test/CodeGenHLSL/builtins/RWTexture-Interlocked.hlsl
[llvm/test/CodeGen/DirectX/ResourceAtomicBinOp-texture.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/DirectX/ResourceAtomicBinOp-texture.ll
[llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L516-586]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILResourceAccess.cpp#L516-L586
[llvm/lib/Analysis/DXILResource.cpp:L729-743]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Analysis/DXILResource.cpp#L729-L743
[llvm/llvm-project#221370]: https://github.com/llvm/llvm-project/issues/221370
[llvm/wg-hlsl:proposals/0015-resource-attributes-in-dxil-and-spirv.md#dxil-binding-annotation]: https://github.com/llvm/wg-hlsl/blob/4af694e57fa735eb3987adef76765105b6338f6a/proposals/0015-resource-attributes-in-dxil-and-spirv.md#dxil-binding-annotation
[microsoft/DirectXShaderCompiler:docs/DXIL.rst#atomicbinop]: https://github.com/microsoft/DirectXShaderCompiler/blob/c3a85328eb12dffdba41026d6ec940c22c5308e0/docs/DXIL.rst#atomicbinop
[Rasterizer order views]: https://learn.microsoft.com/en-us/windows/win32/direct3d12/rasterizer-order-views
[InterlockedCompareExchange]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/interlockedcompareexchange
[SPV_EXT_fragment_shader_interlock]: https://github.khronos.org/SPIRV-Registry/extensions/EXT/SPV_EXT_fragment_shader_interlock.html
[OpImageTexelPointer]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageTexelPointer
[SPV_EXT_shader_image_int64]: https://github.khronos.org/SPIRV-Registry/extensions/EXT/SPV_EXT_shader_image_int64.html
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs