d3d12-shader: make chains() and loop-counter lowering deterministic (D3D12-SHADER-DETERMINISTIC-EMIT, carry of #2872) #3059

Merged
MoonEyes merged 3 commits from upstream-pr/3059 into main 2026-12-02 10:55:44 +00:00
MoonEyes commented 2026-12-02 06:44:31 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-12-02T06:44:31Z.


Claimed in the coordination issue as: D3D12-SHADER-DETERMINISTIC-EMIT (the claim of the original author, https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7311583627)

What and why

Carry of #2872 (kirigayakazima), in conflict with main since both sides appended a test at the end of the same module in d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs. This branch is main merged with the head of #2872, so the author's commit is kept. My only change is the merge: both tests stay.

The change itself is #2872's: chains() builds its per-block plans in tree.order instead of iterating a HashMap, and the loop counters in lower.rs use a BTreeMap, so the emitted instruction IDs no longer depend on the run (chains_emits_deterministic_instruction_ids_across_blocks pins it).

Tested

Windows 11, no GPU, no game files, on current main plus this branch:

  • cargo test -p leonida-shader --lib: 421 passed, 0 failed, 5 ignored.
  • cargo clippy --locked -p leonida-shader -- -D warnings: clean.
  • Not run: the full translator suite, CI.

GPUs tested: n/a (translator unit tests only)

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md).
  • A new translator rule is in the contract table of docs/design.md. (n/a: no new rule, an ordering fix)

Supersedes #2872 once merged; the original stays open until a maintainer decides.

AI-assisted (Claude Code)

🤖 Generated with Claude Code

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5601335698 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3059). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T06:44:31Z. --- Claimed in the coordination issue as: D3D12-SHADER-DETERMINISTIC-EMIT (the claim of the original author, https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7311583627) ### What and why Carry of #2872 (kirigayakazima), in conflict with `main` since both sides appended a test at the end of the same module in `d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs`. This branch is `main` merged with the head of #2872, so the author's commit is kept. My only change is the merge: both tests stay. The change itself is #2872's: `chains()` builds its per-block plans in `tree.order` instead of iterating a `HashMap`, and the loop counters in `lower.rs` use a `BTreeMap`, so the emitted instruction IDs no longer depend on the run (`chains_emits_deterministic_instruction_ids_across_blocks` pins it). ### Tested Windows 11, no GPU, no game files, on current `main` plus this branch: - `cargo test -p leonida-shader --lib`: 421 passed, 0 failed, 5 ignored. - `cargo clippy --locked -p leonida-shader -- -D warnings`: clean. - Not run: the full translator suite, CI. GPUs tested: n/a (translator unit tests only) AI-assisted: yes (Claude Code) ### Checklist - [x] Based on current `main` and merges cleanly. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md). - [ ] A new translator rule is in the contract table of `docs/design.md`. (n/a: no new rule, an ordering fix) Supersedes #2872 once merged; the original stays open until a maintainer decides. AI-assisted (Claude Code) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Kal0s-design commented 2026-12-02 07:36:58 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-02T07:36:58Z.


A red/green and the full suite on a card, since the description has neither. Windows 11, MSVC toolchain 1.98.1. Head 5d47d4f57 merged into current main 229d352ee: the merge is clean.

  • Red/green. I took main's opt.rs and added only the new test, chains_emits_deterministic_instruction_ids_across_blocks. It fails 5 runs of 5, and the block order differs every run:

    [[34, 35], [30, 31], [32, 33], [36, 37], [38, 39], [40, 41]]
    [[38, 39], [34, 35], [30, 31], [40, 41], [36, 37], [32, 33]]
    [[32, 33], [30, 31], [40, 41], [34, 35], [36, 37], [38, 39]]
    [[36, 37], [30, 31], [34, 35], [38, 39], [32, 33], [40, 41]]
    [[40, 41], [36, 37], [38, 39], [30, 31], [32, 33], [34, 35]]
    

    That is the HashMap order changing from one process to the next. With the change it passes 5 runs of 5.

  • cargo test -p leonida-shader --lib, with an NVIDIA card present, so the card-backed tests ran too: 428 passed, 0 failed, 6 ignored. I touched build.rs first so the translator's source hash was fresh.

  • cargo clippy -p leonida-shader --lib --tests: no warning in opt.rs or lower.rs.

A note on lower.rs: main already sorted the counter headers before walking them (headers.sort_unstable()), so the BTreeMap there keeps the same order rather than fixing one. The nondeterminism this fixes is the one in chains().

Not checked: the game, and AMD hardware.

