Skip to content

rdna2spirv: v_fract_f16 passes a NaN operand through, +0 with clamp (gfx1036) - #7257

Open
nonverifiedperson wants to merge 2 commits into
ItsNotRexed:mainfrom
nonverifiedperson:translator/fract-f16-nan
Open

nonverifiedperson wants to merge 2 commits into
ItsNotRexed:mainfrom
nonverifiedperson:translator/fract-f16-nan

Conversation

@nonverifiedperson

Copy link
Copy Markdown
Contributor

Claimed in #7049 (claim). Depends on: none.

What and why

When v_fract_f16 gets a NaN operand, the translator wrote 0x7e00. With clamp, it passed the NaN to FClamp, which GLSL.std.450 leaves undefined, and it refused the shader when the operand was a constant. On gfx1036, under the game's MODE (IEEE 0, DX10_CLAMP 1), the hardware does this instead:

  • NaN operand: comes back unchanged, with its payload, signalling bit and sign. A source neg/abs only flips or clears the sign bit, and mul:2 doesn't change the result.
  • With clamp: the result is +0.
op, source half gfx1036 IEEE 0 gfx1036 IEEE 1 main translator this PR
v_fract_f16 0x7c01 (also _e64, mul:2) 0x7c01 0x7e01 0x7e00 0x7c01
v_fract_f16 0xfe11 (also _e64, mul:2) 0xfe11 0xfe11 0x7e00 0xfe11
_e64 -v1, 0x7c01 0xfc01 0xfe01 0x7e00 0xfc01
_e64 -v1, 0xfe11 0x7e11 0x7e11 0x7e00 0x7e11
_e64 |v1|, 0xfe11 0x7e11 0x7e11 0x7e00 0x7e11
_e64 |v1|, 0x7c01 0x7c01 0x7e01 0x7e00 0x7c01
_e64 ... clamp, NaN (4 rows) 0x0000 0x0000 refused 0x0000

Infinities (0xfe00, or 0 with clamp), ordinary values, 0x8001 (gives 0x3bff) and v_fract_f32 already matched, and they still do.

Change

  • tools/rdna2spirv.py, f16 unary path, Fract only:
    • The operand is read with f16_in_bits.
    • Without clamp, f16_out gets a new nan_keep=(is_nan, bits) and selects the source half after narrowing.
    • With clamp, a NaN operand becomes +0 before FClamp, as an infinity already did.
    • The other f16 unary ops are unchanged.
  • tests/semantic/test_fract_f16_nan.py with fixtures/ps_fract_f16_nan.asm: a run-time half in s0, covering plain, -x, |x| and -x clamp for two NaNs, +inf and 1.5. The test fails on main (the interpreter rejects FClamp of a NaN) and passes here.
  • docs/design.md: the NaN rule, how it was measured and the regression test, added to the "Float min/max/med3 with NaN, v_fract" row.

Tests

  • python -m compileall -q make_game.py tools tests and python -I make_game.py --help: OK.
  • python -I scripts/run_semantic_tests.py tests/semantic: 444 files, 5890 tests, 4 files failed. Those 4 are test_float_nan_fract (fixed by Preserve signaling NaNs through minmax source modifiers #6685) and test_image_atomics_guard, test_image_cube_store_glc, test_image_store_d16. All four fail the same way on main without this change (checked by stashing it).
  • tests/semantic/test_fract_f16_nan.py: passes; on main it errors on both NaN rows. test_fract_f16_infinity and test_valu_fract_f16 still pass.

GPU check. Hardware words come from raw hw_oracle kernels (asm.Kernel(..., raw=True, mode=dict(ieee=0 or 1, dx10_clamp=1, denorm32=0, denorm16_64=3))) on gfx1036. Translator words come from int_hw_probe.run_translator with LEONIDA_VK_DEVICE=radeon and nvidia. 34 rows, 0 differ from the IEEE 0 hardware words on either GPU; 14 differed on main.

Windows 10 22H2, AMD Radeon iGPU (gfx1036, Ryzen 7 9800X3D, driver 32.0.13036.4), NVIDIA RTX 5070 Ti, Vulkan SDK 1.4.363. Synthetic values only, no game files. AI-assisted (Claude Code).

🤖 Generated with Claude Code

…gfx1036)

