Shaders: the SCC a waterfall loop's s_andn2_wrexec leaves is false after the loop #3014
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3016"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by Anzymanz on 2026-11-26T03:29:14Z.
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:
sop()refuses any SALU that writes EXEC, and the refusal marks every value the instruction writes, its SCC included, asa lane mask read as data. When a block after the loop reads that SCC (ans_cselect,s_cbranch_scc*ors_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 ans_andn2_wrexec_b32/_b64that is the second-last instruction of a block whose last iss_cbranch_execnzback to the same block, its SCC is the constantfalse. The loop is left only on its not-taken edge, that is once EXEC is 0, soEXEC != 0is 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) staya lane mask read as data, as before.Tests
valu_tests::the_scc_a_waterfall_loop_leaves_is_false(GPU): the loop above overv1, thens_cselect_b32 s23, s1, s2after it;s2expected. 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'sdxil.dll).Measured over the title's shader pack
All 25,889 distinct containers in
shaders.rpfwent throughemit::translate_withoffline, on main63bcca4and then with this commit:63bcca4a lane mask read as data827b309e779552d7,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 warningsis clean,cargo xtask policyis ok, andlower.rsis 521,918 bytes (+1,355 B).Not run: the differential tests (no console compiler here) and NVIDIA.
🤖 Generated with Claude Code
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T03:30:55Z.
Review: APPROVED
Tested merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP,dxil.dllvalidator 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).
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T03:43:40Z.
Review: CHANGES_REQUESTED
Code review on
84a02d87(static source inspection; no runtime GPU/DXIL validation executed):SCCtofalsealso reaches the successor on loop-limiter forced exits (crates/victory-shader/src/emit/lower.rs:3571-3608, 10214-10224):leaves_with_no_lanefoldss_andn2_wrexec'sSCCoutput tofalseassuming the loop exits only whenEXEC == 0. However, whenVICTORY_SHADER_LOOP_LIMITcuts a waterfall loop short (lower.rs:10214-10224), execution exits into the same successor while pending lanes remain (soEXEC != 0andSCCis stilltrue).:10287) — and verifying that thes_andn2_wrexecretry loop only retains pending lanes asresource.rs:2031-2037does — keeps the fold sound.crates/victory-shader/src/emit/valu_tests.rs:299-318):build_valu_cs/run_casesdispatches 1 thread per group, so thes_cbranch_execnzback edge is never taken inthe_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.emit/lower.rsis521,918 B(within the 512 KiB limit).Imported from Marvisak/VIctory. Originally posted by steven-seagull27 on 2026-11-26T03:40:50Z.
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 weres_cselect_b32,s_cbranch_scc1ands_addc_u32. The emitted DXIL validated. These are guest wave sizes; the RTX 5080 uses 32 physical lanes.Parent
63bcca4refused the new selected-key fixture at lowering witha lane mask read as data. A separate compile-only check exercises your guard: ans_cselect_b32at 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
672c4bcapplies to your head. Runcargo 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.Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T04:38:28Z.
Review: APPROVED
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.
Imported from Marvisak/VIctory. Originally posted by zed6000 on 2026-11-26T04:09:48Z.
Measured offline on the title's shaders (the method in #1247): head
84a02d8merged ontomain63bcca4(a clean merge, security-reviewed first): +4, 0 lost. The 4 are compute programs that fail onmainwithemit: a lane mask read as data. They're the last 4 of that refusal onmain, and exactly the 4 #2289 gained before it was closed for rework.cargo test -p victory-shader --lib --lockedpasses 303/0 on an RX 7900 XTX.It also stacks. On top of #1924 (port) + #2978 + #385 + #2999 + #1737 on
63bcca4, it conflicts inemit/lower.rswhere yours_andn2_wrexechelper and #1924'sstore_lds_outputssit 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.Imported from Marvisak/VIctory. Originally posted by Anzymanz on 2026-11-26T04:43:39Z.
Thanks all. @fuzeofficlal, you're right about the limiter: a
Branchloop cut short byVICTORY_SHADER_LOOP_LIMITexits through its normal successor with lanes still pending (onlyJumpback edges go to the abort block), so SCC isn't 0 there. Fixed in7872701by narrowing the fold rather than changing the limiter:v_readfirstlane_b32result 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() >= 64is required, so the limiter can never cut such a loop short. With a smaller diagnostic limit, the old refusal stands.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_falseruns the loop over a 32-thread group keyedlane & 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 addstry_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 policyis ok, andlower.rsis 522,757 B.@zed6000: thanks for the stacked numbers. Noted on the
store_lds_outputsneighbour from #1924; keep both.AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by zed6000 on 2026-11-26T04:54:38Z.
Re-measured the narrowed head
7872701onmain63bcca4: still +4, 0 lost, the same 4 programs as84a02d8.cargo test -p victory-shader --lib --lockedpasses 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.Imported from Marvisak/VIctory. Originally posted by pawrenity on 2026-11-26T05:04:08Z.
VERIFY-3016: verified
7872701on NVIDIA GeForce RTX 4070, Windows 11, and on WARP. The earlier verifications were at84a02d8; this is the current head.Review (reading). The fold matches the
s_andn2_wrexecrule (EXEC = S0 & ~EXEC, D = EXEC, SCC = EXEC != 0).leaves_with_no_lane(crates/victory-shader/src/emit/lower.rs:3574) requires three things:s_cbranch_execnzis the last instruction and loops back to the same block;Two guards make the fold safe:
enter_loop) and increments on the back edge (count_iteration). The keyedv_readfirstlane/v_cmpx_eq_*form drops at least the first lane on each pass. Requiringloop_limit() >= 64therefore means the limiter cannot end this loop early.I found no defects. One edge case: an
f32/f16key 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) onorigin/main63bcca4. Both new tests fail at build withUnsupported { what: "a lane mask read as data" }.At head:
the_scc_a_waterfall_loop_leaves_is_falseandthe_scc_a_waterfall_loop_over_many_keys_leaves_is_falsepass on hardware and WARP. Neither backend was skipped.falsetotrue, both tests fail. They check the value, not only that the shader compiles.keyed,loop_limit() >= 64, the phi check). The fullvictory-shaderlib suite still gives 300 passed and the same 3 known failures. No test in this PR covers the guards. The test-only commit672c4bcposted 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.origin/main63bcca4is clean.Not checked: the shader-pack counts (no pack here) and the differential tests.
AI-assisted (Claude Code).
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T06:15:17Z.
Automated triage for the maintainers.
In
crates/victory-shader/src/emit/lower.rs,sop()now folds the SCC of ans_andn2_wrexec_b32/_b64tofalsewhen it is the second-last instruction of a self-loop closed bys_cbranch_execnz. The newleaves_with_no_lanealso requires the keyed waterfall form (v_readfirstlane_b32result compared with the same VGPR value byv_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 invalu_tests.rscheck 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. CIcheck: 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
Imported from Marvisak/VIctory. Originally posted by kilolimaVI on 2026-11-29T15:18:45Z.
AMD row and a real target for this one. On a Radeon 780M (RDNA3, driver 32.0.13022.3006), head
7872701: boththe_scc_a_waterfall_loop_leaves_is_falseandthe_scc_a_waterfall_loop_over_many_keys_leaves_is_falsepass.fiestorra traced the last four "a lane mask read as data" refusals in the title's corpus (#201): the
bvh_skinningCSComputeSkinningPedBlendShapes*wave32 compute programs, all with exactly this shape:v_readfirstlane_b32,v_cmpx_eq_u32 exec_lo, sK, vN,s_andn2_wrexec_b32,s_cbranch_execnzback to the loop head, thens_cselect_b32 s3, exec_lo, 0on the SCC after the loop. That is the keyed formleaves_with_no_lanematches.It is in
kilolimaVI/VIctoryintegration/shader-12(e912e4c) with the other pending shader PRs. The one conflict is with #3401 at the same spot (if inst.write(Reg::S(EXEC)).is_some() || self.is_mask(lo)): kept both, this PR's waterfall check first, then #3401's mask-SCC case. On that tree #3401's tests still pass (5/5), all 9 crosslane tests, victory-shader lib 363 passed / 0 failed / 3 ignored, victory-gpu (diagnostics) 246, both clippy configurations and the asset policy.AI-assisted (Claude).
Imported from Marvisak/VIctory. Originally posted by Anzymanz on 2026-11-29T22:31:05Z.
Thanks for the AMD row, @kilolimaVI, and for finding a real target for this. Your conflict order with #3401 (this PR's waterfall check first, then #3401's mask-SCC case) is the order we'd pick too.
Written with Claude Code.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.