Shaders: a scalar a per-lane branch writes is the wave's after the join (#3562) #3593

Open
github-bot wants to merge 1 commit from github-bot/gh-3593 into main AGit
Owner

@⁠ColorMeBoom on GitHub · 2026-11-27 19:41 UTC · original
ColorMeBoom:fix/3562-wave-scalars → main · +150 −0 · 3 files · 1 commits · open

Fixes #3562.

What was wrong

On the console a scalar register is one value per wave. Where some lanes of a wave take a branch and the rest skip it (s_and_saveexec_b64 + s_cbranch_execz), the wave runs the branch, and every lane reads what the branch left after the join (s_or_b64 exec). The per-lane form gives each lane the value along its own edge, so the lanes that skipped the branch kept the old value. A v_readlane, a whole-wave reduction's result (s_or_saveexec_b64 -1 … v_readlane_b32 sN, v, 0) or any scalar written under a lane branch was lost to them.

The change (emit/lower/joins.rs, hooked in after a block's phis)

At a per-lane join (!uniform_join(b)), each SGPR phi takes, in every lane, the value of a lane that came from the latest predecessor (in code order) that any lane came from. That's the path the console's wave ran last. Per lane: the predecessor's rank (an i32 phi), WaveActiveMax of it, WaveActiveMin of the lane index among the lanes with that rank, then WaveReadLaneAt for each scalar phi.

It applies only:

  • with the console's lanes (console_lanes: WaveSize at the console's width, so one host wave holds one console wave). Mesh programs and programs translated without console lanes are unchanged;
  • not at loop headers, and not when a loop lies between the branch and the join. There the per-lane form's scalars are each lane's own (a round count), so no single lane holds the console's value. break_loop got worse without this guard, and is unchanged with it;
  • not to masks (st.masks / st.highs) or EXEC.

lower.rs grows by 789 bytes (521,352, under the policy limit).

Measured

@⁠Lyrnc's 151 hand-written GFX10 wave64 cases (gist), each compared lane by lane with the same code run natively on a gfx1036. RX 5700 XT (RDNA1), native WaveSize(64):

case kind main exact this PR exact
red_branch (reduction in a branch) 8 / 40 40 / 40
red_earlyout 1 / 6 6 / 6
red_nested 0 / 6 6 / 6
readlane_branch 4 / 20 10 / 20
everything else unchanged unchanged
all 59 / 151 108 / 151

No case is worse, and wrong output words go from 5,025 to 2,712. Still open:

  • firstlane_branch: that program has no console lanes, so the host's wave may group lanes differently and the change leaves it alone;
  • the other readlane_branch cases: they also differ inside the branch (a lane read of a lane outside EXEC), which this change doesn't cover;
  • the loop cases.

Test

synthetic_tests::a_scalar_a_lane_branch_writes_is_the_waves_after_the_join (through run_permute_cs, wave64): s38 = 0x5555, lanes 40-63 run v_readlane_b32 s38, v0, 45, then every lane stores s38.

  • With the fix (RX 5700 XT): all 64 lanes store 45. With no lane in the branch, all 64 keep 0x5555.
  • On main: lanes 0-39 store 0x5555.
  • WARP refuses WaveSize(64) there and is skipped, as in the other wave64 permute tests.

