Skip to content

Shaders: a wave64 mask restored from word-by-word scalar copies is the same mask - #2816

Open
0xkn0wh3r3 wants to merge 7 commits into
Marvisak:mainfrom
0xkn0wh3r3:shader/wave64-mask-from-split-copies
Open

0xkn0wh3r3 wants to merge 7 commits into
Marvisak:mainfrom
0xkn0wh3r3:shader/wave64-mask-from-split-copies

Conversation

@0xkn0wh3r3

@0xkn0wh3r3 0xkn0wh3r3 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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:

v_cmp_eq_u32  vcc, 0, v0
s_mov_b32     s2, vcc_lo
s_mov_b32     s3, vcc_hi
...
s_mov_b32     s4, s2
s_mov_b32     s5, s3
s_mov_b64     exec, s[4:5]

Lanes::mask now follows each word back through its chain of s_mov_b32 copies. 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 example s3 = vcc_lo), the pair stays unknown.

Wave64 mask uses read both dwords (later commits)

Structured::finish keys masks and other by the low dword only, and several wave64 uses resolved their mask by that low dword. After s_mov_b32 s3, 0, a mask read from s[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 ..., vcc after s_mov_b32 vcc_hi, 0 lowers 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 of masks and a new paired_other).
  • v_add/sub/subrev_co_ci_u32 carry-in: looks up the exact pair in masks (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_hi copied through an SGPR and back) therefore finds the mask.
  • Atom::Opaque and Atom::Phi: carry the (low, high) pair, not one value (the same value twice in wave32).
    • Opaque, lowered: reads exactly those two dwords. Before, the high dword came from the low dword's definition (pair_high, now removed), so a pair copied with s_mov_b64 after its high dword changed read the old high dword.
    • Phi, lowered: builds each incoming edge's mask from that edge's pair of arguments. Before, it used the low dword's phi argument only, as did the skip-edge check and the join links in avoids.
  • Descriptor picked per lane (Source::Lanes): records the select's high dword alongside its low one. Tracking and lowering look the pair up with mask_bits.
  • A pair read back from VGPR spill lanes: only the mask written with the same pair is the mask; reversed words aren't. The fallback in Lanes::mask now checks that the writing instruction's (low, high) destination is the pair asked for.
  • touches: the check behind dead_on_edge. It counts either dword of a Phi/Opaque atom as a dependency. It also lets a compare's known result settle only that compare's own lane or SCC bit (Atom::compare, as can_be does); before, it also settled a joined or opaque mask whose low dword the compare wrote. The walk is now Bools::reaches.
  • Joins left as a phi atom: at the end of structuring, every edge's pair into the join is resolved, including a loop's back edge, which isn't followed where the join is met. A back edge bringing a copied pair is then found by both dwords.
  • Pairs that aren't tracked: refused as before ("a select on an untracked mask", "a carry-in from an untracked mask", "a lane-mask join of an untracked mask", "a descriptor chosen per lane by an untracked mask", or "an opaque lane mask" without console lanes).

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.md says 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 into s[2:3] word by word and read by one v_cndmask_b32, then s3 written again and read by a second one. If s3 is copied from vcc_hi again, the shader lowers and passes DXIL validation. If s3 is set to 0, the second select is the opaque pair of s2 and the new s3, 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 on s[4:5]. The select's mask is Atom::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:
    • Join: a mask joined at a block entry whose incoming pair on one edge is a compare copy with s3 then cleared.
    • Carry-in: a carry-in read after s_mov_b32 vcc_hi, 0.
    • Loop: a uniform loop that narrows s[2:3] each pass and reads it at its header.
    • Results: with the high dword kept, or copied through another SGPR and back (same bits, a new value), the join, the loop and the carry-in all lower and pass DXIL validation. At 751722f, the copied loop and the copied carry-in were refused; main translates both. With the high dword cleared, the join and the carry-in are refused; at d489540 both lowered with the old high dword.
  • structure::tests::a_wave64_mask_spilled_and_read_back_swapped_is_not_the_mask: s_mov_b64 s[4:5], vcc spilled with v_writelane_b32 to lanes 0 and 1, then read back into s[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_one and emit::waterfall_tests::a_wave64_descriptor_picked_after_the_high_dword_changes_is_refused:
    • Shader: a wave64 waterfall over one of two image descriptors picked per lane by v_cndmask_b32 on VCC.
    • Without s_mov_b32 vcc_hi, 0 after the compare, or with vcc_hi copied through s2 and back: the pick's pair is the compare, and the shader lowers and validates.
    • With it: the pick names the cleared high dword, and lowering refuses it. With the old low-dword lookup it lowered with the compare's mask.
  • structure::tests::only_a_compare_s_own_bit_is_decided_by_the_compare tests the walk touches makes (Bools::reaches with Atom::compare and Atom::values) on a constructed diagram:
    • Shape: a decided atom OR a tainted lane bit, with the compare writing lo known true.
    • Lane bit: for Lane(lo), the tainted branch is pruned.
    • Joined or opaque mask: for Opaque(lo, hi) and Phi(lo, hi), it is still searched, and a tainted hi alone counts.
    • Old rule: with the earlier low-dword pruning, the test fails.
  • cargo test -p victory-shader --lib on Windows 11 (NVIDIA, WARP, dxil.dll), with heap_direct_texture_reads_mark_their_heap_index skipped because it crashes the process: 306 passed, 3 failed, 3 ignored. The 3 failures are gathers_at_a_level_match_the_hardware and two stride_tests heap-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.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Verified 306e73c4 on Windows 11 (RTX 5090 + WARP + Windows SDK 10.0.26100.0 dxil.dll):

@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.

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.

@Machad3x

Copy link
Copy Markdown

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).

