Shaders: scalars written in lane-divergent branches reach every lane after the join #405

Open
github-bot wants to merge 3 commits from github-bot/gh-405 into main AGit
Owner

@⁠simbaui4life on GitHub · 2026-11-27 00:20 UTC · original
simbaui4life:fix/scalars-after-lane-branches → main · +2115 −5 · 5 files · 3 commits · open

Builds on #403 (the oracle tests): its commit is included here, and only the last commit is new.

Overlaps: #81 (Pavl3000) changes the same widen() and branches_around; whichever lands second needs a rebase, and I can rebase this one onto #81. #286 (an SGPR after a scalar loop, per half) and Belobug/VIctory-Belobug-AMD#352 (a scalar counted in a loop that lanes leave at different iterations) are the same family of fault in loops; this PR covers lane-divergent branches only.

Cause: on the console a wave runs both sides of a lane-divergent branch, with EXEC narrowed. An SGPR or SCC written inside the branch is then one value for the whole wave. Lowered as per-lane control flow, that write happens only on the lanes that took the branch. After the join, the "scalar" therefore differs from lane to lane: lanes that skipped the branch keep the old value. Minimal example from the fuzzer (wave32 and wave64):

v_cmp_gt_u32  vcc, s22, v3
s_and_saveexec_b32 s40, vcc_lo
s_mov_b32     s27, s19          ; only in the branch
s_or_b32      exec_lo, exec_lo, s40
; s27 stored from every lane: console = s19 everywhere, here = s19 only where v3 < s22

What this does

  • lower::scalar_splits finds the lane branches whose region writes a scalar (SGPR or SCC; EXEC and lane masks excluded) that a block after the join reads as a phi with different origins on its paths. It follows s_mov copies, so a copy of an older value counts too.
  • emit(), compute programs only, takes exactly those branches wave-wide through the existing widen() / structure_wide, as the console does. Everything else keeps its per-lane form. widen() now takes them as a seed and adds any that scalar_splits still finds after structure_wide.
  • A console wave64 on 32-lane host waves needs group barriers for this, so there it applies only with VICTORY_WAVE_WIDE=1, as widen() already did. Without it, the per-lane form stays.
  • VICTORY_SCALAR_SPLITS=0 turns it off. It is in the shader cache key (victory-gpu/src/shader_cache.rs) beside the other switches that pick a form. VICTORY_WAVE_WHY prints what was taken wave-wide.
  • VICTORY_WAVE_WIDE is read through lower::wave_wide_on(), so the test can turn it on for its own thread instead of changing the environment that other tests read.
  • TRANSLATOR_VERSION 1 → 2. #404, #353 and #354 also go 1 → 2, so whichever lands later needs the next number.

Tested (RTX 4070 Ti, driver 621.01, Windows 11 26200)

  • oracle_scalars_written_in_lane_branches_reach_every_lane covers 9 shapes:

    • then writes, else writes, both write
    • a copy of an older scalar
    • nested, read after the outer join
    • SCC set in the branch
    • v_readfirstlane and v_readlane in the branch
    • a branch in a uniform loop

    Each shape runs for all, some and no lanes, split on the thread id and on a hash, with masks as data or not, in wave32 and wave64. Without the change it fails at 26 places; with it, it passes.

  • This branch: clippy as in CI, --all-features as well, and cargo test -p victory-shader (305 passed, 0 failed).

  • cargo clippy -p victory-gpu --all-targets -- -D warnings reports 4 errors, all in files this PR does not touch (list.rs, watch.rs, host/panel.rs, host/place.rs), as on main.

  • Fuzzer with VICTORY_WAVE_WIDE=1, seed 2, 300 programs per wave size: differing programs went from 50 to 13 (wave32) and 40 to 15 (wave64). With seed 4, 400 programs, none of the rest are this class; they are the open VCC findings listed in #403.

Not tested:

  • In the game (no game data here): how many of the title's programs this touches, and what changes on screen.
  • AMD hardware.
  • The cost of the wave-wide branches: each takes two group barriers when a wave64 runs on 32-lane waves.

No game data in the commits.

🤖 Generated with Claude Code