gfx1036 in the game's MODE (IEEE 0, DX10_CLAMP 1) returns a NaN operand of
v_fract_f16 unchanged (payload, signalling bit, sign; neg/abs act on the sign
bit only) and +0 with clamp. The translator wrote 0x7e00 and, with clamp,
handed the NaN to FClamp (undefined; refused for a constant operand).

Select the source half after narrowing (f16_out nan_keep) and replace a NaN
operand by +0 before FClamp. Adds tests/semantic/test_fract_f16_nan.py and
the rule to docs/design.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tikD1337

Copy link
Copy Markdown
Contributor

Review: #7257 — rdna2spirv: v_fract_f16 passes a NaN operand through, +0 with clamp (gfx1036)

Environment: Windows 11, CPython 3.13. CPU interpreter only, no GPU run on my side.

What changed

tools/rdna2spirv.py

  • Gen.f16_out takes a new nan_keep=(bool, %uint half) argument. When the bool is true, the raw 16-bit source half is written instead of the narrowed result. This happens after the fract 0x3c00 → 0x3bff fix-up, so the two don't interfere: that fix-up only fires on a 1.0 result, never on a NaN.
  • The v_fract_f16 path now reads its operand through f16_in_bits. Before, it used f16_in. The value it computes is the same (the widened f32 with abs/neg applied). It also returns h_mod, the source half with abs (bit 15 cleared) and neg (bit 15 flipped) applied as bit operations. _f16_half_nan(h_mod) then gives the NaN test. Because abs/neg only touch bit 15, testing NaN on h_mod is equivalent to testing it on the raw half.
  • Without clamp: nan_keep = (nan, h_mod). The f32 path (Fract, fract_below_one, the infinity select to 0xffc00000, then narrowing) still runs, but its NaN result is replaced at the end. That fixes the old behaviour, which quieted the NaN and dropped its payload, giving 0x7e00. It is also correct with an output modifier: nan_keep overrides the result after the modifier, which matches the measured "mul:2 does not change it".
  • With clamp: OpLogicalOr(inf, nan) selects %f0 before Fract/FClamp. FClamp therefore never sees a NaN, which GLSL.std.450 leaves undefined. The result is +0, as measured with DX10_CLAMP = 1. The old code also refused a constant NaN operand under clamp; that refusal is gone, because the operand is now selected away before FClamp.
  • The other f16 unary ops (rsq/sqrt/exp/log/floor/ceil/trunc/rndne, and rcp) still go through f16_in, so their code is unchanged.

Tests: tests/semantic/fixtures/ps_fract_f16_nan.asm is a hand-written fixture with the operand read at run time from s0, so nothing is constant-folded. tests/semantic/test_fract_f16_nan.py covers:

  • a signalling NaN (0x7c01), a negative quiet NaN with a payload (0xfe11), +inf, and 1.5;
  • each with no modifier, -x, |x| and -x clamp;
  • the destination's high half each time (the fixture's non-zero high words show it is kept).

Findings

  1. The new sentence in docs/design.md overstates the IEEE 1 match (should fix). It says the rows were measured at "IEEE 0 and 1" and that "the translated words match all of them". The translator has no MODE.IEEE input on this path. nan_keep always returns the source bits, so a signalling 0x7c01 comes out as 0x7c01. The same paragraph says the hardware gives 0x7e01 with IEEE 1. So the IEEE 1 signalling-NaN rows cannot match. Please either:

    • restrict the claim to the IEEE 0 rows plus the IEEE 1 quiet-NaN rows, or
    • say plainly that IEEE 1 signalling NaNs are not modelled, which is consistent with the row's existing "Not modelled: signalling NaNs under IEEE mode".

    Please also give the per-mode row counts out of the 34.

  2. The design table row now contradicts itself (should fix). Right after the new text, these older sentences are still in the row:

    • "A source neg on a signalling NaN operand still quiets it in the translator"
    • "Not changed: … neg on a signalling NaN operand quiets it in the translator"
    • "NaN under the clamp bit (… the translator still emits FClamp)", which appears twice

    For v_fract_f16 all of these are now false; the new test checks that -0x7c01 gives 0xfc01 and that -x clamp gives +0. Either narrow each one to the ops it still applies to (the f32 v_fract, and min/max/med3 under clamp), or remove it. The row was already full of duplicated sentences before this PR. This change makes the contradiction direct, though.

  3. Some measured cases have no fixture coverage (nit).

    • mul:2 (or another output modifier) on a NaN operand: the docs give it as measured, and the code handles it because nan_keep overrides last. A single v_fract_f16_e64 vN, vM mul:2 line would pin that ordering.
    • The op_sel high-half source and destination: f16_in_bits(a[1], sel[0]) / dhi.
    • |x| clamp, and a constant NaN operand under clamp. The constant case is the one the commit message says used to be refused.
  4. The constant-NaN status is discarded (nit, no action needed). f16_in_bits returns a constant-NaN status that the fract path ignores (_). So a constant operand still gets a run-time OpUGreaterThan. This is correct, and the driver will fold it.

