Skip to content

feat(rdna2spirv): measure and support VOP3 output modifiers on v_frexp/ldexp_f16 - #4311

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vadson159:shader/frexp-ldexp-f16-omod
Nov 23, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
Vadson159:shader/frexp-ldexp-f16-omod

Conversation

@Vadson159

Copy link
Copy Markdown
Contributor

AI-assisted (Gemini). Claimed in #4084 (comment) (no overlap with #1970 or #2171; no game files used).

Summary

  • Native RDNA2 hardware probe (tools/frexp_ldexp_f16_mods_probe.py): Measures VOP3 clamp, mul:2, mul:4, div:2, and clamp + mul:*/div:* across 30 binary16 edge inputs (and 5 exponents for v_ldexp_f16), source abs/neg modifiers, dirty upper source halves, and destination high-half preservation across wave32 and wave64 under console MODE (0x1c0), flush control (0x100), and IEEE control (0x3c0).
  • Translator (tools/rdna2spirv.py): Accepts clamp, mul:2, mul:4, div:2, and op_sel: on v_frexp_mant_f16, v_frexp_exp_i16_f16, and v_ldexp_f16:
    • Under console MODE (0x1c0: IEEE=0, DX10_CLAMP=1, denorm_mode_16_64=3), mul:2/mul:4/div:2 are hardware no-ops on v_frexp_mant_f16 and v_ldexp_f16 (emitting byte-identical SPIR-V), while clamp saturates to [0.0, 1.0] both alone and combined with mul:*/div:*.
    • On v_frexp_exp_i16_f16 (16-bit integer result), both clamp and mul:2/mul:4/div:2 are hardware no-ops across every MODE (0x1c0, 0x100, 0x3c0).
    • Unmodelled modifiers (e.g., mul:8, div:4, op_sel_hi:*, neg_lo:*, neg_hi:*) continue to raise Unsupported.
  • Contract documentation (docs/design.md): Updated the v_frexp_mant_f16, v_frexp_exp_i16_f16, v_ldexp_f16 row with the measured rules, counts, GPU/driver info, and test references.
  • Tests (tests/test_frexp_ldexp_f16_mods_probe.py, tests/semantic/test_frexp_ldexp_f16_mods.py, tests/semantic/test_alu_frexp_ldexp_f16.py): Unit, translator + spirv_mini_interp, byte-identical SPIR-V, op_sel, inactive-EXEC, Unsupported, and spirv-val validation checks.

Native RDNA2 Hardware Measurement

  • GPU & Driver: AMD Radeon(TM) Graphics (gfx1036), Windows driver 32.0.21045.29003
  • python -I tools/frexp_ldexp_f16_mods_probe.py --hw-arch gfx1036:
    total_words=3466 (grid=3360, src_mods=22, controls=84) across 2 wave(s) x 3 op(s): mismatches=0, wave_diffs=0, omod_diffs=0 (exit 0)
  • MODE=0x100 (denorm16_64=0) confirmed mul:2/mul:4/div:2 scale v_frexp_mant_f16 and v_ldexp_f16, while v_frexp_exp_i16_f16 still ignores clamp and mul:*/div:*. MODE=0x3c0 (IEEE=1) confirmed mul:*/div:* remain no-ops and clamp still saturates v_frexp_mant_f16 and v_ldexp_f16.

Vulkan GPU Verification

Synthetic wave32 compute shader cs_frexp_ldexp_f16_mods translated by tools/rdna2spirv.py and validated with spirv-val --target-env vulkan1.3 (VK_INSTANCE_LAYERS=VK_LAYER_KHRONOS_validation, VKC_SUBGROUP_SIZE=32):

  • AMD Radeon RX 9070 XT (driver 32.0.32029.2010): vk_pipeline_check 1/1 ok; vk_compute_test 1 groups x 64: 0 mismatches, 8/8 exact words (0xdead3800 0xdead0000 0xdead3c00 0xdead0004 0xdeadffe9 0xdead3c00 0xdead3c00 0xdead0000), 0 validation errors.
  • AMD Radeon(TM) Graphics (gfx1036, driver 32.0.21045.29003): vk_pipeline_check 1/1 ok; vk_compute_test 1 groups x 64: 0 mismatches, 8/8 exact words, 0 validation errors.

