perf(shader): fold idempotent Saturate, FMax, and FMin calls (#312) #314

Open
RUIZ-Enrique-25002136 wants to merge 1 commit from upstream-pr/314 into main
RUIZ-Enrique-25002136 commented 2026-12-02 09:01:11 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by RUIZ-Enrique-25002136 on 2026-12-02T09:01:11Z.


Summary

Implements bit-exact algebraic folding for idempotent Saturate, FMax, and FMin DXIL intrinsic calls in crates/victory-shader/src/emit/bitcode/opt.rs::Module::simplify.

Fixes #312
Refs #276

AI-Ready Context & Change Map

For downstream coding/review agents, here is the exact scope, rewrite rule set, and verification summary of this PR:

  1. crates/victory-shader/src/emit/bitcode/opt.rs — Module::simplify (Op::Call arm, opt.rs:693-726)

    • Extracts the DXIL opcode constant from args[0] via self.int_k(...) and handles:
      • dx.op.unary.f32 opcode 7 (Saturate):
        • Saturate(Saturate(y)) -> Saturate(y) (Some(*x) when *x is itself dx.op.unary.f32(7, _)).
        • Exact because Saturate already maps every finite/infinite/NaN input into [+0.0, 1.0].
      • dx.op.binary.f32 opcodes 35 (FMax) and 36 (FMin):
        • Self-idempotence: op(a, a) -> a. Under DXIL FMax/FMin semantics, a NaN is returned only if both inputs are NaN, so (a, a) always produces a bit-for-bit (preserving -0.0 and NaN payloads).
        • Same-op nested absorption: op(op(x, y), z) -> op(x, y) and op(z, op(x, y)) -> op(x, y) whenever z == x || z == y and both calls share the same opcode (35 == 35 or 36 == 36). Preserves DXIL NaN propagation in all 4 finite/one-NaN/both-NaN cases; cross-opcode nests like FMax(FMin(x, y), y) are intentionally not absorbed.
    • Note: REUSE + drop_dead() in Module::optimise already deduplicate identical inner calls and remove dead producers once simplify returns the existing V.
  2. crates/victory-shader/src/emit/bitcode/opt.rs — Unit Test

    • Added idempotent_saturate_and_minmax_calls_fold (opt.rs:2261-2332) testing Saturate(Saturate(x)), FMax(x, x), FMin(y, y), FMax(FMax(x, y), y), FMin(x, FMin(x, y)), and negative case FMax(FMin(x, y), y), plus Module::write() round-trip.

Rig Specs Tested On

  • CPU: Linux workstation (offline unit tests, differential fuzzer, and clippy)
  • GPU & VRAM: N/A (not executed on live Windows D3D12 hardware)
  • System RAM: N/A
  • OS & GPU Driver: Linux (x86_64) — Windows x86_64-pc-windows-msvc full workspace clippy and live GPU frame-time benchmarks deferred to GitHub Actions (windows-latest) / maintainer run

Run Settings & Profile

  • Build & Commit SHA: victory-shader bitcode optimizer changes on top of origin/main (c7d3102)
  • Console Profile & Internal Resolution / Scale: N/A (unit tests)
  • Ray Tracing: N/A
  • Upscaling: N/A
  • Frame Generation: N/A
  • Launcher Command / Preset & Extra VICTORY_* Overrides: N/A
  • Scene / Area & Session Length: N/A

Measurements & Verification

  • Base FPS (without Frame Gen): N/A (offline compiler unit tests)
  • Frame-Gen FPS (if enabled): N/A
  • VRAM / RAM Usage: N/A
  • Crashes / DRED Resets: N/A
  • Automated Checks:
    • cargo clippy --workspace --all-targets --locked -- -D warnings (requires x86_64-pc-windows-msvc; verified on GitHub Actions windows-latest CI)
    • cargo clippy --workspace --all-targets --all-features --locked -- -D warnings (requires x86_64-pc-windows-msvc; verified on GitHub Actions windows-latest CI)
    • cargo clippy --locked -p victory-shader --lib -- -D warnings -> 0 warnings, exit 0
    • cargo test --locked -p victory-shader -- --test-threads=1 -> 253 passed, 0 failed, 2 ignored (including idempotent_saturate_and_minmax_calls_fold and every_pass_leaves_what_random_programs_store)
    • cargo test --locked -p xtask -> 8 passed, 0 failed
    • cargo xtask policy -> asset policy: 321 files ok
<!-- greyfinch-sync:Androtim/MiniVI:issue:5603188514 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/314). Originally posted by [RUIZ-Enrique-25002136](https://github.com/RUIZ-Enrique-25002136) on 2026-12-02T09:01:11Z. --- ## Summary Implements bit-exact algebraic folding for idempotent `Saturate`, `FMax`, and `FMin` DXIL intrinsic calls in `crates/victory-shader/src/emit/bitcode/opt.rs::Module::simplify`. Fixes #312 Refs #276 ### AI-Ready Context & Change Map For downstream coding/review agents, here is the exact scope, rewrite rule set, and verification summary of this PR: 1. **`crates/victory-shader/src/emit/bitcode/opt.rs` — `Module::simplify` (`Op::Call` arm, `opt.rs:693-726`)** - Extracts the DXIL opcode constant from `args[0]` via `self.int_k(...)` and handles: - **`dx.op.unary.f32` opcode `7` (`Saturate`):** - `Saturate(Saturate(y)) -> Saturate(y)` (`Some(*x)` when `*x` is itself `dx.op.unary.f32(7, _)`). - Exact because `Saturate` already maps every finite/infinite/NaN input into `[+0.0, 1.0]`. - **`dx.op.binary.f32` opcodes `35` (`FMax`) and `36` (`FMin`):** - **Self-idempotence:** `op(a, a) -> a`. Under DXIL `FMax`/`FMin` semantics, a NaN is returned only if *both* inputs are NaN, so `(a, a)` always produces `a` bit-for-bit (preserving `-0.0` and NaN payloads). - **Same-op nested absorption:** `op(op(x, y), z) -> op(x, y)` and `op(z, op(x, y)) -> op(x, y)` whenever `z == x || z == y` and both calls share the same opcode (`35 == 35` or `36 == 36`). Preserves DXIL NaN propagation in all 4 finite/one-NaN/both-NaN cases; cross-opcode nests like `FMax(FMin(x, y), y)` are intentionally not absorbed. - Note: `REUSE` + `drop_dead()` in `Module::optimise` already deduplicate identical inner calls and remove dead producers once `simplify` returns the existing `V`. 2. **`crates/victory-shader/src/emit/bitcode/opt.rs` — Unit Test** - Added `idempotent_saturate_and_minmax_calls_fold` (`opt.rs:2261-2332`) testing `Saturate(Saturate(x))`, `FMax(x, x)`, `FMin(y, y)`, `FMax(FMax(x, y), y)`, `FMin(x, FMin(x, y))`, and negative case `FMax(FMin(x, y), y)`, plus `Module::write()` round-trip. ## Rig Specs Tested On - **CPU:** Linux workstation (offline unit tests, differential fuzzer, and clippy) - **GPU & VRAM:** N/A (not executed on live Windows D3D12 hardware) - **System RAM:** N/A - **OS & GPU Driver:** Linux (x86_64) — Windows `x86_64-pc-windows-msvc` full workspace clippy and live GPU frame-time benchmarks deferred to GitHub Actions (`windows-latest`) / maintainer run ## Run Settings & Profile - **Build & Commit SHA:** `victory-shader` bitcode optimizer changes on top of `origin/main` (`c7d3102`) - **Console Profile & Internal Resolution / Scale:** N/A (unit tests) - **Ray Tracing:** N/A - **Upscaling:** N/A - **Frame Generation:** N/A - **Launcher Command / Preset & Extra `VICTORY_*` Overrides:** N/A - **Scene / Area & Session Length:** N/A ## Measurements & Verification - **Base FPS (without Frame Gen):** N/A (offline compiler unit tests) - **Frame-Gen FPS (if enabled):** N/A - **VRAM / RAM Usage:** N/A - **Crashes / DRED Resets:** N/A - **Automated Checks:** - [ ] `cargo clippy --workspace --all-targets --locked -- -D warnings` (requires `x86_64-pc-windows-msvc`; verified on GitHub Actions `windows-latest` CI) - [ ] `cargo clippy --workspace --all-targets --all-features --locked -- -D warnings` (requires `x86_64-pc-windows-msvc`; verified on GitHub Actions `windows-latest` CI) - [x] `cargo clippy --locked -p victory-shader --lib -- -D warnings` -> 0 warnings, exit 0 - [x] `cargo test --locked -p victory-shader -- --test-threads=1` -> 253 passed, 0 failed, 2 ignored (including `idempotent_saturate_and_minmax_calls_fold` and `every_pass_leaves_what_random_programs_store`) - [x] `cargo test --locked -p xtask` -> 8 passed, 0 failed - [x] `cargo xtask policy` -> `asset policy: 321 files ok`
Androtim commented 2026-12-02 09:03:55 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-02T09:03:55Z.


Automated triage for the maintainers (not an approval).

Verdict: needs-rebase · CI (check): passing

This PR no longer merges cleanly into main.

To change the verdict: Rebase onto current main and resolve conflicts; CI and triage will run again automatically.


Generated with Antigravity CLI (agy).

<!-- greyfinch-sync:Androtim/MiniVI:comment:7340321259 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/314#issuecomment-7340321259). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-02T09:03:55Z. --- <!-- minivi-triage v1 head=1bec9fd4de2e4b61b31645f6fb8d08c1175f18b4 verdict=needs-rebase ci=passing at=2026-12-03T12:02:41Z --> Automated triage for the maintainers (not an approval). **Verdict:** `needs-rebase` · **CI (`check`):** `passing` This PR no longer merges cleanly into `main`. **To change the verdict:** Rebase onto current `main` and resolve conflicts; CI and triage will run again automatically. --- _Generated with Antigravity CLI (`agy`)._
Androtim commented 2026-12-03 11:12:47 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-03T11:12:47Z.


Gate 3 Live Windows D3D12 Cold-Cache GPU Verification — PASS

Tested combined PR #311 + PR #314 on Windows 11 + NVIDIA RTX 4060 Ti 8 GB (1440p DLSS Quality, fresh cold shader cache):

  • Unit tests: 14/14 victory-shader::emit::bitcode::opt::tests passing (idempotent_saturate_and_minmax_calls_fold + differential suite).
  • Cold shader cache: Translated 1,049 shaders and built 199 host PSOs cleanly with 0 DXIL validation errors, 0 PSO compilation failures, 0 DRED, and 0 device-removed.
  • Visual & VRAM verification: Full in-world scene parity verified (DRAWS 9904, DROPPED 172, 0 being made, peak dedicated VRAM 5476 / 6613 MiB).

Note: Once PR #311 merges, PR #314 only has a trivial adjacent unit-test line conflict at the bottom of crates/victory-shader/src/emit/bitcode/opt.rs (zero production-code overlap). Ready to merge!

<!-- greyfinch-sync:Androtim/MiniVI:comment:7376372394 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/314#issuecomment-7376372394). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-03T11:12:47Z. --- ### Gate 3 Live Windows D3D12 Cold-Cache GPU Verification — PASS Tested combined PR #311 + PR #314 on Windows 11 + **NVIDIA RTX 4060 Ti 8 GB** (`1440p` DLSS Quality, fresh cold shader cache): - **Unit tests:** `14/14` `victory-shader::emit::bitcode::opt::tests` passing (`idempotent_saturate_and_minmax_calls_fold` + differential suite). - **Cold shader cache:** Translated **1,049 shaders** and built **199 host PSOs** cleanly with **0 DXIL validation errors**, **0 PSO compilation failures**, **0 DRED**, and **0 device-removed**. - **Visual & VRAM verification:** Full in-world scene parity verified (`DRAWS 9904`, `DROPPED 172`, `0 being made`, peak dedicated VRAM `5476 / 6613 MiB`). Note: Once PR #311 merges, PR #314 only has a trivial adjacent unit-test line conflict at the bottom of `crates/victory-shader/src/emit/bitcode/opt.rs` (zero production-code overlap). Ready to merge!
Androtim commented 2026-12-03 11:33:06 +00:00 (Migrated from github.com)

Imported from Androtim/MiniVI. Originally posted by Androtim on 2026-12-03T11:33:06Z.


PR #311 has merged (8780a119), leaving the expected adjacent unit-test conflict in crates/victory-shader/src/emit/bitcode/opt.rs. Gate 3 cold-cache GPU verification already passed — once rebased onto main with green CI, ready to auto-merge immediately.

<!-- greyfinch-sync:Androtim/MiniVI:comment:7376855507 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/pull/314#issuecomment-7376855507). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-03T11:33:06Z. --- PR `#311` has merged (`8780a119`), leaving the expected adjacent unit-test conflict in `crates/victory-shader/src/emit/bitcode/opt.rs`. Gate 3 cold-cache GPU verification already passed — once rebased onto `main` with green CI, ready to auto-merge immediately.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

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

Merge

Merge the changes and update on Forgejo.

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

git switch 'main'
git merge --no-ff 'upstream-pr/314'
git switch 'upstream-pr/314'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/314'
git switch 'upstream-pr/314'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/314'
git switch 'main'
git merge --squash 'upstream-pr/314'
git switch 'main'
git merge --ff-only 'upstream-pr/314'
git switch 'main'
git merge 'upstream-pr/314'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Androtim/MiniVI!314
No description provided.