The code change is sound. I found no correctness bug in the generated SPIR-V for the game's MODE (IEEE 0, DX10_CLAMP 1).

Test results (no game files)

  • Harness checks: git diff --check, compileall, check_no_game_data.py and make_game.py --help passed.
  • The harness's run_semantic_tests.py result is not a PR failure. It exited with code 2 because it was called without its required directory argument (tests/semantic). It never ran a test.
  • python -m unittest discover -s tests/semantic -p "test_fract_f16_*.py": 2 tests, OK. That covers the new test_fract_f16_nan.py and the existing test_fract_f16_infinity.py, so the infinity rule did not regress.
  • test_float_nan_fract.py: 31 tests, 3 failures and 1 skip. All three failures are in AllNaNSrc0PayloadTests.test_f32_three_input_all_nan_keeps_src0 (v_min3/max3/med3_f32). I restored the base tools/rdna2spirv.py from HEAD~1 and got the same 3 failures, so they already exist on the base and do not involve the f16 path. They should be posted separately as a finding:.
  • I did not run the full semantic suite or a GPU run. The PR reports the translated words matching on gfx1036 (Ryzen 7 9800X3D iGPU, driver 32.0.13036.4) and on an RTX 5070 Ti.

Adherence

  • No game data: the fixture is a 12-line hand-written listing with an operand supplied at run time, and the test inputs are hand-picked half words.
  • No keys, decryption or protection workarounds, and nothing online.
  • Fix, don't disable: the case the translator used to refuse (a constant NaN under clamp) now gets a defined lowering. Nothing was added to a skip list.
  • Contract table: the rule is in the docs/design.md row with its confirmation (raw hw_oracle kernels on gfx1036, 34 rows) and a CPU regression test. Findings 1 and 2 above need fixing before that row is accurate.

Verdict: approve once the two docs/design.md corrections (findings 1 and 2) are made. The translator change itself is correct and well scoped.

AI-assisted.

…ting/FClamp sentences; test mul:2, |x| clamp and constant NaNs (review)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nonverifiedperson

Copy link
Copy Markdown
Contributor Author

@tikD1337 thanks, all four addressed in 3da07f69:

  1. IEEE 1 claim: the row now says the translated words match the IEEE 0 words in all 34 rows. It gives the split: 27 rows with no modifier, clamp or mul:2, and 7 with neg/abs, each measured in both modes. It also says plainly that IEEE 1 isn't modelled: there a signalling NaN is quieted, so 6 of the 34 rows differ between the modes, while the translator still returns the NaN unchanged.

  2. Contradicting sentences:

    • "A source neg on a signalling NaN … quiets it" and the "Not changed" sentence are narrowed to the min/max/med3 ops. The stale "f16 fract(±inf) … a quiet positive NaN in the translator" clause is gone, because the translator gives 0xfe00.
    • Both "the translator still emits FClamp" sentences now say: except v_fract_f16, which selects +0 first.
  3. Coverage: a second fixture, ps_fract_f16_nan_mods.asm, adds:

    • mul:2 on a run-time NaN (0x7c01, 0xfe11 unchanged; 0xfe00 for +inf; 1.5 gives 0x3800);
    • |x| clamp (+0 for a NaN or an infinity);
    • a constant 0x7c01 under clamp (+0; this one used to be refused);
    • a constant 0xfe11 with no modifier (unchanged).

    It fails on main (6 errors over the two tests) and passes here. op_sel can't be tested on this op: GFX10 v_fract_f16_e64 has no op_sel (llvm-mc for gfx1036 rejects it with "not a valid operand"), so no gfx1036 measurement exists for it either.

  4. Constant-NaN status: left as is, as you said. The driver folds the run-time test.

