Skip to content

Shaders: a wave64 mask set by halves tests its high dword in lanes 32-63 (#2780) - #2783

Closed
BastienRodz wants to merge 1 commit into
Marvisak:mainfrom
BastienRodz:shader/wave64-split-mask-high-dword
Closed

BastienRodz wants to merge 1 commit into
Marvisak:mainfrom
BastienRodz:shader/wave64-split-mask-high-dword

Conversation

@BastienRodz

@BastienRodz BastienRodz commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

What's wrong

The structure pass reads a lane mask as a register pair (lo, hi), but stores a mask it cannot see into as Atom::Opaque(lo), so the high value is lost. Lowering then guesses the high dword with Lower::pair_high, which only looks in three places: the instruction that wrote the low dword, the matching input, or a phi in the same block.

That leaves two cases wrong in a wave64 program with console lanes:

  • Dwords from separate writes. s_mov_b32 s10, ...; s_mov_b32 s11, ..., or vcc_lo and vcc_hi written apart. Nothing is found, so lanes 32-63 test the low dword.
  • High dword rewritten after a 64-bit write. The stale high dword is used.

This affects v_cndmask_b32 with an SGPR-pair mask (reachable since #1221), and s_cbranch_vccz/vccnz and guards on such a mask.

RDNA2 ISA, V_CNDMASK_B32: "D.u32 = VCC ? S1.u32 : S0.u32". VCC is "a bit mask with one bit per thread", and VCC_HI holds vcc[63:32].

What the fix does

  • Atom::Opaque carries both dwords of the pair the mask was read from (the same value twice in wave32). Lowering tests the high one in wave64, as it is at the point of use, and pair_high is removed.
  • The atom is born where the later of the two dwords is defined. A loop that redefines only the high dword therefore forgets it.
  • Two masks that share a low dword but have different high dwords are no longer one atom.
  • Lower::touches (whether deciding a guard may test a tainted value) checks the high dword as well as the low one.
  • apps/victory/src/translate.rs's count of guards on opaque masks matches the new variant shape (Atom::Opaque(..)).

This is the "opaque atom that carries both dwords" that #660 names as the full fix.

Tests

New emit::lower::tests::a_wave64_select_on_a_mask_set_by_halves_reads_its_high_dword lowers s_mov_b32 s10, 0; s_mov_b32 s11, X; v_mbcnt_lo; v_cndmask_b32 v1, 0, -1, s[10:11]; ds_write once with X = -1 and once with X = 0.

  • On main, both give byte-identical DXIL: the high dword never reaches the output.
  • With the fix they differ.

The whole victory-shader lib was run one test per process under Wine and vkd3d-proton, on this head and on main with only the new test added. The results are identical except for the new test: 255 ok, 14 failed, 2 crashed and 3 ignored with the fix. The failures and crashes are the same tests on both builds, host quirks of Wine and lavapipe: the semantics and hardware comparisons, 64-bit image atomics, stub_pipeline_is_created, and a few synthetic DPP or gather tests.

an_exec_high_write_does_not_overflow_the_stack and v_cndmask_accepts_uniform_and_constant_masks still pass. cargo clippy --workspace --all-targets --locked -- -D warnings is clean.

Not tested

Conflicts to expect: #660 edits the same mask() arm in structure.rs, and #2210 rewrites pair_high, which this PR removes.

Fixes #2780

No game data. Found and written with AI assistance (Claude Code), reviewed and run locally.

🤖 Generated with Claude Code

@BastienRodz
BastienRodz force-pushed the shader/wave64-split-mask-high-dword branch from 8b62a0b to eff0cd0 Compare November 25, 2026 20:25
@BastienRodz

Copy link
Copy Markdown
Contributor Author

Windows run of this head (eff0cd0) on windows-latest, WARP, cargo test --workspace --locked --no-fail-fast -- --test-threads=1: https://github.com/BastienRodz/VIctory/actions/runs/45694794941

  • victory-shader lib: 271 passed, 0 failed, 3 ignored, including emit::lower::tests::a_wave64_select_on_a_mask_set_by_halves_reads_its_high_dword.
  • Every other crate in the workspace: no failures.

The first push (8b62a0b) missed the Atom::Opaque(_) pattern in apps/victory/src/translate.rs. eff0cd0 fixes it, and cargo clippy --workspace --all-targets --locked -- -D warnings is clean.

@kotekompotek

Copy link
Copy Markdown

Right fix for #2780: pair_high inferred the high dword by register+1 / phi matching, which breaks when the wave64 pair's halves are written separately. Carrying the high Value in Opaque(v, h) makes the mask's two dwords resolve independently at use time - lanes 32-63 now test their real bit.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed and verified eff0cd02 on Windows 11 (RTX 5090 + WARP + Windows SDK 10.0.26100.0 dxil.dll) against main (13f91ca).

Code review

  • Extending Atom::Opaque(Value) to Atom::Opaque(Value, Value) cleanly carries both halves in wave64: Lanes::mask advances born to self.born(k.0).max(self.born(k.1)), derives from either half, and keys masks by (lo, hi) so masks sharing a low dword no longer collide; Lower::boolean resolves hi only when wave64 is set and feeds lane_bit.
  • Minor non-blocking note: Lower::touches (lower.rs:10088) still matches Atom::Opaque(v, _) => Some(v) and only checks the low dword against tainted. If only the high dword derives from a tainted register in wave64, guard/when would miss it—checking both lo and hi in touches would close that edge case cleanly.

Verification (Windows 11, RTX 5090 + WARP + dxil.dll)

  • Targeted suite (a_wave64_select_on_a_mask, v_cndmask, an_exec_high_write, flow_tests::, mask): 17 passed, 0 failed (including a_wave64_select_on_a_mask_set_by_halves_reads_its_high_dword).
  • cargo clippy --locked (victory-shader default & --features host, plus victory): 0 warnings; cargo xtask policy: 184 files ok.
  • Clean git merge-tree against 13f91ca, #2668, #2387, #2711, and #2789.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent WARP/CPU verification of head eff0cd02aa0994ea5d8e4f16639987ac6124fa4b merged cleanly onto origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca.

Environment: Windows 11 x64, WARP/CPU only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1), no physical GPU, no game files; Windows SDK 10.0.26100.0 x64 dxil.dll 1.8.2502.11.

Tests (cargo test --no-fail-fast): victory-shader: 268 passed, 3 failed, 3 ignored (baseline 267 passed, 3 failed, 3 ignored; +1 new test). victory app: 10 + 2 passed (same as baseline). victory-shader baseline on origin/main: 267 passed, 3 failed, 3 ignored; the 3 failures (synthetic_tests::a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_read_the_lane_named_and_zero_from_lanes_left_out) fail identically on origin/main under WARP, so they are not caused by this PR.

Negative control: reading v instead of h for the high dword in the Atom::Opaque(v, h) lowering makes a_wave64_select_on_a_mask_set_by_halves_reads_its_high_dword fail; restored afterwards.

Clippy: cargo clippy -p <affected crates> --all-targets -- -D warnings clean (exit 0).
fmt: cargo fmt --check is not clean on origin/main itself (891 diff hunks across many files); this PR: 891 hunks (same as main).

Review: Carrying both dwords in Atom::Opaque and dating the atom by the later of the two replaces the pair_high register-adjacency search, which is correct when the halves come from separate writes. The test checks DXIL differs in s11 only. Callers in translate.rs are updated mechanically. Diff contains no game files, dumps, shader names or retail hashes.

AI disclosure: this verification was done by Claude Code (Claude Sonnet 5.5).

What's wrong: a lane mask the structure pass cannot see into became
`Atom::Opaque(lo)`, named by its low dword alone, and lowering guessed the
high dword as the one written beside the low dword (`pair_high`: the same
instruction, input or block's phis). A wave64 mask whose dwords come from
separate writes (`s_mov_b32 s10, ...; s_mov_b32 s11, ...`, or
`vcc_lo`/`vcc_hi`) found no such dword, so lanes 32-63 tested the low
dword's bits; one whose high dword was rewritten after a 64-bit write took
the stale one. RDNA2: V_CNDMASK_B32 "D.u32 = VCC ? S1.u32 : S0.u32", VCC
holding one bit per thread and VCC_HI vcc[63:32]. It translated wrongly
for `v_cndmask_b32` (since Marvisak#1221) and for `s_cbranch_vccz/vccnz` and
guards on such a mask (before).

Fix: the atom carries both dwords of the pair the mask was read from, and
lowering tests the high one in wave64. `pair_high` goes. The atom is born
where the later of the two dwords is defined, so a loop redefining only
the high dword forgets it. `Lower::touches` looks at the high dword as
well as the low one.

Tests: `a_wave64_select_on_a_mask_set_by_halves_reads_its_high_dword`
lowers the same select with s11 = -1 and s11 = 0; on main both give the
same DXIL.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@BastienRodz
BastienRodz force-pushed the shader/wave64-split-mask-high-dword branch from eff0cd0 to 39ba8d3 Compare November 25, 2026 20:57
@BastienRodz

Copy link
Copy Markdown
Contributor Author

Thanks for the review and the runs. You're right about Lower::touches: with the high dword now part of the atom, a tainted high dword has to count as well. 39ba8d3 adds that check, in 6 lines in the same function.

After the change, the whole victory-shader lib, run one test per process under Wine and vkd3d-proton, gives the same results as before it (255 ok, with the same host-quirk failures). cargo clippy --workspace --all-targets --locked -- -D warnings is clean.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This makes structure::Atom::Opaque carry both dwords of the mask's register pair. Lanes::mask dates the atom by the later of the two definitions, Lower::boolean reads the high dword as it is at the point of use in wave64, and the pair_high guess is removed. It also checks the high dword in Lower::touches, as a reviewer asked. I checked the born ordering and that the call sites in apps/victory/src/translate.rs and lower.rs were updated to match. The behaviour follows the public RDNA2 ISA definition of VCC/VCC_HI that the description quotes. The new test only shows that the DXIL changes with s11, not the lane values. The description names expected conflicts with #660 and #2210. CI passes.

This is a recommendation for a maintainer to look, not an approval.


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 emit::lower::tests: 34 passed, 0 failed (including new a_wave64_select_on_a_mask_set_by_halves_reads_its_high_dword).
  • cargo test -p victory-shader --lib structure::tests: 13 passed, 0 failed, 1 ignored.
  • cargo clippy -p victory-shader -p victory --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

@wwwwwwww3333333

Copy link
Copy Markdown
Contributor

For the scalar branches from the #2780 triage: on an RX 6750 XT, with only VCC_HI set, the same synthetic program had 63 of 64 lanes take s_cbranch_vccz on #2836, matching the CPU replay reported on #2780 for both PRs. #3023 (on #2836) makes it one decision for the wave; I did not run this PR itself.

@BastienRodz

Copy link
Copy Markdown
Contributor Author

Closing in favour of #2836 + #3023.

As @rezioliver-collab measured on #2780 and @wwwwwwww3333333 confirmed on an RX 6750 XT, this PR (like #2836) makes s_cbranch_vccz/vccnz on a VCC written by halves test each lane's own bit. The ISA makes that one decision for the wave (RDNA2 §12.5). With only VCC_HI set, 63 of 64 lanes took the branch where the hardware takes none. That is worse than main's wrong but uniform answer.

#3023 settles it on top of #2836 with a whole-pair test, and it doesn't apply cleanly here. Two equivalent PRs would only cost review time, so #2836 + #3023 is the complete fix for #2780.

Thanks to everyone who reviewed and ran this one.

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.

Shaders: a wave64 lane mask written by halves tests its low dword in lanes 32-63

7 participants