Skip to content

Shaders: the SCC a waterfall loop's s_andn2_wrexec leaves is false after the loop - #3016

Open
Anzymanz wants to merge 2 commits into
Marvisak:mainfrom
Anzymanz:waterfall-scc
Open

Anzymanz wants to merge 2 commits into
Marvisak:mainfrom
Anzymanz:waterfall-scc

Conversation

@Anzymanz

Copy link
Copy Markdown

AI disclosure: written with Claude Code (Claude Opus 5.5) at the request of the account owner, who reviews what goes out. The measurements below were run on our machine; no game data is included (hashes, counts and error text only).

Problem

The console compiler's waterfall loop runs, one pass per distinct value, the lanes holding the first active lane's value. Its form, as this PR's test writes it:

    s_mov_b32            s20, exec_lo
    s_mov_b32            s21, exec_lo
loop:
    v_readfirstlane_b32  s22, v1
    v_cmpx_eq_u32        s22, v1
    s_andn2_wrexec_b32   s21, s21       ; EXEC = s21 & ~EXEC, s21 = EXEC, SCC = EXEC != 0
    s_cbranch_execnz     loop
    s_mov_b32            exec_lo, s20
    s_cselect_b32        s23, s1, s2    ; reads the SCC the s_andn2_wrexec wrote

sop() refuses any SALU that writes EXEC, and the refusal marks every value the instruction writes, its SCC included, as a lane mask read as data. When a block after the loop reads that SCC (an s_cselect, s_cbranch_scc* or s_addc), the program is refused, though nothing reads a lane mask as a number.

Change

emit/lower.rs, sop(): when the instruction writing EXEC is an s_andn2_wrexec_b32/_b64 that is the second-last instruction of a block whose last is s_cbranch_execnz back to the same block, its SCC is the constant false. The loop is left only on its not-taken edge, that is once EXEC is 0, so EXEC != 0 is false wherever a later block reads it. If one of the loop block's own phis takes the SCC (a later iteration's, on the back edge), the old refusal stands. Its other writes (EXEC and the SGPR) stay a lane mask read as data, as before.

Tests

  • valu_tests::the_scc_a_waterfall_loop_leaves_is_false (GPU): the loop above over v1, then s_cselect_b32 s23, s1, s2 after it; s2 expected. Fails on main at lowering (a lane mask read as data); passes with this commit on an RX 9070 XT (RADV through vkd3d-proton, GE-Proton 11-14, DXC's dxil.dll).

Measured over the title's shader pack

All 25,889 distinct containers in shaders.rpf went through emit::translate_with offline, on main 63bcca4 and then with this commit:

main 63bcca4 + this
translated / refused 25,669 / 220 25,673 / 216
refused on a lane mask read as data 4 0
programs whose output changes 4 (827b309e779552d7, 82a2ad6d07417d36, 90a0692af4320129, 9ffcaff533995118, all refused before)

Every other program's output is byte-identical. Two of the four are the title's skinning kernels that put animated characters into the ray-tracing structures.

All four programs' DXIL converts with dxil-spirv (master). On main + #2348 + #2978 + #385 the same commit changes exactly four programs as well (a lane mask read as data: 202 → 198 refused there), with 0 going from translated to refused.

Windows test binary under Proton on the RX 9070 XT: 301 passed and 2 failed. The 2 failures (hosts_that_do_not_fuse_are_named, stub_pipeline_is_created) fail on main in the same environment too. cargo clippy -p victory-shader -- -D warnings is clean, cargo xtask policy is ok, and lower.rs is 521,918 bytes (+1,355 B).

Not run: the differential tests (no console compiler here) and NVIDIA.


🤖 Generated with Claude Code

…ter the loop

The console compiler's waterfall loop (v_readfirstlane, v_cmpx_eq,
s_andn2_wrexec, s_cbranch_execnz back to the same block) is left only once
the s_andn2_wrexec leaves no lane, so the SCC it writes (EXEC != 0) is 0
wherever a block after the loop reads it. That SCC was refused with the
EXEC write ('a lane mask read as data'), and a later s_cselect or
s_cbranch_scc reading it stopped the program. It is now the constant false;
the loop's own phis taking it (a later iteration's) still refuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib emit::valu_tests::the_scc_a_waterfall_loop_leaves_is_false: 1 passed, 0 failed (RTX 5090 hardware + WARP).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

@steven-seagull27

Copy link
Copy Markdown

I extended the single-thread check to groups with 1, 2, 4 and 8 distinct keys. All 1,152 lane outputs per adapter matched on the RTX 5080, Windows built-in WARP and WARP 1.0.22 at 84a02d8. The 24 synthetic shader containers use both the 32-lane guest form (s_andn2_wrexec_b32) and the 64-lane guest form (_b64). Each lane saves its own key inside the loop, then writes a value that also depends on the exit flag. The three consumers were s_cselect_b32, s_cbranch_scc1 and s_addc_u32. The emitted DXIL validated. These are guest wave sizes; the RTX 5080 uses 32 physical lanes.

