Shaders: a compare in a lane-divergent branch leaves 0 in the mask bits of the lanes it skips #116

Closed
simbaui4life wants to merge 0 commits from upstream-pr/116 into main
simbaui4life commented 2026-11-27 18:23:41 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by simbaui4life on 2026-11-27T18:23:41Z.


A port of the class 5 fix originally made for avatarmanz/VIctory-7900XTX (that repository is no longer reachable). It builds on #70 (the oracle tests, simbaui4life:test/rdna2-oracle): that commit is included, and only the last commit is new.

Overlaps:

  • #73 (scalars after lane branches) adds the same lower::wave_wide_on() / THREAD_WAVE_WIDE and the same test helpers lane_split / by_hash, with identical text in the same places. In a trial merge with #73 (git merge-tree) those shared lines merge into one copy and mod.rs merges cleanly; lower.rs and oracle_tests.rs conflict only where both PRs add their own code at the same place (after branches_around, after THREAD_WAVE_WIDE, after by_hash). Keeping both sides resolves it.
  • #73 takes a lane branch wave-wide for a scalar that its region writes. This PR does the same for a compare's mask, which #73 leaves out.
  • Trial merges with #101 and #108 show no conflicts.

Cause: on RDNA2 a compare (v_cmp_* writing VCC, or an SGPR pair in VOP3 form) that runs while EXEC is narrowed writes 0 into the result bits of the lanes EXEC leaves out. The console's wave runs a lane-divergent branch's region when any lane needs it, so after the join every lane sees the region's mask:

  • each lane's own bit is 0 where it skipped the region;
  • read as data, the mask is the region's number.

