Skip to content

f64_diff: generate v_ldexp_f64, v_cvt_f64_i32 and v_cvt_f64_u32; spirv_interp converts integers to %double exactly - #6443

Open
TreXito wants to merge 8 commits into
ItsNotRexed:mainfrom
TreXito:fuzz/f64-ldexp-cvt
Open

TreXito wants to merge 8 commits into
ItsNotRexed:mainfrom
TreXito:fuzz/f64-ldexp-cvt

Conversation

@TreXito

@TreXito TreXito commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Depends on: #4536. This branch is #4536's head (5c81e056) plus one commit, so only the last commit is new. Depends on: #6403, which conflicts with #4536 and this branch in 3 small hunks (ref_program's EXEC argument); I'll rebase whichever order they land in. FUZZ-55 follow-up, claimed in #5781 (#5781 (comment)).

main translates v_ldexp_f64 (a59367f, a lowering that uses 32-bit integer ops only) and v_cvt_f64_i32/v_cvt_f64_u32 (524d1e9). No fuzzer generates them, and tools/f64_diff.py --list-ops still calls v_ldexp_f64 untranslated. This PR adds the three forms to tools/f64_diff.py, taking it from 47 to 50 forms. It also fixes tests/spirv_interp.py, which blocked the cvt forms.

Reference rules

The reference shares no code with tools/rdna2spirv.py.

Form Rule Source
v_ldexp_f64 S0 * 2^S1, with S1 a signed 32-bit word. Exact Fraction value, rounded once by MODE.FP_ROUND[3:2], subnormal results kept. ±0, ±inf and NaN (a signalling one too, IEEE=0) pass through. Toward zero, nothing rounds up and an overflow gives ±DBL_MAX the docs/design.md row (gfx1036); toward zero measured on gfx1031 in #6218
v_ldexp_f64 ... clamp NaN, -0 and negatives give +0; above 1.0 gives 1.0 same row
v_cvt_f64_i32, v_cvt_f64_u32 exact for every 32-bit integer; clamp to [0, 1]

The clamp of these three forms is judged: the fclamp-nan-zero skip no longer covers them. v_ldexp_f64 clamps with integer ops on the result words, not FClamp, and an integer converted to binary64 is never NaN or -0.

Generation

  • S0 comes from the usual sources (pairs, SGPR pairs, float inline constants, a 32-bit literal as the high dword). Half of the register pairs are refilled with a subnormal, a boundary value or a significand whose low word is all ones, so the rounding carries into the high word.
  • S1 is v16 or an inline integer. It aims at the subnormal and overflow edges of S0 (1-e, -e, -51-e to -53-e, 2046-e, each ±3), plus specials such as ±2^31 and ±4096/4097.
  • s_round_mode 0 or 12 comes before 30% of the ldexp cases.
  • The cvt sources are an inline integer, a literal, s4..s7 or v16.

MODE: a new known divergence, fixed by #6218

main's v_ldexp_f64 ignores a toward-zero MODE.FP_ROUND write. #6218 (not mine) fixes that and measured it on hardware.

Here, an ldexp result that differs between toward-zero and nearest-even is a new known divergence, ldexp-f64-mode:

On this branch merged with #6218, with every case judged (include_known=True): 69,000 programs (seeds 11-13 x 3,000 and 21-23 x 20,000, up to 3 cases each), 1,123 of them with a toward-zero-dependent ldexp result: 0 mismatch. The same seeds 11-13 on this branch alone give 71 mismatches, all inside that toward-zero set (checked per program).

tests/spirv_interp.py

A program with a cvt form declares OpCapability Float64, so it runs on tests/spirv_interp.py, not spirv_mini_interp. Two problems there:

  • Integer conversions rounded through binary32. OpConvertUToF/OpConvertSToF did this even for a %double result, so v_cvt_f64_u32 of 0xffffffff read back as 2^32. The translator's SPIR-V is correct. Now a %double result is exact and a %float result still rounds.
  • Missing integer ops. The interpreter lacked GLSL SClamp, UClamp and FindUMsb and OpSLessThan/OpSGreaterThan. The v_ldexp_f64 lowering uses them, so a program that mixed ldexp with a cvt form failed with NotImplementedError.

New tests test_integer_to_double_conversion_is_exact and test_integer_ops_of_the_ldexp_lowering cover both. Without the interpreter changes, 19 of their subtests fail. The hunks merge cleanly with #3642, which adds OpFConvert/OpFDiv next to them, and with #6218.