The 3 test_float_nan_fract failures are main's: #6654 and #6685 cover them.

@Kev-Ball

Kev-Ball commented Nov 23, 2026 •

Copy link
Copy Markdown

RTX 3080 Ti readback matched the IEEE 0, f16-denormals-kept rule in 393,216/393,216 cases. I ran every 16-bit input through six synthetic wave32 compute forms of v_fract_f16: plain, negation, absolute value, negated absolute value, mul:2, and negation with clamp. Tested translator commit 0ca647cb against its parent 8575d36e. The current head 3da07f69 changes docs and tests only.

  • NaN inputs: 12,276/12,276 expected low halves on the PR, covering both signs and every quiet/signaling payload. The parent missed 10,225; its clamped form already returned +0 for every NaN on this driver. For example, plain input 0x7c01 returned 0x7fff on the parent and 0x7c01 on the PR.
  • Finite values and infinities: all 380,940 results matched the parent and a separate host calculation using the denormals-kept rule measured in fract_f16_probe: v_fract_f16 rounds first, then the omod (SHADER-338) #2198. In this mode mul:2 has the same result as the plain form.
  • The seeded destination upper half (0xbeef) survived every write on both revisions. All six modules per revision passed spirv-val.

CachyOS, NVIDIA 620.73.04, native Vulkan storage-buffer readback. Synthetic inputs only, AI-assisted.

@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested 3da07f6 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass

  • pytest tests/semantic/test_fract_f16_nan.py -q — 2 passed, 8 subtests passed
  • python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build — 217/239 passed, 16 skipped, 0 failed (expected skips)

AI-assisted (OpenCode).

@kamilolejnik

Copy link
Copy Markdown

Tested 3da07f6 on Intel Iris Xe Graphics (TGL GT2, Xe-LP), Mesa ANV 26.3.1, Vulkan 1.4.363, Linux 6.12 (Arch container on Debian 13), llvm-mc 23.1.5, SPIRV-Tools v2026.4: pass. Branch and its merge-base main 8575d36 side by side, same vk_compute_test.

Exhaustive sweep, same shape as the RTX 3080 Ti check above: every 16-bit input through six wave32 compute forms of v_fract_f16 (plain _e32, -v1, |v1|, -|v1|, mul:2, -v1 clamp), destination high half seeded 0xbeef, judged against a host model of the PR's rule (IEEE 0, f16 denormals kept, result capped at 0x3bff, inf → 0xfe00, NaN → source bits with the modifier on the sign, clamp → +0 for NaN/inf). Both modules pass spirv-val.

this PR main
all 6 forms 393,216 / 393,216 380,945 / 393,216
NaN inputs (2,046 per form) 12,276 / 12,276 5 / 12,276
finite, inf, denormals all match all match (unchanged)
high half 0xbeef kept 393,216 393,216

One difference from the NVIDIA run that supports the change: on main, the clamped form returns 0xfe00 for every NaN on ANV (2,046/2,046 wrong), while on NVIDIA 620.73 it already returned +0. That is the undefined FClamp of a NaN that the PR removes: drivers really do disagree, and with the PR both give +0. On main the other forms quiet NaN to 0xfe00, e.g. 0x7c01 → 0xfe00 (want 0x7c01), -0x7c01 → 0xfe00 (want 0xfc01).

The sweep is a throwaway script (one 64-thread group looping over 1,024 × 64 inputs, buffer_store_dword per form); happy to post it if useful. Synthetic inputs only, no game files. AI-assisted (Claude Code).

@username634

Copy link
Copy Markdown

