feat: WaveSync and the guest wave size for compute: wave-wide gathers through groupshared memory #2783

Closed
tedin7 wants to merge 1 commit from upstream-pr/2791 into main
tedin7 commented 2026-11-26 09:35:54 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T09:35:54Z.


Part of #1723

What this unblocks: wave-wide answers (any lane, first lane, one lane) for compute programs in a guest wave that the host splits arbitrarily, and v_readfirstlane_b32 of a per-lane VGPR; the base for the tick-mode and wave-wide EXEC work that stacks on it.

Source: DirectX-Specs, HLSL shader model 6.0 wave intrinsics and "groupshared" memory with GroupMemoryBarrierWithGroupSync (no host wave intrinsic is used); AMD "RDNA 2" Instruction Set Architecture reference guide, sections on wave32/wave64 execution, the EXEC mask and V_READFIRSTLANE_B32 (lowest lane with its EXEC bit set; lane 0 when EXEC is zero).

What changed

  • WaveSync (tools/startup/gfx2hlsl.py): a thread publishes its bit and value into groupshared words, the group meets at one barrier, every thread reads its guest wave's word: gather_any, gather_first, gather_lane. Buffers rotate by site number so a site never reuses a word still being read. A site is only legal where control flow is uniform across the group, otherwise Unsupported. A guest wave is wave consecutive threads in linear thread order (32 or 64), so the result does not depend on the host wave size (WARP has 4-lane waves).
  • ComputePipeline(..., wave=None) takes the guest wave size, or reads it from the program's mask operations (exec_lo / _b32 forms: 32; exec, exec_hi, _b64 forms or a pair-destination compare: 64; none or mixed: unknown, no gather is emitted). It emits the WaveSync declarations and prologue only when a site was used.
  • v_readfirstlane_b32 of a VGPR that is not proven wave-uniform becomes a gather_first site (EXEC bit as the active expression; lane 0 of the wave when EXEC is zero), for groups up to 1024 threads. Programs with any branch not proved the same for every lane are refused after the pass (the barrier must be reached by every thread), as is an unknown guest wave size.
  • A known wave32 program writes only vcc_lo for a compare, and a branch on exec names no wave size.
  • Supersedes #2745 (the same read on a fixed 32-thread groupshared array); that draft can be closed.

What stays refused

  • Unknown guest wave size; groups above 1024; any program with a branch not proved uniform. Per-lane s_cbranch_execz skips are the next step (wave-wide EXEC decisions) and are not in this PR.

Tests

  • New startup_gfx2hlsl_wavesync_check.py (text and refusals of the three gathers, uniform-flow refusal, wave and group limits); registered as startup_gfx2hlsl_wavesync.
  • startup_compute_waterfall.py (run by startup_compute_readfirstlane): first-lane programs for groups of 4, 32, 33, 64 (wave 32) and 64 (wave 64), wave inference unit checks, and a WARP dispatch case with 32- and 64-thread groups (one and two guest waves) and seven EXEC thresholds, each compared with a CPU reference per guest wave, plus a wrong-lane control (lane 0 instead of the first enabled lane).
  • startup_gfx2hlsl_compute_check.py: a per-lane address read translates with a known wave and is refused without one.
  • startup_compute_readfirstlane_check.py: a thread-id source now translates through the gather; the lane-mask controls still refuse.