Parent 63bcca4 refused the new selected-key fixture at lowering with a lane mask read as data. A separate compile-only check exercises your guard: an s_cselect_b32 at the loop header reads the preceding iteration's SCC. The PR kept the refusal for this program. Removing the loop-phi check made the translator accept it, which the regression caught.

The test-only commit 672c4bc applies to your head. Run cargo test --locked -p victory-shader --lib waterfall_scc_tests -- --nocapture --test-threads=1. I also ran your original check, strict Clippy and asset policy. Your title-pack census remains the evidence for the four game shaders.

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review on 84a02d87 (static source inspection; no runtime GPU/DXIL validation executed):

  • P2 — Constant-folding SCC to false also reaches the successor on loop-limiter forced exits (crates/victory-shader/src/emit/lower.rs:3571-3608, 10214-10224):
    • leaves_with_no_lane folds s_andn2_wrexec's SCC output to false assuming the loop exits only when EXEC == 0. However, when VICTORY_SHADER_LOOP_LIMIT cuts a waterfall loop short (lower.rs:10214-10224), execution exits into the same successor while pending lanes remain (so EXEC != 0 and SCC is still true).
    • Routing capped retries to the abort block (:10287) — and verifying that the s_andn2_wrexec retry loop only retains pending lanes as resource.rs:2031-2037 does — keeps the fold sound.
  • Test coverage note (crates/victory-shader/src/emit/valu_tests.rs:299-318):
    • build_valu_cs / run_cases dispatches 1 thread per group, so the s_cbranch_execnz back edge is never taken in the_scc_a_waterfall_loop_leaves_is_false. Adding a multi-lane case with distinct per-lane keys would actually exercise the waterfall iteration and exit.
  • File size check: patched emit/lower.rs is 521,918 B (within the 512 KiB limit).

@zed6000

zed6000 commented Nov 26, 2026

Copy link
Copy Markdown

Measured offline on the title's shaders (the method in #1247): head 84a02d8 merged onto main 63bcca4 (a clean merge, security-reviewed first): +4, 0 lost. The 4 are compute programs that fail on main with emit: a lane mask read as data. They're the last 4 of that refusal on main, and exactly the 4 #2289 gained before it was closed for rework. cargo test -p victory-shader --lib --locked passes 303/0 on an RX 7900 XTX.

It also stacks. On top of #1924 (port) + #2978 + #385 + #2999 + #1737 on 63bcca4, it conflicts in emit/lower.rs where your s_andn2_wrexec helper and #1924's store_lds_outputs sit at the same spot. Keep both. The result is 26,011 of 26,084 programs validated (+1,161, 0 lost), and 334/0 tests. Offline counts only; no program names or data.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — the waterfall pattern match is precise: andn2_wrexec at insts[n-2], execnz at [n-1], back-edge to the same block, and SCC not consumed by own-block phis (a phi taking it mid-loop still gets the real value). Writes marked missing + SCC=false matches post-loop semantics; the test is the canonical readfirstlane/cmpx/andn2 loop.

… a loop limit it cannot reach

Review on Marvisak#3016: a loop cut short by VICTORY_SHADER_LOOP_LIMIT leaves
through its normal exit with lanes still pending, so its SCC is not 0. The
fold now needs the loop to compare its v_readfirstlane result with the VGPR
that was read (each pass then drops at least the first lane, so it runs at
most 64 passes) and loop_limit() of at least 64; otherwise the old refusal
stands. A second GPU test runs the loop over 32 lanes with 1, 2, 8 and 32
distinct keys, so the back edge is taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Anzymanz

Anzymanz commented Nov 26, 2026 •

Copy link
Copy Markdown
Author

Thanks all. @fuzeofficlal, you're right about the limiter: a Branch loop cut short by VICTORY_SHADER_LOOP_LIMIT exits through its normal successor with lanes still pending (only Jump back edges go to the abort block), so SCC isn't 0 there. Fixed in 7872701 by narrowing the fold rather than changing the limiter:

  • The loop must be the keyed waterfall form: an instruction compares the v_readfirstlane_b32 result with the same VGPR value it read (v_cmpx_eq_* reading both). Each pass then drops at least the first active lane, so the loop runs at most 64 passes.
  • super::loop_limit() >= 64 is required, so the limiter can never cut such a loop short. With a smaller diagnostic limit, the old refusal stands.
  • The other conditions (self-loop closed by s_cbranch_execnz, the wrexec just before it, no phi of the block taking the SCC) are unchanged.

Multi-lane coverage. New the_scc_a_waterfall_loop_over_many_keys_leaves_is_false runs the loop over a 32-thread group keyed lane & s0 (1, 2, 8 and 32 distinct keys), so the back edge is taken up to 31 times before the SCC is read. Thanks @steven-seagull27 for the much wider check (1/2/4/8 keys, b32/b64, three consumers) on an RTX 5080 and WARP. It adds try_build_valu_cs_lanes, the same edit #2978 makes, so the two merge cleanly in either order.

