Skip to content

Shaders: track spilled lane masks restored via v_readlane_b32 - #2967

Open
Dukestar69 wants to merge 1 commit into
Marvisak:mainfrom
Dukestar69:fix/lane-mask-spills-joins-carries
Open

Dukestar69 wants to merge 1 commit into
Marvisak:mainfrom
Dukestar69:fix/lane-mask-spills-joins-carries

Conversation

@Dukestar69

@Dukestar69 Dukestar69 commented Nov 26, 2026 •

Copy link
Copy Markdown

Summary

Tracks wave32 lane masks restored from VGPR spills (v_writelane_b32 / v_readlane_b32) when consumed by subsequent SOP mask operations or numeric mask_dword reads, and adds andn1/orn1 support to mask_number.

Changes

  • crates/victory-shader/src/structure.rs:
    • Recognize st.restored operands in Fields::Sop (takes_mask) so SOP mask ops reading a restored wave32 mask are tracked as lane masks.
    • Record restored wave32 masks on v_readlane_b32 in Fields::Valu without claiming EXEC at the restore.
    • Add unit test restored_mask_feeds_sop_mask_op.
  • crates/victory-shader/src/emit/lower.rs:
    • Follow st.restored in mask_dword so numeric reads of a restored lane mask reuse the original mask's ballot.
    • Support andn1 / orn1 (complement of src0) in mask_number.
    • Total lower.rs delta is only +10 lines (522,601 / 524,288 bytes, 1,687 bytes headroom).
  • crates/victory-shader/src/emit/valu_tests.rs:
    • Add end-to-end GPU execution test spilled_lane_mask_restored_into_sop_and_select_matches_rdna2 verifying both v_cndmask_b32 selection and mask_dword readback after spilling a compare mask through v_writelane_b32 / v_readlane_b32 and combining with s_and_b32.

