Skip to content

rdna2spirv: widen packed f16 sources with integer ops, not UnpackHalf2x16 (SHADER-777 follow-up, RDNA4) - #4779

Open
amarovndres wants to merge 3 commits into
ItsNotRexed:mainfrom
amarovndres:pk-f16-int-widen
Open

amarovndres wants to merge 3 commits into
ItsNotRexed:mainfrom
amarovndres:pk-f16-int-widen

Conversation

@amarovndres

@amarovndres amarovndres commented Nov 23, 2026 •

Copy link
Copy Markdown

Claimed in the coordination issue as: new SHADER item, SHADER-777 follow-up (claim, finding with the data)

Depends on: #4461, #4012, #4488

What and why

On the AMD Windows driver 26.10.1 (RX 9060 XT, RDNA4), in wave32, v_pk_mul_f16 gives wrong halves when a source register's high half was just written by a 16-bit op through op_sel from a lane-derived value, once registers are written again later. The smallest repro is valu_fuzz --family f16 --seed 3, program 1009:

v_mov_b32_e32 v31, 0xc150a000
v_max_i16_e64 v31, v20, v25 op_sel:[0,0,1]        ; v20 lane-derived
v_pk_mul_f16 v35, v32, v31 op_sel:[0,0] op_sel_hi:[0,1]
v_mov_b32_e32 v31, 0

It gives 0xbcb64000 instead of 0xbcb02000. The failing module's SPIR-V is the passing one's plus a store placed after v35 is computed, and both pass spirv-val, so this is a driver miscompile. #4461's shift lowering doesn't reach it. On the failing module, rewriting only the UnpackHalf2x16 that widens v31 as integer ops gives the right word. Replacing the producer's OpBitFieldSExtract with shifts, splitting the OpFMul, or OpBitFieldInsert for the high-half write does not.

  • tools/rdna2spirv.py: Gen.f16_widen widens one f16 half exactly with integer ops: a normal half moves its exponent and mantissa into place (+112 on the exponent), ±inf and NaN keep their payload, and ±0 or denormals are sign × mantissa × 2^-24. The packed f16 ops (v_pk_add/mul/max/min/fma_f16) use it for every source half instead of UnpackHalf2x16. The values are the same; only the instructions change.
  • docs/design.md: a contract row, "Packed f16 operand widening", with how it was confirmed. The "f16 rounding" row now says packed-f16 reads no longer use UnpackHalf2x16.
  • GPU test cs_w32_pk_mul_hi: four producers (v_max_i16, v_add_nc_u16, v_mul_lo_u16, v_max_u16) into v_pk_mul_f16, plus a v_pk_add_f16 control. Words are derived in the .asm header.
  • tests/semantic/test_pk_f16_widen.py: each half of 142 values × 1.0 comes back unchanged (±0, denormals, normals, ±inf), in both op_sel orders. NaN stays NaN. No packed f16 op emits UnpackHalf2x16 (this last check fails on main).

#4012's opt-in --native-f16 path is separate; I haven't checked how it interacts with this. #4488 adds a fixture for f16 denormal operands of v_pk_fma_f16, which this widening also handles.

Tested

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). Test programs built with llvm-mingw, llvm-mc 23.1.6 (conda-forge).

check main this PR
valu_fuzz --family f16 --programs 2000, seeds 1-3, wave32 / wave64 5,999/6,000 / 6,000/6,000 (3:1009:32 fails) 6,000/6,000 / 6,000/6,000
producer → consumer matrix (5 × 5; a later write must not change v35), wave32 5 of 25 pairs miscompile, all into v_pk_mul_f16 0 of 25
cs_w32_pk_mul_hi FAIL, 5 of 8 words PASS 8/8, natively and with LEONIDA_W64_EMULATE=1
scripts/run_gpu_tests.py, full suite 196/210, 14 skipped 197/211, 14 skipped, 0 failed; on 98fa503f 203/218, 15 skipped, 0 failed
tests/semantic with every translated module through spirv-val 7,456 tests, 7 failures + 2 errors (pre-existing), 7,321 modules, 0 invalid the same 9, 7,321 modules, 0 invalid
tests/test_shader_checks.py — 75/75

