Issue 238131
Summary [HLSL][DirectX][SPIRV] Implement `Interlocked` functions on `RWTexture2DMS` and `RWTexture2DMSArray` samples
Labels backend:DirectX, HLSL, backend:SPIR-V
Assignees
Reporter Icohedron
    DXIL lowering rejects atomic operations on multisampled texture handles. Support `Interlocked` functions on `RWTexture2DMS` and `RWTexture2DMSArray` through the writable-sample LLVM interface adopted by llvm/wg-hlsl#512. Lower constant sample 0 to DXIL `AtomicBinOp` and `AtomicCompareExchange`. Apply the amendment's approved handling of other or nonconstant sample indices. On SPIR-V, lower the operations through `OpImageTexelPointer` with the sample index in its `Sample` operand.

## Acceptance Criteria

### HLSL

- [ ] Add HLSL codegen tests of `RWTexture2DMS` and `RWTexture2DMSArray` to [clang/test/CodeGenHLSL/builtins/RWTexture-Interlocked.hlsl].
- [ ] Add Sema tests to `clang/test/SemaHLSL/BuiltIns/InterlockedFloatBitwise-spirv.hlsl` that reject `InterlockedCompareExchangeFloatBitwise` and `InterlockedCompareStoreFloatBitwise` on `float` elements of `RWTexture2DMS` and `RWTexture2DMSArray` on the Vulkan target.

### DirectX

- [ ] Lower `atomicrmw` and `cmpxchg` through the approved writable multisampled sample-reference interface with constant sample index 0 to DXIL `AtomicBinOp` and `AtomicCompareExchange` in [llvm/lib/Target/DirectX/DXILResourceAccess.cpp].
- [ ] Apply the sample-index handling adopted by llvm/wg-hlsl#512 to writable multisampled `atomicrmw` and `cmpxchg` whose sample index is not the constant 0, in [llvm/lib/Target/DirectX/DXILResourceAccess.cpp].
- [ ] Add DXIL lowering tests to [llvm/test/CodeGen/DirectX/ResourceAtomicBinOp-texture.ll].
- [ ] Add DXIL lowering tests to [llvm/test/CodeGen/DirectX/ResourceAtomicCompareExchange-texture.ll].
- [ ] Add DXIL lowering tests of the approved handling of unsupported multisampled atomic sample indices to [llvm/test/CodeGen/DirectX/ResourceAtomicBinOp-texture-unsupported-kind.ll].
- [ ] Add DXIL lowering tests of the approved handling of unsupported multisampled atomic sample indices to [llvm/test/CodeGen/DirectX/ResourceAtomicCompareExchange-texture-unsupported-kind.ll].
- [ ] Add shader flag tests to `llvm/test/CodeGen/DirectX/ShaderFlags/atomic64-typed-resource-ms.ll` (new) that check `AtomicInt64OnTypedResource` for 64-bit atomic operations on `RWTexture2DMS` and `RWTexture2DMSArray`.

### SPIR-V

- [ ] Make writable multisampled `atomicrmw` and `cmpxchg` lower through the approved sample-reference interface to `OpImageTexelPointer` with the sample index in its `Sample` operand in [llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp].
- [ ] Add SPIR-V lowering tests of multisampled images to `llvm/test/CodeGen/SPIRV/hlsl-resources/ImageAtomics.ll`.
- [ ] Add SPIR-V lowering tests of multisampled images to `llvm/test/CodeGen/SPIRV/hlsl-resources/ImageAtomicCompareExchange.ll`.

## Notes

- The DXIL atomic operations have no sample operand. Never drop a sample index or move it into the layer coordinate.
- The proposal decides how Clang handles a sample index not proven to be 0; a lowering-time diagnostic is the recommendation, not an adopted policy.
- DXC accepts `Interlocked` functions on `operator[]` of the writable multisampled types. DXC crashes on a `sample[][]` destination, so it is no reference for that form.
- llvm/llvm-project#238061 supplies the adopted writable-sample LLVM interface. llvm/llvm-project#225543 adds `ImageAtomics.ll`. llvm/llvm-project#235929 adds `ImageAtomicCompareExchange.ll`.
- llvm/llvm-project#235837 adds `InterlockedFloatBitwise-spirv.hlsl`. It also wraps the existing float-bitwise calls in `RWTexture-Interlocked.hlsl` in `#ifndef __SPIRV__`.
- llvm/llvm-project#116203 sets `WriteableMSAATextures` for used multisampled UAV handles.

