Skip to content

d3d12 shader: find the wave sections after an unmatched saveexec (D3D12-SHADER-WAVE-SECTIONS-PAST-UNMATCHED-SAVEEXEC) - #2710

Merged
MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
Kal0s-design:fix/wave-sections-unmatched-saveexec
Nov 30, 2026
Merged

MoonEyes merged 2 commits into
ProjectLeonida:mainfrom
Kal0s-design:fix/wave-sections-unmatched-saveexec

Conversation

@Kal0s-design

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: D3D12-SHADER-WAVE-SECTIONS-PAST-UNMATCHED-SAVEEXEC (#2570 (comment))

What and why

emit::wave::sections() finds a block's whole-wave sections: a saveexec of every lane, the code under it, and the EXEC restore that ends it. Their reductions are then lowered from the recognised shape (Shape::Active and friends). When a saveexec's restore is not in the block (the restore lies in a later block), the scan stopped right there:

let Some(end) = end else {
    break;
};

Every whole-wave section later in that block was then missed, and its reduction was translated the general way instead of from its shape. Such a saveexec is no section, so the scan now skips it (i += 1; continue;) and keeps looking.

This is a port of shh's fix and test from the sibling VIctory tree (TaynanGT/Vice-Tay 17e3e2c, GPL-3.0), credited as co-author. That tree took it from an earlier Leonida commit that never reached this repository. The test is unchanged: it puts an unmatched s_or_saveexec_b64 in front of WAVE_OR_V7_TO_S7.

No translator version bump is needed: leonida-gpu/build.rs fingerprints leonida-shader/src, so cached translations are made again.

Tested

  • New emit::wave::tests::an_unmatched_saveexec_leaves_the_later_sections_of_its_block expects the OR section and its shape at index 1 after an unmatched saveexec, and no section at index 0. On main (012b79a) it fails with the section after an unmatched saveexec (left None); with the change it passes.
  • cargo test -p leonida-shader --lib: 386 passed, 0 failed, 4 ignored.
  • cargo clippy -p leonida-shader --all-targets: no warning in wave.rs.
  • How many of the title's shaders change isn't measured here (that needs game data). Windows only; Linux and macOS are not covered.

GPUs tested: n/a (CPU unit tests)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a: this fixes the section scan, not an instruction's semantics)

🤖 Generated with Claude Code

wave::sections() looks in a block for whole-wave sections: a saveexec of
every lane, the code under it, and the EXEC restore that ends it. When a
saveexec's restore was not in the block (it lies in a later one), the scan
stopped there (`let Some(end) = end else { break; }`), so every whole-wave
section later in the same block was missed and its reduction translated
the general way instead of as the section's shape. Such a saveexec is no
section; the scan now goes on past it.

Test: an_unmatched_saveexec_leaves_the_later_sections_of_its_block puts an
unmatched s_or_saveexec_b64 before the wave64 OR reduction the other tests
use and expects that section and its shape at index 1. On main it fails
("the section after an unmatched saveexec", left None).

Port of shh's fix and test in the sibling VIctory tree (TaynanGT/Vice-Tay
17e3e2c, GPL-3.0). The shader cache key covers leonida-shader/src, so
cached translations are made again.

Co-Authored-By: shh <marek8788@proton.me>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

Verified by OreoFR on macOS 27.2.1, Apple M5, Rust 1.98.1, cargo test -p leonida-shader, main 012b79a, PR head 709a6bb. MEASURED:

Not covered: the effect on real shaders (how many translations change, DXIL validation, the image); this is the unit test only.

AI-assisted: Claude Code

@merzleckeier

Copy link
Copy Markdown
Contributor

Independent Linux and Wine verification of 709a6bbb1 against 012b79a67 with Rust 1.98.1. The PR also merges cleanly into 0568cd62e; the shader crate and Cargo inputs are identical for that merge.

  • CachyOS, cargo test --locked --offline -p leonida-shader --lib: 228 passed on main, 229 on the PR, 2 ignored on each. The PR's regression test also failed with the test hunk applied to main and passed with the fix.

  • MSVC x64 builds under Wine 11.22: all 10 stock emit::wave::tests passed on main and all 11 on the PR.

  • I added two local synthetic cases for Wave32 and Wave64. Each puts s_or_saveexec -1 and s_nand_saveexec 0 before two complete sections in one block, then uses an actual s_branch to the block that restores both outer masks. The cases check the two section boundaries and the constant each section produces. main found neither section in either wave width. The PR found both and returned the expected constants in both cases.

