Issue 238570
Summary [HLSL][SPIRV] Implement `Gather`, `GatherRed`, `GatherGreen`, `GatherBlue`, `GatherAlpha`, `GatherCmp` and `GatherCmpRed` on `vk::SampledTexture` types
Labels HLSL, backend:SPIR-V
Assignees
Reporter Icohedron
    The `vk::SampledTexture` types have no gather methods. These Vulkan-only textures carry their own sampler. Add the gather and comparison gather methods to the 2D and cube `vk::SampledTexture` types, without a sampler parameter. Include the single-offset and four-offset overloads of the matching `Texture*` methods. The SPIR-V gather lowering fails on a combined image sampler handle. Make it gather from the loaded sampled image. Make `Gather`, `GatherRed`, `GatherGreen`, `GatherBlue` and `GatherAlpha` work on `vk::SampledTexture` types with scalar and two-component 64-bit integer elements.

## Acceptance Criteria

### Clang

- [ ] Enable the gather methods on `vk::SampledTexture2D`, `vk::SampledTexture2DArray`, `vk::SampledTextureCUBE` and `vk::SampledTextureCUBEArray` in [clang/lib/Sema/HLSLExternalSemaSource.cpp].
- [ ] Declare the gather and comparison gather methods, with their four-offset overloads, without a sampler parameter on the `vk::SampledTexture` types in [clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp].

### SPIR-V

- [ ] Lower `llvm.spv.resource.gather`, `llvm.spv.resource.gather.cmp` and their four-offset forms on a combined image sampler handle without `OpSampledImage` in [llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp]. Include `llvm.spv.resource.gather` on scalar and two-component 64-bit integer elements.

### Tests

- [ ] Add Sema tests to `clang/test/SemaHLSL/Resources/vk.SampledTextures-Gather.hlsl` (new).
- [ ] Change the gather cases of `clang/test/SemaHLSL/Resources/vk.SampledTextures-Sema.hlsl` from undeclared methods to accepted calls.
- [ ] Add HLSL codegen tests, including scalar and two-component 64-bit integer elements, to `clang/test/CodeGenHLSL/resources/vk.SampledTextures-Gather.hlsl` (new).
- [ ] Add SPIR-V lowering tests, including scalar and two-component 64-bit integer elements, to `llvm/test/CodeGen/SPIRV/hlsl-resources/SampledImage-Gather.ll` (new).
- [ ] Add SPIR-V lowering tests of the four-offset gathers to `llvm/test/CodeGen/SPIRV/hlsl-resources/SampledImage-GatherFourOffsets.ll` (new).

## Notes

- The SPIR-V gather selector asserts that its image operand is an `OpTypeImage` ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6508-6593]). A combined image sampler handle is an `OpTypeSampledImage`, so every gather on a `vk::SampledTexture` type fails there.
- `GatherCmpGreen`, `GatherCmpBlue` and `GatherCmpAlpha` stay errors on SPIR-V. The existing check reports `err_hlsl_gathercmp_invalid_component`, as for `Texture*` types.

### Suggested Implementation

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

Use the combined-resource representation and builtin/intrinsic operands adopted by llvm/wg-hlsl#513 through llvm/llvm-project#238561. The attribute/type/helper names, duplicate-handle operand examples and no-new-Sema/codegen recipe below apply only if that recommended design is adopted. Source methods, rejection rules, 64-bit coverage and SPIR-V operations remain required.

#### HLSL Interface

| Type | `location` | Methods |
| --- | --- | --- |
| `vk::SampledTexture2D` | `float2` | `Gather`, `GatherRed`, `GatherGreen`, `GatherBlue`, `GatherAlpha`, `GatherCmp`, `GatherCmpRed`; single-offset overloads; four-offset overloads of the named methods |
| `vk::SampledTexture2DArray` | `float3` | the same |
| `vk::SampledTextureCUBE` | `float3` | the same, without offset overloads |
| `vk::SampledTextureCUBEArray` | `float4` | the same as `vk::SampledTextureCUBE` |

Source: the same-interface rule of [microsoft/hlsl-specs:proposals/0034-vk-sampled-texture.md#hlsl-additions], applied to the `Texture2D`, `Texture2DArray`, `TextureCube` and `TextureCubeArray` gather methods.

- Each method has the overloads of the matching `Texture*` method without the `SamplerState` or `SamplerComparisonState` parameter.
- `GatherCmp` and `GatherCmpRed` accept integer elements, as on `Texture*` types.
- `vk::SampledTexture1D`, `vk::SampledTexture1DArray` and `vk::SampledTexture3D` get no gather methods, as their `Texture*` types.

#### Clang

- In [clang/lib/Sema/HLSLExternalSemaSource.cpp], add `TexCap::Gather` to the `vk::SampledTexture2D`, `vk::SampledTexture2DArray`, `vk::SampledTextureCUBE` and `vk::SampledTextureCUBEArray` entries of `TextureTypes[]`. This bit makes `setupTextureType` call `addGatherMethods` and `addGatherCmpMethods` ([clang/lib/Sema/HLSLExternalSemaSource.cpp:L342-392]).
- In `addGatherMethods` ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L2426-2481]) and `addGatherCmpMethods` ([clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L2483-2544]), omit the `Sampler` parameter when the handle has `IsCombinedImageSampler`. Pass `PH::Handle` as argument 1 of the builtin, and shift the later placeholders. Do the same in the four-offset overloads.
- In the same case, skip the `SamplerState` and `SamplerComparisonState` lookups at the start of both adders. `lookupBuiltinType` searches only the record's context, which is `hlsl::vk` for these types, and asserts when it finds no type.
- `CheckGatherBuiltin` ([clang/lib/Sema/SemaHLSL.cpp:L4283-4361]) and the gather cases of `clang/lib/CodeGen/CGHLSLBuiltins.cpp` need no change. The handle reaches both intrinsic operands.

