Skip to content

Shaders: drop a wave64 mask whose low dword is seen with two high dwords - #660

Open
coderanonymus wants to merge 1 commit into
Marvisak:mainfrom
coderanonymus:wave64-mask-pairs
Open

coderanonymus wants to merge 1 commit into
Marvisak:mainfrom
coderanonymus:wave64-mask-pairs

Conversation

@coderanonymus

@coderanonymus coderanonymus commented Nov 24, 2026 •

Copy link
Copy Markdown

What

In wave64 a lane mask is an SGPR pair. Structurisation tracks masks by the pair, but it exports Structured::masks, highs and active by the low dword alone. When code rewrites only the high dword, two pairs share one low dword, and lowering finds the wrong pair's mask.

  • Select and carry-in. v_cmp_eq_u32 vcc, 0, v0; s_mov_b32 vcc_hi, 0; v_cndmask_b32 v1, 0, v2, vcc: masks[vcc_lo] was still the compare. The select picked src1 by the compare's bit in lanes 32-63, where the hardware has VCC_HI = 0 and picks src0. emit returned Ok. v_add_co_ci_u32 reads its carry-in the same way.
  • EXEC. s_mov_b32 exec_hi, 0; v_mov_b32 v1, 7; buffer_store_dword ...: the guard is Opaque(exec_lo), and lowering took the high dword of EXEC on entry. Lanes 32-63 stored, although the new EXEC disables them. emit returned Ok. With an s_mov_b64 s[2:3], exec first, main already refuses the same program ("a lane mask whose lanes depend on its own value").

The code changes are in structure.rs:

  • finish leaves a low dword out of masks, highs and active when it was seen with two high dwords. Lowering then refuses what reads it ("a select on an untracked mask", "a carry-in from an untracked mask", or for the EXEC case "a hardware SGPR input") when that low dword has no value of its own (EXEC, compare results). See "Still open" for one that holds a number.
  • step records the carry-in pair of v_*_co_ci_u32. Before, it was never recorded, so the check above could not see it. It is only recorded, not resolved through mask as the v_cndmask_b32 condition is, so a carry-in on a join of compares is still refused, as on main.
  • join records the pairs it takes over back edges. Lowering looks phi arguments up by low dword too, and those pairs never go through mask.

emit/mod.rs gets a stores_v1 test helper, two tests, and a doc comment update.

Still open, marked as a shortcut: in mask: Atom::Opaque names its pair by the low dword. Lowering takes the high dword written beside it. This is wrong for a mask whose halves were written apart when the low dword holds a number: two s_mov_b32, or s_mov_b64 s[4:5], k then s_mov_b32 s5, 0, where lowering takes the stale s5. So the PR does not refuse every wave64 mask whose high dword was rewritten alone. When the low dword has no value (EXEC, compare results), lowering now refuses. The full fix is an opaque atom that carries both dwords, which needs a change to emit/lower.rs. I left that out of this PR.

In wave32 both halves of a key are the same value, so no low dword is ever split and no mask is dropped. Wave32 is unchanged.

Found in review, not fixed here (it is on main too): a select on a join of compares after a divergent branch translates wrongly. In v_cmp_eq_u32 vcc, 0, v0; s_andn1_saveexec_b64 s[2:3], vcc; s_cbranch_execz; v_cmp_eq_u32 vcc, 1, v0; s_or_b64 exec, exec, s[2:3]; v_cndmask_b32 v1, 0, v2, vcc, the join resolves to a per-lane phi. Lane 0 skips the inner block, so the phi gives it the first compare's bit. The hardware has 0 there whenever some lane runs the inner block, because VOPC clears the bits of inactive lanes. This is why the carry-in is only recorded: resolving it the same way turned main's refusal of the carry-in version into this wrong code. Compiled code is unlikely to hit it: LLVM merges an i1 across such a join with s_andn2 / s_and / s_or on EXEC, which the per-lane join already gets right. A check at the join that refused the raw form also refused LLVM's own if/else lowering and uniform branches inside a divergent if, so a fix needs a different model of mask joins, not a check. I left it.

