Skip to content

Recompiler: v_min/v_max order -0.0 < +0.0 as RDNA2 does in IEEE mode - #1348

Closed
DavidM1911 wants to merge 1 commit into
xcl-dev:mainfrom
DavidM1911:fix-1282-minmax
Closed

DavidM1911 wants to merge 1 commit into
xcl-dev:mainfrom
DavidM1911:fix-1282-minmax

Conversation

@DavidM1911

@DavidM1911 DavidM1911 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

v_min_f32/v_max_f32/v_min_f16/v_max_f16 and the 3-operand forms emitted plain HLSL min()/max() → DXIL FMin/FMax, whose -0.0/+0.0 result depends on operand order on NVIDIA/WARP. RDNA2 IEEE mode orders -0.0 < +0.0 deterministically: v_min(+0,-0) = v_min(-0,+0) = -0, v_max mirrors to +0.

What is in it

  • fmin_ieee/fmax_ieee HLSL helpers: NaN-suppressing min/max as before, with the both-zero case settled on the sign bits (min: OR keeps -0; max: AND keeps +0). Used for all four f32 forms.
  • The f16 path converts through f32, and f16 -> f32 -> f16 loses the sign of zero entirely (measured: v_add_f16(-0,-0) already yields +0 on this pipeline), so min16z2/3/max16z2/3 settle the zero cases on the raw halves: -0 when a -0 operand is present and nothing is negative, +0 for max when a +0 is present and nothing is positive; NaNs and nonzero results defer to the float path. abs/neg modifiers are already bitwise via hb().
  • New GPU test gpu_min_max_f32_f16_order_negative_zero: both operand orders for all eight instructions, wave32 and wave64.

Note: #1301 adds a test-only version of the f32 coverage; this PR includes the f32 fix plus the f16 handling that test does not cover.

Fixes #1282

Tested

  • cargo test -p xcl --lib recomp: 190 passed, 0 failed, 18 ignored (RTX 3060 Ti, Windows 11, DXC 1.8.2502.11). New test passes on both wave sizes.

Regression run

main 930e8b4:

xcl-smoke v1
commit: 930e8b49c1c2ec683d22c724a398b39111cd0e56
gpu: NVIDIA GeForce RTX 3060 Ti
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1603 (PresentX 1603)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14549 MB

head 052af4d:

xcl-smoke v1
commit: 052af4d65c91796f19154ee6e2872b6d52a6e59f
gpu: NVIDIA GeForce RTX 3060 Ti
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1592 (PresentX 1593)
presents per 2 s (last 3): 60 39 60
device removals: 0
crashes: 0
peak commit: 14550 MB

Not tested

macOS / Linux (crate does not build there). f16 -0 through other arithmetic ops (e.g. v_add_f16) remains as before — out of scope here.

AI use

Devin (Cognition), frontier model. Implemented the fix and tests; ran the suite and smoke above.

v_min_f32/f16 and v_max_f32/f16 (and the 3-operand forms) emitted plain
HLSL min()/max(), which lowers to DXIL FMin/FMax: on NVIDIA and WARP the
signed-zero result depends on operand order, while RDNA2 IEEE mode
deterministically returns -0 for min and +0 for max when the operands
are +0/-0 in either order.

f32 goes through fmin_ieee/fmax_ieee: NaN-suppressing min/max as before,
with the both-zero case settled on the sign bits (min: OR keeps -0,
max: AND keeps +0).

The f16 path converts through f32, and f16 -> f32 -> f16 loses the sign
of zero entirely (v_add_f16(-0,-0) already yields +0), so the zero cases
are settled on the raw halves instead: min16z*/max16z* return -0 when a
-0 operand is present and nothing is negative (max mirrors it, +0
winning over -0), and defer to the float result otherwise.

New GPU test gpu_min_max_f32_f16_order_negative_zero covers both
operand orders for all eight instructions on wave32 and wave64.

Fixes xcl-dev#1282
@futurepro62

