D3D12-SHADER-OPT-HOIST-HEAP-HANDLES: hoist uniform createHandleFromHeap out of branches and loops #2687

Manually merged
github merged 2 commits from github-bot/gh-2687 into gh-pr/2687/base 2026-12-01 11:14:06 +00:00 AGit
Owner

@⁠bobcatchris on GitHub · 2026-11-30 02:38 UTC · original
bobcatchris:feat/d3d12-shader-opt-hoist-heap-handles → main · +179 −1 · 1 files · 2 commits · merged 2026-12-01 11:11 UTC

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:

"descriptors made again in every branch of a program (createHandleFromHeap of an index that is a root constant plus an offset, behind a branch on a load)"

Because REUSE in d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs only deduplicates along a parent -> child dominator path, identical createHandleFromHeap + annotateHandle chains 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() under REUSE:

  • Marks uniform (nonuniform = false) dx.op.createHandleFromHeap calls, their wrapping annotateHandle calls, and their pure, non-trapping index/clamp operands (cbufferLoadLegacy, integer non-div/rem arithmetic, casts, extracts, dx.op.binary.i32 clamps).
  • Moves marked instructions in RPO order to the deepest dominating block that defines their operands (e.g. entry for root-constant + offset), preserving topological order and SSA dominance in a single linear pass.
  • Leaves nonuniform = true handles 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, driver 32.0.32029.2010), pinned toolchain rustc 1.98.1.

  • Red (before fix): uniform_heap_handles_hoist_out_of_branches_and_deduplicate failed because sibling branches kept separate annotateHandle instructions (Inst(6) != Inst(11)).
  • Green (after fix, on main 40e7fe69d):
    • 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)

**@⁠bobcatchris** on GitHub · 2026-11-30 02:38 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2687) `bobcatchris:feat/d3d12-shader-opt-hoist-heap-handles` → `main` · +179 −1 · 1 files · 2 commits · merged 2026-12-01 11:11 UTC 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`: > *"descriptors made again in every branch of a program (`createHandleFromHeap` of an index that is a root constant plus an offset, behind a branch on a load)"* Because `REUSE` in `d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs` only deduplicates along a parent -> child dominator path, identical `createHandleFromHeap` + `annotateHandle` chains 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()` under `REUSE`: - Marks uniform (`nonuniform = false`) `dx.op.createHandleFromHeap` calls, their wrapping `annotateHandle` calls, and their pure, non-trapping index/clamp operands (`cbufferLoadLegacy`, integer non-div/rem arithmetic, casts, extracts, `dx.op.binary.i32` clamps). - Moves marked instructions in RPO order to the deepest dominating block that defines their operands (e.g. `entry` for root-constant + offset), preserving topological order and SSA dominance in a single linear pass. - Leaves `nonuniform = true` handles 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`, driver `32.0.32029.2010`), pinned toolchain `rustc 1.98.1`. - **Red (before fix):** `uniform_heap_handles_hoist_out_of_branches_and_deduplicate` failed because sibling branches kept separate `annotateHandle` instructions (`Inst(6) != Inst(11)`). - **Green (after fix, on `main` `40e7fe69d`):** - `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)
Addresses the d3d12/docs/findings.md item ("descriptors made again in
every branch of a program (createHandleFromHeap of an index that is a
root constant plus an offset, behind a branch on a load)").

Adds Module::hoist_handles() under REUSE: marks uniform (nonuniform =
false) dx.op.createHandleFromHeap calls, their wrapping annotateHandle
calls, and their pure, non-trapping index/clamp operands
(cbufferLoadLegacy, integer non-div/rem arithmetic, casts, extracts,
dx.op.binary.i32 clamp), and hoists each in RPO order to the deepest
dominating block defining its operands. REUSE then collapses duplicate
handles across sibling branches and loops into a single descriptor
handle.

Nonuniform handles and handles whose index depends on branch-local
memory loads remain in their original blocks.

AI-assisted: Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-30 02:44:04 +00:00
Author
Owner

@⁠OreoFR on GitHub · 2026-11-30 02:50 UTC · original

Checked by OreoFR on macOS 27.2.1, Apple M5, Rust 1.98.1, cargo test -p leonida-shader, main 182d0c174, PR head 502b9a97b. MEASURED, partial:

  • main: 224 passed / 2 ignored; main + this PR: 225 passed / 2 ignored. The new test emit::bitcode::opt::tests::uniform_heap_handles_hoist_out_of_branches_and_deduplicate passes; nothing else changes.
  • No red: the test is in the same file as the change, so I could not run it against main's code.
  • Not covered: the effect on real shaders (DXIL validation, GPU dispatch, frame time).
  • Merges cleanly with main and with #2566.

AI-assisted: Claude Code

**@⁠OreoFR** on GitHub · 2026-11-30 02:50 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7267365955) Checked by OreoFR on macOS 27.2.1, Apple M5, Rust 1.98.1, `cargo test -p leonida-shader`, main 182d0c174, PR head 502b9a97b. MEASURED, partial: - main: 224 passed / 2 ignored; main + this PR: **225 passed / 2 ignored**. The new test `emit::bitcode::opt::tests::uniform_heap_handles_hoist_out_of_branches_and_deduplicate` passes; nothing else changes. - No red: the test is in the same file as the change, so I could not run it against main's code. - Not covered: the effect on real shaders (DXIL validation, GPU dispatch, frame time). - Merges cleanly with main and with #2566. AI-assisted: Claude Code
github-bot force-pushed github-bot/gh-2687 from 502b9a97b6 to 0f8f57b6a0 2026-11-30 11:25:17 +00:00 Compare
Author
Owner

