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

Open
simbaui4life wants to merge 3 commits from upstream-pr/405 into main
simbaui4life commented 2026-11-27 00:20:03 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by simbaui4life on 2026-11-27T00:20:03Z.


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

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:issue:5519024424 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/405). Originally posted by [simbaui4life](https://github.com/simbaui4life) on 2026-11-27T00:20:03Z. --- **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)
UwUCammie commented 2026-11-27 00:46:49 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by UwUCammie on 2026-11-27T00:46:49Z.


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

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7206746134 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/405#issuecomment-7206746134). Originally posted by [UwUCammie](https://github.com/UwUCammie) on 2026-11-27T00:46:49Z. --- 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)
simbaui4life commented 2026-11-27 15:26:33 +00:00 (Migrated from github.com)

Imported from avatarmanz/VIctory-7900XTX. Originally posted by simbaui4life on 2026-11-27T15:26:33Z.


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).

<!-- greyfinch-sync:avatarmanz/VIctory-7900XTX:comment:7222062211 --> Imported from [avatarmanz/VIctory-7900XTX](https://github.com/avatarmanz/VIctory-7900XTX/pull/405#issuecomment-7222062211). Originally posted by [simbaui4life](https://github.com/simbaui4life) on 2026-11-27T15:26:33Z. --- 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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/405:upstream-pr/405'
git switch 'upstream-pr/405'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/405'
git switch 'upstream-pr/405'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/405'
git switch 'upstream-pr/405'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/405'
git switch 'main'
git merge --squash 'upstream-pr/405'
git switch 'main'
git merge --ff-only 'upstream-pr/405'
git switch 'main'
git merge 'upstream-pr/405'
git push origin 'main'
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
avatarmanz/VIctory-7900XTX!405
No description provided.