futurepro62 commented Nov 24, 2026 •

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head 052af4d (on its base 930e8b4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 752 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: gpu_min_max_f32_f16_order_negative_zero.
  • Same head on the AMD Radeon iGPU of the 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 752 passed, 0 failed, 41 ignored, no skips.
  • I also ran this PR's new test against main's emitter (main 930e8b4 with only tests.rs from this PR): gpu_min_max_f32_f16_order_negative_zero passes there too, 2 of 2 on each GPU. So on these two drivers (RTX 5070 Ti, Radeon RDNA2 iGPU) plain min/max already gave the RDNA2 -0 / +0 answers for the operand orders in the test, and the test does not show the bug here; it would on a driver that gets the order wrong (WARP, per the description). The same goes for Tests: float min / max order -0 below +0 on the GPU #1301's test, which passed on main on both GPUs here.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@lemosss

lemosss commented Nov 24, 2026

Copy link
Copy Markdown

Adding to the report above with what it does not cover: an Ada data point, WARP, and the f64 forms.

Tested (head 052af4d, XCL_TRANSLATED_CACHE=0 for every run)

  • NVIDIA GeForce RTX 4060 8 GB (Ada, 32-lane waves only), driver 610.88 (32.0.16.1088); Intel Core i7-10700K; Windows 11 Pro 26H2, build 26300.9974; Rust 1.100.0 MSVC; DXC 1.9.2609.5; debug
  • gpu_min_max_f32_f16_order_negative_zero passes on the head, and also with emit.rs put back to 930e8b4 (the new test kept). Same result as on the RTX 5070 Ti and the RDNA2 iGPU above: on Ada too, plain min / max already give the RDNA2 answers for the cases in the test.
  • WARP (XCL_ADAPTER=warp): the test prints SKIPPED … no wave64 and runs nothing, with or without the fix. So the WARP failure in the description cannot be shown with this test as it is.
  • v_min_f64 / v_max_f64, which this PR leaves on plain min / max: a throwaway probe on the same build gives min(+0, −0) = min(−0, +0) = 0x8000000000000000 and max(+0, −0) = max(−0, +0) = 0x0000000000000000 on this GPU, i.e. the RDNA2 order already. If the f32 / f16 forms are guarded for drivers that differ, the f64 pair would need the same.

Not tested: AMD / wave64, a release build, a retail title.

Run and written with Claude Code (Claude Opus 5.5).

@Darkkandadr

Copy link
Copy Markdown

An Ampere data point to add to the two reports above. The RTX 3060 has the same architecture as the author's RTX 3060 Ti, but this run used a different driver.

Tested (head 052af4d, on its base 930e8b4)

  • NVIDIA GeForce RTX 3060 12 GB (Ampere, 32-lane waves only), driver 32.0.16.2101; Intel Core i3-12100F; Windows 11 Pro build 26300; Rust 1.96.0 MSVC, debug; DXC 1.9.2609.5; D3D12 debug layer installed.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 751 passed, 1 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The one failure is e2e::test_title_draws_with_a_root_signature_under_the_debug_layer, and it is unrelated to this PR. It also fails on main in my deep checkout, because the overlay path goes past MAX_PATH. e2e: the debug-layer test keeps its files in the target's tmp folder #1015 fixes it (details there).
  • gpu_min_max_f32_f16_order_negative_zero passes on the head. It also passes with only this PR's tests.rs on main 930e8b4. It ran on the GPU there and was not skipped: DXC loaded, and there was no skip line with --nocapture. So, as on the RTX 5070 Ti, RTX 4060 and RDNA2 iGPU above, plain min / max on Ampere with this driver already give the RDNA2 -0 / +0 answers for the cases in the test. The test does not fail without the fix on any GPU reported so far.

Not tested

  • A release build, AMD / wave64, WARP, a retail title.

Run and written with Claude Code (Claude Opus 5.5).

@pablogonz12

pablogonz12 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

An RDNA4 / Linux (Wine + vkd3d-proton) data point for head 052af4d on its base 930e8b4. It adds the wave64 case and an AMD driver where min / max come from RADV.

Tested

  • AMD Radeon RX 9070 XT (RADV GFX1201, Mesa 26.3.1, wave32 and wave64), Ryzen 9 9900X, CachyOS (Linux 7.3), Wine (GE-Proton11-14) with vkd3d-proton v3.1 plus the Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257 patch, DXC 1.9.2609.5, Rust stable, release, x86_64-pc-windows-gnu (llvm-mingw).
  • cargo test -p xcl --lib --release --target x86_64-pc-windows-gnu, test binary run under Wine with --test-threads=4 --skip jumps_to_outer_loop_heads_are_structured: 702 passed, 5 failed, 21 ignored, no SKIPPED lines. Not the whole workspace: only the xcl lib tests.
  • Failures, none near this change: gpu_f64_roots, gpu_f64_math_random, gpu_nonfused_mad_and_mac_prevent_fma_contraction, xma2_source_voice_decodes_and_manages_lifetime, pe::tests::parses_own_test_binary_tls.
  • gpu_min_max_f32_f16_order_negative_zero and gpu_atomic_fmin_fmax_negative_zero pass on both wave sizes.
  • Negative control: 930e8b4 with only crates/xcl/src/gpu/recomp/tests.rs taken from this PR gives the same 702 / 5 / 21, and the new test passes. So here, as in the reports above, plain min / max already order -0 below +0 on the GPU, and this test cannot tell main from the PR on this adapter.
  • The same check for v_med3_f32 (zeros of both signs in all operand orders, plus the clamp med3(-0, 0, 1)) also passes on current main (b7aa9f3, which has the plain chain), so I have no failing case for v_med3 here either. The PR leaves med3f as it is.

Not tested

AI use

  • Claude Code (Claude Sonnet 5.5) ran the builds and tests above on my machine and wrote this report.

amirshaker000 pushed a commit to amirshaker000/PAG6Port that referenced this pull request Nov 24, 2026
The ISA defines v_med3_f32/v_med3_f16 over v_min_f32/v_max_f32, which order
-0 below +0 (#448 settled those), so of three zeros the median is the sign
two of them share; DXIL's plain FMin/FMax chain gives either zero. The zero
the chain leaves now takes that majority's sign (11 instructions). A NaN
still gives the minimum of the three.

Noticed while porting the idea of xcl-dev/xcl#1348 (DavidM1911).

Tests: hardware execution tests in valu_tests for both operand orders and
the halves.

Written with an AI assistant.
@throny

throny commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Two contributors checked independently and found that #1577 solves the same problem (for #1282) in a different way: #1607, group D3. You may want to compare them with the other author and agree on one, or say in the PR why both are needed.

This is a hint, not a request from the maintainers; nobody from the project has adopted #1607 so far.

AI use: Claude Code (Claude Opus 5.5), posted by a script from the #1607 table.

@DavidM1911

Copy link
Copy Markdown
Contributor Author

Fair point — comparing with #1577, it covers the same ground (v_min/v_max/v_min3/v_max3 for f16/f32) plus v_pk_min/max_f16 and v_med3_*, so it's the more complete fix for #1282. This PR is redundant; closing it in favour of #1577. Thanks for the hint.

@DavidM1911 DavidM1911 closed this Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recompiler: v_min_f32 / v_max_f32 / v_min_f16 / v_max_f16 (and 3-op forms) emit plain HLSL min/max, violating RDNA2 -0.0 < +0.0 ordering on NVIDIA/WARP

6 participants