feat: read the lowest enabled lane for v_readfirstlane_b32 of a per-lane VGPR (groups up to 32 threads) #2737

Closed
tedin7 wants to merge 1 commit from upstream-pr/2745 into main
tedin7 commented 2026-11-26 05:30:57 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T05:30:57Z.


Part of #1723

Status: parked as Draft: groundwork for the shared wave model.

What this unblocks: v_readfirstlane_b32 of a VGPR that differs between lanes (for example a load at a per-lane address) in compute groups of at most 32 threads, which was refused with "requires a proven uniform source".
Source: AMD "RDNA 2" Instruction Set Architecture reference guide, VOP1 instruction V_READFIRSTLANE_B32 (first active lane of EXEC); Microsoft Shader Model 6.0 wave intrinsics, WaveReadLaneFirst.

What changed

  • When the source is not proven wave-uniform, the read takes the VGPR of the lowest lane whose EXEC bit is set (lane 0 when EXEC is zero), as the hardware does.
  • The lowest enabled lane is found through groupshared memory: every thread stores its value in x2w_rfl_val[lane], enabled threads InterlockedMin their lane index into x2w_rfl_min, and all threads read the value of that lane. Three GroupMemoryBarrierWithGroupSync() calls order init, min and read. The result does not depend on how the host splits the group into waves (WARP has 4-lane waves, GPUs 32 or 64).
  • Proven-uniform sources keep the existing path unchanged (no groupshared use).

Why it is correct

  • A group of at most 32 threads is one guest wave of either size and thread t is lane t (SV_GroupThreadID flattened); the model keeps exec & 1 per thread.
  • Every thread must reach the barriers. The translator therefore refuses the whole program when any branch is not proved the same for every lane (uniform_cf is false at the end of the pass), including divergent back edges, s_cbranch_execz and vcc branches.

What stays refused

  • Groups of more than 32 threads (the guest wave size is not known, so one thread group can be two waves): refused with the existing message.
  • Programs with any branch not proved uniform.
  • Non-compute stages.

Tests

  • startup_compute_waterfall.py (run by startup_compute_readfirstlane): a source-built 32-thread program that enables only the lanes above a root constant and reads v5 of the lowest enabled lane; controls for groups of 4 and 32 (translated) and 33 and 64 (refused). The WARP dispatch case runs a 32-thread group (8 host waves on WARP) with six active-lane thresholds (including no enabled lane) and a wrong-lane control (lane 0 instead of the first enabled lane).
  • startup_gfx2hlsl_compute_check.py: the per-lane-address case now translates (was a refusal); a 64-thread group stays refused.
  • startup_compute_readfirstlane_check.py: the lane-mask counter refusal now names the lane mask.

Verification

  • Linux: python3 -B tests/startup_compute_readfirstlane_check.py --llvm-mc llvm-mc-19 and tests/startup_gfx2hlsl_compute_check.py pass.
  • Linux: the full tests/startup_*check.py set has the same failing files as main (those need a Windows toolchain); no new failure.
  • Win2025 WARP dev+release: pending Stage-2. The barriers of the generated HLSL (cs_6_0, DXC) have not been compiled here.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6620213495 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2745). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T05:30:57Z. --- Part of #1723 Status: parked as Draft: groundwork for the shared wave model. What this unblocks: `v_readfirstlane_b32` of a VGPR that differs between lanes (for example a load at a per-lane address) in compute groups of at most 32 threads, which was refused with "requires a proven uniform source". Source: AMD "RDNA 2" Instruction Set Architecture reference guide, VOP1 instruction V_READFIRSTLANE_B32 (first active lane of EXEC); Microsoft Shader Model 6.0 wave intrinsics, `WaveReadLaneFirst`. ## What changed - When the source is not proven wave-uniform, the read takes the VGPR of the lowest lane whose EXEC bit is set (lane 0 when EXEC is zero), as the hardware does. - The lowest enabled lane is found through groupshared memory: every thread stores its value in `x2w_rfl_val[lane]`, enabled threads `InterlockedMin` their lane index into `x2w_rfl_min`, and all threads read the value of that lane. Three `GroupMemoryBarrierWithGroupSync()` calls order init, min and read. The result does not depend on how the host splits the group into waves (WARP has 4-lane waves, GPUs 32 or 64). - Proven-uniform sources keep the existing path unchanged (no groupshared use). ## Why it is correct - A group of at most 32 threads is one guest wave of either size and thread `t` is lane `t` (`SV_GroupThreadID` flattened); the model keeps `exec & 1` per thread. - Every thread must reach the barriers. The translator therefore refuses the whole program when any branch is not proved the same for every lane (`uniform_cf` is false at the end of the pass), including divergent back edges, `s_cbranch_execz` and `vcc` branches. ## What stays refused - Groups of more than 32 threads (the guest wave size is not known, so one thread group can be two waves): refused with the existing message. - Programs with any branch not proved uniform. - Non-compute stages. ## Tests - `startup_compute_waterfall.py` (run by `startup_compute_readfirstlane`): a source-built 32-thread program that enables only the lanes above a root constant and reads `v5` of the lowest enabled lane; controls for groups of 4 and 32 (translated) and 33 and 64 (refused). The WARP dispatch case runs a 32-thread group (8 host waves on WARP) with six active-lane thresholds (including no enabled lane) and a wrong-lane control (lane 0 instead of the first enabled lane). - `startup_gfx2hlsl_compute_check.py`: the per-lane-address case now translates (was a refusal); a 64-thread group stays refused. - `startup_compute_readfirstlane_check.py`: the lane-mask counter refusal now names the lane mask. ## Verification - Linux: `python3 -B tests/startup_compute_readfirstlane_check.py --llvm-mc llvm-mc-19` and `tests/startup_gfx2hlsl_compute_check.py` pass. - Linux: the full `tests/startup_*check.py` set has the same failing files as main (those need a Windows toolchain); no new failure. - Win2025 WARP dev+release: pending Stage-2. The barriers of the generated HLSL (cs_6_0, DXC) have not been compiled here. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-26 11:02:42 +00:00
tedin7 commented 2026-11-26 11:02:58 +00:00 (Migrated from github.com)

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


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:7189053155 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2745#issuecomment-7189053155). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T10:12:30Z. --- 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.

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!2737
No description provided.