Skip to content

rdna2spirv: translate v_div_fixup_f32 from RDNA2 measurements - #2898

Open
evilclanker9000 wants to merge 6 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/f32-div-fixup
Open

evilclanker9000 wants to merge 6 commits into
ItsNotRexed:mainfrom
evilclanker9000:claude/f32-div-fixup

Conversation

@evilclanker9000

@evilclanker9000 evilclanker9000 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: EGEN-F32-DIV-FIXUP (claim comment)

Update: the first measurements used the oracle's default kernels, which run with IEEE on. I re-ran all cases as raw kernels in the game's MODE (IEEE off, DX10_CLAMP on, f32 denormals flushed). There omod scales the result before clamp (a zero or denormal result becomes +0, a NaN is kept), and a signalling NaN passes unchanged. The translation, fixture words, reference and design.md row now follow the game's MODE, and they match all re-measured cases in the interpreter (0 mismatches). GPU tests were re-run: 64/64 on both the RTX 5080 and the Radeon. With IEEE on, omod is ignored and sNaNs are quieted; that mode is documented, not translated.

What and why

v_div_fixup_f32 stopped translation with Unsupported. It is the last step of LLVM's IEEE float division, after v_div_scale_f32 (#2777) and v_div_fmas_f32 (#2441). #2477 implements the f16 and f64 forms, but not f32. #2096 only adds a standalone interpreter test.

The rule comes from native RDNA2 measurements (tools/hw_oracle on the gfx1036 iGPU, 5,072 operand triples including abs/neg/clamp/omod, plus the fixture cases in wave32 and wave64). They were taken in the game's MODE, with f32 denormals flushed. S0 is the quotient, S1 the denominator and S2 the numerator. abs/neg act on the sign bits first. A denormal S1 or S2 reads as a zero of its sign, but S0 is not flushed. With s = sign(S1) xor sign(S2), the first match wins:

case D
S2 NaN S2 unchanged (IEEE off)
S1 NaN S1 unchanged
S1, S2 both zero or both inf 0xffc00000
S1 zero or S2 inf inf with sign s
S1 inf or S2 zero zero with sign s
exp(S2) - exp(S1) < -150 zero with sign s
S0 NaN inf with sign s (not in the ISA pseudocode)
otherwise |S0| with sign s, a denormal S0 too

clamp gives +0 for NaN, zeros and negatives, and 1.0 from 1.0 up. omod scales the result before clamp (IEEE off; ignored with IEEE on). The translation uses integer operations only. v_div_fmas_f32 is left to #2441.

  • runtime/test/shaders/cs_div_fixup_f32.asm: compute dump of 20 cases, with each expected word derived by hand in the header. It covers every row of the table, the -150 boundary from both sides, the denormal numerator and quotient, abs/neg, clamp and mul:2 (applied: IEEE off).
  • tests/semantic/test_value_semantics.py DivFixupF32: a plain-Python reference gives the 20 words, and the translated fixture produces them in the interpreter.
  • docs/design.md: a contract row.

Tested

  • All 5,072 measured cases were translated one per program and run in tests/semantic/spirv_mini_interp.py: 0 mismatches against hardware. The 20 fixture cases gave the same words on the oracle in wave32 and wave64.
  • scripts/run_gpu_tests.py --filter div_fixup: cs_div_fixup_f32 gives 64/64 dumped words exact and passes spirv-val, on NVIDIA GeForce RTX 5080 (driver 616.92) and AMD Radeon RDNA2 iGPU (Ryzen 9 9950X3D, driver 26.10.1), Windows 11.
  • Red on main: Unsupported: v_div_fixup_f32.
  • Negative control: returning a signed zero instead of inf for a NaN quotient (one line) fails on both GPUs (word 13: expected 0xff800000, got 0x80000000) and in DivFixupF32. Reverted.
  • python -m unittest discover -s tests/semantic, python tests/semantic/regen_fixtures.py --check, python -I tests/test_shader_checks.py, compileall: OK. This PR and rdna2spirv: translate v_div_scale_f64 from RDNA2 measurements #2703 / rdna2spirv: translate v_div_scale_f32 from RDNA2 measurements #2777 each change TOTAL in tests/test_shader_checks.py from 11 to 12, so the later ones need it raised.

AI-assisted.

Checklist

  • 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.

v_div_fixup_f32 stopped translation. The rule is measured natively on
RDNA2 gfx1036 with tools/hw_oracle (5,072 operand triples with
modifiers) in the game's MODE with f32 denormals flushed. S0 is not
flushed, and a NaN quotient gives a signed inf. Translated with integer
operations.

Adds the cs_div_fixup_f32 GPU compute dump (20 cases, words derived by
hand), a semantic test with a plain-Python reference and the
docs/design.md contract row.

