perf(shader): fold idempotent Saturate, FMax, and FMin calls in bitcode::opt #312

Open
opened 2026-12-02 08:59:02 +00:00 by RUIZ-Enrique-25002136 · 1 comment
RUIZ-Enrique-25002136 commented 2026-12-02 08:59:02 +00:00 (Migrated from github.com)
Contributor

Imported from Androtim/MiniVI. Originally posted by RUIZ-Enrique-25002136 on 2026-12-02T08:59:02Z.


Summary

crates/victory-shader/src/emit/bitcode/opt.rs::Module::simplify currently leaves Op::Call unhandled (_ => None), missing bit-exact algebraic simplifications on DXIL float intrinsics generated repeatedly by lowered v_med3_f32, v_max_f32, v_min_f32, and clamp/saturate modifiers:

  1. dx.op.unary.f32(7, ...) (Saturate):
    • Saturate(Saturate(x)) -> Saturate(x)
    • Bit-exactness: DXIL Saturate clamps to [0.0, 1.0] and maps NaN -> +0.0 (or passes/normalizes identically on re-application); any output of Saturate(x) is already in [+0.0, 1.0], so applying Saturate a second time is bit-for-bit identical for all inputs (including -0.0 and NaN).
  2. dx.op.binary.f32(35 | 36, ...) (FMax / FMin):
    • Self-idempotence: FMax(x, x) -> x and FMin(x, x) -> x. In DXIL, FMax/FMin return the non-NaN operand if one is NaN, and return NaN only if both are NaN — so for (x, x) the result is always x (preserving -0.0 and NaN payload).
    • Nested same-op absorption: FMax(FMax(x, y), z) -> FMax(x, y) (and symmetric FMax(z, FMax(x, y))) whenever z == x || z == y; likewise for FMin.
    • Bit-exactness:
      • If x and y are both finite/infinite, FMax(FMax(x, y), x) = max(max(x, y), x) = max(x, y).
      • If x is NaN and y is non-NaN: FMax(x, y) = y, then FMax(y, x) = y (and for z == y: FMax(y, y) = y).
      • If both x and y are NaN: FMax(x, y) is NaN (x or y), and FMax(NaN, NaN) is NaN.
  • Part of #276 (victory-shader GPU Shader Time clamp/f32 folds backlog).
<!-- greyfinch-sync:Androtim/MiniVI:issue:6740910603 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/issues/312). Originally posted by [RUIZ-Enrique-25002136](https://github.com/RUIZ-Enrique-25002136) on 2026-12-02T08:59:02Z. --- ## Summary `crates/victory-shader/src/emit/bitcode/opt.rs::Module::simplify` currently leaves `Op::Call` unhandled (`_ => None`), missing bit-exact algebraic simplifications on DXIL float intrinsics generated repeatedly by lowered `v_med3_f32`, `v_max_f32`, `v_min_f32`, and clamp/saturate modifiers: 1. **`dx.op.unary.f32(7, ...)` (`Saturate`):** - `Saturate(Saturate(x)) -> Saturate(x)` - **Bit-exactness:** DXIL `Saturate` clamps to `[0.0, 1.0]` and maps `NaN -> +0.0` (or passes/normalizes identically on re-application); any output of `Saturate(x)` is already in `[+0.0, 1.0]`, so applying `Saturate` a second time is bit-for-bit identical for all inputs (including `-0.0` and `NaN`). 2. **`dx.op.binary.f32(35 | 36, ...)` (`FMax` / `FMin`):** - **Self-idempotence:** `FMax(x, x) -> x` and `FMin(x, x) -> x`. In DXIL, `FMax`/`FMin` return the non-NaN operand if one is NaN, and return `NaN` only if *both* are NaN — so for `(x, x)` the result is always `x` (preserving `-0.0` and `NaN` payload). - **Nested same-op absorption:** `FMax(FMax(x, y), z) -> FMax(x, y)` (and symmetric `FMax(z, FMax(x, y))`) whenever `z == x || z == y`; likewise for `FMin`. - **Bit-exactness:** - If `x` and `y` are both finite/infinite, `FMax(FMax(x, y), x) = max(max(x, y), x) = max(x, y)`. - If `x` is `NaN` and `y` is non-NaN: `FMax(x, y) = y`, then `FMax(y, x) = y` (and for `z == y`: `FMax(y, y) = y`). - If both `x` and `y` are `NaN`: `FMax(x, y)` is `NaN` (`x` or `y`), and `FMax(NaN, NaN)` is `NaN`. ## Related - Part of #276 (`victory-shader` GPU Shader Time `clamp`/`f32` folds backlog).
Androtim commented 2026-12-03 10:33:46 +00:00 (Migrated from github.com)
Author
Contributor

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


Thanks for the clear DXIL bit-exactness breakdown and for opening PR #314 (as well as PR #311)!

Both PRs have green CI (check passing) and solid static analysis — we have them queued for our live Windows D3D12 cold-shader-cache GPU verification gate before merging. Appreciate the clean contributions!

<!-- greyfinch-sync:Androtim/MiniVI:comment:7375417739 --> Imported from [Androtim/MiniVI](https://github.com/Androtim/MiniVI/issues/312#issuecomment-7375417739). Originally posted by [Androtim](https://github.com/Androtim) on 2026-12-03T10:33:46Z. --- Thanks for the clear DXIL bit-exactness breakdown and for opening PR #314 (as well as PR #311)! Both PRs have green CI (`check` passing) and solid static analysis — we have them queued for our live Windows D3D12 cold-shader-cache GPU verification gate before merging. Appreciate the clean contributions!
Sign in to join this conversation.
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#312
No description provided.