Skip to content

tools: differential fuzzer for v_div_fixup_f16/f32/f64 (div_fixup_diff.py) - #5086

Open
TreXito wants to merge 2 commits into
ItsNotRexed:mainfrom
TreXito:fuzz/div-fixup-diff
Open

TreXito wants to merge 2 commits into
ItsNotRexed:mainfrom
TreXito:fuzz/div-fixup-diff

Conversation

@TreXito

@TreXito TreXito commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claim: #4933 (comment)

What and why

No fuzzer generates v_div_fixup_f16 or v_div_fixup_f32. v_div_fixup_f64 is one of the 47 forms in #4536 (tools/f64_diff.py, open). The three lowerings are hand-written bit logic: an eight-rule priority order, an exponent cutoff on biased fields, f32 denormal handling that differs by operand, op_sel halves on f16 and output modifiers on f32. The semantic tests check chosen words with VGPR sources.

tools/div_fixup_diff.py (new) runs seeded programs through rdna2spirv and spirv_mini_interp and compares the four exported words bit for bit with its own model. The model works on the operand bits and shares no code with the translator.

Reference (the docs/design.md rows v_div_fixup_f32 and v_div_fixup_f16, v_div_fixup_f64, measured on gfx1036)

  • abs then neg act on the sign bit, NaNs included.
  • f32 reads an S1 or S2 denormal as a signed zero and keeps an S0 denormal. f16 and f64 keep every denormal.
  • First rule that holds: NaN(S2), NaN(S1), 0/0 or inf/inf, zero, inf, the exponent cutoff (-151 on f32, -1076 on f64, none on f16), NaN(S0), S0's magnitude.
  • f32: mul:2/mul:4/div:2, then clamp, which takes a NaN or any negative value to +0.

tests/test_div_fixup_diff.py pins the model to all 73 measured rows of tests/semantic/test_alu_div_fixup_f32.py and to the f16/f64 rows of test_alu_div_fixup.py, the measured cutoffs among them.

Generated

  • Sources: VGPR, SGPR, literal, integer and float inline constants, within the GFX10 literal and constant-bus limits.
  • f64 sources: VGPR pairs (odd first registers too), SGPR pairs, sign-extended integer inline constants, and 0.15915494309189532 as the exact binary64 1/(2 pi).
  • neg/abs on every register source, op_sel on f16 sources and the destination half, clamp on f16/f32, output multipliers on f32.
  • Programs chain: later ops read earlier results, f64 halves as f32 or f16 values included.

