Skip to content

tests, rdna2spirv: GPU readback for frexp and ldexp, and their neg/abs sources move the sign bit (GPU-FREXP-LDEXP, SHADER-FREXP-LDEXP-SIGN-MODS) - #1063

Merged
aslan2142 merged 8 commits into
ProjectLeonida:mainfrom
milk4sale:gpu-test/frexp-ldexp
Nov 24, 2026
Merged

aslan2142 merged 8 commits into
ProjectLeonida:mainfrom
milk4sale:gpu-test/frexp-ldexp

Conversation

@milk4sale

@milk4sale milk4sale commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: GPU-FREXP-LDEXP (#14 (comment)), SHADER-FREXP-LDEXP-SIGN-MODS (#14 (comment))

Depends on: #1034, which depends on #1012 (this branch is one commit on top of them. All three add a memory-fed fixture next to cs_w32_shift64 and touch the same lines of check.py, run_gpu_tests.py and tests/test_shader_checks.py; the last commit is the one to review. It does not touch what #1047 changes.)

What and why

v_frexp_mant_f32, v_frexp_exp_i32_f32, v_ldexp_f32, v_frexp_mant_f64 and v_frexp_exp_i32_f64 are lowered with integer ops on the float's words. They are CPU-checked and measured natively on gfx1036, but no fixture in runtime/test/shaders ran any of the five.

The fixture. cs_w32_frexp_ldexp is memory-fed, like cs_w32_shift64 (#590): check.py seeds 64 records of a binary32 value, an ldexp exponent and a binary64 value, and each thread loads one at run time. They cover normals, the clamp of the exponent at ±512 and beyond, results that overflow to an infinity and that fall below the smallest normal, binary32 denormals (read as zero), 24 binary64 subnormals (single fraction bits on both sides of the dword boundary, and patterns with several), zeros, infinities and NaNs. A thread stores the five results, v_frexp_mant_f32 with a neg source, and v_ldexp_f32 with an abs source run with the odd threads turned off. The expected words come from an oracle written from the rows of docs/design.md; tests/test_gpu_frexp_ldexp.py checks it against math.frexp and math.ldexp wherever those agree with the hardware, and checks the assembly, the readback and the runner verdicts without a GPU. Both runners compare all 768 heap words.

What it found, and the fix (the finding linked above). On an RTX 4070 Ti six of the 512 result words were wrong: every NaN source under a neg or abs modifier came back as 0x7fffffff, the driver's default NaN, where the hardware returns the source with its sign bit moved. The binary32 branch read its source with Gen.src_u, which applies the modifiers as FAbs and OpFNegate, and Vulkan lets a driver return any NaN from those. The three ops only move bits, and Gen.src_u_sign_mods exists for that case (the min/max family uses it), so the branch now reads its source through it: one line of tools/rdna2spirv.py. SourceModifierBitsTests in tests/semantic/test_alu_frexp_ldexp.py checks that the modifiers are emitted as sign-bit ops and that a NaN, a denormal, a zero and an infinity under them give the hardware's word. The fixture cannot pass on NVIDIA without that line, which is why both are in one pull request; this corrects the "no translator change" of my first claim.

The two rows of docs/design.md name the fixture, and the binary32 one the rule.

Tested

At this head, llvm-mc 23.1.4, SPIRV-Tools v2026.4:

  • python -I scripts/run_gpu_tests.py --build runtime/build --filter cs_w32_frexp_ldexp --require-pass: PASS ("768 words exact") on the RTX 4070 Ti natively and with LEONIDA_W64_EMULATE=1, and on llvmpipe. With the one line put back, word 419 fails on the 4070 Ti ("expected 0xffc00000, got 0x7fffffff"; six words differ in all) and llvmpipe still passes.
  • Rules broken one at a time, fixture rebuilt, on both devices; each failed at the word the oracle predicts: the mantissa given the exponent field 127 (word 0) or, for binary64, 1023 (word 5), the exponent subtracting 127 (word 1), v_ldexp_f32 without its clamp (word 258) or overflowing to the largest finite value (word 106), a binary32 denormal not read as zero (word 352), the binary64 subnormal exponent off by one in the low or in the high dword (words 134 and 150), the fixture without its v_cmpx (word 15).
  • python -m unittest discover -s tests/semantic -p test_alu_frexp_ldexp.py: 15 tests. With tools/rdna2spirv.py put back to the commit below, test_the_modifiers_are_not_float_instructions fails for all eight modifier forms. The interpreter negates on the bits whichever way the module does it, so the value tests hold with both lowerings; the instruction check and the GPU run are what tell them apart.
  • Whole translator suite, python -I scripts/run_semantic_tests.py tests/semantic -j 8: 470 files, 6,169 tests, 0 files failed.
  • Corpus: none of the 25,734 disassembly listings in my work folder contains a frexp or ldexp op, so no game shader's translation changes.
  • git merge-tree against main 32950551e and against rdna2spirv: the binary64 roundings keep a NaN's bits and round a subnormal without the driver (SHADER-ROUND-F64-SPECIALS) #1047's head: clean; the three-way result of this branch, rdna2spirv: the binary64 roundings keep a NaN's bits and round a subnormal without the driver (SHADER-ROUND-F64-SPECIALS) #1047 and main passes the tests below as well.
  • tests/test_gpu_frexp_ldexp.py (11 tests), tests/test_shader_checks.py (87), tests/test_run_gpu_tests.py (64), tests/test_gpu_round_f64.py, tests/test_gpu_cmp_f64.py, tests/test_semantic_translator_use.py, tests/test_docs_design_rows.py, tests/test_docs_links.py, tests/test_docs_repo_paths.py, tests/test_text_write_line_endings.py: OK. python -m compileall -q make_game.py tools tests and scripts/check_no_game_data.py on the changed files: OK.

Not run: the whole GPU suite at this head (it ran clean on main 32950551e for #1059, and this adds one fixture and changes three ops no other fixture uses); llvm-mc 19; Windows; AMD hardware. The binary16 forms and v_ldexp_f64 are not in the fixture. The hardware's words are the rows' gfx1036 measurements, which this does not repeat.

Merge of #1034's 60c2ed910 (eba94cbe8). The red linux-gpu-tools here was not this fixture: cs_w32_frexp_ldexp passed in that run, and #1034's cs_w32_round_f64, which is in this stack, failed on a runner without AVX-512, where llvmpipe computes Trunc and RoundEven of a double with its float code (details in #1034). #1034 now carries an allowance for llvmpipe, merged here without conflict. On the merge: --filter cs_w32_ gives 15 of 23 passed and 8 skipped on llvmpipe (Mesa 26.2.6), natively and under GALLIUM_OVERRIDE_CPU_CAPS=avx, and 23 of 23 on the RTX 4070 Ti; tests/test_gpu_frexp_ldexp.py, tests/test_gpu_round_f64.py, tests/test_shader_checks.py and tests/test_run_gpu_tests.py pass, also on the tree merged with main 4a50caf.

GPUs tested: NVIDIA GeForce RTX 4070 Ti, driver 620.70.06, Linux (CachyOS, kernel 7.3.3); Mesa llvmpipe 26.2.6 (LLVM 23.1.4), same machine

AI-assisted: yes (Claude Code)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • 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.

🤖 Generated with Claude Code

milk4sale and others added 2 commits November 24, 2026 11:04
…64, v_rndne_f64 and v_floor_f64 (GPU-ROUND-F64)

The translator gives the four ops to the driver's double-precision Trunc, Ceil, RoundEven and Floor, and no
fixture in runtime/test/shaders ran them, so what a driver does with ties, the largest value below 0.5, values at
and above 2^52, signed zeros, subnormals, infinities and NaNs had never gone through the GPU suite.

cs_w32_round_f64 is memory-fed, like cs_w32_shift64 and cs_w32_cmp_f64: check.py seeds 64 inputs and each thread
loads one at run time and stores eight results: the four roundings, v_trunc_f64 with a neg source, v_ceil_f64 with
an abs source, a v_floor_f64 whose destination pair overlaps its source pair, and a v_rndne_f64 run with the odd
threads turned off, whose destination must keep the pair it held. The expected words come from Python floats
(exact IEEE-754, the result keeping its source's sign) and are checked against exact rational rounding in
tests/test_gpu_round_f64.py.

Two kinds of input are judged by what Vulkan lets a driver do, not by RDNA2: a NaN may come back as another NaN,
and a subnormal may be read as a zero of its sign, since the module declares no DenormPreserve for 64 bits. That is
what the fixture measured: on an RTX 4070 Ti a signalling NaN comes back quiet, and on llvmpipe the smallest
subnormals are also read as zero. The row in docs/design.md records both as an approximation. dump_test takes an
optional judge for such a readback.

No translator or runtime change.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…s sources move the sign bit (GPU-FREXP-LDEXP, SHADER-FREXP-LDEXP-SIGN-MODS)

v_frexp_mant_f32, v_frexp_exp_i32_f32, v_ldexp_f32, v_frexp_mant_f64 and v_frexp_exp_i32_f64 are lowered with
integer ops on the float's words, and no fixture in runtime/test/shaders ran any of them.

cs_w32_frexp_ldexp is memory-fed, like cs_w32_shift64: check.py seeds 64 records of a binary32 value, an ldexp
exponent and a binary64 value, and each thread loads one at run time and stores the five results, v_frexp_mant_f32
with a neg source, and v_ldexp_f32 with an abs source run with the odd threads turned off. The records cover the
exponent clamp, overflow and underflow, binary32 denormals, binary64 subnormals on both sides of the dword boundary,
zeros, infinities and NaNs. The expected words come from an oracle written from the rows of docs/design.md, checked
against math.frexp and math.ldexp in tests/test_gpu_frexp_ldexp.py.

On an RTX 4070 Ti (NVIDIA 620.70.06) six words were wrong: every NaN source under neg or abs came back as
0x7fffffff, the driver's default NaN. The binary32 branch applied the modifiers as FAbs and OpFNegate, from which
Vulkan lets a driver return any NaN. The three ops only move bits, so the branch now reads its source through
Gen.src_u_sign_mods, as the min/max family does. tests/semantic/test_alu_frexp_ldexp.py checks that the modifiers
are emitted as sign-bit ops. With that the fixture's 768 words are exact on the RTX 4070 Ti and on llvmpipe.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

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

Reviewed the stacked change on head 5f063e5. CPU verification passed: frexp/ldexp semantic tests 15/15; readback/oracle unit tests 11/11; shader-check 87/87; GPU-runner 64/64; binary64 round/cmp harness tests 14/14 and 9/9; semantic translator-use 3/3; docs design 5/5, links 16/16, repo paths 3/3, repeated lines 2/2, line endings 3/3; fixture regeneration 7/7. No-game-data scan, diff check and merge-tree to current main passed. This verifies CPU translators, expected-word oracles and runner behavior, not execution of the new GPU fixture. I could not run the GPU readback locally because llvm-mc is unavailable and the existing build cannot assemble the added shader; the RTX 4070 Ti/llvmpipe runs are author-reported. The stack includes open dependency PRs #1012 and #1034; the tests above exercised their combined diff too.

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

Follow-up to my approval: I have now run the new cs_w32_frexp_ldexp fixture locally on the RTX 3090 (NVIDIA 620.73.04), explicitly selecting the NVIDIA ICD and disabling implicit layers. Containerized LLVM 23.1.5 assembled the PR shader; the PR runner translated it and the existing Vulkan compute harness reported PASS with 768/768 words exact across 64 records, including unchanged inputs. The generated SPIR-V separately passed spirv-val --target-env vulkan1.2. The harness binary was prebuilt from upstream main 21625bca8c675c62f9dbffc318ce6a59d57faf31; this PR changes no C++ harness code, so this verifies the translated GPU fixture but is not a rebuild of current runtime C++. This supersedes my earlier note that the GPU readback was not locally run. The full GPU suite was not run.

milk4sale and others added 4 commits November 24, 2026 15:55
…undEven (GPU-ROUND-F64)

linux-gpu-tools failed this fixture with `word 164 (thread 10): rndne of 0x3fe0000000000001 gave
0x0000000000000000`, on some runners and not on others. The cause is llvmpipe, not the translation: its double
vectors are twice its native width (512 bits at the usual 256), and it has the exact Trunc and RoundEven only for a
width the CPU has a vector extension for. Without AVX-512 the two ops go through the code it has for a float, where
a magnitude above 2^24 comes back unchanged and RoundEven of a smaller one can give the other neighbouring integer.
That is 48 of the 512 results, the same 48 with Mesa 25.2.8 (LLVM 20.1.2) on a runner and with Mesa 26.2.6
(LLVM 23.1.4) under GALLIUM_OVERRIDE_CPU_CAPS=avx, sse4.1 or sse2 on a Ryzen 9 7900X; Floor and Ceil are exact on
that path, and with AVX-512 (or AVX at LP_NATIVE_VECTOR_WIDTH=128) so are all four.

On llvmpipe the judge now lets v_trunc_f64 and v_rndne_f64 return what that path can give: the source itself above
2^24, either neighbouring integer with the source's sign from 0.5 up to it. Everything else stays exact there, and no other
device gets the allowance (ROUND_F64_FLOAT_PATH, keyed like UNJUDGED_WORDS). dump_test hands the test program's
output to a judge, for its device line.

Tested: the fixture passes on llvmpipe natively and under each of the three overrides, at
LP_NATIVE_VECTOR_WIDTH=128, and on the RTX 4070 Ti (NVIDIA 620.70.06) natively and with LEONIDA_W64_EMULATE=1; the
previous judge fails under each override at word 164. tests/test_gpu_round_f64.py rebuilds the 48 measured results
and checks that nothing wider passes.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…s llvmpipe allowance

ProjectLeonida#1034 gained a commit: on llvmpipe, v_trunc_f64 and v_rndne_f64 may return what its float code gives a double on a
CPU without AVX-512. That fixture is in this stack and was this branch's red linux-gpu-tools (`word 164 (thread 10):
rndne of 0x3fe0000000000001 gave 0x0000000000000000`); cs_w32_frexp_ldexp passed in that run. No conflict.

Tested on this merge: `--filter cs_w32_` on llvmpipe (Mesa 26.2.6) natively and under
GALLIUM_OVERRIDE_CPU_CAPS=avx, 15 of 23 passed and 8 skipped in both, and 23 of 23 on the RTX 4070 Ti
(NVIDIA 620.70.06).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Brings the branch up to date with main 68d1331. No conflicts; the merged tree was tested before the push (every standalone
test file under tests/ passes except tests/test_ci_registration.py, which fails on main alone since ProjectLeonida#930).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Brings the branch up to date with main 68d1331. No conflicts; the merged tree was tested before the push (every standalone
test file under tests/ passes except tests/test_ci_registration.py, which fails on main alone since ProjectLeonida#930).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
milk4sale and others added 2 commits November 24, 2026 17:11
Brings the branch up to date with main 6558d53, which now holds ProjectLeonida#1012 (GPU-CMP-F64), the pull request this one was
stacked on. Git merged it without conflicts (git merge-tree, exit 0). GitHub had reported a conflict since ProjectLeonida#1012
merged, with the branch and main on two merge bases (68d1331 and 5040782); main is an ancestor of this commit.

The merged tree was tested before the push: every standalone test file under tests/ passes except
tests/test_ci_registration.py, which fails on main 6558d53 alone (ProjectLeonida#1365 fixes it).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…ProjectLeonida#1012 merged

Takes the updated head of ProjectLeonida#1034's branch, which holds main 6558d53 (ProjectLeonida#1012, GPU-CMP-F64, is merged there). Git merged
it without conflicts (git merge-tree, exit 0), and the tree is the one a direct merge of main gives. GitHub had
reported a conflict since ProjectLeonida#1012 merged, with the branch and main on two merge bases (68d1331 and 5040782). Merging
ProjectLeonida#1034's head instead of main leaves this branch with one merge base with main when ProjectLeonida#1034 merges.

The merged tree was tested before the push: every standalone test file under tests/ passes except
tests/test_ci_registration.py, which fails on main 6558d53 alone (ProjectLeonida#1365 fixes it).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@aslan2142
aslan2142 merged commit 2376e7d into ProjectLeonida:main Nov 24, 2026
4 of 5 checks passed
@aslan2142

Copy link
Copy Markdown
Collaborator

Merged, after #1012 and #1034. Checked locally on main e0b08c77d + 5431c75a6:

  • Claims: both valid, posted before this PR and unedited: GPU-FREXP-LDEXP (17:21 UTC) and SHADER-FREXP-LDEXP-SIGN-MODS (17:25 UTC). It is one topic: the GPU test and the one-line fix it exposed.
  • The fix is right: src_u_sign_mods clears bit 31 for abs, then flips it for neg, on the raw word, as the min/max family does. Only the binary32 source is touched; the integer exponent of v_ldexp_f32 is unchanged.
  • test_alu_frexp_ldexp.py: 8 subtests fail without the translator line; all 15 tests pass with it.
  • GPU: cs_w32_frexp_ldexp passes with 768 words exact on llvmpipe (Mesa 26.3.1) and on an RDNA2 iGPU (RADV); all 23 cs_w32_ cases pass on the iGPU.
  • Not NVIDIA-only: without the fix RADV fails too (word 419: expected 0xffc00000, got 0x7fc00000), so the neg is lost on a NaN there as well. That is worth adding to the row in docs/design.md.
  • On the merge: the semantic suite passes (475 files, 6,220 tests, 0 files failed); test_gpu_frexp_ldexp, test_run_gpu_tests, test_shader_checks pass; the 13 docs and writer tests pass; check_no_game_data.py is clean on the 9 changed files.
  • CI had not finished on this head.

AI-assisted (Claude Code, Claude Fable 5.2 and Claude Opus 5.5).

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.

3 participants