Shaders: v_readlane inside a loop the lanes leave at different iterations reads wrong lanes (wave32 and wave64, every GPU) #225

Open
opened 2026-11-26 03:55:30 +00:00 by github-bot · 2 comments
Owner

@⁠r4tedx on GitHub · 2026-11-24 01:36 UTC · original

What happens

A v_readlane inside a loop that the lanes leave at different iterations is translated wrongly on every GPU tried, both for wave32 and for wave64 programs.

On the console a wave runs a loop while any of its lanes does, and v_readlane reads the lane it names whatever EXEC holds. The translation runs such loops per lane (structurisation turns EXEC into per-lane branches), which loses two things:

  1. A lane that has left the loop is not there for the later iterations' lane reads. WaveReadLaneAt of a lane that is no longer active is undefined; here it reads 0.
  2. A lane that skips the loop never sees the scalar the loop set.

Minimal repro

Hand-assembled GFX10, one wave per group (32 or 64 threads). Lane t runs the loop t times, reading lane L's thread id (L = 20 of 32, 40 of 64) into s9 in each iteration, and then stores t + s9. On the console every lane stores t + L.

s_mov_b32 s9, 0
v_mov_b32 v2, 0
v_cmp_ne_u32 vcc, 0, v0
s_and_saveexec_b64 s[10:11], vcc          ; _b32 for wave32
s_cbranch_execz end
loop:
v_readlane_b32 s9, v0, L
v_add_nc_u32 v2, 1, v2
v_cmp_lt_u32 vcc, v2, v0
s_and_b64 exec, exec, vcc
s_cbranch_execnz loop
end:
s_or_b64 exec, exec, s[10:11]
v_add_nc_u32 v2, s9, v2                    ; store v2 at dword t

