Skip to content

rdna2spirv: align v_div_fixup_f16/f64 with native RDNA2 gfx1036 measurements - #2477

Merged
ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
JavierBlanco23:rdna2-div-fixup
Nov 23, 2026
Merged

ItsNotRexed merged 3 commits into
ItsNotRexed:mainfrom
JavierBlanco23:rdna2-div-fixup

Conversation

@JavierBlanco23

Copy link
Copy Markdown
Contributor

Implements RDNA2 VOP3 division special-case fixup instructions v_div_fixup_f16 and v_div_fixup_f64 in tools/rdna2spirv.py (+2 RDNA2 ISA coverage):

  • Bit-exact IEEE-754 special-case resolution (S0 quotient, S1 denominator, S2 numerator; RDNA2 ISA V_DIV_FIXUP_F16 / V_DIV_FIXUP_F64):
    • sign_out = sign(S1) ^ sign(S2)
    • NaN(S2) -> quieted S2; NaN(S1) -> quieted S1
    • (0/0) or (inf/inf) -> canonical negative qNaN (0xFE00 / 0xFFF80000_00000000)
    • (x/inf) or (0/y) -> copysign(0, sign_out)
    • (x/0) or (inf/y) or NaN(S0) -> copysign(inf, sign_out)
    • otherwise copysign(S0, sign_out)
  • v_div_fixup_f16: supports op_sel:[s0,s1,s2,dst], abs/neg, output modifiers (mul:2/mul:4/div:2/clamp), and preserves the unwritten 16-bit half of the destination.
  • v_div_fixup_f64: supports VGPR/SGPR pairs, inline constants, abs/neg, needs no OpCapability Float64 in plain form, enables Float64 + clamp_mods_f64() when output modifiers are present, and rejects non-pair destinations.
  • Added contract row in docs/design.md and CPU semantic + spirv-val tests in tests/semantic/test_alu_div_fixup.py.

Verification

  • Negative control: python -B -I tests/semantic/test_alu_div_fixup.py on unpatched tools/rdna2spirv.py failed with 24 Unsupported errors (v_div_fixup_f16 and v_div_fixup_f64).
  • Positive:
    • python -m compileall -q make_game.py tools tests
    • python -B -I tests/semantic/test_alu_div_fixup.py -v (5/5 pass: all 11 special/normal cases bit-exact in spirv_mini_interp for both f16 and f64, op_sel, abs/neg, SGPR/inline, output modifiers, non-pair rejection, spirv-as + spirv-val --target-env vulkan1.2)
  • Environment: AI-assisted, tested on Windows 11 (NVIDIA GeForce RTX 5080, driver 616.56, Vulkan SDK 1.4.363.0).

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independent review of 21a1ecf on Windows 11 build 26300, RTX 5070 driver 621.24. All five PR tests and Vulkan 1.2 validation passed. I found two places where the translated output differs from the published RDNA2 pseudocode, §12.12, VOP3 opcodes 863 and 352.

  1. For finite, nonzero S1 and S2 and no exponent exception, the last branch applies the result sign to abs(S0). The PR instead maps NaN(S0) to infinity. I ran all 65,536 FP16 S0 patterns with S1=+1 or -1 and S2=+1, packing both results in one dword. All 63,490 non-NaN patterns matched. All 2,046 NaN patterns became infinities. For instance S0=0x7e35 produced 0xfc007c00; the ISA rule gives signed NaNs (0xfe357e35 if the payload is retained). The same occurs in FP64: S0=0xfff8123456789abc, S1=2, S2=1 produced positive infinity where the signed magnitude is still a NaN. The existing nan_quot_overflow tests encode the PR behavior.
  2. The FP64 pseudocode has an exponent-underflow branch before the S0 fallback. With S0=1, S1=2^100, S2=2^-1022, both source operands are normal and their exponent difference is -1122. The translated result was 1.0, bypassing the underflow branch.

These are synthetic standalone inputs, useful for distinguishing instruction branches. I verified the translated outputs on NVIDIA. Native AMD confirmation of the NaN payload and underflow word would help settle the contract. AI-assisted review.

AMD RDNA2 ISA reference