@⁠Kal0s-design on GitHub · 2026-11-30 12:43 UTC · original

Independent Windows verification of 0f8f57b6a on main 497bdd039 (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.

  • Red: main + only this PR's test hunk. emit::bitcode::opt::tests::uniform_heap_handles_hoist_out_of_branches_and_deduplicate fails with uniform heap handles in sibling branches should hoist to entry and collapse to one, left: Inst(6), right: Inst(11); the other 14 bitcode::opt::tests pass.
  • Green: main + this PR, the whole crate including the tests that validate DXIL and run on the local GPU: 386 passed, 0 failed, 4 ignored. main on 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

**@⁠Kal0s-design** on GitHub · 2026-11-30 12:43 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7278842633) **Independent Windows verification** of `0f8f57b6a` on `main` `497bdd039` (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. - Red: `main` + only this PR's test hunk. `emit::bitcode::opt::tests::uniform_heap_handles_hoist_out_of_branches_and_deduplicate` fails with `uniform heap handles in sibling branches should hoist to entry and collapse to one`, `left: Inst(6)`, `right: Inst(11)`; the other 14 `bitcode::opt::tests` pass. - Green: `main` + this PR, the whole crate including the tests that validate DXIL and run on the local GPU: 386 passed, 0 failed, 4 ignored. `main` on 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
Author
Owner

@⁠merzleckeier on GitHub · 2026-11-30 13:03 UTC · original

Independent checks for hoisting boundaries. I merged 0f8f57b6 into f9f3ec4c (tree f9595f0d) on CachyOS with Rust 1.98.1. Hand-built IR fixtures only.

  • MEASURED, guarded operands. With REUSE | DEAD, the branch-local UDiv, SDiv, URem and 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.
  • MEASURED, loop index. A handle built in a loop body reads the induction PHI from the header. With 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 on main failed the expected header-location assertion. With DEAD alone, both revisions kept the handle and annotation in the body.
  • MEASURED, negative controls. The guard test failed on the division assertion when I temporarily allowed the optimizer to move divisions. When I treated every non-uniform flag as eligible, it failed on the dynamic-flag assertion. Each successful fixture also serialized with Module::write().

The stock cargo test -p leonida-shader --lib passed 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.

**@⁠merzleckeier** on GitHub · 2026-11-30 13:03 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7279306042) **Independent checks for hoisting boundaries.** I merged `0f8f57b6` into `f9f3ec4c` (tree `f9595f0d`) on CachyOS with Rust 1.98.1. Hand-built IR fixtures only. - **MEASURED, guarded operands.** With `REUSE | DEAD`, the branch-local `UDiv`, `SDiv`, `URem` and 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. - **MEASURED, loop index.** A handle built in a loop body reads the induction PHI from the header. With `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 on `main` failed the expected header-location assertion. With `DEAD` alone, both revisions kept the handle and annotation in the body. - **MEASURED, negative controls.** The guard test failed on the division assertion when I temporarily allowed the optimizer to move divisions. When I treated every non-uniform flag as eligible, it failed on the dynamic-flag assertion. Each successful fixture also serialized with `Module::write()`. The stock `cargo test -p leonida-shader --lib` passed 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.
Author
Owner

@⁠KelboLive on GitHub · 2026-11-30 16:05 UTC · original

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).

**@⁠KelboLive** on GitHub · 2026-11-30 16:05 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7284105404) 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).
Author
Owner

@⁠bobcatchris on GitHub · 2026-11-30 19:12 UTC · original

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 --release after the merge: 394 passed, 0 failed, 5 ignored.

AI-assisted: Claude Sonnet 5.5

**@⁠bobcatchris** on GitHub · 2026-11-30 19:12 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7289013928) 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 --release` after the merge: 394 passed, 0 failed, 5 ignored. AI-assisted: Claude Sonnet 5.5
github-bot changed target branch from main to gh-pr/2687/base 2026-12-01 11:13:51 +00:00
github manually merged commit 40a97ef09f into gh-pr/2687/base 2026-12-01 11:13:54 +00:00
Author
Owner