JMAN730 added a commit to JMAN730/project-flamingo that referenced this pull request Nov 25, 2026
@0xkn0wh3r3
0xkn0wh3r3 force-pushed the shader/wave64-mask-from-split-copies branch from 306e73c to 7fafdf3 Compare November 25, 2026 23:07
0xkn0wh3r3 added a commit to 0xkn0wh3r3/VIctory that referenced this pull request Nov 26, 2026

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Lanes::mask (structure.rs) now follows each word of a wave64 mask back through s_mov_b32 copies (move_source). When both words still come from a known mask, in order, the pair is that mask. Swapped or mixed words stay opaque, and the test checks both cases. It only reuses masks already in self.masks (EXEC/VALU-derived), so it cannot invent a mask, and the copy chain follows SSA defs, so it terminates. It is a port of DexNom's fork commit, with authorship kept.

CI: passing.


Generated by Claude Code

@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:

  • cargo test -p victory-shader --lib structure::tests: 14 passed, 0 failed, 1 ignored (including new split_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).

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 26, 2026
@Venom2054

Copy link
Copy Markdown

Tested the mask change at 7fafdf36 on Windows 11 with an RTX 5070 Ti. I applied the translator change to Zayfway/victory-fork@3e760ed and added only the resulting cache entry for one alpha-tested cascade-shadow compute shader to a private copy of the warm cache. The game executable and other shader entries were the same as in the control.

The stock translator refused this shader at 0x1eb4. With the change it passed Windows SDK DXIL validation and D3D12 created its compute pipeline. In the Series S prologue, both the second and sixth frames after world entry logged a dropped command for this shader in the control. The treatment recorded two ExecuteIndirect dispatch commands in each of those frames. The indirect group counts remain unmeasured. In the loaded-window interval the control reported about one drop per frame for this shader, and the treatment logged none. The run completed without device loss.

The localized flicker at the courtyard tables remained visible with this shader enabled.

Mdemets pushed a commit to Mdemets/project-flamingo that referenced this pull request Nov 26, 2026
…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 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.

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.

DexNom and others added 2 commits November 26, 2026 05:09
…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.
@0xkn0wh3r3
0xkn0wh3r3 force-pushed the shader/wave64-mask-from-split-copies branch from 7fafdf3 to d489540 Compare November 26, 2026 10:14
@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

Thanks @fuzeofficlal, good catch. It reproduced. At 7fafdf3, a wave64 shader that copies a compare into s[2:3] word by word, selects on it, sets s3 to 0 with s_mov_b32 s3, 0 and selects on s[2:3] again lowered both selects with the compare's predicate. The low-word keying is older than this PR: on main, v_cndmask_b32 ..., vcc after s_mov_b32 vcc_hi, 0 lowers the same way. The copy chain here only adds another way to reach it.