Not measured:

  • The corpus (translated and spirv-val counts, and how much larger packed-f16 shaders get: each widened half is about 20 integer ops instead of one shared UnpackHalf2x16).
  • RDNA2 and older AMD drivers, NVIDIA, Intel, and RADV beyond the emulate-mode run above.
  • The other UnpackHalf2x16 uses (Gen.f16_in for scalar f16 ops, interpolants, d16 results). They are unchanged, and I have no repro for them.

AI-assisted: yes (Claude Code)

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

amarovndres and others added 2 commits November 23, 2026 01:02
…2x16 (SHADER-777 follow-up)

On the AMD Windows driver (26.10.1, RX 9060 XT / RDNA4) in wave32, UnpackHalf2x16
of a register whose high half a 16-bit op had just written through op_sel from a
lane-derived value gave wrong halves when registers were written again later:
v_max_i16 v31.hi, v_pk_mul_f16 reading it, v_mov_b32 v31, 0 gave 0xbcb64000 for
0xbcb02000 (valu_fuzz --family f16 --seed 3, program 1009). The SPIR-V of the
failing module only adds a store after the result, so it is a miscompile.
Rewriting that UnpackHalf2x16 with integer ops gave the right word; replacing
OpBitFieldSExtract, splitting the OpFMul or OpBitFieldInsert did not.

Gen.f16_widen widens one half exactly: normal halves move exponent and mantissa
into place, +-inf and NaN keep their payload, and a zero exponent field (+-0,
denormals) is sign * mantissa * 2^-24. The packed f16 ops (v_pk_add/mul/max/
min/fma_f16) use it for every source half.

- tests/semantic/test_pk_f16_widen.py: each half of 142 values by 1.0 unchanged,
  both op_sel orders, NaN stays NaN, no UnpackHalf2x16 left in packed f16 ops
  (that one fails on the old translation).
- GPU test cs_w32_pk_mul_hi: four producers into v_pk_mul_f16 plus a
  v_pk_add_f16 control. On the RX 9060 XT it fails on the old translation
  (5 of 8 words) and passes with this change, natively and with
  LEONIDA_W64_EMULATE=1.
- docs/design.md: a contract row for the widening.

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

@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 4e0808492 on Windows 11, RTX 5070 Ti:

  • tests/semantic/test_pk_f16_widen.py -> 4 tests, OK.
  • GPU: run_gpu_tests.py --filter pk_mul_hi -> 1/1 passed, cs_w32_pk_mul_hi -> 0xbcb02000 0xf8b02000 0xc0002000 0xbcb02000 0x3180bc08 0xbcb02000 0x3c00 0x0; translator 1/1 + spirv-val.
  • git diff --check clean.

Packed f16 sources are now widened with integer ops instead of UnpackHalf2x16, matching the RDNA4 measurement (the SHADER-777 follow-up); the new GPU fixture and semantic tests keep the exact words. No blocking issue.

@chpeu

chpeu commented Nov 23, 2026

Copy link
Copy Markdown

Tested 4e080849 on Linux, no game files. Two of our three measurements went red on this head, with one common
cause, and it is a toolchain-version problem rather than a wrong translation.

1. The GPU suite stopped before running anything.
LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux --llvm-mc /usr/lib/llvm-20/bin/llvm-mc

RuntimeError: llvm-mc failed on cs_w32_pk_mul_hi:
<stdin>:30:27: error: not a valid operand.
  v_max_i16_e64 v20, v2, v3 op_sel:[0,0,1]
                          ^
<stdin>:38:30: error: not a valid operand.
  v_mul_lo_u16_e64 v22, v2, v3 op_sel:[0,0,1]
<stdin>:42:27: error: not a valid operand.
  v_max_u16_e64 v23, v2, v3 op_sel:[0,0,1]

assemble_to_s raises, so the run ends there: 0 of the 217 programs ran on this box, in both the emulated and
the default mode. On main 98fa503f, the same machine and the same command run 201/217 passed, 16 skipped,
0 failed
(170/217 without the variable), so this is your new runtime/test/shaders/cs_w32_pk_mul_hi.asm meeting
this box's assembler, not the machine being broken.

2. The translator pytest job sees the same thing — 2 new failures, both on your new program.
The 31 pytest-marked files as CI's translator-tests job runs them:

  • your branch: 1637 passed, 37 skipped, 2 failed
  • main, minutes earlier: 1637 passed, 37 skipped, 0 failed