GPUs tested: one NVIDIA desktop card.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7338397679 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3059#issuecomment-7338397679). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-02T07:36:58Z. --- A red/green and the full suite on a card, since the description has neither. Windows 11, MSVC toolchain 1.98.1. Head `5d47d4f57` merged into current `main` `229d352ee`: the merge is clean. - **Red/green.** I took `main`'s `opt.rs` and added only the new test, `chains_emits_deterministic_instruction_ids_across_blocks`. It fails 5 runs of 5, and the block order differs every run: ``` [[34, 35], [30, 31], [32, 33], [36, 37], [38, 39], [40, 41]] [[38, 39], [34, 35], [30, 31], [40, 41], [36, 37], [32, 33]] [[32, 33], [30, 31], [40, 41], [34, 35], [36, 37], [38, 39]] [[36, 37], [30, 31], [34, 35], [38, 39], [32, 33], [40, 41]] [[40, 41], [36, 37], [38, 39], [30, 31], [32, 33], [34, 35]] ``` That is the `HashMap` order changing from one process to the next. With the change it passes 5 runs of 5. - `cargo test -p leonida-shader --lib`, with an NVIDIA card present, so the card-backed tests ran too: 428 passed, 0 failed, 6 ignored. I touched `build.rs` first so the translator's source hash was fresh. - `cargo clippy -p leonida-shader --lib --tests`: no warning in `opt.rs` or `lower.rs`. A note on `lower.rs`: `main` already sorted the counter headers before walking them (`headers.sort_unstable()`), so the `BTreeMap` there keeps the same order rather than fixing one. The nondeterminism this fixes is the one in `chains()`. Not checked: the game, and AMD hardware. GPUs tested: one NVIDIA desktop card. AI-assisted: yes (Claude Code).
enlight3d commented 2026-12-02 08:20:16 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by enlight3d on 2026-12-02T08:20:16Z.


