Skip to content

rdna2spirv: use NMax/NMin in v_cvt_pk_u8_f32 so NaN inputs convert to 0 - #1129

Open
ItsJaved wants to merge 1 commit into
ItsNotRexed:mainfrom
ItsJaved:fix/cvt-pk-u8-nan
Open

ItsJaved wants to merge 1 commit into
ItsNotRexed:mainfrom
ItsJaved:fix/cvt-pk-u8-nan

Conversation

@ItsJaved

Copy link
Copy Markdown
Contributor

Summary

Closes the �_cvt_pk_u8_f32(NaN) (FClamp) open gap listed in docs/design.md (line 266):

  • Claim: Coordinating who works on what (4) (moved to #1218) #1006 (comment)
  • Translator ( ools/rdna2spirv.py): Replaced GLSL.std.450 FClamp(x, 0.0, 255.0) (which leaves NaN inputs undefined in SPIR-V) with NMin(NMax(x, 0.0), 255.0). Since NMax(NaN, 0.0) returns .0 and NMin(0.0, 255.0) keeps .0, NaN inputs (quiet/signalling, positive/negative) cleanly convert to while [0, 255] values, negative/infinity clamping, and round-to-nearest-even behaviour remain unchanged.
  • Tests & Docs:
    • ests/semantic/test_f32_cvt_pk_u8.py: covers ±qNaN, ±sNaN, ±inf, -0, negative values, rounding ties (12.5 -> 12, 13.5 -> 14), clamping at 255, byte insertion mask + VOP3 source modifiers, plus sabotage detection back to FClamp.
      

untime/test/shaders/cs_cvt_pk_u8.asm wired into
untime/test/shaders/check.py and scripts/run_gpu_tests.py.

  • Updated docs/design.md row (also recording RTX 5090 GPU verification of ps_f32_mods.asm and cs_cvt_pk_u8.asm).

Testing (AI-assisted, no game files)

  • python -m compileall -q make_game.py tools tests scripts runtime/test/shaders -> exit 0
  • python -I tools/f32_fuzz.py --programs 2000 --ops v_cvt_pk_u8_f32 -> 1,702 match, 0 mismatch, 0 undefined in SPIR-V, 298 skipped (vs 17 undefined in 500 programs on main)
  • python -I -m unittest discover -s tests/semantic -p test_f32_cvt_pk_u8.py -v -> 2/2 OK
  • python -m pytest tests/test_translate.py -q -> 315 passed
  • GPU (Windows 11, NVIDIA GeForce RTX 5090, driver 621.24, LEONIDA_VK_VALIDATION=1):
    • cs_cvt_pk_u8: 1/1 passed (xaabb00dd 0xaabb00dd 0xaabb00dd 0xaabb00dd 0xaabbffdd 0xaabb0cdd 0xaabb0edd 0xaabb00dd) both natively and under LEONIDA_W64_EMULATE=1.
    • Negative control: reverting NMin(NMax(...)) to FClamp in est_f32_cvt_pk_u8.py raises UnsupportedConstruct on FClamp of NaN (and triggers undefined in SPIR-V in ools/f32_fuzz.py).

@whattheflop

Copy link
Copy Markdown
Contributor

AI-assisted independent verification of 5043c089325cac7db43996e691b83ab9105a2dbf on Windows 11 Pro 25H2 (26200.9974), AMD Radeon(TM) Graphics iGPU (Ryzen 7 9800X3D, PCI 1002:13c0). Native AMD ICD 25.10.30.02 (Windows driver 32.0.21030.2001, Vulkan device API 1.4.315), subgroup range 32–64, fixture requesting 32.

  • Stock synthetic cs_cvt_pk_u8 passed 1/1, with 8/8 exact lane words: aabb00dd aabb00dd aabb00dd aabb00dd aabbffdd aabb0cdd aabb0edd aabb00dd. Bytes 0, 2 and 3 were preserved in every lane.
  • Against exact parent c63773d51235d72250d48699f29830ac9ddafd60, the same fixture with the three NaN inputs replaced by −0, 255.4f and 256 produced byte-identical readback on both commits, 8/8 each. This also covered infinities, a negative input, and ties 12.5 → 12 and 13.5 → 14. The CPU interpreter matched all nine defined controls on both commits and all four signed quiet/signaling NaN cases at head. The parent CPU interpreter identified all four NaN cases as undefined FClamp.
  • All three GPU runs passed spirv-val --target-env vulkan1.2 and emitted no Khronos validation warnings or errors. The loader confirmed the AMD ICD and validation layer were active with process-local selection. The fixture used native wave32.

Only vk_compute_test was built (MSVC 19.38.33145.0, Windows SDK 10.0.26100.0, CMake 4.4.4, Ninja 1.13.2). Used uv 0.11.19 / CPython 3.12.13, LLVM 21.1.8 for AMDGPU assembly via WSL, and Vulkan SDK/loader/validation 1.4.363 / SPIRV-Tools v2026.4 (gef96ed76).

@undeadhuman37

Copy link
Copy Markdown
Contributor

Tested 5043c08 on NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 Home 25H2 (26200.9974), VS 2022 Build Tools, Vulkan SDK 1.4.363.1, llvm-mc 21.1.8: pass

  • run_gpu_tests.py --filter cs_cvt_pk_u8: PASS, words 0xaabb00dd 0xaabb00dd 0xaabb00dd 0xaabb00dd 0xaabbffdd 0xaabb0cdd 0xaabb0edd 0xaabb00dd (translated, passes spirv-val).
  • Full run_gpu_tests.py: 113 translated, all pass spirv-val; 84/111 passed, 27 skipped (all wave64-native, no 64-wide subgroup on NVIDIA).
  • tests/semantic/test_f32_cvt_pk_u8.py: 2 OK. unittest discover -s tests/semantic: 1103 OK.
  • Runtime build: 0 warnings; ctest 55/55 passed.

AI-assisted. No game files.

@quackottu quackottu 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.

Tested 5043c08 on NVIDIA GeForce RTX 5070 (subgroup 32), driver 621.24, Windows 11 Pro (26300), Vulkan SDK 1.4.363.0, llvm-mc 21.1.8 (built with scripts/build_llvm_mc.ps1), scripts/run_gpu_tests.py:

  • native: 84/111 passed, 27 skipped (main 8df277f here: 64/91 passed, 27 skipped, all wave64 tests needing a 64-wide subgroup)
  • LEONIDA_W64_EMULATE=1: 111/111 passed (main here: 91/91)
  • python -m unittest discover -s tests/semantic: Ran 1103 tests in 194.143s OK

@BACONMANXDGOD

Copy link
Copy Markdown
Contributor

Tested 5043c08: pass on both GPUs. Windows 11 26200, MSVC 19.44, llvm-mc 23.1.6, Vulkan SDK 1.4.363, runtime built from main c63773d; applied to c63773d (merges cleanly). GPUs: Intel Arrow Lake Xe-LPG iGPU (Windows 32.0.101.8132, subgroup 8..32) and RTX 5070 Ti Laptop (617.42), picked with VK_LOADER_DRIVERS_SELECT.

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

@IAmNotSiob IAmNotSiob 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.

yep, FClamp with a NaN is undefined. NMax(NaN, 0) = 0 gives the hw's NaN -> 0 without guessing. tests pass with main ^^ (#409 had the same goal a while back, might be worth closing that one if it's stale) AI-assisted review (Claude Code).

