Skip to content

[pr-fix] Shaders: the wave64 fold takes branches by the wave, and nine smaller faults in it - #14

Merged
KyleS98 merged 10 commits into
KyleS98:mainfrom
Melow-code:fix/wave64-fold
Nov 29, 2026
Merged

KyleS98 merged 10 commits into
KyleS98:mainfrom
Melow-code:fix/wave64-fold

Conversation

@Melow-code

Copy link
Copy Markdown
Contributor

Found by comparing folded programs with an interpreter of the machine code, and by reading fold.rs against the RDNA 2 ISA. The code is trustno1-coder/VIctory-NVIDIA#1504's (merged there); the same faults are in its head 53af8605 and in that tree's main 829188e9.

What it changes

The fold (crates/victory-shader/src/fold.rs, folded in emit/mod.rs) is the first form tried for a wave64 compute program whose plain form needs 64 lanes or code every lane runs. It gave wrong results. One commit each:

  1. Branches. A folded program without LDS, barriers or a mask opened again took its EXEC branches lane by lane. v_cmpx_gt_u32 s0, v0; s_cbranch_execz over; s_mov_b32 s12, 9; over: set s12 for the lanes below s0 only; the console sets it for the wave. Scalars behind loops that lanes leave one by one were wrong the same way. Every branch on a lane mask is now taken wave-wide, which is exact with one host wave per console wave. EXEC branches are taken so from the start, so a program with "lane conditions too large" still folds. This removes the retries inside the folded form, which structured the program lane by lane again. docs/wave64-folded.md is brought up to date.
  2. Late mask joins. A mask join that only a late ballot reads had no values: PHI nodes must have at least one entry, no valid DXIL.
  3. Labels. A jump comes to a label with EXEC as a run left it (one half's lanes). An s_cbranch_execz there could test one half and skip code the console runs.
  4. v_mbcnt then v_cmpx. The upper half counted lanes the lower half's compare had already turned off.
  5. v_readfirstlane_b32 with EXEC 0 read lane 32; the ISA says lane 0.
  6. SDWA compare with EXEC as its destination: E never got the result.
  7. DPP16 row mask. The upper half used the bits of rows 0 and 1 for rows 2 and 3.
  8. A register range past the program's VGPRs (v_lshlrev_b64 v[15:16] of 16) read another lane's register. Refused now.
  9. A panic in a folded lowering left the thread marked as folding.
  10. Code that only ends in the fold's magic dword panicked.

How it was tested

  • GPU and profile: RTX 3070 (32 lanes), shader tests only. No game run, no RTX 2080 Ti run. I do not have the title's programs: please check a full preload for refused programs before merging.
  • Test: each commit but the panics' has a test in emit/folded_tests.rs that dispatches a program and compares every lane. They run on Windows with a card.
  • Before, 1675e22 (the shader crate is the same in ab89c8d): my own hand case for each of 1 to 7 failed on the card with the fault named above (2 in the validator); 8 and 10 failed on Linux. After: all pass.
  • 700 random wave64 programs, 5 inputs each, fold form alone, against a reference interpreter. Before: 122 right, 223 wrong, 9 invalid DXIL, 198 refused, 148 not run because the device was removed twice (at one random program each time, the second a folded one). After, with this branch and the lane writes that follow it: 480 right, 71 wrong, 3 invalid DXIL, 146 refused, no device loss. That tree also had other changes of mine outside the fold (not in this branch); on this branch alone I ran emit/folded_tests.rs on the card, the 80-second test included: 28 passed, 0 failed. I did not sort the 71 wrong one by one. A mask dword through two joins, which this does not fix, still fails as a hand case; so does the fault below.
  • One fault among them that main has too: a vector write under EXEC over a value the lowering has no form for (a carry-in from an untracked mask) keeps 0. The 3 invalid ones (lds declared and not used) are on main too.
  • Refusals through the whole translation, 700 programs, Linux: main translates 516 and refuses 184; this branch translates 531 and refuses 169. None that main translates is refused.
  • Not one of the 700 was refused for a branch lanes take one by one. The "conditions too large" case is refused by 1 without its last sentence, and folded with it and on main (a test, ignored: about 80 s in a debug build).
  • Translation time of the corpus, debug build: within 10 % of main either way.
  • The cache key hashes the translator's sources: every cached translation is redone once.
  • Wave sizes tested: 32.
  • cargo test passed for the crates it touches: yes, Linux, Rust 1.98.1, cargo test -p victory-formats -p victory-shader --lib: 57 and 245 passed, 0 failed, at each commit. CARGO_FEATURE_PURE=1 cargo clippy -p victory-gpu -p victory-shader --tests --target x86_64-pc-windows-msvc: exit 0, no more warnings than main. cargo xtask policy: ok.
  • Not run: the whole Windows suite on this branch.

Checked for overlap

🤖 Generated with Claude Code

Melow-code and others added 10 commits November 29, 2026 13:07
A folded program without LDS, barriers or a mask opened again took its EXEC
branches lane by lane. The console's wave takes them together: scalar code
behind a skip runs when some lane is left and sets every lane's scalar, not the
taken lanes' alone, and lanes that leave a loop one by one are still there for
the ballot behind it. One host wave is one console wave, so taking every such
branch wave-wide is exact. A program with a branch lanes would still take one
by one is left to the other forms.

Branches on EXEC are taken so from the start: their conditions over both
halves' compares can be too large to follow lane by lane, as before this.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A join of numbers one of which is a lane mask takes the mask's ballot after
the lane-mask joins were filled; a mask join only that ballot reads stayed
without a value, which is no valid DXIL ("PHI nodes must have at least one
entry").

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A jump comes to a label with EXEC as a run of vector code left it (one half's
lanes). The rewrite took it for E.lo | E.hi when the code above the label had
settled it, and a s_cbranch_execz there tested one half: code the console runs
was skipped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… dword

v_mbcnt_lo_u32_b32 of exec_lo, then v_cmpx: the upper half's copy of the count
reads E.lo, which the lower half's copy of the compare had written by then.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
v_readfirstlane_b32 with EXEC 0 read lane 32.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…console's EXEC

Its result stayed in the host's EXEC; E never got it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It names the console wave's four rows of 16 lanes. The upper half's copy kept
the bits of rows 0 and 1 for its own two, the console's rows 2 and 3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Past the count are the twins, another console lane's registers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The pipeline workers catch it and go on; the thread's later programs were
lowered as folded ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Its last words may name VGPR bases past the registers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KyleS98

KyleS98 commented Nov 29, 2026

Copy link
Copy Markdown
Owner

I read the ten commits against main. What I could confirm by reading: the old wave_wide_branches returned nothing without LDS or barriers (1), the s_cselect when E.hi is zero (5), t.exec |= d == EXEC_LO (6), the DPP row bits (7), the guard that resets the folding flag (9) and the clamped base (10). For 2, 3, 4 and 8 I can see the mechanism but not the effect without running it.

One question before merging. fill_phis now calls fill_mask_phis a second time after the number joins, but by then data_phis has been taken (mem::take), and the mask pass's own comment says an edge "may ask for more joins, of masks and of numbers". If the second pass asks for a number join, is it left unfilled? If that can happen, the two passes would need to run until neither adds anything; if it cannot, a line saying why would settle it.

Also: a folded program is now refused whenever a per-lane region is left. Your corpus shows no new refusals; the title's own programs may differ, so I will compare VICTORY_WAVE_WHY=1 over the whole of shaders.rpf on main and on this branch, then run a full preload and play the opening, the cell and the first house, together with #12. Results here. (Small thing: with_local_waves' doc comment now sits on in_folded. The red check is main's own Clippy finding.)

AI-assisted (Claude Code, Claude Fable 5.2).

@KyleS98
KyleS98 merged commit 06f81c3 into KyleS98:main Nov 29, 2026
1 of 2 checks passed
@KyleS98

KyleS98 commented Nov 29, 2026

Copy link
Copy Markdown
Owner

Merged (06f81c3, in main at 147473d), thank you. What I ran first, on an RTX 3090 Ti (32-lane waves), with #12 and #13 on the same branch:

  • Every program of the title, offline (translate over shaders.rpf as for a 32-lane host): 2,677 of 2,682 compute programs translate on main and on the branch alike, the same five refused ("a descriptor read only as data", the shadow rays). 223 fold exactly where 213 did: the ten that stopped at "a lane mask holding lanes elsewhere read as data" fold now. Nothing is newly refused, so the new "per-lane region left" refusal does not fire on the title.
  • The validator over all of it: 2,677 of 2,677 compute programs and 14,247 of 14,247 vertex and pixel programs pass on both builds (8,694 of 8,765 mesh programs on both, not this change's).
  • cargo test -p victory-shader on the card: 380 passed, your ten folded tests among them.
  • In the game after a full preload: the opening and the cell twice, the Series S profile, the first house with its mirror, the deck. No error, no shader untranslated, no device lost; 36.0 and 35.4 frames a second in the cell where main ran 35.8 and 36.2 (no frame generation in these runs), Series S 38.5 against 38.9.

My question about the second fill_mask_phis stands as a question, not as a fault found: with every program validating, no join is left unfilled in the title today. If you know it cannot ask for a number join there, a line in the comment would close it.

AI-assisted (Claude Code, Claude Fable 5.2).

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.

2 participants