Repository navigation
Shaders: a wave64 mask restored from word-by-word scalar copies is the same mask - #2816
0xkn0wh3r3 wants to merge 7 commits into
Conversation
|
Verified
|
kotekompotek
left a comment
There was a problem hiding this comment.
Complementary to #2836 (which carries the true pair through Atom::Opaque) — this recovers masks that were tracked and merely parked behind s_mov chains, which is strictly better than keeping them opaque: they participate in BDD reasoning again rather than becoming lane-bit reads. move_source follows only single-read s_mov_b32 chains so SSA can't cycle, the source != k guard avoids self-matches, and consulting only masks (not other) means moved non-derived scalars conservatively stay opaque. The test's reorder case (s3 from vcc_lo) correctly asserts no false recovery. Sits cleanly before the restored/join/Opaque fallbacks. LGTM.
|
Hardware result: tested at head 306e73c. One host: RTX 3080 Ti, driver 621.24, Windows 11, a fork stacking open PRs, Series X mode. Cherry-picked together with #332 and #2831; one 7-minute run in play, no device removal. A test the stack carried that expected a dword-by-dword mask restore into EXEC to be refused now translates; turned into an execution test (64 threads, wave64 split in halves, lanes below 20 take the branch), it gives the expected values on this GPU. Ran with AI assistance (Claude Code). |
Shaders: port four translator fixes from upstream (Marvisak#2813, Marvisak#2812, Marvisak#2816, Marvisak#2817)
306e73c to
7fafdf3
Compare
|
Automated triage for the maintainers.
CI: passing. Generated by Claude Code |
Dukestar69
left a comment
There was a problem hiding this comment.
Tested merged onto origin/main (63bcca4) on Windows 11 x64:
cargo test -p victory-shader --lib structure::tests: 14 passed, 0 failed, 1 ignored (including newsplit_scalar_copies_across_a_branch_preserve_both_words_of_a_wave64_mask).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).
…d scalar copies is the same mask
|
Tested the mask change at The stock translator refused this shader at The localized flicker at the courtyard tables remained visible with this shader enabled. |
…e same mask A 64-bit lane mask parked in two scalars one dword at a time (s_mov_b32 s2, vcc_lo; s_mov_b32 s3, vcc_hi) and later copied back into EXEC reached the structurizer as an unknown pair, so the mask was opaque and shaders that restore EXEC this way (a voxel visibility shader of the title) were refused or lost the branch condition. Follow each word through its chain of s_mov_b32 back to its source; when both words still come from a known mask, in their original order, the pair is that mask. Ported from Marvisak#2816 (306e73c), itself from DexNom/VIctory@76749ce. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fuzeofficlal
left a comment
There was a problem hiding this comment.
Verified 7fafdf36 locally on Windows 11 x64 (git merge-tree onto 63bcca43 is clean):
cargo clippy -p victory-shader --all-targets --locked -- -D warnings(default and--all-features): clean (2.57s/2.21s).cargo test -p xtask --locked policy::tests::repository_passes_policy: 1 passed, 0 failed (0.34s).cargo test -p victory-shader --lib --locked structure::tests: 14 passed, 0 failed, 1 ignored (0.01s).
move_source and the (lo, hi) copy-chain lookup inside Lanes::mask (structure.rs:935-942, 986-999) are sound, acyclic, and reject swapped/mixed words. However, Structured::finish (structure.rs:875) flattens self.masks to low-word-only keys ((lo, _) -> (lo, m)), and v_cndmask_b32 in emit/lower.rs:6533-6542 looks up the predicate by the low word alone. If a wave64 shader uses the restored (lo, hi) pair once, overwrites only the high word (e.g. s_mov_b32 hi, 0), and uses (lo, 0) in a second v_cndmask_b32, the second select silently reuses the first full-wave predicate for the upper 32 lanes. Please validate the full (lo, hi) pair at wave64 select lowering (or reject mismatched high words) and add a single-shader regression covering both uses.
…e same mask A 64-bit lane mask parked in two scalars one dword at a time (s_mov_b32 s2, vcc_lo; s_mov_b32 s3, vcc_hi) and later copied back into EXEC reached the structurizer as an unknown pair, so the mask was opaque and shaders that restore EXEC this way (a voxel visibility shader of the title) were refused or lost the branch condition. Follow each word through its chain of s_mov_b32 back to its source; when both words still come from a known mask, in their original order, the pair is that mask. Ported from DexNom/VIctory@76749ce.
Structured::finish keys masks and other values by their low dword, so a v_cndmask_b32 whose high dword had been written since (s_mov_b32 s3, 0) found the old pair's lane predicate and took it for all 64 lanes. In wave64 the select now looks its mask up by the exact (low, high) pair the pass tracked for it, and an opaque mask is read only with its low dword's own high dword; otherwise the select is refused as an untracked mask.
7fafdf3 to
d489540
Compare
|
Thanks @fuzeofficlal, good catch. It reproduced. At 7fafdf3, a wave64 shader that copies a compare into Fixed in d489540 (the branch is also rebased onto 63bcca4):
The |
fuzeofficlal
left a comment
There was a problem hiding this comment.
d4895407 cleanly closes the direct-select failure after a Wave64 high-word overwrite (paired_other, Structured::select_bits(lo, hi), and checking hi == self.pair_high(lo) for Atom::Opaque(lo)), and the new a_wave64_select_after_its_high_word_changes_is_not_the_old_mask test passes.
One bypass remains before merge:
- Opaque pair forwarding via
s_mov_b64(structure.rs:940-998, 1078-1145,emit/lower.rs:2595-2607, 6540-6547): Whens[4:5]is copied from an opaque pairs[2:3]withs_mov_b64afters3is overwritten (e.g., cleared to 0),s[4:5]'spaired_otherpredicate inheritsAtom::Opaque(src_lo)rather thanAtom::Opaque(dst_lo).v_cndmask_b32only checkssupport(m).contains(&Atom::Opaque(lo))fordst_lo, missesOpaque(src_lo), andAtom::Opaque(src_lo)later readspair_high(src_lo)from the source's definition site—causing lanes 32–63 to see the old high word when console lane numbering is active. - Related low-word paths:
Atom::Phimask materialization (structure.rs:1021-1075,emit/lower.rs:2575-2587) andv_{add,sub,subrev}_co_ci_u32carry-in (emit/lower.rs:6599-6614) also still resolve predicates by the low word alone.
Please preserve the full opaque pair identity through copies/phis (or reject mismatched/ambiguous pairs) and add a regression covering lanes 32–63 after s_mov_b64. Also note rustfmt --edition 2024 --check flags formatting on structure.rs lines 491 and 895.
Verified locally on Windows 11 x64 (d4895407):
cargo run --locked -p xtask -- policy: passed (191 files ok)cargo clippy -p victory-shader --all-targets --locked -- -D warnings(default and--all-features): cleanstructure::tests/emit::lower::tests: 14 passed (1 ignored) / 33 passed, 0 faileda_wave64_select_after_its_high_word_changes_is_not_the_old_mask: 1 passed, 0 failed
An opaque mask was named by its low dword alone and lowered with the high dword written alongside it, so a pair copied with s_mov_b64 after its high dword changed read the old one for lanes 32-63. A mask joined at a block entry took each edge's mask by the low dword's phi argument, and a carry-in looked its mask up by the low dword. Atom::Opaque and Atom::Phi now hold the (low, high) pair. An opaque mask is lowered from exactly those two dwords; a join takes each edge's mask by that edge's pair of arguments, in its incoming values, the skip-edge check and the join links of avoids; a carry-in reads both dwords and looks the pair up in the masks. A pair that is not tracked is refused as before.
|
Thanks @fuzeofficlal. Both points reproduced, and both are fixed in 39a3718.
The round-one select test still passes; its second case is now refused as an opaque pair of
|
Atom::Opaque now holds a pair, so the pattern in translate's mask count matches any fields.
fuzeofficlal
left a comment
There was a problem hiding this comment.
Verified the fixes in df392b22: the direct v_cndmask_b32, _co_ci_u32 carry-in, pending_masks, no_mask_lane_on_edge, and avoids paths now check both Wave64 dwords, Atom::Opaque carries both (lo, hi) across s_mov_b64, and the new structure and synthetic lowering tests pass (emit/lower.rs = 520,658 bytes).
Two bugs still bypass the full-pair check, plus one liveness gap:
- Reversed spill-restore pairs in
Lanes::mask(crates/victory-shader/src/structure.rs:971-989): when(lo, hi)comes fromrestoredin swapped order (e.g.,s5ands4defined bys_mov_b64 s[4:5], vcc, spilled, and read back in reverse), the(None, _)fallback only checksdefs.get(&k.0) == defs.get(&k.1)and returnssop(i).dstwithout verifying that(k.0, k.1)matches the definition's(low, high)register order. - Descriptor selection via
Source::Lanes(crates/victory-shader/src/resource.rs:1708-1783,crates/victory-shader/src/emit/lower.rs:5639-5663):lane_pickedonly records the low SGPR (Reg::S(code)) ofv_cndmask_b32, andlane_select/lane_heap_indexlook upmasks.get(&mask)by that low dword alone, bypassingselect_bits(lo, hi)when the high dword is overwritten. - High-word liveness in
touches(crates/victory-shader/src/emit/lower.rs:10416-10431):Atom::Phi(v, _) | Atom::Opaque(v, _) => Some(v)drops the high dword, sodead_on_edgemisses dependencies that only taint the high word.
Please preserve the ordered pair check in both paths (with regressions for reversed spill words and descriptor selection) and check both words in touches.
Verified locally on Windows 11 x64 (df392b22):
cargo run --locked -p xtask -- policy(191 files ok) andcargo clippy --workspace --all-targets -- -D warnings(default and--all-features): clean.structure::tests(15 passed, 1 ignored),emit::lower::tests(33 passed), and both new Wave64 synthetic tests: passed.
… mask's pair A pair read back from VGPR spill lanes whose two dwords one instruction wrote was taken as that instruction's destination mask even with the dwords swapped; the destination's (low, high) key must now be the pair. A descriptor picked per lane by v_cndmask_b32 recorded only the mask's low dword, and resource tracking and lowering looked the mask up by it, so a pick after s_mov_b32 vcc_hi, 0 took the compare's mask for lanes 32-63. Source::Lanes records the high dword too and both look the pair up. Whether a branch or guard depends on a stale register now also checks the high dword of a joined or opaque mask.
|
Thanks @fuzeofficlal. All three are fixed in 68c0d2b, on top of df392b2.
Checks: |
|
Follow-up to my earlier fork result. I tested the same cascade-shadow compute shader at upstream PR revision Both builds translated the shader and passed DXIL validation. On both, D3D12 rejected compute pipeline creation with message 622. The shader requests a wave size of 64 and this adapter reports a native range of 32–32. A separate compute shader successfully created its pipeline on both builds. The successful pipeline creation in my earlier comment used the downstream translator with 32-lane host support. The wave-size limit reproduces on the upstream base as well. AI-assisted hardware test. |
|
Thanks, that settles both questions. The shader still translates and validates with the stricter wave64 mask checks in this PR (df392b2; 68c0d2b only adds the spill-restore and per-lane descriptor checks). And on this tree it only runs where the host has 64-lane waves: an RX-class AMD card reporting 64 in its wave range. On a 32–32 adapter, D3D12 refuses a wave-size-64 pipeline, on main as well. So the gain this PR gives in the title is limited to hosts with 64-lane waves until there's 32-lane host support. I've added that to the description's limits. |
fuzeofficlal
left a comment
There was a problem hiding this comment.
Both the ordered spill-restore pair check (structure.rs:984-988) and tracking (mask, high) through Source::Lanes (resource.rs:1702-1796, emit/lower.rs:5643-5657) cleanly resolve the earlier mask-reuse issues.
One liveness bug remains in touches (crates/victory-shader/src/emit/lower.rs:10423-10426):
- While lines
10415-10421check both words ofAtom::Phi(v, h)andAtom::Opaque(v, h)againsttainted, line10423still prunes BDD branches usingself.compared(env, v)on the low word alone. compared(emit/lower.rs:10524-10558) only evaluates the integer compare writing the low SSA wordv. If anAtom::Opaque(lo, hi)has a constant-truelocompare and zerohi(false in lanes 32–63), or anAtom::Phi(lo, hi)joins only on the high word whilelocomes from a compare,compared(env, lo)returnsSome(true)and skips the false branch where another tainted dependency may sit, risking unsounddead_on_edgepruning.
Please restrict compared-based pruning in touches to Atom::Lane and Atom::Uniform (matching can_be at emit/lower.rs:10446-10449) and add a focused regression test.
Local verification on 68c0d2b9: xtask policy (191 files ok), clippy (default & --all-features) clean, CLI tests (18 passed), structure::tests (16 passed, 1 ignored), resource::tests (36 passed), emit::lower::tests (33 passed), and emit::waterfall_tests (6 passed) + emit::synthetic_tests (2 passed) on both RTX 5090 and WARP (cargo fmt --check only flags pre-existing lines in resource.rs).
… register touches, which tells dead_on_edge whether a branch or guard depends on a stale register, followed one branch of a joined or opaque mask when the compare writing its low dword had a known result. The high dword can clear lanes 32-63 (or a join take another edge), so the other branch, where a dependency on the stale value may sit, went unsearched. Only a lane or SCC bit is now decided by its compare (Atom::compare, as can_be already did), and the walk is Bools::reaches over the atoms' values, both dwords of a pair.
|
Thanks @fuzeofficlal. It's fixed in 751722f.
Regression:
With the old pruning (Phi and Opaque decided by their low dword), the test fails. Checks: |
fuzeofficlal
left a comment
There was a problem hiding this comment.
The BDD follow-up resolves my earlier concern: Atom::compare() (crates/victory-shader/src/structure.rs:68-84) only lets a compare's own Lane or Uniform bit select a branch, and either word of a Phi or Opaque mask counts as a dependency in touches.
One Wave64 carry-in regression remains:
structure.rs:1345-1364resolvesv_cndmask_b32select inputs (self.mask(k)) and records carry outputs, but never resolves the carry-in input pair (v_{add,sub,subrev}_co_ci_u32) thatcrates/victory-shader/src/emit/lower.rs:6587-6595now looks up as an exact(lo, hi)pair viamask_bits(lo, hi).- Copying
vcc_hithrough an SGPR and back (e.g.,s_mov_b32 s2, vcc_hi; s_mov_b32 vcc_hi, s2) preserves the mask bits while producing a new high-word SSAValue. When a carry-in instruction is the first consumer of that pair, lowering fails witha carry-in from an untracked mask, whereasmain(63bcca43) succeeds.
Please resolve and register the ordered pair at the carry-in use during structuring and add a positive copy-preserving carry-in regression test alongside the existing cleared-high-word test.
Local verification on 751722ff890c: xtask policy (191 files ok; lower.rs = 519,963 bytes), cargo clippy --workspace --all-targets / --all-features, victory CLI tests (18 passed), structure:: (17 passed, 1 ignored), resource:: (36 passed), emit::lower::tests (33 passed), and emit::waterfall_tests (6 passed) + both Wave64 synthetic_tests cases (2 passed) on both RTX 5090 and VICTORY_WARP=1.
…k pair finds the mask The lowering now looks a carry-in's mask and each edge of a joined mask up by both dwords, but the structurizer resolved neither pair: a carry-in after vcc_hi was copied through an SGPR and back (the same bits, a new value), or a loop's back edge bringing such a pair, was refused as untracked where the low dword alone had found it. The structurizer resolves a carry-in's pair where it is read, as it does a select's, and at the end each edge's pair into a join left as a phi atom, back edges included.
|
Thanks @fuzeofficlal. It reproduced: at 751722f, a wave64 carry-in that was the first reader of a pair whose
Regressions, all positive cases also DXIL-validated:
Checks: |
fuzeofficlal
left a comment
There was a problem hiding this comment.
The vcc_hi copy round trip for carry-in is fixed: structure.rs:1369-1378 now resolves the carry-in pair before recording the new carry-out, and synthetic_tests.rs verifies both the copied and cleared cases.
Two edge cases remain in structure.rs:
- High-word copy placed inside the join block (
structure.rs:1038-1046):mask()only caches a scalar-move alias ifsourceis already inself.masks. If the twos_mov_b32high-word copies sit inside the join block itself right before the firstv_cndmask_b32,(phi_lo, phi_hi)has not been entered inself.masksyet, sojoin()receives(phi_lo, copied_hi)and propagates the post-joincopied_hiback along incoming edges. Resolving the normalizedsourcefirst (and adding a test withCOPIEDin the join block) would close that gap. - Unvalidated constant
Joins fromjoin_args()(structure.rs:573-582, 893, 1143-1158):finish()runsself.join_args()afterwrong_joins()has already exited the fixpoint loop. If a back-edge pair discovered there letsjoin()fold a newJoinusing only forward edges, thatJoinskips back-edge implication validation. Please validate newly created joins alongsidewrong_joins()or defer folding them until their back edges are checked.
Local verification on commit 5ef2e88a7770: emit/lower.rs is 519,963 B (< 524,288 B), cargo run --locked -p xtask -- policy (191 files ok), cargo clippy --workspace --all-targets (default and --all-features), victory (18 passed), structure::tests (17 passed, 1 ignored), resource::tests (36 passed), and emit::lower::tests (33 passed) all pass, as do emit::waterfall_tests (6 passed) and both Wave64 synthetic_tests cases on both RTX 5090 and VICTORY_WARP=1.
What
A wave64 lane mask copied into two scalars one dword at a time and later copied back into EXEC used to reach the structurizer as an unknown pair, so the mask was opaque:
Lanes::masknow follows each word back through its chain ofs_mov_b32copies. When both words still come from a known mask, in their original order, the pair is that mask. If the words are swapped or mixed (for examples3 = vcc_lo), the pair stays unknown.Wave64 mask uses read both dwords (later commits)
Structured::finishkeysmasksandotherby the low dword only, and several wave64 uses resolved their mask by that low dword. Afters_mov_b32 s3, 0, a mask read froms[2:3]could therefore get the earlier pair's predicate for lanes 32-63. This is older than this PR: on main,v_cndmask_b32 ..., vccafters_mov_b32 vcc_hi, 0lowers the same way. This PR's copy chain adds another way to reach it. Each use now goes by the full(low, high)pair:v_cndmask_b32: looks up the exact pair the structurizer tracked for it (Structured::select_bits, over the wave64 pairs ofmasksand a newpaired_other).v_add/sub/subrev_co_ci_u32carry-in: looks up the exact pair inmasks(Structured::mask_bits). The structurizer resolves the carry-in's pair where it is read, as it does for a select's. A carry-in that is the first reader of a pair made of copies (e.g.vcc_hicopied through an SGPR and back) therefore finds the mask.Atom::OpaqueandAtom::Phi: carry the(low, high)pair, not one value (the same value twice in wave32).pair_high, now removed), so a pair copied withs_mov_b64after its high dword changed read the old high dword.avoids.Source::Lanes): records the select's high dword alongside its low one. Tracking and lowering look the pair up withmask_bits.Lanes::masknow checks that the writing instruction's (low, high) destination is the pair asked for.touches: the check behinddead_on_edge. It counts either dword of aPhi/Opaqueatom as a dependency. It also lets a compare's known result settle only that compare's own lane or SCC bit (Atom::compare, ascan_bedoes); before, it also settled a joined or opaque mask whose low dword the compare wrote. The walk is nowBools::reaches.Wave32 is unchanged.
Where it came from
Ported from DexNom's fork: DexNom/VIctory@76749ce ("Recover voxel visibility shader when a wave64 mask is restored from scalar copies"). The first commit keeps DexNom as author. The fork's
BENCHMARK.mdsays this recovered one previously refused pixel shader in the title's archive, a voxel visibility shader.The change only needed adapting to this tree's
mask(), which now also follows masks spilled to VGPR lanes (restored). The copy-chain check runs before that.Tested
structure::tests::split_scalar_copies_across_a_branch_preserve_both_words_of_a_wave64_mask(DexNom's). It fails on main and passes with the change. It covers the intact pair and a pair whose high word is a copy of the low one, which must stay distinct.emit::synthetic_tests::a_wave64_select_after_its_high_word_changes_is_not_the_old_mask, one wave64 shader: a compare copied intos[2:3]word by word and read by onev_cndmask_b32, thens3written again and read by a second one. Ifs3is copied fromvcc_hiagain, the shader lowers and passes DXIL validation. Ifs3is set to 0, the second select is the opaque pair ofs2and the news3, which this shader (without console lanes) refuses. At the first commit alone, it lowered with the compare's predicate.structure::tests::a_wave64_pair_copied_after_its_high_dword_changes_keeps_the_new_one:s_mov_b64 s[2:3], vcc,s_mov_b32 s3, 0,s_mov_b64 s[4:5], s[2:3]and a select ons[4:5]. The select's mask isAtom::Opaque(s2, cleared s3), so lanes 32-63 read the cleared word.emit::synthetic_tests::wave64_joins_and_carry_ins_read_both_dwords_of_their_mask:s3then cleared.s_mov_b32 vcc_hi, 0.s[2:3]each pass and reads it at its header.structure::tests::a_wave64_mask_spilled_and_read_back_swapped_is_not_the_mask:s_mov_b64 s[4:5], vccspilled withv_writelane_b32to lanes 0 and 1, then read back intos[6:7]. In order, it is the compare. Swapped, it is not; without the destination check, the swapped pair was the compare.resource::tests::a_wave64_descriptor_picked_after_the_high_dword_changes_names_the_new_oneandemit::waterfall_tests::a_wave64_descriptor_picked_after_the_high_dword_changes_is_refused:v_cndmask_b32on VCC.s_mov_b32 vcc_hi, 0after the compare, or withvcc_hicopied throughs2and back: the pick's pair is the compare, and the shader lowers and validates.structure::tests::only_a_compare_s_own_bit_is_decided_by_the_comparetests the walktouchesmakes (Bools::reacheswithAtom::compareandAtom::values) on a constructed diagram:loknown true.Lane(lo), the tainted branch is pruned.Opaque(lo, hi)andPhi(lo, hi), it is still searched, and a taintedhialone counts.cargo test -p victory-shader --libon Windows 11 (NVIDIA, WARP,dxil.dll), withheap_direct_texture_reads_mark_their_heap_indexskipped because it crashes the process: 306 passed, 3 failed, 3 ignored. The 3 failures aregathers_at_a_level_match_the_hardwareand twostride_testsheap-buffer reads, known failures on main on this machine.cargo test -p victory: all passed.cargo clippy --workspace --all-targets -- -D warnings(default and--all-features): clean.cargo xtask policy: 191 files ok.Not tested
I haven't run the title's shader archive or the game, so the voxel visibility shader itself isn't checked here.
Wave64 pipelines don't run on this machine's NVIDIA adapter or on WARP. The new tests check translation and the structurizer's masks, not results on a GPU.
@Venom2054 checked a cascade-shadow compute shader at df392b2 on an RTX 5070 Ti: it translates and passes DXIL validation, but D3D12 refuses its pipeline (wave size 64 on an adapter whose range is 32–32), as it does on main. On this tree, the programs this unblocks run only on hosts with 64-lane waves.