Skip to content

Shader: preserve the first NaN payload in binary f32 min and max - #3057

Merged
ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:codex/minmax-f32-nan
Nov 22, 2026
Merged

ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:codex/minmax-f32-nan

Conversation

@evilclanker9000

@evilclanker9000 evilclanker9000 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as SHADER-568, binary f32 min/max quiet NaN selection.

What and why

On RX 6800, binary v_min_f32 and v_max_f32 returned the first quiet NaN with its sign and payload when both operands were quiet NaNs. GLSL.std.450 NMin/NMax permit an arbitrary NaN in this case. Select the first operand when the second is NaN. A shader that inspects the resulting bits then sees the measured word. Original gfx1030 measurements and translator change.

Tested

  • One hand-written pixel shader assembled for gfx1013 and gfx1030. On upstream main and this branch, 1/1 synthetic shader translated, assembled as SPIR-V and passed Vulkan 1.2 spirv-val.
  • The regression reads two runtime operands and uses integer XOR to turn the result words into finite MRT colors. Before, all four channels read 0x3f000000. After, min and max give 0x3f200080 in the first operand order and 0xbf110040 in the reverse order. Single-NaN and signed-zero controls passed for both opcodes.
  • Windows CPU tests in four focused modules passed 88 tests and 132 subtests. One optional hardware-oracle test skipped. No GPU or game corpus was used.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

AI-assisted with Codex.

RDNA2 keeps the first quiet NaN word when both operands are quiet NaNs. A shader can convert its sign and payload into a finite color, while NMin and NMax alone may discard both operands in this case.
@zzEmmazz

Copy link
Copy Markdown
Contributor

Tested b36765a6, applied alone on main 16282101, CPU only (Windows 11, Python 3.14, Vulkan SDK 1.4.363, llvm-mc 21.1.8). The translator change does what the test asks, but the test file has the problem @itsonlysimbo reported on #3032.

  • python -m unittest discover -s tests/semantic -p test_minmax_f32_payload.py: ImportError: Failed to import test module, from from conftest import Shader, requires_tools (ModuleNotFoundError: No module named 'conftest'). That is the runner of the CI job "Translator semantic tests" (python -m unittest discover -s tests/semantic -v), and the pytest step of ci.yml leaves tests/semantic out (find tests -path 'tests/semantic' -prune), so as it stands the file fails the first job and is not run by the second. No file in tests/semantic on main imports conftest; they are unittest cases with helpers from tests/semantic/support.py.
  • Under pytest the test itself is sound: with only the test and its fixture applied, python -m pytest tests/semantic/test_minmax_f32_payload.py fails (1 failed); with the whole PR it passes (1 passed).

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested b36765a69f1c72072e58b0fc764a076ae0e74687 on AMD Radeon Pro V540 (RDNA1, Navi 12 / gfx1011, PCI 1002:7360, RADV Mesa 25.2.8-0ubuntu0.24.04.4, Ubuntu 24.04.4, kernel 7.0.0-31-generic, Vulkan device 1.4.318, subgroup 32..64): pass, no regression. This is the first GPU result on this PR.

  • Command: DRI_PRIME=pci-0000_1c_00_0! python -I scripts/run_gpu_tests.py --filter ps_fnan_med3 --build runtime/build/linux: 1/1 passed (4096/4096 pixels exact). Same for ps_fnan_fract: 1/1. Both also passed on two more pinned cards (1f:00.0, ca:00.0). These are the existing NaN/med3/fract GPU fixtures; this PR adds a semantic fixture only, so no new GPU test was available to run.
  • Command: python -I scripts/run_gpu_tests.py --build runtime/build/linux: 159/189 passed, 28 skipped, 2 failed (194 shaders translated, 194 pass spirv-val), identical to clean base 16282101 (159/189, 28 skipped, 2 failed). The two failures (cs_signed_zero_nan, cs_w32_salu64) are pre-existing on base and unrelated to NaN min/max payloads.
  • Command: pytest tests/semantic/test_minmax_f32_payload.py -q: 1 passed.

Tools: llvm-mc 21.1.8 with AMDGPU target, SPIRV-Tools 2026.1, GCC 13.3.0, Python 3.12.3. No code was edited or pushed; only synthetic fixtures were used.

AI-assisted.

