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

Merged
mizzied merged 1 commit from upstream-pr/1538 into main 2026-11-29 08:16:56 +00:00
mizzied commented 2026-11-28 21:23:17 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by mizzied on 2026-11-28T21:23:17Z.


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.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5544702801 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/1538). Originally posted by [mizzied](https://github.com/mizzied) on 2026-11-28T21:23:17Z. --- **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.
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
trustno1-coder/VIctory-NVIDIA!1538
No description provided.