Skip to content

rdna2spirv: translate v_div_scale_f32 from RDNA2 measurements - #2777

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

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

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-SCALE (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_scale_f32 stopped translation with Unsupported. It is the first step of LLVM's IEEE float division (v_div_scale x2, v_rcp_f32, v_fma_f32, v_div_fmas_f32, v_div_fixup_f32). This is the f32 companion of #2703 (v_div_scale_f64), measured and implemented the same way. The two PRs are independent, but both change TOTAL in tests/test_shader_checks.py (11 to 12) and the same tools/lane_masks.py line (identically). Whichever lands second needs TOTAL set to 13.

The rule comes from native RDNA2 measurements (tools/hw_oracle on the gfx1036 iGPU, 3,074 operand triples, plus the fixture cases in wave32 and wave64). They were taken in the game's MODE, with f32 denormals flushed, which is what the translator assumes for f32 operands (SHADER-298). The hardware differs from the ISA pseudocode. A denormal operand (S0 too) reads as a zero of its sign. With e1, e2 the exponent fields of S1 (denominator) and S2 (numerator), the first match wins:

condition sdst bit D
e2 - e1 >= 96 1 S0 * 2^64 if S0 == S1, else S0
e1 == 0 0 S0 * 2^64 (only visible through the bit: a zero S1 makes D NaN)
e1 >= 253 and e1 - e2 >= 96 1 S0 * 2^-64 if S0 == S1, else S0
e1 >= 253 0 S0 * 2^-64
e1 - e2 >= 96 1 S0 * 2^64 unless S0 == S1
e2 <= 24 0 S0 * 2^64
otherwise 0 S0

Also measured:

  • The rule uses only the exponent fields, 0 for zero and 255 for inf and NaN; mantissas never matter. The bounds are exact at 95/96, 252/253 and 24/25.
  • A zero S1 or S2 (a denormal included) makes D 0xffc00000, and the sdst bit still follows the table.
  • S0 == S1 compares values after neg. The translator applies neg as a sign-bit flip, so a NaN keeps its payload.
  • Scaling keeps NaN (a signalling payload too), inf and zero. 2^64 overflows to inf from exponent field 191. 2^-64 gives a zero of S0's sign at exponent field 64 (the denormal result is flushed) and inf of S0's sign below that.
  • clamp gives +0 for NaN, zero and negatives, and 1.0 from 1.0 up. omod scales the result before clamp (IEEE off; ignored with IEEE on). VOP3b has no abs.
  • Inactive lanes write 0 to sdst.

The translation uses integer operations only.

  • runtime/test/shaders/cs_div_scale_f32.asm: compute dump, 16 cases x (D, vcc), with each expected word derived by hand in the header. It covers both forms of each visible rule, the denormal-as-zero numerator, zero to NaN, sNaN payload, neg, clamp on the NaN, mul:2 (applied: IEEE off), and the flushed / inf results of 2^-64.
  • tests/semantic/test_value_semantics.py DivScaleF32: a plain-Python reference gives the 32 words, and the translated fixture produces them in the interpreter.
  • docs/design.md: a contract row. tools/lane_masks.py counts the sdst of v_div_scale_f32/f64 as a lane mask.

Tested

  • All 3,074 measured cases were translated one per program and run in tests/semantic/spirv_mini_interp.py: 0 mismatches against hardware. The 16 fixture cases gave the same words on the oracle in wave32 and wave64.
  • scripts/run_gpu_tests.py --filter div_scale: cs_div_scale_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_scale_f32.
  • Negative control: dropping the numerator scaling of the e1 - e2 >= 96 rule (one line) fails on both GPUs (word 4: expected 0x2d800003, got 0x0d800003) and in DivScaleF32. Reverted.
  • python -m unittest discover -s tests/semantic, python tests/semantic/regen_fixtures.py --check, python -I tests/test_shader_checks.py, tests/test_lane_masks.py, tests/test_isa_coverage.py, tests/test_shader_meta.py, compileall: OK.

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_scale_f32 stopped translation. The rule is measured natively on
RDNA2 gfx1036 with tools/hw_oracle (3,074 operand triples) in the game's
MODE with f32 denormals flushed; it differs from the ISA pseudocode.
Translated with integer operations.

Adds the cs_div_scale_f32 GPU compute dump (16 cases, words derived by
hand), a semantic test with a plain-Python reference, the docs/design.md
contract row, and counts the sdst of v_div_scale as a lane mask.

AI-assisted.
# Conflicts:
#	docs/design.md
#	tests/test_shader_checks.py
#	tools/rdna2spirv.py
@padhossein

Copy link
Copy Markdown
Contributor

Independent f32 division-scaling fixture run (b9ae3bc5). Ran scripts/run_gpu_tests.py --filter cs_div_scale_f32 end to end on Mesa llvmpipe 26.1.6 with LLVM 21.1.8 and SPIRV-Tools. The shader translated, passed spirv-val and matched 64/64 readback words, covering the 16 result/VCC pairs and untouched sentinels. The 33 semantic value tests passed as well.

Synthetic inputs only. AI-assisted (Codex).

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested b9ae3bc 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_scale --build runtime/build/linux: 1 shader translated, 1 pass spirv-val; 1/1 passed — cs_div_scale_f32 64/64 dumped words exact.

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

AI-assisted.

@evilclanker9000

Copy link
Copy Markdown
Contributor Author

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

AI-assisted.

@hidrogenone

Copy link
Copy Markdown
Contributor

Tested dfece58d 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).

