Shaders: scalars written in lane-divergent branches reach every lane after the join (port of avatarmanz#405) #73

Closed
github-bot wants to merge 2 commits from github-bot/gh-73 into main AGit
Owner

@⁠simbaui4life on GitHub · 2026-11-27 16:14 UTC · original
fix/scalars-after-lane-branches → main · +2121 −3 · 4 files · 2 commits · closed without merging

A port of avatarmanz/VIctory-7900XTX#405, review fix included. It builds on #70 (the oracle tests): that commit is included, and only the last commit is new.

Cause: on the console a wave runs a lane-divergent branch once, with EXEC narrowed. An SGPR or SCC written inside the branch is one value for the whole wave. Lowered as per-lane control flow, the write happens only on the lanes that took the branch, so after the join the "scalar" differs from lane to lane. Example from the fuzzer:

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 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 that a block after the join reads with different origins on its paths. It follows s_mov copies.
  • emit(), for compute only, takes exactly those branches wave-wide through this fork's widen().
    • 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.
    • Everything else keeps its per-lane form.
  • widen() takes one new parameter, scalar_splits: Option<&[usize]>, which keeps it at seven arguments for clippy.
    • Some(branches) seeds those branches and adds new scalar splits each round.
    • None (VICTORY_SCALAR_SPLITS=0) does neither, in every caller, the fallbacks included. That is the review fix of avatarmanz#405.
    • The fork's own parameters (active-lane loops, groups of several console waves) are unchanged.
  • VICTORY_SCALAR_SPLITS is added to the switch list in shader_cache.rs.
  • VICTORY_WAVE_WIDE is read through lower::wave_wide_on(), so the test can switch it per thread.

Tested (RTX 4070 Ti, driver 621.01)

  • oracle_scalars_written_in_lane_branches_reach_every_lane: 9 shapes, for all, some or no lanes, wave32 and wave64. It fails at 26 places without the change.
  • widen_takes_scalar_splits_only_when_asked (translation only, runs in CI): it fails if the expansion is unconditional.
  • Base 288 passed; this branch 292 passed. Together with the s_min/s_max and lane PR: 293 passed. Clippy -D warnings is clean.

Not tested:

  • in the game (no game data here): how often such shaders occur and what changes on screen;
  • the cost of the wave-wide branches.

No game data in the commits.

🤖 Generated with Claude Code

**@⁠simbaui4life** on GitHub · 2026-11-27 16:14 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/73) `fix/scalars-after-lane-branches` → `main` · +2121 −3 · 4 files · 2 commits · closed without merging A port of avatarmanz/VIctory-7900XTX#405, review fix included. It **builds on #70** (the oracle tests): that commit is included, and only the last commit is new. **Cause:** on the console a wave runs a lane-divergent branch once, with EXEC narrowed. An SGPR or SCC written inside the branch is one value for the whole wave. Lowered as per-lane control flow, the write happens only on the lanes that took the branch, so after the join the "scalar" differs from lane to lane. Example from the fuzzer: ``` 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 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 that a block after the join reads with different origins on its paths. It follows `s_mov` copies. - `emit()`, for compute only, takes exactly those branches wave-wide through this fork's `widen()`. - 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. - Everything else keeps its per-lane form. - **`widen()` takes one new parameter,** `scalar_splits: Option<&[usize]>`, which keeps it at seven arguments for clippy. - `Some(branches)` seeds those branches and adds new scalar splits each round. - `None` (`VICTORY_SCALAR_SPLITS=0`) does neither, in every caller, the fallbacks included. That is the review fix of avatarmanz#405. - The fork's own parameters (active-lane loops, groups of several console waves) are unchanged. - `VICTORY_SCALAR_SPLITS` is added to the switch list in `shader_cache.rs`. - `VICTORY_WAVE_WIDE` is read through `lower::wave_wide_on()`, so the test can switch it per thread. **Tested** (RTX 4070 Ti, driver 621.01) - `oracle_scalars_written_in_lane_branches_reach_every_lane`: 9 shapes, for all, some or no lanes, wave32 and wave64. It fails at 26 places without the change. - `widen_takes_scalar_splits_only_when_asked` (translation only, runs in CI): it fails if the expansion is unconditional. - Base 288 passed; this branch 292 passed. Together with the s_min/s_max and lane PR: 293 passed. Clippy `-D warnings` is clean. **Not tested:** - in the game (no game data here): how often such shaders occur and what changes on screen; - the cost of the wave-wide branches. 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.

Ported from avatarmanz/VIctory-7900XTX#403.

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.

Here widen() also takes `scalar_splits`: the first probe passes true, the
fallbacks lower::scalar_splits_on(), so VICTORY_SCALAR_SPLITS=0 reaches every caller
(the review fix of #405; test widen_takes_scalar_splits_only_when_asked). This fork's
widen() keeps its own parameters (active-lane loops, groups of several console waves).

Ported from avatarmanz/VIctory-7900XTX#405. No version bump here: the shader cache is keyed by the build.

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

@⁠Dukestar69 on GitHub · 2026-11-27 16:29 UTC · original · approved these changes

LGTM — verified on Windows (RTX 5090):

  • cargo test -p victory-shader --lib oracle_tests:: passed (4/4, including oracle_scalars_written_in_lane_branches_reach_every_lane)
  • cargo test -p victory-shader --lib emit::tests:: passed (21/21)
  • cargo clippy -p victory-shader --all-targets -- -D warnings passed with 0 warnings
  • cargo xtask policy passed (280 files ok)
  • Note: this branch includes commit 62eed99 from #70.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-27 16:29 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/73#pullrequestreview-6091000941) · **approved these changes** LGTM — verified on Windows (RTX 5090): - `cargo test -p victory-shader --lib oracle_tests::` passed (4/4, including `oracle_scalars_written_in_lane_branches_reach_every_lane`) - `cargo test -p victory-shader --lib emit::tests::` passed (21/21) - `cargo clippy -p victory-shader --all-targets -- -D warnings` passed with 0 warnings - `cargo xtask policy` passed (`280 files ok`) - Note: this branch includes commit `62eed99` from #70. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
github-bot closed this pull request 2026-11-27 21:47:07 +00:00

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.