Tested 3da07f6 on Intel UHD Graphics 730 (Alder Lake-S GT1, Xe-LP, i5-12400), Intel's Windows driver 101.7092, Vulkan 1.4.323, Windows 11 Pro (build 26300): pass. The Iris Xe result above is Mesa ANV, so this is the other Intel driver. The RTX 5060 Ti in the same PC was excluded with LEONIDA_VK_DEVICE=uhd.

The sweep has the same shape as the two above. Every 16-bit input goes through six wave32 compute forms of v_fract_f16 (plain _e32, -v1, |v1|, -|v1|, mul:2, -v1 clamp), with the destination high half seeded 0xbeef. One 64-thread group loops over 1,024 × 64 inputs with buffer_store_dword per form, in a local fixture that is not committed. The branch and its merge-base main 8575d36ed run side by side with the same vk_compute_test. Both modules pass spirv-val.

this PR main
NaN inputs, PR rule (2,046 per form) 12,276 / 12,276 2,051 / 12,276
non-NaN inputs (finite, inf, denormals) 380,940 / 380,940 same words as the PR
high half 0xbeef kept 393,216 / 393,216 393,216 / 393,216

The non-NaN words also match a host model: exact x − floor(x) of the modified input, rounded to f16 with denormals kept and capped at 0x3bff; inf → 0xfe00; clamp → +0; mul:2 the same as plain.

On this driver, main quiets every NaN to 0xfe00 in the five unclamped forms. The clamped form already gives +0, as NVIDIA 620.73 does and unlike ANV 26.3.1 (0xfe00). So the three drivers split two to one on the undefined FClamp of a NaN, and the PR makes all of them agree.

Tools: standalone Vulkan tools built with llvm-mingw 20261117 (clang 23.1.6), llvm-mc 23.1.6 (conda-forge), SPIRV-Tools 2026.4. Synthetic inputs only, no game files. AI-assisted (Claude Code).

@carlos-seiffert

Copy link
Copy Markdown

Tested 3da07f6 (merged onto main 86a7807) on NVIDIA GeForce RTX 2060 (Turing), NVIDIA 621.24, Vulkan 1.4.357, Windows 11 (10.0.26300), subgroup size 32: pass (no regression)

What I ran: python -I scripts/run_gpu_tests.py with the Vulkan tools built from 86a7807 (llvm-mingw + Ninja, -DLEONIDA_BUILD_WINDOWS_RUNTIME=OFF, Vulkan-Headers 6802bb47), llvm-mc 23.1.6 and SPIRV-Tools from conda-forge.

main 86a7807 this PR on main
full GPU suite 191/240 passed, 48 skipped, 1 fail 191/240 passed, 48 skipped, 1 fail (same as main)

No GPU test changed status against main. This PR adds no GPU fixture of its own, so the run checks that the translator change breaks none of the existing GPU tests; it does not exercise the new rule directly.

Unit tests changed by the PR: tests/semantic/test_fract_f16_nan.py: OK.

The one failure on both sides is ms_wave_info_s3, which fails on main on this GPU (10/10 runs) and is fixed by #7136 / #5741 / #5855; it is unrelated to this PR.

AI-assisted run (Claude Code); the numbers are from my machine.

@InCaseOf

Copy link
Copy Markdown

Tested 3da07f6 on RX 5700 XT (RDNA1, gfx1010, RADV NAVI10), Mesa 26.3.1, Linux 6.18: pass

  • scripts/run_gpu_tests.py: 211/239 passed, 28 skipped, 0 failed.
  • w64_graphics.py over all fixtures: 29/29 ok.
  • scripts/run_semantic_tests.py tests/semantic -j 8: 444 files, 5891 tests, 2 files failed (the same 2 files that fail on main here, nothing new).
  • python -m unittest discover -s tests/semantic -p test_fract_f16_nan.py: 2 tests OK.

LLVM llvm-mc 23.1.5, SPIRV-Tools v2026.4, Python 3.14; test programs built from main e36d4c4 unless rebuilt for the PR as noted. main e36d4c4 on the same GPU: run_gpu_tests.py 212/240, 28 skipped, 0 failed; w64_graphics.py 29/29; semantic suite 2 files failing (test_image_atomics_guard, test_image_store_d16). No game files.

AI-assisted (Claude Code).

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