CPU & Repository Checks

  • python -I tools/frexp_ldexp_f16_mods_probe.py --self-test -> exit 0 (1733 synthetic words checked)
  • python -I tests/test_frexp_ldexp_f16_mods_probe.py -> 14/14 passed (with LEONIDA_AMD_CLANG wrapper; 1 skipped without AMD clang)
  • python -m unittest discover -s tests/semantic -p test_*frexp_ldexp_f16*.py -v -> 32/32 passed (including spirv-val --target-env vulkan1.3)
  • python -m unittest discover -s tests/semantic -v -> 7,463 tests run (7 failures, 2 errors all in pre-existing unrelated files on aa2988cc: test_cmp_class_f64.py, test_loop_ballot_barrier.py, test_mutations.py)
  • python -m compileall -q make_game.py tools tests & python -I make_game.py --help -> exit 0
  • python -I tests/test_semantic_translator_use.py (3/3), tests/test_text_write_line_endings.py (3/3), tests/test_tool_startup.py (2/2), tests/test_tool_stdout_encoding.py (4/4), tests/test_ci_registration.py (8/8), tests/test_i18n_index.py (5/5), tests/semantic/test_spirv_tools_required.py with LEONIDA_REQUIRE_SPIRV_TOOLS=1 (1/1) -> all passed
  • python -I tests/test_docs_design_rows.py (2/2), tests/test_design_contract_rows.py (4/4), tests/test_docs_links.py (6/6), tests/test_docs_repo_paths.py (2/2), tests/test_docs_table_rows.py (2/2), tests/test_docs_cli_flags.py (3/3), python -I scripts/check_no_game_data.py -> exit 0

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

Ran this at 2c3cc2a8 on Windows 11:

  • tests/semantic/test_alu_frexp_ldexp_f16.py -> 25 tests, OK (existing coverage, still passes).
  • tests/semantic/test_frexp_ldexp_f16_mods.py -> 7 tests, OK (new output-modifier cases).
  • tests/test_frexp_ldexp_f16_mods_probe.py -> 14 tests, OK (1 skipped).
  • git diff --check clean.

The translator now accepts mul:2, mul:4, div:2 and clamp on these three ops according to the measured gfx1036 behavior: no-ops on v_frexp_exp_i16_f16, no-ops on the mantissa/v_ldexp_f16 results under console MODE, with clamp still clamping where the existing path did. Other modifiers are still refused. I cannot reproduce the hardware measurement on this NVIDIA host; the semantic/probe tests pin it. No blocking issue.

@MKmajster

Copy link
Copy Markdown

Tested 2c3cc2a exhaustively on a non-AMD GPU: NVIDIA GeForce RTX 3070 (Ampere, wave32), driver 617.42, Windows 11 24H2. The probe measures 30 edge inputs; this runs every binary16 input. All 56 programs, 3,670,016 words, exact against this branch's expected_word (console MODE).

How: one compute program per (op, mod[, n]): v_frexp_mant_f16 and v_frexp_exp_i16_f16 × the 8 MODS ("", clamp, mul:2, mul:4, div:2, clamp mul:2/4, clamp div:2), and v_ldexp_f16 × the 8 MODS × the 5 LDEXP_EXPS. Each is a wave32 scalar loop: lane L of iteration k takes x = 32k + L with a dirty upper half (v1 = 0xabcd0000 | x, v2 = 0x1234xxxx). v8 is preset to 0xdeadbeef, and the instruction comes from the probe's own _emit_inst, so the upper-half preservation is checked on every word. Assembled with llvm-mc 23.1.6 (gfx1013), translated with this branch's rdna2spirv.py, run with vk_compute_test ... 1 dump.

For comparison, main aa2988c already translates v_frexp_mant_f16 plain and clamp (65536/65536 each here). It refuses the mul:*/div:* forms this PR adds (v_frexp_exp_i16_f16 mul:2, v_ldexp_f16 clamp mul:4: not translated).

No game files. AI-assisted (Claude).

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested 2c3cc2a8 on Linux (WSL2, Python 3.13.5, spirv-as/spirv-val, CPU only, no GPU, no game files): pass (native hardware probe run not reproduced here).

  • Code read (tools/rdna2spirv.py, docs/design.md):
    • Accepts clamp, mul:2, mul:4, div:2, and op_sel: on v_frexp_mant_f16, v_frexp_exp_i16_f16, and v_ldexp_f16 while continuing to reject any other modifier; applies clamp only to the two float-result ops and treats mul:*/div:* (and clamp on the integer-result v_frexp_exp_i16_f16) as no-ops matching the measured console-MODE (0x1c0) hardware behaviour.
  • Checks run (clean worktree on 03b09a08):
    • tests/semantic/test_alu_frexp_ldexp_f16.py, tests/semantic/test_frexp_ldexp_f16_mods.py, tests/test_frexp_ldexp_f16_mods_probe.py (with spirv-as/spirv-val on PATH): 46 tests run, 45 passed, 1 skipped (amd_clang unavailable), including test_spirv_validates_with_spirv_val and test_noop_omod_emits_byte_identical_spirv.
    • test_semantic_translator_use.py (3/3), git diff --check, test_docs_links.py (6/6), test_docs_repo_paths.py (2/2), test_text_write_line_endings.py (3/3), test_text_encoding.py (2/2): all pass.
  • Not run here: native RDNA2 tools/frexp_ldexp_f16_mods_probe.py hardware probe.

AI-assisted review.

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

P2 — contradictory FP16 denormal contract (docs/design.md:388)