FAILED tests/test_translate.py::test_gpu_samples_translate_and_validate[cs_w32_pk_mul_hi.asm]
FAILED tests/test_translate.py::test_wave64_emulate_uses_no_subgroup_ops[cs_w32_pk_mul_hi.asm]
>   assert r.returncode == 0, r.stderr
E   AssertionError: <stdin>:30:27: error: not a valid operand.
E     v_max_i16_e64 v20, v2, v3 op_sel:[0,0,1]

(tests/conftest.py:180, calling /usr/lib/llvm-20/bin/llvm-mc -triple=amdgcn--amdpal -mcpu=gfx1013 …)

No other fixture in the tree uses op_sel on a 16-bit instruction — your new one is the first — so LLVM 20's
assembler is the boundary here. This box has only llvm-20 (Ubuntu LLVM 20.1.2); I cannot test 21 to confirm that
newer versions accept it, and the other reporters on this project quote llvm-mc 21.1.8. Worth either widening the
fixture to a form LLVM 20 accepts, or saying in the docs that this program needs llvm-mc ≥ 21 — otherwise every
contributor on an Ubuntu 24.04 box loses the whole GPU suite, not just the new case.

3. The semantic side is clean. python -m unittest discover -s tests/semantic

  • your branch: 6249 tests, 4 failures, 2 errors in 456 s;
  • main, same command: 6245 tests, 4 failures, 2 errors in 451 s.

Four extra tests (yours) and the identical failing set — tests/semantic/test_pk_f16_widen.py itself is green:
python -m unittest discover -s tests/semantic -p test_pk_f16_widen.py → 4 tests OK. Note the whole folder changed
on main since yesterday (6245 tests here against 7456 the day before), which is why I measured both sides in the
same session rather than quoting old numbers.

Machine: Linux Mint 22 (Ubuntu 24.04 base), LLVM 20.1.2, SPIRV-Tools 2025.1~rc1, GCC 13.3, Python 3.12.3 with pytest
9.2.0 and numpy 2.5.5, Vulkan driver 615.71.09, RTX 3080; built with cmake -S runtime -B runtime/build/linux -DLEONIDA_VULKAN_HEADERS_FETCH=ON. The RTX 5080 is holding another job (186 MiB free) so it was not used.
AI-assisted testing, output read through by hand.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 4e08084 merged with main 98fa503 (PR was already up to date with main) on Linux WSL2, Python 3.12.16: pass (CPU/semantic only; GPU runs in run_gpu_tests.py / check.py skipped, so the cs_w32_pk_mul_hi words and the driver bug itself are not verified here)

  • tests/semantic/test_pk_f16_widen.py: 4/4 pass.
  • Other semantic tests: test_*pk*.py 93/93, test_*f16*.py 797/797 (1 skipped), test_fixtures.py and test_rule_gpu_fixtures.py pass. tests/test_docs_*.py: all 10 pass.
  • Negative control (tools/rdna2spirv.py checked out from main): test_no_unpackhalf2x16 fails (UnpackHalf2x16 is emitted for v_pk_mul_f16); the other three tests (each half unchanged, op_sel swap, NaN stays NaN) still pass on main, so they only show the new widening is value-equivalent to UnpackHalf2x16 for the sampled halves, not that the driver bug is covered.
  • test_fixtures.py also passes with main's translator, so the new fixture check does not exercise the change on the CPU.
  • No issues found in the diff.

@abnalstlll-byte abnalstlll-byte 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 at head 4e08084 on Windows 11 (Python 3.14):

  • \python -m unittest discover -s tests/semantic -p test_pk_f16_widen.py\ -- 4/4 OK (normals, subnormals, +-inf, and NaN preservation across op_sel combinations; no UnpackHalf2x16 emitted).
  • \python -m unittest tests/test_docs_design_rows.py\ -- 4/4 OK.
  • \python -I tests/test_docs_links.py\ -- 6/6 OK. \git diff --check\ clean.
  • Code read: \Gen.f16_widen\ widens each half cleanly through integer bit manipulation (+112 exponent bias, small/denormal scaling by 2^-24), avoiding the driver miscompile under \UnpackHalf2x16.

No game files. AI-assisted review (Gemini).

…Rexed#4779)

v_max_i16, v_max_u16 and v_mul_lo_u16 with op_sel need llvm-mc 21 or newer,
and run_gpu_tests.py stops at the first program that doesn't assemble, so on
LLVM 20 (Ubuntu 24.04) no GPU test ran at all (@chpeu). The fixture now writes
the high half with v_add_nc_u16, v_sub_nc_u16, v_add_nc_i16 and v_sub_nc_i16,
whose op_sel LLVM 18.1.8, 20.1.8 and 23.1.6 all assemble.