Checks

  • cargo test -p victory-shader --lib --features host, RX 5700 XT: 303 passed, 0 failed, 3 ignored.
  • VICTORY_WARP=1: 299 passed, 4 failed. Those are the same 4 synthetic_tests that fail on main alone with this machine's system WARP.
  • cargo clippy -p victory-shader --all-targets --all-features -- -D warnings: clean.
  • cargo xtask policy: ok.
  • No new rustfmt sites (the crate's existing 127 are unchanged).

Not tested: NVIDIA. On main this code path needs WaveSize(64), which 32-lane hosts refuse; on them it matters through #2633-style halves. Also not tested: other AMD generations, and the title.

AI use: Claude Code (Claude Opus 5.5).

🤖 Generated with Claude Code

**@⁠ColorMeBoom** on GitHub · 2026-11-27 19:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/3593) `ColorMeBoom:fix/3562-wave-scalars` → `main` · +150 −0 · 3 files · 1 commits · open Fixes #3562. ## What was wrong On the console a scalar register is one value per wave. Where some lanes of a wave take a branch and the rest skip it (`s_and_saveexec_b64` + `s_cbranch_execz`), the wave runs the branch, and every lane reads what the branch left after the join (`s_or_b64 exec`). The per-lane form gives each lane the value along its own edge, so the lanes that skipped the branch kept the old value. A `v_readlane`, a whole-wave reduction's result (`s_or_saveexec_b64 -1` … `v_readlane_b32 sN, v, 0`) or any scalar written under a lane branch was lost to them. ## The change (`emit/lower/joins.rs`, hooked in after a block's phis) At a per-lane join (`!uniform_join(b)`), each SGPR phi takes, in every lane, the value of a lane that came from the latest predecessor (in code order) that any lane came from. That's the path the console's wave ran last. Per lane: the predecessor's rank (an `i32` phi), `WaveActiveMax` of it, `WaveActiveMin` of the lane index among the lanes with that rank, then `WaveReadLaneAt` for each scalar phi. It applies only: - with the console's lanes (`console_lanes`: `WaveSize` at the console's width, so one host wave holds one console wave). Mesh programs and programs translated without console lanes are unchanged; - not at loop headers, and not when a loop lies between the branch and the join. There the per-lane form's scalars are each lane's own (a round count), so no single lane holds the console's value. `break_loop` got worse without this guard, and is unchanged with it; - not to masks (`st.masks` / `st.highs`) or EXEC. `lower.rs` grows by 789 bytes (521,352, under the policy limit). ## Measured @⁠Lyrnc's 151 hand-written GFX10 wave64 cases ([gist](https://gist.github.com/Lyrnc/00c6589f26930d5d07c077f8c4a30131)), each compared lane by lane with the same code run natively on a gfx1036. RX 5700 XT (RDNA1), native `WaveSize(64)`: | case kind | `main` exact | this PR exact | | --- | --- | --- | | `red_branch` (reduction in a branch) | 8 / 40 | **40 / 40** | | `red_earlyout` | 1 / 6 | **6 / 6** | | `red_nested` | 0 / 6 | **6 / 6** | | `readlane_branch` | 4 / 20 | **10 / 20** | | everything else | unchanged | unchanged | | **all** | **59 / 151** | **108 / 151** | No case is worse, and wrong output words go from 5,025 to 2,712. Still open: - `firstlane_branch`: that program has no console lanes, so the host's wave may group lanes differently and the change leaves it alone; - the other `readlane_branch` cases: they also differ inside the branch (a lane read of a lane outside EXEC), which this change doesn't cover; - the loop cases. ## Test `synthetic_tests::a_scalar_a_lane_branch_writes_is_the_waves_after_the_join` (through `run_permute_cs`, wave64): `s38 = 0x5555`, lanes 40-63 run `v_readlane_b32 s38, v0, 45`, then every lane stores `s38`. - With the fix (RX 5700 XT): all 64 lanes store 45. With no lane in the branch, all 64 keep `0x5555`. - On `main`: lanes 0-39 store `0x5555`. - WARP refuses `WaveSize(64)` there and is skipped, as in the other wave64 permute tests. ## Checks - `cargo test -p victory-shader --lib --features host`, RX 5700 XT: **303 passed, 0 failed, 3 ignored**. - `VICTORY_WARP=1`: 299 passed, 4 failed. Those are the same 4 `synthetic_tests` that fail on `main` alone with this machine's system WARP. - `cargo clippy -p victory-shader --all-targets --all-features -- -D warnings`: clean. - `cargo xtask policy`: ok. - No new rustfmt sites (the crate's existing 127 are unchanged). Not tested: NVIDIA. On `main` this code path needs `WaveSize(64)`, which 32-lane hosts refuse; on them it matters through #2633-style halves. Also not tested: other AMD generations, and the title. AI use: Claude Code (Claude Opus 5.5). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
On the console a scalar register is one value per wave: where some lanes
take a branch and the rest skip it, the wave runs the branch and every lane
reads what it left after the join. The per-lane form gives each lane the
value along its own edge, so the lanes that skipped a branch kept the old
value: a v_readlane, a whole-wave reduction's result or any scalar written
under a lane branch was lost to them.

At a per-lane join (lanes arrive along different edges; not a loop header,
and no loop between the branch and the join, where the per-lane form's
scalars are each lane's own), each SGPR phi now takes in every lane the
value of a lane that came from the latest predecessor in code order any lane
came from: the path the console's wave ran last. Only with the console's
lanes (WaveSize at the console's width, so one host wave is one console
wave); mesh programs and programs without console lanes are unchanged. The
code is in lower/joins.rs (lower.rs stays under the policy's size limit).

Lyrnc's 151 hand-written GFX10 wave64 cases against the gfx1036 oracle, RX
5700 XT, native WaveSize(64): 59 -> 108 exact, none worse; every
whole-wave reduction in a branch (red_branch 40/40, red_earlyout 6/6,
red_nested 6/6) and 10 of 20 v_readlane cases are now exact.

Test: synthetic_tests::a_scalar_a_lane_branch_writes_is_the_waves_after_the_join
(lanes 40-63 run v_readlane_b32 s38, v0, 45; every lane stores s38). It
passes on the RX 5700 XT; on main lanes 0-39 store 0x5555.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 19:46:57 +00:00
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3593/head:github-bot/gh-3593'
git switch 'github-bot/gh-3593'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3593'
git switch 'github-bot/gh-3593'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3593'
git switch 'github-bot/gh-3593'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3593'
git switch 'main'
git merge --squash 'github-bot/gh-3593'
git switch 'main'
git merge --ff-only 'github-bot/gh-3593'
git switch 'main'
git merge 'github-bot/gh-3593'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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!3593
No description provided.