D3D12-SHADER-OPT-CHAINS-CANONICAL: a chain with its steps the other way round is in order (-8 % translate CPU, -0.34 % DXIL instructions) #2800
Open
bobcatchris wants to merge 3 commits from
upstream-pr/2800 into main
pull from: upstream-pr/2800
merge into: ProjectLeonida:main
ProjectLeonida:main
ProjectLeonida:claude/loop-total
ProjectLeonida:claude/pops-collision-seed
ProjectLeonida:claude/dstorage-hold-page-reads
ProjectLeonida:claude/host-image-copy-switch
ProjectLeonida:feat/gdk-lifecycle-window-hook
ProjectLeonida:claude/d16s8-uav-nvidia-fallback
ProjectLeonida:feat/mfplat-offline-shim
ProjectLeonida:claude/gameinput-slow-reader
ProjectLeonida:fix/rt-null-barrier-clear
ProjectLeonida:claude/depth-resolve-sample-positions
ProjectLeonida:integration/palmetto-parity
ProjectLeonida:claude/valu-literal-sext
ProjectLeonida:claude/slot74-pool-handle
ProjectLeonida:claude/rgb9e5-uint-alias-view
ProjectLeonida:claude/fssm-root-layout-tables
ProjectLeonida:feat/gdk-speech-display-lifecycle
ProjectLeonida:asset-audio-skip-current
ProjectLeonida:qemmhd/old-9940-rectangle-quad-lists
ProjectLeonida:feat/ws2-offline-shim
ProjectLeonida:qemmhd/rt-d16-unorm-uav
ProjectLeonida:feat/xs-device-stops-7-9
ProjectLeonida:qemmhd/rt-depth-same-base-shapes
ProjectLeonida:qemmhd/rt-lazy-attachment-variant
ProjectLeonida:feat/acphal-xma2-decoder
ProjectLeonida:asset-audio-skip
ProjectLeonida:codex/coord-claim-check-20261124-7e48
ProjectLeonida:feat/xs-frame-pacing-presentx
ProjectLeonida:feat/xs-device-vm-options
ProjectLeonida:old-pr/9844
ProjectLeonida:old-pr/9978
ProjectLeonida:old-pr/9815
ProjectLeonida:old-pr/10057
ProjectLeonida:old-pr/9874
ProjectLeonida:old-pr/9965
ProjectLeonida:old-pr/9863
ProjectLeonida:old-pr/10068
ProjectLeonida:old-pr/10066
ProjectLeonida:old-pr/10067
ProjectLeonida:old-pr/10007
ProjectLeonida:old-pr/10061
ProjectLeonida:old-pr/10065
ProjectLeonida:old-pr/10059
ProjectLeonida:old-pr/10063
ProjectLeonida:old-pr/10055
ProjectLeonida:old-pr/10062
ProjectLeonida:old-pr/10042
ProjectLeonida:old-pr/10041
ProjectLeonida:old-pr/10049
ProjectLeonida:old-pr/10047
ProjectLeonida:old-pr/10046
ProjectLeonida:old-pr/10045
ProjectLeonida:old-pr/10056
ProjectLeonida:old-pr/10043
ProjectLeonida:old-pr/10036
ProjectLeonida:old-pr/10034
ProjectLeonida:old-pr/9984
ProjectLeonida:old-pr/10052
ProjectLeonida:old-pr/10038
ProjectLeonida:old-pr/10033
ProjectLeonida:old-pr/10028
ProjectLeonida:old-pr/10050
ProjectLeonida:old-pr/9891
ProjectLeonida:old-pr/9889
ProjectLeonida:old-pr/10051
ProjectLeonida:old-pr/10019
ProjectLeonida:old-pr/10027
ProjectLeonida:old-pr/10029
ProjectLeonida:old-pr/10031
ProjectLeonida:old-pr/10026
ProjectLeonida:old-pr/9941
ProjectLeonida:old-pr/10030
ProjectLeonida:old-pr/10040
ProjectLeonida:old-pr/9990
ProjectLeonida:old-pr/10021
ProjectLeonida:old-pr/9962
ProjectLeonida:old-pr/10022
ProjectLeonida:old-pr/10020
ProjectLeonida:old-pr/10015
ProjectLeonida:old-pr/10025
ProjectLeonida:old-pr/9886
ProjectLeonida:old-pr/10048
ProjectLeonida:old-pr/10014
ProjectLeonida:old-pr/10011
ProjectLeonida:old-pr/9861
ProjectLeonida:old-pr/10010
ProjectLeonida:old-pr/10008
ProjectLeonida:old-pr/9860
ProjectLeonida:old-pr/10006
ProjectLeonida:old-pr/9920
ProjectLeonida:old-pr/9995
ProjectLeonida:old-pr/10000
ProjectLeonida:old-pr/10016
ProjectLeonida:old-pr/10004
ProjectLeonida:old-pr/10023
ProjectLeonida:old-pr/9982
ProjectLeonida:old-pr/9981
ProjectLeonida:old-pr/9983
ProjectLeonida:old-pr/9970
ProjectLeonida:old-pr/9969
ProjectLeonida:old-pr/9968
ProjectLeonida:old-pr/9971
ProjectLeonida:old-pr/9972
ProjectLeonida:old-pr/9942
ProjectLeonida:old-pr/9963
ProjectLeonida:old-pr/9960
ProjectLeonida:old-pr/9964
ProjectLeonida:old-pr/9985
ProjectLeonida:old-pr/9973
ProjectLeonida:old-pr/9862
ProjectLeonida:old-pr/9953
ProjectLeonida:old-pr/10017
ProjectLeonida:old-pr/9952
ProjectLeonida:old-pr/9955
ProjectLeonida:old-pr/9946
ProjectLeonida:old-pr/9954
ProjectLeonida:old-pr/9938
ProjectLeonida:old-pr/9956
ProjectLeonida:old-pr/9856
ProjectLeonida:old-pr/9947
ProjectLeonida:old-pr/9924
ProjectLeonida:old-pr/9961
ProjectLeonida:old-pr/9927
ProjectLeonida:old-pr/9979
ProjectLeonida:old-pr/10032
ProjectLeonida:old-pr/9926
ProjectLeonida:old-pr/5237
ProjectLeonida:old-pr/9951
ProjectLeonida:old-pr/9931
ProjectLeonida:old-pr/9902
ProjectLeonida:old-pr/9921
ProjectLeonida:old-pr/9994
ProjectLeonida:old-pr/9775
ProjectLeonida:old-pr/9768
ProjectLeonida:old-pr/9905
ProjectLeonida:old-pr/9936
ProjectLeonida:old-pr/9901
ProjectLeonida:old-pr/9904
ProjectLeonida:old-pr/9906
ProjectLeonida:old-pr/9988
ProjectLeonida:old-pr/9908
ProjectLeonida:old-pr/9909
ProjectLeonida:old-pr/9911
ProjectLeonida:old-pr/9912
ProjectLeonida:old-pr/9913
ProjectLeonida:old-pr/9887
ProjectLeonida:old-pr/9916
ProjectLeonida:old-pr/9919
ProjectLeonida:old-pr/9925
ProjectLeonida:old-pr/9933
ProjectLeonida:old-pr/9935
ProjectLeonida:old-pr/9918
ProjectLeonida:old-pr/9879
ProjectLeonida:old-pr/9880
ProjectLeonida:old-pr/9881
ProjectLeonida:old-pr/9996
ProjectLeonida:old-pr/9876
ProjectLeonida:old-pr/9892
ProjectLeonida:old-pr/9899
ProjectLeonida:old-pr/9915
ProjectLeonida:old-pr/9914
ProjectLeonida:old-pr/9872
ProjectLeonida:old-pr/9869
ProjectLeonida:old-pr/9866
ProjectLeonida:old-pr/9819
ProjectLeonida:old-pr/10012
ProjectLeonida:old-pr/9857
ProjectLeonida:old-pr/9893
ProjectLeonida:old-pr/9897
ProjectLeonida:old-pr/9928
ProjectLeonida:old-pr/9868
ProjectLeonida:old-pr/9917
ProjectLeonida:old-pr/9940
ProjectLeonida:old-pr/9873
ProjectLeonida:old-pr/9816
ProjectLeonida:old-pr/10009
ProjectLeonida:old-pr/9900
ProjectLeonida:old-pr/9966
ProjectLeonida:old-pr/9859
ProjectLeonida:old-pr/9843
ProjectLeonida:old-pr/9991
ProjectLeonida:old-pr/10058
ProjectLeonida:old-pr/9883
ProjectLeonida:old-pr/9838
ProjectLeonida:old-pr/9835
ProjectLeonida:old-pr/9959
ProjectLeonida:old-pr/9895
ProjectLeonida:old-pr/9837
ProjectLeonida:old-pr/9858
ProjectLeonida:old-pr/9865
ProjectLeonida:old-pr/9864
ProjectLeonida:old-pr/9840
ProjectLeonida:old-pr/9820
ProjectLeonida:old-pr/4669
ProjectLeonida:old-pr/9848
ProjectLeonida:old-pr/9850
ProjectLeonida:old-pr/9810
ProjectLeonida:old-pr/9799
ProjectLeonida:old-pr/9853
ProjectLeonida:old-pr/9875
ProjectLeonida:old-pr/9831
ProjectLeonida:old-pr/9796
ProjectLeonida:old-pr/9801
ProjectLeonida:old-pr/9813
ProjectLeonida:old-pr/9794
ProjectLeonida:old-pr/7417
ProjectLeonida:old-pr/9781
ProjectLeonida:old-pr/9806
ProjectLeonida:old-pr/9958
ProjectLeonida:old-pr/9809
ProjectLeonida:old-pr/9791
ProjectLeonida:old-pr/9792
ProjectLeonida:old-pr/10053
ProjectLeonida:old-pr/9784
ProjectLeonida:old-pr/9789
ProjectLeonida:old-pr/9849
ProjectLeonida:old-pr/9846
ProjectLeonida:old-pr/9773
ProjectLeonida:old-pr/9785
ProjectLeonida:old-pr/9803
ProjectLeonida:old-pr/9826
ProjectLeonida:old-pr/9839
ProjectLeonida:old-pr/9922
ProjectLeonida:old-pr/9948
ProjectLeonida:old-pr/7189
ProjectLeonida:old-pr/9957
ProjectLeonida:old-pr/9823
ProjectLeonida:old-pr/9807
ProjectLeonida:old-pr/9851
ProjectLeonida:old-pr/9774
ProjectLeonida:old-pr/9830
ProjectLeonida:old-pr/9836
ProjectLeonida:old-pr/9822
ProjectLeonida:old-pr/9783
ProjectLeonida:old-pr/9833
ProjectLeonida:old-pr/9890
ProjectLeonida:old-pr/9770
ProjectLeonida:old-pr/9769
ProjectLeonida:old-pr/9772
ProjectLeonida:old-pr/9787
ProjectLeonida:old-pr/9852
ProjectLeonida:old-pr/9834
ProjectLeonida:old-pr/9845
ProjectLeonida:old-pr/9871
ProjectLeonida:old-pr/9771
ProjectLeonida:old-pr/9776
ProjectLeonida:old-pr/10005
ProjectLeonida:old-pr/9949
ProjectLeonida:old-pr/9923
ProjectLeonida:old-pr/9805
ProjectLeonida:old-pr/9829
ProjectLeonida:old-pr/9790
ProjectLeonida:old-pr/9827
ProjectLeonida:old-pr/9929
ProjectLeonida:old-pr/9855
ProjectLeonida:old-pr/9934
ProjectLeonida:old-pr/9907
ProjectLeonida:old-pr/9910
ProjectLeonida:old-pr/9779
ProjectLeonida:old-pr/9780
ProjectLeonida:old-pr/9885
ProjectLeonida:old-pr/9882
ProjectLeonida:old-pr/9877
ProjectLeonida:old-pr/9998
ProjectLeonida:old-pr/4251
ProjectLeonida:old-pr/9987
ProjectLeonida:old-pr/9989
ProjectLeonida:old-pr/9950
ProjectLeonida:old-pr/9777
ProjectLeonida:old-pr/10044
ProjectLeonida:old-pr/9884
ProjectLeonida:old-pr/2906
ProjectLeonida:old-pr/5076
ProjectLeonida:old-pr/9937
ProjectLeonida:old-pr/9986
ProjectLeonida:old-pr/3014
ProjectLeonida:old-pr/9943
ProjectLeonida:old-pr/9939
ProjectLeonida:old-pr/4682
ProjectLeonida:old-pr/4765
No reviewers
Labels
Clear labels
No items
No labels
Milestone
Clear milestone
No items
No milestone
Assignees
Clear assignees
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!2800
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2800"
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-30T23:15:55Z.
What
Module::optimise(leonida-shader/src/emit/bitcode/opt.rs) runsmerge,prune,guardsandchainsfor up to four rounds and stops early when none of them changes anything. In a full-archiveleonida translateon currentmain, 10,291 of 25,689 emitted programs (40 %) run all four rounds, and rounds 3 and 4 are 44 % of the time the optimiser takes. I traced those rounds (live instructions, dead instructions, blocks, and which pass said "changed") and they are a loop, not progress: from round 2 onmergeandchainsboth report a change every round while the counts do not move. With the limit raised to 16, 10,236 programs still say "changed" in the last round.The cause is in
chains. It rewrites a conjunction of lane conditions into(((t1 & t2) & t3) & ...), terms in the order they were made, and leaves a chain that is in that form (canonical).canonicalonly accepted a step writtenand(lower, term).key()treats an integerandas commutative, so a chain writtenand(term, lower)was never canonical:chainsbuiltand(lower, term)as new instructions, the reuse pass ofmergeput the instruction that was there back, and the next round did the same. I found it by printing the plans of a small program per round: the same root and the same terms every round, with the prefixandthere with its operands the other way round.canonicalnow accepts a step with its two operands either way round. That is one match arm.Claim: D3D12-SHADER-OPT-CHAINS-CANONICAL (coordination issue #2570, https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7294408822). No open pull request edits
chains(checked the diffs of the ones that touchopt.rs: #2750, #2698, #2687, #2681, #2569).The DXIL is not identical to
main's, and whyI claimed an identical-DXIL bar in the claim; this is not that, so here is what changes. Full archive, 25,689 programs, hash of the DXIL of each (a scratch patch writing the hashes; not in this pull request):
arith: -341,356 instructions (-0.84 % of arith, -0.34 % of all instructions). Every other kind (select, phi, calls, loads, stores, casts, extracts, handles) and the block count are equal, from the census (LEONIDA_SHADER_CENSUS) of both builds.mainhas those extra instructions: its last round ends on achainsrewrite and nomergefollows, so the rewrite's duplicates of existingands are written out. I checked that:mainwith onemergeadded after the last round gives the same arith count (40,370,778) as this branch and the same DXIL in 25,548 programs (total bytes within 8 of this branch's).Not rewriting a chain is always sound (it leaves the lowering's own form), so the new output is
main's with duplicateands absent and some operands in the other order; I did not run a GPU test on it.leonida translate --validate(Windows SDK 10.0.26100dxil.dll) on both builds: the same counts (14,220 of 14,247 vertex and pixel programs, 2,677 of 2,677 compute, 8,673 of 8,765 mesh) and the same table of reasons for the rest (compared line by line, counts included), so nothing new is refused.Measured
Full-archive
leonida translate, release build withmimalloc, 16 threads, Ryzen 7 7800X3D,mainat2878a3911as the baseline (the same tree with the one arm taken out), four alternating runs of each:Means: wall 36.65 -> 33.78 s (-7.8 %), user CPU 477.2 -> 436.9 s (-8.5 %).
Tests
a_chain_with_the_operands_of_its_steps_the_other_way_round_is_left_as_it_is: the term first at the root, below it, and both;chains()finds nothing to rewrite, still nothing afteroptimise(CHAINS | REUSE | DEAD)has merged them, and a second run leaves the instructions as they were. Negative control: taking the new arm out makes it fail on the first assertion.a_chain_with_its_terms_out_of_order_is_still_rewritten: the control for the other direction (it passes with and without the arm).cargo test -p leonida-shader(debug): 402 passed, 0 failed, 5 ignored.rustfmt --edition 2024 --check opt.rs: no diff in the lines this changes (the file's other diffs were there before).cargo clippy -p leonida-shader --no-deps --all-targets -- -D warningsreports errors in other files of the crate and two inopt.rs(lines 1318 and 1778, from the commit that addedchains), none in the lines this changes.check_no_game_data.pyon the changed file: clean.AI-assisted: Gemini 4 Neon (including an independent read-only review pass on follow-up commit
b6ebcedd4)Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T23:19:22Z.
Verified by OreoFR on macOS 27.2.1, Apple M5, Rust (rustup), head
9e39fbb58, maind4fabd3fe. CPU only, no game files.MEASURED
cargo test --release -p leonida-shader: main 242 passed, 0 failed, 2 ignored; main + this PR (merge-tree) 244 passed, 0 failed, 2 ignored. The two extra tests are the new ones.Op::Bin(Bin::And, x, y) if x == want => cur = y,), tests matchinga_chain_with: 1 failed, 1 passed.a_chain_with_the_operands_of_its_steps_the_other_way_round_is_left_as_it_isfails, atopt.rs:1853(assert!(!m.chains(), "no chain here is out of order")).a_chain_with_its_terms_out_of_order_is_still_rewrittenpasses, as a control should.READ IN CODE
canonical. It walks the chain from the root using whichever operand ofandis not the expected term. That matcheskey()treating an integerandas commutative, sochainsand the reuse pass now agree on what "in order" means.optimise(CHAINS | REUSE | DEAD)leaves the same instructions, which is the fixed point that was missing.ASSUMED
arith, -0.34 % instructions, -8 % translation CPU) are from your run and are not reproduced here: they need the game's archive. The DXIL is not byte-identical to main, so an in-game check on the translated output is what would cover the behavior change.AI-assisted: Claude Code
Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-12-01T05:19:52Z.
Review: COMMENTED
Verified the Rust arm (this is
d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs, not the Python translator — so the semantic suite cannot discriminate it):cargo test --locked -p leonida-shader --libis 402 passed / 0 failed / 5 ignored, and removing the new match arm makesa_chain_with_the_operands_of_its_steps_the_other_way_round_is_left_as_it_isfail atopt.rs:1853, while the out-of-order control test still passes. So the new arm is pinned in both directions.The change is semantically safe as far as I can tell: only i1
andnodes (plus purexor) are replaced, leaves — including effectful loads/calls — keep their instructions and positions, and a store/atomic/barrier is not an i1andstep, so nothing with side effects is reordered. The term order is irrelevant to AND, and the terms are sorted by definition site and deduped, with a term-with-its-complement refused (:1242-1261); the only hash-ordered collection is the pre-existingby_blockmap, which does not decide which chains collapse.One residual, performance only but worth closing:
born[p] = (rank, 0)for every phi in a block (:1202) and a stablesort_by_key(:1249) mean same-block phis tie, so their order follows the DFS — which reverses when the chain is rebuilt. With three i1 phis from one block in a chain,canonicalrejects the rebuilt chain andchains()rewrites again next call (temporary test: first call true, second call true; with three distinct comparisons the second is false). A total tie-break —(born, base)or a per-phi birth index — finishes the loop fix this PR is after.(The Python semantic suite is irrelevant here: 553 files / 6,720 tests pass, but it never loads
leonida-shader. The archive hash change is not reproducible here either — no work folder with disassembly on this machine — and the PR does not claim identical output anyway.)Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-01T15:43:36Z.
VERIFY-2800 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game files
Verdict: PASS. The fix is one line and the pull request brings its own control: the out-of-order chain is still rewritten.
Head
9e39fbb5, base the currentmain88a9f133, merge base2878a391, 1 commit, 1 file, +86/−1:d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs.1. The change
chains()asks whether the chain it is looking at is already in canonical order, and the answer walked the chain's steps like this:That only accepts a step written as
x & want— the term second. An integerandis the same value whichever way round its operands are, sowant & xis the same chain; the old answer said "not canonical" for it. The pull request adds the other way:The comment in the pull request describes the consequence: the reuse pass would put the instruction that is there back in place of the rewrite, and the next round would plan the same rewrite again, "up to the round limit".
2. Red and green, and the discrimination
cargo test -p leonida-shader(Linux, pinned toolchain) on the pull request's branch:With only that one line put back (the pull request's tests kept):
So the new test looks at the code it is about.
3. The control is in the pull request
a_chain_with_its_terms_out_of_order_is_still_rewrittenis the negative control, and it is the part I like most about this pull request:The first assertion is that the pass finds work, the second that a second call finds none — so the fix cannot be "the pass stopped rewriting anything". With the fix reverted, the other test fails and this one still passes, which is what a control should do.
4. The claim about the reuse pass
The pull request says the chain "would be rebuilt each round for the reuse pass to put back, and say that it had changed something every time, up to the limit of rounds". The new test checks the end of that:
m.optimise(CHAINS | REUSE | DEAD)merges the three chains, and a secondoptimiseleaves the instructions identical (assert_eq!(ops(&m), merged)).I did not measure the round count or the CPU saving on a real shader corpus — the pull request's title quotes "-8 % translate CPU, -0.34 % DXIL instructions", and that needs the game's shaders, which are not in the repository and which I do not have.
5. It does not collide with #2872
Both pull requests change
chains()inopt.rs(#2872 makes the block grouping deterministic, this one makes the canonical test accept a commutative step). I merged both onto today'smainand ran the crate:No conflict, and neither pull request's test breaks the other's.
6. What I did not check
leonida-gpu,leonida-hleandleonida-loaderdo not build on Linux for reasons that predate this pull request (upstream: "The crates are Windows-only today … Linux follows #2577"), so I ranleonida-shaderon its own.Bin::Andis always commutative in this IR. The fix assumes an integerand; the pass works oni1/i32values, andandon those is commutative. I did not look for a use ofBin::Andon a non-integer type, where the reasoning would not hold.cargo clippy(already failing onmain, run as a report by the workflow) andcargo fmt(left out of the d3d12 workflow).AI-assisted: MiniMax M3.1 Flash.
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-12-01T18:25:59Z.
@KelboLive Addressed the same-block phi tie-break residual in
b6ebcedd4(after mergingmain, no rebase/force-push):(rank, k)for each phi (0..phis.len()) and(rank, phis.len() + k)for body instructions inchains(), giving every instruction in reachable blocks a strict total birth order.a_chain_of_three_phis_in_the_same_block_settles_in_one_passpinning that a 3-phi chain in one block rewrites once and settles (chains()returnsfalseon the second call).cargo test --locked -p leonida-shader --lib bitcode::opt(21 passed, 0 failed) andpython -I scripts/check_no_game_data.py(clean).AI-assisted: Gemini 4 Neon (including an independent read-only reviewer pass).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T12:25:31Z.
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:mainSHA it was merged onto;mainalone 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).
Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T17:54:01Z.
Ran natively on Windows 11, head
b6ebcedmerged into currentmain(45dc41f, clean merge). Rust 1.98.1,--locked. Some runs were done onmain5b1fff8, whereleonida-shaderis byte-identical to45dc41f.The other checks in this thread were on
9e39fbb. This one is on the current head, which adds the phi tie-break. It also covers the overlap with #3156.Overlap with #3156 (measured)
#3156 adds the same
canonical()arm as this PR,if x == want => cur = y, character for character. Beyond that the two PRs share nothing:b6ebced).merge()after the round loop whenchains()fired in the last round.main+ #2800 + #3156 merges cleanly in either order. Both orders give the same tree.There are five new tests in total (three from this PR, two from #3156). I ran them against the optimiser code of each variant, from the merged file with the other PR's code hunks reverted:
a_chain_with_the_operands_of_its_steps_the_other_way_round_is_left_as_it_isa_chain_with_its_terms_out_of_order_is_still_rewritten(control)a_chain_of_three_phis_in_the_same_block_settles_in_one_passchains()returns true)chains_accepts_commuted_and_operands(#3156)optimise_merges_chains_emitted_on_final_round(#3156)and, want 1)mainand green withb6ebced. It also fails on #3156's code, so #3156 does not cover it.mainwith onemergeadded after the last round"; that one merge is exactly #3156's second half.The tie-break, narrower case (measured)
Two same-block phis are enough to loop on
mainwhen a dominating term comes first.bris a compare in the entry block;p0/p1arei1phis of the join;chains()was called 4 times:(br & p0) & p1(p0 & p1) & t(tmade in the join)The commuted arm on its own absorbs the tie only when the two phis are the lowest pair.
b6ebcedfixes both shapes.Differential fuzz of
chains()(throwaway test, not proposed for the PR)Programs come from two generators:
and(operands in either order),or,xor,xor true,select(c, t, false),select(c, false, f),i1add/mul, andzext→add/mul→ compare.true/falseconstants, and commuted copies of existingands.i1phis.random_program, seeds 1–10,000.Each program ran in the crate's interpreter (
run) on 12 input vectors, before and afteroptimise()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:
chains()+merge(REUSE)not settled after 12 alternationschains()still fires right afteroptimise(CHAINS)optimise(CHAINS|REUSE)twice ≠ onceoptimise(ALL)twice ≠ onceALL(ands)optimise(ALL)time, 3 passes over the setrandom_program, 10,000 seeds:optimise(ALL)twice ≠ onceALLoptimise(ALL)twice ≠ once behave the same withCHAINSoff (ALL & !CHAINS,GUARDS|REUSE|DEAD) and onmain. They do not come from this PR.ALLoutput is larger withCHAINSthan without it in every variant: 1,652,367 live and 266,546ands without it. It is less so with this PR than onmain. Shared sub-ands get rebuilt per root. This is a property of the pass on these shapes; the archive figures in the PR body point the other way for real shaders.Suites (measured)
cargo test -p leonida-shader, NVIDIA cardThe 8 WARP failures are the same on
main. They are pipeline creation failing with "The parameter is incorrect", inpack_tests,valu_testsandwave64_tests. They are not related to this PR.cargo clippy -p leonida-shader --all-targets: the same 6 warnings asmain, none new.Not checked: game shaders or archive output, the in-game run requested above, debug-layer runs,
rustfmt, other crates. WARP was not run for this PR on its own. The full suite onmain+ this PR alone was stopped part way. Theopttests above were run on this PR's code, and the full suite passed on the tree with both PRs merged.Suggestion: the two PRs are complementary. Merging both, or taking #3156's trailing merge into this one, passes all five tests and gives zero unsettled programs in the fuzz.
GPUs tested: one NVIDIA desktop card + WARP
AI-assisted: yes (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T18:21:39Z.
RUN (in game, batch of seventeen render-path pull requests), each head merged with
git merge --no-ffonto main3c106eb2f, in one build: #311115b579756, #29485284c6eae, #26981f188c4f7, #294644bf554fe, #3001d924cbb65, #28320c861595a, #291558e8535cf, #2822d359a2f85, #2896c9c6b103d, #29838278ef1b9, #3115ba5121192, #2945d2c08c14f, #297574512d658, #295126da88e8e, #2800b6ebcedd4, #28937c7e99b48, #3113fa1460b6b(and #30970d7a451ab, merged since). RTX 4060 8 GB, driver 610.88, Windows 11. Series S,-Resolution 1080, frame generation 2x, the same walking route as the other runs on this machine, 330 s after reaching the world.The drops under 5 fps line up with shaders being translated while walking (about 500 translations in the last 330 s): this batch changes the shader translation, so the shader cache built by earlier runs did not apply and the pipeline precompile alone took about 45 minutes. The batch-5 run on the same machine and route had no sample under 5 fps (median 16.4). I read this as cold-cache stutter, not as a regression of a single pull request, but this run cannot tell them apart.
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. A first attempt of this batch is not counted: the run script gave up waiting for the game window after 40 minutes of precompile.
AI-assisted (Claude Code).
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T18:28:56Z.
Verification of #2800 (D3D12-SHADER-OPT-CHAINS-CANONICAL): third-party run on Linux x86_64 with Rust 1.98.1 (the pinned toolchain), no GPU and no game files.
b6ebcedd4, merge base27613ab6b, merged locally (detached, clean merge) intomain45dc41f95.Commands and results
cargo test -p leonida-shader --locked: 256 passed, 0 failed, 2 ignored.d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs: the per-phi birth index at L1362-1367 and theif x == want => cur = yarm at L1435. The new tests stayed in. Then I rerancargo test -p leonida-shader --locked --lib: 254 passed and 2 failed (a_chain_of_three_phis_in_the_same_block_settles_in_one_passanda_chain_with_the_operands_of_its_steps_the_other_way_round_is_left_as_it_is). The control testa_chain_with_its_terms_out_of_order_is_still_rewrittenpasses both ways, as intended. After restoring the file, all tests pass again.python -I scripts/check_no_game_data.pyon the changed file: clean.checkandd3d12 (windows-latest)pass on this head.Review
key()treats integerandas commutative, so countingand(want, lower)as canonical only stops a rewrite that the reuse pass would undo.(rank, k)for phis and(rank, phis + k)for the body. That gives a total order within a block, so no two same-block phis tie.Process notes
AI-assisted: Claude Sonnet 5.5. CONTRIBUTING.md lists Sonnet for documentation and simple fixes only. New code should use one of the models listed under "new code and reviews", and the body does not name the reviewer agent's model.b6ebcedin this thread covers unit tests and fuzzing only.Verdict: ready on the code (red/green reproduced on Linux). Still needs the in-game run and a disclosure fix.
AI-assisted verification: Claude Opus (Claude Code), separate agent per batch.
Generated by Claude Code
Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-12-02T21:02:03Z.
@MoonEyes Thank you for the review and
@Kal0s-designfor verifying compatibility with#3156.AI-assisteddisclosure perCONTRIBUTING.md(namingGemini 4 Neonand the independent read-only review pass onb6ebcedd4).b6ebcedd4untouched:@MoonEyes's2026-12-02T18:21:39Zin-game batch PASS (330 s, RTX 4060, main3c106eb2f) already covers#2800at exact headb6ebcedd4.AI-assisted: Gemini 4 Neon
Imported from ubiquitarianism/leonida. Originally posted by fiestorra on 2026-12-03T05:08:27Z.
VERIFY-2800 — 21 tests de
optpasan, y el cambio es de canonicalizaciónEl título: «a chain with its steps the othe…» — una cadena de optimización cuyos pasos están en otro orden. Y el cambio está en
opt.rs, que es el fichero que decide el orden.Por qué ese orden importa, dicho sin jerga: dos programas que hacen lo mismo tienen que compilar al mismo DXIL, o el caché de shaders (que va por hash) los trata como distintos y compila dos veces. Una cadena cuyos pasos no están canónicamente ordenados es un programa que se ve distinto según cómo se escribió — y el juego, que compila miles, paga la duplicación.
Y es la tercera vez esta noche que aparece el mismo tema:
#3284(formatos enlazados),#2892(joins de descriptores) y#2957(gemelos enmascarados) son claves de caché que no incluyen algo; este es programas que son iguales y se ven distintos. Cuatro PRs, cuatro autores, el mismo invariante: la identidad de lo compilado tiene que depender de lo que hace, no de cómo está escrito.Lo que verifiqué: 21 tests de
opten verde, y el fichero es el que toca el orden.Lo que no: que el DXIL final sea idéntico para las dos escrituras — el test compara la decisión de optimización, no el binario emitido. Para eso haría falta un test de extremo a extremo con dos programas equivalentes, y ese es el que cerraría el círculo con el caché.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-03T18:03:51Z.
In-game gate run on an RTX 4060 8 GB (driver 610.88, Windows 11), in a batch of #2822, #2800, #2664 and #2661 merged into main
09273248c; this PR tested at its current headb6ebcedd4. Series S 1080, DLSS FG 2x,play-nvidia.ps1 -SeriesS -Resolution 1080 -FrameGen 2 -PrecompileUsed, scripted walk, 330 s, screen kept on.AI-assisted (Claude Opus 5.5), run and read by MoonEyes' agent.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.