@Marukooh

Copy link
Copy Markdown
Contributor

Tested translator commit 21a1ecf on NVIDIA GeForce RTX 5070 Ti (Blackwell), NVIDIA 620.73.04, CachyOS Linux.

  • All 22 existing f16/f64 normal and special-case inputs matched the expected bits at all 4,096 pixels per draw. I assembled and validated each synthetic fragment module for Vulkan 1.2, then read back the result using vk_harness from 4eb6155d. The GPU run covers the basic opcode forms. The modifier cases were checked by the semantic suite.
  • The whole semantic suite passed 2,572 tests with Python 3.14.10. AI-assisted.

@JavierBlanco23

Copy link
Copy Markdown
Contributor Author

Thanks @duudiis for the thorough GPU review and exact reproducers! Fixed both issues: (1) removed NaN(S0) from the infinity branch so NaN(S0) falls through to copysign(S0, sign_out) (0x7e35 -> 0xfe35 in f16, 0xfff8123456789abc -> 0x7ff8123456789abc in f64); (2) added exponent underflow/overflow checks (exp(S2)-exp(S1) <= -25 / >= 16 for f16, <= -1075 / >= 1024 for f64, so 2^-1022 / 2^100 -> +0.0). Updated docs/design.md and tests/semantic/test_alu_div_fixup.py (5/5 pass with spirv-val).

@timelery

Copy link
Copy Markdown
Contributor

Measured v_div_fixup_f16 and v_div_fixup_f64 natively on RDNA2 silicon (AMD Radeon(TM) Graphics gfx1036, Windows driver 32.0.21042.62, tools/hw_oracle, ieee=0, denorm16_64=3, dx10_clamp=1, wave32) on the exact reproducers discussed above — the actual RDNA2 silicon differs from the PDF pseudocode on both points:

  1. NaN(S0) returns ±inf (not copysign(S0, sign_out)):

    • v_div_fixup_f16(0x7e35, +1.0, +1.0) → 0x7c00 (+inf)
    • v_div_fixup_f16(0x7e35, -1.0, +1.0) → 0xfc00 (-inf)
    • v_div_fixup_f16(0x7c01, +1.0, +1.0) → 0x7c00 (+inf)
    • v_div_fixup_f64(0xfff8123456789abc, 2.0, 1.0) → 0x7ff0000000000000 (+inf)
    • v_div_fixup_f64(0x7ff8123456789abc, -2.0, 1.0) → 0xfff0000000000000 (-inf)
      So the original NaN(S0) -> ±inf rule matched RDNA2 hardware (same on v_div_fixup_f32).
  2. Exponent-difference underflow / overflow checks:

    • v_div_fixup_f16: no exponent-difference underflow or overflow check at all on gfx1036: e2 - e1 of -25, -29, +16, and +29 all return 0x3c00 (copysign(S0, sign_out)).
    • v_div_fixup_f64: no overflow check (e2 - e1 of +1024 and +2045 both return 0x3ff0000000000000), and the underflow cutoff is e2 - e1 <= -1076 (not -1075):
      • e2 - e1 = -1074 (e1=2046, e2=972) → 0x3ff0000000000000
      • e2 - e1 = -1075 (e1=2046, e2=971) → 0x3ff0000000000000
      • e2 - e1 = -1076 (e1=2046, e2=970) → 0x0000000000000000
      • e2 - e1 = -1122 (e1=1123, e2=1) → 0x0000000000000000
        (Matches v_div_fixup_f32, whose measured cutoff on gfx1036 is e2 - e1 <= -151 = -(127 + 23 + 1) with no overflow check.)
  3. Signalling NaN under IEEE=0: v_div_fixup_f16(..., s2=0x7c01) → 0x7c01, v_div_fixup_f64(..., s2=0x7ff0000000000001) → 0x7ff0000000000001 (unchanged with IEEE=0; quieted only when IEEE=1).

(AI-assisted hardware verification.)

@JavierBlanco23

Copy link
Copy Markdown
Contributor Author

