Issue 238061
Summary [HLSL][DirectX][SPIRV] Implement `RWTexture2DMS` and `RWTexture2DMSArray`
Labels backend:DirectX, HLSL, backend:SPIR-V
Assignees
Reporter Icohedron
    Clang does not declare `RWTexture2DMS` and `RWTexture2DMSArray`. Declare both types and their members. Implement the writable-sample builtin and LLVM interface adopted by llvm/wg-hlsl#512. Translate reads and stores of samples to DirectX `load.ms` and `store.ms`, and to SPIR-V `OpImageRead` and `OpImageWrite` with the `Sample` image operand.

On the DirectX target, both types require Shader Model 6.7.

## Acceptance Criteria

### HLSL

- [ ] Declare the `RWTexture2DMS` and `RWTexture2DMSArray` texture types in [clang/lib/Sema/HLSLExternalSemaSource.cpp].
- [ ] Add `Load(Location, SampleIndex)`, `GetDimensions` and `GetSamplePosition` to `RWTexture2DMS` and `RWTexture2DMSArray` in [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp] and [clang/lib/Sema/HLSLExternalSemaSource.cpp].
- [ ] Make `GetSamplePosition` on both writable types reuse native DXIL lowering and accepted shared SPIR-V emulation in [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp]. Require the warning about standard 1/2/4/8/16 positions and unsupported-count `(0, 0)`, rather than arbitrary device locations.
- [ ] Implement the approved builtin interface for writable multisampled sample references in [clang/include/clang/Basic/Builtins.td] and [clang/lib/Sema/SemaHLSL.cpp].
- [ ] Generate DirectX and SPIR-V LLVM IR for writable multisampled sample references using the builtin and intrinsic interfaces adopted by llvm/wg-hlsl#512, in [clang/lib/CodeGen/CGHLSLBuiltins.cpp], [clang/lib/CodeGen/CGHLSLRuntime.h], [llvm/include/llvm/IR/IntrinsicsDirectX.td] and [llvm/include/llvm/IR/IntrinsicsSPIRV.td].
- [ ] Make `operator[]` and `sample[][]` of `RWTexture2DMS` and `RWTexture2DMSArray` return writable references through the approved builtin interface in [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp].
- [ ] Make every member of `RWTexture2DMS` and `RWTexture2DMSArray` available from Shader Model 6.7 on the DirectX target in [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp].
- [ ] Add AST tests of `RWTexture2DMS` and `RWTexture2DMSArray` to [clang/test/AST/HLSL/MultiSampledTextures-AST.hlsl].
- [ ] Add Sema tests to [clang/test/SemaHLSL/Resources/MultiSampledTextures-Sema.hlsl].
- [ ] Add Sema tests of the approved writable-sample builtin interface to [clang/test/SemaHLSL/BuiltIns/resource_getpointer-errors.hlsl].
- [ ] Add Sema tests to `clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl` that reject `RWTexture2DMS` and `RWTexture2DMSArray` with 16-bit or `double` elements on Vulkan.
- [ ] Add HLSL codegen tests to `clang/test/CodeGenHLSL/resources/RWMultiSampledTextures.hlsl` (new).

### DirectX

- [ ] Lower writable multisampled sample-reference loads and stores, including partial-element stores, to `llvm.dx.resource.load.ms` and `llvm.dx.resource.store.ms` in [llvm/lib/Target/DirectX/DXILResourceAccess.cpp].
- [ ] Set the `TypedUAVLoadAdditionalFormats` shader flag for `llvm.dx.resource.load.ms` reads of writable multisampled textures with multi-component elements in [llvm/lib/Target/DirectX/DXILShaderFlags.cpp].
- [ ] Add DXIL resource access tests of writable multisampled sample-reference loads to [llvm/test/CodeGen/DirectX/ResourceAccess/load_texture.ll].
- [ ] Add DXIL resource access tests of writable multisampled sample-reference stores and partial-element stores to [llvm/test/CodeGen/DirectX/ResourceAccess/store_texture.ll].
- [ ] Add shader flag tests of `llvm.dx.resource.load.ms` on writable multisampled textures to [llvm/test/CodeGen/DirectX/ShaderFlags/typed-uav-load-additional-formats.ll].
- [ ] Document the approved writable multisampled sample-reference LLVM interface in the "Accessing Resources as Memory" section of [llvm/docs/DirectX/DXILResources.md].

