Skip to content

FUZZ-3: float VALU differential fuzzer; v_mac_f32 unfused, VOP3 output modifiers on conversions - #394

Merged
ItsNotRexed merged 4 commits into
ProjectLeonida:mainfrom
tomgamer22:fuzz-3-f32-valu
Nov 22, 2026
Merged

ItsNotRexed merged 4 commits into
ProjectLeonida:mainfrom
tomgamer22:fuzz-3-f32-valu

Conversation

@tomgamer22

@tomgamer22 tomgamer22 commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Item: FUZZ-3 (32-bit float VALU), claimed in #156; findings posted there before fixing (comment).

What this adds

tools/f32_fuzz.py: a differential fuzzer for the translator's 32-bit float VALU. Seeded VOP1/VOP2/VOP3 programs (with neg/abs source modifiers, clamp and mul:2/mul:4/div:2) run through two independent paths, and their results are compared bit-exact:

  • an RDNA2 ISA model with exact rational arithmetic and one round-to-nearest-even per operation (it shares no code with the translator);
  • tools/rdna2spirv.py, whose SPIR-V output is then run by tests/semantic/spirv_mini_interp.py.

It covers 36 ops: add/sub/subrev/mul, legacy mul/mad, mad/mac/fma/fmac/fmaak/madak, v_cvt_f32_{i32,u32,ubyte0..3,f16}, v_cvt_f16_f32, v_cvt_pkrtz_f16_f32, v_cvt_pk_u8_f32, floor/ceil/trunc/rndne, rcp/rsq/sqrt/exp/log (exact-result inputs only), the cube ops and v_cmp_class_f32. Three kinds of input are counted, never compared:

  • f32 denormals (the MODE question, SHADER-35);
  • clamp of NaN or -0 (@shelbykb2's claim);
  • inputs whose emitted SPIR-V is undefined. These form their own class, "undefined in SPIR-V".

--self-test checks the harness can both pass and fail.

Translator fixes it found

  1. v_mac_f32 was emitted as Fma. It is the VOP2 form of v_mad_f32, so the product is rounded before the add. LLVM SIInstrInfo.cpp getNewFMAInst maps V_MAC_F32 to V_MAD_F32_e64 and V_FMAC_F32 to V_FMA_F32_e64. It now lowers like v_mad_f32 (OpFMul + OpFAdd, both NoContraction). Example: 0xbc500c59 × 0x435a2099 + 0x3e3db015 gives 0xc0256a01; fused gives 0xc0256a00.
  2. VOP3 clamp and output modifiers were dropped on v_cvt_f32_i32/u32, v_cvt_f32_ubyte0..3, v_cvt_f32_f16 and v_rcp_iflag_f32. LLVM's VOPProfileI2F has Ins64 = $src0, $clamp, $omod, and the float-source profiles have both. These handlers now call clamp_mods.

Not in this PR: v_med3_f32 (#350) and v_cvt_f32_f64 (#380) are fixed elsewhere. Still open, for discussion in #156: v_rcp_f32(±0), v_rsq/v_log of ±0 or a negative, v_sqrt of a negative and v_cvt_pk_u8_f32(NaN) emit SPIR-V that is undefined on inputs where the ISA defines the result (recorded in the new docs/design.md row).

Interpreter (test infrastructure)

  • RoundEven lost the sign of a zero result (Python's round() returns an int), so roundEven(-0.4) gave +0.
  • Fma rounded through a double. It is now exact: (1 + 2⁻¹²)² + 2⁻⁷⁰ lands on a binary32 midpoint, and the double path gave 0x3f801000 instead of 0x3f801001. That is the trap docs/shader_validation.md warns about.
  • New: OpFDiv, OpIsNan, OpIsInf, Floor/Ceil/Trunc/Exp2/Log2/Sqrt/InverseSqrt. Each raises UnsupportedConstruct where SPIR-V or GLSL.std.450 leave the result undefined, like the existing ops.

Tests

  • runtime/test/shaders/ps_f32_mods.asm: one word per rule, wired into check.py, run_gpu_tests.py and the semantic fixtures. Each missing modifier gives a different word.
  • tests/semantic/test_f32_valu.py: the fixture against hand-written constants, cross-checked by a plain-Python reference, plus interpreter tests against GLSL.std.450.
  • tests/test_f32_fuzz.py: model spot checks, harness canaries, and a seeded 400-program sweep that must have no mismatch.
  • Negative control: on the unfixed translator the new tests fail (3 fixture words, 5 fuzz-harness tests).

Run on Windows 11, Python 3.11, rebased on d7cbc3e:

  • python -m unittest discover -s tests/semantic: 450 OK.
  • python -m pytest tests --ignore=tests/semantic: 1188 passed, 160 skipped.
  • The CI loop over the standalone tests/test_*.py: all pass.
  • regen_fixtures.py --check and compileall: OK.
  • Fuzzer: 13,000 seeded programs after the fix, 0 mismatches.

GPU-tested by @Pipodi on ed6da6b: NVIDIA GeForce RTX 4090, driver 617.42, Windows 11 (build 26300): python scripts/run_gpu_tests.py --filter ps_f32_mods passes, spirv-val 1/1, 4096/4096 pixels exact (comment). I did not run it myself (this machine has no llvm-mc/SPIR-V tools or MSVC); the expected words come from the ISA and LLVM references above.

Other open PRs touch the same files: runtime/test/shaders/check.py, scripts/run_gpu_tests.py and tests/semantic/regen_fixtures.py (one added line each), and #350 and #331 edit nearby lines of the multiply-add block in tools/rdna2spirv.py.

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

🤖 Generated with Claude Code

… output modifiers

tools/f32_fuzz.py runs seeded VOP1/VOP2/VOP3 float programs (neg/abs, clamp,
omod) through an exact-arithmetic RDNA2 model and through the translated
SPIR-V in tests/semantic's interpreter, and compares them bit-exact.
Denormals, and inputs the emitted SPIR-V leaves undefined, are counted
separately instead of being compared.

Translator fixes it found:
- v_mac_f32 is the VOP2 form of v_mad_f32 (product rounded before the add),
  not of v_fma_f32 (LLVM getNewFMAInst: V_MAC_F32 -> V_MAD_F32_e64). It
  emitted Fma.
- VOP3 clamp and mul:2/mul:4/div:2 were dropped on v_cvt_f32_i32/u32,
  v_cvt_f32_ubyte0..3, v_cvt_f32_f16 and v_rcp_iflag_f32 (LLVM
  VOPProfileI2F: $src0, $clamp, $omod).

Interpreter: RoundEven keeps the sign of a zero result; Fma rounds once
(it double-rounded through a double); adds OpFDiv, OpIsNan, OpIsInf,
Floor/Ceil/Trunc/Exp2/Log2/Sqrt/InverseSqrt, raising where SPIR-V or
GLSL.std.450 leave the result undefined.

Pinned by runtime/test/shaders/ps_f32_mods.asm (check.py, run_gpu_tests.py,
tests/semantic/test_f32_valu.py) and tests/test_f32_fuzz.py.

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

Pipodi commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Tested ed6da6b on NVIDIA GeForce RTX 4090, driver 617.42, Windows 11 (build 26300): pass.

python scripts/run_gpu_tests.py --filter ps_f32_mods (llvm-mc from LLVM 23.1.5): the shader translates and passes spirv-val (1/1), and ps_f32_mods gives 4096/4096 pixels exact.

AI-assisted (Claude Code).

@Aminecsmc

Aminecsmc commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Tested ed6da6b on NVIDIA GeForce RTX 3050 Laptop GPU (Ampere, subgroup 32), driver 610.91, Windows 11 build 26300: pass. AI-assisted (Claude Code).

  • run_gpu_tests.py: 62/62 translated, 62/62 pass spirv-val. 35/59 pass, 24 skipped (wave64), 0 fail. ps_f32_mods PASS, 4096/4096 pixels exact.
  • unittest discover -s tests/semantic: 450 OK.
  • tools/f32_fuzz.py --self-test: "canaries: model checks ok, match ok, mismatch detected".
  • tools/f32_fuzz.py --programs 300 with seeds 1, 2 and 3: exit 0, 0 mismatches. Of 900 programs, 841 match, 25 are undefined in SPIR-V and 34 are skipped (for example denormal results, cube of NaN, clamp of NaN or -0).
  • Note: it conflicts with main as of 4abd545 (docs/design.md, regen_fixtures.py, spirv_mini_interp.py, tools/rdna2spirv.py).

Toolchain: llvm-mc 21.1.8 (built with scripts/build_llvm_mc.ps1), SPIRV-Tools v2026.4.

Edit: the fuzzer line first said that the only non-compared cases were "undefined in SPIR-V". That missed the 34 skipped programs. The exit code and mismatch count I first posted had not been read from the fuzzer itself (I had tail's exit code), so I re-ran all three seeds at ed6da6b. The numbers above are from that run.

Keep both sides in docs/design.md (contract rows), regen_fixtures.py
(ps_f32_mods + ps_fma_literal), spirv_mini_interp.py (our OpFDiv,
Floor/Ceil/Trunc, Exp2/Log2/Sqrt/InverseSqrt and RoundEven sign fix;
upstream's OpIsNan/OpIsInf, OpConvertUToF/SToF and FMax/FClamp) and
rdna2spirv.py (upstream fmamk/fmaak fix; v_mac_f32 stays with v_mad_f32).

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

Copy link
Copy Markdown
Contributor Author

Merged main into the branch to resolve conflicts (8a6deb1, no force push). The GPU passes above (@Pipodi, RTX 4090; @Aminecsmc, RTX 3050 Laptop) were on ed6da6b. The merge brings in only main's changes:

  • docs/design.md and regen_fixtures.py: both sides kept.
  • The interpreter: our duplicate OpIsNan/OpIsInf and OpConvertUToF/OpConvertSToF were dropped in favour of the copies already on main.
  • rdna2spirv.py: main's fmamk/fmaak fix is kept, and v_mac_f32 stays with the unfused v_mad_f32.

After the merge: semantic unittest 472 OK, pytest 1769 passed / 178 skipped, tests/test_f32_fuzz.py OK, f32_fuzz.py --programs 3000: 0 mismatches. AI-assisted (Claude Code).

@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 8a6deb1ee1375fdf6166e3b1ab8dee8668ad148e on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Verified both sides of the multiply-add translator merge on Ubuntu 26.04.1 LTS. I built the runtime from this head and ran each fixture with a separate empty work directory. Both shaders passed SPIR-V validation. A separate comparison of the raw framebuffer matched all four expected words at every pixel in each draw.

Fixture Observed RGBA words (hex) Exact pixels
ps_f32_mods c0256a01 3f800000 41e00000 3fc00000 4,096/4,096
ps_fma_literal 41b80000 41800000 40e00000 41a40000 4,096/4,096

The first draw confirms the fixture’s unfused MAC result and conversion output modifiers. The second confirms the literal multiply-add cases retained from main. Coverage was limited to these two synthetic fixtures.

AI-assisted verification.

@esteban-dcp

Copy link
Copy Markdown

Tested 8a6deb1 on two GPUs, Linux 7.3.5 (CachyOS): pass.

  • NVIDIA RTX 4050 Laptop (Ada), driver 620.73.04, subgroup 32: 70 shaders: 70 translated, 70 pass spirv-val; 41/67 passed, 26 wave64 skipped; with LEONIDA_W64_EMULATE=1: 67/67 passed
  • AMD Radeon 740M (RDNA3, RADV, Mesa 26.3.1), subgroup 32..64: 67/67 passed
  • New ps_f32_mods: 4096/4096 pixels exact on all three runs
  • CPU: unittest discover -s tests/semantic 472 OK; tests/test_f32_fuzz.py OK

Ran python -I scripts/run_gpu_tests.py --build runtime/build/linux on the PR head as-is, with the test programs built from this branch, VK_LOADER_DRIVERS_SELECT pinning each GPU (device line checked in each run) and VK_LOADER_LAYERS_DISABLE=~implicit~. 0 failures.

spirv_mini_interp.py: keep main's _F32 (sNaN bits, #533) and its vector-aware
RoundEven/Floor/Ceil/Trunc; keep this branch's fma_f32, OpFDiv and
Exp2/Log2/Sqrt/InverseSqrt. regen_fixtures.py: both sides' shaders.

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

Copy link
Copy Markdown
Contributor Author

Updated onto main b8f6c74 (8521b0b, a merge with no force-push). In spirv_mini_interp.py I kept main's _F32 sNaN handling (#533) and its vector-aware RoundEven/Floor/Ceil/Trunc, plus this PR's fma_f32, OpFDiv and Exp2/Log2/Sqrt/InverseSqrt. Semantic suite OK, test_f32_fuzz.py OK, regen_fixtures.py --check OK, f32_fuzz.py --programs 2000: 0 mismatches. If you would rather have a single rebased commit, say so and I will squash it. AI-assisted (Claude Code).

…2_mods and ps_f64_mods)

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

Copy link
Copy Markdown
Contributor

Tested b3688332f2f6f6c1654991222dc60da58218bc90 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

Rebuilt the runtime at the rebased head and replayed the two synthetic pixel shaders from the earlier RTX 5090 check. Each shader translated and passed spirv-val. I checked the four raw 32-bit RGBA words at every pixel against the expected tuple.

Fixture RGBA words (hex) Exact pixels
ps_f32_mods c0256a01 3f800000 41e00000 3fc00000 4096/4096
ps_fma_literal 41b80000 41800000 40e00000 41a40000 4096/4096

Both fixtures retained their earlier bit-exact results after integration with c432cdc. Coverage here is limited to the two GPU draws. Earlier RTX 5090 readback at 8a6deb1

AI-assisted verification.

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.

6 participants