scripts/run_gpu_tests.py --filter div_scale:

cs_div_scale_f32   64/64 dumped words exact
1/1 passed

Translator: 1 shader translated, spirv-val clean. NVIDIA Blackwell (RTX 50-series) coverage. AI-assisted.

@crastinator

Copy link
Copy Markdown
Contributor

Tested dfece58 merged into main 16282101 (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • New test passes natively: cs_div_scale_f32 (64/64 dumped words exact).
  • Whole run_gpu_tests.py: 176/190 native, 176/190 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 37/37.
  • tests/semantic/test_value_semantics.py, tests/test_shader_checks.py: 106 passed, 462 subtests passed.

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

@tupacfan04

Copy link
Copy Markdown
Contributor

Tested at the head on Windows 11 / Python 3.13.0: DivScaleF32 2/2, the translator fixture runs through support.translate_fixture (tests/semantic/test_value_semantics.py:749) with 16 cases, and the row at docs/design.md:300 records the measurement. Merge clean.

The rules match the table: S1 is the denominator and S2 the numerator exponent fields, S0 the scaled operand; the priority order, sdst = big_q || small_q, 2^64 from e0 >= 191, 2^-64 at e0 = 64, zero S1/S2 -> 0xffc00000, NaN/Inf/zero preservation, neg, clamp, ignored omod and inactive sdst = 0 all line up. The negative control (dropping rule 5's numerator scaling) fails word 4 exactly as described (0x0d800003 vs 0x2d800003).

Non-blocking gaps: no denormal/Inf/NaN S1 case; no 95/96, 24/25, 252/253 or e0 = 191 boundary case; the inactive-lane sdst is not asserted; the tools/lane_masks.py:173 change has no test in tests/test_lane_masks.py; and the row says "Radeon RDNA2 iGPU" without the gfx id (the body says 9950X3D).

Approve. AI-assisted review, 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.
docs/design.md
tests/test_shader_checks.py

AI-assisted.
tests/test_shader_checks.py
tools/lane_masks.py
tools/rdna2spirv.py

AI-assisted.
@amarovndres

Copy link
Copy Markdown

Tested 36805312 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_scale_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.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 3680531 merged with main 98fa503 on Linux (Python 3.12.16) and Windows (RTX 4080 SUPER, UHD 770): pass. test_value_semantics, test_shader_checks, test_run_gpu_tests 3/3 ok. run_gpu_tests.py cs_div_scale_f32 1/1 on RTX 4080 SUPER and UHD 770. Negative control with main's tools/rdna2spirv.py: test_value_semantics errors (1 error) and the GPU case fails to translate (no .spv).

@DenRakEiw

Copy link
Copy Markdown
Contributor

Independent hardware verification of 36805312 (merged cleanly onto origin/main 3faac557; git diff --check clean) on Windows 11 x64, Python 3.12.10, SPIRV-Tools v2026.4, LLVM 21 llvm-mc:

  • Semantic & unit tests:
    • python -m unittest discover -s tests/semantic -p test_value_semantics.py: 41/41 OK
    • python -I tests/test_shader_checks.py: 79/79 OK
  • Dual-GPU fixture (scripts/run_gpu_tests.py --filter cs_div_scale_f32):
    • Translation: 1 shaders: 1 translated, 1 pass spirv-val
    • NVIDIA GeForce RTX 5090 (driver 32.0.16.1088, LEONIDA_W64_EMULATE=1): PASS cs_div_scale_f32 64/64 dumped words exact (1/1 passed)
    • AMD Radeon(TM) Graphics RDNA2 iGPU (driver 31.0.22044.1, LEONIDA_VK_DEVICE=AMD): PASS cs_div_scale_f32 64/64 dumped words exact (1/1 passed)

AI-assisted (Gemini 4 Neon).

docs/design.md
tests/test_shader_checks.py

AI-assisted.
@sovljanskii

Copy link
Copy Markdown
Contributor

Tested 3bf37c9 merged onto main 561b65a (no-op, the head already contains it) on Apple M4, macOS 27.2.1, MoltenVK 1.4.2, Python 3.15.2 and 3.12.16: pass

  • GPU, test programs built from main (this PR changes no C++): run_gpu_tests.py --filter div_scale gives cs_div_scale_f32 PASS, 64/64 words exact, both natively (wave32 on the M4's 32-wide subgroups) and with LEONIDA_W64_EMULATE=1. Full suite: 166/233 passed, 59 skipped, 8 failed. Main is 165/232, 59 skipped, 8 failed, and the 8 failures are the same tests.
  • Denormals on MoltenVK: the device reports neither shaderDenormPreserveFloat32 nor shaderDenormFlushToZeroFloat32, yet the fixture's denormal cases pass (case 6's denormal S2, the flushed and inf results of cases 14/15). That is expected, because the flush and the 2^±64 scaling are integer ops. The only float op is omod's multiply, and case 13 runs it on normal values. So the test needs no float-controls gate.
  • CPU, on both Pythons: DivScaleF32 2/2, test_value_semantics.py 43, test_shader_checks.py 82, test_run_gpu_tests.py 47, test_lane_masks.py and test_isa_coverage.py 22 (4 skipped) all OK. regen_fixtures.py --check, compileall and git diff --check are clean. Semantic suite (3.15): Ran 6152, OK (skipped=1, expected failures=1). Main gives 6150 with the same result; the 2 extra tests are the new DivScaleF32, and no test fails.
  • Corpus slice --shard 0/64: 415/415 translated and 415 pass spirv-val, the same as main. 0 of the 415 .spv differ and the index is identical. None of the 25,734 listings contains v_div_scale_f32.
  • Mutations, each checked against both DivScaleF32 and the GPU test. Caught by both:
    • dropping the numerator x2^64 of the e1 - e2 >= 96 rule (word 4);
    • scaling S0 == S1 up when e1 >= 253 and e1 - e2 >= 96, as the ISA pseudocode text says (word 8 becomes 0x7f800000);
    • no sdst bit for e1 - e2 >= 96 (word 5);
    • no NaN for a zero operand (word 12);
    • omod ignored (word 26);
    • clamp ignored (word 24);
    • x2^-64 below field 64 giving ±0 instead of inf (word 30).
  • Survived both:
    • e2 <= 24 changed to the pseudocode's <= 23;
    • e2 - e1 >= 96 changed to >= 97;
    • e1 >= 253 changed to >= 254;
    • x2^64 never overflowing to inf (191 changed to 255);
    • inactive lanes keeping the sdst bit instead of 0;
    • denormal operands not flushed;
    • neg applied as a float negate.
  • The row calls the bounds exact (95/96, 252/253, 24/25), and it also documents the overflow from field 191 and the inactive-lane zero. No test pins any of these. Every fixture case sits away from the bounds: the exponent gaps are 100 and 127, e1 is 254, e2 is 20, and only lane 0 is active. I ran 2,048 random boundary triples (exponents 0/1/24/25/63/64/190/191/252–255, gaps 95–97, neg and clamp) through the interpreter. The translation matches the PR's own div_scale_f32_reference on every one, and the same check flags each of the boundary mutations above (18 to 125 mismatches). So the code and its model agree, but nothing in the repo would catch a regression there. The corpus can't help either, since no listing uses the op. Suggested addition: a second block of cases at e2 - e1 = 95/96, e1 = 252/253, e2 = 24/25, an S0 with exponent field 191 under a x2^64 rule, a denormal S0 with normal S1 and S2, and one case that stores vcc_lo itself with a partial EXEC (tools/rdna2spirv.py:5955, :5971, :5997 are the lines nothing covers).
  • Cases 2, 3, 4, 6, 9, 12 and 15 differ from the RDNA2 ISA pseudocode:
    • cases 2 and 3: the sdst bit and the numerator scaling at e1 - e2 = 100, where S2/S1 is not denormal;
    • case 4: -64 instead of +64;
    • cases 6, 9 and 12: the sdst bit set with a zero operand;
    • case 15: inf from 2^-128.
      These cases rest on the gfx1036 oracle data alone. The RTX, Radeon and M4 runs confirm the translation, not the hardware rule. The row says this, which is the right call.
  • Nit: the e1 == 0 rule (tools/rdna2spirv.py:5964, docs/design.md:366) can't be observed. With denormals flushed, e1 == 0 means S1 is zero, so D is 0xffc00000, and e1 == 0 can never meet e1 - e2 >= 96. Deleting the rule changes no word: the 2,048-case check, DivScaleF32 and the fixture all give the same results. The PR body's "only visible through the bit" isn't right either. The row could say the rule is unobservable under flush-to-zero, or drop it.

AI-assisted.

@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested 3bf37c9 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass.

  • pytest tests/semantic/test_value_semantics.py tests/test_shader_checks.py -q: 129 passed, 591 subtests passed.
  • Targeted GPU filter cs_div_scale_f32: 1/1 pass (64/64 dumped words exact).
  • Full GPU suite (python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build): 213/233 passed, 16 skipped, 0 failed (expected skips; total includes the new fixture).

No runtime C++ changes in this PR; tested from the PR worktree against the existing main runtime build.

AI-assisted (OpenCode).

@shipped-it

Copy link
Copy Markdown

Tested 3bf37c9661d on AMD Radeon RX 7900 XTX, RADV NAVI31 (Mesa 26.2.5, Vulkan 1.4.354), Linux: build clean; full GPU suite on the PR head — 217/233 passed, 16 skipped, 0 failed; new cs_div_scale_f32 PASS 64/64 words exact, spirv-val clean. VERDICT: pass. AI-assisted (omp coding assistant).

@DavidM1911

Copy link
Copy Markdown

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

  • tests/semantic/test_value_semantics.py: 43/43 OK: OK.
  • runtime/test/shaders/build.py: 262/262 translated, all pass spirv-val.
  • check.py, NVIDIA GeForce RTX 3060 Ti (driver 610.88): cs_div_scale_f32 ok; 169/204 pass, 35 skipped, 0 failed.
  • check.py, AMD Radeon(TM) Graphics iGPU (Ryzen 9 7900X, gfx1036, driver 24.10.24, 128-byte push constants): cs_div_scale_f32 ok; 104/204 pass, 97 skipped, 0 failed.

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.