A self-contained test (it runs on every hardware adapter, lists each wrong lane, and is #[ignore]d so the suite stays green) is on r4tedx:repro/loop-lane-read, commit a7f34b7, file crates/victory-shader/src/emit/lane_read_loop_tests.rs:

cargo test -p victory-shader lane_read_loop -- --ignored --nocapture

Results on main (d2a4bae)

adapter wave32 (L = 20) wave64 (L = 40)
NVIDIA RTX 5070 12 of 32 lanes wrong no wave64 pipeline (host range 32..32)
AMD Radeon iGPU (RDNA2) 12 of 32 lanes wrong 24 of 64 lanes wrong

The same lanes are wrong on both GPUs: lane 0 stores 0 (it skipped the loop, so s9 stayed 0), and every lane above L stores t (lane L had left the loop when their later iterations read it).

Possible direction

Run such programs as the console does: every lane of the wave through every block, vector instructions guarded by the lane's EXEC bit, and the console's s_cbranch_* taken wave-wide. Lane reads then see lanes outside EXEC, as on the console. This is now #320 (on top of #233's "console flow"): a compute program with v_readlane / v_writelane and a branch its lanes take differently runs that way on every host, and this repro is exact for wave32 and wave64 on the RTX 5070 and the Radeon iGPU with it.

Not verified against console hardware; the expected values follow the ISA's v_readlane and EXEC semantics.

AI-assisted; no game assets were used.

🤖 Generated with Claude Code

**@⁠r4tedx** on GitHub · 2026-11-24 01:36 UTC · [original](https://github.com/Marvisak/VIctory/issues/225) ## What happens A `v_readlane` inside a loop that the lanes leave at different iterations is translated wrongly on every GPU tried, both for wave32 and for wave64 programs. On the console a wave runs a loop while any of its lanes does, and `v_readlane` reads the lane it names whatever EXEC holds. The translation runs such loops per lane (structurisation turns EXEC into per-lane branches), which loses two things: 1. A lane that has left the loop is not there for the later iterations' lane reads. `WaveReadLaneAt` of a lane that is no longer active is undefined; here it reads 0. 2. A lane that skips the loop never sees the scalar the loop set. ## Minimal repro Hand-assembled GFX10, one wave per group (32 or 64 threads). Lane t runs the loop t times, reading lane L's thread id (L = 20 of 32, 40 of 64) into s9 in each iteration, and then stores t + s9. On the console every lane stores t + L. ``` s_mov_b32 s9, 0 v_mov_b32 v2, 0 v_cmp_ne_u32 vcc, 0, v0 s_and_saveexec_b64 s[10:11], vcc ; _b32 for wave32 s_cbranch_execz end loop: v_readlane_b32 s9, v0, L v_add_nc_u32 v2, 1, v2 v_cmp_lt_u32 vcc, v2, v0 s_and_b64 exec, exec, vcc s_cbranch_execnz loop end: s_or_b64 exec, exec, s[10:11] v_add_nc_u32 v2, s9, v2 ; store v2 at dword t ``` A self-contained test (it runs on every hardware adapter, lists each wrong lane, and is `#[ignore]`d so the suite stays green) is on [`r4tedx:repro/loop-lane-read`](https://github.com/r4tedx/VIctory/tree/repro/loop-lane-read), commit a7f34b7, file `crates/victory-shader/src/emit/lane_read_loop_tests.rs`: ``` cargo test -p victory-shader lane_read_loop -- --ignored --nocapture ``` ## Results on main (d2a4bae) | adapter | wave32 (L = 20) | wave64 (L = 40) | |---|---|---| | NVIDIA RTX 5070 | 12 of 32 lanes wrong | no wave64 pipeline (host range 32..32) | | AMD Radeon iGPU (RDNA2) | 12 of 32 lanes wrong | 24 of 64 lanes wrong | The same lanes are wrong on both GPUs: lane 0 stores 0 (it skipped the loop, so s9 stayed 0), and every lane above L stores t (lane L had left the loop when their later iterations read it). ## Possible direction Run such programs as the console does: every lane of the wave through every block, vector instructions guarded by the lane's EXEC bit, and the console's `s_cbranch_*` taken wave-wide. Lane reads then see lanes outside EXEC, as on the console. This is now #320 (on top of #233's "console flow"): a compute program with `v_readlane` / `v_writelane` and a branch its lanes take differently runs that way on every host, and this repro is exact for wave32 and wave64 on the RTX 5070 and the Radeon iGPU with it. Not verified against console hardware; the expected values follow the ISA's `v_readlane` and EXEC semantics. AI-assisted; no game assets were used. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:55:30 +00:00
Author
Owner

@⁠r4tedx on GitHub · 2026-11-24 04:30 UTC · original

Status: the fix is in #320 (head a5ba4a4). A compute program with v_readlane / v_writelane and a branch its lanes take differently is translated in the console flow on every host, and falls back to the per-lane form only where the console flow cannot lower it.

Confirmed by others:

Not shown: a title program whose picture changes because of this. In compiled shaders these lane reads mostly come from SGPR spill slots, which PRs such as #54 and #309 resolve more cheaply, so the bug may be rare in practice.

No game data used. AI-assisted.

**@⁠r4tedx** on GitHub · 2026-11-24 04:30 UTC · [original](https://github.com/Marvisak/VIctory/issues/225#issuecomment-7122076772) Status: the fix is in #320 (head `a5ba4a4`). A compute program with `v_readlane` / `v_writelane` and a branch its lanes take differently is translated in the console flow on every host, and falls back to the per-lane form only where the console flow cannot lower it. Confirmed by others: - The repro of this issue (`lane_reads_in_a_loop_lanes_leave_at_different_iterations`) is [exact for wave32 and wave64 on an RTX 5080 and on an AMD Radeon iGPU](https://git.gtahub.st/github/Marvisak--VIctory/pulls/320). - In the title on an RTX 5070 Ti, [no program that ran on `main` stops with #320](https://git.gtahub.st/github/Marvisak--VIctory/pulls/320). Not shown: a title program whose picture changes because of this. In compiled shaders these lane reads mostly come from SGPR spill slots, which PRs such as #54 and #309 resolve more cheaply, so the bug may be rare in practice. No game data used. AI-assisted.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:45 UTC · original

Automated triage for the maintainers.

Confirmed by reading main: in crates/victory-shader/src/emit/lower.rs, valu_op lowers a v_readlane_b32 of a non-spill value to waveReadLaneAt (read_lane) inside the structured, per-lane control flow, so a lane that has already left the loop (or never entered it) is not active for the read, and a lane that skipped the loop never gets the scalar the loop set. That matches the wrong lanes listed in the report. The open PR #320 addresses it by running such compute programs in the console flow; the repro test is on the reporter's fork, and #320 is where the fix is under review.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 01:45 UTC · [original](https://github.com/Marvisak/VIctory/issues/225#issuecomment-7149915045) <!-- victory-issue-triage v1 verdict=confirmed seen=2026-11-24T04:30:07Z at=2026-11-25T01:45:30Z --> Automated triage for the maintainers. Confirmed by reading `main`: in `crates/victory-shader/src/emit/lower.rs`, `valu_op` lowers a `v_readlane_b32` of a non-spill value to `waveReadLaneAt` (`read_lane`) inside the structured, per-lane control flow, so a lane that has already left the loop (or never entered it) is not active for the read, and a lane that skipped the loop never gets the scalar the loop set. That matches the wrong lanes listed in the report. The open PR #320 addresses it by running such compute programs in the console flow; the repro test is on the reporter's fork, and #320 is where the fix is under review. --- _Generated by [Claude Code](https://claude.ai/code)_
This discussion has been locked. Commenting is limited to contributors.
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory#225
No description provided.