### SPIR-V

- [ ] Lower writable multisampled sample-reference loads and stores, including partial-element stores, to `OpImageRead` and `OpImageWrite` with `Sample` in [llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp] and [llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp].
- [ ] Lower `Load` calls on `RWTexture2DMS` and `RWTexture2DMSArray` to `OpImageRead` with the `Sample` image operand in [llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp].
- [ ] Add SPIR-V lowering tests to `llvm/test/CodeGen/SPIRV/hlsl-resources/LoadStoreMS.ll` (new).

## Notes

- Reads of only the first component (`.x`) of a typed texture element wait on llvm/llvm-project#191248 and llvm/llvm-project#235902. On DirectX, a compound assignment to `.x`, such as `T[pos].x += v`, also reads only the first component. Today it asserts in the DXIL resource access pass for `RWTexture2D`.
- llvm/llvm-project#235905 creates `clang/test/SemaHLSL/Resources/TypedResources-element-type-Vulkan.hlsl`. The file does not exist at the linked revision.

### Suggested Implementation

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

The `__builtin_hlsl_resource_getpointer_ms`, `llvm.dx.resource.getpointer.ms` and `llvm.spv.resource.getpointer.ms` names and signatures below are proposed interfaces. Use them only if the amendment adopts them. The HLSL references, target operations, flags and test coverage are required independently of those names.

#### HLSL Interface

```hlsl
template <typename T, int N = 0> class RWTexture2DMS;      // T has no default, as in Texture2DMS
template <typename T, int N = 0> class RWTexture2DMSArray;
```

| Member | `RWTexture2DMS` | `RWTexture2DMSArray` | Result |
| --- | --- | --- | --- |
| `Load(Location, SampleIndex)` | `int2`, `int` | `int3`, `int` | `T` |
| `sample[SampleIndex][Position]` | `uint`, `uint2` | `uint`, `uint3` | `T &` |
| `operator[](Position)` | `uint2` | `uint3` | `T &` of sample 0 |
| `GetDimensions` | width, height, samples | width, height, elements, samples | all `uint` or all `float` outputs |
| `GetSamplePosition(SampleIndex)` | `int` | `int` | `float2` |

Sources: [SM6.7 Writable MSAA Textures], [Texture2DMS methods], [Texture2DMSArray methods].

- The z component of a `RWTexture2DMSArray` location or position is the array layer.
- `Load` has no offset overload: the DXIL validator rejects offsets on UAV loads ([microsoft/DirectXShaderCompiler:lib/DxilValidation/DxilValidation.cpp:L2726-2737]).
- Stores through `operator[]` and `sample[][]` include whole-element, component and swizzle stores, compound assignment and `out` or `inout` arguments.
- [SM6.7 Writable MSAA Textures] calls the sample count `N` required. Match DXC and `Texture2DMS`: `N` defaults to 0.
- Writable `GetSamplePosition` reuses the readonly builtin, native DXIL lowering, SPIR-V standard-table emulation and required limits warning. llvm/wg-hlsl#514 owns common policy; no independent table or rejection rule belongs here.

