Skip to content

rdna2spirv: translate v_div_scale_f32 from RDNA2 measurements - #2777

Open
Vonsek wants to merge 5 commits into
ItsNotRexed:mainfrom
Vonsek:claude/f32-div-scale
Open

Vonsek wants to merge 5 commits into
ItsNotRexed:mainfrom
Vonsek:claude/f32-div-scale

Conversation

@Vonsek

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

@Vonsek

Vonsek commented Nov 22, 2026

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

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.

8 participants