perf: v_max_f32/v_min_f32 are the host's FMax/FMin alone on NVIDIA (2.2x on a 4060 Laptop) #587

Open
KainanP wants to merge 7 commits from upstream-pr/587 into main
KainanP commented 2026-11-28 16:37:24 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-28T16:37:24Z.


Stacked on #586 (perm), #585 (rndne), #581 and #570. It uses #581's bench and #585's bulk harness. Only the last commit is new. Full victory-shader suite and pytest results: see #585 (run on this branch).

What

On hosts other than AMD, v_max_f32/v_min_f32 and the like (max3/min3, and med3 between constants since #581) write a fix-up after FMax/FMin: an or, a mask, a compare, a bitwise op and a select. That fix-up orders -0 below +0, which DXIL's FMax/FMin do not promise.

I measured each host's own FMax/FMin on two zeros:

Host max(+0,-0) max(-0,+0) min(+0,-0) min(-0,+0)
RTX 4060 Laptop, driver 610.47 +0 +0 -0 -0
Intel UHD (Xe-LP), 31.0.101.5445 +0 +0 -0 -0
WARP -0 +0 -0 +0 (the second operand)

NVIDIA's FMax/FMin already are RDNA 2's maximum and minimum. Over all the inputs below, plain FMax/FMin give the same bits as the fix-up, denormals and NaN payloads included. So on NVIDIA the translator now writes them alone.

How it works:

  • victory-gpu sets emit::set_host_zero_order from the adapter's PCI vendor (orders_zeros: 0x10DE).
  • On such a host host_rdna() gives rdna::MAX_MIN, the bit AMD hosts already use, so the shader cache keys on it.
  • VICTORY_RDNA_FLOAT=0 switches it off, as on AMD.

Intel UHD orders the zeros too, but plain FMax keeps a denormal that the fix-up form flushes (v_med3_f32 1e-45, 0, 1.0: 0x00000001 against 0x00000000). Intel therefore keeps the fix-up, as does WARP. The offline victory translate and replay paths do not call it, so they keep writing the fix-up, which is valid everywhere.

I looked for a universal cheaper exact form first. None beat the current 5 instructions with the same bits on all three adapters:

  • a float compare for the tie: FTZ hosts call denormals equal;
  • FMax(a,b) & FMax(b,a): WARP's two-NaN results break it;
  • sign tricks: they break on a positive NaN with a negative other operand.

TRANSLATOR_VERSION 24.

Bench (RTX 4060 Laptop, 8 dependent chains x 256 rounds, median of 3 alternating rounds; the bench now translates as the runtime does for the host)

Instruction (+ add) #586 (before) This PR HLSL Gain
v_max_f32 registers 0.552 ms 0.255 ms 0.176-0.199 ms 2.16x
v_min_f32 registers 0.561 ms 0.255 ms 0.199 ms 2.2x
v_med3_f32 (x, 0, 1) 0.421 ms 0.209 ms 0.181 ms 2.0x
v_max_f32 (x, 0) 0.421 ms 0.255 ms 0.199 ms 1.65x
v_max_f32 (x, 0.5), v_min_f32 (x, 1.0), med3 (x,-1,1), med3 (x,a,b) unchanged unchanged 1.0x

Tests

  • max_min_tests::max_min_on_a_zero_ordering_host_are_one_operation (shape): on a zero-ordering host, a max/min of registers has no select and is ≥ 5 instructions shorter than with the fix-up. orders_zeros holds for NVIDIA only. It fails before (no such host setting: TEST_ZERO_ORDER/orders_zeros do not exist, and the old lowering writes the fix-up on every non-AMD host).

  • max_min_tests::max_min_as_written_for_each_host_match_rdna2 covers v_max_f32, v_min_f32, v_max3_f32, v_min3_f32 and v_med3_f32 (x, 0, 1) on 135,972 lanes:

    • every pair of edge values (±0, denormals, quiet and signalling NaNs of both signs, infinities, around 1 and 2^23);
    • 2^17 random triples.

    For each host, the form the runtime writes there gives the fix-up's bits on every lane, and RDNA 2's result wherever no denormal is involved. It passes on the RTX 4060 Laptop (plain FMax), WARP and Intel UHD (fix-up).

  • max_min_tests::max_min_match_the_interpreter_away_from_zeros_and_nans: the reference interpreter agrees on 114,779 lanes per form. It is left out where it is not RDNA 2: it uses Python's max/min, which keep the first of two equal zeros and order NaNs by comparison.

AI use: written with Claude Code (Claude Opus 5.5): the host measurement, lowering, tests, benches on my machine. The vendor gate rests on our RTX 4060 Laptop (Ada) only; other NVIDIA generations are not measured. VICTORY_RDNA_FLOAT=0 turns it off.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5542352389 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/587). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-28T16:37:24Z. --- **Stacked on #586** (perm), #585 (rndne), #581 and #570. It uses #581's bench and #585's bulk harness. Only the last commit is new. Full `victory-shader` suite and pytest results: see #585 (run on this branch). ## What On hosts other than AMD, `v_max_f32`/`v_min_f32` and the like (`max3`/`min3`, and `med3` between constants since #581) write a fix-up after FMax/FMin: an or, a mask, a compare, a bitwise op and a select. That fix-up orders -0 below +0, which DXIL's FMax/FMin do not promise. I measured each host's own FMax/FMin on two zeros: | Host | max(+0,-0) | max(-0,+0) | min(+0,-0) | min(-0,+0) | |---|---|---|---|---| | RTX 4060 Laptop, driver 610.47 | +0 | +0 | -0 | -0 | | Intel UHD (Xe-LP), 31.0.101.5445 | +0 | +0 | -0 | -0 | | WARP | -0 | +0 | -0 | +0 (the second operand) | NVIDIA's FMax/FMin already are RDNA 2's maximum and minimum. Over all the inputs below, plain FMax/FMin give **the same bits as the fix-up**, denormals and NaN payloads included. So on NVIDIA the translator now writes them alone. How it works: - `victory-gpu` sets `emit::set_host_zero_order` from the adapter's PCI vendor (`orders_zeros`: 0x10DE). - On such a host `host_rdna()` gives `rdna::MAX_MIN`, the bit AMD hosts already use, so the shader cache keys on it. - `VICTORY_RDNA_FLOAT=0` switches it off, as on AMD. Intel UHD orders the zeros too, but plain FMax keeps a denormal that the fix-up form flushes (`v_med3_f32 1e-45, 0, 1.0`: `0x00000001` against `0x00000000`). Intel therefore keeps the fix-up, as does WARP. The offline `victory translate` and replay paths do not call it, so they keep writing the fix-up, which is valid everywhere. I looked for a universal cheaper exact form first. None beat the current 5 instructions with the same bits on all three adapters: - a float compare for the tie: FTZ hosts call denormals equal; - `FMax(a,b) & FMax(b,a)`: WARP's two-NaN results break it; - sign tricks: they break on a positive NaN with a negative other operand. `TRANSLATOR_VERSION` 24. ## Bench (RTX 4060 Laptop, 8 dependent chains x 256 rounds, median of 3 alternating rounds; the bench now translates as the runtime does for the host) | Instruction (+ add) | #586 (before) | This PR | HLSL | Gain | |---|---|---|---|---| | `v_max_f32` registers | 0.552 ms | 0.255 ms | 0.176-0.199 ms | **2.16x** | | `v_min_f32` registers | 0.561 ms | 0.255 ms | 0.199 ms | **2.2x** | | `v_med3_f32 (x, 0, 1)` | 0.421 ms | 0.209 ms | 0.181 ms | **2.0x** | | `v_max_f32 (x, 0)` | 0.421 ms | 0.255 ms | 0.199 ms | **1.65x** | | `v_max_f32 (x, 0.5)`, `v_min_f32 (x, 1.0)`, `med3 (x,-1,1)`, `med3 (x,a,b)` | unchanged | unchanged | | 1.0x | ## Tests - `max_min_tests::max_min_on_a_zero_ordering_host_are_one_operation` (shape): on a zero-ordering host, a max/min of registers has no select and is ≥ 5 instructions shorter than with the fix-up. `orders_zeros` holds for NVIDIA only. **It fails before** (no such host setting: `TEST_ZERO_ORDER`/`orders_zeros` do not exist, and the old lowering writes the fix-up on every non-AMD host). - `max_min_tests::max_min_as_written_for_each_host_match_rdna2` covers `v_max_f32`, `v_min_f32`, `v_max3_f32`, `v_min3_f32` and `v_med3_f32 (x, 0, 1)` on 135,972 lanes: - every pair of edge values (±0, denormals, quiet and signalling NaNs of both signs, infinities, around 1 and 2^23); - 2^17 random triples. For each host, the form the runtime writes there gives the fix-up's bits on every lane, and RDNA 2's result wherever no denormal is involved. It passes on the RTX 4060 Laptop (plain FMax), WARP and Intel UHD (fix-up). - `max_min_tests::max_min_match_the_interpreter_away_from_zeros_and_nans`: the reference interpreter agrees on 114,779 lanes per form. It is left out where it is not RDNA 2: it uses Python's `max`/`min`, which keep the first of two equal zeros and order NaNs by comparison. _AI use: written with Claude Code (Claude Opus 5.5): the host measurement, lowering, tests, benches on my machine. The vendor gate rests on our RTX 4060 Laptop (Ada) only; other NVIDIA generations are not measured. `VICTORY_RDNA_FLOAT=0` turns it off._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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/587:upstream-pr/587'
git switch 'upstream-pr/587'

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/587'
git switch 'upstream-pr/587'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/587'
git switch 'upstream-pr/587'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/587'
git switch 'main'
git merge --squash 'upstream-pr/587'
git switch 'main'
git merge --ff-only 'upstream-pr/587'
git switch 'main'
git merge 'upstream-pr/587'
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
LooperSalty/AnyXboxX!587
No description provided.