d3d12-shader: merge after chains() and accept commuted And in canonical() #3156

Open
molodayamama wants to merge 1 commit from upstream-pr/3156 into main
molodayamama commented 2026-12-02 14:38:46 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by molodayamama on 2026-12-02T14:38:46Z.


Summary

Fixes two bugs in d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs that caused CHAINS (LEONIDA_SHADER_OPT bit 256) to leave duplicate and i1 chains behind in large shaders instead of deduplicating shared prefixes:

  1. Accept commuted Bin::And operands in chains()'s canonical() check: Module::key() commutes integer Bin::And, so merge() (REUSE) can replace a & b with an earlier dominating b & a. Previously canonical() only checked Op::Bin(Bin::And, x, y) if y == want, causing it to return false on (b & a) & c, re-emit the entire chain on every round, and ping-pong with merge() through all 4 rounds.
  2. Run a trailing merge() if chains() fired on the final round: Module::optimise ran chains() at the end of for _ in 0..4, so any chains emitted on round 4 were never seen by merge() (REUSE). Tracking need_merge and running one final self.merge(passes) when chains() modified the module on the last iteration guarantees every canonicalized prefix is deduplicated.

Verification

  • Red/Green unit tests (opt.rs):
    • chains_accepts_commuted_and_operands — failed on main (3 != 2 Bin::Ands left behind), passes with fix (!m.chains() after merge(REUSE) and 2 Bin::Ands total).
    • optimise_merges_chains_emitted_on_final_round — failed on main (3 != 1 Bin::Ands in dominated block b4), passes with fix (1 Bin::And in b4).
    • cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib bitcode::opt::tests: 23/23 passed (including the 400 random-program interpreter equivalence suite).
  • Offline shader census & dxil.dll validation (LEONIDA_HOST_MAX_WAVE=32, 25,687 emitted shaders):
    • 7,271 shaders strictly smaller, 0 bigger.
    • Total DXIL instructions: 103,500,704 -> 103,045,759 (-454,945, -0.44%; arith: 49,590,221 -> 49,135,276, -0.92%; largest single-shader drop: -18,498 instructions).
    • dxil.dll validation counts unchanged (2,694 VP, 2,675 CS, 2,650 MS validated; 0 regressions).
    • Full-archive translation wall clock: 106.8 s -> 104.5 s.
  • Checks & Review:
    • cargo clippy --manifest-path d3d12/Cargo.toml -p leonida-shader --lib -- -D warnings: 0 warnings.
    • python -I scripts/check_no_game_data.py: clean.
    • python -I tools/d3d12_switches.py --check d3d12/docs/switches.md: clean.
    • Independent read-only review: LGTM (0 blockers).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5608649335 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3156). Originally posted by [molodayamama](https://github.com/molodayamama) on 2026-12-02T14:38:46Z. --- ### Summary Fixes two bugs in `d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs` that caused `CHAINS` (`LEONIDA_SHADER_OPT` bit 256) to leave duplicate `and i1` chains behind in large shaders instead of deduplicating shared prefixes: 1. **Accept commuted `Bin::And` operands in `chains()`'s `canonical()` check:** `Module::key()` commutes integer `Bin::And`, so `merge()` (`REUSE`) can replace `a & b` with an earlier dominating `b & a`. Previously `canonical()` only checked `Op::Bin(Bin::And, x, y) if y == want`, causing it to return `false` on `(b & a) & c`, re-emit the entire chain on every round, and ping-pong with `merge()` through all 4 rounds. 2. **Run a trailing `merge()` if `chains()` fired on the final round:** `Module::optimise` ran `chains()` at the end of `for _ in 0..4`, so any chains emitted on round 4 were never seen by `merge()` (`REUSE`). Tracking `need_merge` and running one final `self.merge(passes)` when `chains()` modified the module on the last iteration guarantees every canonicalized prefix is deduplicated. ### Verification - **Red/Green unit tests (`opt.rs`):** - `chains_accepts_commuted_and_operands` — failed on `main` (`3 != 2` `Bin::And`s left behind), passes with fix (`!m.chains()` after `merge(REUSE)` and `2` `Bin::And`s total). - `optimise_merges_chains_emitted_on_final_round` — failed on `main` (`3 != 1` `Bin::And`s in dominated block `b4`), passes with fix (`1` `Bin::And` in `b4`). - `cargo test --manifest-path d3d12/Cargo.toml -p leonida-shader --lib bitcode::opt::tests`: 23/23 passed (including the 400 random-program interpreter equivalence suite). - **Offline shader census & `dxil.dll` validation (`LEONIDA_HOST_MAX_WAVE=32`, 25,687 emitted shaders):** - `7,271` shaders strictly smaller, `0` bigger. - Total DXIL instructions: `103,500,704 -> 103,045,759` (`-454,945`, `-0.44%`; `arith`: `49,590,221 -> 49,135,276`, `-0.92%`; largest single-shader drop: `-18,498` instructions). - `dxil.dll` validation counts unchanged (`2,694` VP, `2,675` CS, `2,650` MS validated; `0` regressions). - Full-archive translation wall clock: `106.8 s -> 104.5 s`. - **Checks & Review:** - `cargo clippy --manifest-path d3d12/Cargo.toml -p leonida-shader --lib -- -D warnings`: 0 warnings. - `python -I scripts/check_no_game_data.py`: clean. - `python -I tools/d3d12_switches.py --check d3d12/docs/switches.md`: clean. - Independent read-only review: `LGTM` (0 blockers). AI-assisted.
merzleckeier commented 2026-12-02 14:43:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-02T14:43:59Z.

Review: COMMENTED


Independently checked 53c3100 on main acc350745 with native Linux Rust 1.98.1 and separate Cargo target directories. With the two submitted tests copied onto main, the optimizer suite passed 21 tests and failed exactly the two new cases. The commuted prefix was rewritten, and the final-round test left three And instructions in b4 where it expected one. The clean PR build passed 255 shader-crate tests, with two existing ignores. This includes all 23 optimizer tests and the 400-program interpreter check.

I also ran local variants of the two new fixtures with independently controlled Boolean inputs. For all 8 and 16 input combinations, their store and return traces matched before and after optimization. The structural assertions still passed in those variants. These were synthetic checks. Archive size, throughput and native D3D12 validation were outside this run. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6153040001 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3156#pullrequestreview-6153040001). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-02T14:43:59Z. **Review:** COMMENTED --- Independently checked `53c3100` on `main` `acc350745` with native Linux Rust 1.98.1 and separate Cargo target directories. With the two submitted tests copied onto `main`, the optimizer suite passed 21 tests and failed exactly the two new cases. The commuted prefix was rewritten, and the final-round test left three `And` instructions in `b4` where it expected one. The clean PR build passed 255 shader-crate tests, with two existing ignores. This includes all 23 optimizer tests and the 400-program interpreter check. I also ran local variants of the two new fixtures with independently controlled Boolean inputs. For all 8 and 16 input combinations, their store and return traces matched before and after optimization. The structural assertions still passed in those variants. These were synthetic checks. Archive size, throughput and native D3D12 validation were outside this run. AI-assisted with OpenAI Codex.
fiestorra commented 2026-12-02 15:41:06 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T15:41:06Z.


Verified on this machine: red then green, no test moved, and the whole archive translates to the same numbers as main.

python -I scripts/red_green.py 3156                    15.7 min

base origin/main 0d6757b70, head 3156 53c3100ce, merge base 9cb1902d3
ran: cargo -p leonida-shader
red then green (2):
  opt::tests::chains_accepts_commuted_and_operands
  opt::tests::optimise_merges_chains_emitted_on_final_round
pass on both: 428
verdict: red then green

And the corpus, because this is the optimiser and a unit test only says the two cases are fixed: leonida translate over the title's shaders.rpf, the pull request's build against main's build, both --jobs 16:

                    PR build (@ 53c3100ce)          main's build
programs            26084 of 26084 (40597574 IR)     26084 of 26084 (40597574 IR)
vertex and pixel    14247 of 14247                   14247 of 14247
mesh                8765 of 8765                     8765 of 8765
compute             2677 of 2682                      2677 of 2682
emit refusals       5                                 5

Identical to the instruction, including all 40,597,574 IR instructions - so the change moves nothing on the 26,084 programs the archive holds, which is what a fix to two edge cases of CHAINS should look like from outside.

What that does not say, and I am not claiming it:

  • that the two bugs never fire on the title's shaders - the counts are the same, and the count is not the code: the emitted bitcode could differ on a program and I would not see it here (I compared totals, not hashes);
  • CHAINS is bit 256 of LEONIDA_SHADER_OPT and part of MODULE, so it was on in both runs - what the runs say is that turning the fixes on does not change the corpus result, not that the pass does nothing;
  • no game run: whether the fixed pass makes a frame faster is not something this machine can answer.

AI-assisted: yes - DeepSeek v4.1 (flash).

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7350744665 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3156#issuecomment-7350744665). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T15:41:06Z. --- **Verified on this machine: `red then green`, no test moved, and the whole archive translates to the same numbers as `main`.** ``` python -I scripts/red_green.py 3156 15.7 min base origin/main 0d6757b70, head 3156 53c3100ce, merge base 9cb1902d3 ran: cargo -p leonida-shader red then green (2): opt::tests::chains_accepts_commuted_and_operands opt::tests::optimise_merges_chains_emitted_on_final_round pass on both: 428 verdict: red then green ``` **And the corpus**, because this is the optimiser and a unit test only says the two cases are fixed: `leonida translate` over the title's `shaders.rpf`, the pull request's build against `main`'s build, both `--jobs 16`: ``` PR build (@ 53c3100ce) main's build programs 26084 of 26084 (40597574 IR) 26084 of 26084 (40597574 IR) vertex and pixel 14247 of 14247 14247 of 14247 mesh 8765 of 8765 8765 of 8765 compute 2677 of 2682 2677 of 2682 emit refusals 5 5 ``` **Identical to the instruction, including all 40,597,574 IR instructions** - **so the change moves nothing on the 26,084 programs the archive holds**, which is what a fix to two edge cases of `CHAINS` should look like from outside. **What that does not say, and I am not claiming it:** - **that the two bugs never fire on the title's shaders** - the counts are the same, and the count is not the code: **the emitted bitcode could differ on a program and I would not see it here** (I compared totals, not hashes); - **`CHAINS` is bit 256 of `LEONIDA_SHADER_OPT` and part of `MODULE`**, so it was on in both runs - what the runs say is that **turning the fixes on does not change the corpus result**, not that the pass does nothing; - **no game run**: whether the fixed pass makes a frame faster is not something this machine can answer. **AI-assisted:** yes - DeepSeek v4.1 (flash). Written with an AI assistant.
Kal0s-design commented 2026-12-02 17:54:00 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T17:54:00Z.


Ran natively on Windows 11, head 53c3100 merged into current main (45dc41f, clean merge). The runs below were done on main 5b1fff8, where leonida-shader is byte-identical to 45dc41f. Rust 1.98.1, --locked.

Below is what this thread does not have yet: how this PR overlaps with #2800, and a differential fuzz of chains().

Overlap with #2800 (measured)

#2800 adds the same canonical() arm, if x == want => cur = y, character for character. Beyond that the two PRs share nothing. #3156 adds the trailing merge(). #2800 (b6ebced) gives same-block phis distinct birth positions in chains().

  • main + #3156 + #2800 merges cleanly in either order. Both orders give the same tree.
  • Five new tests in total (two from this PR, three from #2800). I ran them against the optimiser code of each variant, from the merged file with the other PR's code hunks reverted:
test main #3156 code #2800 code both
chains_accepts_commuted_and_operands FAIL ok ok ok
optimise_merges_chains_emitted_on_final_round FAIL (b3) ok FAIL (b4: 3 and, want 1) ok
a_chain_with_the_operands_of_its_steps_the_other_way_round_is_left_as_it_is FAIL ok ok ok
a_chain_with_its_terms_out_of_order_is_still_rewritten (control) ok ok ok ok
a_chain_of_three_phis_in_the_same_block_settles_in_one_pass FAIL FAIL (second chains() returns true) ok ok

Neither PR subsumes the other:

  • #2800 does not include the trailing merge. Your final-round test fails on #2800's code.
  • This PR does not include the phi fix. #2800's phi test fails on this PR's code.

A residual loop this PR leaves (measured)

With this PR's code, a chain over one dominating term and two i1 phis of one block is rewritten on every call. The shape is (br & p0) & p1, with br a compare in the entry block and p0/p1 phis of the join:

chains() called 4 times main #3156 code #2800 code
(br & p0) & p1 true ×4 true ×4 false ×4
(p0 & p1) & t (t made in the join) true ×4 false ×4 false ×4

Three phis are not needed. Two tied phis are enough when they are not the first two terms, because the new arm accepts either operand order only at the lowest step. #2800's tie-break closes it.

Differential fuzz of chains() (throwaway test, not proposed for the PR)

Programs come from two generators:

  • 20,000 synthetic programs:
    • 3–7 compare leaves from input/buffer loads.
    • Nested and (operands in either order), or, xor, xor true, select(c, t, false), select(c, false, f), i1 add/mul, and zext → add/mul → compare.
    • Repeated terms, true/false constants, and commuted copies of ands already built.
    • 0–2 diamonds with 1–4 i1 phis.
  • The crate's own random_program, seeds 1–10,000.

Each program ran in the crate's interpreter (run) on 12 input vectors, before and after optimise() with 6 pass sets: CHAINS, +REUSE, +DEAD, +FOLD, GUARDS|CHAINS|REUSE|DEAD, ALL. Each was also optimised a second time.

To check that the harness can catch a miscompile, I ran it on a build where chains() drops the last term of a 4+ term chain. That build gave 70,100 mismatches.

Synthetic generator, 20,000 programs:

main #3156 #2800 both
store/return traces differ from the unoptimised program 0 0 0 0
chains() + merge(REUSE) not settled after 12 alternations 10,726 748 0 0
chains() still fires right after optimise(CHAINS) 1,018 751 0 0
optimise(CHAINS|REUSE) twice ≠ once 10,711 748 0 0
optimise(ALL) twice ≠ once 5,455 591 1 1
live instructions after ALL (ands) 1,695,006 (343,670) 1,680,803 (329,344) 1,680,781 (329,317) 1,680,781 (329,317)
optimise(ALL) time, 3 passes over the set 10.8 s 6.0 s 6.7 s 6.7 s

random_program, 10,000 seeds:

main #3156 #2800 both
traces differ 0 0 0 0
not settled after 12 alternations 363 0 0 0
optimise(ALL) twice ≠ once 9 2 2 2
live instructions after ALL 800,498 800,491 800,491 800,491
  • All 748 programs still unsettled with this PR are settled with #2800's code. The first one (seed 21) has the (dominating term & phi & phi) shape above.
  • The 1–2 programs where optimise(ALL) twice ≠ once with both PRs behave the same with CHAINS turned off (ALL & !CHAINS, GUARDS|REUSE|DEAD) and on main. They do not come from either PR.
  • Across all 30,000 programs, adding this PR's trailing merge on top of #2800 changed no instruction count. Only your hand-built three-round-cascade fixture needs it. It is still a real fix: #2800 alone fails that test.
  • Timing is a rough figure, not a benchmark: release build, run alongside a GPU test run.
  • On the synthetic generator, CHAINS makes ALL output larger than ALL & !CHAINS in every variant: 1,652,367 live and 266,546 ands without it. It does this less with the PRs than on main. Shared sub-ands get rebuilt per root. This is a property of the pass on these shapes, not of this PR.

Suites (measured)

tree cargo test -p leonida-shader, NVIDIA card WARP
main + #3156 430 passed, 0 failed, 6 ignored not run
main + #3156 + #2800 433 passed, 0 failed, 6 ignored 425 passed, 8 failed
main — 420 passed, 8 failed (the same 8)

The 8 WARP failures are the same on main. They are pipeline creation failing with "The parameter is incorrect", in pack_tests, valu_tests and wave64_tests. They are not related to this PR.

cargo clippy -p leonida-shader --all-targets: the same 6 warnings as main on every tree, none new.

Reading, not measured

  • The trailing merge runs only when chains() fired in the last round and passes has one of FOLD|REUSE|FACTS|LOADS. Nothing runs after it except DEAD. A prune/guards opportunity the extra merge opens is left for the next optimise call. That matches the residual counts above, which are the same with CHAINS off.

Not checked: game shaders or archive output, any in-game run, debug-layer runs, rustfmt, other crates. WARP was not run for this PR on its own.

Suggestion: the two PRs are complementary. Merging both, or folding #2800's born change into this one, gives zero unsettled programs in the fuzz above.

GPUs tested: one NVIDIA desktop card + WARP

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7353753157 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3156#issuecomment-7353753157). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T17:54:00Z. --- Ran natively on Windows 11, head `53c3100` merged into current `main` (`45dc41f`, clean merge). The runs below were done on `main` `5b1fff8`, where `leonida-shader` is byte-identical to `45dc41f`. Rust 1.98.1, `--locked`. Below is what this thread does not have yet: how this PR overlaps with #2800, and a differential fuzz of `chains()`. **Overlap with #2800 (measured)** #2800 adds the same `canonical()` arm, `if x == want => cur = y`, character for character. Beyond that the two PRs share nothing. #3156 adds the trailing `merge()`. #2800 (`b6ebced`) gives same-block phis distinct birth positions in `chains()`. - `main` + #3156 + #2800 merges cleanly in either order. Both orders give the same tree. - Five new tests in total (two from this PR, three from #2800). I ran them against the optimiser code of each variant, from the merged file with the other PR's code hunks reverted: | test | main | #3156 code | #2800 code | both | |---|---|---|---|---| | `chains_accepts_commuted_and_operands` | FAIL | ok | ok | ok | | `optimise_merges_chains_emitted_on_final_round` | FAIL (b3) | ok | FAIL (b4: 3 `and`, want 1) | ok | | `a_chain_with_the_operands_of_its_steps_the_other_way_round_is_left_as_it_is` | FAIL | ok | ok | ok | | `a_chain_with_its_terms_out_of_order_is_still_rewritten` (control) | ok | ok | ok | ok | | `a_chain_of_three_phis_in_the_same_block_settles_in_one_pass` | FAIL | **FAIL** (second `chains()` returns true) | ok | ok | Neither PR subsumes the other: - #2800 does not include the trailing merge. Your final-round test fails on #2800's code. - This PR does not include the phi fix. #2800's phi test fails on this PR's code. **A residual loop this PR leaves (measured)** With this PR's code, a chain over one dominating term and two `i1` phis of one block is rewritten on every call. The shape is `(br & p0) & p1`, with `br` a compare in the entry block and `p0`/`p1` phis of the join: | `chains()` called 4 times | main | #3156 code | #2800 code | |---|---|---|---| | `(br & p0) & p1` | true ×4 | **true ×4** | false ×4 | | `(p0 & p1) & t` (`t` made in the join) | true ×4 | false ×4 | false ×4 | Three phis are not needed. Two tied phis are enough when they are not the first two terms, because the new arm accepts either operand order only at the lowest step. #2800's tie-break closes it. **Differential fuzz of `chains()` (throwaway test, not proposed for the PR)** Programs come from two generators: - 20,000 synthetic programs: - 3–7 compare leaves from input/buffer loads. - Nested `and` (operands in either order), `or`, `xor`, `xor true`, `select(c, t, false)`, `select(c, false, f)`, `i1` `add`/`mul`, and `zext` → `add`/`mul` → compare. - Repeated terms, `true`/`false` constants, and commuted copies of `and`s already built. - 0–2 diamonds with 1–4 `i1` phis. - The crate's own `random_program`, seeds 1–10,000. Each program ran in the crate's interpreter (`run`) on 12 input vectors, before and after `optimise()` with 6 pass sets: `CHAINS`, `+REUSE`, `+DEAD`, `+FOLD`, `GUARDS|CHAINS|REUSE|DEAD`, `ALL`. Each was also optimised a second time. To check that the harness can catch a miscompile, I ran it on a build where `chains()` drops the last term of a 4+ term chain. That build gave 70,100 mismatches. Synthetic generator, 20,000 programs: | | main | #3156 | #2800 | both | |---|---|---|---|---| | store/return traces differ from the unoptimised program | 0 | 0 | 0 | 0 | | `chains()` + `merge(REUSE)` not settled after 12 alternations | 10,726 | 748 | 0 | 0 | | `chains()` still fires right after `optimise(CHAINS)` | 1,018 | 751 | 0 | 0 | | `optimise(CHAINS\|REUSE)` twice ≠ once | 10,711 | 748 | 0 | 0 | | `optimise(ALL)` twice ≠ once | 5,455 | 591 | 1 | 1 | | live instructions after `ALL` (`and`s) | 1,695,006 (343,670) | 1,680,803 (329,344) | 1,680,781 (329,317) | 1,680,781 (329,317) | | `optimise(ALL)` time, 3 passes over the set | 10.8 s | 6.0 s | 6.7 s | 6.7 s | `random_program`, 10,000 seeds: | | main | #3156 | #2800 | both | |---|---|---|---|---| | traces differ | 0 | 0 | 0 | 0 | | not settled after 12 alternations | 363 | 0 | 0 | 0 | | `optimise(ALL)` twice ≠ once | 9 | 2 | 2 | 2 | | live instructions after `ALL` | 800,498 | 800,491 | 800,491 | 800,491 | - All 748 programs still unsettled with this PR are settled with #2800's code. The first one (seed 21) has the `(dominating term & phi & phi)` shape above. - The 1–2 programs where `optimise(ALL)` twice ≠ once with both PRs behave the same with `CHAINS` turned off (`ALL & !CHAINS`, `GUARDS|REUSE|DEAD`) and on `main`. They do not come from either PR. - Across all 30,000 programs, adding this PR's trailing merge on top of #2800 changed no instruction count. Only your hand-built three-round-cascade fixture needs it. It is still a real fix: #2800 alone fails that test. - Timing is a rough figure, not a benchmark: release build, run alongside a GPU test run. - On the synthetic generator, `CHAINS` makes `ALL` output larger than `ALL & !CHAINS` in every variant: 1,652,367 live and 266,546 `and`s without it. It does this less with the PRs than on `main`. Shared sub-`and`s get rebuilt per root. This is a property of the pass on these shapes, not of this PR. **Suites (measured)** | tree | `cargo test -p leonida-shader`, NVIDIA card | WARP | |---|---|---| | main + #3156 | 430 passed, 0 failed, 6 ignored | not run | | main + #3156 + #2800 | 433 passed, 0 failed, 6 ignored | 425 passed, 8 failed | | main | — | 420 passed, 8 failed (the same 8) | The 8 WARP failures are the same on `main`. They are pipeline creation failing with "The parameter is incorrect", in `pack_tests`, `valu_tests` and `wave64_tests`. They are not related to this PR. `cargo clippy -p leonida-shader --all-targets`: the same 6 warnings as `main` on every tree, none new. **Reading, not measured** - The trailing merge runs only when `chains()` fired in the last round and `passes` has one of `FOLD|REUSE|FACTS|LOADS`. Nothing runs after it except `DEAD`. A `prune`/`guards` opportunity the extra merge opens is left for the next `optimise` call. That matches the residual counts above, which are the same with `CHAINS` off. Not checked: game shaders or archive output, any in-game run, debug-layer runs, `rustfmt`, other crates. WARP was not run for this PR on its own. Suggestion: the two PRs are complementary. Merging both, or folding #2800's `born` change into this one, gives zero unsettled programs in the fuzz above. GPUs tested: one NVIDIA desktop card + WARP AI-assisted: yes (Claude Code).
MoonEyes commented 2026-12-02 18:01:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T18:01:59Z.


Code review, not built here (leonida-shader tests were run by fiestorra). Ready.

  • Head: 53c3100ce, merge base 9cb1902d3, 59 behind current main 45dc41f95. A trial merge is clean. CI on the head: all 6 checks pass.
  • Already verified on this exact head after its only commit: fiestorra ran python -I scripts/red_green.py 3156 (red then green on both new tests, 428 pass on both sides) and a full-corpus translate with identical counts (26,084 programs, 40,597,574 IR instructions). Nothing has changed since.

Review of d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs:

  • The commuted match Op::Bin(Bin::And, x, y) if x == want => cur = y in canonical() is the right fix for the ping-pong between REUSE, which picks c1 & a, and CHAINS, which re-emitted a & c1. chains_accepts_commuted_and_operands pins it directly (assert!(!m.chains()) after merge(REUSE)).
  • need_merge is set only by chains() in the last round that ran. A loop that ends on !changed leaves it false, so the extra merge runs only when round 4 still emitted chains. The cost is bounded at one more merge per optimise. The guard passes & (FOLD | REUSE | FACTS | LOADS) is narrower than the unconditional merge(passes) inside the loop. That is harmless: merge does nothing for the other passes.
  • Not a defect: a merge after the loop can itself enable another prune or chains that is never run. This was equally true before, since the loop is capped at 4 rounds.

Verdict: ready.

AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.


Generated by Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7353878297 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3156#issuecomment-7353878297). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T18:01:59Z. --- **Code review, not built here (`leonida-shader` tests were run by fiestorra). Ready.** - Head: `53c3100ce`, merge base `9cb1902d3`, 59 behind current `main` `45dc41f95`. A trial merge is clean. CI on the head: all 6 checks pass. - Already verified on this exact head after its only commit: fiestorra ran `python -I scripts/red_green.py 3156` (red then green on both new tests, 428 pass on both sides) and a full-corpus translate with identical counts (26,084 programs, 40,597,574 IR instructions). Nothing has changed since. **Review of `d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs`:** - The commuted match `Op::Bin(Bin::And, x, y) if x == want => cur = y` in `canonical()` is the right fix for the ping-pong between `REUSE`, which picks `c1 & a`, and `CHAINS`, which re-emitted `a & c1`. `chains_accepts_commuted_and_operands` pins it directly (`assert!(!m.chains())` after `merge(REUSE)`). - `need_merge` is set only by `chains()` in the last round that ran. A loop that ends on `!changed` leaves it false, so the extra `merge` runs only when round 4 still emitted chains. The cost is bounded at one more merge per `optimise`. The guard `passes & (FOLD | REUSE | FACTS | LOADS)` is narrower than the unconditional `merge(passes)` inside the loop. That is harmless: `merge` does nothing for the other passes. - Not a defect: a merge after the loop can itself enable another `prune` or `chains` that is never run. This was equally true before, since the loop is capped at 4 rounds. Verdict: **ready**. AI-assisted verification: Claude Opus (Claude Code), separate agent per batch. --- _Generated by [Claude Code](https://claude.ai/code)_
MoonEyes commented 2026-12-02 22:00:53 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T22:00:53Z.


This pull request touches the render path, so it needs an in-game run before it can be merged: unit tests and CI do not catch rendering regressions (a device loss after an earlier merge is why this gate exists).

Could you, or anyone with a working setup, post a run report on the current head merged onto current main:

  • the head SHA and the main SHA it was merged onto;
  • GPU, driver and OS;
  • at least 300 s in the world after loading;
  • device lost / crashes (count), and rendered fps (median) if you have them;
  • the same run on main alone for comparison, if you can.

Please do not attach logs or screenshots that show game content. If a run on this exact head is already posted, a link to it in a reply is enough.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7360101739 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3156#issuecomment-7360101739). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T22:00:53Z. --- This pull request touches the render path, so it needs an in-game run before it can be merged: unit tests and CI do not catch rendering regressions (a device loss after an earlier merge is why this gate exists). Could you, or anyone with a working setup, post a run report on the **current head** merged onto current `main`: - the head SHA and the `main` SHA it was merged onto; - GPU, driver and OS; - at least 300 s in the world after loading; - device lost / crashes (count), and rendered fps (median) if you have them; - the same run on `main` alone for comparison, if you can. Please do not attach logs or screenshots that show game content. If a run on this exact head is already posted, a link to it in a reply is enough. AI-assisted (Claude Code).
fiestorra commented 2026-12-02 23:36:29 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-02T23:36:29Z.


VERIFY-3156 — Windows nativo: red then green en el optimizador de bitcode

Own commit 53c3100ce — emit/bitcode/opt.rs, +132/−1

Ran at the head

cargo test -p leonida-shader --lib opt     23 passed; 0 failed
  emit::bitcode::opt::tests::chains_accepts_commuted_and_operands ... ok
  emit::bitcode::opt::tests::optimise_merges_chains_emitted_on_final_round ... ok

Negative control (synthetic): I disabled the commuted case (Op::Bin(Bin::And, x, y) if x == want → && false), rebuilt and ran:

FAILED: chains_accepts_commuted_and_operands

The test bites.

What the change is, in two halves:

  1. chains() accepts the commuted form — and is symmetric, so x & want and want & x are the same chain, and the recogniser only matched one order.
  2. A chain created on the final pass is merged — the walk now notes need_merge when chains() changed something and runs merge(passes) if any of FOLD | REUSE | FACTS | LOADS is on, instead of leaving a chain emitted by the last round unmerged. That is the classic final-round bug: every pass is correct in isolation and the last one has no successor to fix it.

What I could not test: a title frame whose shaders go through this path — the tests drive the optimiser's own representation.

Written with an AI assistant.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7362209620 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3156#issuecomment-7362209620). Originally posted by [fiestorra](https://github.com/fiestorra) on 2026-12-02T23:36:29Z. --- ## VERIFY-3156 — Windows nativo: `red then green` en el optimizador de bitcode | | | |---|---| | Own commit | `53c3100ce` — `emit/bitcode/opt.rs`, +132/−1 | **Ran at the head** ``` cargo test -p leonida-shader --lib opt 23 passed; 0 failed emit::bitcode::opt::tests::chains_accepts_commuted_and_operands ... ok emit::bitcode::opt::tests::optimise_merges_chains_emitted_on_final_round ... ok ``` **Negative control (synthetic)**: I disabled the commuted case (`Op::Bin(Bin::And, x, y) if x == want` → `&& false`), rebuilt and ran: ``` FAILED: chains_accepts_commuted_and_operands ``` **The test bites.** **What the change is, in two halves**: 1. **`chains()` accepts the commuted form** — `and` is symmetric, so `x & want` and `want & x` are the same chain, **and the recogniser only matched one order**. 2. **A chain created on the final pass is merged** — the walk now notes `need_merge` when `chains()` changed something and runs `merge(passes)` **if any of `FOLD | REUSE | FACTS | LOADS` is on**, instead of leaving a chain emitted by the last round unmerged. **That is the classic final-round bug: every pass is correct in isolation and the last one has no successor to fix it.** **What I could not test**: a title frame whose shaders go through this path — the tests drive the optimiser's own representation. Written with an AI assistant.
MoonEyes commented 2026-12-03 00:02:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-03T00:02:11Z.


RUN (in game, batch of nine pull requests), each head merged with git merge --no-ff onto main 81efe4566, in one build: #3136 74bd1bcd4, #3156 53c3100ce, #3206 d024194b3, #3248 efd21d867, #3256 e7aad1d4c, #3262 3643c2879, #2851 b83b0b375, #3125 89bf1d328, #2642 164506d01. RTX 4060 8 GB, driver 610.88, Windows 11. Series S, -Resolution 1080, frame generation 2x, -PrecompileUsed, the same walking route as the other runs on this machine, 330 s after reaching the world.

What Result
Device lost / device removed 0
Exceptions, panics, hang watchdog, GPU stall, title suspended 0
Process alive at the end yes, stopped by the time limit
Rendered fps, last 240 s (48 samples) median 15.6, mean 15.6
Rendered fps, last 120 s median 14.3
Time under 5 fps in the world 3 %

For reference, the same machine and route rendered 13.3 to 17.2 fps on other batches in the last day. These fps are a check that nothing fell over, not a measurement of a gain.

Verdict: PASS for the in-game gate (300 s or more in the world, no device loss) for this batch, on the heads listed above. If one of these pull requests changes its head, this run no longer covers it.

Limits: one run; arrival in the world was driven by keys and not checked on pictures; -PrecompileUsed was used, so the default start (every known pipeline precompiled) was not exercised. The precompile before the world took 619 s, against 157 s for the previous batch; the shader-side changes in this batch (#3156, #3262) probably left the pipeline cache cold (not checked). Switches that are off by default (for example #2851's LEONIDA_TITLE_MEMORY_SCALE, #3136's timeline attribution) were not turned on: this run covers their default path only. #2642 targets Wine and was run on Windows only.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7362720196 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3156#issuecomment-7362720196). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-03T00:02:11Z. --- RUN (in game, batch of nine pull requests), each head merged with `git merge --no-ff` onto main `81efe4566`, in one build: #3136 `74bd1bcd4`, #3156 `53c3100ce`, #3206 `d024194b3`, #3248 `efd21d867`, #3256 `e7aad1d4c`, #3262 `3643c2879`, #2851 `b83b0b375`, #3125 `89bf1d328`, #2642 `164506d01`. RTX 4060 8 GB, driver 610.88, Windows 11. Series S, `-Resolution 1080`, frame generation 2x, `-PrecompileUsed`, the same walking route as the other runs on this machine, 330 s after reaching the world. | What | Result | |---|---| | Device lost / device removed | 0 | | Exceptions, panics, hang watchdog, GPU stall, title suspended | 0 | | Process alive at the end | yes, stopped by the time limit | | Rendered fps, last 240 s (48 samples) | median 15.6, mean 15.6 | | Rendered fps, last 120 s | median 14.3 | | Time under 5 fps in the world | 3 % | For reference, the same machine and route rendered 13.3 to 17.2 fps on other batches in the last day. These fps are a check that nothing fell over, not a measurement of a gain. Verdict: PASS for the in-game gate (300 s or more in the world, no device loss) for this batch, on the heads listed above. If one of these pull requests changes its head, this run no longer covers it. Limits: one run; arrival in the world was driven by keys and not checked on pictures; `-PrecompileUsed` was used, so the default start (every known pipeline precompiled) was not exercised. The precompile before the world took 619 s, against 157 s for the previous batch; the shader-side changes in this batch (#3156, #3262) probably left the pipeline cache cold (not checked). Switches that are off by default (for example #2851's `LEONIDA_TITLE_MEMORY_SCALE`, #3136's timeline attribution) were not turned on: this run covers their default path only. #2642 targets Wine and was run on Windows only. AI-assisted (Claude Code).
limittox commented 2026-12-03 02:41:49 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by limittox on 2026-12-03T02:41:49Z.


RUN (in game): this PR alone against main alone, 3 runs each, interleaved. No device loss or crash in any run. The fps difference is within run-to-run noise.

This adds to MoonEyes's batch run above in three ways: the PR is in a build of its own, a second GPU is used, and the launcher's default start (every known pipeline precompiled) is exercised.

  • Builds:
    • head 53c3100ce merged with git merge --no-ff onto main 81efe4566, with no conflicts;
    • main 81efe4566 alone.
  • System: RTX 3060 Ti 8 GB, driver 610.88, Windows 11 Pro 26300, i5-12600K, 32 GB.
  • Settings: the launcher's defaults, Series S, -Dlss quality (1280x720 → 1920x1080). LEONIDA_SHADER_OPT unset: its default, 447, includes CHAINS (256), so the changed pass ran.
  • Method:
    • Each build got one warm-up start first. It precompiled all 88,872 pipelines from a cold cache (88,865 made; 7 have no host pipeline).
    • Then 6 runs, in the order main, PR, PR, main, main, PR.
    • Each run:
      1. load the save;
      2. a few seconds of scripted W/S input;
      3. no input for about 3.5 minutes, while 17 screenshots are taken;
      4. a standing window of about 300 s, still with no input.
Run Build In the world Median fps, whole time in the world Median fps, standing window Device lost / removed Access violations, unhandled exceptions, panics, hang watchdog
1 main 635 s 24.7 24.8 0 0
2 PR 645 s 24.8 25.6 0 0
3 PR 645 s 24.5 24.9 0 0
4 main 650 s 23.9 24.4 0 0
5 main 655 s 23.1 23.6 0 0
6 PR 655 s 23.8 24.8 0 0

Every run exited normally after its time limit.

  • Fps:
    • Standing: PR 24.8–25.6 (median of runs 24.9), main 23.6–24.8 (median 24.4).
    • The PR was at or above main in every run, by about 0.5 fps at the median. With 3 runs each, and a 1.2 fps spread between main's own runs, I can't tell that apart from noise. I don't claim a gain.
    • In these logs, CPU-side preparation (including copies and texture uploads) is about 75–80 % of each frame. That is not where a cut in DXIL instructions (the PR description's offline count: −0.44 %) would be expected to show.
  • Draws per frame: the median while standing was about 3,500–3,600 in every run.
  • Slow frames:
    • Run 2, the PR build's first run after its warm-up, had 7 five-second samples under 5 fps. They fell in its first 3 minutes in the world, before the standing window.
    • Runs 3 and 4 had 1 each, in their first 10 s. Runs 1, 5 and 6 had none.
    • I haven't found the cause.
  • Picture: all 17 screenshots per run drew the scene, with no blank frames. The darkest was a dim scene, not a blank one. They stay local and aren't attached.
  • Precompile at the default start (one cold start each, so not a measurement):
    • main: 2,712 s, then the pipeline library written in 23 s.
    • PR: 2,894 s, then the pipeline library written in 388 s.
    • The PR's warm-up ran right after main's, which had just filled the NVIDIA driver cache with a full set of new pipelines. That may explain the slow library write, but I haven't checked.

Limits:

  • One machine and DLSS quality only.
  • Movement was scripted. I checked arrival in the world from the log and the screenshots.
  • Picture quality was not compared beyond "the scene draws".

AI-assisted: Claude Opus 5.5 (Claude Code) built both versions, scripted the runs and wrote this report. A separate read-only reviewer agent rechecked it against the raw logs.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7365760245 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3156#issuecomment-7365760245). Originally posted by [limittox](https://github.com/limittox) on 2026-12-03T02:41:49Z. --- **RUN (in game): this PR alone against `main` alone, 3 runs each, interleaved. No device loss or crash in any run. The fps difference is within run-to-run noise.** This adds to MoonEyes's batch run above in three ways: the PR is in a build of its own, a second GPU is used, and the launcher's default start (every known pipeline precompiled) is exercised. - **Builds:** - head `53c3100ce` merged with `git merge --no-ff` onto `main` `81efe4566`, with no conflicts; - `main` `81efe4566` alone. - **System:** RTX 3060 Ti 8 GB, driver 610.88, Windows 11 Pro 26300, i5-12600K, 32 GB. - **Settings:** the launcher's defaults, Series S, `-Dlss quality` (1280x720 → 1920x1080). `LEONIDA_SHADER_OPT` unset: its default, 447, includes `CHAINS` (256), so the changed pass ran. - **Method:** - Each build got one warm-up start first. It precompiled all 88,872 pipelines from a cold cache (88,865 made; 7 have no host pipeline). - Then 6 runs, in the order `main`, PR, PR, `main`, `main`, PR. - Each run: 1. load the save; 2. a few seconds of scripted W/S input; 3. no input for about 3.5 minutes, while 17 screenshots are taken; 4. a standing window of about 300 s, still with no input. | Run | Build | In the world | Median fps, whole time in the world | Median fps, standing window | Device lost / removed | Access violations, unhandled exceptions, panics, hang watchdog | |---|---|---|---|---|---|---| | 1 | `main` | 635 s | 24.7 | 24.8 | 0 | 0 | | 2 | PR | 645 s | 24.8 | 25.6 | 0 | 0 | | 3 | PR | 645 s | 24.5 | 24.9 | 0 | 0 | | 4 | `main` | 650 s | 23.9 | 24.4 | 0 | 0 | | 5 | `main` | 655 s | 23.1 | 23.6 | 0 | 0 | | 6 | PR | 655 s | 23.8 | 24.8 | 0 | 0 | Every run exited normally after its time limit. - **Fps:** - Standing: PR 24.8–25.6 (median of runs 24.9), `main` 23.6–24.8 (median 24.4). - The PR was at or above `main` in every run, by about 0.5 fps at the median. With 3 runs each, and a 1.2 fps spread between `main`'s own runs, I can't tell that apart from noise. I don't claim a gain. - In these logs, CPU-side preparation (including copies and texture uploads) is about 75–80 % of each frame. That is not where a cut in DXIL instructions (the PR description's offline count: −0.44 %) would be expected to show. - **Draws per frame:** the median while standing was about 3,500–3,600 in every run. - **Slow frames:** - Run 2, the PR build's first run after its warm-up, had 7 five-second samples under 5 fps. They fell in its first 3 minutes in the world, before the standing window. - Runs 3 and 4 had 1 each, in their first 10 s. Runs 1, 5 and 6 had none. - I haven't found the cause. - **Picture:** all 17 screenshots per run drew the scene, with no blank frames. The darkest was a dim scene, not a blank one. They stay local and aren't attached. - **Precompile at the default start** (one cold start each, so not a measurement): - `main`: 2,712 s, then the pipeline library written in 23 s. - PR: 2,894 s, then the pipeline library written in 388 s. - The PR's warm-up ran right after `main`'s, which had just filled the NVIDIA driver cache with a full set of new pipelines. That may explain the slow library write, but I haven't checked. **Limits:** - One machine and DLSS quality only. - Movement was scripted. I checked arrival in the world from the log and the screenshots. - Picture quality was not compared beyond "the scene draws". AI-assisted: Claude Opus 5.5 (Claude Code) built both versions, scripted the runs and wrote this report. A separate read-only reviewer agent rechecked it against the raw logs.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3156:upstream-pr/3156'
git switch 'upstream-pr/3156'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3156'
git switch 'upstream-pr/3156'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3156'
git switch 'upstream-pr/3156'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3156'
git switch 'main'
git merge --squash 'upstream-pr/3156'
git switch 'main'
git merge --ff-only 'upstream-pr/3156'
git switch 'main'
git merge 'upstream-pr/3156'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
ProjectLeonida/mandy!3156
No description provided.