#### SPIR-V

- In `selectGatherIntrinsic` ([llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6508-6593]), replace the image assert, the separate image and sampler loads and the `OpSampledImage` with `loadSampledImageForUse`. Read the dimension and the sampled type from the image operand of the `OpTypeSampledImage`.
- Use the helper on every gather path of the function: the single gather, the `ConstOffsets` gather, the four-gather fallback and the packed 64-bit path. Each gather then loads the variable in its own block.
- Clang stores a two-component 64-bit element as a packed four-component `i32` image. llvm/llvm-project#236254 adds the packed path to `selectGatherIntrinsic`. It detects the packed image from the sampled type. Read that sampled type from the inner image, so that the detection also works for a combined handle.
- A scalar 64-bit element uses an `i64` image. Its gather needs `OpCapability Int64` and no `Int64ImageEXT`.
- No new capability. `ConstOffsets` and `Offset` already add `ImageGatherExtended`.

#### Test Cases

The LLVM type and duplicate-handle operand examples below are illustrative until the proposal adopts them. Follow the adopted representation for these same test cases, retaining all SPIR-V and validation coverage.

##### `clang/test/SemaHLSL/Resources/vk.SampledTextures-Gather.hlsl`

- RUN lines with `-triple spirv-vulkan-library` and `-DTEXTURE=vk::SampledTexture2D` style parameters, as [clang/test/SemaHLSL/Resources/Textures-GatherCmp-Vulkan.hlsl] does.
- Each overload on `vk::SampledTexture2D` and `vk::SampledTexture2DArray`, with constant and nonconstant offsets.
- `vk::SampledTextureCUBE` and `vk::SampledTextureCUBEArray`: no offset overloads.
- A `SamplerState` or `SamplerComparisonState` argument: no matching member function.
- `Gather` and `GatherCmp` with four offsets: rejected.
- `GatherCmpGreen`, `GatherCmpBlue` and `GatherCmpAlpha`, with and without four offsets: `err_hlsl_gathercmp_invalid_component`.
- `GatherCmp` and `GatherCmpRed` on `int` and `uint` elements: accepted.
- `vk::SampledTexture1D`, `vk::SampledTexture1DArray` and `vk::SampledTexture3D`: no gather members.

##### `clang/test/SemaHLSL/Resources/vk.SampledTextures-Sema.hlsl`

- `Gather` and `GatherCmp` calls that the file expects to be undeclared: now accepted.

##### `clang/test/CodeGenHLSL/resources/vk.SampledTextures-Gather.hlsl`

- `llvm.spv.resource.gather` and `llvm.spv.resource.gather.cmp` with the same `spirv.SampledImage` handle as operands 0 and 1.
- Channels 0 to 3, a zero offset and a constant offset.
- The four-offset intrinsics with the handle as operands 0 and 1.
- `int4` elements: `spirv.SignedSampledImage` and a `<4 x i32>` result.
- The cube types without an offset operand.
- `vk::SampledTexture2D<int64_t>` and `vk::SampledTexture2D<uint64_t>`: an `i64` sampled image and a `<4 x i64>` result.
- `vk::SampledTexture2D<int64_t2>` and `vk::SampledTexture2D<uint64_t2>`: a packed four-component `i32` sampled image and a `<4 x i64>` result.

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

