rdna2spirv: float min and max order -0 below +0 on the GPU too (SHADER-MINMAX-SIGNED-ZERO) #1027

Closed
github-bot wants to merge 1 commit from github-bot/gh-1027 into main AGit
Owner

@⁠oscarlosu-lab on GitHub · 2026-11-24 17:02 UTC · original
oscarlosu-lab:pr/minmax-signed-zero → main · +118 −15 · 7 files · 1 commits · closed without merging

Claimed in the coordination issue as: SHADER-MINMAX-SIGNED-ZERO

Draft: the fix is not demonstrated on the GPU I have. On an NVIDIA RTX 3060 (Windows 11, driver 621.35) the new fixture passes both before and after the change, native and with LEONIDA_W64_EMULATE=1: that driver already returns +0 for NMax(-0, +0). I'm asking for a run on AMD (RADV and the proprietary driver) and Intel before this leaves draft; if none of them returns the first zero either, this can be closed and only the fixture kept.

Why

tools/rdna2spirv.py lowers v_max_f32/v_min_f32 and their f16, packed, three-operand and med3 forms to GLSL.std.450 NMax/NMin. GLSL.std.450 defines those as "y if x < y, otherwise x" (the other operand for a NaN). -0 and +0 compare equal, so on a driver that follows that literally, with two zeros of opposite sign the result is the first operand: max(-0, +0) is -0 and min(+0, -0) is +0. The modules declare SignedZeroInfNanPreserve 32, so the sign is meant to survive. RDNA2 gives +0 for max and -0 for min in either order (docs/design.md, Float min/max row: "-0 < +0", measured on gfx1030). A -0 that should be +0 turns, for example, a following v_rsq's +inf into -inf.