Not generated: a literal on f64 (#3791 moves it to bits 63:32), output multipliers on f16 (SHADER-297), any output modifier on f64 (needs Float64, which the interpreter does not run).

Not judged (counted with the reason):

  • an f64 cutoff that depends on how a denormal's exponent counts (only normal operands were measured; the grid has 8 such cases);
  • an output multiplier on -0 without clamp (the hardware gives +0, listed as not modelled) or on an f32 denormal;
  • clamp of a positive f32 denormal, an f16 -0 or an f16 NaN (FClamp of a NaN is undefined).

Stages: a sweep of every encodable source-kind combination (279), an edge grid (3,585 cases in 1,155 programs: rule order, cutoff at -1/0/+1 also against a NaN or inf S0, NaN payloads under abs/neg, output modifiers on each rule's result), then random programs. Each stage yields its programs one at a time, so memory does not grow with --programs (4f5e641, from review).

Tested

Run Result
Seeds 911001 to 911008, --programs 10000 --max-ops 8 (each with the sweep and grid: 11,713 programs) 76,112 of 76,112 judged match, 0 skipped, exit 0 each
--ops with one op, --programs 5000 --max-ops 8 --seed 77 f16 4,360, f32 3,967, f64 5,593 judged, all match
--programs 300 --seed 1 (sweep, grid, random: 2,013 programs) 1,901 of 1,901 judged match, 0 skipped
--self-test 16 checks ok
python -I tests/test_div_fixup_diff.py 22 tests OK (4f5e641)
the 10 tests/test_docs_*.py, tests/test_text_write_line_endings.py OK

Mutations of the translator's lowerings (--programs 300 --seed 1; every mutant exits 1, the unmutated translator exits 0):

Mutation Mismatching programs
A: f32 cutoff at -150 instead of -151 4
B: f32 NaN(S1) checked before NaN(S2) 14
C: f32 S0 denormal flushed 22
D: f32 clamp without the NaN-to-+0 select 0, 92 skipped
E: f32 output multiplier after clamp 24
F: f32 S1 denormal not read as zero 102
G: f16 op_sel ignored on S2 53
H: f16 high-half destination drops the low half 133
I: f16 0/0 gives 0x7e00, not 0xfe00 33
J: f16 float inline constant read as its f32 word 170
K: f64 cutoff at -1075 instead of -1076 8
L: f64 abs clears bit 31 of the low word 126
M: f64 NaN(S0) does not give inf 47
N: f64 integer inline constant zero-extended 38

D shows as skips, not mismatches: without the NaN select, the interpreter refuses FClamp of a NaN, and a skip exits 1.

Setup: CPU only, no GPU needed.

  • Linux 7.0 with Python 3.14.4, and the long runs on Linux 6.18 with Python 3.13.5.
  • On main 3faac55.

Docs: a subsection in docs/shader_validation.md, placed away from the one #5029 adds so the two merge cleanly.

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

🤖 Generated with Claude Code

No fuzzer generated the three div_fixup ops. tools/div_fixup_diff.py runs
seeded programs through rdna2spirv and spirv_mini_interp against a bitwise
model of the docs/design.md rows (gfx1036 measurements) and compares the
exported words bit for bit.

- Sources: VGPRs, SGPRs, a literal (not on f64), inline constants; f64
  VGPR pairs (odd ones too) and SGPR pairs; abs/neg, f16 op_sel, clamp,
  f32 mul:2/mul:4/div:2.
- Stages: every encodable source-kind combination, an edge grid (rule
  order, the -151/-1076 exponent cutoffs, NaN payloads under abs/neg,
  output modifiers on each rule's result), random chained programs.
- tests/test_div_fixup_diff.py pins the model to the 73 measured f32
  rows and the f16/f64 rows in tests/semantic.

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

shdann commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

run_suite keeps every generated program in items before it starts checking the first one. The consumer, _run, makes one forward pass. Large --programs values therefore retain source text for the entire run.

I measured peak Python allocations during generation on Windows with CPython 3.14 at 66ab513b. I used seed 1 and the default six-op limit. I replaced _run with a consumer that feeds repr(item).encode() to SHA-256 in order. I started tracemalloc after imports. These figures exclude translation.

Programs Current list Generator
300 465,609 bytes 14,186 bytes
1,200 1,854,785 bytes 15,207 bytes

The hashes match at both sizes. The generator uses the same seeded loop and yields each (label, lines, used) tuple directly to _run. With the real translator and interpreter, the two versions also return the same counters and first-failure result for 30 programs. All 21 PR tests pass.

AI-assisted.

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

Ran this at 66ab513b5 on Windows 11: pytest tests/test_div_fixup_diff.py -q -> 21 passed, 118 subtests passed; git diff --check clean.

The new tools/div_fixup_diff.py differentially fuzzes v_div_fixup_f16/f32/f64 against the reference model, including the NaN/inf/zero-sign cases and the f16 rounding path; the 299-line test suite covers the op table, the accepted forms and the mismatch reporting. I cannot run the hardware oracle here. No blocking issue.

@Atomic258

Copy link
Copy Markdown
Contributor

Independent check — pass (CPU only, no game files).

Environment: Windows 11, CPython 3.15.2.

  • python -I tests/test_div_fixup_diff.py → 21/21 on the PR head
  • compileall clean on tools/div_fixup_diff.py
  • No parent A/B: the fuzzer is new in this PR; the suite exercises its model/differential paths
  • docs/shader_validation.md documents it as required for a new fuzz family

AI-assisted (Devin).

…uilding the whole list

run_sweep, grid_items and run_suite built every (label, lines, ops) tuple before _run
checked the first, so peak memory grew with --programs. sweep_items, grid_items and
suite_items now yield one at a time. Same seeded programs (SHA-256 of every program
matches), same output for --programs 300 --seed 1. Generation-only peak at 5,000
programs: 7,715,187 -> 28,026 bytes. From shdann's review.

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

TreXito commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, shdann. Confirmed, and fixed in 4f5e641.

Change. sweep_items, grid_items and suite_items now yield each (label, lines, ops) tuple, and _run checks it before the next one is made. All three stages had the same list pattern, not just run_suite.

Same programs. I hashed every generated program, as you did, with run_one replaced by a SHA-256 consumer.

Programs (seed 1) Peak before Peak after Hash
300 478,092 bytes 27,050 bytes same
1,200 1,866,865 bytes 28,026 bytes same
5,000 7,715,187 bytes 28,026 bytes same

These figures are tracemalloc peaks during generation, without translation.

New test. test_each_program_is_checked_before_the_next_is_made counts the programs made at each run_one call across the suite, sweep and grid. With the fix the counts are 1, 2, 3, .... On 66ab513 they are [5, 5, 5, 5, 5, 221, ...], so the test fails there.

Re-run on 4f5e641

Run Result
python -I tests/test_div_fixup_diff.py 22 tests OK
--programs 300 --seed 1 same output as on 66ab513: 1,901 of 1,901 judged match, same per-op and per-rule counts
the 10 tests/test_docs_*.py, tests/test_text_write_line_endings.py OK

Environment: Linux 7.0, Python 3.14.4, CPU only.

#5029 (f16misc_diff) had the same pattern, including a 49,152-program --exhaustive-f16 list. I'm fixing it there too.

AI-assisted (Claude Code).

TreXito added a commit to TreXito/Leonida that referenced this pull request Nov 23, 2026
…lding the whole list

run_sweep, run_grid, run_exhaustive and run_suite built every program before _run checked
the first, so peak memory grew with --programs and --exhaustive-f16 held all 49,152
programs. sweep_items, grid_items, suite_items and exhaustive_programs now yield one at a
time. Same programs (SHA-256 of every program in all four stages matches), same output for
--programs 300 --seed 2. Generation-only peak: --exhaustive-f16 64,279,176 -> 305,573
bytes, 3,000 random programs 5,092,014 -> 310,551. Same pattern shdann found in ItsNotRexed#5086.

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

Copy link
Copy Markdown

Independent Windows check of 4f5e641f with CPython 3.12.8, using the published patch on its base 3faac557.

python -B -I tests/test_div_fixup_diff.py passed all 22 tests. I also compared generation with 66ab513b on the same interpreter at seed 1 and the default six-op limit. A SHA-256 consumer replaced _run and hashed each (label, lines, ops) tuple in order. Every count and digest matched.

Stage Programs Before peak (bytes) After peak (bytes)
Sweep 558 707,435 10,779
Edge grid 1,155 3,264,200 352,174
Random 300 437,737 7,866
Random 1,200 1,761,073 8,011

The tracemalloc figures cover generation and the streaming consumer, with translation excluded. CPU only, using synthetic programs. AI-assisted.

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 4f5e641 on Windows Server 2022, CPython 3.13.12: pass

  • python -I tests/test_div_fixup_diff.py -> Ran 22 tests, OK
  • New tool - removing div_fixup_diff.py makes the suite fail to import, as expected.

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

TreXito added a commit to TreXito/Leonida that referenced this pull request Nov 23, 2026
…lding the whole list

run_sweep, run_grid, run_exhaustive and run_suite built every program before _run checked
the first, so peak memory grew with --programs and --exhaustive-f16 held all 49,152
programs. sweep_items, grid_items, suite_items and exhaustive_programs now yield one at a
time. Same programs (SHA-256 of every program in all four stages matches), same output for
--programs 300 --seed 2. Generation-only peak: --exhaustive-f16 64,279,176 -> 305,573
bytes, 3,000 random programs 5,092,014 -> 310,551. Same pattern shdann found in ItsNotRexed#5086.

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.

5 participants