@EchoTD

EchoTD commented Nov 22, 2026

Copy link
Copy Markdown

Tested 5043c08 on AMD Radeon RX 6800 (RDNA2, RADV NAVI21, Mesa 26.3.1, Vulkan 1.4.363), Arch Linux container on Linux 7.2.15: pass

  • Toolchain in the container: llvm-mc 23.1.5, GCC 16.2.1, Python 3.14.10. Runtime built from this PR's head.
  • run_gpu_tests.py: 111/111 passed. cs_cvt_pk_u8 PASS, natively and with LEONIDA_W64_EMULATE=1: 0xaabb00dd 0xaabb00dd 0xaabb00dd 0xaabb00dd 0xaabbffdd 0xaabb0cdd 0xaabb0edd 0xaabb00dd.
  • unittest discover -s tests/semantic: 1103 OK. test_f32_cvt_pk_u8.py: 2 OK.

Control: this PR's tree with only the tools/rdna2spirv.py hunk reversed (FClamp again, the new tests kept):

  • test_f32_cvt_pk_u8.py: 5 errors, one for each of the four NaN words (0x7fc00000, 0xffc00000, 0x7f800001, 0xff800001) plus the sabotage check. So the semantic test detects the old rule.
  • cs_cvt_pk_u8 on the GPU still passes, with the same eight words, natively and emulated. On RADV/RDNA2 this GPU test doesn't tell the fix from the old rule.
  • Why, from RADV_DEBUG=shaders on vk_compute_test: RADV compiles both FClamp(x, 0, 255) and NMin(NMax(x, 0), 255) to the same instruction, v_med3_f32 v1, 0x437f0000, 0, v1 (identical encoding, followed by v_rndne_f32 and v_cvt_u32_f32). That v_med3_f32 returns 0 for the NaN lanes here. The inputs are selected per lane from v0, so they aren't compile-time constants the driver could fold.