1. Add `RWTexture2DMS` and `RWTexture2DMSArray` rows to `TextureTypes` in [clang/lib/Sema/HLSLExternalSemaSource.cpp] ([clang/lib/Sema/HLSLExternalSemaSource.cpp:L342-392]): `ResourceClass::UAV`, `ResourceDimension::Dim2D`, `TemplateShape::ElementTypeAndSampleCount`, and the `sample[][]`, `GetDimensions` and `GetSamplePosition` capabilities of the readonly multisampled rows. The handle types follow from the UAV class: `target("dx.MSTexture", T, 1, N, IsSigned, Kind)` and `spirv.Image` with `MS` 1 and `Sampled` 2 ([clang/lib/CodeGen/Targets/SPIR.cpp:L917-922]).
2. `addTextureLoadMSMethods` ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L1746-1779]) always adds the offset overload. Give the writable rows `Load(Location, SampleIndex)` only, through `__builtin_hlsl_resource_load_ms` with three arguments. `CheckLoadMSBuiltin` already accepts three arguments ([clang/lib/Sema/SemaHLSL.cpp:L4409-4447]), and Clang then emits a zero offset ([clang/lib/CodeGen/CGHLSLBuiltins.cpp:L954-969]).
3. Add `HLSLResourceGetPointerMS` with the spelling `__builtin_hlsl_resource_getpointer_ms` and prototype `void(...)` beside `HLSLResourceGetPointer` in [clang/include/clang/Basic/Builtins.td] ([clang/include/clang/Basic/Builtins.td:L5396-5400]).
4. In [clang/lib/Sema/SemaHLSL.cpp], check the builtin beside `__builtin_hlsl_resource_getpointer` ([clang/lib/Sema/SemaHLSL.cpp:L4719-4735]):
   - three arguments: handle, position, sample index;
   - a multisampled handle, with the predicate of `CheckLoadMSBuiltin`;
   - an integer position with the dimension count of the resource, through `CheckIndexType` ([clang/lib/Sema/SemaHLSL.cpp:L4080-4116]);
   - an integer scalar sample index;
   - the result type of `__builtin_hlsl_resource_getpointer`: a pointer to the element type in the address space of the resource class.
5. In [clang/lib/CodeGen/CGHLSLRuntime.h], add `GENERATE_HLSL_INTRINSIC_FUNCTION(CreateResourceGetPointerMS, resource_getpointer_ms)` beside `CreateResourceGetPointer` ([clang/lib/CodeGen/CGHLSLRuntime.h:L174-177]). In [clang/lib/CodeGen/CGHLSLBuiltins.cpp], emit the intrinsic beside `__builtin_hlsl_resource_getpointer` ([clang/lib/CodeGen/CGHLSLBuiltins.cpp:L770-789]), with the result, handle and position types as overloads and the sample index as `i32`.
6. Define `int_dx_resource_getpointer_ms` in [llvm/include/llvm/IR/IntrinsicsDirectX.td] and `int_spv_resource_getpointer_ms` in [llvm/include/llvm/IR/IntrinsicsSPIRV.td]: `[llvm_anyptr_ty]`, `[llvm_any_ty, llvm_any_ty, llvm_i32_ty]`, with the attributes of `getpointer` ([llvm/include/llvm/IR/IntrinsicsDirectX.td:L55-57], [llvm/include/llvm/IR/IntrinsicsSPIRV.td:L654-656]).
7. In [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp], the writable rows get:
   - `operator[](Position)`: a writable reference from `__builtin_hlsl_resource_getpointer_ms(Handle, Position, 0)`. `addArraySubscriptOperators` and `addHandleAccessFunction` call `__builtin_hlsl_resource_getpointer` today ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L1541-1562], [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L2713-2757]).
   - `sample[][]`: the `sample_type` and `sample_slice_type` records of the readonly types, with a `sample_slice_type::operator[](Position)` that returns a writable reference from `__builtin_hlsl_resource_getpointer_ms(Handle, Position, __sample)`.
8. Add an implicit `AvailabilityAttr` for the `shadermodel` platform, introduced in 6.7 and with no environment, to each member method of both types, including the `operator[]` methods of the nested `sample` records. `addDerivativeAvailabilityAttrs` is the model ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L115-139]). The SPIR-V target has no shader model rule.

#### DirectX