### Suggested Implementation

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

The `llvm.dx.resource.getpointer.ms` and `llvm.spv.resource.getpointer.ms` spellings and DirectX compile-error recipe below illustrate recommendations. Follow the sample-reference interface and unsupported-sample policy adopted by the 0037 amendment. Native DXIL operations still address only sample 0; never discard an index or move it into the layer coordinate. Keep the listed regression files and cases.

#### HLSL Interface

| Function | Element types |
| --- | --- |
| `InterlockedAdd`, `InterlockedAnd`, `InterlockedOr`, `InterlockedXor`, `InterlockedMax`, `InterlockedMin` | `int`, `uint`, `int64_t`, `uint64_t` |
| `InterlockedExchange` | `int`, `uint`, `int64_t`, `uint64_t`, `float` |
| `InterlockedCompareExchange`, `InterlockedCompareStore` | `int`, `uint`, `int64_t`, `uint64_t` |
| `InterlockedCompareExchangeFloatBitwise`, `InterlockedCompareStoreFloatBitwise` | `float`, DirectX only |

Sources: [InterlockedAdd], [InterlockedCompareExchange], [InterlockedCompareStore], [SM6.6 64-bit and float atomics].

- The destination is `T[pos]` (sample 0) or `T.sample[s][pos]` of an `RWTexture2DMS` or `RWTexture2DMSArray` with a scalar element ([SM6.7 Writable MSAA Textures]). `pos` is `uint2`, or `uint3` with the layer in `z`.
- No Clang source change is expected. `CheckInterlockedBuiltin` accepts any modifiable scalar lvalue in the `hlsl_device` address space ([clang/lib/Sema/SemaHLSL.cpp:L4548-4618]). Clang emits `atomicrmw` or `cmpxchg` on the pointer of the destination reference, which comes from `llvm.dx.resource.getpointer.ms` or `llvm.spv.resource.getpointer.ms` with the sample index as the third operand.

#### DirectX

- `createAtomicBinOpIntrinsic` and `createAtomicCompareExchangeIntrinsic` report "DXIL atomicrmw not implemented for this texture resource kind" and "DXIL cmpxchg not implemented for this texture resource kind" for `Texture2DMS` and `Texture2DMSArray` ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L529-537], [llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L566-574]).
- In both cases, when the pointer comes from `llvm.dx.resource.getpointer.ms` and its sample operand is the constant 0, call `createTextureAtomicBinOp` or `createTextureAtomicCompareExchange`. `getTextureAtomicCoords` already reads the coordinates from operand 1 ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L364-379]).
- For any other sample operand, call `reportFatalUsageError` with a message that names multisampled textures and sample 0.
- A pointer from `llvm.dx.resource.getpointer` on a multisampled handle carries no sample index. Keep the existing kind error for it.

| Operation | Opcode | Operands after the handle |
| --- | --- | --- |
| `AtomicBinOp` | 78 | atomicOp, x, y, layer or `poison`, newValue |
| `AtomicCompareExchange` | 79 | x, y, layer or `poison`, compareValue, newValue |