Overlap with open PRs:

Rebased on main (ffe310e)

main now builds paired_masks (exact pairs, for the waterfall proof) from the same map. finish takes it before dropping split low dwords: an exact pair stays right when its low dword is split, so the waterfall proof sees the same pairs as before. The stores_v1 test helper now passes the structure to resource::track, which takes it since main changed.

After the rebase: cargo test -p victory-shader 100 passed; cargo clippy -p victory-shader --all-targets --all-features --target x86_64-pc-windows-msvc -- -D warnings clean.

Verification

All run on Linux. The target dir is shared, so I passed --config profile.dev.package.<crate>.codegen-units=251 (for each workspace crate built) to force a fresh build of this worktree.

  • New tests, on unmodified main (tests only):
    cargo test -p victory-shader --lib -- a_mask_ a_carry_in
    • structure::tests::a_mask_whose_high_dword_is_rewritten_is_not_the_compare FAILED: 0x2020480: vcc_hi was zeroed after the compare, left and right both Some((Lane(Value(5)), Bool(0), Bool(1))). The carry-in program fails the same way.
    • structure::tests::a_mask_joined_over_a_back_edge_with_a_rewritten_high_dword_is_not_the_compare FAILED: left and right both Some((Lane(Value(16)), Bool(0), Bool(1))).
    • emit::tests::a_mask_whose_high_dword_is_rewritten_is_refused FAILED: the vcc_hi select program translated, and so did the vcc_hi carry-in program (checked on its own). The exec_hi program also translated (Ok). Its last program (exec_hi after s_mov_b64 s[2:3], exec) already refuses on main, so it adds no regression coverage; it stays as a guard.
    • emit::tests::a_carry_in_from_a_divergent_join_is_refused passes on main (it refuses with "a carry-in from an untracked mask"). It fails if the carry-in goes through mask like the select (emit returns Ok), which is what it guards.
  • emit::tests::an_exec_high_write_does_not_overflow_the_stack (existing) now goes another way: the guard's low dword is split and has no mask, so the v_mov_b32 no longer needs the mask's own value. emit still returns Ok, since nothing with a side effect reads it. I updated its doc comment.
  • With the fix: all 4 new tests pass. If I remove the back-edge recording, the loop test fails. If I remove the carry-in recording, the structure and emit carry-in cases fail.
  • cargo test -p victory-formats -p victory-shader -p victory-pipeline -p victory-core -p xtask: all pass (victory-shader 60 passed).
  • cargo clippy -p victory-shader --tests --target x86_64-pc-windows-msvc: no warnings.
  • rustfmt --edition 2024 --check on both touched files: clean.

Not run: Windows-only crates and tests, and no game shaders. How often console code writes one half of a mask outside a whole-wave section was not measured.

No game data used.

🤖 Generated with Claude Code

@LAKSHYA-cmd-tech LAKSHYA-cmd-tech 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! Tested on Windows 11 with MSVC Rust 1.98.1. All 73 tests in �ictory-shader passed cleanly, including the new unit tests verifying that rewriting the high dword of an SGPR pair drops stale wave64 lane masks and prevents silent miscompilations on split mask pairs.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

I tested the remaining opaque numeric-mask case on d92ebe9. A synthetic Wave64 compute program writes two scalar words, rewrites only the high word, and ANDs the pair into EXEC before a per-lane output store. Both baseline shaders pass SDK DXIL validation. On the Radeon iGPU the readback matches 32/64 lanes in each case, because lanes 32–63 use the old high word.

Keeping both SSA word identities in the opaque guard gives 64/64 expected values for both variants. At lanes 31/32, clearing the high word changes 7/7 to 7/0, and setting it changes 0/0 to 0/7. Both candidates pass SDK DXIL validation. The serial shader suite reports 74 passed, including the earlier refusal tests and a new structural test distinguishing two guards with the same low word.