@Kam471 Kam471 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on the PR head: python -I -m pytest for the new test -> 1 passed, related suite 20 passed / 38 subtests, docs-row test 2 passed. The 4 rows / 16 output words check both-qNaN orders and finite/±0; mutating to main's translator fails at index 0 (0x3F000000 instead of 0x3F200080), so the test is non-vacuous. The rule (NMin/NMax plus OpIsNan(y) ? x : r) gives one-NaN -> other operand and both-NaN -> first operand, matching the measured RX 6800 (gfx1030) result in docs/design.md / docs/shader_validation.md:999, also confirmed on gfx1150. No game data or absolute paths. Approve.

Minor: OpIsNan is emitted even when y is a non-NaN constant, adding a little SPIR-V; sNaN, clamp and min3 are not covered by the new fixture.

The semantic CI job discovers tests with unittest. Run the numerical fixture there and retain compiled shader validation in the tool-backed pytest job.
@TheMrBeez

Copy link
Copy Markdown

Tested b36765a (base 16282101): the two-NaN case on two GPUs, plus native gfx1036 measurements. The translator change passes; the test file still has the conftest import problem. AI-assisted run, synthetic shaders only, no game files.

1. Native hardware, a second RDNA2. AMD Radeon iGPU (Ryzen 9 9950X3D, gfx1036, driver 25.10.30.02), raw v_min_f32_e32 / v_max_f32_e32 through tools/hw_oracle, wave32 and wave64 (identical), MODE.IEEE 1 and 0. With qA = 0x7fc00080 and qB = 0xffd00040: min/max(qA, qB) = 0x7fc00080 and min/max(qB, qA) = 0xffd00040 in both IEEE modes, so the first operand's sign and payload, as the PR says. Controls: min/max(qA, 1.0) = 1.0, min(-0, +0) = −0, max(-0, +0) = +0.

A note on signalling NaNs, which the PR doesn't claim anything about (sNaN = 0x7f800021):

  • IEEE 0 (the MODE docs/design.md infers for the game): the PR's rule holds for them too, and the sNaN comes back raw: min(sNaN, qA) = 0x7f800021, min(qA, sNaN) = 0x7fc00080, min(1.0, sNaN) = min(sNaN, 1.0) = 1.0, and the same for max.
  • IEEE 1: any sNaN operand gives that NaN quieted (0x7fc00021), even against 1.0 or a quiet NaN. That is the existing "Not modelled: signalling NaNs under IEEE mode" note, so it isn't a problem with this PR.

2. On the GPU, with run-time operands. A hand-written compute shader (not part of this PR) reads x = s4 and y = s5 from VKC_SGPRS, so nothing can be constant-folded, and stores min(x,y), min(y,x), max(x,y), max(y,x). Translated at the base and at this head, run through vk_compute_test, and compared with the gfx1036 IEEE-0 words:

(x, y) base 16282101, RTX 5080 (621.01) base, gfx1036 iGPU this PR, both GPUs
(qA, qB) 7fffffff ×4 ✗ 7fc00080 ×4 ✗ (the order is lost) 7fc00080 ffd00040 7fc00080 ffd00040 ✓
(sNaN, qA) 7fffffff ×4 ✗ 7f800021 ×4 ✗ 7f800021 7fc00080 7f800021 7fc00080 ✓
(qA, sNaN) 7fffffff ×4 ✗ 7fc00080 ×4 ✗ 7fc00080 7f800021 7fc00080 7f800021 ✓
(qA, 1.0), (1.0, qA) 1.0 ✓ 1.0 ✓ 1.0 ✓
(−0, +0) 80000000 80000000 00000000 00000000 ✓ ✓ ✓

So before the change NVIDIA's driver returned its canonical NaN and AMD's returned one operand regardless of order. With the change, both give the hardware words in both orders.

3. The test file. I can reproduce @zzEmmazz's report: python -m unittest discover -s tests/semantic -p test_minmax_f32_payload.py and python -I tests/semantic/test_minmax_f32_payload.py both stop at ModuleNotFoundError: No module named 'conftest', because tests/semantic has no conftest.py. @bobcatchris's pytest run passes, so the test only runs under pytest. In CI that is the wrong runner: the semantic-tests job in .github/workflows/ci.yml runs python -m unittest discover -s tests/semantic -v (as CONTRIBUTING.md and tests/semantic/README.md do), and the pytest "Translator tests" step prunes tests/semantic, so the new file would error in CI.

