Skip to content

rdna2spirv: translate v_div_scale_f64 from RDNA2 measurements - #2703

Open
Vonsek wants to merge 8 commits into
ItsNotRexed:mainfrom
Vonsek:claude/f64-div-scale
Open

Vonsek wants to merge 8 commits into
ItsNotRexed:mainfrom
Vonsek:claude/f64-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-F64-DIV-SCALE (claim comment)

What and why

v_div_scale_f64 stopped translation with Unsupported. It is the first step of LLVM's double division (v_div_scale x2, v_rcp_f64, v_fma_f64, v_div_fmas_f64, v_div_fixup_f64); the other f64 ops of that sequence are in open PRs (#2244, #2441, #2477, #1976), this one was not.

The rule comes from native RDNA2 measurements (tools/hw_oracle on the gfx1036 iGPU, 2,008 operand triples, wave32 and wave64), because the hardware differs from the ISA pseudocode in several places. With e1, e2 the exponent fields of S1 (denominator) and S2 (numerator), the first match wins:

condition sdst bit D
e2 - e1 >= 768 1 S0 * 2^128 if S0 == S1, else S0
e1 == 0 (S1 zero or denormal) 0 S0 * 2^128
e1 >= 2045 and e1 - e2 >= 768 1 S0 * 2^-128 if S0 == S1, else S0 (the pseudocode says 2^+128)
e1 >= 2045 0 S0 * 2^-128
e1 - e2 >= 768 1 S0 * 2^128 unless S0 == S1 (the pseudocode scales only S0 == S2)
e2 <= 53 0 S0 * 2^128
otherwise 0 S0

Also measured:

  • The thresholds use only the exponent fields: inf and NaN count as 2047, zero and denormals as 0, and mantissas never matter. The bounds are exact at 767/768, 2044/2045 and 53/54.
  • A zero S1 or S2 makes D 0xfff8000000000000. The sdst bit still follows the table, so 0/1 sets it, unlike the pseudocode's VCC = 0.
  • S0 == S1 compares values after neg, and a copy of S1 in another register counts as equal.
  • Scaling keeps a NaN (a signalling payload too), inf and zero. 2^-128 rounds to nearest even into the denormals down to exponent field 117 and gives inf of S0's sign at 116 or below. Only an S0 that is neither S1 nor S2 reaches that path.
  • clamp gives +0 for NaN, zero and negatives, and 1.0 from 1.0 up. omod is accepted by the assembler and ignored. VOP3b has no abs.
  • Inactive lanes write 0 to sdst.