Verification

  • Linux: python3 -B tests/startup_gfx2hlsl_wavesync_check.py, tests/startup_compute_readfirstlane_check.py --llvm-mc llvm-mc-19, tests/startup_gfx2hlsl_compute_check.py all rc=0.
  • Linux: the full tests/startup_*check.py set differs from main only by the new check (rc=0).
  • Win2025 WARP dev+release: pending Stage-2. The HLSL with barriers and InterlockedMin was not compiled with DXC/FXC here.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6623717887 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2791). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T09:35:54Z. --- Part of #1723 What this unblocks: wave-wide answers (any lane, first lane, one lane) for compute programs in a guest wave that the host splits arbitrarily, and `v_readfirstlane_b32` of a per-lane VGPR; the base for the tick-mode and wave-wide EXEC work that stacks on it. Source: DirectX-Specs, HLSL shader model 6.0 wave intrinsics and "groupshared" memory with `GroupMemoryBarrierWithGroupSync` (no host wave intrinsic is used); AMD "RDNA 2" Instruction Set Architecture reference guide, sections on wave32/wave64 execution, the EXEC mask and `V_READFIRSTLANE_B32` (lowest lane with its EXEC bit set; lane 0 when EXEC is zero). ## What changed - `WaveSync` (`tools/startup/gfx2hlsl.py`): a thread publishes its bit and value into groupshared words, the group meets at one barrier, every thread reads its guest wave's word: `gather_any`, `gather_first`, `gather_lane`. Buffers rotate by site number so a site never reuses a word still being read. A site is only legal where control flow is uniform across the group, otherwise `Unsupported`. A guest wave is `wave` consecutive threads in linear thread order (32 or 64), so the result does not depend on the host wave size (WARP has 4-lane waves). - `ComputePipeline(..., wave=None)` takes the guest wave size, or reads it from the program's mask operations (`exec_lo` / `_b32` forms: 32; `exec`, `exec_hi`, `_b64` forms or a pair-destination compare: 64; none or mixed: unknown, no gather is emitted). It emits the `WaveSync` declarations and prologue only when a site was used. - `v_readfirstlane_b32` of a VGPR that is not proven wave-uniform becomes a `gather_first` site (EXEC bit as the active expression; lane 0 of the wave when EXEC is zero), for groups up to 1024 threads. Programs with any branch not proved the same for every lane are refused after the pass (the barrier must be reached by every thread), as is an unknown guest wave size. - A known wave32 program writes only `vcc_lo` for a compare, and a branch on exec names no wave size. - Supersedes #2745 (the same read on a fixed 32-thread groupshared array); that draft can be closed. ## What stays refused - Unknown guest wave size; groups above 1024; any program with a branch not proved uniform. Per-lane `s_cbranch_execz` skips are the next step (wave-wide EXEC decisions) and are not in this PR. ## Tests - New `startup_gfx2hlsl_wavesync_check.py` (text and refusals of the three gathers, uniform-flow refusal, wave and group limits); registered as `startup_gfx2hlsl_wavesync`. - `startup_compute_waterfall.py` (run by `startup_compute_readfirstlane`): first-lane programs for groups of 4, 32, 33, 64 (wave 32) and 64 (wave 64), wave inference unit checks, and a WARP dispatch case with 32- and 64-thread groups (one and two guest waves) and seven EXEC thresholds, each compared with a CPU reference per guest wave, plus a wrong-lane control (lane 0 instead of the first enabled lane). - `startup_gfx2hlsl_compute_check.py`: a per-lane address read translates with a known wave and is refused without one. - `startup_compute_readfirstlane_check.py`: a thread-id source now translates through the gather; the lane-mask controls still refuse. ## Verification - Linux: `python3 -B tests/startup_gfx2hlsl_wavesync_check.py`, `tests/startup_compute_readfirstlane_check.py --llvm-mc llvm-mc-19`, `tests/startup_gfx2hlsl_compute_check.py` all rc=0. - Linux: the full `tests/startup_*check.py` set differs from main only by the new check (rc=0). - Win2025 WARP dev+release: pending Stage-2. The HLSL with barriers and `InterlockedMin` was not compiled with DXC/FXC here. AI-assisted; commands listed were executed locally.
RavenCurseBurg commented 2026-11-26 10:04:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RavenCurseBurg on 2026-11-26T09:56:14Z.


I will test WaveSync on non-1D thread groups with an incomplete final guest wave. I will compare first/any/indexed gathers with a CPU reference on WARP, UHD 770 and RTX 4060, including an empty mask, missing final-wave lane, and a wrong-lane control.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7188677444 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2791#issuecomment-7188677444). Originally posted by [RavenCurseBurg](https://github.com/RavenCurseBurg) on 2026-11-26T09:56:14Z. --- I will test WaveSync on non-1D thread groups with an incomplete final guest wave. I will compare first/any/indexed gathers with a CPU reference on WARP, UHD 770 and RTX 4060, including an empty mask, missing final-wave lane, and a wrong-lane control.
upstream-sync closed this pull request 2026-11-26 11:02:49 +00:00
tedin7 commented 2026-11-26 11:02:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T10:12:55Z.