**@⁠simbaui4life** on GitHub · 2026-11-27 00:20 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/405) `simbaui4life:fix/scalars-after-lane-branches` → `main` · +2115 −5 · 5 files · 3 commits · open **Builds on #403** (the oracle tests): its commit is included here, and only the last commit is new. **Overlaps:** #81 (Pavl3000) changes the same `widen()` and `branches_around`; whichever lands second needs a rebase, and I can rebase this one onto #81. #286 (an SGPR after a scalar loop, per half) and Belobug/VIctory-Belobug-AMD#352 (a scalar counted in a loop that lanes leave at different iterations) are the same family of fault in loops; this PR covers lane-divergent branches only. **Cause:** on the console a wave runs both sides of a lane-divergent branch, with EXEC narrowed. An SGPR or SCC written inside the branch is then one value for the whole wave. Lowered as per-lane control flow, that write happens only on the lanes that took the branch. After the join, the "scalar" therefore differs from lane to lane: lanes that skipped the branch keep the old value. Minimal example from the fuzzer (wave32 and wave64): ``` v_cmp_gt_u32 vcc, s22, v3 s_and_saveexec_b32 s40, vcc_lo s_mov_b32 s27, s19 ; only in the branch s_or_b32 exec_lo, exec_lo, s40 ; s27 stored from every lane: console = s19 everywhere, here = s19 only where v3 < s22 ``` **What this does** - `lower::scalar_splits` finds the lane branches whose region writes a scalar (SGPR or SCC; EXEC and lane masks excluded) that a block after the join reads as a phi with different origins on its paths. It follows `s_mov` copies, so a copy of an older value counts too. - `emit()`, compute programs only, takes exactly those branches wave-wide through the existing `widen()` / `structure_wide`, as the console does. Everything else keeps its per-lane form. `widen()` now takes them as a seed and adds any that `scalar_splits` still finds after `structure_wide`. - A console wave64 on 32-lane host waves needs group barriers for this, so there it applies only with `VICTORY_WAVE_WIDE=1`, as `widen()` already did. Without it, the per-lane form stays. - `VICTORY_SCALAR_SPLITS=0` turns it off. It is in the shader cache key (`victory-gpu/src/shader_cache.rs`) beside the other switches that pick a form. `VICTORY_WAVE_WHY` prints what was taken wave-wide. - `VICTORY_WAVE_WIDE` is read through `lower::wave_wide_on()`, so the test can turn it on for its own thread instead of changing the environment that other tests read. - `TRANSLATOR_VERSION` 1 → 2. #404, #353 and #354 also go 1 → 2, so whichever lands later needs the next number. **Tested** (RTX 4070 Ti, driver 621.01, Windows 11 26200) - `oracle_scalars_written_in_lane_branches_reach_every_lane` covers 9 shapes: - then writes, else writes, both write - a copy of an older scalar - nested, read after the outer join - SCC set in the branch - `v_readfirstlane` and `v_readlane` in the branch - a branch in a uniform loop Each shape runs for all, some and no lanes, split on the thread id and on a hash, with masks as data or not, in wave32 and wave64. Without the change it fails at 26 places; with it, it passes. - This branch: clippy as in CI, `--all-features` as well, and `cargo test -p victory-shader` (305 passed, 0 failed). - `cargo clippy -p victory-gpu --all-targets -- -D warnings` reports 4 errors, all in files this PR does not touch (`list.rs`, `watch.rs`, `host/panel.rs`, `host/place.rs`), as on main. - Fuzzer with `VICTORY_WAVE_WIDE=1`, seed 2, 300 programs per wave size: differing programs went from 50 to 13 (wave32) and 40 to 15 (wave64). With seed 4, 400 programs, none of the rest are this class; they are the open VCC findings listed in #403. **Not tested:** - In the game (no game data here): how many of the title's programs this touches, and what changes on screen. - AMD hardware. - The cost of the wave-wide branches: each takes two group barriers when a wave64 runs on 32-lane waves. No game data in the commits. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Random compute programs (integer VALU and SALU arithmetic, lane compares,
lane-divergent if/else under EXEC, uniform ifs and loops on SCC, cross-lane
reads and writes) are encoded to GFX10 machine code, translated by the
production path (spill::build, structure, resource::track, io::raise,
emit_with_retries), run on the host GPU and compared lane by lane with an
interpreter that shares nothing with the translator (not the decoder, the
IR or the lowering). The encoder is checked against the decoder's
instruction names, so a mismatch means the translation, not the encoding.
Failing programs are minimized and printed as a listing.

Wave64 runs on a host of 32-lane waves (TEST_HOST_WAVE), once with exact
translations only (VICTORY_WAVE_LOCAL=idiom) and once as the default.

Default tests: the encoder round trip and the interpreter on known cases
(no GPU needed). The fuzzer is #[ignore]d:
  cargo test -p victory-shader --lib oracle -- --ignored --nocapture --test-threads=1
with VICTORY_ORACLE_RUNS (200), VICTORY_ORACLE_SEED (1), VICTORY_ORACLE_OUT
(report file) and VICTORY_ORACLE_AVOID (leave out known bug classes).
Without an SM 6.6 hardware device it skips.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On the console a wave runs both sides of a lane-divergent branch once, with
EXEC narrowed; an SGPR (or SCC) written inside is one value for the whole
wave afterwards. Lowered as per-lane control flow, the write only happens on
the lanes that took the branch, so after the join the "scalar" differs per
lane (lanes that skipped the branch keep the old value). Example:

  v_cmp_gt_u32 vcc, s0, v3 ; s_and_saveexec ... ; s_mov_b32 s27, s19 ;
  s_or_b32 exec ... ; (read s27 in every lane)

lower::scalar_splits finds the lane branches whose region writes a scalar
that a block after the join reads with different values on the paths
(following s_mov copies), and emit() takes exactly those branches
wave-wide through the existing widen() (structure_wide), as the console
does. Everything else keeps its per-lane form. widen() takes those
branches as a seed and adds any that structure_wide's result still has.