The translation uses 32-bit integer operations only: exponent compares, exponent-field add/subtract, normalisation of a denormal S0, and a rounded right shift for the denormal results. It needs no Float64 capability, so it does not depend on shaderFloat64 or on how a device handles f64 denormals. Operands are VGPR/SGPR pairs or inline constants; a 32-bit literal stays Unsupported, because which half it fills is SHADER-366 (#2057). tools/lane_masks.py now counts the sdst of v_div_scale_f32/f64 as a lane mask, like v_add_co_u32's carry.

  • runtime/test/shaders/cs_div_scale_f64.asm: compute dump, 13 cases x (D.lo, D.hi, vcc), with each expected word derived by hand in the header. It covers both forms of every rule, zero to NaN, sNaN payload, neg on src0, clamp on the NaN, and the ignored mul:2. A compute dump, so the NaN words never pass through a float render target.
  • tests/semantic/test_value_semantics.py DivScaleF64: a plain-Python reference of the rule gives the 39 words, and the translated fixture produces them in the interpreter (no f64 needed there either).
  • docs/design.md: a contract row.

Tested

  • MODE: the oracle's default kernels run with IEEE on. All 2,008 cases were run again as raw kernels in the game's MODE (IEEE off, DX10_CLAMP on, f32 denormals flushed, f64 denormals kept) and gave identical words, so for this op omod stays ignored and NaN payloads are kept in both modes.
  • All 2,008 measured cases (oracle JSON in my scratch) were translated one per program and run in tests/semantic/spirv_mini_interp.py: 0 mismatches against hardware. The 13 fixture cases were also run on the oracle in both wave32 and wave64 with the same words.
  • scripts/run_gpu_tests.py --filter div_scale: cs_div_scale_f64 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: the main translator raises Unsupported: v_div_scale_f64 for the fixture (semantic test errors, no SPIR-V for the GPU test).
  • Negative control: dropping the numerator scaling of the e1 - e2 >= 768 rule (one line) fails on both GPUs (word 7: expected 0x15f00000, got 0x0df00000) and in DivScaleF64. 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: 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_f64 stopped translation. The rule is measured natively on
RDNA2 gfx1036 with tools/hw_oracle (2,008 operand triples, wave32 and
wave64); it differs from the ISA pseudocode. Translated with 32-bit
integer operations only, so no Float64 capability is needed.

Adds the cs_div_scale_f64 GPU compute dump (13 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 in
tools/lane_masks.py.

AI-assisted.
@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent RTX 5070 / 621.24 Vulkan run of 8e2dc8d. I used 8,192 dynamic operand triples in each of wave32 and emulated wave64. All 81,920 destination/flag pairs matched the published rule and an ldexp scaling reference. The inputs cover the exponent-distance cutoff at 767/768, denominator 2044/2045, numerator 53/54, every leading-bit position of a subnormal on scale-up, and downscale rounding including carry into the smallest normal. Zeros, infinities and NaN encodings were checked by raw bits.

The shaders covered VCC and SGPR lane-mask destinations, independently loaded S0, S0 equal to denominator/numerator, neg plus clamp and ignored omod, and an alternating EXEC mask. Inactive lanes preserved their destination words and cleared the written scale flag after restoring EXEC. Both modules validated for Vulkan 1.2, declared no Float64, and ran with shaderFloat64 disabled. The parent refused the instruction, and the value-semantics and lane-mask tests passed on this head.

This checks the Vulkan implementation against the reported gfx1036 rule. It does not independently measure native RDNA2 behavior or the full division sequence.

@Marukooh

Copy link
Copy Markdown
Contributor

Tested 8e2dc8d on NVIDIA GeForce RTX 5070 Ti (Blackwell), NVIDIA 620.73.04, CachyOS Linux. Built the harness from the PR and ran scripts/run_gpu_tests.py --filter cs_div_scale_f64 with LEONIDA_W64_EMULATE=0 and =1. The synthetic module passed SPIR-V validation and all 64 readback words matched in both runs. The 60 shader-checker and 33 value-semantics tests passed. AI-assisted.

@ricarmandoagent

Copy link
Copy Markdown

Additional check on 8e2dc8d409c1a07e734dbadd2c4484d24d39701e (base 49475796) on NVIDIA GeForce RTX 4060 (Ada, AD107; Vulkan subgroupSize 32..32), driver 620.70.06, Vulkan 1.4.357, Bazzite 45 / Linux 7.3.4, Python 3.15.2, llvm-mc 23.1.5, shared prebuilt harness.

The RTX 5070 sweep and the 5070 Ti pass above already cover the dynamic-triple matrix, so this adds only: a third NVIDIA part/driver on the fixture, the exact main-side failure end to end, an independent negative control, and a hand re-derivation of all 13 fixture cases from the published rule.

Head: --filter div_scale -> 1 shader translated, 1 pass spirv-val, cs_div_scale_f64 64/64 dumped words exact.

Before side, only tools/rdna2spirv.py restored from origin/main 49475796, same command:

=== rdna2spirv.py ===
1 shaders: 0 translated, 0 pass spirv-val
       1  v_div_scale_f64

=== GPU TEST RESULTS ===
  FAIL  cs_div_scale_f64      missing .spv (translation failed)

0/1 passed

Which is the "Red on main" claim.

Negative control (independent of the one in the description: drop the numerator scaling of the e1 - e2 >= 768 rule, (small_q, ne01, ...) -> (small_q, "%false", ...)):

  FAIL  cs_div_scale_f64      word 7: expected 0x15f00000, got 0x0df00000

Exactly the word the description predicts, and only that word. Translator restored (git status clean).

Independent derivation: I re-derived all 13 fixture cases by hand from the rule table in the description. The checkpoints that matter all agree with the .asm header, check.py and the 64 dumped words: 767/768 (e2-e1 = 800, e1-e2 = 800), 2044/2045 (e1 = 2046 takes the downscale rules), 53/54 (e2 = 20 takes S0*2^128), a denormal S0 scale-up to 0x04fc0000_00000000 (7 * 2^-944, exponent field 0x04f, mantissa 0xc0000), a zero denominator giving 0xfff8000000000000, an sNaN numerator kept with its payload 0x7ff00000_00000001 and not quieted, neg on S0 counted in the S0 == S1 compare (0xbff00000_00000005), clamp on the NaN giving +0, and the ignored mul:2 leaving 0x3fd00000_00000000.

What this GPU cannot confirm, plainly: the rule itself is the native gfx1036 measurement, and the RTX 4060 only verifies the translator's 32-bit implementation of it on Ada. It does not measure RDNA2, and it says nothing about the rest of the f64 division sequence (the PR depends on others for that).

Semantic side: test_value_semantics.py -k DivScaleF64 -> 2 tests OK.

Verdict: LGTM.

AI-assisted review.

# Conflicts:
#	docs/design.md
#	runtime/test/shaders/check.py
#	scripts/run_gpu_tests.py
#	tests/test_shader_checks.py
#	tools/rdna2spirv.py
@edubox

edubox commented Nov 22, 2026

Copy link
Copy Markdown

Tested 78369205 on an NVIDIA GeForce RTX 4080 SUPER (Ada), driver 620.70.06, Vulkan 1.4.357, Linux x86-64: pass.
Tested 78369205 on an AMD Radeon 610M iGPU (RDNA2, gfx1036), RADV Mesa 26.2.6, Vulkan 1.4.354, same host: pass.

Both through the full path, built from the PR head (CMake/Ninja, RelWithDebInfo, GCC 16.2.1) with llvm-mc 23.1.5 and the AMDGPU target:

python3 -I scripts/run_gpu_tests.py --filter div_scale
rdna2spirv.py: 1 shaders: 1 translated, 1 pass spirv-val
PASS  cs_div_scale_f64  64/64 dumped words exact
1/1 passed

The second run is the same command with VK_LOADER_DRIVERS_SELECT='*radeon*'. Your run of the rule was on an RDNA2 iGPU through the AMD Windows driver; this is the same GPU class through RADV on Linux, plus an independent NVIDIA run on Linux. No skip on either device (the fixture needs no shaderFloat64).

Unit tests: tests/semantic/test_value_semantics.py 35 OK, tests/test_shader_checks.py 71 OK. On 49475796 there is no div_scale_f64 handler at all, so the asserted red-on-main is consistent with the code.

The 13 cases really discriminate the rule branches. Two local mutations of div_scale_f64 in tools/rdna2spirv.py (each reverted before the next run), same fixture:

Mutation Result
den_huge e1 >= u(2045) → u(2047) FAIL word 13: expected 0x77e00000, got 0x7fe00000 (case 4)
num_tiny e2 <= u(53) → u(0) FAIL word 22: expected 0x09400000, got 0x01400000 (case 7)

So case 4 fails when the e1 >= 2045 rule is weakened, and case 7 fails when e2 <= 53 is, i.e. the fixture is not passing on a coincidentally equivalent path.

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.

All 2,008 cases were run again with IEEE off, DX10_CLAMP on and f32
denormals flushed (the game's MODE): identical words, so omod stays
ignored and a NaN keeps its payload for this op.

AI-assisted.
docs/design.md
tests/test_shader_checks.py

AI-assisted.
docs/design.md
scripts/run_gpu_tests.py
tests/semantic/regen_fixtures.py
tests/test_shader_checks.py
tools/lane_masks.py
tools/rdna2spirv.py

AI-assisted.
@amarovndres

Copy link
Copy Markdown

Tested a4f4a4d0 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_f64 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.

@tedin7

tedin7 commented Nov 23, 2026

Copy link
Copy Markdown

Tested ace5bf6 (merge base 7b7d73f) on NVIDIA GeForce RTX 5080, Linux 580.65.06: pass

Tested the PR head merged with current main 7b7d73f8 (clean merge, not pushed). Runtime harnesses built from main 7b7d73f8, llvm-mc 21, Python 3.13, Vulkan SDK 1.4.363, Linux native.

Check main 7b7d73f8 this PR
run_gpu_tests.py native 170/217 passed, 47 skipped 171/218 passed, 47 skipped
run_gpu_tests.py with LEONIDA_W64_EMULATE=1 201/217 passed, 16 skipped 202/218 passed, 16 skipped
failed (native / emulated) 0 / 0 0 / 0
tests/semantic/regen_fixtures.py --check ok ok
pytest tests/semantic/test_value_semantics.py tests/test_find_hash_lookup.py tests/test_first_look_shim_report.py tests/test_shader_checks.py tests/test_shader_checks.py tests/test_run_gpu_tests.py tests/test_cf_diff.py 162 passed, 315 subtests passed (3 shared files) 207 passed, 1 skipped, 598 subtests passed in 5.69s

Line-by-line against my main run (results that differ from main):

  • native:
    • cs_div_scale_f64 main: - pr: PASS 64/64 dumped words exact
  • emulated:
    • cs_div_scale_f64 main: - pr: PASS 64/64 dumped words exact

Not run: the full unittest discover -s tests/semantic (about 6 minutes, and main alone has failures there); only the test files this PR touches plus the shared checker tests ran. No other GPU vendor, no Windows. No game files.

AI-assisted (Claude Code).

@kapitche

Copy link
Copy Markdown
Contributor

Tested ace5bf6 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_f64 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