On the RX 9060 XT (AMD 26.10.1) it still fails on the old translation (out[0],
out[2], out[5] = 0xF8B64000 for 0xF8B02000; the v_sub_nc cases, whose high half
is a denormal, came out right) and passes with Gen.f16_widen.

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

Copy link
Copy Markdown
Author

@chpeu, thanks, that was a real problem: I had only assembled with llvm-mc 23.1.6, and one fixture taking down the whole suite on LLVM 20 would have hit every Ubuntu 24.04 contributor and runtime.yml's llvm-18 jobs. Fixed in 415f6f90.

Change. cs_w32_pk_mul_hi now writes the high half with v_add_nc_u16, v_sub_nc_u16, v_add_nc_i16 and v_sub_nc_i16. Those are the 16-bit forms whose op_sel all three versions assemble: of the producers I tried, v_max_i16, v_max_u16 and v_mul_lo_u16 with op_sel are rejected by 18.1.8 and 20.1.8, and their SDWA dst_sel:WORD_1 forms are rejected by every version. New words: 0xF8B02000 0x80B02000 0xF8B02000 0x80B02000 0x78B0BC08 0xF8B02000 0x00003C00 0x00000000.

Checked on the RX 9060 XT (AMD 26.10.1), Windows 11:

  • The fixture still discriminates. With LLVM 18.1.8: on the old translation it fails (out[0], out[2], out[5] = 0xF8B64000 for 0xF8B02000; the v_sub_nc cases, whose high half is a denormal, come out right), and with this PR it passes 8/8.
  • Full run_gpu_tests.py: LLVM 20.1.8 203/218 passed, 15 skipped, 0 failed; LLVM 23.1.6 the same.
  • tests/test_translate.py with LLVM 20.1.8 (pytest from tests/requirements.txt): 598 passed, including the two cs_w32_pk_mul_hi cases you saw fail.
  • test_pk_f16_widen.py 4/4 as before.

Separate, not from this PR: with conda-forge's LLVM 18.1.8, main d974c88a stops the same way on an existing fixture: RuntimeError: llvm-mc failed on ps_sdwa_f16_const: <stdin>:30:24: error: invalid operand for instruction. runtime.yml installs Ubuntu's llvm-18 (18.1.3), which I can't test here, so I don't know whether CI sees it. If it does, the general fix is probably run_gpu_tests.py reporting a program that doesn't assemble as one failure instead of aborting the run.

AI-assisted (Claude Code).

@bobcatchris

Copy link
Copy Markdown
Contributor

Tested 415f6f9 on Apple M2 with MoltenVK 1.4.2 (macOS 26.4 (25E246), subgroup 4..32, device Vulkan 1.4.357): cs_w32_pk_mul_hi passes; full suite 153/218 passed, 58 skipped, 7 failed vs base 98fa503 152/217, 58 skipped, 7 failed A/B; no regression.

  • python -I scripts/run_gpu_tests.py --filter cs_w32_pk_mul_hi --build runtime/build/vulkan: 1 shader translated, 1 pass spirv-val; out[0..7] = 0xf8b02000 0x80b02000 0xf8b02000 0x80b02000 0x78b0bc08 0xf8b02000 0x3c00 0x0. So the integer-op widening also behaves correctly on MoltenVK; the RDNA4 miscompile it works around is not reproduced here.
  • python -m unittest discover -s tests/semantic -p test_pk_f16_widen.py: OK.
  • Full suite with one shared harness: head 153/218, 58 skipped, 7 failed (the seven known MoltenVK expected failures); base 98fa503f8a19 152/217, 58 skipped, 7 failed. The only difference is the new fixture, which passes; no common test changed status in either direction.
  • Clean configure and build of the six GPU-suite targets on AppleClang 17 (llvm-mc 21.1.8, Python 3.13.18). No code changes of my own.

No game data; synthetic fixture and counts only.

AI-assisted.

@gertify

gertify commented Nov 23, 2026

Copy link
Copy Markdown

