D3D12-SHADER-OPT-CROSS-BLOCK-LOADS: propagate LOADS across write-free dominating blocks #2684
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2684"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T02:29:10Z.
Claimed in #2570 as
D3D12-SHADER-OPT-CROSS-BLOCK-LOADS(#issuecomment-7266940327).What and why
Addresses the open shader-performance item in
d3d12/docs/findings.md:In
d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs,merge()previously calledread.clear()at the entry of every basic block, soLOADScould never reuse anEffect::Readsload across sequential blocks, single-predecessor branch targets, or write-free diamonds/loops.This change splits
Effect::Reads(memory loads) fromEffect::Lanes(wave/quad ops, which remain strictly block-local because active lanes can change across branches) and propagatesEffect::Readsalong the dominator tree:preds[b] == [idom]): inheritdom_readsdirectly fromidomin O(1).idomwhether any block on any path (including loop back-edges) executesEffect::Writes; preservedom_readsonly if no write can occur.Effect::Writes: cleardom_readsfor the remainder of the block and its dominator subtree, restoring the pre-block map onStep::Leaveso sibling branches are unaffected.Verification (Red -> Green)
Hardware / OS: native Windows 11 Pro (
10.0.26200) + AMD Radeon RX 7900 XTX (gfx1100, driver32.0.32029.2010), pinned toolchainrustc 1.98.1.cross_block_reads_reuse_dominating_load_without_writes,cross_block_reads_invalidated_when_branch_writes,cross_block_reads_in_loop_respect_backedge_writes) failed becauseLOADScleared reads at every block boundary.main40e7fe69d):cargo test -p leonida-shader --lib bitcode::opt::tests—17 passed; 0 failed(3.20s), including all 3 new tests and the 400-seed × 6-input × 12-pass randomized differential interpreter fuzzer (every_pass_leaves_what_random_programs_store).cargo test -p leonida-shader --lib -- --skip rdna_forms_give_what_the_spelled_out_ones_give --test-threads=4—370 passed; 0 failed; 4 ignored(81.00s, live GPU execution + DXIL validation on RX 7900 XTX; the 1 skipped test is the pre-existingmainfailure fixed in #2624).python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs— PASS.AI-assisted: Antigravity (Gemini 4 Neon)
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T02:38:18Z.
Independent CachyOS check of
d2be6ba70merged intomain182d0c174(merge6659ccd57, treed47886846). MEASURED The nativeleonida-shader --libsuites passed with 224 tests onmainand 227 on the merge, with two ignored on each.The positive diamond fixture currently uses both branch-local results in
joinas ordinary operands. Putting those results on the incoming edges of a PHI gives a valid test module before optimization as well. My fixture with the PHI executes both paths before and afterLOADS | DEADand compares their store traces. MEASURED The traces match. The merge reduces four loads to one for the write-free diamond and to three when one branch changes the loaded location.mainkeeps all four in both cases. The wave test keeps one call in each block and eliminates only the repeat inside the same block.READ IN CODE The new
LOADSblock reacheskey()forPureandWritesinstructions too.key()clones arguments for calls. Restoring the previous class filter skips those keys and keeps the same directed results.MEASURED Three interleaved release runs per arm, pinned to one CPU. These are the per-run medians in ms per synthetic module for
merge(LOADS | REUSE).mainThe second fixture retains 192 loads on
mainand 12 on both PR variants. The timing covers the optimizer pass on synthetic IR. AI-assisted with OpenAI Codex.Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T06:17:29Z.
Thanks for the careful review @merzleckeier! Restored the
matches!(class, Effect::Reads | Effect::Lanes)filter before callingself.key(i)soPureandWritesinstructions skip cloning call arguments, and updated the positive diamond test to route branch reads through aPhinode before optimization (17/17bitcode::opt::testspass). AI-assisted: Antigravity (Gemini 4 Neon).Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T07:30:51Z.
Thanks for updating both points. I retested
2edb15796on GitHub merge1a612697b(basebbd45f5cc) on CachyOS. The nativeleonida-shader --libsuite ran 228 passing tests on the base and 231 on the merge, with two ignored and zero failures on each. All three cross-block cases passed, including the updated PHI fixture, branch-store invalidation, and loop-backedge invalidation. I also verified that theReads | Lanescheck precedeskey()in this revision.AI-assisted with OpenAI Codex.
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T15:11:23Z.
Native Windows check of
bb68c6e19on currentmain232f9e603(no conflict,11fee995f). Windows 11, MSVC x64, Rust 1.98.1,cargo test -p leonida-shader --libon a hardware GPU (not WARP), so the DXIL validation and the GPU dispatch/readback tests run too. That's what the two Linux runs above don't cover.main+ only this PR's test hunk: the 3 newcross_block_reads_*tests fail (reuse_dominating_load_without_writes,invalidated_when_branch_writes,in_loop_respect_backedge_writes); the other 15bitcode::opttests pass.main+ this PR, the whole crate: 388 passed, 0 failed, 4 ignored.mainlists 389 tests (385 run + 4 ignored); the merge lists 392, so the 3 additions are all that changes and no existing test turns red with cross-blockLOADSon.Not covered: the effect on real shaders' DXIL and frame time.
AI-assisted: yes
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T16:45:32Z.
Pairwise merge check for the optimizer changes
MEASURED. I tested
bb68c6e19onmain08e42a516. This PR, #2687 (0f8f57b6a) and #2750 (55996ef0c) each merge cleanly into that baseline. Merging the resulting trees in pairs withgit merge-tree --write-treegives two conflicts ind3d12/crates/leonida-shader/src/emit/bitcode/opt.rs:cross_block_reads_*tests and the uniform-heap-handle test as separate functions.dom_readsandlane_reads. Preserve both roles if carrying #2750'sFxHashMapconversion forward. #2750 also declares its dependency on #2745.These are local merge checks. The two pairs still need tests after resolving the hunks. AI-assisted: Codex (GPT-6.2 Sol).
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T16:54:51Z.
Both optimizer merge resolutions tested
MEASURED. Follow-up to the pairwise merge check. I tested the exact PR heads
bb68c6e19,0f8f57b6a,88d0dbf9cand55996ef0cfrommain7663c34fc, with Rust 1.98.1 on CachyOS x86-64. I builtcargo test --locked --offline -p leonida-shader --lib --no-runand ran each test binary in three fresh processes.mainef5ebc8fb357d5506b800995cb6955e0e969b3759f7d1Every run had zero failures and two ignored tests. For #2687, I kept all three cross-block load tests and the heap-handle test as complete functions. For #2750, I used
FxHashMapforknown,dom_reads,lane_reads, and the matching saved-map type inStep::Leave. The two A/B diffs each cover #2750's six files.I captured optimizer bitcode and DXIL containers from the synthetic suite, keyed by test and emission ordinal. Adding #2750 preserved 4,923 bitcode captures and 100 DXIL containers byte for byte without #2687, and 4,924 bitcode captures and 100 DXIL containers with #2687. Each comparison agreed across all six runs.
Scope: local merge resolutions and CPU-side synthetic translation. AI-assisted: Codex (GPT-6.2 Sol).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T18:24:31Z.
RUN · VERIFY-2684-RTX4060 · by MoonEyes (another person than the author) on head
bb68c6e19, together with #2684 (bb68c6e19), #2685 (903e3731e) and #2757 (fe7c76d3b) merged intomain99167bcaf. Windows 11 Pro 26300, RTX 4060 8 GB,-SeriesS -FrameGen 2, diag build.cargo test -p leonida-shader --locked: 392 passed, 0 failed.AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T18:27:52Z.
Merging. Head
bb68c6e19merges cleanly withmain6cdc0a0d1and does not force a rebase of #2566. Two runs in the game (RTX 4060, the second 425 s steady, no device loss) with #2684, #2685 and #2757 together; cargo test 392 passed; checked by other people too. The shaders are translated again at the first start. Merged by MoonEyes.AI-assisted (Claude Code).