So on this GPU and driver the PR makes no difference to the generated code and introduces no regression. The fix matters where a driver lowers FClamp's undefined NaN case differently. I haven't measured which drivers do.

AI-assisted (Claude Code). No game files. The run used a rootless podman container with no network.

@YaBulochka YaBulochka 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.

Tested 5043c089 from a clean worktree on AMD Radeon RX 7600 (gfx1102), RADV Mesa 27.0.0-devel, Linux 7.3.3:

  • tests/semantic/test_f32_cvt_pk_u8.py -> 2 passed, 13 subtests.
  • scripts/run_gpu_tests.py: 113 shaders translated and spirv-val; 111/111 passed, including
    PASS cs_cvt_pk_u8 0xaabb00dd 0xaabb00dd 0xaabb00dd 0xaabb00dd 0xaabbffdd 0xaabb0cdd 0xaabb0edd 0xaabb00dd.

So the NaN path gives 0 on this card, same as the measurement that motivated the fix.

One coordination note: #409 implements the same behaviour (FClamp replaced, with a GPU test whose expected words are the hardware ones) and has already been verified on this card at fc1895e. Under the rule that the first to land wins, whichever merges first makes the other redundant; this branch is currently CONFLICTING against main, so a rebase is what it needs if it is to be the one.

@NathansDomainSolutions

Copy link
Copy Markdown

Tested 5043c08 on the full shader corpus from my own 1.0.2.1 copy. Native Linux (Arch), llvm-mc 23.1.5, --wave64-emulate, 12 shards. RTX 4070, NVIDIA 620.73.04. AI-assisted run.

  • Corpus: 26,503 translated, 26,503 pass spirv-val, the same as main at c63773d. No regression.
  • Scope: 649 SPIR-V modules differ from main. Those are exactly the 649 shaders whose disassembly contains v_cvt_pk_u8_f32, so no other shader changed.
  • GPU suite: run_gpu_tests.py gives 84/111 native (27 skipped, wave64) and 111/111 with LEONIDA_W64_EMULATE=1. cs_cvt_pk_u8 passes.

Only outcomes here, no shader data. (The PR now conflicts with main; these numbers are for the head before the rebase.)

crastinator added a commit to crastinator/Leonida that referenced this pull request Nov 23, 2026
…class test

main's active-run check (ItsNotRexed#3549) rejects every OpFunctionCall inside an
if (active) run, so ps_mandelbrot failed on the v_sqrt_f32 helper call.
Each f32 helper body now passes check_lane_local when it is built and is
recorded in lane_local_fns; calls to those are allowed in a run, any
other call is still rejected.

test_undefined_spirv_is_its_own_class used v_cvt_pk_u8_f32 of NaN, which
ItsNotRexed#1129 made defined, and with the helpers no oracle op translates to
undefined SPIR-V any more. The test now hands check() an interpreter
that refuses the input.
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.

9 participants