The CPU interpreter in tests/semantic already orders -0 below +0 for NMax/NMin, so no CPU test could see the difference. Marvisak/VIctory (GPL-3.0) commit bf84134b2 found and fixed the same thing in its DXIL lowering (DXIL's FMax/FMin are a compare and select there).

Change

  • Gen.glsl_minmax emits NMax/NMin and, when both operands are zeros (magnitude bits 0), takes the result from their sign bits: AND for max, OR for min. Every f32, f16, packed, min3/max3 and med3 lowering goes through it. med3 skips it when at most one operand can be a zero (a constant that is not one), so an all-constant med3 still emits no select (test_v_med3_f32_all_constants_needs_no_select). NaN handling is unchanged. Cost: 8 instructions per min/max.
  • runtime/test/shaders/ps_fminmax_signed_zero.asm: max and min of -0 and +0 in both orders, read from memory; in check.py and scripts/run_gpu_tests.py, with its translator fixture.
  • tests/semantic/test_float_nan_fract.py: the expected words, and the sign-bit select after each min/max.
  • docs/design.md: the Float min/max row says how the order is kept.

Tested

  • scripts/run_semantic_tests.py tests/semantic: passes except test_image_cube_store_glc, which cannot start llvm-mc on this host (the Windows path splitting claimed as TEST-CUBE-WINPATH), unrelated.
  • regen_fixtures.py --check, check_no_game_data.py, tests/test_docs_links.py: pass.
  • GPU: NVIDIA RTX 3060, Windows 11, driver 621.35: run_gpu_tests.py --filter ps_fminmax_signed_zero passes before and after (see above).

AI-assisted (Claude Code). No game files.

**@⁠oscarlosu-lab** on GitHub · 2026-11-24 17:02 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1027) `oscarlosu-lab:pr/minmax-signed-zero` → `main` · +118 −15 · 7 files · 1 commits · closed without merging Claimed in the coordination issue as: SHADER-MINMAX-SIGNED-ZERO **Draft: the fix is not demonstrated on the GPU I have.** On an NVIDIA RTX 3060 (Windows 11, driver 621.35) the new fixture passes both before and after the change, native and with `LEONIDA_W64_EMULATE=1`: that driver already returns +0 for `NMax(-0, +0)`. I'm asking for a run on AMD (RADV and the proprietary driver) and Intel before this leaves draft; if none of them returns the first zero either, this can be closed and only the fixture kept. ### Why `tools/rdna2spirv.py` lowers `v_max_f32`/`v_min_f32` and their f16, packed, three-operand and med3 forms to GLSL.std.450 `NMax`/`NMin`. GLSL.std.450 defines those as "y if x < y, otherwise x" (the other operand for a NaN). -0 and +0 compare equal, so on a driver that follows that literally, with two zeros of opposite sign the result is the first operand: `max(-0, +0)` is -0 and `min(+0, -0)` is +0. The modules declare `SignedZeroInfNanPreserve 32`, so the sign is meant to survive. RDNA2 gives +0 for max and -0 for min in either order (`docs/design.md`, Float min/max row: "-0 < +0", measured on gfx1030). A -0 that should be +0 turns, for example, a following `v_rsq`'s +inf into -inf. The CPU interpreter in `tests/semantic` already orders -0 below +0 for `NMax`/`NMin`, so no CPU test could see the difference. Marvisak/VIctory (GPL-3.0) commit `bf84134b2` found and fixed the same thing in its DXIL lowering (DXIL's FMax/FMin are a compare and select there). ### Change - `Gen.glsl_minmax` emits `NMax`/`NMin` and, when both operands are zeros (magnitude bits 0), takes the result from their sign bits: AND for max, OR for min. Every f32, f16, packed, min3/max3 and med3 lowering goes through it. med3 skips it when at most one operand can be a zero (a constant that is not one), so an all-constant med3 still emits no select (`test_v_med3_f32_all_constants_needs_no_select`). NaN handling is unchanged. Cost: 8 instructions per min/max. - `runtime/test/shaders/ps_fminmax_signed_zero.asm`: max and min of -0 and +0 in both orders, read from memory; in `check.py` and `scripts/run_gpu_tests.py`, with its translator fixture. - `tests/semantic/test_float_nan_fract.py`: the expected words, and the sign-bit select after each min/max. - `docs/design.md`: the Float min/max row says how the order is kept. ### Tested - `scripts/run_semantic_tests.py tests/semantic`: passes except `test_image_cube_store_glc`, which cannot start `llvm-mc` on this host (the Windows path splitting claimed as TEST-CUBE-WINPATH), unrelated. - `regen_fixtures.py --check`, `check_no_game_data.py`, `tests/test_docs_links.py`: pass. - GPU: NVIDIA RTX 3060, Windows 11, driver 621.35: `run_gpu_tests.py --filter ps_fminmax_signed_zero` passes before and after (see above). AI-assisted (Claude Code). No game files.
v_max_f32/v_min_f32 and their f16, packed, three-operand and med3 forms were
lowered to GLSL.std.450 NMax/NMin, which are "y if x < y, otherwise x": -0
and +0 compare equal, so with two zeros of opposite sign a conforming driver
returns the first operand and max(-0, +0) is -0. RDNA2 orders -0 below +0 in
either order (docs/design.md, Float min/max row, measured on gfx1030). The
CPU interpreter in tests/semantic already orders -0 below +0 for NMax/NMin,
so no CPU test could see the difference.

Gen.glsl_minmax emits NMax/NMin and, when both operands are zeros, takes the
result from their sign bits: AND for max, OR for min. Every f32, f16, packed,
min3/max3 and med3 lowering goes through it; med3 skips it when at most one
operand can be a zero (a constant that is not one), so all-constant med3
still has no select. NaN handling is unchanged.

New GPU fixture runtime/test/shaders/ps_fminmax_signed_zero.asm (max and min
of -0 and +0 in both orders, from memory) in check.py and run_gpu_tests.py,
its translator fixture, and two tests in test_float_nan_fract.py (the words,
and the sign-bit select after each min/max). Marvisak/VIctory (GPL-3.0)
commit bf84134b2 fixed the same lowering for DXIL's FMax/FMin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:34:10 +00:00
Author
Owner

@⁠syntaxixr on GitHub · 2026-11-24 18:18 UTC · original

The AMD proprietary-driver run you asked for, on a discrete RDNA2 card:

AMD Radeon RX 6600 (RDNA2, Navi 23 / GFX1032), AMD Windows driver 26.6.4 (32.0.21043.19003), Vulkan 1.4.315, Windows 10 22H2 19045, llvm-mc 23.1.6 (conda-forge), SPIRV-Tools v2026.4. vk_harness was built from main 3aa97542 with llvm-mingw 20261117, and every run used this PR's fixture, check.py and run_gpu_tests.py; only tools/rdna2spirv.py changed between runs.

translator native LEONIDA_W64_EMULATE=1
this PR (054f640e) PASS, 4096/4096 exact PASS, 4096/4096 exact
its parent (1c93c869) PASS, 4096/4096 exact n/a
main 3aa97542 PASS, 4096/4096 exact PASS, 4096/4096 exact

So AMD's Windows compiler on RDNA2 also returns +0 for NMax(-0, +0) and -0 for NMin(+0, -0) without the sign-bit select, the same as your RTX 3060. Together with the NVIDIA result, neither proprietary driver shows the bug. That leaves RADV and Intel as the remaining question before deciding whether only the fixture is kept. I did not run RADV or Intel.

Command: python -I scripts/run_gpu_tests.py --build <vulkan build> --filter fminmax. No game files. AI-assisted.

**@⁠syntaxixr** on GitHub · 2026-11-24 18:18 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1027#issuecomment-7140447816) The AMD proprietary-driver run you asked for, on a discrete RDNA2 card: **AMD Radeon RX 6600** (RDNA2, Navi 23 / GFX1032), AMD Windows driver 26.6.4 (32.0.21043.19003), Vulkan 1.4.315, Windows 10 22H2 19045, llvm-mc 23.1.6 (conda-forge), SPIRV-Tools v2026.4. `vk_harness` was built from `main` 3aa97542 with llvm-mingw 20261117, and every run used this PR's fixture, `check.py` and `run_gpu_tests.py`; only `tools/rdna2spirv.py` changed between runs. | translator | native | `LEONIDA_W64_EMULATE=1` | |---|---|---| | this PR (`054f640e`) | PASS, 4096/4096 exact | PASS, 4096/4096 exact | | its parent (`1c93c869`) | PASS, 4096/4096 exact | n/a | | `main` 3aa97542 | PASS, 4096/4096 exact | PASS, 4096/4096 exact | So AMD's Windows compiler on RDNA2 also returns +0 for `NMax(-0, +0)` and -0 for `NMin(+0, -0)` without the sign-bit select, the same as your RTX 3060. Together with the NVIDIA result, neither proprietary driver shows the bug. That leaves RADV and Intel as the remaining question before deciding whether only the fixture is kept. I did not run RADV or Intel. Command: `python -I scripts/run_gpu_tests.py --build <vulkan build> --filter fminmax`. No game files. AI-assisted.
Author
Owner

@⁠whoalin1 on GitHub · 2026-11-24 19:08 UTC · original

GPU verification on real hardware (AI-assisted run by Devin on the night shift). Tested head 054f640e0 in a clean worktree; runtime/build built with MSVC 19.44 / Ninja, Vulkan SDK 1.4.363.

ps_fminmax_signed_zero — all four combinations PASS

Command shape (run from repo root):

LLVM_MC="wsl -d Ubuntu -e /usr/lib/llvm-21/bin/llvm-mc" \
VK_LOADER_LAYERS_DISABLE=~implicit~ MSYS_NO_PATHCONV=1 \
py -3.14 -I scripts/run_gpu_tests.py --filter fminmax --work <dir>

with the per-GPU env from the repo docs (VK_LOADER_DRIVERS_SELECT/LEONIDA_VK_DEVICE), plus LEONIDA_PC_TAIL_UBO=1 on the AMD iGPU (see note below).

GPU / driver wave64 emulation result mrt0 readback (every pixel)
AMD Radeon iGPU, Ryzen 5 7600 RDNA2, AMD proprietary driver 24.10.24, Vulkan 1.3.280 off PASS 4096/4096 0x00000000, 0x00000000, 0x80000000, 0x80000000
same LEONIDA_W64_EMULATE=1 PASS 4096/4096 same
NVIDIA RTX 4070 SUPER, driver 610.88 off PASS 4096/4096 same
same LEONIDA_W64_EMULATE=1 PASS 4096/4096 same

The readback is max(-0,+0)=+0, max(+0,-0)=+0, min(-0,+0)=-0, min(+0,-0)=-0 — the RDNA2 "-0 < +0" ordering, on both vendors.

Baseline check: I also re-ran the same fixture with tools/rdna2spirv.py temporarily reverted to the PR's parent (1c93c8697, plain NMax/NMin, no sign-bit select). It also passed on both GPUs with identical readback — neither the AMD proprietary driver nor the NVIDIA driver exhibits the spec-literal "return the first operand" behavior for -0/+0 here (both appear to lower NMax/NMin to the hardware v_max/v_min-equivalent, which already orders -0 below +0). So the fix is spec-compliance hardening against a strictly conforming driver rather than a fix for an observed failure on these two devices; it is correct and causes no regression, and it does make the emitted SPIR-V match the documented RDNA2 contract regardless of driver.

AMD note: the iGPU has maxPushConstantsSize = 128, so without LEONIDA_PC_TAIL_UBO=1 the fixture reports SKIP (unsupported: need 144 push constant bytes, device has 128). With the UBO tail it runs and passes in both modes.

Validation: one filtered run with LEONIDA_VK_VALIDATION=1 on the AMD iGPU — PASS, no validation errors.

Full suite regression check (scripts/run_gpu_tests.py, no filter)

  • AMD iGPU (with LEONIDA_PC_TAIL_UBO=1): 225 passed / 20 skipped / 4 failed of 249. Failures: cs_imgload_tfe, cs_imgload_d16_tfe (known issue, tracked as #31) and ms_clip, ms_wave_info_s3 (known AMD Windows driver mesh-shader crash, SHADER-167 — driver banner printed, no shader output). All four are pre-existing on main and unrelated to this change (float min/max lowering only).
  • NVIDIA RTX 4070 SUPER: 198 passed / 49 skipped / 2 failed of 249. Failures: only cs_imgload_tfe and cs_imgload_d16_tfe (same known issue #31). ms_clip and ms_wave_info_s3 pass here.

RADV was not available on this machine (Windows host, AMD proprietary driver only); Intel was not tested.

AI-assisted verification (Devin).

**@⁠whoalin1** on GitHub · 2026-11-24 19:08 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1027#issuecomment-7141657575) GPU verification on real hardware (AI-assisted run by Devin on the night shift). Tested head `054f640e0` in a clean worktree; `runtime/build` built with MSVC 19.44 / Ninja, Vulkan SDK 1.4.363. ## `ps_fminmax_signed_zero` — all four combinations PASS Command shape (run from repo root): ``` LLVM_MC="wsl -d Ubuntu -e /usr/lib/llvm-21/bin/llvm-mc" \ VK_LOADER_LAYERS_DISABLE=~implicit~ MSYS_NO_PATHCONV=1 \ py -3.14 -I scripts/run_gpu_tests.py --filter fminmax --work <dir> ``` with the per-GPU env from the repo docs (`VK_LOADER_DRIVERS_SELECT`/`LEONIDA_VK_DEVICE`), plus `LEONIDA_PC_TAIL_UBO=1` on the AMD iGPU (see note below). | GPU / driver | wave64 emulation | result | mrt0 readback (every pixel) | |---|---|---|---| | AMD Radeon iGPU, Ryzen 5 7600 RDNA2, AMD proprietary driver 24.10.24, Vulkan 1.3.280 | off | PASS 4096/4096 | `0x00000000, 0x00000000, 0x80000000, 0x80000000` | | same | `LEONIDA_W64_EMULATE=1` | PASS 4096/4096 | same | | NVIDIA RTX 4070 SUPER, driver 610.88 | off | PASS 4096/4096 | same | | same | `LEONIDA_W64_EMULATE=1` | PASS 4096/4096 | same | The readback is `max(-0,+0)=+0`, `max(+0,-0)=+0`, `min(-0,+0)=-0`, `min(+0,-0)=-0` — the RDNA2 "-0 < +0" ordering, on both vendors. **Baseline check:** I also re-ran the same fixture with `tools/rdna2spirv.py` temporarily reverted to the PR's parent (`1c93c8697`, plain `NMax`/`NMin`, no sign-bit select). It **also passed on both GPUs** with identical readback — neither the AMD proprietary driver nor the NVIDIA driver exhibits the spec-literal "return the first operand" behavior for -0/+0 here (both appear to lower NMax/NMin to the hardware `v_max`/`v_min`-equivalent, which already orders -0 below +0). So the fix is spec-compliance hardening against a strictly conforming driver rather than a fix for an observed failure on these two devices; it is correct and causes no regression, and it does make the emitted SPIR-V match the documented RDNA2 contract regardless of driver. **AMD note:** the iGPU has `maxPushConstantsSize = 128`, so without `LEONIDA_PC_TAIL_UBO=1` the fixture reports `SKIP (unsupported: need 144 push constant bytes, device has 128)`. With the UBO tail it runs and passes in both modes. **Validation:** one filtered run with `LEONIDA_VK_VALIDATION=1` on the AMD iGPU — PASS, no validation errors. ## Full suite regression check (`scripts/run_gpu_tests.py`, no filter) - **AMD iGPU** (with `LEONIDA_PC_TAIL_UBO=1`): 225 passed / 20 skipped / 4 failed of 249. Failures: `cs_imgload_tfe`, `cs_imgload_d16_tfe` (known issue, tracked as #31) and `ms_clip`, `ms_wave_info_s3` (known AMD Windows driver mesh-shader crash, SHADER-167 — driver banner printed, no shader output). All four are pre-existing on `main` and unrelated to this change (float min/max lowering only). - **NVIDIA RTX 4070 SUPER**: 198 passed / 49 skipped / 2 failed of 249. Failures: only `cs_imgload_tfe` and `cs_imgload_d16_tfe` (same known issue #31). `ms_clip` and `ms_wave_info_s3` pass here. RADV was not available on this machine (Windows host, AMD proprietary driver only); Intel was not tested. AI-assisted verification (Devin).
Author
Owner

@⁠iFeyz on GitHub · 2026-11-24 19:40 UTC · original

AMD run, as asked: RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11. Head 054f640e0, scripts/run_gpu_tests.py --filter ps_fminmax_signed_zero with the test programs of a current main build.

  • ps_fminmax_signed_zero: translated, passes spirv-val, PASS native (subgroup 64) and with LEONIDA_W64_EMULATE=1.
  • tests/semantic/test_float_nan_fract.py: 34 tests OK.

So the proprietary AMD driver on RDNA4 agrees with the fixture. No RADV run here (Windows only).

**@⁠iFeyz** on GitHub · 2026-11-24 19:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1027#issuecomment-7142433819) AMD run, as asked: RX 9070 XT (RDNA4, GFX1201), AMD proprietary Windows driver 26.11.2 (LLPC, Vulkan 1.4.359), Windows 11. Head `054f640e0`, `scripts/run_gpu_tests.py --filter ps_fminmax_signed_zero` with the test programs of a current `main` build. - `ps_fminmax_signed_zero`: translated, passes `spirv-val`, **PASS** native (subgroup 64) and with `LEONIDA_W64_EMULATE=1`. - `tests/semantic/test_float_nan_fract.py`: 34 tests OK. So the proprietary AMD driver on RDNA4 agrees with the fixture. No RADV run here (Windows only).
Author
Owner

@⁠oscarlosu-lab on GitHub · 2026-11-25 07:44 UTC · original

Thank you @⁠syntaxixr (RX 6600), @⁠whoalin1 (Ryzen 7600 iGPU, RTX 4070 SUPER, validation & full-suite sweep), and @⁠iFeyz (RX 9070 XT) for the AMD proprietary and NVIDIA checks!

Rebased cleanly onto main (3f81d46ac, head 9bc384b8b):

  • tests/semantic/test_float_nan_fract.py + tests/test_docs_links.py: 50/50 passed (0 failures)
  • py_compile on tools/rdna2spirv.py, scripts/run_gpu_tests.py, runtime/test/shaders/check.py: OK
  • scripts/check_no_game_data.py: OK

Keeping as Draft for now while deciding whether to keep the spec-compliance glsl_minmax select against strictly conforming drivers (RADV/Intel still untested) or land the fixture alone.

**@⁠oscarlosu-lab** on GitHub · 2026-11-25 07:44 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1027#issuecomment-7156197199) Thank you @⁠syntaxixr (RX 6600), @⁠whoalin1 (Ryzen 7600 iGPU, RTX 4070 SUPER, validation & full-suite sweep), and @⁠iFeyz (RX 9070 XT) for the AMD proprietary and NVIDIA checks! Rebased cleanly onto `main` (`3f81d46ac`, head `9bc384b8b`): - `tests/semantic/test_float_nan_fract.py` + `tests/test_docs_links.py`: **50/50 passed** (`0` failures) - `py_compile` on `tools/rdna2spirv.py`, `scripts/run_gpu_tests.py`, `runtime/test/shaders/check.py`: **OK** - `scripts/check_no_game_data.py`: **OK** Keeping as **Draft** for now while deciding whether to keep the spec-compliance `glsl_minmax` select against strictly conforming drivers (RADV/Intel still untested) or land the fixture alone.
github-bot closed this pull request 2026-11-26 14:53:36 +00:00
Author
Owner

@⁠oscarlosu-lab on GitHub · 2026-11-26 14:50 UTC · original

Closing draft PR (moving work to local/offline).

**@⁠oscarlosu-lab** on GitHub · 2026-11-26 14:50 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1027#issuecomment-7195401999) Closing draft PR (moving work to local/offline).

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.