Skip to content

rdna2spirv: v_cvt_pkrtz_f16_f32 applies its VOP3 clamp and omod to each f16 half - #7683

Open
best2135 wants to merge 1 commit into
ItsNotRexed:mainfrom
best2135:hunt/xlat-pkrtz-mods
Open

best2135 wants to merge 1 commit into
ItsNotRexed:mainfrom
best2135:hunt/xlat-pkrtz-mods

Conversation

@best2135

Copy link
Copy Markdown

Claimed in the coordination issue as: #7333 (comment) (rdna2spirv v_cvt_pkrtz_f16_f32)

What and why

The bug: llvm-mc 23 for gfx1030 encodes VOP3 clamp and mul:2/mul:4/div:2 on v_cvt_pkrtz_f16_f32_e64. For example, v_cvt_pkrtz_f16_f32_e64 v0, v4, v5 clamp assembles to [0x00,0x80,0x2f,0xd5,0x04,0x0b,0x02,0x02]. The translator ignored mods for this op, so both modifiers were dropped without an error. With them dropped, pkrtz(1.5, -2.0) clamp gave 0xc0003e00 instead of 0x00003c00, so a saturate folded into the pack was lost.

The fix: each f16 half is now narrowed toward zero as before, then goes through the same omod-then-clamp path v_cvt_f16_f32 uses. That path was measured on RDNA2 (#718 and gfx1036, tools/cvt_f16_omod_probe.py). It is now a shared helper, Gen.cvt_f16_out_mods. v_cvt_f16_f32 emits the same code as before, and the _e32 form is unchanged.

Why the fuzzers missed it: tools/f32_fuzz.py lists the op in NO_OUT_MODS, and tools/valu_fuzz.py only emits _e32.

Contract table: there is a new row in docs/design.md, "v_cvt_pkrtz_f16_f32 output modifiers". It says plainly that the rule is not measured on hardware for this instruction: it follows the v_cvt_f16_f32 measurement. That includes a finite omod overflow giving ±inf in nearest MODE, even though the conversion itself rounds toward zero.

Fixture

tests/semantic/fixtures/pkrtz_out_mods.asm is hand-written and assembles cleanly with llvm-mc 23 for gfx1030. It is run by tests/semantic/test_pkrtz_out_mods.py:

word instruction expected before the fix
0 pkrtz(1.5, -2.0) clamp 0x00003c00 0xc0003e00
1 pkrtz(3.0, 0.75) mul:2 0x3e004600 0x3a004200
2 pkrtz(3.0, -1.0) div:2 0xb8003e00 0xbc004200
3 pkrtz(0.375, 0.125) clamp mul:4 0x38003c00 0x30003600

Running clamp before mul:4 would give 0x38003a00 for word 3, so that word also pins the order. The test file adds:

  • per-half clamp cases: ±1, FLT_MAX→65504→1.0, 1/3, ±0, ±inf
  • omod cases: overflow, e == 1 div:2, and an f16 denormal flushed by the omod
  • a check that the _e32 form is unchanged

The test fails on origin/main: 5 assertions, including the fixture words in the "before" column. It passes with the change.

Tested

  • python3 -I tests/semantic/test_pkrtz_out_mods.py: 4 tests OK. Run with LEONIDA_TOOLS_DIR pointed at origin/main's translator, the same file fails.
  • python3 -I scripts/run_semantic_tests.py tests/semantic:
  • spirv-val --target-env vulkan1.2 on the translated fixture: valid.
  • tests/test_design_contract_rows.py, test_docs_design_rows.py, test_docs_table_rows.py, test_docs_table_ids.py, test_docs_repo_paths.py, test_docs_repeated_lines.py, test_docs_list_items.py, test_semantic_translator_use.py: OK. test_docs_links.py fails the same way on main, on docs/dev-tools.md.
  • python3 -I scripts/check_no_game_data.py on all four changed files: exit 0.
  • Not run on a GPU. The runtime test programs are not built on this machine, which has an NVIDIA GeForce RTX 4070 Ti SUPER, driver 615.78.08, Linux. No AMD hardware here, so the hardware result for this instruction is still unmeasured; the row says so.
  • No game files, so no full-corpus count. The change only adds instructions to v_cvt_pkrtz_f16_f32_e64 with clamp or an omod; every other instruction is unaffected.

AI-assisted: yes

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.

🤖 Generated with Claude Code

…ch f16 half

llvm-mc for gfx1030 encodes clamp and mul:2/mul:4/div:2 on v_cvt_pkrtz_f16_f32_e64, but the translator
dropped both: pkrtz(1.5, -2.0) clamp gave 0xc0003e00 instead of 0x00003c00. Each half now goes through
the omod-then-clamp path v_cvt_f16_f32 already uses (Gen.cvt_f16_out_mods), after the toward-zero
narrowing. New contract row in docs/design.md; tests/semantic/test_pkrtz_out_mods.py with the
hand-written fixture pkrtz_out_mods.asm fails before the change and passes after.

AI-assisted.

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

Copy link
Copy Markdown

Tested 4b56fe5 on Arch Linux, Python 3.14.10, CPU only: pass — new tests/semantic/test_pkrtz_out_mods.py 4/4 OK (semantic fixture only, no GPU fixture in the PR). Negative control: merge-base translator → 9 failures. Full semantic suite: 448 files, 5960 tests, only the 2 files that are red on clean main fail (test_image_atomics_guard, test_image_store_d16 — fixed by open #7608). No game files, no GPU. AI-assisted.

@moistman7

Copy link
Copy Markdown

Measured on hardware: RDNA2 ignores VOP3 clamp and omod on v_cvt_pkrtz_f16_f32, so this PR changes outputs that main already gets right.

Setup: AMD Radeon iGPU (Ryzen 7 9800X3D, gfx1036), Windows 11, Adrenalin driver 32.0.21042.62. Ran natively through tools/hw_oracle (HIP runtime from the driver; AMD clang from rocm-sdk-core 10.0.0). PR head merged onto main d6f8a66.

Encoding check: llvm-mc 21.1.8 (-mcpu=gfx1036 -show-encoding) sets the bits. For example, v_cvt_pkrtz_f16_f32_e64 v2, v1, v4 clamp div:2 assembles to [0x02,0x80,0x2f,0xd5,0x01,0x09,0x02,0x18] (clamp bit 15, omod = 3).

Result: 30 input pairs × 7 modifier sets (clamp, mul:2, mul:4, div:2, clamp + each omod) × 2 MODEs (IEEE=0, DX10_CLAMP=1, FP_DENORM f16 = 0 and 3) × wave32/wave64 gave 840 results. Every one is bit-identical to the same pair with no modifier. IEEE=0 with f16 denormals flushed is the case where v_cvt_f16_f32 does apply omod (docs/design.md, tools/cvt_f16_omod_probe.py), so pkrtz does not follow that rule.

This PR's own test vectors, at MODE IEEE=0, DX10_CLAMP=1:

src0, src1 mods hardware main this PR
1.5, -2.0 clamp 0xC0003E00 0xC0003E00 0x00003C00
3.0, 0.75 mul:2 0x3A004200 0x3A004200 0x3E004600
3.0, -1.0 div:2 0xBC004200 0xBC004200 0xB8003E00
0.375, 0.125 clamp mul:4 0x30003600 0x30003600 0x38003C00

Control in the same kernel (src0 = 1.5, src1 = -2.0, seed 0xAAAA5555 in v2), which shows the harness does deliver a clamp bit:

v_cvt_f16_f32_e64 v2, v1               -> 0xaaaa3e00
v_cvt_f16_f32_e64 v2, v1 clamp         -> 0xaaaa3c00   (clamp honored)
v_cvt_pkrtz_f16_f32_e64 v2, v1, v4       -> 0xc0003e00
v_cvt_pkrtz_f16_f32_e64 v2, v1, v4 clamp -> 0xc0003e00   (ignored)
v_cvt_pkrtz_f16_f32_e64 v2, v1, v4 mul:2 -> 0xc0003e00   (ignored)

Steps: on an RDNA2 GPU, build one asm.Kernel([... "v_mov_b32_e32 v1, 0x3fc00000", "v_mov_b32_e32 v4, 0xc0000000", "v_cvt_pkrtz_f16_f32_e64 v2, v1, v4 clamp", "v_mov_b32_e32 v3, 0", "global_store_dword v3, v2, s[2:3]", "s_waitcnt vmcnt(0)", "s_endpgm"], wave=32, threads=1, words=1, raw=True, mode={"round16_64": 0, "denorm16_64": 0, "dx10_clamp": 1, "ieee": 0}) and pass it to runner.open_oracle().run([...], words=1). The stored word is 0xC0003E00, not 0x00003C00.

The table measures one RDNA2 part (gfx1036), not the console GPU. Still, the result is the same in all 840 cases, and main already matches it. It looks like pkrtz should keep ignoring these bits, and the contract table should say that was measured. Tested by me with AI assistance (Claude Code).

@kamilolejnik

Copy link
Copy Markdown

Tested 4b56fe5 on Debian 13 (CPython 3.13.5) and on Intel Iris Xe Graphics (TGL GT2, Xe-LP), Mesa ANV 26.3.1, Vulkan 1.4.363, Linux 6.12 (Arch container), llvm-mc 23.1.5, SPIRV-Tools v2026.4: pass.

  • CPU: test_pkrtz_out_mods 4 OK, and the other f16-conversion suites that touch the shared path (test_cvt_f16_omod, test_alu_cvt_f16, test_f16_int_convert, test_alu_sdwa_hw, test_value_semantics, test_valu_mul_f16_mode_round, test_f16_interp_fallback, test_wave64_halves, test_scalar_noop_waitcnt) all OK.

  • GPU: the four lines of tests/semantic/fixtures/pkrtz_out_mods.asm run as a wave32 compute dump (thread 0 stores v0..v3), translated by this branch and by its merge-base 7deb162, assembled for both gfx1013 and gfx1030 (same result either way), spirv-val OK:

    word instruction this PR on ANV merge-base on ANV PR table expected / before
    0 pkrtz(1.5, -2.0) clamp 0x00003c00 0xc0003e00 0x00003c00 / 0xc0003e00
    1 pkrtz(3.0, 0.75) mul:2 0x3e004600 0x3a004200 0x3e004600 / 0x3a004200
    2 pkrtz(3.0, -1.0) div:2 0xb8003e00 0xbc004200 0xb8003e00 / 0xbc004200
    3 pkrtz(0.375, 0.125) clamp mul:4 0x38003c00 0x30003600 0x38003c00 / 0x30003600

    So the translated SPIR-V gives on a real driver exactly what the CPU interpreter gives, before and after. This checks the translation, not the RDNA2 rule itself: as the new design.md row says, the rule still needs a gfx1013/gfx1036 hardware measurement.

Synthetic inputs only, no game files. AI-assisted (Claude Code).

@kamilolejnik

Copy link
Copy Markdown

Correction to my "pass" above, which I posted before reading @moistman7's hardware result: my run only shows the translated SPIR-V gives the same words as the CPU interpreter on ANV (for both the merge-base and this branch). It says nothing about what RDNA2 does. Against gfx1036 hardware, the merge-base words I got on ANV (0xc0003e00 0x3a004200 0xbc004200 0x30003600) are the ones that match, so on this evidence my result supports keeping main's behaviour, not this change.

@javdc

javdc commented Nov 23, 2026

Copy link
Copy Markdown

Tested 4b56fe5 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass

Command:

  • PYTHONPATH=. python3 -m pytest tests/semantic/test_pkrtz_out_mods.py -q (4 passed, 8 subtests passed)

AI-assisted (OpenCode)

@djasdh

djasdh commented Nov 23, 2026

Copy link
Copy Markdown

Tested 4b56fe515 on AMD Radeon RX 6900 XT (RDNA2, gfx1030, RADV NAVI21), Mesa 26.3.1, Vulkan 1.4.363, Arch Linux, llvm-mc 23.1.5, SPIRV-Tools 1.4.363.0, Python 3.14.10.

GPU suite (full) — 224/241 passed, 16 skipped, 1 failed (ms_wave_info_s3, same as main)
Translator suite (python -I scripts/run_semantic_tests.py tests/semantic) — 448 files, 5960 tests, 2 files failed

Two semantic files fail on this head: test_image_atomics_guard and test_image_store_d16. Both also fail on main (dde82f27e) on this machine with the same counts, so this branch does not introduce them — they are the stale-after-#4473/#6805 pair already tracked in #7284. ms_wave_info_s3 fails in the full GPU suite on main here as well (it passes with --filter ms_wave_info_s3; reported in #6497).

Counts and error classes only; no game data. AI-assisted.

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.

6 participants