- `DXILResourceAccess` handles only `llvm.dx.resource.getpointer` and `llvm.dx.resource.getbasepointer`. Add `llvm.dx.resource.getpointer.ms` in [llvm/lib/Target/DirectX/DXILResourceAccess.cpp] wherever `getpointer` appears: `traverseGEPOffsets` ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L66-76]), `collectUsedHandles` ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L1060-1095]), `getAccessIndices` ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L1176-1200]) and `transformResourcePointers` ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L1469-1490]). Forward the sample index (operand 2).
- For a pointer from `llvm.dx.resource.getpointer.ms`:
  - A load becomes `llvm.dx.resource.load.ms(handle, coords, sample, zero offsets)`. Extract the component as `createTextureLoad` does ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L619-654]). `createTextureLoad` extracts only at a nonzero offset, so a scalar load at offset 0 asserts until llvm/llvm-project#191248 lands.
  - A whole-element store becomes `llvm.dx.resource.store.ms(handle, coords, sample, value)`.
 - A partial-element store loads the element of the same sample with `llvm.dx.resource.load.ms`, inserts the stored component at the `traverseGEPOffsets` offset and stores the element with `llvm.dx.resource.store.ms`. `createTextureStore` does the same with `load.level` ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L172-209]).
 - Stores to `Texture2DMS` and `Texture2DMSArray` handles report "DXIL Store not implemented for this texture resource kind" today ([llvm/lib/Target/DirectX/DXILResourceAccess.cpp:L265-297]).
- `llvm.dx.resource.store.ms`, its lowering to `TextureStoreSample`, its 64-bit split and its shader flags come from llvm/llvm-project#238060. `llvm.dx.resource.load.ms` already lowers to `TextureLoad` and keeps the sample slot for multisampled UAVs ([llvm/lib/Target/DirectX/DXILOpLowering.cpp:L710-756]).
- In `DXILOpLowering.cpp`, add `dx_resource_getpointer_ms` to the `lowerGetPointer` dispatch ([llvm/lib/Target/DirectX/DXILOpLowering.cpp:L1423-1426]), which removes the dead declaration.

| Flag | Shader flag bit | `SFI0` bit (mask) | Set when |
| --- | --- | --- | --- |
| `TypedUAVLoadAdditionalFormats` | 13 | 11 (`0x800`) | `llvm.dx.resource.load.ms` on a UAV handle whose element has more than one component |

Sources: [microsoft/DirectXShaderCompiler:lib/DXIL/DxilShaderFlags.cpp:L603-636], [llvm/include/llvm/BinaryFormat/DXContainerConstants.def:L35].

- In [llvm/lib/Target/DirectX/DXILShaderFlags.cpp], the `TypedUAVLoadAdditionalFormats` rule handles only `load.level` and `load.typedbuffer` ([llvm/lib/Target/DirectX/DXILShaderFlags.cpp:L294-304]). Add `load.ms` to it. DXC falls through from `TextureLoad` to the same rule.
- [llvm/docs/DirectX/DXILResources.md] documents only `llvm.dx.resource.getpointer` in "Accessing Resources as Memory" ([llvm/docs/DirectX/DXILResources.md:L277-321]). Add an `@llvm.dx.resource.getpointer.ms` table with its sample operand.

#### SPIR-V

- `selectLoad` ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L2014-2040]) and `selectStore` ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L2199-2252]) emit `OpImageRead` and `OpImageWrite` only for a pointer from `llvm.spv.resource.getpointer` or `llvm.spv.resource.getbasepointer`. Accept `llvm.spv.resource.getpointer.ms` and pass its sample index (operand 4) in the `Sample` image operand (0x40). On signed images, set both bits in one mask (`SignExtend|Sample`, 0x1040); the sample index is the only operand after the mask.
- `generateImageReadOrFetch` adds image operands only for `OpImageFetch` ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6595-6687]). Emit `Sample` for `OpImageRead` too. The `selectLoadMSIntrinsic` selection of `llvm.spv.resource.load.ms` then gives `OpImageRead` with `Sample` on a storage image.
- Dispatch `spv_resource_getpointer_ms` to `selectResourceGetPointer` beside `spv_resource_getpointer` ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L5893-5896]); it emits nothing for images ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6689-6700]). Add it to `intrinsicHasSideEffects` beside `spv_resource_getpointer` ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L733-736]).
- In [llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp], treat `spv_resource_getpointer_ms` like `spv_resource_getpointer` in `expectIgnoredInIRTranslation` ([llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp:L430-435]) and in the element type deduction for image handles ([llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp:L1095-1125]). Extend the partial-element store rewrite of llvm/llvm-proj<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