Incremental source change on this head

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

Merges cleanly with main; macOS arm64, rustc 1.98.1, cargo test -p victory-shader --locked with the PR merged into main 8e79c80 → 74 passed / 0 failed / 1 ignored.

This fixes a real soundness hole: a wave64 mask whose low dword is read with two different high dwords (one rewritten alone via s_mov_b32 exec_hi/vcc_hi) used to make lowering recurse until stack overflow. The seen set in Lanes now records pairs mask doesn't resolve (carry-ins, back-edge joins), and finish drops any low dword observed with two high dwords from masks/active, so lowering refuses the instruction instead of taking the wrong pair's lanes 32-63.

I checked the carry-in path: _co_ci_ masks are recorded in seen rather than resolved through mask(), which is what keeps a divergent join a refusal instead of an unsound per-lane phi — the comment explains exactly that. Tests cover the direct rewrite, the rewritten EXEC high dword with a saved EXEC, and the loop back-edge join. The two new structure.rs tests assert the mask is not the compare's lane atom, which is the right invariant.

One nit: the shortcut comment at mask() (~line 665) says "Give the atom both dwords when such code turns up" — fine as a documented limitation, and the refusal path makes it safe rather than wrong.

@rpmaster2

rpmaster2 commented Nov 24, 2026 •

Copy link
Copy Markdown

Independent test on RDNA4: this PR alone, on main @58fed37, diag build, DRED on, 300 s from an identical restored save.

Counting unique stage/hash pairs whose pipelines are dropped with shader does not translate:

build unique refused of which graphic stages
main (two runs) 472, 440 65 (26 mesh, 34 pixel, 5 vertex)
main + this PR 33 0 - all compute

Both mask reasons disappear entirely with this PR: a select on an untracked mask (95-96 log lines on main) and a lane mask holding lanes elsewhere read as data (80-81) go to 0. Remaining reasons here: a descriptor read only as data (24 lines), a structured buffer of unknown stride (8), a descriptor's address or format read as data (1).

Interesting detail: the 33 hashes left are exactly the same set that #585 alone leaves, even though #585 only touches emit/lower.rs and this PR touches emit/mod.rs and structure.rs. So on this title the two PRs each unblock the same set from different sides; either one is enough to get every pixel/vertex/mesh shader translated.

What this PR does not change: the world is still black (6 black-frame warnings, ~1 fps, 150 draws/frame) and the run still loses the device at frame 1143 with the same page fault ... inside freed [texture ...] signature that every main run shows. So it is not a stability fix, only a translation one.

Note on method: my first run of this PR stayed in the front end and measured nothing (16 draws/frame, 30 fps); the numbers above are from a retry with later key timings that reached the world. Not tested: cargo test, clippy, other GPUs.

No game data: counts, shader hashes, driver version and error strings only.

AI-assisted: written by DeepSeek (OpenCode harness) from my run logs and observations; the hardware, the dump and the runs are mine.

@qntic

qntic commented Nov 24, 2026

Copy link
Copy Markdown

tested on rx 6600 xt. the select pattern after vcc_hi = 0 silently produces incorrect values in 16 out of 64 lanes on main. with this pr the unsafe translation is correctly rejected instead of producing incorrect results.

hardware tests:
https://github.com/qntic/VIctory/tree/hwtest/compute-lanes

@rpmaster2

rpmaster2 commented Nov 24, 2026 •

Copy link
Copy Markdown

Correction to my earlier comment: offline translation shows no change from this PR, so the in-game difference I reported is not evidence for it and I withdraw that claim.

victory translate --pipelines over the whole dump (26,148 programs, same input per build):

build vertex+pixel compute mesh
main @58fed37 12,236 of 14,013 1,440 of 2,581 8,163 of 8,506
main + this PR 12,236 1,440 8,163