Tested 415f6f9 on AMD Radeon 610M (RADV RAPHAEL_MENDOCINO, RDNA2, subgroup 32..64), Mesa 26.2.6, CachyOS Linux, LEONIDA_VK_DEVICE=RADV: pass. This is RDNA2 on Linux, so it does not show the RDNA4 Windows fix itself, only that nothing regresses here.

  • scripts/run_gpu_tests.py --filter cs_w32_pk_mul_hi: PASS cs_w32_pk_mul_hi 0xf8b02000 0x80b02000 0xf8b02000 0x80b02000 0x78b0bc08 0xf8b02000 0x3c00 0x0; 1/1 passed, native and with LEONIDA_W64_EMULATE=1.
  • Full scripts/run_gpu_tests.py after a full build: 203/218 passed, 15 skipped.
  • valu_fuzz --family f16 --programs 2000, waves 32 and 64, seeds 1, 2 and 3: each 4000/4000 programs that ran match, 0 mismatched, 0 harness errors.
  • tests/semantic test_pk_f16_widen.py: Ran 4 tests, OK. tests/test_shader_checks.py: Ran 83 tests, OK.

AI-assisted.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 415f6f9 merged with main 3faac55 on Linux (WSL2), Python 3.12.16: pass.
Ran tests/semantic/test_pk_f16_widen.py (4 tests OK), the semantic groups test_alu_.py (1601 tests, OK, 1 expected failure), test_pk.py (93 OK), test_f16.py (797 OK, 1 skipped) and test_ps_.py (7 OK), plus tests/test_shader_checks.py, tests/test_run_gpu_tests.py and all tests/test_docs_*.py (OK). The full tests/semantic discover did not finish within my time box on a loaded machine, so I did not run it end to end.
Negative control: with tools/rdna2spirv.py reset to main, only test_no_unpackhalf2x16 fails; the three value tests (each half of v_pk_mul_f16 by 1.0 across zero, denormal, normal, inf and NaN, and op_sel) also pass against main's code. That is expected, since the mini interpreter's UnpackHalf2x16 is exact, so on the CPU these only show that the integer widening is bit-identical to it; the driver bug itself (RX 9060 XT) needs the GPU run of cs_w32_pk_mul_hi.
The new .asm and its expected words in check.py/run_gpu_tests.py were not run here (no GPU).

@chpeu

chpeu commented Nov 23, 2026

Copy link
Copy Markdown

Re-tested the new head 415f6f90 on the same Linux box (RTX 3080, Ubuntu LLVM 20.1.2, no game files) — the
blockage is gone here.

  • LEONIDA_W64_EMULATE=1 python -I scripts/run_gpu_tests.py --build runtime/build/linux --llvm-mc /usr/lib/llvm-20/bin/llvm-mc
    → 202/218 passed, 16 skipped, 0 failed (at 4e080849 the run stopped after the first assembly error and 0
    programs ran); without the variable 171/218, 47 skipped, 0 failed.
  • the 31 pytest-marked translator files, as CI's translator-tests job runs them → 1639 passed, 37 skipped,
    108 subtests, 0 failed
    (the two failures on cs_w32_pk_mul_hi.asm are gone, and the two new cases of
    tests/semantic/test_pk_f16_widen.py are counted).
  • tests/test_cube_sample.py on this head: 18 passed.

So with an LLVM 20 toolchain your new program now assembles and the whole suite runs again — from this machine the
LLVM-20 row is unblocked. The RTX 5080 was not used in this pass (186 MiB free, another job holds the card).
AI-assisted testing, counts read through by hand.

@memorypressure

Copy link
Copy Markdown

Verified merged with current main, CPU-only: the integer f16_widen replaces UnpackHalf2x16 for packed-f16 operands; the new test_pk_f16_widen.py runs 4/4 (142 values x both halves, NaN, op_sel, plus the no-UnpackHalf2x16 grep), and the neighbours (test_valu_pk_mul_f16, test_pk_f16_clamp, test_alu_f16_min_max, test_fixtures, test_translate 598 passed) are green on the merged tree. Contract row, hand-written fixture with word derivations, and identical expectations in check.py/run_gpu_tests.py all present; the GPU fixture run itself needs hardware. Note #4777 edits the same file in a disjoint region — merges fine with it. No CI checks were reported. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@Atomic258

Copy link
Copy Markdown
Contributor

Tested 415f6f9 on Windows Server 2022, CPython 3.13.12: pass

  • python -I -m unittest discover -s tests/semantic -p "test_pk_f16_widen.py" -> OK

git diff --check clean; python -m compileall -q exit 0. No game files, no GPU. AI-assisted (Devin).

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.

9 participants