@⁠aslan2142 on GitHub · 2026-12-01 11:11 UTC · original · approved these changes

Verified hoist_handles in d3d12/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 in enum Bin (which has no SRem variant), phi and impure calls are excluded, and entry hoisting 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.)

**@⁠aslan2142** on GitHub · 2026-12-01 11:11 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2687#pullrequestreview-6130074024) · **approved these changes** Verified `hoist_handles` in `d3d12/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 in `enum Bin` (which has no `SRem` variant), `phi` and impure calls are excluded, and `entry` hoisting 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.)*
Author
Owner

@⁠enlight3d on GitHub · 2026-12-01 11:17 UTC · original

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 main e56aff1ce with git 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.
  • Red, by assertion: the merge with the call to hoist_handles() taken out of optimise (main's let mut changed = self.merge(passes); put back), the new test kept. cargo test -p leonida-shader --lib uniform_heap_handles fails on the first assertion: "uniform heap handles in sibling branches should hoist to entry and collapse to one", left Inst(6), right Inst(11). Same values as in the body.
  • Second mutation: the 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.
  • Third mutation, which survives: every operand kind made hoistable (_ => true at the end of the operand match, so divisions, remainders and any other load or call would move too). All 18 tests of bitcode::opt::tests still 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 no createHandleFromHeap, so hoist_handles returns at once there: that test does not exercise this pass.
  • Green: the file restored (git status clean), 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.md is clean on the merge (no switch added).
  • Not covered here: the game. The pass moves any uniform createHandleFromHeap up 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.

**@⁠enlight3d** on GitHub · 2026-12-01 11:17 UTC · [original](https://github.com/ProjectLeonida/mandy/pull/2687#issuecomment-7309447313) 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 main e56aff1ce with `git 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. - Red, by assertion: the merge with the call to `hoist_handles()` taken out of `optimise` (main's `let mut changed = self.merge(passes);` put back), the new test kept. `cargo test -p leonida-shader --lib uniform_heap_handles` fails on the first assertion: "uniform heap handles in sibling branches should hoist to entry and collapse to one", left `Inst(6)`, right `Inst(11)`. Same values as in the body. - Second mutation: the `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. - Third mutation, which survives: every operand kind made hoistable (`_ => true` at the end of the operand match, so divisions, remainders and any other load or call would move too). All 18 tests of `bitcode::opt::tests` still 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 no `createHandleFromHeap`, so `hoist_handles` returns at once there: that test does not exercise this pass. - Green: the file restored (`git status` clean), `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.md` is clean on the merge (no switch added). - Not covered here: the game. The pass moves any uniform `createHandleFromHeap` up 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.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github/ubiquitarianism--leonida!2687
No description provided.