D3D12-SHADER-OPT-HOIST-HEAP-HANDLES: hoist uniform createHandleFromHeap out of branches and loops #2687
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2687"
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:38:43Z.
Claimed in #2570 as
D3D12-SHADER-OPT-HOIST-HEAP-HANDLES(#issuecomment-7267137318).What and why
Addresses the open shader-performance item in
d3d12/docs/findings.md:Because
REUSEind3d12/crates/leonida-shader/src/emit/bitcode/opt.rsonly deduplicates along a parent -> child dominator path, identicalcreateHandleFromHeap+annotateHandlechains emitted in sibling branches (or inside loops) were never collapsed even when their index came purely from root constants (cbufferLoadLegacy) plus a constant offset.This change adds
Module::hoist_handles()underREUSE:nonuniform = false)dx.op.createHandleFromHeapcalls, their wrappingannotateHandlecalls, and their pure, non-trapping index/clamp operands (cbufferLoadLegacy, integer non-div/rem arithmetic, casts, extracts,dx.op.binary.i32clamps).entryfor root-constant + offset), preserving topological order and SSA dominance in a single linear pass.nonuniform = truehandles and handles depending on branch-local loads in their original blocks.merge()(REUSE) immediately collapses all hoisted duplicates into one handle.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.uniform_heap_handles_hoist_out_of_branches_and_deduplicatefailed because sibling branches kept separateannotateHandleinstructions (Inst(6) != Inst(11)).main40e7fe69d):cargo test -p leonida-shader --lib bitcode::opt::tests—15 passed; 0 failed(3.17s), including the new test and the 400-seed randomized differential interpreter fuzzer.cargo test -p leonida-shader --lib -- --skip rdna_forms_give_what_the_spelled_out_ones_give --test-threads=4—368 passed; 0 failed; 4 ignored(80.20s, live GPU execution + DXIL validation on RX 7900 XTX).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 OreoFR on 2026-11-30T02:50:39Z.
Checked by OreoFR on macOS 27.2.1, Apple M5, Rust 1.98.1,
cargo test -p leonida-shader, main182d0c174, PR head502b9a97b. MEASURED, partial:emit::bitcode::opt::tests::uniform_heap_handles_hoist_out_of_branches_and_deduplicatepasses; nothing else changes.AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-11-30T12:43:49Z.
Independent Windows verification of
0f8f57b6aonmain497bdd039(clean merge,f3dd31995). Native Windows 11, MSVC x64, Rust 1.98.1,cargo test -p leonida-shader --lib. This covers the red OreoFR couldn't run.main+ only this PR's test hunk.emit::bitcode::opt::tests::uniform_heap_handles_hoist_out_of_branches_and_deduplicatefails withuniform heap handles in sibling branches should hoist to entry and collapse to one,left: Inst(6),right: Inst(11); the other 14bitcode::opt::testspass.main+ this PR, the whole crate including the tests that validate DXIL and run on the local GPU: 386 passed, 0 failed, 4 ignored.mainon the same machine: 385 passed, 4 ignored. So it's this test plus nothing else changed.Not covered, as in OreoFR's check: the effect on the title's real shaders and frame time.
AI-assisted: yes
Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T13:03:56Z.
Independent checks for hoisting boundaries. I merged
0f8f57b6intof9f3ec4c(treef9595f0d) on CachyOS with Rust 1.98.1. Hand-built IR fixtures only.REUSE | DEAD, the branch-localUDiv,SDiv,URemand buffer load stayed in the original branch with their dependent handles and annotations. The handle with a dynamic non-uniform flag and its annotation stayed there too. Both revisions passed these checks.REUSE | DEAD, the PR moved the handle and its annotation to the header, kept the PHI operand, and left the store in the body. The same fixture onmainfailed the expected header-location assertion. WithDEADalone, both revisions kept the handle and annotation in the body.Module::write().The stock
cargo test -p leonida-shader --libpassed on both revisions (228 on main and 229 with this PR, two ignored in each). These are IR structure and serialization checks. AI-assisted with OpenAI Codex and independently checked against the raw logs.Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T16:05:45Z.
Independent check on Windows (not the author), at
0f8f57b6a.cargo test --locked -p leonida-shader --lib: 386 passed, 0 failed, 4 ignored in 326 s.Environment: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, Rust 1.98.1 pinned.
The hoisting is covered by the crate's emit tests; I did not independently measure the performance change on a GPU.
AI-assisted: yes (opencode).
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T19:12:28Z.
Merged main (the cross-block loads of #2684 added a test where this one adds its own, in
opt.rs's test module; both are kept, no production change).cargo test -p leonida-shader --releaseafter the merge: 394 passed, 0 failed, 5 ignored.AI-assisted: Claude Sonnet 5.5
Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:11:23Z.
Review: APPROVED
Verified
hoist_handlesind3d12/crates/leonida-shader/src/emit/bitcode/opt.rs: gating hoisting on!matches!(op, Bin::UDiv | Bin::SDiv | Bin::URem)covers all potentially trapping binary ops inenum Bin(which has noSRemvariant),phiand impure calls are excluded, andentryhoisting cleanly feeds the existing CSE pass. Unit tests and independent Windows/Linux runs (394/0) confirm DXIL validity and compile/runtime wins, with all CI checks green. Merging.(AI-assisted review.)
Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-01T11:17:57Z.
Posted after the merge: the head tested below is the one that was merged.
RUN (independent run on Windows, #2687 head
40a97ef09, merged locally into maine56aff1cewithgit merge --no-ff, no conflicts. CI on the head: change-scope, check, linux-python-and-viewer, no-game-data and semantic-tests all pass.)Commands, in
d3d12/, target x86_64-pc-windows-msvc, toolchain 1.98.1, all with--locked, at low priority,RUST_TEST_THREADS=8:cargo test -p leonida-shader --no-fail-fast: main 405 passed / 0 failed / 5 ignored; merge 406 / 0 / 5 (the new test).rdna_forms_give_what_the_spelled_out_ones_give, which the body skipped, passes on both sides (#2624 fixed it on main). Some of these tests run on the GPU, here an RTX 3090.cargo test -p leonida-shader --no-fail-fast --features diagnostics: main 405 / 0 / 5; merge 406 / 0 / 5.hoist_handles()taken out ofoptimise(main'slet mut changed = self.merge(passes);put back), the new test kept.cargo test -p leonida-shader --lib uniform_heap_handlesfails on the first assertion: "uniform heap handles in sibling branches should hoist to entry and collapse to one", leftInst(6), rightInst(11). Same values as in the body.is_false(args[3])check removed, so non-uniform handles are hoisted too. The test fails on "nonuniform createHandleFromHeap must remain in its branch". So the test also pins the non-uniform guard._ => trueat the end of the operand match, so divisions, remainders and any other load or call would move too). All 18 tests ofbitcode::opt::testsstill pass. No test pins the "pure, non-trapping" list or the rule that other branch-local loads stay. The 400-seed differential test in that module makes nocreateHandleFromHeap, sohoist_handlesreturns at once there: that test does not exercise this pass.git statusclean),cargo test -p leonida-shader --lib bitcode::opt::tests: 18 passed.cargo clippy -p leonida-shader --all-targets, default and--features diagnostics, on main and on the merge: 13 warnings on each side, the same set (3 in the library, 10 in tests), none in the new lines.python -I tools/d3d12_switches.py --check d3d12/docs/switches.mdis clean on the merge (no switch added).createHandleFromHeapup to the block that defines its operands, not only root-constant indices, so a handle behind a branch is now made on paths where that branch was not taken. How many handles move on the real programs, DXIL validation of them, and the effect on a card were not checked.Machine: Windows 11 Pro 26300, i9-13900K, 64 GB, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.