Fixed in d489540 (the branch is also rebased onto 63bcca4):

  • Structured keeps the wave64 pairs of other (paired_other) as it already did for masks. select_bits(lo, hi) looks a select's mask up by the exact (lo, hi) pair the structurizer tracked at that instruction.
  • v_cndmask_b32 in wave64 reads both dwords and uses that lookup. An opaque mask is lowered with its low dword's own high dword, so the select doesn't take one whose high dword is a different value either. Anything else is refused as "a select on an untracked mask", the select's existing outcome for an unknown mask. Wave32 is unchanged.
  • New test emit::synthetic_tests::a_wave64_select_after_its_high_word_changes_is_not_the_old_mask: one shader with both selects. When s3 is copied back from vcc_hi, it lowers and passes DXIL validation. When s3 is set to 0, translation is refused. The test fails at 7fafdf3. The PR's copy-chain code and its test are unchanged.

The _co_ci_u32 carry-in still looks its mask up by the low dword. I left it as is.

@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.

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): When s[4:5] is copied from an opaque pair s[2:3] with s_mov_b64 after s3 is overwritten (e.g., cleared to 0), s[4:5]'s paired_other predicate inherits Atom::Opaque(src_lo) rather than Atom::Opaque(dst_lo). v_cndmask_b32 only checks support(m).contains(&Atom::Opaque(lo)) for dst_lo, misses Opaque(src_lo), and Atom::Opaque(src_lo) later reads pair_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::Phi mask materialization (structure.rs:1021-1075, emit/lower.rs:2575-2587) and v_{add,sub,subrev}_co_ci_u32 carry-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): clean
  • structure::tests / emit::lower::tests: 14 passed (1 ignored) / 33 passed, 0 failed
  • a_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.
@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

Thanks @fuzeofficlal. Both points reproduced, and both are fixed in 39a3718.

  1. Opaque pair forwarding via s_mov_b64: Atom::Opaque now carries the (low, high) pair it was made for. Lowering reads those two dwords, and pair_high is gone. s_mov_b64 s[4:5], s[2:3] after s_mov_b32 s3, 0 therefore gives Opaque(s2, cleared s3), and lanes 32-63 read the cleared word. That made the select-side guard from d489540 unnecessary, so I removed it. Regression: structure::tests::a_wave64_pair_copied_after_its_high_dword_changes_keeps_the_new_one asserts the select's mask is exactly Atom::Opaque(s2, cleared s3).

  2. Low-word paths:

    • Phi: Atom::Phi carries the joined (low, high) pair. Its lowering builds each incoming edge from that edge's pair of phi arguments, looked up by pair. The skip-edge check (no_mask_lane_on_edge) and the join links in avoids use the same pairs.
    • Carry-in: the v_{add,sub,subrev}_co_ci_u32 carry-in reads both dwords and looks the pair up in masks.
    • Regression: emit::synthetic_tests::wave64_joins_and_carry_ins_read_both_dwords_of_their_mask. A join whose incoming pair on one edge has s3 cleared, and a carry-in after s_mov_b32 vcc_hi, 0, both lower without the clearing write and are refused with it. At d489540 both lowered with the old high dword.

The round-one select test still passes; its second case is now refused as an opaque pair of s2 and the new s3, not as an untracked mask. The rustfmt --check hunks at structure.rs 491 and 895 are fixed by hand.

cargo test -p victory-shader --lib: 302 passed; only the 3 known failures (the gathers test and two stride_tests) fail, with the crashing heap test skipped. Clippy is clean (default and --all-features) and cargo xtask policy passes. Wave64 doesn't run on this machine's GPU or on WARP, so the tests check translation and the structurizer, not GPU results.

Atom::Opaque now holds a pair, so the pattern in translate's mask count matches any fields.

@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.

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:

  1. Reversed spill-restore pairs in Lanes::mask (crates/victory-shader/src/structure.rs:971-989): when (lo, hi) comes from restored in swapped order (e.g., s5 and s4 defined by s_mov_b64 s[4:5], vcc, spilled, and read back in reverse), the (None, _) fallback only checks defs.get(&k.0) == defs.get(&k.1) and returns sop(i).dst without verifying that (k.0, k.1) matches the definition's (low, high) register order.
  2. Descriptor selection via Source::Lanes (crates/victory-shader/src/resource.rs:1708-1783, crates/victory-shader/src/emit/lower.rs:5639-5663): lane_picked only records the low SGPR (Reg::S(code)) of v_cndmask_b32, and lane_select / lane_heap_index look up masks.get(&mask) by that low dword alone, bypassing select_bits(lo, hi) when the high dword is overwritten.
  3. 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, so dead_on_edge misses 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) and cargo 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.
