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 #1272

Open
opened 2026-11-24 14:15:07 +00:00 by hyperland956-lgtm · 1 comment
hyperland956-lgtm commented 2026-11-24 14:15:07 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by hyperland956-lgtm on 2026-11-24T14:15:07Z.


Summary

In crates/xcl/src/gpu/recomp/emit.rs, scalar and 3-operand float min/max instructions emit plain HLSL min() / max():

  • emit.rs:2987-2992: v_min_f16, v_max_f16, v_min3_f16, v_max3_f16
  • emit.rs:3362-3363, 3374-3375: v_min_f32, v_max_f32, v_min3_f32, v_max3_f32

On RDNA2 in IEEE mode, v_min_* and v_max_* strictly order -0.0 < +0.0:

  • v_min(+0.0, -0.0) == -0.0 and v_min(-0.0, +0.0) == -0.0
  • v_max(-0.0, +0.0) == +0.0 and v_max(+0.0, -0.0) == +0.0

Plain HLSL min(a, b) / max(a, b) lowers to DXIL FMin / FMax, which does not guarantee signed-zero ordering across D3D12 drivers (on NVIDIA and WARP, min(+0.0, -0.0) can return +0.0 and max(-0.0, +0.0) can return -0.0, depending on operand order). Note that XCL already handles -0.0 explicitly for float atomics (#38), and #679 / #719 address fma_legacy and NaN behavior in min/max, but signed-zero ordering for v_min_*/v_max_* is not yet covered.

Suggested fix

Add small helper functions (e.g., fmin_ieee(float a, float b) / fmax_ieee(float a, float b) and f16 equivalents) that preserve NaN-suppressing min/max while resolving the a == 0.0 && b == 0.0 case via sign-bit OR (for min: asfloat(asuint(a) | asuint(b))) and sign-bit AND (for max: asfloat(asuint(a) & asuint(b))), and use them for v_min_*, v_max_*, v_min3_*, and v_max3_*.

Synthetic GPU test

Run v_min_f32, v_max_f32, v_min_f16, v_max_f16, v_min3_f32, v_max3_f32 on both (+0.0, -0.0) and (-0.0, +0.0) and assert on raw bit patterns (0x80000000 / 0x8000 for min, 0x00000000 / 0x0000 for max) on NVIDIA and WARP.

AI use: source-level audit of xcl-main (crates/xcl/src/gpu/recomp/emit.rs) with Gemini; not built or run on Windows.

<!-- greyfinch-sync:ivall/xcl:issue:6583781792 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/1282). Originally posted by [hyperland956-lgtm](https://github.com/hyperland956-lgtm) on 2026-11-24T14:15:07Z. --- ## Summary In `crates/xcl/src/gpu/recomp/emit.rs`, scalar and 3-operand float min/max instructions emit plain HLSL `min()` / `max()`: - `emit.rs:2987-2992`: `v_min_f16`, `v_max_f16`, `v_min3_f16`, `v_max3_f16` - `emit.rs:3362-3363, 3374-3375`: `v_min_f32`, `v_max_f32`, `v_min3_f32`, `v_max3_f32` On RDNA2 in IEEE mode, `v_min_*` and `v_max_*` strictly order `-0.0 < +0.0`: - `v_min(+0.0, -0.0) == -0.0` and `v_min(-0.0, +0.0) == -0.0` - `v_max(-0.0, +0.0) == +0.0` and `v_max(+0.0, -0.0) == +0.0` Plain HLSL `min(a, b)` / `max(a, b)` lowers to DXIL `FMin` / `FMax`, which does not guarantee signed-zero ordering across D3D12 drivers (on NVIDIA and WARP, `min(+0.0, -0.0)` can return `+0.0` and `max(-0.0, +0.0)` can return `-0.0`, depending on operand order). Note that XCL already handles `-0.0` explicitly for float atomics (#38), and #679 / #719 address `fma_legacy` and NaN behavior in `min`/`max`, but signed-zero ordering for `v_min_*`/`v_max_*` is not yet covered. ## Suggested fix Add small helper functions (e.g., `fmin_ieee(float a, float b)` / `fmax_ieee(float a, float b)` and f16 equivalents) that preserve NaN-suppressing `min`/`max` while resolving the `a == 0.0 && b == 0.0` case via sign-bit OR (for min: `asfloat(asuint(a) | asuint(b))`) and sign-bit AND (for max: `asfloat(asuint(a) & asuint(b))`), and use them for `v_min_*`, `v_max_*`, `v_min3_*`, and `v_max3_*`. ## Synthetic GPU test Run `v_min_f32`, `v_max_f32`, `v_min_f16`, `v_max_f16`, `v_min3_f32`, `v_max3_f32` on both `(+0.0, -0.0)` and `(-0.0, +0.0)` and assert on raw bit patterns (`0x80000000` / `0x8000` for min, `0x00000000` / `0x0000` for max) on NVIDIA and WARP. *AI use: source-level audit of `xcl-main` (`crates/xcl/src/gpu/recomp/emit.rs`) with Gemini; not built or run on Windows.*
upipibi commented 2026-11-24 15:05:51 +00:00 (Migrated from github.com)
Author
Contributor

Imported from ivall/xcl. Originally posted by upipibi on 2026-11-24T14:23:41Z.


A measurement for this. On an RTX 3070 (driver 616.56, Windows 11), main's HLSL min/max already give the ISA's signed-zero results:

  • min(±0, ∓0) = -0 and max(±0, ∓0) = +0, in both operand orders;
  • for v_min/max_f32, v_min3/max3_f32, v_min/max_f16, v_pk_min/max_f16 and v_min/max_f64;
  • checked on the result bits, in a GPU test I have locally (gpu_min_max_order_minus_zero_below_plus_zero, wave32 and split wave64).

WARP skips that test because it has neither 32- nor 64-lane waves, so the WARP half of this issue is untested. On AMD, FMin/FMax are compiled to the native v_min/v_max, which order the zeros themselves.

So I would rather land the test than the helpers. It pins the behaviour, and fails on any driver that breaks it. Wrapping every min/max in bit operations would add HLSL per instruction for no change on these drivers, the opposite of what #1269 asks for. I'll open the test as its own PR.

AI-assisted (Claude Code).

<!-- greyfinch-sync:ivall/xcl:comment:7134301995 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/issues/1282#issuecomment-7134301995). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-24T14:23:41Z. --- A measurement for this. On an RTX 3070 (driver 616.56, Windows 11), `main`'s HLSL `min`/`max` already give the ISA's signed-zero results: - `min(±0, ∓0) = -0` and `max(±0, ∓0) = +0`, in both operand orders; - for `v_min/max_f32`, `v_min3/max3_f32`, `v_min/max_f16`, `v_pk_min/max_f16` and `v_min/max_f64`; - checked on the result bits, in a GPU test I have locally (`gpu_min_max_order_minus_zero_below_plus_zero`, wave32 and split wave64). WARP skips that test because it has neither 32- nor 64-lane waves, so the WARP half of this issue is untested. On AMD, `FMin`/`FMax` are compiled to the native `v_min`/`v_max`, which order the zeros themselves. So I would rather land the test than the helpers. It pins the behaviour, and fails on any driver that breaks it. Wrapping every min/max in bit operations would add HLSL per instruction for no change on these drivers, the opposite of what #1269 asks for. I'll open the test as its own PR. AI-assisted (Claude Code).
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
xcl-dev/xcl#1272
No description provided.