(Updated per triage & review feedback: dropped v_mad_i64_i32 carry-out in favor of #171, and dropped Atom::Phi / phi_joins / carry-in changes to eliminate overlap with #385.)

Verification

  • cargo test -p victory-shader structure::tests: 14 passed, 0 failed.
  • cargo test -p victory-shader spilled_lane_mask_restored_into_sop_and_select_matches_rdna2 -- --test-threads=1: passed on NVIDIA GeForce RTX 5090 (with dxil.dll validation).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • rustfmt --edition 2024 --check: clean.
  • cargo xtask policy: 191 files ok.

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

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

Reviewed and verified against main (63bcca43) on Windows 11 — solid implementation and tests pass:

  • Fixed-point convergence & spilled masks: phi_joins (structure.rs:802-833) feeds all Atom::Phi incoming args (including back-edges) through wrong_joins with cached join() results and monotonic progress, guaranteeing termination while closing the loop-carried zero-init mask gap. Wave32 v_readlane_b32 restoration (structure.rs:1199-1205, 1264-1275) preserves masks identity without fabricating EXEC liveness, and mask_operand (lower.rs:1814-1860) unifies constant/scalar/other mask handling across v_cndmask_b32 and *_co_ci_u32.
  • Two merge-planning notes:
    1. emit/lower.rs file-size headroom: on 63bcca43, crates/victory-shader/src/emit/lower.rs grows to 523,264 B (+2,701 B), leaving only 1,024 B under the 524,288 B (512 KiB) policy cap.
    2. Overlap with #385: conflicts directly with #385 (which also refactors Atom::Phi mask tracking in structure.rs and moves phi lowering into emit/lower/phi.rs, freeing ~4.9 KiB in lower.rs). No conflict with #2923.
  • (Minor note: carry_in_constant_and_mad_i64_carry_out_lower verifies DXIL lowering; adding a GPU execution case in valu_tests for v_mad_i64_i32 signed-overflow carry-out ((p ^ r) & (addend ^ r)) < 0 would be a nice extra check.)

Local verification:

  • cargo test --locked -p victory-shader --lib structure::: 16/16 passed (1 ignored bench)
  • cargo test --locked -p victory-shader --lib lower::tests: 36/36 passed
  • cargo test --locked -p victory-shader -- mad cndmask carry readlane: 6/6 passed
  • cargo clippy --locked -p victory-shader --tests -- -D warnings: 0 warnings
  • cargo xtask policy: 191 files ok

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

Verified at the mechanism level: phi_joins defers arg resolution until all edges are known (loop-carried mask phis whose back-edge arg postdates the phi), the restored-map lets a v_readlane'd wave32 mask keep mask identity without claiming EXEC at restore, and *_co_ci_u32 carry-ins resolve like v_cndmask's mask operand. The takes_mask precedence (phi && !loaded || restored) && needs-mask reads right. Each of the three cases has a test that fails without its mechanism.

@steven-seagull27

steven-seagull27 commented Nov 26, 2026 •

Copy link
Copy Markdown

@Dukestar69 @fuzeofficlal I ran the suggested GPU carry-out check. On eb0d782, v_mad_i64_i32 writes the correct 64-bit vector result but the wrong SDST bit in 32,759 of 65,536 inputs on both the RTX 5080 and built-in Microsoft WARP. AMD's RDNA 2 ISA, §12.12, opcode 375 defines the destination as the concatenation of the carry bit and the low 64 bits. I computed the exact signed multiply-add in Rust i128 and compared bit 64 against a shader readback through v_cndmask_b32.

Inputs Low 64 bits on both adapters Expected SDST This PR With #171
-3 * 5 + 0 0xfffffffffffffff1 1 0 1
1 * 1 + i64::MAX 0x8000000000000000 0 1 0
Wrong fields per adapter This PR This PR + @n-romanovich's #171
Signed SDST 32,759 / 65,536 0 / 65,536
Signed result (both dwords) 0 / 65,536 0 / 65,536
Unsigned result and SDST 0 / 65,536 0 / 65,536

The corpus includes boundaries and deterministic pseudorandom inputs. The fourth output dword was a sentinel and matched throughout. The signed-overflow predicate in this PR explains both examples. #171 computes bit 64 with unsigned carry XOR the two operand sign bits.

I merged both original PR histories and added a six-case GPU regression. The new test fails on this head and passes after the merge on both adapters. The original loop-phi, restored-mask and carry-lowering checks still pass. Strict shader Clippy passed, and the asset-policy check passed with lower.rs at 523,370 bytes (918 bytes free).

To apply both commits on top of eb0d782:

git fetch https://github.com/steven-seagull27/VIctory.git refs/heads/codex/pr2967-171-signed-mad
git merge --ff-only FETCH_HEAD
cargo test --locked -p victory-shader --lib signed_mad_carry_is_bit_64_of_the_exact_sum -- --nocapture

I also resolved the review's #385 overlap on a separate integration branch. After merging both PRs, three carry-in programs still refused to translate: inline 15, 15 in a scalar register, and a runtime register mask. With the direct-mask fix, the RTX 5080 and WARP 1.0.22 each returned 288/288 correct Wave32 carry-in lane values. Portable WARP additionally passed 512/512 Wave64 lanes. Built-in WARP passed both width-independent controls (64/64 lanes) and skipped fixed Wave32. The two histories and #171 remain intact. Patch, counts and full-suite limits on #385.

AI-assisted. All inputs were synthetic.

Marvisak commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate (recommending a look, not approving). Thanks for splitting it. Now:

  • The PR only tracks wave32 lane masks spilled with v_writelane_b32 and restored with v_readlane_b32.
  • structure.rs (Lanes) treats a restored mask as a mask, both at the restore and in SOP mask ops.
  • Lower::mask_dword follows st.restored to the original mask's ballot.
  • mask_number gains andn1/orn1, which complement src0 as the RDNA2 ISA defines.

The v_mad_i64_i32 carry, Atom::Phi and carry-in changes are gone, so this no longer overlaps #171 or #385. spilled_lane_mask_restored_into_sop_and_select_matches_rdna2 checks values on the GPU for both the v_cndmask select and the numeric read, and restored_mask_feeds_sop_mask_op covers the tracking.

The description says which instruction pattern fails on main but not whether title shaders hit it. Saying whether they do would help prioritise.

CI: not run on this head (no check runs yet).


Generated by Claude Code

@Dukestar69
Dukestar69 force-pushed the fix/lane-mask-spills-joins-carries branch from eb0d782 to a6923f6 Compare November 26, 2026 05:57
@Dukestar69 Dukestar69 changed the title Shaders: track spilled lane masks, Atom::Phi back-edges, and carry masks Shaders: track spilled lane masks restored via v_readlane_b32 Nov 26, 2026
@Dukestar69

Copy link
Copy Markdown
Author

Thanks @steven-seagull27 and triage for the thorough catch and feedback! Updated this PR to address every point:

  1. Dropped v_mad_i64_i32 carry-out (covered cleanly by Shaders: lower v_mad_u64_u32 and v_mad_i64_i32 #171).
  2. Scoped strictly to spilled lane masks (v_writelane_b32/v_readlane_b32) + andn1/orn1 in mask_number, dropping the Atom::Phi / phi_joins and carry-in changes so there is zero overlap with Shaders: track every incoming edge of a lane-mask phi #385.
  3. Added end-to-end GPU execution test (spilled_lane_mask_restored_into_sop_and_select_matches_rdna2 in emit/valu_tests.rs), verified on NVIDIA GeForce RTX 5090 with dxil.dll validation.
  4. Size budget: lower.rs is now only +10 lines (522,601 / 524,288 bytes, 1,687 bytes free), clippy --all-targets -- -D warnings clean, rustfmt --edition 2024 --check clean, cargo xtask policy 191 files ok.

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

@cloph-dsp

Copy link
Copy Markdown

Independent local verification of head 039b30c on Windows 11, Rust 1.100.0/MSVC:

  • cargo test --locked -p victory-shader --lib structure::tests::restored_mask_feeds_sop_mask_op — 1 passed, 0 failed.
  • git diff --check origin/main HEAD — clean.

I ran only the CPU structuring test; the PR's GPU-execution test was not run here. Synthetic inputs only; no game assets, shaders extracted from game data, or DRM material used. AI-assisted (Hy4 flash).

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

Tracking st.restored for Wave32 v_readlane_b32 restores works on the v_cndmask_b32 path, and emit/lower.rs stays under the 512 KiB limit (521,065 bytes).

Two blockers need fixing before merge:

  1. Scalar SCC regression (structure.rs:1184-1208, emit/lower.rs:3567-3593): Classifying an SOP that reads a restored mask (such as s_and_b32 s7, s6, 1) into st.masks causes Lower::sop to return Err(LANE_MASK) and record Val::Missing for both the SGPR destination and Reg::Scc. A subsequent scalar SCC consumer (e.g., s_cselect_b32) that translated cleanly on main now fails on Val::Missing, and s_cbranch_scc* can switch from wave-wide scalar SCC to lane-local scc_lane branching. Please preserve scalar SCC for numeric consumers and add multi-lane regression coverage.
  2. VICTORY_WARP=1 test failure (emit/valu_tests.rs:2291-2297): Calling Host::new(false) honors VICTORY_WARP=1, so when VICTORY_WARP=1 is set in the environment, the ("hardware", false) iteration opens WARP, has its Wave32 PSO rejected, and panics on assert!(warp, ...). Also note that the second sub-test can reuse the Val::I32 already emitted for v_readlane_b32 instead of independently exercising the st.restored recursion in mask_dword, and andn1/orn1 have no standalone non-EXEC-writing SOP2 entries in gfx10/opcodes.rs.

Verified locally on Windows 11 x64 (039b30c5):

  • cargo run --locked -p xtask -- policy: passed
  • 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
  • spilled_lane_mask_restored_into_sop_and_select_matches_rdna2 -- --test-threads=1: passed on RTX 5090 (1 passed, 0 failed), failed under VICTORY_WARP=1 (exit 101 at valu_tests.rs:2296:17)

@steven-seagull27

Copy link
Copy Markdown

@Dukestar69 @fuzeofficlal I reproduced both findings on 039b30c and put the fix and a 32-lane regression in one commit directly on your head. Review · Patch and test

The shader spills and restores a comparison mask, runs s_and_b32 to test bits 0 and 31, then consumes SCC with either s_cbranch_scc0 or s_cselect_b32. On the PR head, when bit 0 was set, the branch wrote 7 only on lane 0. The other 31 lanes wrote 11, although all 32 should write 7. The select failed translation at byte offset 56 with a lane mask read as data. Both symptoms reproduced on the RTX 5080 and WARP 1.0.22.

The fix keeps an ordinary scalar use of a restored word on the scalar path. An actual lane-mask consumer can still recover its lane predicate. The regression checks lane 0, lane 31, an unselected bit, and an empty mask.

Adapter Parent 63bcca4, new regression Fix, new regression
RTX 5080, driver 621.40 256/256 correct 256/256 correct
Microsoft.Warp 1.0.22 256/256 correct 256/256 correct
Built-in Windows WARP, Wave4 only No Wave32 readback No Wave32 readback

With VICTORY_WARP=1, the original PR test treated built-in WARP as hardware and panicked when its Wave32 pipeline was refused. The patched test checks the selected adapter once and reports the unsupported width. On WARP 1.0.22 the original one-thread numeric-mask case still returns 0 instead of 1 for equal inputs. I reproduced it on the parent with the same fixture, on this PR head, and with the patch. The original fixture passed on the RTX. The structuring and lowering unit tests, strict shader Clippy, and repository asset policy also passed.

git fetch https://github.com/steven-seagull27/VIctory.git refs/heads/codex/pr2967-restored-scc
git cherry-pick 5203c1441420bf6b86ca83a4e5740e59f0ef52cf
cargo test --locked -p victory-shader --lib restored_mask_sop_scc_is_wave_uniform -- --nocapture

AI-assisted with Codex. Synthetic RDNA2 shaders and D3D12 readbacks.

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.

6 participants