The per-lane lowering joined the mask lane by lane, so a lane that skipped the region kept its old mask. That shows both as its bit (v_cndmask) and as the number (s_mov/v_mov of vcc_lo, built per incoming edge). Minimal cases from the fuzzer (wave32; minimized on the 7900XTX fork, whose oracle is the same as #70's):

v_cmp_gt_i32  vcc, s23, v3      ; the branch condition
s_and_saveexec_b32 s40, vcc_lo
s_cbranch_execz  ...
v_cmp_lt_i32  vcc, v6, v3       ; only in the branch
s_mov_b32     exec_lo, s40
; vcc_lo as data: console 0x02001400 in every lane,
; here 0x96009443 (the old mask) in the 22 lanes that skipped the branch
if (lane) { } else { v_cmp_lt_u32 vcc, v4, v6 }
v_cndmask_b32 v10, 7, v1, vcc
; the lanes that took then: console bit 0 (else ran), here their old bit 1

What this does

  • lower::CompareJoins::skipped finds a join that mixes two masks: a compare's mask written in a per-lane branch's region, and the old mask that the branch's other lanes bring past the region. It follows joins inside the region, so nested branches and loops in the region count too.
  • In compute programs, when such a join is read, the lowering names the region's blocks and stops with LANE_MASK. A join counts as read in two cases:
    • as lane bits, unless every lane that skipped brings 0 already (as for a branch on the mask itself);
    • as data, including through a join it is made of.
  • emit()'s existing LANE_MASK retry then takes those branches wave-wide (widen / structure_wide), as the console does. Active lanes and the v_cmpx EXEC result are unchanged.
  • Where the branches cannot be taken wave-wide, emit() lowers the program again with these joins per lane (lower::with_per_lane_compare_joins), as before.
  • A console wave64 on 32-lane host waves needs group barriers for wave-wide branches. There this applies only with VICTORY_WAVE_WIDE=1, as in #73; without it, the per-lane form stays. VICTORY_WAVE_WIDE=0 turns it off everywhere (it is already in the shader cache key). It is not used in the local-wave forms. The default wave-wide form of the scene kernels listed in emit() is left as it was.
  • In a form with wave-wide branches, the joins that a read SGPR join is made of are marked as read too. Otherwise a mask joined twice (a compare in a nested branch, read as data after the outer join) becomes 0, through the shortcut documented in fill_phis (VICTORY_STRICT_JOINS). The per-lane form is untouched.
  • No version bump: this fork keys the shader cache by the translator's sources, so cached translations are redone after the update. No new switch.

Differences from the 7900XTX version

  • widen() and emit() are this fork's: the existing calls are unchanged, and only the fallback after the LANE_MASK retry gained the per-lane re-lowering.
  • The wide_on line of the wave64 retry is not touched (#73 leaves it too): the test passes the same with and without reading it through wave_wide_on().
  • The test: this fork refuses the "nested" shape with masks as data in wave32, before and after this change ("a lane mask holding lanes elsewhere read as data", where not every lane runs; this fork has no retry that lowers such a mask as the number it is). The test counts those 7 refusals and fails on any other refusal or difference. In wave64 the same shape translates and passes.

Tested (RTX 4070 Ti, driver 621.01, Windows 11 26200; wave64 runs as two 32-lane host waves)

  • oracle_compares_in_lane_branches_zero_the_lanes_left_out covers 5 shapes:

    • a compare in then
    • in else
    • in a nested branch
    • in a uniform loop inside the branch
    • in a branch inside a uniform loop

    VCC is read after the compare inside the branch and after the join, through v_cndmask and, with masks as data, as numbers (s_mov to an SGPR, stored). 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: 140 cases.

    • Without the change 42 of them differ, in all five shapes (10 in wave32, 32 in wave64); with it, none.
    • 7 wave32 cases of the nested shape with masks as data are refused, before and after the change (see above). The test counts them and fails on any other refusal or difference.
  • This branch: cargo test -p victory-shader (291 passed, 0 failed, 5 ignored; #70 alone: 290), cargo clippy -p victory-shader --all-targets -- -D warnings, and the same with --all-features.

  • Fuzzer (oracle_fuzz, 300 programs per wave size). Programs that differ from the oracle, before → after:

    seed 2 seed 4
    wave32 122 → 104 98 → 91
    wave64, default (exact / default form) 69 / 83 → unchanged 70 / 93 → unchanged
    wave64, VICTORY_WAVE_WIDE=1 92 → 84 100 → 85
    wave32, other known classes left out¹ 33 → 2 21 → 3
    wave64, default, other known classes left out¹ 15 → unchanged 14 → unchanged
    wave64, VICTORY_WAVE_WIDE=1, other known classes left out¹ 15 → 3 14 → 2

    ¹ VICTORY_ORACLE_AVOID=salu_in_branch,minmax_scc,lane_range,writelane_inactive.

    • No program differs that did not before, and no program that still differs gained a register.
    • The refusals are the same, program by program and by reason (wave64 default: 42 / 43 in the default form, 57 / 68 in the exact one; none elsewhere).
    • The wave32 numbers are the same with and without VICTORY_WAVE_WIDE=1.
    • With the other classes left out, the 10 programs that still differ (wave32 and wave64 with VICTORY_WAVE_WIDE=1) all match the oracle under VICTORY_STRICT_JOINS=1: 300 of 300 in both wave sizes, for both seeds.
    • The numbers before the change are this fork's own runs; they are within one program of the 7900XTX fork's, and the changes are the same.

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: a wave vote per branch, guards on the vector instructions inside it, and two group barriers per branch when a wave64 runs on 32-lane waves.
  • Device losses: more wave32 compute programs now take branches wave-wide. That form has no group barriers. A device loss with widened loops has been reported for the barrier form (VICTORY_WAVE_WIDE), which this PR uses for wave64 only when VICTORY_WAVE_WIDE=1 is set. Neither was run in the game here.
  • Thread groups of several console waves on 32-lane hosts (the per-console-wave meetings of widen): the fuzzer and the test use one console wave per group.
  • Not covered:
    • Wave64 on 32-lane hosts without VICTORY_WAVE_WIDE=1 (it keeps the per-lane form).
    • Carry-outs (v_add_co_* and the like) and SALU writes of a mask inside a lane branch.
    • Masks joined across uniform branches and read as data only through another join, which stay 0 in the per-lane form (the VICTORY_STRICT_JOINS shortcut, before and after this PR).

No game data in the commits.

🤖 Generated with Claude Code

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5531321346 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/116). Originally posted by [simbaui4life](https://github.com/simbaui4life) on 2026-11-27T18:23:41Z. --- A port of the class 5 fix originally made for avatarmanz/VIctory-7900XTX (that repository is no longer reachable). It **builds on #70** (the oracle tests, `simbaui4life:test/rdna2-oracle`): that commit is included, and only the last commit is new. **Overlaps:** - #73 (scalars after lane branches) adds the same `lower::wave_wide_on()` / `THREAD_WAVE_WIDE` and the same test helpers `lane_split` / `by_hash`, with identical text in the same places. In a trial merge with #73 (`git merge-tree`) those shared lines merge into one copy and `mod.rs` merges cleanly; `lower.rs` and `oracle_tests.rs` conflict only where both PRs add their own code at the same place (after `branches_around`, after `THREAD_WAVE_WIDE`, after `by_hash`). Keeping both sides resolves it. - #73 takes a lane branch wave-wide for a scalar that its region writes. This PR does the same for a compare's mask, which #73 leaves out. - Trial merges with #101 and #108 show no conflicts. **Cause:** on RDNA2 a compare (`v_cmp_*` writing VCC, or an SGPR pair in VOP3 form) that runs while EXEC is narrowed writes **0** into the result bits of the lanes EXEC leaves out. The console's wave runs a lane-divergent branch's region when any lane needs it, so after the join every lane sees the region's mask: - each lane's own bit is 0 where it skipped the region; - read as data, the mask is the region's number. The per-lane lowering joined the mask lane by lane, so a lane that skipped the region kept its old mask. That shows both as its bit (`v_cndmask`) and as the number (`s_mov`/`v_mov` of `vcc_lo`, built per incoming edge). Minimal cases from the fuzzer (wave32; minimized on the 7900XTX fork, whose oracle is the same as #70's): ``` v_cmp_gt_i32 vcc, s23, v3 ; the branch condition s_and_saveexec_b32 s40, vcc_lo s_cbranch_execz ... v_cmp_lt_i32 vcc, v6, v3 ; only in the branch s_mov_b32 exec_lo, s40 ; vcc_lo as data: console 0x02001400 in every lane, ; here 0x96009443 (the old mask) in the 22 lanes that skipped the branch ``` ``` if (lane) { } else { v_cmp_lt_u32 vcc, v4, v6 } v_cndmask_b32 v10, 7, v1, vcc ; the lanes that took then: console bit 0 (else ran), here their old bit 1 ``` **What this does** - `lower::CompareJoins::skipped` finds a join that mixes two masks: a compare's mask written in a per-lane branch's region, and the old mask that the branch's other lanes bring past the region. It follows joins inside the region, so nested branches and loops in the region count too. - In compute programs, when such a join is read, the lowering names the region's blocks and stops with `LANE_MASK`. A join counts as read in two cases: - as lane bits, unless every lane that skipped brings 0 already (as for a branch on the mask itself); - as data, including through a join it is made of. - `emit()`'s existing `LANE_MASK` retry then takes those branches wave-wide (`widen` / `structure_wide`), as the console does. Active lanes and the `v_cmpx` EXEC result are unchanged. - Where the branches cannot be taken wave-wide, `emit()` lowers the program again with these joins per lane (`lower::with_per_lane_compare_joins`), as before. - A console wave64 on 32-lane host waves needs group barriers for wave-wide branches. There this applies only with `VICTORY_WAVE_WIDE=1`, as in #73; without it, the per-lane form stays. `VICTORY_WAVE_WIDE=0` turns it off everywhere (it is already in the shader cache key). It is not used in the local-wave forms. The default wave-wide form of the scene kernels listed in `emit()` is left as it was. - In a form with wave-wide branches, the joins that a read SGPR join is made of are marked as read too. Otherwise a mask joined twice (a compare in a nested branch, read as data after the outer join) becomes 0, through the shortcut documented in `fill_phis` (`VICTORY_STRICT_JOINS`). The per-lane form is untouched. - No version bump: this fork keys the shader cache by the translator's sources, so cached translations are redone after the update. No new switch. **Differences from the 7900XTX version** - `widen()` and `emit()` are this fork's: the existing calls are unchanged, and only the fallback after the `LANE_MASK` retry gained the per-lane re-lowering. - The `wide_on` line of the wave64 retry is not touched (#73 leaves it too): the test passes the same with and without reading it through `wave_wide_on()`. - The test: this fork refuses the "nested" shape with masks as data in wave32, before and after this change ("a lane mask holding lanes elsewhere read as data", where not every lane runs; this fork has no retry that lowers such a mask as the number it is). The test counts those 7 refusals and fails on any other refusal or difference. In wave64 the same shape translates and passes. **Tested** (RTX 4070 Ti, driver 621.01, Windows 11 26200; wave64 runs as two 32-lane host waves) - `oracle_compares_in_lane_branches_zero_the_lanes_left_out` covers 5 shapes: - a compare in then - in else - in a nested branch - in a uniform loop inside the branch - in a branch inside a uniform loop VCC is read after the compare inside the branch and after the join, through `v_cndmask` and, with masks as data, as numbers (`s_mov` to an SGPR, stored). 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: 140 cases. - Without the change 42 of them differ, in all five shapes (10 in wave32, 32 in wave64); with it, none. - 7 wave32 cases of the nested shape with masks as data are refused, before and after the change (see above). The test counts them and fails on any other refusal or difference. - This branch: `cargo test -p victory-shader` (291 passed, 0 failed, 5 ignored; #70 alone: 290), `cargo clippy -p victory-shader --all-targets -- -D warnings`, and the same with `--all-features`. - Fuzzer (`oracle_fuzz`, 300 programs per wave size). Programs that differ from the oracle, before → after: | | seed 2 | seed 4 | |---|---|---| | wave32 | 122 → 104 | 98 → 91 | | wave64, default (exact / default form) | 69 / 83 → unchanged | 70 / 93 → unchanged | | wave64, `VICTORY_WAVE_WIDE=1` | 92 → 84 | 100 → 85 | | wave32, other known classes left out¹ | 33 → 2 | 21 → 3 | | wave64, default, other known classes left out¹ | 15 → unchanged | 14 → unchanged | | wave64, `VICTORY_WAVE_WIDE=1`, other known classes left out¹ | 15 → 3 | 14 → 2 | ¹ `VICTORY_ORACLE_AVOID=salu_in_branch,minmax_scc,lane_range,writelane_inactive`. - No program differs that did not before, and no program that still differs gained a register. - The refusals are the same, program by program and by reason (wave64 default: 42 / 43 in the default form, 57 / 68 in the exact one; none elsewhere). - The wave32 numbers are the same with and without `VICTORY_WAVE_WIDE=1`. - With the other classes left out, the 10 programs that still differ (wave32 and wave64 with `VICTORY_WAVE_WIDE=1`) all match the oracle under `VICTORY_STRICT_JOINS=1`: 300 of 300 in both wave sizes, for both seeds. - The numbers before the change are this fork's own runs; they are within one program of the 7900XTX fork's, and the changes are the same. **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: a wave vote per branch, guards on the vector instructions inside it, and two group barriers per branch when a wave64 runs on 32-lane waves. - Device losses: more wave32 compute programs now take branches wave-wide. That form has no group barriers. A device loss with widened loops has been reported for the barrier form (`VICTORY_WAVE_WIDE`), which this PR uses for wave64 only when `VICTORY_WAVE_WIDE=1` is set. Neither was run in the game here. - Thread groups of several console waves on 32-lane hosts (the per-console-wave meetings of `widen`): the fuzzer and the test use one console wave per group. - Not covered: - Wave64 on 32-lane hosts without `VICTORY_WAVE_WIDE=1` (it keeps the per-lane form). - Carry-outs (`v_add_co_*` and the like) and SALU writes of a mask inside a lane branch. - Masks joined across uniform branches and read as data only through another join, which stay 0 in the per-lane form (the `VICTORY_STRICT_JOINS` shortcut, before and after this PR). No game data in the commits. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Aniseto commented 2026-11-27 18:31:28 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by Aniseto on 2026-11-27T18:31:28Z.


Ran the new oracle test for compare masks in lane-divergent branches on an NVIDIA Blackwell part at d212aea:

Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, wave32 only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09), MSVC 19.44.35230, DXC 1.9.2609.5, debug build, VICTORY_REQUIRE_HW_GPU=1.

cargo test -p victory-shader --lib oracle_compares_in_lane_branches_zero_the_lanes_left_out -- --nocapture
1 passed; 0 failed; 0 ignored      (exit 0)

The test printed "7 of 140 cases refused as before (wave32, nested, masks as data)". This adds a Blackwell part with a newer driver and Windows build than the RTX 4070 Ti referenced by the PR, with VICTORY_REQUIRE_HW_GPU set so a silent skip would have failed the run. The thread had no prior independent datapoint.

Not tested: the ignored oracle fuzzer, other seeds, AMD/Intel adapters, Wine, release profile, game data.

AI-assisted (OpenCode), model: DeepSeek V4.1 Flash Free - checked out the PR head in a detached worktree, ran the command above and wrote this report; no game data was used.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7225576325 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/116#issuecomment-7225576325). Originally posted by [Aniseto](https://github.com/Aniseto) on 2026-11-27T18:31:28Z. --- Ran the new oracle test for compare masks in lane-divergent branches on an NVIDIA Blackwell part at d212aea: Environment: Windows 11 Pro build 26200 x64, NVIDIA GeForce RTX 5070 Ti (Blackwell, wave32 only, 16 GB) driver 32.0.16.2124, Intel Core i5-12400F, 64 GB RAM, rustc 1.100.0 (0ef1bc386 2026-11-09), MSVC 19.44.35230, DXC 1.9.2609.5, debug build, VICTORY_REQUIRE_HW_GPU=1. ``` cargo test -p victory-shader --lib oracle_compares_in_lane_branches_zero_the_lanes_left_out -- --nocapture 1 passed; 0 failed; 0 ignored (exit 0) ``` The test printed "7 of 140 cases refused as before (wave32, nested, masks as data)". This adds a Blackwell part with a newer driver and Windows build than the RTX 4070 Ti referenced by the PR, with VICTORY_REQUIRE_HW_GPU set so a silent skip would have failed the run. The thread had no prior independent datapoint. Not tested: the ignored oracle fuzzer, other seeds, AMD/Intel adapters, Wine, release profile, game data. AI-assisted (OpenCode), model: **DeepSeek V4.1 Flash Free** - checked out the PR head in a detached worktree, ran the command above and wrote this report; no game data was used.

Pull request closed

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
trustno1-coder/VIctory-NVIDIA!116
No description provided.