Skip to content

Shaders: handle redundant partial descriptor writes and lane-preserving VGPR moves (#750) - #1229

Closed
Ctrl-Alt-Haze wants to merge 2 commits into
Marvisak:mainfrom
Ctrl-Alt-Haze:fix/resource-vgpr-descriptors
Closed

Ctrl-Alt-Haze wants to merge 2 commits into
Marvisak:mainfrom
Ctrl-Alt-Haze:fix/resource-vgpr-descriptors

Conversation

@Ctrl-Alt-Haze

Copy link
Copy Markdown

Summary

Addresses #750 (joined_descriptor_redundant_partial_write) and extends victory-shader resource tracking for "a descriptor read from the first lane of computed VGPRs".

(Note: #1182 also addresses #750 for plain v_mov_b32; this PR covers both #750 and the broader computed-VGPR move/extract/select family described below.)

Changes

  • crates/victory-shader/src/structure.rs:
    • Added Bools::var_bool and non-mutating Bools::meet_with_facts (meet_facts_in) to enforce equivalence/complement groups over Atom::Lanes and check leaf consistency without cloning or mutating Bools.
  • crates/victory-shader/src/resource.rs:
    • Added equivalence-aware lane feasibility (feasible, canon_cmp, cmp_leaf_ok with 1D integer constant bounds) across runs, lane_phi, lane_select, waterfall, and lane_value_equals.
    • Fixed Shader resource tracking: redundant masked write after a per-lane descriptor join is refused (synthetic repro on #566) #750 (lane_value_in, redundant_write, lane_value_equals, split_term): when a guarded lane_copy or v_cndmask_b32 fails runs(site, known), keeps the prior register value iff old and the written value provably evaluate to the same 32-bit word on every feasible active lane in known where reach[site.0] and guard hold.
    • Generalized lane_copy (guarded by !clamp && omod == 0) for sound lane-preserving moves, selects, and 32/64-bit word extracts (v_mov_b32 incl. DWORD SDWA, v_swap_b32, identity v_or_b32/v_and_b32/v_xor_b32/v_add_*/v_sub*/v_lshlrev_b32/v_lshrrev_b32/v_ashrrev_i32/v_mul_lo_u32/v_min_*/v_max_*/v_bfi_b32, v_perm_b32, v_alignbit_b32/v_alignbyte_b32, 64-bit 0/32-bit word shifts, and constant-mask / equal-operand v_cndmask_b32 with !self.p.wave64 guard on 32-bit u32::MAX literals).
    • Updated lane_select (same_mask and single-feasible-branch v_cndmask_b32) and waterfall (using lane_value(..., &loop_known)).
    • Added 7 unit tests in resource::tests, including Shader resource tracking: redundant masked write after a per-lane descriptor join is refused (synthetic repro on #566) #750 image & sampler reproducers, swapped/negated compares, lane-preserving moves/extracts, and negative soundness controls (non-identity writes, widened masks, flipped arms, non-identity ALU/perm/shifts, guarded v_swap_b32, mixed lane_select masks, and Wave64 literal u32::MAX mask refusal).
  • crates/victory-shader/src/emit/waterfall_tests.rs:
    • Added 3 end-to-end tests (lanes_sample_texture_after_redundant_partial_write, lanes_sample_joined_sampler_after_redundant_partial_write, lanes_sample_after_vgpr_swap_and_word_extract) validated with dxil.dll and executed on RTX 5070 Ti + WARP.

Verification

  • Hardware / Toolchain: Windows x64, NVIDIA GeForce RTX 5070 Ti (driver 32.0.16.2135), WARP (VICTORY_WARP=1), Windows SDK 10.0.26100.0 dxil.dll, Rust 1.98.1, base ffe310eb.
  • cargo xtask policy: 154 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default & --all-features): passed
  • cargo test --workspace --locked: 352 passed, 0 failed, 6 ignored (victory-shader 130/1 ign)
  • cargo test -p victory-shader --features host --locked (RTX 5070 Ti + WARP): 130 passed, 0 failed, 1 ignored
  • VICTORY_WARP=1 cargo test -p victory-shader --features host --locked (WARP): 130 passed, 0 failed, 1 ignored
  • cargo test -p victory-gpu --features diagnostics --locked & VICTORY_D3D12_WARP=1: 53 passed, 0 failed, 1 ignored each
  • rustfmt --edition 2024 --check on modified files: zero diffs on added/modified lines (waterfall_tests.rs completely clean).
  • Not tested: retail title corpus (no retail game assets used; synthetic tests only).

AI use: Written and verified locally with an AI coding swarm (gemini-4-hydrogen-high), then independently audited and tested on RTX 5070 Ti + WARP.

@pablogonz12

Copy link
Copy Markdown
Contributor

Setup: Linux, RX 9070 XT, RADV (Mesa 26.3.1), dxil-spirv master, spirv-val 1.3. Your head folded onto a base of origin/main ffe310e plus #1017 (main's host build fails without it: E0425 host_resource), then the four-stage offline check over 768 programs (751-program world dump + 52-program BVH capture, 35 duplicate containers skipped).

translate valid SPIR-V RADV pipeline
base 666 / 768 656 656
your head 666 / 768 656 656

No program gained or lost and the failure table is identical to the base, reason for reason: nothing in this capture reaches the class you handle, so this is a no-regression result only (same 10 SPIR-V validation failures as the base).

Not tested: in-game.

@Zipettte

Copy link
Copy Markdown

Tested on AMD Radeon RX 7700 XT (RDNA3, gfx1101), driver 32.0.32029.2010, Windows 11, Rust 1.98.1 MSVC at 7b4cb23: cargo test -p victory-shader passes, 130 passed / 0 failed. Workspace baseline on main (ffe310e) is green on the same machine (342 passed).

Tested with AI assistance (Claude).

@Ctrl-Alt-Haze

Copy link
Copy Markdown
Author

@pablogonz12 Thank you for running the 768-program Linux/RADV (dxil-spirv / spirv-val) check and confirming zero regressions on this branch!

@Ctrl-Alt-Haze

Copy link
Copy Markdown
Author

@Zipettte Thanks for verifying 7b4cb23 on the AMD Radeon RX 7700 XT!

@SantannaCarlos

SantannaCarlos commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Scope of the PR, as described: handle redundant partial descriptor writes (#750) and lane-preserving VGPR moves/extracts/selects (v_mov_b32, v_swap_b32, identity v_perm_b32/v_alignbit_b32, 64-bit shifts by 32, same-operand v_cndmask_b32…) in front of a waterfall v_readfirstlane_b32, so that fewer programs stop with "a descriptor read from the first lane of computed VGPRs".

Measurement (title data, local; counts only)

The pattern that stays out (instructions only)

One representative program; the others share the shape:

  1. A per-lane if/else: v_cmp_eq_u32 s[6:7], vX, sY then s_and_saveexec_b64 s[64:65], s[6:7] (then arm), s_andn2_b64 exec, s[64:65], exec + s_cbranch_execz <join> (else arm), and s_mov_b64 exec, s[64:65] at the join.
  2. Then arm: does not write v79..v82. Those registers keep values computed before the split (a buffer_load_dwordx3 and float math such as v_sub_f32/v_mul_legacy_f32).
  3. Else arm: s_load_dwordx4 s[8:11], s[24:25], <imm> (a sampler from a descriptor table, uniform), then v_mov_b32 v79..v82, s8..s11.
  4. After the join, with the full mask: a waterfall loop over the sampler: v_readfirstlane_b32 s28..s31 ← v79..v82, two v_cmp_eq_u64, s_and_saveexec_b64, image_sample_lz …, s[28:31], s_xor_b64 exec, exec, …, s_cbranch_execnz back to the loop head.

Where the tracker stops (confirmed by instrumenting per_lane locally): the four words reach per_lane as the then-arm values: v_sub_f32, v_mul_legacy_f32 and two buffer_load_dwordx3 results, defined before the split. lane_select/lane_phi do not resolve the join, and the refusal names the first varying word, an arithmetic instruction, hence "computed VGPRs". It is not the loop-head phi and not a move the PR handles: the v_mov copies are on the other arm of a per-lane join.

So the descriptor is "uniform sampler in the else lanes, non-descriptor data in the then lanes". The console runs the waterfall over both kinds of lanes (sampling with whatever the then lanes hold). Whether the then lanes' results are dead is not proven here: the sample's result is used afterwards under the full mask, and I did not follow it further.

Suggestion for the PR (or a follow-up)

  • A narrow rule: accept a per-lane join whose one side is a descriptor source (here Loaded) and whose other side is not a descriptor only when the join's condition is the same mask that later selects which lanes use the sample's result. Then the lanes holding computed data never consume what they sample, and the descriptor side can be used for the waterfall. This needs a mask-liveness check from the join to the uses of the sample's result; without it the case should keep refusing, since we have not shown that the then lanes' results are dead.
  • A test shaped like steps 1–4 (if/else, uniform sampler copied by v_mov on one arm, untouched VGPRs on the other, waterfall after the join) would pin the case.

Counts and instruction shapes only; no title bytes. Instrumentation was local and is not part of any branch.

@Ctrl-Alt-Haze

Copy link
Copy Markdown
Author

@SantannaCarlos Thank you so much for testing #1229 against shaders.rpf and pinning down the exact 4-step foliage mesh pattern!

I've pushed f55ba49 to implement your exact suggestion soundly:

What changed (crates/victory-shader/src/resource.rs, emit/waterfall_tests.rs)

  • Threaded site: Site through descriptor / classify / phi / select / per_lane / lane_select / lane_phi.
  • Added lazy SSA def-use indexing (Tracer::uses) and a strict forward per-lane deadness proof (Tracer::dead_for_bad_lanes):
    • Only applies when site is a pure VGPR-producing Image or Buffer read (stores, atomics, LDS, tfe/lwe, SGPR/SCC writes stay refused).
    • Tracks (Value, Known) forward through Phis (enforcing loop invariance via bool_born_before on backedges so waterfall loop-header phis preserving site's dst are handled soundly), v_cndmask_b32 mask narrowing (Wave32 & Wave64 via Structured::mask_bits), v_swap_b32, and pure per-lane VALU ops (tracking partial-destination overwrites including sdwa, *_f16/*_u16/*_i16/*_b16, v_cvt_f16_f32, v_fma_mix*_f16, and _d16 buffer loads).
    • In lane_phi / lane_select, when one arm resolves to a valid descriptor Source and the other arm fails, accepts the descriptor side only when dead_for_bad_lanes proves the failing arm's lanes never consume site's output; otherwise it keeps refusing with "a descriptor read from the first lane of computed VGPRs".
  • Added synthetic Wave32 & Wave64 tests matching your exact 4-step pattern (both positive dead-downstream and 8 negative live-consumer / partial-overwrite soundness controls), plus lanes_sample_joined_sampler_with_dead_non_descriptor_arm validated by dxil.dll and executed on RTX 5070 Ti + WARP.

Re-verified on f55ba49

  • cargo xtask policy: 154 files ok
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default & --all-features): pass
  • cargo test --workspace --locked: pass (victory-shader 135/0/1 ign)
  • cargo test -p victory-shader --features host --locked (RTX 5070 Ti & VICTORY_WARP=1): 135 passed, 0 failed, 1 ignored each
  • cargo test -p victory-gpu --features diagnostics --locked (RTX 5070 Ti & VICTORY_D3D12_WARP=1): 53 passed, 0 failed, 1 ignored each

If you get a chance to re-run victory translate --validate --list on those 38 containers with f55ba49, I'd love to hear how many clear!

AI-assisted fix (gemini-4-hydrogen-high), independently audited and tested on RTX 5070 Ti + WARP; no game data.

@SantannaCarlos

Copy link
Copy Markdown
Contributor

Thanks for implementing the dead-lane rule in f55ba49. Measured again the same way (title data, local; counts only):

  • The 38 containers that stop with "a descriptor read from the first lane of computed VGPRs":
  • Whole shaders.rpf, integration stack with and without f55ba49: identical totals (VS+PS 12231 emitted / 12155 validated, CS 1480/1445, MS 8214/8152). 0 programs gained, 0 lost; dxil.dll validator failures unchanged (234 → 234). The 39 programs with this reason are the same programs at the same offsets.
  • Tests: cargo test -p victory-shader -- --test-threads=1 passes on the PR alone (135 passed, including the four new foliage-shaped tests) and merged into our stack (180 passed), on an RTX 4070.

Why the rule does not fire on the title's programs. The synthetic shader matches the shape up to the waterfall: per-lane if/else, a uniform sampler loaded and copied by v_mov on one arm, computed VGPRs left on the other, a full-mask waterfall after the join. The difference is what happens to the sample's result.

  • In the title it is not masked back by the branch condition: it feeds a v_mul_legacy_f32 under the full mask, then several v_fmac_f32/v_fma_mix_f32 accumulations, still under the full mask.
  • No v_cndmask_b32 and no guarded consumer separates the then-arm lanes again.
  • So the proof the rule needs (the non-descriptor lanes' results are dead) does not hold here, and refusing is the right answer for this rule. Your negative test with the unconditional store is the title's case.

What remains is a question of intent, not of tracking:

  • On the console, those lanes sample with whatever their computed words hold, and the results are used.
  • Offline we cannot tell whether such lanes ever exist at run time (the branch condition may always pick the descriptor arm for this content) or whether their results are harmless.
  • Two options, maintainer's call:
    1. Keep the rule as it is: safe, and it handles programs that do mask the result.
    2. Add an explicit, documented shortcut for this shape, where the non-descriptor lanes read a null/static sampler. It would diverge from the console only for lanes that sample with non-descriptor words. If you go this way, a log line naming the program would make the divergence visible.

Either way, in our measurement the PR changes nothing for the title's programs and regresses nothing (same totals, no validator changes).

Counts and instruction shapes only; no title bytes.

@Ctrl-Alt-Haze

Copy link
Copy Markdown
Author

Thanks @SantannaCarlos for re-testing f55ba49 on RTX 4070 (135 standalone / 180 in the stacked build, 0 regressions) and confirming why those remaining 38 containers stop at that point (full-mask v_mul_legacy_f32/v_fmac_f32/v_fma_mix_f32 consumers without re-masking).

We'll keep Option 1 — keeping the strictly sound dead-lane proof rule as-is rather than substituting a fallback descriptor on potentially live non-descriptor lanes — unless the maintainers explicitly prefer adding an opt-in/logged fallback shortcut (Option 2).

@Ctrl-Alt-Haze

Copy link
Copy Markdown
Author

Rebased onto upstream main (70564ae) at da25359 (merged self::meet pruning from 70564ae into feasible alongside both spilled/spill_at and uses).

Verified locally on Windows 11 x64 / NVIDIA GeForce RTX 5070 Ti + WARP:

  • cargo xtask policy: passed (174 files ok)
  • cargo clippy --workspace --all-targets --locked -- -D warnings (default & --all-features): passed
  • Workspace crates (--locked -- --test-threads=1): victory 1 passed, victory-core 11 passed, victory-formats 23 passed / 4 ignored, victory-game 15 passed, victory-loader 25 passed (22 unit + 2 load_probe + 1 title_dlls), victory-pipeline 4 passed, victory-hle 99 passed (97 unit + 1 fidelityfx + 1 no_dll_planting), victory-pe 7 passed, xtask 4 passed; victory-gpu 76 passed / 2 pre-existing 70564ae failures (dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views) / 1 ignored
  • cargo test -p victory-shader --features host --locked (hardware): 221 passed; 3 failed; 2 ignored (the 3 failures are pre-existing on clean upstream/main 70564ae on RTX 5070 Ti: stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride, stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros, synthetic_tests::gathers_at_a_level_match_the_hardware)
  • VICTORY_WARP=1 cargo test -p victory-shader --features host --locked: 222 passed; 2 failed; 2 ignored (pre-existing gathers_at_a_level_match_the_hardware + permutes_read_the_lane_named_and_zero_from_lanes_left_out which skips under VICTORY_WARP=1 and passed on RTX hardware)

@Marvisak

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. Closing because this change is already in main, or main has since replaced the code it patches.

rebased, but still 2,130 lines rewriting main's lane-value tracking for #750 and 'first lane of computed VGPRs', classes main brought to 0 on the title's programs; no count of programs it adds on main.

If this is mistaken, reply here with what was missed (or push a change and say so) and it will be looked at again. Thank you for the contribution.

@Marvisak Marvisak closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shader resource tracking: redundant masked write after a per-lane descriptor join is refused (synthetic repro on #566)

5 participants