Shaders: exact rewrites of float maximum, minimum and median on any host #1538

Manually merged
github merged 1 commit from github-bot/gh-1538 into main 2026-11-29 08:20:41 +00:00 AGit
Owner

@⁠mizzied on GitHub · 2026-11-28 21:23 UTC · original
mizzied:up/minmax-exact → main · +583 −59 · 7 files · 1 commits · merged 2026-11-29 08:16 UTC

What it does. Two exact rewrites that leave every stored bit unchanged and write fewer instructions. Each is on unless its switch is 0, and both are in the translation fingerprint, so the shader cache goes cold once.

  • VICTORY_MINMAX_NONZERO (emit/cost.rs, lower/minmax.rs): a maximum or minimum with a constant whose magnitude bits are not all zero skips the two-zeros test and its select. That test is made on the bits, so it's false for any other operand and the select already picks FMax's own result. It's exact by construction on every host, and denormal or NaN constants count.
  • VICTORY_MED3_CLAMP: the median of a value and two constants becomes min(max(x, lo), hi) (a NaN value gives lo, as the general form does) where both constants are normal numbers or infinities. rdna::MED3 already writes this clamp on RDNA hosts only; this writes it on any host. Zero bounds are left to the general form, because a host's maximum of -0 and +0 is its own (WARP's depends on operand order), so the forms would differ for med3(0, x, 1) with x = -0.

max_min and med3 move from lower.rs to lower/minmax.rs (lower.rs shrinks by 59 lines).

How it was tested. RTX 5070, Windows 11. New tests in valu_tests/cost_checks.rs (with a test-only census::counting) translate each program with no rewrite, with each one alone and with both on. They run it on the card and on WARP over denormals, signed zeros, NaNs and infinities in every operand. Every store is bit-identical, and the programs the rewrites apply to write fewer instructions: max(x, 1.0) goes from 19 to 13, med3 with bounds -1 and 2 from 20 to 14, and a run of five from 60 to 53. Programs they must leave alone (a zero bound, a denormal bound, a NaN bound, two variables) come out the same. cargo test --workspace passes apart from repository_passes_policy, which already fails on main: lower.rs is over the 640 KiB file-size limit there (#1493 raises it). Clippy reports only the 16 findings already on main, none on lines this PR changes.

**@⁠mizzied** on GitHub · 2026-11-28 21:23 UTC · [original](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1538) `mizzied:up/minmax-exact` → `main` · +583 −59 · 7 files · 1 commits · merged 2026-11-29 08:16 UTC **What it does.** Two exact rewrites that leave every stored bit unchanged and write fewer instructions. Each is on unless its switch is `0`, and both are in the translation fingerprint, so the shader cache goes cold once. - **`VICTORY_MINMAX_NONZERO`** (`emit/cost.rs`, `lower/minmax.rs`): a maximum or minimum with a constant whose magnitude bits are not all zero skips the two-zeros test and its select. That test is made on the bits, so it's false for any other operand and the select already picks FMax's own result. It's exact by construction on every host, and denormal or NaN constants count. - **`VICTORY_MED3_CLAMP`**: the median of a value and two constants becomes `min(max(x, lo), hi)` (a NaN value gives `lo`, as the general form does) where both constants are normal numbers or infinities. `rdna::MED3` already writes this clamp on RDNA hosts only; this writes it on any host. Zero bounds are left to the general form, because a host's maximum of -0 and +0 is its own (WARP's depends on operand order), so the forms would differ for `med3(0, x, 1)` with `x = -0`. `max_min` and `med3` move from `lower.rs` to `lower/minmax.rs` (`lower.rs` shrinks by 59 lines). **How it was tested.** RTX 5070, Windows 11. New tests in `valu_tests/cost_checks.rs` (with a test-only `census::counting`) translate each program with no rewrite, with each one alone and with both on. They run it on the card and on WARP over denormals, signed zeros, NaNs and infinities in every operand. Every store is bit-identical, and the programs the rewrites apply to write fewer instructions: `max(x, 1.0)` goes from 19 to 13, med3 with bounds -1 and 2 from 20 to 14, and a run of five from 60 to 53. Programs they must leave alone (a zero bound, a denormal bound, a NaN bound, two variables) come out the same. `cargo test --workspace` passes apart from `repository_passes_policy`, which already fails on `main`: `lower.rs` is over the 640 KiB file-size limit there (#1493 raises it). Clippy reports only the 16 findings already on `main`, none on lines this PR changes.
Two rewrites that leave the stored bits unchanged and write fewer
instructions, each on unless its switch is 0 (both are in the translation
fingerprint, so the shader cache goes cold):

- VICTORY_MINMAX_NONZERO (emit/cost.rs, lower/minmax.rs): a maximum or
  minimum with a constant whose magnitude bits are not all zero skips the
  test for two zeros and its select. That test is made on the bits, so it is
  false for any other operand and the select picks FMax's own result: exact
  by construction on every host (a denormal or NaN constant counts).
- VICTORY_MED3_CLAMP: the median of a value and two constants is
  min(max(x, lo), hi) (a NaN value gives lo, as the general form does) where
  both constants are normal numbers or infinities. The median already takes
  the host's raw FMax/FMin, and rdna::MED3 writes the same clamp on RDNA
  hosts only; this is that clamp on any host. Bounds that are zero are left to
  the general form: a host's maximum of -0 and +0 is its own, and WARP's
  depends on the order of the operands, so the two forms differ there
  (med3(0, x, 1) with x = -0).

max_min and med3 move from lower.rs to lower/minmax.rs (lower.rs shrinks by
59 lines).

Tests (valu_tests/cost_checks.rs, with a test-only census::counting): each
program is translated with no rewrite, each alone and all on, and run on the
card and on WARP over denormals, signed zeros, NaNs and infinities in every
operand: every store is bit-identical, and the programs the rewrites apply
to write fewer instructions (max(x, 1.0) 19 to 13; med3 with bounds -1 and 2
20 to 14; a run of five 60 to 53). Programs they must leave alone (a zero
bound, a denormal bound, a NaN bound, two variables) come out the same.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-28 21:25:00 +00:00
github-bot changed title from [pr-performance][shaders][NVIDIA] Shaders: exact rewrites of float maximum, minimum and median on any host to Shaders: exact rewrites of float maximum, minimum and median on any host 2026-11-28 21:37:38 +00:00
github manually merged commit b8e4dd4e6e into main 2026-11-29 08:20:41 +00:00
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/trustno1-coder--VIctory-NVIDIA!1538
No description provided.