Repository navigation
d3d12-shader: make chains() and loop-counter lowering deterministic - #2872
kirigayakazima wants to merge 1 commit into
Conversation
|
Independent cross-process check of the optimizer regression from AI-assisted with OpenAI Codex. |
KelboLive
left a comment
There was a problem hiding this comment.
Nice catch — the outer HashMap in chains() was the real bug: it decided the order in which new and/xor ids were allocated, and replacing it with a Vec filled in tree.order gives a genuine total order. Reverting it reliably fails the new test (new instruction IDs must advance monotonically in tree order: [[32,33],[36,37],[40,41],[34,35],[38,39],[30,31]]), and my repeated-run probe is stable over 32 in-process runs and 8 independent threads × 8 (module bytes 0x355c3da5eb8360f7, id hash 0x888268bd2da29585).
Two things to fix or state before merge:
- The emitted DXIL was already deterministic —
Module::write()encodes relative value ids (bitcode.rs:572,:852) and per-block body order was already stable, so under the old code the output bytes were byte-identical; only the internalModule.instsnumbering (and dumps/tests that print ids) diverged. Worth saying in the commit so it doesn't read as a cache-key/output fix. - The
countersHashMap→BTreeMapchange is inert. The only ordered consumer (find_exit_caps) already didheaders.sort_unstable()before this PR, andinto_values()(:15688) writes disjoint phi slots. I reverted it toHashMap+ sort and got identical DXIL (0xf9e31b0f5f6a0fb6), no failing test, and the same on a two-header program. Drop it, or label it as cleanup with no behaviour change.
Minor: the plans.next_if(|id| id == i) walk silently relies on plans being a subsequence of the body it is matched against — true today, but one line of comment would save the next refactor. Also note the branch's merge-base with main is stale (git diff main...pr shows 211 files); the commit itself is the two files.
Everything else checks out: leaves.sort_by_key in terms_of is a stable sort on (born, neg) with a contains-only HashSet, and the other HashMaps in lower.rs are get/insert/remove-only.
VERIFY-2872 — Linux, Rust 1.98.1 (the pinned toolchain), no GPU device, no game filesVerdict: PASS. The repository's own red/green tool says the new test is red on Head 1. The changeTwo places where a
Both are the same defect: an order that is not fixed, in a stage whose result is hashed into the shader cache's key. 2. Red and green
On the red side the new test fails; on the green side the crate is green. 3. What the new test checks
The second half is the part that catches the 4. It does not collide with #2800#2800 also changes No conflict, and neither pull request's test breaks the other's. 5. What I did not check
AI-assisted: MiniMax M3.1 Flash. |
|
Independent red/green on Windows 11 (RTX 5080, NVIDIA 621.24), head
No in-game run from me. AI-assisted (Claude Code), posted for DadaCap. |
Claimed in the coordination issue as: D3D12-SHADER-DETERMINISTIC-EMIT (#2570 (comment))
What and why
Addresses @bobcatchris's finding (#2570 (comment)) on non-deterministic DXIL emission across runs:
d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs(Module::chains()), rewritten condition-chain plans were grouped intoby_block: std::collections::HashMap<usize, Vec<(u32, Vec<Cond>)>>and then iterated in randomizedRandomStatebucket order while allocating newV::Inst(x)IDs (self.insts.push(...)). Becausetree.orderalready visits each reachable block once andself.blocks[b].bodyis already in instruction order,chains()now buildsby_blockdirectly asVec<(usize, Vec<(u32, Vec<Cond>)>)>intree.orderand consumes per-block plans sequentially withpeekable().next_if(...), eliminating bothHashMaps inchains()(and removing the now-unusedstd::collections::HashMapimport).d3d12/crates/leonida-shader/src/emit/lower.rs,countersis switched fromHashMap<usize, (V, Vec<(V, usize)>)>toBTreeMapsostd::mem::take(&mut self.counters).into_values()in phi completion iterates in deterministic ascending IR-block order andfind_exit_caps()no longer needsheaders.sort_unstable()(reducinglower.rssize slightly).Tested
Verified locally (Windows 11, Rust 1.98.1, Windows SDK
dxil.dll, no game data):cargo test --locked -p leonida-shader --lib chains_emits_deterministic_instruction_ids_across_blockspanicked across blocks ([[40, 41], [30, 31], [36, 37], [32, 33], [34, 35], [38, 39]]).cargo test --locked -p leonida-shader --lib:410 passed; 0 failed; 5 ignored(includingchains_emits_deterministic_instruction_ids_across_blocks)cargo clippy --locked -p leonida-shader -- -D warnings: 0 warningspython -I scripts/check_no_game_data.py&git diff --check: cleanGPUs tested: n/a (translator unit tests only; validated against local Windows SDK
dxil.dll)AI-assisted: yes
Checklist
mainand merges cleanly (a conflicting pull request is labeledneeds rebase).docs/design.md, with how it was confirmed. (n/a: no new instruction rule)