Also: scripts/run_gpu_tests.py with this translator: RTX 5080 143/189 (46 skipped, wave64), iGPU 174/189 (15 skipped), no failures. Windows 11 Pro 10.0.26200, Vulkan SDK 1.4.363.0, llvm-mc and AMD clang from rocm-sdk-core 10.0.0, Python 3.12.10.

@evilclanker9000

Copy link
Copy Markdown
Contributor Author

I reproduced the conftest import failure with Windows unittest discovery. The semantic test now reads the existing assembly fixture through tests/semantic/support.py. python -I -m unittest discover -s tests/semantic -p test_minmax_f32_payload.py -v passed all four input pairs. With the same test and the translator from 16282101, the two-NaN case failed: all four exported words were 0x3f000000. The LLVM-backed fixture also passed assembly and Vulkan 1.2 SPIR-V validation through tests/test_translate.py, which the pytest CI job collects.

Test-only commit e99017f9.

@TheMrBeez

Copy link
Copy Markdown

Follow-up: the head moved to e99017f while I was posting, so part 3 of my comment above applies to b36765a only. Retested e99017f (AI-assisted, same machine):

  • python -m unittest discover -s tests/semantic -p test_minmax_f32_payload.py (the CI semantic-tests command): Ran 1 test, OK. The conftest problem is fixed.
  • tests/test_translate.py under pytest: 542 passed, 4 skipped (the skips are ms_f16_pack's missing synthetic DXBC, unrelated).
  • The GPU check from part 2, rerun at e99017f: the same exact words on the RTX 5080 and the gfx1036 iGPU. tools/rdna2spirv.py is unchanged from b36765a.

Keep the targeted regression and the intervening upstream changes together for review.
@bielfreedom

Copy link
Copy Markdown

Tested e99017f9 (on its base 16282101) on AMD Radeon RX 9070 XT (RDNA4), AMD driver 32.0.32029.2010 (subgroup 32..64), Windows 11 Pro build 26200, with Vulkan validation on (LEONIDA_VK_VALIDATION=1, Vulkan SDK 1.4.363, VK_LOADER_LAYERS_DISABLE=~implicit~): pass, no regression.

  • python -m unittest discover -s tests/semantic (includes this PR's ps_minmax_f32_payload fixture and test): 4143 tests OK.
  • scripts/run_gpu_tests.py: 194 shaders, 194 translated, 194 pass spirv-val; 175/189 passed, 14 skipped, 0 failures. The skips are the shaders run by the d3d12 tests and ps_w64x_readlane.
  • Full ctest: 119/119, 0 validation errors.

The new fixture is a CPU (interpreter) fixture, so the changed lowering was not run on hardware here. This run shows it breaks nothing else on AMD.

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

@iurysf

iurysf commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested fa5f5e45 (the merge of main f847748c) on RTX 4070 (NVIDIA 621.01, subgroup 32..32), Windows 11 (10.0.26300), Python 3.14, llvm-mc 23.1.6 (AMDGPU), SPIRV-Tools 2026.4: pass. The earlier results on this PR are for b36765a6/e99017f9.

  • test_minmax_f32_payload.py: 1 test OK. Negative control: with only tools/rdna2spirv.py restored to main it fails ([0x3f000000, ...] instead of [0x3f200080, 0x3f200080, 0xbf110040, 0xbf110040]); with the PR's file it passes again.
  • pytest tests/test_translate.py: 563 passed, 4 skipped (missing synthetic DXBC for gs_w64_*/ms_f16_pack); the new fixture is collected.
  • Full tests/semantic with llvm-mc: main 7322 tests OK, this PR 7323 tests OK (one extra test, the new one).
  • scripts/run_gpu_tests.py --require-pass: 153/199 passed, 46 skipped, 0 failed, same per-test result as main (the skips are wave64/64-wide and d3d12-test shaders). The PR has no new GPU test, so this shows no regression, not the two-NaN case itself.

Not tested: AMD/Intel GPUs, Linux/Wine. Tested with AI assistance (Claude); synthetic shaders only, no game data.

@ItsNotRexed
ItsNotRexed merged commit 03290f1 into ItsNotRexed:main Nov 22, 2026
2 of 4 checks passed
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.

8 participants