Withdrawn: shader translation is moving to the native C++ frontend (see #870), so we are not continuing this change to the Python translator. The branch stays available if any part is useful later. Thank you to everyone who reviewed and tested it.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7189062751 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2791#issuecomment-7189062751). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T10:12:55Z. --- Withdrawn: shader translation is moving to the native C++ frontend (see #870), so we are not continuing this change to the Python translator. The branch stays available if any part is useful later. Thank you to everyone who reviewed and tested it.
RavenCurseBurg commented 2026-11-26 11:02:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by RavenCurseBurg on 2026-11-26T10:31:00Z.


I tested the WaveSync helpers at 2c7d1bb7 with 3D groups (5,4,3) and (7,5,2), guest wave sizes 32 and 64, and loops of 1, 2, 3, 4 and 7 rounds. The output address uses SV_GroupIndex, independently of the helper's linearization. A CPU reference checks four words per thread (first lane, its value, any bit, indexed value) and all remaining sentinel words in the 320-word UAV. The mask for any is independent of the first-lane mask, including a case with no first lane and a true any result.

On WARP, Intel UHD 770 and RTX 4060, each of the 20 original cases matched all 320 words. As a control I compiled a shader that requests a neighboring lane. It matched its independently predicted output in all 20 cases. Compared with the original reference it failed in exactly 17 cases per adapter, with precisely the predicted number of different words (32, 60, 64 or 70). In the other three cases both indices were absent from a 60-thread partial wave64 and both correctly returned zero.

Reproduction script. From that checkout:

python tests/verify_wavesync_group_shape.py <dxc> <offscreen_check> warp

Use hardware with XBOX2WINDOWS_ADAPTER=Intel or NVIDIA for the physical runs. Windows 11 26100.9711, WARP 10.0.26100.9711, Intel 32.0.101.5973, NVIDIA 32.0.15.9579, DXC SM 6.0, D3D12 debug layer unavailable. The probe instantiates the PR's WaveSync helpers directly. AI-assisted with Codex.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7189469617 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2791#issuecomment-7189469617). Originally posted by [RavenCurseBurg](https://github.com/RavenCurseBurg) on 2026-11-26T10:31:00Z. --- I tested the WaveSync helpers at `2c7d1bb7` with 3D groups **(5,4,3)** and **(7,5,2)**, guest wave sizes 32 and 64, and loops of 1, 2, 3, 4 and 7 rounds. The output address uses `SV_GroupIndex`, independently of the helper's linearization. A CPU reference checks four words per thread (first lane, its value, any bit, indexed value) and all remaining sentinel words in the 320-word UAV. The mask for `any` is independent of the first-lane mask, including a case with no first lane and a true any result. On **WARP, Intel UHD 770 and RTX 4060**, each of the 20 original cases matched all 320 words. As a control I compiled a shader that requests a neighboring lane. It matched its independently predicted output in all 20 cases. Compared with the original reference it failed in exactly 17 cases per adapter, with precisely the predicted number of different words (32, 60, 64 or 70). In the other three cases both indices were absent from a 60-thread partial wave64 and both correctly returned zero. [Reproduction script](https://github.com/RavenCurseBurg/Xbox2Windows/blob/94e39cd5db2ec718dec1b0a5570881bd73a710cd/tests/verify_wavesync_group_shape.py). From that checkout: ```text python tests/verify_wavesync_group_shape.py <dxc> <offscreen_check> warp ``` Use `hardware` with `XBOX2WINDOWS_ADAPTER=Intel` or `NVIDIA` for the physical runs. Windows 11 26100.9711, WARP 10.0.26100.9711, Intel 32.0.101.5973, NVIDIA 32.0.15.9579, DXC SM 6.0, D3D12 debug layer unavailable. The probe instantiates the PR's WaveSync helpers directly. AI-assisted with Codex.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
R0Y00/Xbox2Windows!2783
No description provided.