Compute programs only. A console wave64 on a host of 32-lane waves needs
group barriers for this, so there it applies only with VICTORY_WAVE_WIDE=1
(as widen() already did). VICTORY_SCALAR_SPLITS=0 turns it off; it is part of the shader cache key
(victory-gpu shader_cache.rs) like the other switches that pick a form.
VICTORY_WAVE_WIDE is read through lower::wave_wide_on(), which tests can
turn on per thread instead of changing the environment.

Test: oracle_scalars_written_in_lane_branches_reach_every_lane, 9 shapes
(then/else/both write, a copy of an older scalar, nested, SCC, readfirstlane
and readlane in the branch, a branch in a uniform loop), for all, some and
no lanes on the thread id and on a hash, masks as data or not, wave32 and
wave64. It fails at 26 places without the change. It needs an SM 6.6
hardware device and skips otherwise.

Not tested in the game: how often such shaders occur and what changes on
screen is unknown.

TRANSLATOR_VERSION 1 -> 2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 00:56:45 +00:00
Author
Owner

@⁠UwUCammie on GitHub · 2026-11-27 00:46 UTC · original

I noticed one path where VICTORY_SCALAR_SPLITS=0 still takes the new scalar branches wave-wide. It skips the first probe in emit, but the existing wave64 and lane-mask fallbacks call widen with an empty seed. The loop in widen still calls scalar_splits and extends wide in both cases.

For an off/on comparison, could you pass the same switch into widen and guard the scalar-only expansion with it? The existing expansion from branches_around can stay enabled. Call site · Expansion

**@⁠UwUCammie** on GitHub · 2026-11-27 00:46 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/405#issuecomment-7206746134) I noticed one path where `VICTORY_SCALAR_SPLITS=0` still takes the new scalar branches wave-wide. It skips the first probe in `emit`, but the existing wave64 and lane-mask fallbacks call `widen` with an empty seed. The loop in `widen` still calls `scalar_splits` and extends `wide` in both cases. For an off/on comparison, could you pass the same switch into `widen` and guard the scalar-only expansion with it? The existing expansion from `branches_around` can stay enabled. [Call site](https://github.com/avatarmanz/VIctory-7900XTX/blob/9b6280b1557d3a5019c4b08db37de925fb2c7eef/crates/victory-shader/src/emit/mod.rs#L603-L607) · [Expansion](https://github.com/avatarmanz/VIctory-7900XTX/blob/9b6280b1557d3a5019c4b08db37de925fb2c7eef/crates/victory-shader/src/emit/mod.rs#L721-L731)
widen() took scalar splits in its loop for every caller, so the fallbacks (the wave64
form and the lane-mask retry), which start from no branch, still took those branches
wave-wide with the switch off. widen() now takes the switch (`scalar_splits`); the
first probe in emit() passes true, the fallbacks pass lower::scalar_splits_on(). The
expansion from branches_around is unchanged.

Found by UwUCammie in the review of #405. Test: oracle_tests::
widen_takes_scalar_splits_only_when_asked (translation only, no GPU; it fails with
the expansion unconditional).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Owner

@⁠simbaui4life on GitHub · 2026-11-27 15:26 UTC · original

Good catch, thanks. widen() now takes the switch (abf5c1f):

  • the first probe in emit() passes true;
  • the wave64 form and the lane-mask retry pass lower::scalar_splits_on().

So VICTORY_SCALAR_SPLITS=0 reaches every caller, and the branches_around expansion is unchanged.

New test: widen_takes_scalar_splits_only_when_asked. It runs translation only, so it needs no GPU and runs in CI. With an empty seed, it checks that the switch decides whether the scalar branch is taken wave-wide, and it fails if the expansion is made unconditional again.

This branch: cargo test -p victory-shader 306 passed, clippy -D warnings clean, also with --all-features (RTX 4070 Ti).

**@⁠simbaui4life** on GitHub · 2026-11-27 15:26 UTC · [original](https://github.com/avatarmanz/VIctory-7900XTX/pull/405#issuecomment-7222062211) Good catch, thanks. `widen()` now takes the switch (abf5c1f): - the first probe in `emit()` passes `true`; - the wave64 form and the lane-mask retry pass `lower::scalar_splits_on()`. So `VICTORY_SCALAR_SPLITS=0` reaches every caller, and the `branches_around` expansion is unchanged. New test: `widen_takes_scalar_splits_only_when_asked`. It runs translation only, so it needs no GPU and runs in CI. With an empty seed, it checks that the switch decides whether the scalar branch is taken wave-wide, and it fails if the expansion is made unconditional again. This branch: `cargo test -p victory-shader` 306 passed, clippy `-D warnings` clean, also with `--all-features` (RTX 4070 Ti).
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/405/head:github-bot/gh-405'
git switch 'github-bot/gh-405'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-405'
git switch 'github-bot/gh-405'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-405'
git switch 'github-bot/gh-405'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-405'
git switch 'main'
git merge --squash 'github-bot/gh-405'
git switch 'main'
git merge --ff-only 'github-bot/gh-405'
git switch 'main'
git merge 'github-bot/gh-405'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.