Repository navigation
Shaders: drop a wave64 mask whose low dword is seen with two high dwords - #660
coderanonymus wants to merge 1 commit into
Conversation
LAKSHYA-cmd-tech
left a comment
There was a problem hiding this comment.
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.
|
I tested the remaining opaque numeric-mask case on 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 |
catminou4
left a comment
There was a problem hiding this comment.
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.
|
Independent test on RDNA4: this PR alone, on Counting unique
Both mask reasons disappear entirely with this PR: Interesting detail: the 33 hashes left are exactly the same set that #585 alone leaves, even though #585 only touches 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 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: 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. |
|
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: |
|
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.
The in-game "440 -> 33 refused" I posted came from runs that covered different amounts of world content (a No regression either: the same programs emit, and the one world run I have ended the same way as If your intent is the wave64 mask pairs, the remaining mask refusals offline are 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. |
|
Independent Windows check of
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>
d92ebe9 to
19b02ec
Compare
…en with two high dwords
…en with two high dwords
…en with two high dwords
|
Automated triage for the maintainers. Needs work. The fix is worth having, but it needs a rebase for a new export on What it does: in Blocking: since this branch was cut, CI: no checks have run on this head. Generated by Claude Code |
…-440-111a Shaders: track scalar addresses in RPO before waterfall lookup (Marvisak#440)
What
In wave64 a lane mask is an SGPR pair. Structurisation tracks masks by the pair, but it exports
Structured::masks,highsandactiveby 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.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.emitreturned Ok.v_add_co_ci_u32reads its carry-in the same way.s_mov_b32 exec_hi, 0; v_mov_b32 v1, 7; buffer_store_dword ...: the guard isOpaque(exec_lo), and lowering took the high dword of EXEC on entry. Lanes 32-63 stored, although the new EXEC disables them.emitreturned Ok. With ans_mov_b64 s[2:3], execfirst,mainalready refuses the same program ("a lane mask whose lanes depend on its own value").The code changes are in
structure.rs:finishleaves a low dword out ofmasks,highsandactivewhen 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.steprecords the carry-in pair ofv_*_co_ci_u32. Before, it was never recorded, so the check above could not see it. It is only recorded, not resolved throughmaskas thev_cndmask_b32condition is, so a carry-in on a join of compares is still refused, as onmain.joinrecords the pairs it takes over back edges. Lowering looks phi arguments up by low dword too, and those pairs never go throughmask.emit/mod.rsgets astores_v1test helper, two tests, and a doc comment update.Still open, marked as a
shortcut:inmask:Atom::Opaquenames 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: twos_mov_b32, ors_mov_b64 s[4:5], kthens_mov_b32 s5, 0, where lowering takes the stales5. 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 toemit/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
maintoo): a select on a join of compares after a divergent branch translates wrongly. Inv_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 turnedmain'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 withs_andn2/s_and/s_oron 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:
v_cndmask_b32block instepand fixes the select case another way (a select mask keyed by the select's destination, from the exact pair it reads). Whichever lands second needs a rebase there.joinhandles back edges. The back-edge recording here sits in the loop it changes, so the two may conflict.othersexport keyed by(lo, _). It needs the same split filter asmasks.Rebased on main (ffe310e)
mainnow buildspaired_masks(exact pairs, for the waterfall proof) from the same map.finishtakes 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. Thestores_v1test helper now passes the structure toresource::track, which takes it since main changed.After the rebase:
cargo test -p victory-shader100 passed;cargo clippy -p victory-shader --all-targets --all-features --target x86_64-pc-windows-msvc -- -D warningsclean.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.main(tests only):cargo test -p victory-shader --lib -- a_mask_ a_carry_instructure::tests::a_mask_whose_high_dword_is_rewritten_is_not_the_compareFAILED:0x2020480: vcc_hi was zeroed after the compare, left and right bothSome((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_compareFAILED: left and right bothSome((Lane(Value(16)), Bool(0), Bool(1))).emit::tests::a_mask_whose_high_dword_is_rewritten_is_refusedFAILED: the vcc_hi select program translated, and so did the vcc_hi carry-in program (checked on its own). Theexec_hiprogram also translated (Ok). Its last program (exec_hiafters_mov_b64 s[2:3], exec) already refuses onmain, so it adds no regression coverage; it stays as a guard.emit::tests::a_carry_in_from_a_divergent_join_is_refusedpasses onmain(it refuses with "a carry-in from an untracked mask"). It fails if the carry-in goes throughmasklike 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 thev_mov_b32no longer needs the mask's own value.emitstill returns Ok, since nothing with a side effect reads it. I updated its doc comment.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 --checkon 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