@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

Thanks @fuzeofficlal. All three are fixed in 68c0d2b, on top of df392b2.

  1. Reversed spill-restore pairs: reproduced. The (None, _) fallback in Lanes::mask now takes the writing instruction's destination mask only when that destination's (low, high) key is the pair asked for (.filter(|d| d.0 == k)). Reversed words now get an opaque pair of their own.

    • Regression: structure::tests::a_wave64_mask_spilled_and_read_back_swapped_is_not_the_mask. s_mov_b64 s[4:5], vcc is spilled with v_writelane_b32 to lanes 0 and 1 and read back into s[6:7]. In order, it is the compare; swapped, it is not.
    • Without the filter, the swapped case was the compare.
  2. Descriptor selection via Source::Lanes: reproduced. Source::Lanes gains high: Option<Value>. lane_picked records both words the v_cndmask_b32 reads (the high one in wave64 only). lane_select and lane_heap_index look the pair up with Structured::mask_bits(mask, high).

    • Regression on the tracking side: resource::tests::a_wave64_descriptor_picked_after_the_high_dword_changes_names_the_new_one. The program is a wave64 version of WATERFALL_PICKED_WAVE32 (picked_wave64), with lanes below 48 taking the first descriptor and the WATERFALL_FETCHED_WAVE64 loop.
    • Regression on the lowering side: emit::waterfall_tests::a_wave64_descriptor_picked_after_the_high_dword_changes_is_refused. Without s_mov_b32 vcc_hi, 0, the pick is the compare and the shader lowers. With it, the pick names the cleared high word and lowering refuses it as "a descriptor chosen per lane by an untracked mask".
    • With the old low-word lookup, the lowering test fails: the shader lowered with the compare's mask. waterfall_tests::build is split so this test can lower in wave64 (lower_wave).
  3. High-word liveness in touches: Atom::Phi and Atom::Opaque now give both words, and either one being tainted counts. There is no test for this one: building a synthetic wave64 branch whose only tainted word is the high one isn't easy.

Checks: cargo clippy --workspace --all-targets -- -D warnings, with and without --all-features, is clean. cargo xtask policy passes. cargo test -p victory passes. cargo test -p victory-shader --lib: 305 passed; only the 3 known failures (the gathers test and two stride_tests) fail, with the crashing heap test skipped.

@Venom2054

Copy link
Copy Markdown

Follow-up to my earlier fork result. I tested the same cascade-shadow compute shader at upstream PR revision df392b22 and the base translator (63bcca43). I used separate build directories, the Windows SDK DXIL validator, and the D3D12 debug layer on an RTX 5070 Ti.

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.

@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

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 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.

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-10421 check both words of Atom::Phi(v, h) and Atom::Opaque(v, h) against tainted, line 10423 still prunes BDD branches using self.compared(env, v) on the low word alone.
  • compared (emit/lower.rs:10524-10558) only evaluates the integer compare writing the low SSA word v. If an Atom::Opaque(lo, hi) has a constant-true lo compare and zero hi (false in lanes 32–63), or an Atom::Phi(lo, hi) joins only on the high word while lo comes from a compare, compared(env, lo) returns Some(true) and skips the false branch where another tainted dependency may sit, risking unsound dead_on_edge pruning.

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.
@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

Thanks @fuzeofficlal. It's fixed in 751722f.

touches no longer prunes on compared(env, lo) for a joined or opaque mask. A compare's known result now settles only that compare's own bit, Atom::Lane or Atom::Uniform. The rule is Atom::compare(), which can_be now uses as well, so the two match. Atom::Phi and Atom::Opaque are walked both ways, and either of their dwords being tainted counts (Atom::values()). The walk itself moved into Bools::reaches(b, decide, hit), and touches is now those two closures over it.