RUN (independent run on Windows, #3059 head 5d47d4f57, merged locally into main f37c4a87d with git merge --no-ff, no conflicts. The head already contains that main, so the merge adds only this pull request's diff: 2 files, +76/-15. Its added and removed lines are the same as in #2872's commit 9bb420346, so the carry leaves the author's change as it was. main's bounded_budget_caps_wide_phi_dag is still in place, just above the new test. CI on the head: change-scope, check, d3d12 (windows-latest), 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, one target folder per side:

  • cargo test -p leonida-shader --no-fail-fast: main 420 passed / 0 failed / 5 ignored; merge 421 / 0 / 5 (the new test). Some of these tests run on the GPU, here an RTX 3090.
  • cargo test -p leonida-shader --no-fail-fast --features diagnostics: main 420 / 0 / 5; merge 421 / 0 / 5.
  • Red, by assertion: main's opt.rs with only this pull request's test added and its production hunk left out. cargo test -p leonida-shader --lib chains_emits_deterministic, run in 3 separate processes, fails 3 times out of 3 on the monotonic assertion, with a different order each time: [[36, 37], [34, 35], [38, 39], [40, 41], [30, 31], [32, 33]], [[32, 33], [34, 35], [38, 39], [36, 37], [40, 41], [30, 31]], [[30, 31], [32, 33], [36, 37], [34, 35], [40, 41], [38, 39]]. That is the reason the pull request gives: the blocks were walked in HashMap order when the new ands got their IDs.
  • Green: cargo test -p leonida-shader --lib bitcode::opt::tests on the merge: 21 passed.
  • What changes in the output, checked with a local probe: three tests that are not in this pull request, added to opt.rs on both sides. They build the new test's six-block module 64 times in one process. On main, the new IDs came out in 59 different orders, but Module::write() gave the same bytes in all 64 builds, whether after chains() alone, after optimise(CHAINS | REUSE | DEAD) or after optimise(MODULE). On the merge: one order of the IDs in all 64 builds, and the same bytes in all three cases. So on this module, main's emitted bitcode was already the same from run to run. The pull request fixes the numbering inside Module.insts (what dumps and tests that print IDs see), as KelboLive's review on #2872 said. The body of #3059 says "instruction IDs", which is accurate. This is not a fix for output bytes or a shader cache key that changes between runs. Real programs were not checked.
  • Mutations of the merge. Each file was restored afterwards, and git status was clean:
    • The last block's plans are dropped (by_block.pop() after the empty check), so chains() rewrites one chain fewer. Killed by the new test (emitted ids in next block) and by the existing conjunctions_of_conditions_share_the_terms_they_start_with (stored values 4 instead of 2). The other 19 bitcode::opt::tests pass.
    • The block order goes back through a HashMap (for (b, plans) in by_block.into_iter().collect::<HashMap<_, _>>()). This is the defect itself. Killed by the new test in 3 processes out of 3, each with a different order. The other 20 bitcode::opt::tests pass, so the new test is the only one that pins it.
    • counters goes back to a HashMap, with the sort_unstable() still removed, so the loop headers come in random order in find_exit_caps and in the phi completion. Whole lib: 421 / 0 / 5, it survives.
    • find_exit_caps walks the headers in reverse (headers.into_iter().rev()). Whole lib: 421 / 0 / 5, it survives.
      So no test depends on the order in which loop headers claim an exit, and no test pins the lower.rs half. Reading the code: BTreeMap::keys() is ascending, which is what main's sort_unstable() gave, and into_values() fills each counter's own phi, so the order there does not reach the output either. The lower.rs change is a cleanup with no change in behaviour, as KelboLive said.
  • cargo clippy -p leonida-shader --all-targets, default and --features diagnostics, on main and on the merge: 6 warnings on each side, the same set (all in valu_tests.rs), none in the changed lines. cargo clippy --locked -p leonida-shader -- -D warnings (the command in the body) is clean on both sides.
  • Read: next_if needs each block's plans in body order. That holds, because they are collected by walking that same body just before, and tree.order (reverse postorder, the order write lays the blocks out) lists each reachable block once. The new IDs therefore follow the layout order.
  • Not covered here: the game; translations of real programs (whether any output byte changes there); Linux; the other crates; the host feature.

Verdict: PASS. The new test is red on main for the stated reason and green on the merge, the suites and clippy match main with one test more, and the carry keeps #2872's change as it was. Two notes for the maintainers: on the probe module, the fix is about the internal numbering, not the emitted bytes, and the lower.rs half is a no-op cleanup that no test covers.

Machine: Windows 11 Pro 26300, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7339310843 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3059#issuecomment-7339310843). Originally posted by [enlight3d](https://github.com/enlight3d) on 2026-12-02T08:20:16Z. --- RUN (independent run on Windows, #3059 head 5d47d4f57, merged locally into `main` f37c4a87d with `git merge --no-ff`, no conflicts. The head already contains that `main`, so the merge adds only this pull request's diff: 2 files, +76/-15. Its added and removed lines are the same as in #2872's commit 9bb420346, so the carry leaves the author's change as it was. `main`'s `bounded_budget_caps_wide_phi_dag` is still in place, just above the new test. CI on the head: change-scope, check, d3d12 (windows-latest), 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`, one target folder per side: - `cargo test -p leonida-shader --no-fail-fast`: main 420 passed / 0 failed / 5 ignored; merge 421 / 0 / 5 (the new test). Some of these tests run on the GPU, here an RTX 3090. - `cargo test -p leonida-shader --no-fail-fast --features diagnostics`: main 420 / 0 / 5; merge 421 / 0 / 5. - Red, by assertion: `main`'s `opt.rs` with only this pull request's test added and its production hunk left out. `cargo test -p leonida-shader --lib chains_emits_deterministic`, run in 3 separate processes, fails 3 times out of 3 on the monotonic assertion, with a different order each time: `[[36, 37], [34, 35], [38, 39], [40, 41], [30, 31], [32, 33]]`, `[[32, 33], [34, 35], [38, 39], [36, 37], [40, 41], [30, 31]]`, `[[30, 31], [32, 33], [36, 37], [34, 35], [40, 41], [38, 39]]`. That is the reason the pull request gives: the blocks were walked in `HashMap` order when the new `and`s got their IDs. - Green: `cargo test -p leonida-shader --lib bitcode::opt::tests` on the merge: 21 passed. - What changes in the output, checked with a local probe: three tests that are not in this pull request, added to `opt.rs` on both sides. They build the new test's six-block module 64 times in one process. On main, the new IDs came out in 59 different orders, but `Module::write()` gave the same bytes in all 64 builds, whether after `chains()` alone, after `optimise(CHAINS | REUSE | DEAD)` or after `optimise(MODULE)`. On the merge: one order of the IDs in all 64 builds, and the same bytes in all three cases. So on this module, main's emitted bitcode was already the same from run to run. The pull request fixes the numbering inside `Module.insts` (what dumps and tests that print IDs see), as KelboLive's review on #2872 said. The body of #3059 says "instruction IDs", which is accurate. This is not a fix for output bytes or a shader cache key that changes between runs. Real programs were not checked. - Mutations of the merge. Each file was restored afterwards, and `git status` was clean: - The last block's plans are dropped (`by_block.pop()` after the empty check), so `chains()` rewrites one chain fewer. Killed by the new test (`emitted ids in next block`) and by the existing `conjunctions_of_conditions_share_the_terms_they_start_with` (stored values 4 instead of 2). The other 19 `bitcode::opt::tests` pass. - The block order goes back through a `HashMap` (`for (b, plans) in by_block.into_iter().collect::<HashMap<_, _>>()`). This is the defect itself. Killed by the new test in 3 processes out of 3, each with a different order. The other 20 `bitcode::opt::tests` pass, so the new test is the only one that pins it. - `counters` goes back to a `HashMap`, with the `sort_unstable()` still removed, so the loop headers come in random order in `find_exit_caps` and in the phi completion. Whole lib: 421 / 0 / 5, it survives. - `find_exit_caps` walks the headers in reverse (`headers.into_iter().rev()`). Whole lib: 421 / 0 / 5, it survives. So no test depends on the order in which loop headers claim an exit, and no test pins the `lower.rs` half. Reading the code: `BTreeMap::keys()` is ascending, which is what main's `sort_unstable()` gave, and `into_values()` fills each counter's own phi, so the order there does not reach the output either. The `lower.rs` change is a cleanup with no change in behaviour, as KelboLive said. - `cargo clippy -p leonida-shader --all-targets`, default and `--features diagnostics`, on main and on the merge: 6 warnings on each side, the same set (all in `valu_tests.rs`), none in the changed lines. `cargo clippy --locked -p leonida-shader -- -D warnings` (the command in the body) is clean on both sides. - Read: `next_if` needs each block's plans in body order. That holds, because they are collected by walking that same body just before, and `tree.order` (reverse postorder, the order `write` lays the blocks out) lists each reachable block once. The new IDs therefore follow the layout order. - Not covered here: the game; translations of real programs (whether any output byte changes there); Linux; the other crates; the `host` feature. Verdict: PASS. The new test is red on `main` for the stated reason and green on the merge, the suites and clippy match `main` with one test more, and the carry keeps #2872's change as it was. Two notes for the maintainers: on the probe module, the fix is about the internal numbering, not the emitted bytes, and the `lower.rs` half is a no-op cleanup that no test covers. Machine: Windows 11 Pro 26300, RTX 3090 24 GB, NVIDIA 621.40, Rust 1.98.1.
lixonik commented 2026-12-02 09:09:33 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by lixonik on 2026-12-02T09:09:33Z.


Verified locally on Windows 10 x64 (rebased onto main a917c54d6, PR head 5d47d4f57):

  • Red -> Green proof (chains_emits_deterministic_instruction_ids_across_blocks):
    • Without the opt.rs fix (keeping only the new test against main a917c54d6): FAILED (rc=101, non-monotonic block emission order [[38, 39], [36, 37], [30, 31], [40, 41], [32, 33], [34, 35]]).
    • With PR #3059 (5d47d4f57): PASSED (1 passed; 0 failed).
  • Targeted unit suite: cargo test -p leonida-shader --locked --lib emit::bitcode::opt:: -- 21 passed, 0 failed.
  • Lint & clean-room:
    • cargo clippy -p leonida-shader --locked --lib --tests -- exit 0 (0 warnings in touched opt.rs / lower.rs).
    • python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs d3d12/crates/leonida-shader/src/emit/lower.rs -- exit 0.
  • Code check:
    • opt.rs removes std::collections::HashMap completely; chains() now preserves tree.order in by_block: Vec<(usize, Vec<(u32, Vec<Cond>)>)> and consumes each block's sorted plans via .into_iter().peekable() without per-block HashMap allocation.
    • lower.rs changes Lower::counters from HashMap to BTreeMap, making std::mem::take(&mut self.counters).into_values() deterministic and removing the now-redundant .sort_unstable().

Result: PASS (note: full leonida-shader --lib hits a pre-existing Win10 GPU test 0xc0000005 in untouched emit::atomic_tests::heap_texture_atomic_widths, unrelated to this PR).

AI-assisted: yes (Antigravity).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7340464812 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3059#issuecomment-7340464812). Originally posted by [lixonik](https://github.com/lixonik) on 2026-12-02T09:09:33Z. --- Verified locally on Windows 10 x64 (rebased onto `main` `a917c54d6`, PR head `5d47d4f57`): - **Red -> Green proof (`chains_emits_deterministic_instruction_ids_across_blocks`):** - Without the `opt.rs` fix (keeping only the new test against `main` `a917c54d6`): **FAILED** (`rc=101`, non-monotonic block emission order `[[38, 39], [36, 37], [30, 31], [40, 41], [32, 33], [34, 35]]`). - With PR `#3059` (`5d47d4f57`): **PASSED** (`1 passed; 0 failed`). - **Targeted unit suite:** `cargo test -p leonida-shader --locked --lib emit::bitcode::opt::` -- **21 passed, 0 failed**. - **Lint & clean-room:** - `cargo clippy -p leonida-shader --locked --lib --tests` -- exit `0` (0 warnings in touched `opt.rs` / `lower.rs`). - `python -I scripts/check_no_game_data.py d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs d3d12/crates/leonida-shader/src/emit/lower.rs` -- exit `0`. - **Code check:** - `opt.rs` removes `std::collections::HashMap` completely; `chains()` now preserves `tree.order` in `by_block: Vec<(usize, Vec<(u32, Vec<Cond>)>)>` and consumes each block's sorted plans via `.into_iter().peekable()` without per-block `HashMap` allocation. - `lower.rs` changes `Lower::counters` from `HashMap` to `BTreeMap`, making `std::mem::take(&mut self.counters).into_values()` deterministic and removing the now-redundant `.sort_unstable()`. Result: **PASS** (note: full `leonida-shader --lib` hits a pre-existing Win10 GPU test `0xc0000005` in untouched `emit::atomic_tests::heap_texture_atomic_widths`, unrelated to this PR). AI-assisted: yes (Antigravity).
imShangLi commented 2026-12-02 10:18:55 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by imShangLi on 2026-12-02T10:18:55Z.


VERIFY-3059 — Linux, Fedora 45, Rust 1.98.1, no GPU device, no game files

Verdict: PASS. The change removes a real non-determinism from the translator's output, at the root cause (two standard HashMaps whose iteration order decided the order of the emitted instructions), and the new test fails on the merge base and passes with the pull request. Nothing regressed.

head 5d47d4f57 ("Merge remote-tracking branch 'origin/main' into carry/2872"), base origin/main = 80910637f, merge base f37c4a87d. Two files, +76/−15: d3d12/crates/leonida-shader/src/emit/lower.rs and d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs.

This pull request is a carry of #2872 (kirigayakazima): the original was in conflict with main because both sides appended a test at the end of the same module in opt.rs, so this branch is main merged with #2872's head and keeps both tests. #2872 stays open until a maintainer decides which one lands.

1. What it changes

Two maps whose iteration order decided the order of the emitted instructions:

  • lower.rs:591 — the loop headers' iteration counters become a BTreeMap instead of a HashMap. The order matters at lower.rs:15721:
    for (phi, incoming) in std::mem::take(&mut self.counters).into_values() {
    
    — the phi nodes are emitted in the map's iteration order, and a standard HashMap's order is different in every run (its RandomState seeds per map). The other use of the map (lower.rs:15407) collected the headers and sorted them, so it was already deterministic; the change replaces that sort with the BTreeMap's own order.
  • opt.rs:1434 — chains() grouped its per-block rewrite plans in a HashMap<usize, Vec<(u32, Vec<Cond>)>> and iterated it, so the blocks were rewritten in a random order and the new instructions landed in a random order. The plans are now grouped in tree.order (the block order) in a Vec, and the per-block lookup is a peekable() instead of a HashMap.

The new test chains_emits_deterministic_instruction_ids_across_blocks builds a module with six blocks in a reverse-ordered three-term conjunction, runs chains() on it and asserts that the new instruction IDs advance monotonically in tree order, then runs the whole thing 16 times in one process and asserts that the block bodies and the written bytes (m.write()) are identical every time. Within one process two HashMaps already iterate differently (RandomState is seeded per map), so the base fails the test.

2. The claims it makes, and which ones I checked

  • "chains() builds its per-block plans in tree.order instead of iterating a HashMap" — checked: by_block: Vec<(usize, Vec<(u32, Vec<Cond>)>)> is filled while walking for &b in &tree.order (opt.rs:1434), and the later loop walks that Vec. True.
  • "the loop counters in lower.rs use a BTreeMap" — checked: counters: BTreeMap<usize, (V, Vec<(V, usize)>)> (lower.rs:591, lower.rs:2953), and lower.rs:15407 no longer sorts the keys. True.
  • "the emitted instruction IDs no longer depend on the run (chains_emits_deterministic_instruction_ids_across_blocks pins it)" — checked: the test compares the bodies and m.write() over 16 runs, and it fails on the merge base (see below). True.
  • "carry of #2872 ... the author's commit is kept ... both tests stay" — checked: the author's commit 9bb42034 ("d3d12-shader: make chains() and loop-counter lowering deterministic") is in the branch's history, main's own test at the end of opt.rs (the 5-level 4-way diamond) is still there, and the new test follows it. True.
  • Why it matters — the module's bytes are the shader cache's key: the module doc of opt.rs says "Which passes run is emit::shader_opt() (LEONIDA_SHADER_OPT), part of the translation and so of the shader cache's key", and the emitted instruction order is part of those bytes. A translation whose bytes differ per run cannot hit the cache. (The pull request does not measure the cache-hit effect; neither did I.)
  • Windows numbers: "cargo test -p leonida-shader --lib: 421 passed, 0 failed, 5 ignored" — the author's; on Linux the crate gives 253 passed, 0 failed, 2 ignored (the difference is platform-gated tests). Their Windows run was not repeated here.

3. What the tests do here

  • The repository's own red/green tool: python3 -I scripts/red_green.py 3059 --crate leonida-shader --work-dir /home/shangli/oc-workspace/gta6/rg-3059 — verdict "red then green": the red side (the merge base plus only the pull request's test hunks) fails leonida_shader::emit::bitcode::opt::tests::chains_emits_deterministic_instruction_ids_across_blocks, the green side (the head) passes it, 252 tests pass on both sides, 0 regressions.
  • cargo test -p leonida-shader --lib --locked on the head: 253 passed, 0 failed, 2 ignored (2.6 s).

4. Anything wrong in the change (with the file and line)

Nothing that changes the verdict. Two observations for the maintainer:

  • Which of #2872 and #3059 lands first is a call for the maintainer. The carry keeps the author's commit and adds main's test, so the two pull requests overlap; the repository's rule is that the one that lands first is merged and the other is asked to rebase or close.
  • The remaining maps in the emit path are deterministic, but not sorted. The emit path still uses FxHashMap (rustc-hash) in several places and a standard HashMap in three (lds_exact.rs:74, lds_flags.rs:48, bitcode.rs:233): the FxHashMaps iterate in hash order, which is stable across runs of the same binary but would change if the hasher or the key types changed (a rebuild), and the three standard ones are only looked up, never iterated, so their order does not reach the output. The output is therefore stable across runs now; it is not, and was not, sorted.

5. What I did not check

  • No GPU, no game files, no title: the crate's unit tests are CPU-only. I did not translate the corpus, so how often a shader actually reaches chains() or a loop counter (and how often the non-determinism was visible in the shipped shader library) is not measured — the finding is from the code, not from a corpus count.
  • No Windows build: everything here is Linux with the Rust toolchain in d3d12/rust-toolchain.toml. The author's Windows numbers (421 passed, 5 ignored) are theirs; the counts differ because of platform-gated tests.
  • The cache-hit effect is not measured (no run of the runtime, no shader cache).
  • The FxHashMap claim above is from reading the code (opt.rs:150, opt.rs:1028, lower.rs:416 and the three standard maps); I did not instrument a rebuild to see whether the emitted bytes change.
  • The PR's CI log and the discussion on the pull request were not read (read-only, and no gh CLI here).
  • The other crates of the workspace were not built; only leonida-shader was, because the change is confined to it.

AI-assisted: the report was written with MiniMax M3.1 Flash (the pull request's own author says it was written with Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7342287196 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3059#issuecomment-7342287196). Originally posted by [imShangLi](https://github.com/imShangLi) on 2026-12-02T10:18:55Z. --- ## VERIFY-3059 — Linux, Fedora 45, Rust 1.98.1, no GPU device, no game files **Verdict: PASS.** The change removes a real non-determinism from the translator's output, at the root cause (two standard `HashMap`s whose iteration order decided the order of the emitted instructions), and the new test fails on the merge base and passes with the pull request. Nothing regressed. head `5d47d4f57` ("Merge remote-tracking branch 'origin/main' into carry/2872"), base `origin/main` = `80910637f`, merge base `f37c4a87d`. Two files, +76/−15: `d3d12/crates/leonida-shader/src/emit/lower.rs` and `d3d12/crates/leonida-shader/src/emit/bitcode/opt.rs`. This pull request is a **carry** of #2872 (kirigayakazima): the original was in conflict with `main` because both sides appended a test at the end of the same module in `opt.rs`, so this branch is `main` merged with #2872's head and keeps both tests. #2872 stays open until a maintainer decides which one lands. ### 1. What it changes Two maps whose **iteration order** decided the order of the emitted instructions: - **`lower.rs:591`** — the loop headers' iteration counters become a `BTreeMap` instead of a `HashMap`. The order matters at `lower.rs:15721`: ```rust for (phi, incoming) in std::mem::take(&mut self.counters).into_values() { ``` — the phi nodes are emitted in the map's iteration order, and a standard `HashMap`'s order is different in every run (its `RandomState` seeds per map). The other use of the map (`lower.rs:15407`) collected the headers and sorted them, so it was already deterministic; the change replaces that sort with the `BTreeMap`'s own order. - **`opt.rs:1434`** — `chains()` grouped its per-block rewrite plans in a `HashMap<usize, Vec<(u32, Vec<Cond>)>>` and iterated it, so the blocks were rewritten in a random order and the new instructions landed in a random order. The plans are now grouped in `tree.order` (the block order) in a `Vec`, and the per-block lookup is a `peekable()` instead of a `HashMap`. The new test `chains_emits_deterministic_instruction_ids_across_blocks` builds a module with six blocks in a reverse-ordered three-term conjunction, runs `chains()` on it and asserts that the new instruction IDs advance monotonically in tree order, then runs the whole thing **16 times in one process** and asserts that the block bodies and the written bytes (`m.write()`) are identical every time. Within one process two `HashMap`s already iterate differently (`RandomState` is seeded per map), so the base fails the test. ### 2. The claims it makes, and which ones I checked - **"chains() builds its per-block plans in tree.order instead of iterating a HashMap"** — checked: `by_block: Vec<(usize, Vec<(u32, Vec<Cond>)>)>` is filled while walking `for &b in &tree.order` (`opt.rs:1434`), and the later loop walks that `Vec`. True. - **"the loop counters in lower.rs use a BTreeMap"** — checked: `counters: BTreeMap<usize, (V, Vec<(V, usize)>)>` (`lower.rs:591`, `lower.rs:2953`), and `lower.rs:15407` no longer sorts the keys. True. - **"the emitted instruction IDs no longer depend on the run (chains_emits_deterministic_instruction_ids_across_blocks pins it)"** — checked: the test compares the bodies *and* `m.write()` over 16 runs, and it fails on the merge base (see below). True. - **"carry of #2872 ... the author's commit is kept ... both tests stay"** — checked: the author's commit `9bb42034` ("d3d12-shader: make chains() and loop-counter lowering deterministic") is in the branch's history, `main`'s own test at the end of `opt.rs` (the 5-level 4-way diamond) is still there, and the new test follows it. True. - **Why it matters** — the module's bytes are the shader cache's key: the module doc of `opt.rs` says "Which passes run is `emit::shader_opt()` (`LEONIDA_SHADER_OPT`), part of the translation and so of the shader cache's key", and the emitted instruction order is part of those bytes. A translation whose bytes differ per run cannot hit the cache. (The pull request does not measure the cache-hit effect; neither did I.) - **Windows numbers: "cargo test -p leonida-shader --lib: 421 passed, 0 failed, 5 ignored"** — the author's; on Linux the crate gives 253 passed, 0 failed, 2 ignored (the difference is platform-gated tests). Their Windows run was not repeated here. ### 3. What the tests do here - **The repository's own red/green tool**: `python3 -I scripts/red_green.py 3059 --crate leonida-shader --work-dir /home/shangli/oc-workspace/gta6/rg-3059` — verdict **"red then green"**: the red side (the merge base plus only the pull request's test hunks) fails `leonida_shader::emit::bitcode::opt::tests::chains_emits_deterministic_instruction_ids_across_blocks`, the green side (the head) passes it, **252 tests pass on both sides, 0 regressions**. - `cargo test -p leonida-shader --lib --locked` on the head: **253 passed, 0 failed, 2 ignored** (2.6 s). ### 4. Anything wrong in the change (with the file and line) Nothing that changes the verdict. Two observations for the maintainer: - **Which of #2872 and #3059 lands first is a call for the maintainer.** The carry keeps the author's commit and adds main's test, so the two pull requests overlap; the repository's rule is that the one that lands first is merged and the other is asked to rebase or close. - **The remaining maps in the emit path are deterministic, but not sorted.** The emit path still uses `FxHashMap` (rustc-hash) in several places and a standard `HashMap` in three (`lds_exact.rs:74`, `lds_flags.rs:48`, `bitcode.rs:233`): the `FxHashMap`s iterate in hash order, which is stable across runs of the same binary but would change if the hasher or the key types changed (a rebuild), and the three standard ones are only **looked up**, never iterated, so their order does not reach the output. The output is therefore stable across runs now; it is not, and was not, sorted. ### 5. What I did not check - **No GPU, no game files, no title**: the crate's unit tests are CPU-only. I did not translate the corpus, so how often a shader actually reaches `chains()` or a loop counter (and how often the non-determinism was visible in the shipped shader library) is not measured — the finding is from the code, not from a corpus count. - **No Windows build**: everything here is Linux with the Rust toolchain in `d3d12/rust-toolchain.toml`. The author's Windows numbers (421 passed, 5 ignored) are theirs; the counts differ because of platform-gated tests. - **The cache-hit effect is not measured** (no run of the runtime, no shader cache). - **The `FxHashMap` claim above is from reading the code** (`opt.rs:150`, `opt.rs:1028`, `lower.rs:416` and the three standard maps); I did not instrument a rebuild to see whether the emitted bytes change. - **The PR's CI log and the discussion on the pull request** were not read (read-only, and no gh CLI here). - **The other crates of the workspace** were not built; only `leonida-shader` was, because the change is confined to it. AI-assisted: the report was written with MiniMax M3.1 Flash (the pull request's own author says it was written with Claude Code).
MoonEyes commented 2026-12-02 10:38:00 +00:00 (Migrated from github.com)

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


RUN (in game), #3059 head 5d47d4f57 merged onto main a917c54d6 with git merge --no-ff, d3d12 layer rebuilt from that merge (diag profile). RTX 4060 8 GB, driver 610.88, Windows 11. Series S, -Resolution 1080, frame generation 2x, launcher default frame limit, same walking route as the #3062 runs, 330 s after reaching the world.

What Result
Device lost / device removed 0
Exceptions, panics, hang watchdog, GPU stall lines 0
Process alive at the end yes, stopped by the time limit
Rendered fps, last 240 s (48 samples) median 15.2, mean 14.5
Rendered fps, last 120 s median 15.2
Pipeline precompilation before the first frame 1425 s (157 s on the same machine this morning on main + #3062)

For reference, the #3062 runs this morning on the same machine and route rendered 13.3 to 13.9 fps over the last 240 s; those had LEONIDA_FG_LIMIT_FPS set explicitly, this one did not, so the fps are not a like-for-like comparison, only a check that nothing fell over. Three samples dropped to 1.0-4.3 fps, the last one at the moment the run was stopped.

Verdict: PASS for the in-game gate (300 s or more in the world, no device loss). The long precompilation is consistent with a shader-compiler change invalidating the pipeline cache (ASSUMED, not checked by key).

Limits: one run; free system memory was low (median 0.6 GB); arrival in the world was driven by keys and not checked on pictures.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7342755519 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3059#issuecomment-7342755519). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T10:38:00Z. --- RUN (in game), #3059 head 5d47d4f57 merged onto main a917c54d6 with `git merge --no-ff`, d3d12 layer rebuilt from that merge (diag profile). RTX 4060 8 GB, driver 610.88, Windows 11. Series S, -Resolution 1080, frame generation 2x, launcher default frame limit, same walking route as the #3062 runs, 330 s after reaching the world. | What | Result | |---|---| | Device lost / device removed | 0 | | Exceptions, panics, hang watchdog, GPU stall lines | 0 | | Process alive at the end | yes, stopped by the time limit | | Rendered fps, last 240 s (48 samples) | median 15.2, mean 14.5 | | Rendered fps, last 120 s | median 15.2 | | Pipeline precompilation before the first frame | 1425 s (157 s on the same machine this morning on main + #3062) | For reference, the #3062 runs this morning on the same machine and route rendered 13.3 to 13.9 fps over the last 240 s; those had `LEONIDA_FG_LIMIT_FPS` set explicitly, this one did not, so the fps are not a like-for-like comparison, only a check that nothing fell over. Three samples dropped to 1.0-4.3 fps, the last one at the moment the run was stopped. Verdict: PASS for the in-game gate (300 s or more in the world, no device loss). The long precompilation is consistent with a shader-compiler change invalidating the pipeline cache (ASSUMED, not checked by key). Limits: one run; free system memory was low (median 0.6 GB); arrival in the world was driven by keys and not checked on pictures. AI-assisted (Claude Code).
MoonEyes commented 2026-12-02 10:40:00 +00:00 (Migrated from github.com)

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


Merging. Head 5d47d4f57 merges cleanly with main 3a3c00822 and does not force a rebase of #2566. Independent check on head 5d47d4f57: enlight3d's red/green (new test red on main in 3/3 processes, green 21/21, leonida-shader 420 -> 421, comment 7339310843); in-game run of 330 s in the world on main a917c54d6 + this head, 0 device lost, 0 exceptions (comment 7342755519); CI green; merge approved in writing by Moon for this render-path PR. Merged by MoonEyes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7342803908 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3059#issuecomment-7342803908). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-12-02T10:40:00Z. --- Merging. Head `5d47d4f57` merges cleanly with `main` `3a3c00822` and does not force a rebase of #2566. Independent check on head 5d47d4f57: enlight3d's red/green (new test red on main in 3/3 processes, green 21/21, leonida-shader 420 -> 421, comment 7339310843); in-game run of 330 s in the world on main a917c54d6 + this head, 0 device lost, 0 exceptions (comment 7342755519); CI green; merge approved in writing by Moon for this render-path PR. Merged by MoonEyes. AI-assisted (Claude Code).
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!3059
No description provided.