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

Open
opened 2026-11-26 04:49:17 +00:00 by github-bot · 1 comment
Owner

@⁠hyperland956-lgtm on GitHub · 2026-11-24 14:15 UTC · original

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.

**@⁠hyperland956-lgtm** on GitHub · 2026-11-24 14:15 UTC · [original](https://github.com/xcl-dev/xcl/issues/1282) ## 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.*
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:49:17 +00:00
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 14:23 UTC · original

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).

**@⁠upipibi** on GitHub · 2026-11-24 14:23 UTC · [original](https://github.com/xcl-dev/xcl/issues/1282#issuecomment-7134301995) 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).
This discussion has been locked. Commenting is limited to contributors.
No description provided.