Sources: [microsoft/DirectXShaderCompiler:docs/DXIL.rst#atomicbinop], [microsoft/DirectXShaderCompiler:docs/DXIL.rst#atomiccompareexchange], [llvm/lib/Target/DirectX/DXIL.td:L1067-1086].

- The DXIL.rst tables do not list the multisampled types. The DXC validator accepts any UAV handle ([microsoft/DirectXShaderCompiler:lib/DxilValidation/DxilValidation.cpp:L3380-3391]). DXC emits `x`, `y` and `undef` for `RWTexture2DMS`, and `x`, `y` and the layer for `RWTexture2DMSArray`.
- `emitAtomicBinOp` already exchanges a `float` element as an `i32` ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L381-420]). Clang emits the float-bitwise compares as an `i32` `cmpxchg`, which `createTextureAtomicCompareExchange` accepts ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L499-514]).
- A 64-bit `llvm.dx.resource.atomic.binop` or `llvm.dx.resource.atomic.compare.exchange` on a typed resource sets `AtomicInt64OnTypedResource` ([llvm/lib/Target/DirectX/DXILShaderFlags.cpp:L310-321]). `isTyped` includes both multisampled kinds ([llvm/lib/Analysis/DXILResource.cpp:L407-432]), so the flag needs no code change.

| Flag | Shader flag bit | `SFI0` bit (mask) |
| --- | --- | --- |
| `Int64Ops` | 20 | 15 (`0x8000`) |
| `AtomicInt64OnTypedResource` | 27 | 22 (`0x400000`) |
| `WriteableMSAATextures` | 35 | 30 (`0x40000000`) |

Source: [llvm/include/llvm/BinaryFormat/DXContainerConstants.def:L39-54].

- A used `Texture2DMS` or `Texture2DMSArray` UAV handle also sets `WriteableMSAATextures`. That changes the combined value that `atomic64-typed-resource.ll` and `atomic64-typed-resource-cmpxchg.ll` check, so the multisampled cases go in a new file.

#### SPIR-V

- llvm/llvm-project#225543 builds `OpImageTexelPointer` for atomic users of `llvm.spv.resource.getpointer` on an image, with a constant 0 `Sample` operand. llvm/llvm-project#235929 uses the same texel pointer for `cmpxchg`.
- Build the same texel pointer for atomic users of `llvm.spv.resource.getpointer.ms`, and pass its sample operand as `Sample`. `OpImageTexelPointer` already has a `$sample` operand ([llvm/lib/Target/SPIRV/SPIRVInstrInfo.td:L266-268]). `selectResourceGetPointer` defines nothing for an image handle ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6689-6700]).
- [OpImageTexelPointer] accepts any sample index, constant or not, on a multisampled image.
- The backend already adds `StorageImageMultisample` for multisampled storage images, and `ImageMSArray` for arrayed ones ([llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp:L1195-1199]).

#### Test Cases

##### `clang/test/CodeGenHLSL/builtins/RWTexture-Interlocked.hlsl`

- A DXIL RUN line at `shadermodel6.7`: the writable multisampled members need SM6.7.
- `InterlockedAdd` on `operator[]` of `RWTexture2DMS<int>`: `getpointer.ms` with sample 0, then `atomicrmw add`.
- `InterlockedMax` and `InterlockedCompareExchange` on `sample[s][pos]` of `RWTexture2DMSArray<uint>`: `getpointer.ms` with `<3 x i32>` coordinates and the sample.
- `InterlockedCompareStore` on an `int64_t` element. `InterlockedExchange` on `int64_t` and `float` elements.
- `InterlockedCompareExchangeFloatBitwise` and `InterlockedCompareStoreFloatBitwise` on `RWTexture2DMS<float>`, inside the `#ifndef __SPIRV__` block of the existing float-bitwise calls.
- `target("dx.MSTexture", ...)` handles on DXIL. `spirv.Image`, or `spirv.SignedImage` for signed elements, with `MS` 1 and `Sampled` 2 on SPIR-V.

##### `clang/test/SemaHLSL/BuiltIns/InterlockedFloatBitwise-spirv.hlsl`

- Both functions on `operator[]` of `RWTexture2DMS<float>` and on `sample[s][pos]` of `RWTexture2DMSArray<float>`: the error.

##### `llvm/test/CodeGen/DirectX/ResourceAtomicBinOp-texture.ll`

- `atomicrmw` through `getpointer.ms` with sample 0 on `Texture2DMS` and `Texture2DMSArray` handles: two coordinates and `poison`, or three coordinates.
- `i32` and `i64` elements, and `xchg` on a `float` element.

##### `llvm/test/CodeGen/DirectX/ResourceAtomicCompareExchange-texture.ll`