Regression: structure::tests::only_a_compare_s_own_bit_is_decided_by_the_compare, a unit test on a constructed diagram. The diagram is x OR Lane(t), with t tainted and the compare writing lo known true:

  • for x = Lane(lo), the tainted branch is pruned;
  • for x = Opaque(lo, hi) and x = Phi(lo, hi), it is searched;
  • a tainted hi alone is found for both pair atoms.

With the old pruning (Phi and Opaque decided by their low dword), the test fails.

Checks: cargo clippy --workspace --all-targets -- -D warnings, with and without --all-features, is clean. cargo xtask policy passes. cargo test -p victory passes. cargo test -p victory-shader --lib: 306 passed; only the 3 known failures (the gathers test and two stride_tests) fail, with the crashing heap test skipped.

@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.

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-1364 resolves v_cndmask_b32 select inputs (self.mask(k)) and records carry outputs, but never resolves the carry-in input pair (v_{add,sub,subrev}_co_ci_u32) that crates/victory-shader/src/emit/lower.rs:6587-6595 now looks up as an exact (lo, hi) pair via mask_bits(lo, hi).
  • Copying vcc_hi through 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 SSA Value. When a carry-in instruction is the first consumer of that pair, lowering fails with a carry-in from an untracked mask, whereas main (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.
@0xkn0wh3r3

Copy link
Copy Markdown
Contributor Author

Thanks @fuzeofficlal. It reproduced: at 751722f, a wave64 carry-in that was the first reader of a pair whose vcc_hi had been copied through an SGPR and back was refused with "a carry-in from an untracked mask", while main translates it. It's fixed in 5ef2e88.

  • Carry-in: Lanes::step now resolves the carry-in pair of v_{add,sub,subrev}_co_ci_u32 where it is read, the same way it resolves a select's mask. A pair of copies is followed back by move_source and registered as the mask.
  • Select and descriptor pick: I checked both for the same first-reader gap, and neither has it.
    • The pick is itself a v_cndmask_b32, so its pair is already resolved at the select.
    • Both copy cases already pass at 751722f. The round-one select test's kept case reads a pair whose s3 was copied from vcc_hi, and the pick tests now include a vcc_hi copied through s2 and back.
  • Joins: I found the same gap on a loop's back edge. A join left as a phi atom only had its forward-edge pairs resolved, so lowering the join couldn't find a back edge bringing a copied pair. Main finds that by the low dword. finish now first resolves every edge's pair of each such join (join_args).

Regressions, all positive cases also DXIL-validated:

  • emit::synthetic_tests::wave64_joins_and_carry_ins_read_both_dwords_of_their_mask. A forward join, a uniform loop that narrows s[2:3] each pass (read at its header), and a carry-in all lower with the high dword kept or copied through another SGPR and back. The cleared-high-dword refusals for the join and the carry-in are unchanged. Without the new resolution, the copied carry-in and the copied loop are refused; on main (63bcca4) both translate.
  • resource::tests::a_wave64_descriptor_picked_after_the_high_dword_changes_names_the_new_one and emit::waterfall_tests::a_wave64_descriptor_picked_after_the_high_dword_changes_is_refused now also cover vcc_hi copied through s2 and back: the pick is the compare, and the shader lowers. The cleared case is still refused.

Checks: cargo clippy --workspace --all-targets -- -D warnings, with and without --all-features, is clean. cargo xtask policy passes. cargo test -p victory passes. cargo test -p victory-shader --lib: 306 passed; only the 3 known failures (the gathers test and two stride_tests) fail, with the crashing heap test skipped.

@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.

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:

  1. High-word copy placed inside the join block (structure.rs:1038-1046): mask() only caches a scalar-move alias if source is already in self.masks. If the two s_mov_b32 high-word copies sit inside the join block itself right before the first v_cndmask_b32, (phi_lo, phi_hi) has not been entered in self.masks yet, so join() receives (phi_lo, copied_hi) and propagates the post-join copied_hi back along incoming edges. Resolving the normalized source first (and adding a test with COPIED in the join block) would close that gap.
  2. Unvalidated constant Joins from join_args() (structure.rs:573-582, 893, 1143-1158): finish() runs self.join_args() after wrong_joins() has already exited the fixpoint loop. If a back-edge pair discovered there lets join() fold a new Join using only forward edges, that Join skips back-edge implication validation. Please validate newly created joins alongside wrong_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.

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