Thanks @timelery for measuring both ops on native RDNA2 gfx1036 silicon! Updated tools/rdna2spirv.py, docs/design.md, and tests/semantic/test_alu_div_fixup.py (5/5 pass with spirv-val) to match every measured gfx1036 rule under IEEE=0: NaN(S0) -> +-inf, no exponent-diff cutoff on f16, f64 underflow cutoff at e2 - e1 <= -1076 (no overflow cutoff), and sNaN preserved unchanged under IEEE=0.

@lunisahdidouch

Copy link
Copy Markdown

Tested 1e1aae8 on an RX 9070 (RDNA4), AMD 26.11.1 (LLPC), Vulkan 1.4.359, Windows 11: pass

tests/semantic passes with the div_fixup tests (4142 tests, OK). Full run_gpu_tests.py on the branch: 175/189 passed, 14 skipped, nothing failed. (The previous head 21a1ecf was clean too.)

AI-assisted

@JavierBlanco23 JavierBlanco23 changed the title rdna2spirv: implement v_div_fixup_f16 and v_div_fixup_f64 rdna2spirv: align v_div_fixup_f16/f64 with native RDNA2 gfx1036 measurements Nov 22, 2026
@TheMrBeez

Copy link
Copy Markdown

Independent check of 48a1a98b's v_div_fixup_f64 exponent rules: native hardware words compared with this head's translation on two GPUs. I came here from #3791, where a main word differed from hardware.

Native gfx1036 (Ryzen 9 9950X3D iGPU, AMD driver 32.0.21030.2001, raw tools/hw_oracle, wave32, assembler-default MODE with f64 denormals kept). All runs use v_div_fixup_f64(S0 = 4.0, S1 = den, S2 = num), positive finite operands:

  • Grid, 63 cases: den in {min denormal, 0x00000000_c0040000, 2^-1022, 2^-1000, 2^-60, 1, 2^60, 2^1000, 2^1023} × num in {min denormal, 2^-1022, 2^-60, 10, 2^60, 2^1000, 2^1023}. The hardware never produces ±inf by exponent difference: all 17 cases with exp(S2) - exp(S1) >= 1024 return S0 = 4.0. That includes a denormal denominator with num 2^1023, and 2^-1022 / 10.
  • Underflow edge, 60 cases: den 2^1000 or 2^1023 (mantissa 1.0 and 1.999), num chosen so exp(S2) - exp(S1) is −1073…−1077 (mantissa 1.0, 1.5, 1.999). The result is +0 at −1076 and −1077, and S0 at −1075 and above, whatever the mantissas. So the cutoff is <= -1076, as this PR has it.

Translated (compute .asm → vk_compute_test, spirv-val clean), word for word against the native words:

RTX 5080 (621.01) Radeon gfx1036
48a1a98b, grid 126/126 126/126
48a1a98b, edge 120/120 120/120
main 4c52ce27, grid 109/126 (the 17 overflow cases give +inf) 109/126
main 4c52ce27, edge 108/120 (the 12 cases at exactly −1075 give +0) 108/120

So this head's f64 exponent rules match the hardware on every case here. main still has the >= 1024 → ±inf and <= -1075 → ±0 cutoffs (its "V_DIV_FIXUP" row says so). test_alu_div_fixup.py is OK at this head.

Not covered by this PR or by my runs above: the same grid with f64 denormals flushed (MODE denorm16_64 = 0). There, a denormal operand acts as zero on hardware: min denormal / min denormal → 0xfff8000000000000, 2^-1022 / denormal → +inf, denormal / 1.0 → +0. That only matters if a shader runs with f64 denormals flushed.

Windows 11, llvm-mc AMD LLVM 23.0.0git, Vulkan SDK 1.4.363. Synthetic programs only, no game files. The scripts are local. AI-assisted (Claude Code).

@zneel

zneel commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 48a1a98b on AMD Radeon RX 9070 XT (RDNA4, subgroup 32..64), AMD 26.11.2, Windows 11 Pro 10.0.26300, AMD Ryzen 7 9800X3D CPU (64 GB RAM), Python 3.12.10: pass.

Runtime and test programs built from this head (MSVC 14.51, Ninja, RelWithDebInfo), native Windows llvm-mc 21.1.8, Vulkan SDK 1.4.363.0. No game files.

run result
run_gpu_tests.py (whole suite, native wave64) 185/199, 14 skipped, 0 failed (main 49475796: 170/185)
same with LEONIDA_W64_EMULATE=1 185/199, 14 skipped, 0 failed
pytest tests/test_translate.py 562 passed, 4 skipped in 64.86s (0:01:04)
unittest discover -s tests/semantic -p test_alu_div_fixup.py 5 OK

No suite shader uses v_div_fixup_f16/f64, so the GPU rows show no regression, not the fixup ops on hardware; the semantic test covers those. The suite count is higher than main's because this head includes a newer main.

AI-assisted (Claude Code); I checked the results.

@ItsNotRexed
ItsNotRexed merged commit 299ec5f into ItsNotRexed:main Nov 23, 2026
3 of 10 checks passed
iurysf added a commit to iurysf/Leonida that referenced this pull request Nov 23, 2026
…Rexed#2477 is on main

ItsNotRexed#2477 (48a1a98) makes the translator return signed infinity for v_div_fixup_f64
with NaN(S0), as measured on RDNA2 gfx1036, which is what the reference already
modelled. The case is no longer a known divergence (as TreXito's review of ItsNotRexed#4536
observed: two tests fail on main because they expect the old divergence).

- Remove the div-fixup-nan-s0 entry, its reference skip and its dedicated --known
  program; NaN(S0) programs are compared like any other.
- Add a positive test that the reference and the translator give +-inf for qNaN,
  -qNaN and sNaN with both output signs.
- Rework the two --known tests so they no longer depend on div_fixup: the three
  remaining forms are excluded with a reason by default, run with --known, and a
  planted clamp-only translator error exits 1 and prints the shrunk program.
- docs/shader_validation.md no longer lists the div_fixup case.

Same change as 41525870 (prepared for ItsNotRexed#4586, which ItsNotRexed#2477 made redundant).

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
iurysf added a commit to iurysf/Leonida that referenced this pull request Nov 23, 2026
…nts on main (ItsNotRexed#2477)

TreXito's review of ItsNotRexed#4536 found 40 mismatches in 24000 programs (seeds 776401 and
776402, exp(S2) - exp(S1) >= 1024): the reference predates ItsNotRexed#2477 (48a1a98),
whose measured RDNA2 gfx1036 rules are in the docs/design.md v_div_fixup row.
The translator is the side that is right. Checked against that row and against
the v_div_fixup_f64 lowering in tools/rdna2spirv.py on main, not only against the
proposed patch:

- NaN in S2 or S1 passes through unchanged (a signalling NaN is not quieted with
  IEEE=0); the priority NaN(S2) over NaN(S1) is unchanged.
- The underflow cutoff is exp(S2) - exp(S1) <= -1076 (was < -1074).
- There is no overflow check (was >= 1024).
- NaN(S0) gives signed infinity, still below the underflow case.

test_div_fixup_table is updated row by row (gap 1074/1075 pass S0, 1076 gives a
signed zero, no overflow rows, unquieted sNaN) and docs/shader_validation.md
records the rule.

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.com>
iurysf added a commit to iurysf/Leonida that referenced this pull request Nov 23, 2026
Found while validating the ItsNotRexed#2477 follow-up on the RTX 4070: with the current
translator, `v_cmp_eq_i32_e32 vcc_lo, 0, 0; v_div_fmas_f64 v[8:9], s[4:5], -1.0, 1`
mismatched. The reference scaled by a fixed 2^64 when VCC is set; main now follows the
hardware-verified (gfx1036, 153/153 f64 stress cases) rule in the docs/design.md
v_div_fmas row: 2^+128 when exp(S2) > 1023 (|S2| >= 2.0), 2^-128 otherwise.

Same class of stale reference as the v_div_fixup_f64 one fixed in the previous
commit (TreXito's review of ItsNotRexed#4536). A table test pins both sides of the |S2| = 2.0
threshold and the VCC-clear case; reverting to the fixed 2^64 fails it.

AI-assisted (OpenBot team).

Co-authored-by: OpenBot <335604065+openbotgit[bot]@users.noreply.github.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.

8 participants