D3D12-SHADER-STRUCTURE-GIVEN-FORCED: Bools::given from the literals a condition forces (translator -8% CPU, DXIL unchanged) #2726

Merged
bobcatchris merged 1 commit from upstream-pr/2726 into main 2026-11-30 20:13:01 +00:00
bobcatchris commented 2026-11-30 13:33:43 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by bobcatchris on 2026-11-30T13:33:43Z.


Claim: D3D12-SHADER-STRUCTURE-GIVEN-FORCED (comment).

AI-assisted: Claude Sonnet 5.5 (Cursor agent). No game files are in this pull request: the numbers are counts and times from running the translator on the author's own copy of the archive, and the tool that hashed the output was a local patch that is not part of it.

What

Bools::given(b, known) (the BDD behind structure::structure) restricted b by every atom that known decides. It found the atoms one at a time: for each atom of b, build the conjunction of known with the atom's negation (and again with the atom) to compare it with 0. It is called from Lanes::finish for each block's guards, branches and mask joins.

It is now the restriction of b by the literals known forces, found once per node of known (Bools::forced: a node's literals are what both children force, or the one child's plus the node's own atom where the other child is 0) and kept for the later calls on the same condition (bounded at 16 M literals, then it starts over). Where nothing satisfies known every atom b has is decided both ways and the old code took them as true; that is kept (b at all-ones). The result is the same function, so the same node.

Why

Counts from one leonida translate <shaders.rpf> (LEONIDA_HOST_RDNA=1, 26,084 programs, 40.6 M IR instructions, --release at 497bdd039, Ryzen 7 7800X3D, 16 threads; timers around each stage in a scratch build): 890 s of CPU, of them 330 in structure::structure, 313 of those in the 720 programs of 10,000 to 30,000 instructions (0.4 µs an instruction below 1,000 instructions, 28 µs there). finish is 114 s of the 330 and given is 97 s of that (1.52 M calls, 5.4 M atoms, 10.7 M implies). That is what a cold shader cache pays for the heavy compute programs on the translating threads before their first dispatch; docs/findings.md has the table (new section "Where the translator's time goes").

Result

Same machine, same archive, alternating old and new builds of the same source plus a local patch that appends blake3(dxil) of each emitted program to a file:

run wall CPU (user + kernel)
old 1 / 2 / 3 65.2 / 65.1 / 65.1 s 890 / 894 / 889 s
new 1 / 2 / 3 59.4 / 59.5 / 61.6 s 818 / 820 / 821 s

(about 8 % of the CPU and 6 to 9 % of the wall clock; the wall clock ends on the longest program, so it gains less than the sum.)

Output unchanged: the DXIL of all 25,689 emitted programs (15,799 distinct; the rest are duplicate containers) hashes the same in all 9 runs (5 of the old build, 4 of the new), compared as multisets of (hash, length); the label of a duplicate container is whichever thread gets there first and differs between runs of the same build, which is why the labels are not compared.

Tests

  • New given_as_the_forced_literals_of_what_is_known_gives_what_testing_each_atom_gave: keeps the old algorithm as the reference (given_anew) and compares it with given for every pair of 72 random functions of 10 atoms (conjunctions of literals among them, as a block's reach is, and the constants), and asserts that the cases where the function changes, stays, or nothing is known each come up more than 10 times; it also checks forced's literals against implies for every atom and their variable order. Controlled: with the intersection's comparison of the two sides' values left out it fails (Bool(10) given Bool(14)).
  • cargo test -p leonida-shader: 385 passed, 1 failed, 4 ignored. The one failure, emit::valu_tests::rdna_forms_give_what_the_spelled_out_ones_give (mad_legacy: written alike), fails the same on main without this change (run with this file stashed); it is what #2624 fixes. structure:: alone: 15 passed.
  • cargo clippy -p leonida-shader --no-deps --all-targets: 3 warnings in the library and 7 in its tests, the same as on main; Bools::restrict is only the test's reference now and is #[cfg(test)], so it adds none.
  • python -I scripts/check_no_game_data.py on both files: clean.
  • No GPU is involved (leonida translate without --validate or --pipelines), so no GPU or driver to name.

Not covered: the effect on a title's frame time (it is the translation, not the frame), and the other ~230 s of the BDD's ite (masks_in_order, paths), which this does not touch.

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5566296643 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2726). Originally posted by [bobcatchris](https://github.com/bobcatchris) on 2026-11-30T13:33:43Z. --- Claim: D3D12-SHADER-STRUCTURE-GIVEN-FORCED ([comment](https://github.com/ProjectLeonida/mandy/issues/2570#issuecomment-7279537850)). AI-assisted: Claude Sonnet 5.5 (Cursor agent). No game files are in this pull request: the numbers are counts and times from running the translator on the author's own copy of the archive, and the tool that hashed the output was a local patch that is not part of it. ## What `Bools::given(b, known)` (the BDD behind `structure::structure`) restricted `b` by every atom that `known` decides. It found the atoms one at a time: for each atom of `b`, build the conjunction of `known` with the atom's negation (and again with the atom) to compare it with 0. It is called from `Lanes::finish` for each block's guards, branches and mask joins. It is now the restriction of `b` by the literals `known` forces, found once per node of `known` (`Bools::forced`: a node's literals are what both children force, or the one child's plus the node's own atom where the other child is 0) and kept for the later calls on the same condition (bounded at 16 M literals, then it starts over). Where nothing satisfies `known` every atom `b` has is decided both ways and the old code took them as true; that is kept (`b` at all-ones). The result is the same function, so the same node. ## Why Counts from one `leonida translate <shaders.rpf>` (`LEONIDA_HOST_RDNA=1`, 26,084 programs, 40.6 M IR instructions, `--release` at `497bdd039`, Ryzen 7 7800X3D, 16 threads; timers around each stage in a scratch build): 890 s of CPU, of them 330 in `structure::structure`, 313 of those in the 720 programs of 10,000 to 30,000 instructions (0.4 µs an instruction below 1,000 instructions, 28 µs there). `finish` is 114 s of the 330 and `given` is 97 s of that (1.52 M calls, 5.4 M atoms, 10.7 M `implies`). That is what a cold shader cache pays for the heavy compute programs on the translating threads before their first dispatch; `docs/findings.md` has the table (new section "Where the translator's time goes"). ## Result Same machine, same archive, alternating old and new builds of the same source plus a local patch that appends `blake3(dxil)` of each emitted program to a file: | run | wall | CPU (user + kernel) | |---|---|---| | old 1 / 2 / 3 | 65.2 / 65.1 / 65.1 s | 890 / 894 / 889 s | | new 1 / 2 / 3 | 59.4 / 59.5 / 61.6 s | 818 / 820 / 821 s | (about 8 % of the CPU and 6 to 9 % of the wall clock; the wall clock ends on the longest program, so it gains less than the sum.) **Output unchanged:** the DXIL of all 25,689 emitted programs (15,799 distinct; the rest are duplicate containers) hashes the same in all 9 runs (5 of the old build, 4 of the new), compared as multisets of (hash, length); the label of a duplicate container is whichever thread gets there first and differs between runs of the same build, which is why the labels are not compared. ## Tests - New `given_as_the_forced_literals_of_what_is_known_gives_what_testing_each_atom_gave`: keeps the old algorithm as the reference (`given_anew`) and compares it with `given` for every pair of 72 random functions of 10 atoms (conjunctions of literals among them, as a block's reach is, and the constants), and asserts that the cases where the function changes, stays, or nothing is known each come up more than 10 times; it also checks `forced`'s literals against `implies` for every atom and their variable order. Controlled: with the intersection's comparison of the two sides' values left out it fails (`Bool(10) given Bool(14)`). - `cargo test -p leonida-shader`: 385 passed, 1 failed, 4 ignored. The one failure, `emit::valu_tests::rdna_forms_give_what_the_spelled_out_ones_give` (`mad_legacy: written alike`), fails the same on `main` without this change (run with this file stashed); it is what #2624 fixes. `structure::` alone: 15 passed. - `cargo clippy -p leonida-shader --no-deps --all-targets`: 3 warnings in the library and 7 in its tests, the same as on `main`; `Bools::restrict` is only the test's reference now and is `#[cfg(test)]`, so it adds none. - `python -I scripts/check_no_game_data.py` on both files: clean. - No GPU is involved (`leonida translate` without `--validate` or `--pipelines`), so no GPU or driver to name. Not covered: the effect on a title's frame time (it is the translation, not the frame), and the other ~230 s of the BDD's `ite` (`masks_in_order`, `paths`), which this does not touch.
KelboLive commented 2026-11-30 14:36:11 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by KelboLive on 2026-11-30T14:36:11Z.


Independent check on Windows (not the author), at 986771506.

  • cargo test --locked -p leonida-shader (pinned 1.98.1): 386 passed, 0 failed, 4 ignored in 247.9 s; doc-tests 0.
  • Integration build with scripts/build-nvidia.ps1 (diag profile, features diagnostics,mimalloc): succeeded in 1m 00s, leonida.exe 20,209,152 bytes.
  • The new randomized given/forced equivalence test runs as part of the suite.

Machine: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), MSVC 14.52.37025, Rust 1.98.1 as pinned by d3d12/rust-toolchain.toml.

Not independently measured: the PR's -8% translator CPU claim (that needs a corpus work folder); this check covers correctness and that the d3d12 runtime still builds with the change.

AI-assisted: yes (opencode).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7281634608 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2726#issuecomment-7281634608). Originally posted by [KelboLive](https://github.com/KelboLive) on 2026-11-30T14:36:11Z. --- Independent check on Windows (not the author), at `986771506`. - `cargo test --locked -p leonida-shader` (pinned 1.98.1): **386 passed, 0 failed, 4 ignored** in 247.9 s; doc-tests 0. - Integration build with `scripts/build-nvidia.ps1` (diag profile, features `diagnostics,mimalloc`): **succeeded in 1m 00s**, `leonida.exe` 20,209,152 bytes. - The new randomized `given`/`forced` equivalence test runs as part of the suite. Machine: Ryzen 9 7950X3D, Windows 11 Pro 10.0.26200.9711, NVIDIA RTX 4070 Ti SUPER (driver 621.01), MSVC 14.52.37025, Rust 1.98.1 as pinned by `d3d12/rust-toolchain.toml`. Not independently measured: the PR's -8% translator CPU claim (that needs a corpus work folder); this check covers correctness and that the d3d12 runtime still builds with the change. AI-assisted: yes (opencode).
merzleckeier commented 2026-11-30 14:57:51 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-11-30T14:57:51Z.


Independent Linux truth-table and guard_runs check

I tested 986771506 merged onto main 232f9e603 (tree a6680ead) on CachyOS, Ryzen 7 7800X3D, Rust 1.98.1.

  • MEASURED, crate unit tests. cargo test --locked --offline -p leonida-shader --lib passed 228 tests on main and 229 with the PR, with 2 ignored in each run.
  • MEASURED, independent truth-table oracle. I extracted the BDD engine from each tree and checked given(formula, known) for every pair of three-variable truth tables over all eight assignments. Both engines matched all 524,288 evaluations, including unsatisfiable known. The PR also matched every evaluation with a test-only limit of 16 cached literals, which triggered 11 clears during this run. The production limit was used for the stock tests and benchmark.
  • MEASURED, stock synthetic guard_runs benchmark. I alternated nine pairs of optimized benchmark executables on one pinned CPU. Each cell shows the median of the nine per-program medians and their range, in microseconds.
Case main µs PR µs Median change
Unmasked 13.399 (13.290–14.950) 13.555 (13.338–15.681) +1.2%
Short masked runs 6.750 (6.659–6.952) 5.233 (5.177–5.341) −22.5%
Long masked runs 19.809 (19.355–20.758) 18.134 (17.766–20.022) −8.5%

The masked timings were lower in 9/9 short-run pairs and 8/9 long-run pairs. The unmasked ranges overlap. This benchmark measures structurisation on its synthetic fixtures. A second agent independently verified the oracle and recomputed the benchmark statistics. AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7282209290 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2726#issuecomment-7282209290). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-11-30T14:57:51Z. --- **Independent Linux truth-table and `guard_runs` check** I tested `986771506` merged onto `main` `232f9e603` (tree `a6680ead`) on CachyOS, Ryzen 7 7800X3D, Rust 1.98.1. - **MEASURED, crate unit tests.** `cargo test --locked --offline -p leonida-shader --lib` passed 228 tests on `main` and 229 with the PR, with 2 ignored in each run. - **MEASURED, independent truth-table oracle.** I extracted the BDD engine from each tree and checked `given(formula, known)` for every pair of three-variable truth tables over all eight assignments. Both engines matched all 524,288 evaluations, including unsatisfiable `known`. The PR also matched every evaluation with a test-only limit of 16 cached literals, which triggered 11 clears during this run. The production limit was used for the stock tests and benchmark. - **MEASURED, stock synthetic `guard_runs` benchmark.** I alternated nine pairs of optimized benchmark executables on one pinned CPU. Each cell shows the median of the nine per-program medians and their range, in microseconds. | Case | `main` µs | PR µs | Median change | | --- | ---: | ---: | ---: | | Unmasked | 13.399 (13.290–14.950) | 13.555 (13.338–15.681) | +1.2% | | Short masked runs | 6.750 (6.659–6.952) | 5.233 (5.177–5.341) | −22.5% | | Long masked runs | 19.809 (19.355–20.758) | 18.134 (17.766–20.022) | −8.5% | The masked timings were lower in 9/9 short-run pairs and 8/9 long-run pairs. The unmasked ranges overlap. This benchmark measures structurisation on its synthetic fixtures. A second agent independently verified the oracle and recomputed the benchmark statistics. AI-assisted with OpenAI Codex.
MoonEyes commented 2026-11-30 20:11:42 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T20:11:42Z.


RUN · VERIFY-2726-RTX4060 · by MoonEyes (another person than the author) on head 986771506, with #2745 (88d0dbf9c), #2773 (6bbaeed11), #2726 (986771506) and #2780 (6c4474cfd) merged into main 51dbc5b58. Windows 11 Pro 26300, RTX 4060 8 GB, -SeriesS -FrameGen 2, diag build.

  • cargo test -p leonida-shader --locked: 400 passed; -p leonida-gpu: 580 passed; 0 failed.
  • Game: two runs of 480 s, the person walking in the world from 345 s. First (shaders translated again: 132 s, pipelines warmed: 533 s): steady after 98 s, about 500 s of it. Second, warm: steady after 107 s, about 495 s of it, about 51 m walked. In both no device loss, no unhandled exception, the process alive at the end. PASS for the gate of 300 s. The picture was not looked at.
  • Translation at a first start 132 s here, 177 s on main 8f41d7d01 with two other pull requests: n=1 on each side and not the same tree, so no speed-up is claimed. The four pull requests were in one build.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7290548623 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2726#issuecomment-7290548623). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-30T20:11:42Z. --- RUN · VERIFY-2726-RTX4060 · by MoonEyes (another person than the author) on head `986771506`, with #2745 (`88d0dbf9c`), #2773 (`6bbaeed11`), #2726 (`986771506`) and #2780 (`6c4474cfd`) merged into `main` `51dbc5b58`. Windows 11 Pro 26300, RTX 4060 8 GB, `-SeriesS -FrameGen 2`, diag build. - `cargo test -p leonida-shader --locked`: 400 passed; `-p leonida-gpu`: 580 passed; 0 failed. - Game: two runs of 480 s, the person walking in the world from 345 s. First (shaders translated again: 132 s, pipelines warmed: 533 s): steady after 98 s, about 500 s of it. Second, warm: steady after 107 s, about 495 s of it, about 51 m walked. In both no device loss, no unhandled exception, the process alive at the end. PASS for the gate of 300 s. The picture was not looked at. - Translation at a first start 132 s here, 177 s on `main` `8f41d7d01` with two other pull requests: n=1 on each side and not the same tree, so no speed-up is claimed. The four pull requests were in one build. AI-assisted (Claude Code).
MoonEyes commented 2026-11-30 20:12:57 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T20:12:57Z.


Merging. Head 986771506 merges cleanly with main 810651e15 and does not force a rebase of #2566. Two runs in the game (RTX 4060, about 500 s steady each, no device loss) with #2745, #2773, #2726 and #2780 together on this head; cargo test green; checked by other people. The shaders are translated again at the first start. Merged by MoonEyes.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7290578921 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/2726#issuecomment-7290578921). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-30T20:12:57Z. --- Merging. Head `986771506` merges cleanly with `main` `810651e15` and does not force a rebase of #2566. Two runs in the game (RTX 4060, about 500 s steady each, no device loss) with #2745, #2773, #2726 and #2780 together on this head; cargo test green; checked by other people. The shaders are translated again at the first start. 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!2726
No description provided.