- RUN lines: `llc -O0 -verify-machineinstrs -mtriple=spirv1.6-vulkan1.3-compute` with FileCheck, and `spirv-val --target-env vulkan1.3`, as [llvm/test/CodeGen/SPIRV/hlsl-resources/LoadLevel.ll] does. Give `@main` `"hlsl.numthreads"="1,1,1"` and `"hlsl.shader"="compute"`. Take the gather checks from [llvm/test/CodeGen/SPIRV/hlsl-resources/Gather.ll].
- `OpImageGather` and `OpImageDrefGather` on an `OpLoad` of the `OpTypeSampledImage` variable. No `OpSampledImage` and no `OpTypeSampler`.
- No offset, `ConstOffset` and `Offset`, with `OpCapability ImageGatherExtended`.
- A 2D arrayed image and a cube image.
- A `spirv.SignedSampledImage` handle with `<4 x i32>` results, and a `uint` comparison gather with a `v4uint` result.
- A gather in another block than the handle: the `OpLoad` is in that block.
- A `spirv.Image` handle and a `spirv.Sampler` handle in the same module: `OpSampledImage` as before.
- A scalar `i64` sampled image: one `OpImageGather` with a `v4i64` result, `OpCapability Int64` and no `Int64ImageEXT`.
- A packed two-component 64-bit element, an `OpTypeSampledImage` of a four-component `i32` image. `GatherRed` gives two `v4i32` gathers of channels 0 and 1, joined with `OpBitcast`. `GatherGreen` gathers channels 2 and 3. `GatherBlue` gives a zero `v4i64` constant and `GatherAlpha` a one `v4i64` constant. No `v4i64` gather on the `i32` image.

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

- The same RUN lines.
- Constant offsets in range: one `OpImageGather` with `ConstOffsets`.
- A nonconstant offset: four `OpImageGather`, each on a loaded sampled image. Lane *i* comes from gather *i*.
- A constant offset outside the gather offset range: four gathers.
- The four-offset `OpImageDrefGather` in both forms.

</details>

## References

<details>
<summary>HLSL</summary>

- [microsoft/hlsl-specs:proposals/0034-vk-sampled-texture.md#hlsl-additions] — `vk::SampledTexture` types and their methods.
- [Texture2D::Gather] — HLSL method reference.
- [Texture2D::GatherRed] — HLSL method reference.
- [Texture2D::GatherGreen] — HLSL method reference.
- [Texture2D::GatherBlue] — HLSL method reference.
- [Texture2D::GatherAlpha] — HLSL method reference.
- [Texture2D::GatherRed four-offset overload] — Four-offset `GatherRed` overload.
- [Texture2D::GatherCmp] — HLSL method reference.
- [Texture2D::GatherCmpRed] — HLSL method reference.
- [Texture2D::GatherCmpRed four-offset overload] — Four-offset `GatherCmpRed` overload.

</details>

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

- [OpTypeSampledImage] — SPIR-V sampled image type.
- [OpImageGather] — SPIR-V gather instruction.
- [OpImageDrefGather] — SPIR-V depth-comparison gather.
- [SPIR-V image operands] — `ConstOffsets`, `ConstOffset` and `Offset` image operands.
- [OpBitcast] — SPIR-V bit pattern conversion between `v4i32` and `v2i64`.

</details>

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

[clang/lib/Sema/HLSLExternalSemaSource.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/HLSLExternalSemaSource.cpp
[clang/lib/Sema/HLSLExternalSemaSource.cpp:L342-392]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/HLSLExternalSemaSource.cpp#L342-L392
[clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp
[clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L2426-2481]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp#L2426-L2481
[clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp:L2483-2544]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp#L2483-L2544
[clang/lib/Sema/SemaHLSL.cpp:L4283-4361]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/lib/Sema/SemaHLSL.cpp#L4283-L4361
[llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
[llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp:L6508-6593]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp#L6508-L6593
[clang/test/SemaHLSL/Resources/Textures-GatherCmp-Vulkan.hlsl]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/clang/test/SemaHLSL/Resources/Textures-GatherCmp-Vulkan.hlsl
[llvm/test/CodeGen/SPIRV/hlsl-resources/Gather.ll]: https://github.com/llvm/llvm-project/blob/da1ff2f70e1c2d3289cf9dea5478e8935a8d0fd7/llvm/test/CodeGen/SPIRV/hlsl-resources/Gather.ll
[microsoft/hlsl-specs:proposals/0034-vk-sampled-texture.md#hlsl-additions]: https://github.com/microsoft/hlsl-specs/blob/5c1aae7ef249efba8a993ca50d69430c379ac1a1/proposals/0034-vk-sampled-texture.md#hlsl-additions
[Texture2D::Gather]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture2d-gather
[Texture2D::GatherRed]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture2d-gatherred
[Texture2D::GatherGreen]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture2d-gathergreen
[Texture2D::GatherBlue]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture2d-gatherblue
[Texture2D::GatherAlpha]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture2d-gatheralpha
[Texture2D::GatherRed four-offset overload]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/t2d-gatherred-s-float-int2-int2-int2-int2-
[Texture2D::GatherCmp]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture2d-gathercmp
[Texture2D::GatherCmpRed]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/sm5-object-texture2d-gathercmpred
[Texture2D::GatherCmpRed four-offset overload]: https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/t2d-gathercmpred-s-float-float-int2-int2-int2-int2-
[OpTypeSampledImage]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpTypeSampledImage
[OpImageGather]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageGather
[OpImageDrefGather]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpImageDrefGather
[SPIR-V image operands]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Image_Operands
[OpBitcast]: https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpBitcast
[llvm/test/CodeGen/SPIRV/hlsl-resour<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