This edited row says both that f16 denormals are “read as zero” and that they are kept and normalized (v_frexp_mant_f16(0x0001) = 0.5). It also says v_ldexp_f16 produces no denormals, then gives measured denormal results such as 0x0200 and 0x0002. These statements cannot all describe the console MODE documented in this same row, and they make the translator contract unsafe to follow.

Remove the stale “read as zero” and “no denormal results” clauses, keeping the later measured behavior for denorm_mode_16_64=3. The row is the changed contract for these opcodes.

@SebSilent

Copy link
Copy Markdown
Contributor

Independent second-check of 2c3cc2a, Linux, CPU only (Turing GPU, non-AMD, so the native gfx1036 probe measurements themselves are outside what I can repeat here, and your own probe run covers those):

  • python -I tests/test_frexp_ldexp_f16_mods_probe.py, tests/semantic/test_alu_frexp_ldexp_f16.py, tests/semantic/test_frexp_ldexp_f16_mods.py: OK.
  • compileall clean; docs tests (links, repo paths, design rows, list items) OK.
  • Full semantic suite on this head: 7463 tests ran (the seven new f16 cases included). The 9 failures are exactly the set the merge base already carries and each has an owner or PR: loop_ballot_barrier x5 (tests/semantic: restore test_loop_ballot_barrier after structured wave64 emulation #3953), mutation anchors x2 (tests/semantic: re-anchor the v_mul_legacy_f32 mutation after the flush change #3883), cmp_class_f64 rejected-forms x2 (TEST-853, claimed). Nothing new, nothing in your files.
  • The rdna2spirv change is 7 precise lines: the reject list for the three f16 ops becomes a whitelist of the measured modifiers (clamp, mul:2, mul:4, div:2, op_sel) with unmodelled forms still refusing, which matches the rewritten docs/design.md row.

Code read of the diff and its tests: clean. AI-assisted.

@dieguesmosken

Copy link
Copy Markdown

Tested 2c3cc2a (PR head, based on an older main) on NVIDIA GeForce RTX 5050 (Blackwell), NVIDIA 595.104.02, Pop!_OS 24.04: pass.

run_gpu_tests.py: 164/210 passed, 46 skipped, 0 failed (same as main at 03b09a0). build.py: 214 shaders, 214 translated, 214 pass spirv-val.
Regression check only: there is no committed shader that exercises the new v_frexp/v_ldexp f16 modifiers on the GPU, so this does not verify them on NVIDIA.

No game data. AI-assisted.

@Krashgamer

Copy link
Copy Markdown
Contributor

Heads-up from #2171 (SHADER-354, approved): both PRs rewrite the same docs/design.md row (v_frexp_mant_f16, v_frexp_exp_i16_f16, v_ldexp_f16, line 385), so whichever merges second gets a conflict. The two changes are compatible: #2171 only extends the row's last sentence with the GPU check, and this PR rewrites the modifier sentences and the CPU check. If #2171 lands first, the last sentence could read:

Checked on the CPU and with spirv-val by tests/semantic/test_alu_frexp_ldexp_f16.py, tests/semantic/test_frexp_ldexp_f16_mods.py, and tests/test_frexp_ldexp_f16_mods_probe.py, and on the GPU by runtime/test/shaders/cs_f16_frexp_ldexp.asm: check.py and run_gpu_tests.py compare the translated program's words for all 65,536 inputs of the three ops, v_ldexp_f16 under 21 exponents, with the same rule (exact on an RTX 3080, NVIDIA 610.88); not run on the shader corpus.

If this one lands first, I'll fold your sentence into #2171 the same way.

@ItsNotRexed
ItsNotRexed merged commit 7878871 into ItsNotRexed:main Nov 23, 2026
3 of 10 checks passed
Krashgamer added a commit to Krashgamer/Leonida that referenced this pull request Nov 23, 2026
Conflicts after ItsNotRexed#4311 and main's cs_fmt_all:
- docs/design.md: keep main's f16 frexp/ldexp row (ItsNotRexed#4311) and extend its last
  sentence with this PR's GPU check.
- scripts/run_gpu_tests.py: main's verdict_line() for the case above, plus the
  cs_f16_frexp_ldexp branch.
- tests/test_shader_checks.py: both sides added one case to TOTAL (13 -> 14),
  so the merged total is 15.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bielfreedom

Copy link
Copy Markdown

Corpus check of 2c3cc2a8 (base aa2988cc), full game corpus, 26,503 disassembled shaders. The reference is a translation at 03b09a08, which I had found byte-identical to aa2988cc:

  • 26,503 translated on both sides; 26,422 spirv-val valid on both sides. The 81 failures are the %fmt_size_tab issue fixed on main by 06b47de1, not this PR;
  • byte-identical: 0 modules differ, index.json identical.

That is expected. No listing in the corpus contains v_frexp_mant_f16, v_frexp_exp_i16_f16 or v_ldexp_f16 (0 of 26,503), with or without modifiers. The new rules are covered only by the probe and the semantic tests.

AI-assisted (Claude Code). Counts only.

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.

10 participants