- `cmpxchg` through `getpointer.ms` with sample 0 on both handle kinds, with `i32` and `i64` elements.
- An unused original value, as `InterlockedCompareStore` emits.

##### `llvm/test/CodeGen/DirectX/ResourceAtomicBinOp-texture-unsupported-kind.ll`

- `getpointer.ms` with the constant sample 2 and with a sample in an argument, on `Texture2DMS` and `Texture2DMSArray`: the new error.
- The existing `getpointer` cases on multisampled handles keep the kind error.

##### `llvm/test/CodeGen/DirectX/ResourceAtomicCompareExchange-texture-unsupported-kind.ll`

- The same `getpointer.ms` cases as for `atomicrmw`: the new error.
- A `texture2dmsarray` section with `getpointer`, like the one in the `atomicrmw` file: the kind error. The file has no such section today.

##### `llvm/test/CodeGen/DirectX/ShaderFlags/atomic64-typed-resource-ms.ll`

- `llvm.dx.resource.atomic.binop.i64` on a `Texture2DMS` UAV handle and `llvm.dx.resource.atomic.compare.exchange.i64` on a `Texture2DMSArray` UAV handle, in a `shadermodel6.7` module.
- The `print-dx-shader-flags` output: `Int64Ops`, `AtomicInt64OnTypedResource` and `WriteableMSAATextures`.
- The `SFI0` flags through `obj2yaml`, as in [llvm/test/CodeGen/DirectX/ShaderFlags/atomic64-typed-resource.ll].

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

- `atomicrmw` through `getpointer.ms` on a 2D multisampled image and on an arrayed multisampled image: `OpImageTexelPointer` with the sample.
- A constant sample 0, a constant sample 3 and a sample in a register.
- An `i64` element with `--spirv-ext=+SPV_EXT_shader_image_int64`.
- The `StorageImageMultisample` capability and `spirv-val --target-env vulkan1.3`.

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

- `cmpxchg` through `getpointer.ms` with the result used and unused, on both image kinds, with a constant and a register sample.
- `i32` and `i64` elements.

</details>

## References

<details>
<summary>HLSL</summary>

- [InterlockedAdd] — HLSL intrinsic reference; the other `Interlocked` pages are its siblings.
- [InterlockedCompareExchange] — HLSL compare-exchange intrinsic reference.
- [InterlockedCompareStore] — HLSL compare-store intrinsic reference.
- [SM6.6 64-bit and float atomics] — 64-bit integer and float atomic functions.
- [SM6.7 Writable MSAA Textures] — Writable multisampled texture types.

</details>

<details>
<summary>DirectX</summary>

- [microsoft/DirectXShaderCompiler:docs/DXIL.rst#atomicbinop] — DXIL `AtomicBinOp` operation, opcode 78.
- [microsoft/DirectXShaderCompiler:docs/DXIL.rst#atomiccompareexchange] — DXIL `AtomicCompareExchange` operation, opcode 79.

</details>

<details>
<summary>SPIR-V</summary>

- [OpImageTexelPointer] — Pointer to a texel sample for atomic operations.
- [SPIR-V Capabilities] — `StorageImageMultisample` and `ImageMSArray`.

</details>

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

[clang/test/CodeGenHLSL/builtins/RWTexture-Interlocked.hlsl]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/test/CodeGenHLSL/builtins/RWTexture-Interlocked.hlsl
[clang/lib/Sema/SemaHLSL.cpp:L4548-4618]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/SemaHLSL.cpp#L4548-L4618
[llvm/lib/Target/DirectX/DXILResourceAccess.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILResourceAccess.cpp
[llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L364-379]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILResourceAccess.cpp#L364-L379
[llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L381-420]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILResourceAccess.cpp#L381-L420
[llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L499-514]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILResourceAccess.cpp#L499-L514
[llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L529-537]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILResourceAccess.cpp#L529-L537
[llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L566-574]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/DirectX/DXILResourceAccess.cpp#L566-L574
[llvm/lib/Target/DirectX/DXIL.td<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