Re-measured (main 63bcca4 + 7872701), whole shader pack: refused 220 → 216, the same 4 programs as before (827b309e, 82a2ad6d, 90a0692a, 9ffcaff5; all keyed waterfalls, so the narrower check keeps them), every output identical to the first version, 0 programs lost. Both GPU tests pass on an RX 9070 XT (RADV via vkd3d-proton). The full Windows suite is at 302 passed and 2 failed; the 2 (hosts_that_do_not_fuse_are_named, stub_pipeline_is_created) fail on main in this environment too. cargo clippy -p victory-shader --all-targets -- -D warnings (Windows target) is clean, xtask policy is ok, and lower.rs is 522,757 B.

@zed6000: thanks for the stacked numbers. Noted on the store_lds_outputs neighbour from #1924; keep both.

AI-assisted (Claude Code).

@zed6000

zed6000 commented Nov 26, 2026

Copy link
Copy Markdown

Re-measured the narrowed head 7872701 on main 63bcca4: still +4, 0 lost, the same 4 programs as 84a02d8. cargo test -p victory-shader --lib --locked passes 304/0 on the RX 7900 XTX. So the narrower fold still covers this title's waterfall loops. Offline counts only; no program names or data.

LAKSHYA-cmd-tech added a commit to LAKSHYA-cmd-tech/VIctory that referenced this pull request Nov 26, 2026
…dia-translate

Shaders, HLE: fold waterfall loop SCC after loop and translate \??\ NT paths in media (Marvisak#3016, Marvisak#3030)
@pawrenity

Copy link
Copy Markdown

VERIFY-3016: verified 7872701 on NVIDIA GeForce RTX 4070, Windows 11, and on WARP. The earlier verifications were at 84a02d8; this is the current head.

Review (reading). The fold matches the s_andn2_wrexec rule (EXEC = S0 & ~EXEC, D = EXEC, SCC = EXEC != 0). leaves_with_no_lane (crates/victory-shader/src/emit/lower.rs:3574) requires three things:

  • the wrexec is the second-last instruction;
  • s_cbranch_execnz is the last instruction and loops back to the same block;
  • the block exits only on the not-taken edge, where EXEC is 0, so the SCC is 0.

Two guards make the fold safe:

  • Loop limit. The counter belongs to each loop header, resets on forward entry (enter_loop) and increments on the back edge (count_iteration). The keyed v_readfirstlane/v_cmpx_eq_* form drops at least the first lane on each pass. Requiring loop_limit() >= 64 therefore means the limiter cannot end this loop early.
  • Back-edge phi. The guard on the block's own phis keeps a back-edge SCC refused.

I found no defects. One edge case: an f32/f16 key that is NaN never compares equal to itself, so no lane drops. That loop also never ends on hardware, so I don't count it as a defect.

Regression check. I put the PR's test hunk alone (valu_tests.rs) on origin/main 63bcca4. Both new tests fail at build with Unsupported { what: "a lane mask read as data" }.

At head:

  • the_scc_a_waterfall_loop_leaves_is_false and the_scc_a_waterfall_loop_over_many_keys_leaves_is_false pass on hardware and WARP. Neither backend was skipped.
  • Mutation 1. With the folded SCC changed from false to true, both tests fail. They check the value, not only that the shader compiles.
  • Mutation 2. I removed all three guards at once (keyed, loop_limit() >= 64, the phi check). The full victory-shader lib suite still gives 300 passed and the same 3 known failures. No test in this PR covers the guards. The test-only commit 672c4bc posted earlier covers the phi guard, but it is not in the PR.
  • cargo test --locked -p victory-shader --lib (one test skipped because it crashes the process on NVIDIA): 300 passed, 3 failed. The 3 (stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, synthetic_tests::gathers_at_a_level_match_the_hardware) also fail on main on this card.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings: clean.
  • A trial merge onto current origin/main 63bcca4 is clean.

Not checked: the shader-pack counts (no pack here) and the differential tests.

AI-assisted (Claude Code).

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

In crates/victory-shader/src/emit/lower.rs, sop() now folds the SCC of an s_andn2_wrexec_b32/_b64 to false when it is the second-last instruction of a self-loop closed by s_cbranch_execnz. The new leaves_with_no_lane also requires the keyed waterfall form (v_readfirstlane_b32 result compared with the same VGPR value by v_cmpx_eq_*), loop_limit() >= 64, and no phi of the loop block taking that SCC. That follows the RDNA2 rule SCC = (EXEC != 0): the loop is left only on the not-taken edge, where EXEC is 0, and the limit guard covers the diagnostic limiter's early exit. The other writes stay refused as lane masks, as before. Two GPU tests in valu_tests.rs check the selected value, with 1 lane and with 32 lanes and several key counts. Title-pack counts posted by the author and a second contributor show +4, 0 lost. CI check: passing.

Suggestion, not a blocker: no test in the PR exercises the guards (a reviewer reports that removing all three leaves the suite green). Adding the phi-guard regression from the test-only commit offered in this thread would cover that.


Generated by Claude Code

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…s_andn2_wrexec leaves is false after the loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants