Skip to content

rdna2spirv: normalize binary64 subnormals in v_frexp_{mant,exp}_f64 (gfx1036 measured) - #3041

Closed
TheAirborneCoder wants to merge 1 commit into
ItsNotRexed:mainfrom
TheAirborneCoder:shader-frexp-f64-subnorm
Closed

TheAirborneCoder wants to merge 1 commit into
ItsNotRexed:mainfrom
TheAirborneCoder:shader-frexp-f64-subnorm

Conversation

@TheAirborneCoder

Copy link
Copy Markdown
Contributor

Summary

Fixes v_frexp_mant_f64 and v_frexp_exp_i32_f64 in tools/rdna2spirv.py to normalize binary64 subnormals under RDNA2's default denorm_mode_16_64 = 3 mode, matching native RDNA2 (gfx1036, wave32 & wave64) hardware and the sibling v_frexp_mant_f16 / v_frexp_exp_i16_f16 lowering.

Claimed in #2891: #2891 (comment)

Problem

Previously, tools/rdna2spirv.py flushed all binary64 subnormals (e == 0, 52-bit fraction != 0) to ±0.0 / exponent 0 (docs/design.md noted this was unmeasured and copied the f32 denorm_mode_32 = 0 flush rule).

Measured natively on AMD Radeon(TM) Graphics (gfx1036), Windows driver 32.0.21042.62, tools/hw_oracle (wave32 and wave64 identical on every case):

  • Under RDNA2's default denorm_mode_16_64 = 3, IEEE = 0, DX10_CLAMP = 1, every non-zero binary64 subnormal is normalized into [0.5, 1.0) with sign preserved and exponent msb - 1073 (-1073..-1022):
    • +sub_min (0x0000000000000001) -> mant 0x3FE0000000000000 (+0.5), exp -1073
    • -sub_min (0x8000000000000001) -> mant 0xBFE0000000000000 (-0.5), exp -1073
    • +sub_max (0x000FFFFFFFFFFFFF) -> mant 0x3FEFFFFFFFFFFFFE, exp -1022
    • +sub_bit20 (0x0000000000100000) -> mant 0x3FE0000000000000 (+0.5), exp -1053
    • All 71 measured cases (every single bit position 0..51, lo/hi boundary-crossing multi-bit subnormals, negative subnormals, ±0, normals, ±inf, qNaN, sNaN, plus VOP3 abs/neg/clamp) matched math.frexp bit-for-bit across wave32 and wave64.
  • Hardware only flushes binary64 subnormals when denorm_mode_16_64 = 0.

Changes

  • tools/rdna2spirv.py:
    • Implemented binary64 subnormal normalization in pure 32-bit SPIR-V (OpExtInst %int %glsl FindUMsb + 32-bit shifts with all shift counts masked & 31 to avoid SPIR-V shift UB on unselected paths) — no OpCapability Float64 or OpCapability Int64 required unless VOP3 output modifiers are used.
    • Leaves f64_literal decoding untouched to avoid overlapping BUILD-828: decode literal words in unary binary64 shader operations #2863.
  • tests/semantic/test_alu_frexp_f64.py:
    • Updated reference helpers (ref_mant, ref_exp) and test_zero_subnormals_and_specials for denorm_mode_16_64 = 3.
    • Added test_subnormals_match_gfx1036 covering all 52 single-bit subnormal positions (0..51), lo/hi boundary-crossing multi-bit subnormals, negative subnormals, VOP3 abs/neg, and exact math.ldexp(m, e) == x reconstruction identity.
  • docs/design.md:
    • Updated the v_frexp_mant_f64, v_frexp_exp_i32_f64 contract row with the native gfx1036 measurements and updated rule.

Testing & Disclosure

  • AI-assisted: Yes (authored and verified with coding-agent assistance).
  • GPU / Driver: AMD Radeon(TM) Graphics (gfx1036), Windows driver 32.0.21042.62 (71/71 wave32 & wave64 hardware oracle verification).
  • Unit & regression tests run locally (no game files):
    • python -I tests/semantic/test_alu_frexp_f64.py -v — 9/9 OK (including all 63 subnormal inputs x 2 ops + reconstruction identity + VOP3 abs/neg)
    • python -I tests/semantic/test_alu_frexp_ldexp.py — 11/11 OK
    • python -I tests/semantic/test_alu_frexp_ldexp_f16.py — 23/23 OK
    • python -I tests/test_shader_checks.py — 69/69 OK
    • python -I tests/test_docs_links.py, tests/test_docs_list_items.py, tests/test_docs_repo_paths.py, tests/test_text_write_line_endings.py — all OK
    • python -I tests/semantic/regen_fixtures.py --check — OK
    • python -m compileall -q make_game.py tools tests — OK
    • Note: spirv-as / spirv-val are not installed locally, so validate_spirv skips locally and runs in CI.
  • No game data: All inputs and test fixtures are hand-written synthetic values.

@AC-Storm-YT

Copy link
Copy Markdown
Contributor

Tested 42d5197, Windows 11, no game files.

Independent hardware check, two AMD GPUs and two more drivers. I ran the raw v_frexp_mant_f64 / v_frexp_exp_i32_f64 through OpenCL inline assembly on 123 inputs (all 52 single-bit subnormals and their negatives, ±sub_max, subnormals crossing the 32-bit halves, ±0, the smallest normal, normals, ±inf, qNaN, sNaN), each with s_denorm_mode 0xf (denorm_mode_16_64 = 3) and 0x3 (= 0), plus the |x| and -x source modifiers, against frexp on the CPU:

GPU driver denormals on: matches frexp |x|, -x denormals off: subnormals → ±0, exp 0
Ryzen 7 9800X3D iGPU (gfx1036, RDNA2) 32.0.21045.5002 123/123 123/123 111/111
RX 9070 XT (gfx1201, RDNA4) 32.0.31041.1004 123/123 123/123 111/111

Your quoted values match exactly (+sub_min → 0x3FE0000000000000, -1073; +sub_max → 0x3FEFFFFFFFFFFFFE, -1022), and the flush only happens with denorm_mode_16_64 = 0, as you say. Both kernels started with denorm_mode_16_64 = 3.

The PR's tests, with spirv-as/spirv-val from Vulkan SDK 1.4.363.1 on PATH, so the validation you had to skip ran here:

  • test_alu_frexp_f64.py 9/9 OK, nothing skipped; test_plain_needs_no_float64_and_validates and test_mant_output_modifiers_and_validation pass spirv-val.
  • test_alu_frexp_ldexp.py 11/11, test_alu_frexp_ldexp_f16.py 23/23.
  • Negative control: with main's tools/rdna2spirv.py, test_subnormals_match_gfx1036 fails on the subnormal cases.
  • Full python -m unittest discover -s tests/semantic: 4143 tests, OK.

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

Verified on the PR head: python -I tests/semantic/test_alu_frexp_f64.py -> 9 tests OK. Overlaying the PR test on current main gives 64 failures, so the subnormal coverage is genuinely new. The 52 single-bit subnormals plus the multi-bit boundary patterns agree with an independent math.frexp reference, including sign, VOP3 abs/neg, and the exponent edge cases; the doc examples (0x0000000000000001 -> 0x3FE0000000000000 / -1073, 0x000FFFFFFFFFFFFF -> 0x3FEFFFFFFFFFFFFE / -1022) are correct. The shift paths mask counts to 0..31, so no SPIR-V shift UB. No game data or absolute paths. Approve.

Non-blocking: the 71 measured cases are all from gfx1036 (not the console's gfx1013); the new test holds the derived expectations rather than the raw measured words, but it does compare them against an independent reference.

@CaravelaLabs

Copy link
Copy Markdown
Contributor

Tested 42d51975 merged onto main 16282101 on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5, subgroup 32..64), Mesa 25.2.8, Ubuntu 24.04, LLVM 23.1.6, SPIRV-Tools 2026.4: pass.

  • python -I scripts/run_gpu_tests.py --build runtime/build/linux: 172/189 passed, 15 skipped, identical per test to plain main 16282101 on this machine. The two failures in both runs, cs_signed_zero_nan and cs_w32_salu64, are the known Mesa 25.2 RADV ones (GPU test results (one comment per GPU and driver) #166, docs/testing.md).
  • tests/semantic/test_alu_frexp_f64.py: 9 passed, 142 subtests.
  • GPU probe: a throwaway hand-written compute fixture (not committed, no game data), translated by this branch and run with vk_compute_test (wave32 program). v_frexp_mant_f64/v_frexp_exp_i32_f64 of +sub_min → 0x3fe00000_00000000 / -1073, -sub_min → hi 0xbfe00000, 0x000fffff_ffffffff → 0x3fefffff_fffffffe / -1022, 0x00000000_00100000 → exp -1053: all 8 words match your gfx1036 measurements. Plain main returns 0 / 0x80000000 (flushed) for the same inputs, so the probe does tell the two apart.

This is a Vulkan run of the translated SPIR-V on RDNA 3.5, not a native-ISA run: it shows the 32-bit-only lowering survives a real driver compiler (ACO) bit-exactly, while the RDNA2 behaviour itself rests on your gfx1036 oracle data. AI-assisted (Claude Code).

@Yourrespawn

Copy link
Copy Markdown
Contributor

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

  • Semantic suite: 4143 OK (Python 3.12.10), including the updated test_alu_frexp_f64 subnormal-normalization coverage
  • run_gpu_tests.py: 143/189 passed, 46 skipped (native wave64, expected on this 32-lane GPU) — no regressions from the f64 frexp normalization change; no new GPU fixture in this PR (the reference is the author's gfx1036 hw_oracle run, the CPU side is the semantic suite), so this confirms the whole suite stays green with the fix on NVIDIA.

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

@evilclanker9000

Copy link
Copy Markdown
Contributor

Tested 42d5197 on Windows 11, AMD Radeon iGPU gfx1036 (driver 26.10.1, subgroup 32..64) and RTX 5080 (driver 616.92, subgroup 32): translator checks plus the GPU suite (LEONIDA_VK_DEVICE set per run, device line confirmed in the runner output, spirv-as/spirv-val and llvm-mc available).

  • python -m unittest discover -s tests/semantic: 4143 tests OK, including test_alu_frexp_f64.py (9/9), also run alone; test_alu_frexp_ldexp.py 11/11, test_alu_frexp_ldexp_f16.py 23/23, tests/test_shader_checks.py 69/69, regen_fixtures.py --check clean (the last three run on the PR head as listed in the PR).
  • scripts/run_gpu_tests.py (full suite, no failures): AMD Radeon gfx1036: 174/189 passed, 15 skipped; RTX 5080: 143/189 passed, 46 skipped (skips are tests the device cannot run, e.g. wave64-only). --filter f64 (ps_f64_mods, the only f64 pixel test): 4096/4096 exact on both.
    Limit: this PR adds no GPU test of v_frexp_mant_f64 / v_frexp_exp_i32_f64 on subnormals, so the run shows no regression on either GPU but does not re-measure the subnormal behaviour on hardware; I did not write a new shader to do so.

AI-assisted.

dev-ewann pushed a commit to dev-ewann/Leonida that referenced this pull request Nov 22, 2026
# Conflicts:
#	docs/design.md
@iurysf

iurysf commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Re-tested d92f199c (head moved since the earlier results) on Windows 11, Python 3.14, llvm-mc 23.1.6, CPU only: pass. test_alu_frexp_f64.py 9 tests OK; with only tools/rdna2spirv.py restored to main: 64 failures. Not run: full semantic suite, GPU tests. Tested with AI assistance (Claude); synthetic shaders only, no game data.

@TheAirborneCoder

Copy link
Copy Markdown
Contributor Author

Already merged into main at 73697f1 (from pre-rebase head 42d5197); closing this PR since GitHub didn't auto-close after the rebase force-push.

ItsNotRexed added a commit that referenced this pull request Nov 23, 2026
# Conflicts:
#	docs/design.md
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.

7 participants