The in-game "440 -> 33 refused" I posted came from runs that covered different amounts of world content (a main run reached 2,220 draws/frame at frame 1500, this PR's run 152 draws/frame at frame 1171), so fewer shaders were requested and fewer failures appear. Not something this PR can take credit for.

No regression either: the same programs emit, and the one world run I have ended the same way as main (device removed at frame 1143, page fault inside a freed texture), so on this host it changes neither coverage nor stability.

If your intent is the wave64 mask pairs, the remaining mask refusals offline are a lane mask holding lanes elsewhere read as data (285 programs) and a lane mask read as data (266), plus 22 a lane-mask join of an untracked mask - so there is still room, but this title's failing set is dominated by other reasons.

No game data: counts, driver version and error strings only.

AI-assisted: written by DeepSeek (OpenCode harness) from my run logs and observations; the hardware, the dump and the runs are mine.

@molodayamama

Copy link
Copy Markdown

Independent Windows check of d92ebe9c9dc9eda57fdf54681a2b21f4058a4265 (Windows 10 22H2 19045, Rust 1.98.1, MSVC x64).

  • cargo test -p victory-shader --locked --lib a_mask_whose_high_dword_is_rewritten -- --test-threads=1 --nocapture: 2 passed.
  • cargo test -p victory-shader --locked --lib a_mask_joined_over_a_back_edge -- --test-threads=1 --nocapture: 1 passed.
  • cargo test -p victory-shader --locked --lib a_carry_in_from_a_divergent_join -- --test-threads=1 --nocapture: 1 passed.

The shader fixtures checked that rewriting a high dword invalidates the corresponding mask through a later use and across a loop back edge. The divergent carry-in refusal also passed.

Only source-built synthetic fixtures were used. AI-assisted verification.

Structured::masks is keyed by a mask's low dword. After `v_cmp vcc` and
`s_mov_b32 vcc_hi, 0`, a select or carry-in on vcc found the compare's
mask under that low dword and took the compare's bits in lanes 32-63,
where the hardware sees 0. After `s_mov_b32 exec_hi, 0` the next vector
instruction's guard named EXEC by its low dword, and lowering took the
high dword of EXEC on entry, so lanes 32-63 ran code the new EXEC
disables.

Structurisation now leaves a low dword it saw with two high dwords out
of masks, highs and active, so lowering refuses what reads it when that
low dword has no value of its own (EXEC, compare results). It also
records carry-in pairs and the pairs a join takes over back edges, so
those count. A carry-in pair is only recorded, not resolved like a
select's mask, so a carry-in on a join of compares is still refused.

A mask whose halves were written apart and whose low dword holds a
number (two s_mov_b32, or s_mov_b64 then s_mov_b32 of the high dword)
still gets the wrong high dword in lowering. That needs the opaque atom
to carry both dwords; it is marked as a shortcut.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. The fix is worth having, but it needs a rebase for a new export on main.

What it does: in structure.rs (Lanes::finish), a low dword seen with two high dwords is dropped from masks, highs and active. Carry-in pairs (step) and back-edge pairs (join) are also recorded. Lowering then refuses a wave64 mask whose high dword was rewritten alone (s_mov_b32 vcc_hi/exec_hi) instead of reading lanes 32-63 from the stale pair. This only adds refusals. The thread backs it with an RX 6600 XT hardware run showing the silent miscompile on main, and an offline corpus run with no coverage change.

Blocking: since this branch was cut, main merged #1221, which adds Structured::other. It is exported by low dword in finish and is now read when lowering a v_cndmask_b32. Your description already names this case ("an others export keyed by (lo, _) needs the same split filter"). It still merges cleanly, but self.other is not filtered there, so a split low dword can still reach the select through other. Please rebase, apply the split filter to other, and confirm a_mask_whose_high_dword_is_rewritten_is_refused still passes on the result.

CI: no checks have run on this head.


Generated by Claude Code

geiyiliyabumo pushed a commit to geiyiliyabumo/VIctory that referenced this pull request Nov 25, 2026
…-440-111a

Shaders: track scalar addresses in RPO before waterfall lookup (Marvisak#440)
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