Tested

Linux, CPU only: the translator plus tests/spirv_interp.py. Rig: an i5-12400F with Python 3.13.5, at most 6 jobs at once.

Run Programs Result
rig, --ops v_ldexp_f64,v_cvt_f64_i32,v_cvt_f64_u32, seeds 4 to 6, --programs 20000 --max-ops 3 3 x 20,003 59,017 match, 0 mismatch, 992 skipped (all ldexp-f64-mode)
rig, all 50 forms, seeds 1 to 3, --programs 3000 --max-ops 4 3 x 3,050 5,990 match, 0 mismatch, 3,160 skipped
rig, all 50 forms, seeds 7 to 9, --programs 10000 --max-ops 4 3 x 10,050 19,490 match, 0 mismatch, 10,660 skipped
rig, #4536's head for comparison, seeds 1 to 3, --programs 3000 --max-ops 4 3 x 3,047 5,970 match, 0 mismatch
rig, every case judged (run_one(..., include_known=True)), merged with #6218, seeds 11 to 13 and 21 to 23 69,000 69,000 match
rig, the same script on this branch alone, seeds 11 to 13 9,000 8,929 match, 71 mismatch (all toward-zero cases, #6218)
rig, a separate sweep script (its own reference, not in this PR), plain and inline sources, seeds 11 to 13 and 21 to 23 6 x 2,000 12,000 match, 0 mismatch
local, --ops as above, seeds 1 and 2, --programs 400 --max-ops 3 2 x 403 791 match, 0 mismatch, 15 skipped (ldexp-f64-mode)

The full-campaign skips are the kinds #4536 already counts: interpreter limits such as OpSelectionMerge, binary64 OpFConvert/OpFDiv, FTZ, and directed rounding the translator refuses. The rate is unchanged: 65.5% of programs match here and 65.3% on #4536's head. Among them, 37 are ldexp-f64-mode skips, 9 in seeds 1 to 3 and 28 in seeds 7 to 9.

  • tests/test_f64_diff.py, tests/test_spirv_interp_f64_glsl.py: 47 passed (678 subtests). New tests:
    • ldexp and cvt tables (22 + 8 toward-zero rows, ref_program cases);
    • the known probe skipped by default and run with --known;
    • S1 reaching both edges;
    • a 150-program translator run;
    • 10 fixed translator programs, one aimed at each ldexp mutation below.
  • Every test file that imports tests/spirv_interp.py (10 files): 395 passed (730 subtests).
  • tests/test_docs_*.py: 33 passed (6 subtests).
  • Also passing: tests/test_report_paths.py, test_runtime_paths.py, test_semantic_translator_use.py, test_csv_line_endings.py, test_text_write_line_endings.py, test_tool_stdout_encoding.py, test_text_encoding.py, and compileall.
  • python -I tools/f64_diff.py --self-test: 23 reference invariants plus the planted mismatch.

Mutations

I planted eight translator mutations in tools/rdna2spirv.py. Each was checked with --ops v_ldexp_f64,v_cvt_f64_i32,v_cvt_f64_u32 --programs 1000 --seed 3 --all-failures, and against tests/test_f64_diff.py. Every mutation fails both. The unmutated copy, set up the same way, gives 0 mismatches and passes all 41 tests.

Mutation Mismatching programs Unit tests failed
v_ldexp_f64 rounds ties away from zero 2 1 subtest
S1 clamped to ±1000 instead of ±4096 107 1 test
flush to zero only from a shift of 55 (54 on main) 3 1 subtest
clamp lets -0 through 8 1 test
the neg modifier ignored 29 1 test
the rounding carry into the high word lost 1 1 subtest
v_cvt_f64_u32 converts as signed 161 1 test
v_cvt_f64_i32 converts as unsigned 135 1 test

The unmutated row is 993 match, 0 mismatch and 10 ldexp-f64-mode skips.

The lost carry needs a low result word of all ones that then rounds up. A random significand almost never gives that, and the first version of the generator missed this mutation in 1,000 programs. S0 is now also refilled with all-ones low words, and a fixed program (0x3fefffff_ffffffff * 2^-1022) catches it in the unit tests.

Docs

docs/shader_validation.md: the f64_diff section now covers the 50 forms, the ldexp/cvt rules, the new known divergence and the interpreter fix.

Not done

No --oracle gpu or --oracle hw run: I have no AMD GPU and no Vulkan binary64 device here. The rules come from the docs/design.md row and #6218's measurements.

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

🤖 Generated with Claude Code

iurysf and others added 8 commits November 23, 2026 01:18
…Rexed#2477 is on main

ItsNotRexed#2477 (48a1a98) makes the translator return signed infinity for v_div_fixup_f64
with NaN(S0), as measured on RDNA2 gfx1036, which is what the reference already
modelled. The case is no longer a known divergence (as TreXito's review of ItsNotRexed#4536
observed: two tests fail on main because they expect the old divergence).

- Remove the div-fixup-nan-s0 entry, its reference skip and its dedicated --known
  program; NaN(S0) programs are compared like any other.
- Add a positive test that the reference and the translator give +-inf for qNaN,
  -qNaN and sNaN with both output signs.
- Rework the two --known tests so they no longer depend on div_fixup: the three
  remaining forms are excluded with a reason by default, run with --known, and a
  planted clamp-only translator error exits 1 and prints the shrunk program.
- docs/shader_validation.md no longer lists the div_fixup case.

Same change as 41525870 (prepared for ItsNotRexed#4586, which ItsNotRexed#2477 made redundant).

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
…nts on main (ItsNotRexed#2477)

TreXito's review of ItsNotRexed#4536 found 40 mismatches in 24000 programs (seeds 776401 and
776402, exp(S2) - exp(S1) >= 1024): the reference predates ItsNotRexed#2477 (48a1a98),
whose measured RDNA2 gfx1036 rules are in the docs/design.md v_div_fixup row.
The translator is the side that is right. Checked against that row and against
the v_div_fixup_f64 lowering in tools/rdna2spirv.py on main, not only against the
proposed patch:

- NaN in S2 or S1 passes through unchanged (a signalling NaN is not quieted with
  IEEE=0); the priority NaN(S2) over NaN(S1) is unchanged.
- The underflow cutoff is exp(S2) - exp(S1) <= -1076 (was < -1074).
- There is no overflow check (was >= 1024).
- NaN(S0) gives signed infinity, still below the underflow case.

test_div_fixup_table is updated row by row (gap 1074/1075 pass S0, 1076 gives a
signed zero, no overflow rows, unquieted sNaN) and docs/shader_validation.md
records the rule.

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
Found while validating the ItsNotRexed#2477 follow-up on the RTX 4070: with the current
translator, `v_cmp_eq_i32_e32 vcc_lo, 0, 0; v_div_fmas_f64 v[8:9], s[4:5], -1.0, 1`
mismatched. The reference scaled by a fixed 2^64 when VCC is set; main now follows the
hardware-verified (gfx1036, 153/153 f64 stress cases) rule in the docs/design.md
v_div_fmas row: 2^+128 when exp(S2) > 1023 (|S2| >= 2.0), 2^-128 otherwise.

Same class of stale reference as the v_div_fixup_f64 one fixed in the previous
commit (TreXito's review of ItsNotRexed#4536). A table test pins both sides of the |S2| = 2.0
threshold and the VCC-clear case; reverting to the fixed 2^64 fails it.

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
The reviewer's `--oracle gpu --ops v_div_fmas_f64 --programs 300 --seed 1` run had one
mismatch: `v_div_fmas_f64 v[8:9], 16, 0xa5126aa7ea6efc1f, 0` (VCC clear), reference -0,
GPU +0. Diagnosis (RTX 4070):

- IEEE is -0: the exact result is a negative nonzero that underflows, and the C library
  fma and the reference agree. Denormal operands are preserved by the GPU, so it is not
  operand flush-to-zero.
- The translator emits a plain GLSL `Fma` for that path (SPIR-V checked), and adding
  SignedZeroInfNanPreserve/DenormPreserve 64 does not change the GPU result.
- The same `Fma(2^-600, -2^-600, +0)` on runtime operands (a FragCoord-derived shader)
  returns -0 on the GPU. Only an all-constant `Fma` loses the zero sign, which points at
  the driver folding it. The fuzz shaders are all constants.

So neither the reference nor the translator is wrong. A GPU result that differs from
the reference only in the sign of such a zero (an underflowing Fma against an
opposite-signed zero addend) is now a counted skip with that reason; any other
difference still fails. ref_program reports the case through an optional notes list.
Tests pin the flag, the skip, and that a flip without the underflow, or with another
differing word, stays a mismatch (four mutations fail them). docs/shader_validation.md
records the diagnosis.

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
…tests

origin/main already carries tools/f64_diff.py at 7bd5f01 plus two small maintainer
edits (the stdout reconfigure guard in tools/f64_diff.py, and the two --known tests
adapted to a translator that matches NaN(S0)). This branch drops div-fixup-nan-s0
from KNOWN_DIVERGENCES and rewrites those two tests, so the merge keeps this branch's
versions of them and main's stdout guard.

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
…-sign skip vendor-neutral

Review follow-ups on ItsNotRexed#4536.

abnalstlll-byte ran `--oracle hw` on a gfx1036 and got 1 mismatch ("word 1: expected
00000001 ffffffff, actual 00000001 00000001"). The hardware kernel runs one thread of a
wave32, so EXEC starts as 1: a v_cmpx result read back through exec_lo is 1 or 0 there,
while the translator and Vulkan oracles show the full 32-lane mask. The reference
assumed 32 lanes for every oracle. ref_program now takes the starting EXEC mask and
run_one passes HW_LANE_MASK (1) for --oracle hw; the other oracles keep 0xffffffff. A
fake one-lane machine reproduces the reported mismatch with the old assumption and
matches with the new one. Not confirmable here (no AMD GPU): that the same one-lane
reading is all that differs on the real device.

memorypressure noted that the constant-folded Fma zero-sign exclusion applies to every
--oracle gpu run while its reason and the doc named one vendor. The reason is now
vendor-neutral, the doc says it was observed on an NVIDIA RTX 4070 only, and a test
pins that --oracle hw never uses it.

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
…d v_sqrt_f64

After merging current main, CPU seeds 1-3 and `--programs 3000 --seed 9` reported
mismatches on v_frexp_mant_f64, v_frexp_exp_i32_f64, v_rsq_f64 and v_sqrt_f64 only,
for example `v_frexp_mant_f64 v[8:9], 0x3470fbc87309e9cc` (expected 7309e9cc 3fe0fbc8,
actual 00000000 3fe9e9cc), and `--oracle gpu --programs 300 --seed 1` also reported
v_rcp_f64 (the CPU run skips it: no OpFDiv in the interpreter). Main's translator now
reads the literal of those forms as the upper half of the f64 operand and takes only its
low 32 bits, which is the ISA rule for a 32-bit literal (measured on gfx1036); the
generator printed a full-width text literal for them. They now get 32-bit literals like
the rounding, add, compare and class forms, so the listings are also valid assembler
input for --oracle hw. v_div_fmas_f64 and v_div_fixup_f64 keep full-width literals:
v_div_fixup_f64 reads a printed 32-bit literal as the low word on main (a 32-bit literal
there mismatches the ISA rule; reported, not worked around).

Checked on origin/main 561b65a: CPU seeds 1-5 and 7 clean, `--programs 3000 --seed 9`
2737 match / 0 mismatch, a 4000-program run of the affected forms (seed 11) 1980 match /
0 mismatch, and on the RTX 4070 `--oracle gpu` seed 1 (300 programs) 331 / 0 and
`--programs 1500 --seed 9` 1460 / 0. A test pins the literal width per form.

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
main translates all three (a59367f, 524d1e9), but no fuzzer generated
them and --list-ops still called v_ldexp_f64 untranslated. 47 -> 50 forms.

- Reference: v_ldexp_f64 is the exact S0 * 2^S1 rounded once by
  MODE.FP_ROUND[3:2] (subnormals kept, zeros/infinities/NaNs pass, toward
  zero never rounds up and overflows to DBL_MAX); the integer conversions
  are exact. Their clamp is judged: ldexp clamps with integer ops and an
  integer is never NaN or -0.
- Generation: S1 aimed at the subnormal and overflow edges, S0 refilled
  with subnormals, boundaries and all-ones low words (rounding carry), and
  s_round_mode 0/12 before 30% of the ldexp cases.
- Known divergence ldexp-f64-mode: main ignores a toward-zero MODE write
  in v_ldexp_f64 (ItsNotRexed#6218 fixes it); such results are counted skips, and the
  --known probe (DBL_MAX * 2) mismatches until ItsNotRexed#6218 lands.
- tests/spirv_interp.py: OpConvertUToF/OpConvertSToF to %double are exact
  (they rounded through binary32: v_cvt_f64_u32 0xffffffff read 2^32), and
  SClamp, UClamp, FindUMsb, OpSLessThan and OpSGreaterThan exist, which
  the integer-only ldexp lowering needs once a module has a %double op.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

2 participants