AI-assisted.
# Conflicts:
#	docs/design.md
#	tools/rdna2spirv.py
@bobcatchris

Copy link
Copy Markdown
Contributor

Tested b2d3f51 on Apple M2 (Mac14,3), macOS 26.4 (25E246), MoltenVK 1.4.2 (direct), subgroup 4..32, llvm-mc 23.1.6, Python 3.13.18: pass.

python -I scripts/run_gpu_tests.py --filter div_fixup --build runtime/build/linux: 1 shader translated, 1 pass spirv-val; 1/1 passed — cs_div_fixup_f32 64/64 dumped words exact. Adds an Apple/MoltenVK data point to the NVIDIA and AMD results in the PR body.

Build note: the full build fails on AppleClang 17 at vk_pipeline_check.cpp (the floating-point from_chars Mac build break reported on the live coordination board); all GPU binaries built, so the run above is valid. Head fetched unchanged into a separate worktree; no rebase/edit/push. Synthetic shaders, counts only; no game files.

AI-assisted.

@tupacfan04

Copy link
Copy Markdown
Contributor

Code read of b2d3f51f (I didn't run it). The OpSelect chain implements the docstring's table in the right priority. It is applied lowest-first (S0 NaN, tiny, S1 inf/S2 zero, S1 zero/S2 inf, 0/0 or inf/inf, S1 NaN, S2 NaN), so the last matching rule wins, which equals "first match wins" read top-down. Details I checked:

  • e2 + 150 < e1 is e2 − e1 < −150 without unsigned wrap, since exponents are ≤ 255.
  • Zero detection on the biased exponent treats S1/S2 denormals as signed zeros, while S0's magnitude is taken as is, which matches "S0 is not flushed".
  • NaN results set bit 22 on the original word (sign and payload kept).
  • clamp: d <= 0x7F800000 && d != 0 selects positive non-zero values including +inf. Those at or above 1.0 become 1.0, NaN, zeros and negatives become +0, and a positive denormal passes through, all as described.

AI-assisted review (Claude Code).

@padhossein

Copy link
Copy Markdown
Contributor

Independent f32 division fixup draw (b2d3f51f). On Mesa llvmpipe 26.1.6 I ran scripts/run_gpu_tests.py --filter cs_div_fixup_f32 from this head with LLVM 21 and SPIRV-Tools. Translation and validation passed, and the dispatch matched 64/64 readback words. The 35 semantic value tests also passed. Synthetic shader data only.

Synthetic inputs only. AI-assisted (Codex).

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

Tested b2d3f51f merged locally with b8259d91 on Windows, CPU only. The base translator rejected v_div_fixup_f32 as unsupported. I ran a synthetic wave32 program with the destination sharing each source register in turn, first with even lanes active, then with odd lanes active. All 192 output words matched bit for bit. Inactive lanes retained the original destination, including NaN payloads, and the input and guard memory stayed intact.

The semantic suite passed (4,136 passed, 3 skipped without SPIRV-Tools), as did all 69 shader-runner unit tests and the fixture regeneration check.

AI-assisted review (Codex Astra 6.2 High).

@evilclanker9000

Copy link
Copy Markdown
Contributor Author

Rechecked in real wave64 with the hw_oracle fix in #2961: same words.

AI-assisted.

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested b2d3f51 on Steam Deck LCD (Van Gogh, RADV Mesa 26.2.0-devel, SteamOS 3.9.5, kernel 7.2.7-valve2-2-neptune-72-g31504582a073): pass.

Commands run (from the PR worktree, against the existing runtime/build on ec8ec4c9):

  • PYTHONPATH=tests/semantic python3 tests/semantic/test_value_semantics.py -> 35/35 OK
  • python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build --filter div_fixup_f32 -> 1/1 passed (cs_div_fixup_f32)
  • python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build -> 176/190 passed, 14 skipped, 0 failed

The 14 skips are the expected D3D12-owned fixtures and ps_w64x_readlane on this RADV device (cannot require a 64-wide fragment subgroup); no failures. Total increased by 1 vs the 175/189 baseline due to the new cs_div_fixup_f32 fixture.

AI-assisted (OpenCode).

@hidrogenone

Copy link
Copy Markdown
Contributor

Tested b2d3f51f on NVIDIA GeForce RTX 5080 (Blackwell), driver 32.0.16.2124, Windows 11, Vulkan SDK 1.4.357.0. llvm-mc LLVM 21.1.8 (WSL Ubuntu), runtime/translator from the PR's tree.

scripts/run_gpu_tests.py --filter div_fixup:

cs_div_fixup_f32   64/64 dumped words exact
1/1 passed

Translator: 1 shader translated, spirv-val clean. Adds NVIDIA Blackwell coverage for this rule (prior checks were Apple M2, Mesa llvmpipe, and AMD RADV / Steam Deck). AI-assisted.

@Yourrespawn

Copy link
Copy Markdown
Contributor

Tested b2d3f51f on NVIDIA GeForce RTX 4080 (Ada, subgroup 32..32), driver 617.14, Vulkan API 1.4.351, Windows 11 Pro 10.0.26200: pass.

  • New fixture on real hardware: cs_div_fixup_f32 PASS, 64/64 dumped words exact — all 20 cases including every row of the special-value table (quieted NaNs, the both-zero/both-inf 0xffc00000, the signed inf/zero rows, the −150 exponent boundary from both sides), the denormal numerator and quotient, abs/neg, clamp, and the ignored mul:2.
  • run_gpu_tests.py: 144/190 passed, 46 skipped (native wave64, expected on this 32-lane GPU)
  • Semantic suite: 4139 OK (Python 3.12.10), including the new test_value_semantics div-fixup coverage

The 5,072-program gfx1036 measurement's rule (sign from S1 xor S2, S0 not flushed) holds bit-for-bit through the translated SPIR-V on NVIDIA silicon too — a clean cross-vendor confirmation.

AI-assisted (ZCode agent): builds and runs executed by a coding agent on this machine.

@tupacfan04

Copy link
Copy Markdown
Contributor

Coordination note, since #2858 implements the same instruction: git merge-tree between the two is textually clean, but the merged tree is not.

#2858 is already recorded as the dependency of #3011 (the TEST-545 rewrite), and it agrees with the existing "output modifiers on hardware" row. Suggest landing #2858 first and rebasing this one to add its GPU fixture and check.py/run_gpu_tests.py wiring, which #2858 does not have. The quieting/omod disagreement is a hardware question worth re-measuring rather than reconciling in text; the PR body's 5,072 triples and the docstring's 4,940 should also agree.

AI-assisted, no game files, no GPU.

The first measurements used the oracle's default kernels, which run with
IEEE on. All cases were run again as raw kernels with IEEE off,
DX10_CLAMP on and f32 denormals flushed (the game's MODE): there omod
scales the result before clamp (a zero or denormal result becomes +0, a
NaN is kept), and a signalling NaN is not quieted. Translation, fixture,
reference words and the design.md row follow that.

AI-assisted.
@JRamos1993

Copy link
Copy Markdown
Contributor

Tested b2d3f51 merged onto main 1628210 on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 144/190 passed, 46 skipped, 0 failed. main 1628210 gives 143/189, 46 skipped, 0 failed. All 195 test shaders translate and pass spirv-val.
  • New GPU tests: cs_div_fixup_f32 PASS.
  • tests/semantic/test_value_semantics.py: ran 35 tests in 3.626s; OK.
  • tests/test_shader_checks.py: ran 69 tests in 1.813s; OK.

AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested on Windows 11 with two GPUs on one machine (runtime built from this PR's head, MSVC 14.51):

No problems found. ctest on the RTX 4080 SUPER: 129/129 passed. Intel UHD 770 ctest: only the two known base failures, d3d12_descriptor (fixed by #2948) and d3d12_subgroup (fixed by #2934).
Fixture cs_div_fixup_f32: PASS on both RTX 4080 SUPER and Intel UHD 770 (64/64 dumped words exact).

docs/design.md
tests/test_shader_checks.py

AI-assisted.
runtime/test/shaders/check.py
tests/semantic/regen_fixtures.py
tests/test_shader_checks.py
tools/rdna2spirv.py

AI-assisted.
@amarovndres

Copy link
Copy Markdown

Tested 0978af04 on AMD Radeon RX 9060 XT (RDNA4, discrete, subgroup 32..64), AMD 26.10.1 (32.0.32015.7029, LLPC, Vulkan 1.4.359), Windows 11 Pro (build 26200): pass.

  • scripts/run_gpu_tests.py (full suite; test programs built from this head with llvm-mingw, llvm-mc 23.1.6): 197/211 passed, 14 skipped, 0 failed. main d0835dd9 on the same machine: 196/210 passed, 14 skipped.
    • PASS cs_div_fixup_f32 64/64 dumped words exact
  • tests/semantic/test_value_semantics.py: 41 tests, OK
  • tests/test_shader_checks.py: 75 tests, OK

AI-assisted (Claude Code). Synthetic shaders only, no game files.

@tylerstraub

Copy link
Copy Markdown
Contributor

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

The submitted cs_div_fixup_f32 fixture and a synthetic variant both passed spirv-val and matched all 64 GPU readback words on Linux RTX 5090.

  • NaN precedence: with denominator S1 = 0xffc12345 and numerator S2 = 0x7fa00000, word 2 was 0x7fa00000. The numerator's signalling NaN payload survived, including its clear quiet bit.
  • Underflow boundary: with S1 = 0xff000000 and S2 = 0x33800000 (biased exponents 254 and 103), word 11 was negative zero, 0x80000000. The fixture's adjacent −150 control on word 12 retained 0x00800003.

Only the two input literals changed between runs. This checks the translated shader on this Vulkan device.

AI-assisted (Codex).

AI-assisted verification.

@Kev-Ball

Copy link
Copy Markdown

Tested 0978af04 on an NVIDIA GeForce RTX 3080 Ti (32-lane subgroups), driver 620.73.04, CachyOS Linux 7.3.5-2-cachyos. I rebuilt the Vulkan harnesses from this revision and selected the NVIDIA device explicitly.

Ran scripts/run_gpu_tests.py --filter cs_div_fixup_f32 --require-pass: the shader translated, passed Vulkan SPIR-V validation, and all 64 dumped words matched on the translated compute path.

AI-assisted verification with synthetic inputs.

@lunarminus

Copy link
Copy Markdown

Tested 0978af04 merged onto main d0835dd9 on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: GPU suite passes in both modes, but main's test_alu_div_fixup_f32 test fails 1 of 4

  • Merge: clean. The GPU test programs are the d0835dd9 build, because this PR changes no C++ or CMake.
  • scripts/run_gpu_tests.py, full suite, native and LEONIDA_W64_EMULATE=1: 197/211 passed, 14 skipped, 0 failed in each mode. 215 shaders translated, 215 pass spirv-val. I compared every test with d0835dd9 without the PR (196/210, 14 skipped, in each mode). The only difference is the new cs_div_fixup_f32: PASS, 64/64 dumped words exact, in both modes.
  • --filter div_fixup, both modes: 1/1 passed.
  • This PR's tests: test_value_semantics 41 OK (including DivFixupF32), tests/test_shader_checks.py 75 OK, regen_fixtures.py --check and compileall OK.
  • Failure: main already has a v_div_fixup_f32 rule from rdna2spirv: implement v_div_fixup_f32 (measured on RDNA2 gfx1036) #2858 (b32059d5, which this head also contains). Its test tests/semantic/test_alu_div_fixup_f32.py passes 4/4 on d0835dd9, and 3/4 on the merge and on 0978af04 alone. test_measured_gfx1036_rows passes now (the earlier "13 rows fail" note was about b2d3f51f). test_spirv_structure_and_unsupported fails with 'OpIsNan' not found in clamp_spv.
  • Cause: this PR's branch for v_div_fixup_f32 (tools/rdna2spirv.py:6374 on the merge) comes before main's (:9168), so main's is never used. On the merge, a clamp form emits no OpIsNan/FClamp, and op_sel:[0,0,0,0] and op_sel:[1,1,1,1] now translate, where d0835dd9 raises Unsupported: v_div_fixup_f32 modifier op_sel:[…].
  • Docs: docs/design.md now has two v_div_fixup_f32 rows (lines 328 and 425).
  • Full semantic suite on the merge: 7458 tests, 8 failures and 2 errors. 9 of them (in test_cmp_class_f64, test_loop_ballot_barrier and test_mutations) fail the same way on d0835dd9 when those modules are run alone. The 10th is the test_alu_div_fixup_f32 failure above.

Suggestion: keep one v_div_fixup_f32 branch and one design row. Then either keep main's op_sel rejection and clamp structure, or update test_alu_div_fixup_f32.py to match.

Synthetic shaders only, no game files.

AI-assisted (Claude Code).

docs/design.md
scripts/run_gpu_tests.py
tests/semantic/regen_fixtures.py
tests/test_shader_checks.py

AI-assisted.
@DavidM1911

Copy link
Copy Markdown

Tested ed79d6c on two Vulkan devices, Windows 11, Vulkan SDK 1.4.363, llvm-mc 23.1.6 (AMDGPU, LLVM-MinGW build):

  • tests/semantic/test_value_semantics.py: 82/82 OK; tests/test_shader_checks.py OK.
  • runtime/test/shaders/build.py: 262/262 translated (cs_div_fixup_f32 included), all pass spirv-val.
  • check.py, AMD Radeon(TM) Graphics iGPU (Ryzen 9 7900X, gfx1036, driver 24.10.24 — RDNA2, the family the rule was measured on): cs_div_fixup_f32 ok; 116/204 pass, 84 skipped.
  • check.py, NVIDIA GeForce RTX 3060 Ti (driver 610.88): cs_div_fixup_f32 ok; 165/204 pass, 35 skipped.

The only failures on both GPUs — cs_bvh, cs_bvh32, cs_heap_init, ps_array_resinfo — reproduce identically with main's shaders on the same binaries, so they are environmental here, not from this change.

AI-assisted. No game files.

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.