CPU-only synthetic tests. AI-assisted with OpenAI Codex.

@MoonEyes

Copy link
Copy Markdown
Collaborator

@Kal0s-design could you merge current main into this branch (a merge, no rebase or force-push)? The two red checks on 709a6bbb1 (check and linux-python-and-viewer) show the same failing test names as main at 012b79a67, and main has had a fix since #2708.

OreoFR's and merzleckeier's results are on 709a6bbb1, so the red/green will need to be run again on the new head.

AI-assisted: yes

@Kal0s-design

Copy link
Copy Markdown
Contributor Author

Done: merged main (d367675c7) into the branch, new head 61945356f (merge commit, no rebase or force-push of the PR's commit). cargo test -p leonida-shader --lib on the new head: 386 passed, 4 ignored, 0 failed.

AI-assisted: yes

@MoonEyes

Copy link
Copy Markdown
Collaborator

@OreoFR @merzleckeier could one of you run your red/green again on the new head 61945356f? Kal0s-design merged main (d367675c7) into the branch, and your results are on the previous head 709a6bbb1. CI there: 4 of 5 checks green, check still running.

AI-assisted: yes

@ecox982745

Copy link
Copy Markdown
Contributor

Independent red/green and native RTX 4090 check of the updated head 61945356f. I merged it into ec1d85846 (tree 11d409073), then built both arms with Rust 1.98.1 MSVC in release mode on Windows 11.

  • The published regression test, copied by itself to the base, failed at the section after an unmatched saveexec (None instead of Active(Or, ...)). All 11 emit::wave::tests passed on the merged tree.
  • A separate synthetic Wave64 dispatch saves EXEC in the same block as a complete OR reduction and restores that outer mask in the target of an actual branch. The base recognized zero sections and emitted three group barriers. The PR recognized one section and emitted two. Both DXIL modules passed the pinned SDK 26100 validator, created hardware PSOs on the RTX 4090 (NVIDIA 616.64), and returned all 64 expected 0xffffffff words in three alternating runs per arm.

The test inputs were synthetic. The barrier count is for this case, and runtime timing remains unmeasured. AI-assisted with OpenAI Codex (GPT-6.2 Sol).

@OreoFR

OreoFR commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

RUN - in-game gate (Verified by OreoFR), PROVISIONAL - RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11.

  • Base: main f9f3ec4c6; B = main + this PR at 61945356f (git merge --no-ff), diag build of leonida.exe.
  • Recipe: play-d3d12.ps1 -SeriesS -NoDlss -NoFrameGen, BAR 0, texture budget 1024, standing in the world, 360 s per run (window: world +30 to +330 s). Each exe got a cold run and a throw-away run first; measured runs interleaved with main (A B B A).

Result: PASS - 0 device loss, 0 panics, every run alive at the end (cold run included).

Arm runs fps median p5 preparing (ms/frame)
main 4 24.53 (23.9-24.8) 20.35 30.3
main + #2710 2 24.30 (23.6, 25.0) 19.85 30.40
  • No fps regression: -0.9 %, within the ~4 % run-to-run noise on this machine.
  • One-time cost: the translator's fingerprint changes, so the first start translated every shader again before the title. It took 10.7 min to reach the title, against about 1 min. After that, the log's hits=/misses= line shows 41 misses in the cold run, 16 in the throw-away run and 0-1 in the measured runs. This is expected for any translator change.
  • Not measured: how many of the title's shaders the change affects (no counter for that), and image correctness beyond the run screenshots.

This run was AI-assisted (Claude Code, oreofr-agent).

@MoonEyes

Copy link
Copy Markdown
Collaborator

Merging. Head 61945356f merges cleanly with main 2a880c31d and does not force a rebase of #2566. Run in the game of 360 s, no device loss, posted on this head (OreoFR, RTX 4070 Laptop); red/green on RTX 4090 by another person; cargo test on Windows with main: leonida-shader 386 passed (main 385), 0 failed. First start after this retranslates the shaders. Merged by MoonEyes.

AI-assisted (Claude Code).

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.

5 participants