Skip to content

v_pk_{lshl,lshr,ashr}rev_b16: lower without bit-field extract to avoid the AMD Windows driver wave32 miscompile (SHADER-777) - #4461

Draft
evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:fix/pk-shift-driver-proof
Draft

evilclanker9000 wants to merge 3 commits into
ItsNotRexed:mainfrom
evilclanker9000:fix/pk-shift-driver-proof

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

Requested by Lucas · project thread

Claimed in the coordination issue as: SHADER-777, #4257

What and why

v_pk_lshlrev_b16, v_pk_lshrrev_b16 and v_pk_ashrrev_i16 were lowered with OpBitFieldUExtract for the shift amount and two near-identical lo/hi expressions. On the AMD Windows driver (25.10.36, and still 26.10.1) the compiled wave32 pipeline returns a wrong high half for some programs: valu_fuzz --family f16 --seed 1 programs 534, 1146, 1540, 1556 at 32 lanes (plus 378 at 64) mismatch the hardware oracle on the Radeon with the old lowering. The minimal trigger found by bisection is the three-instruction shift sequence inside the fuzz prologue; it is not reproducible with a small standalone shader, so the regression evidence is those programs, not a red GPU fixture.

New lowering: the amount per half is (src0 >> 16*sel) & 15, each value half is placed in bits 31:16, and the result halves come from a right shift of that word (lshl: shift left then mask), so no bit-field extract is used and the two halves are different expressions. The result is bit-identical to the old lowering in the translator reference.

Tested

  • With the new lowering the five fuzz programs plus controls match the hardware oracle on AMD Radeon gfx1036 (driver 26.10.1) and on the RTX 5080 (6/6 programs that ran match, 378 at 64 lanes skipped by the harness: no full 64-wide subgroups). With the old lowering the Radeon mismatches on the four 32-lane programs.
  • tools/pk16_diff.py --programs 150 over the three ops: 150 pass on both GPUs.
  • New tests/semantic/test_pk_shift_16.py: all 16 op_sel/op_sel_hi combinations x 3 ops x 6 value pairs against a Python reference, plus the expected words of the new GPU test cs_w32_pk_shift (registered in check.py and scripts/run_gpu_tests.py; PASS on both GPUs, old lowering also passes it, so it guards semantics, not the driver bug).
  • test_mutations.py: the v_pk_lshlrev_b16 mutation re-anchored on the new amount read; it is caught.
  • python -m unittest discover -s tests/semantic: 7458 tests; the only failures (loop_ballot_barrier, cmp_class_f64, and the v_mul_legacy_f32 mutation anchor) fail identically on upstream main, none touch this change.
  • tests/test_docs_design_rows.py: OK.

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 (new row "Packed 16-bit shifts").

AI-assisted.

…ows driver keeps the high half (SHADER-777)

The OpBitFieldUExtract amount and the identical lo/hi expressions gave a wrong
high half in wave32 on AMD driver 25.10.36 and 26.10.1 (valu_fuzz f16 seed 1,
programs 534, 1146, 1540, 1556 at 32 lanes). Adds the semantic test over all
op_sel combinations, GPU test cs_w32_pk_shift, the design.md row, and re-anchors
the pk_lshl mutation.

AI-assisted.

@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 79be6dc5 on Windows 11: tests/semantic/test_pk_shift_16.py -> 2 tests, OK; compileall and git diff --check clean.

The three packed 16-bit shift forms now lower to plain shifts (no bit-field extract), avoiding the AMD Windows driver wave32 miscompile while keeping the high-half behavior; the new GPU fixture cs_w32_pk_shift and the semantic cases cover it. I cannot run the AMD driver path here, but the CPU translation is pinched. No blocking issue.

@BetterJdue

Copy link
Copy Markdown
Contributor

SHADER-777 raw-ISA check on 03b09a08, AMD Radeon RX 6700 (discrete RDNA2, gfx1031), Windows 11 build 26300, AMD driver 32.0.21045.29003. @Vonsek, for the lowering: the same seed-1 wave32 programs run natively through tools/hw_oracle (valu_fuzz --family f16 --programs 1557 --seed 1 --wave 32 --oracle hw, no SPIR-V or Vulkan in between) give 1,557 of 1,557 matches, including 1:534:32, 1:1146:32, 1:1540:32 and 1:1556:32. The hardware computes the high half of v_pk_ashrrev_i16 as the model does, so the wrong high half comes from the driver's compilation of the translated module, not from the GPU itself.

AI-assisted verification using synthetic inputs.

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

Reviewed 79be6dc: the lowering keeps each 16-bit result half independent and implements shift amounts from the selected source half without bit-field extracts. Semantic cases cover all op_sel combinations; the independent raw-ISA run confirms the hardware result on AMD wave32.

@bobcatchris

Copy link
Copy Markdown
Contributor

Apple M2 no-regression check at head 79be6dc (macOS 26.4, MoltenVK 1.4.2 direct, subgroup 4..32, llvm-mc 23.1.6, clean full build exit 0).

python -I scripts/run_gpu_tests.py --build runtime/build/linux: 214 shaders translated, 214 pass spirv-val; 128/211 passed, 76 skipped, 7 failed. Compared with main aa2988c on this M2 (127/210, 76 skipped, 7 failed), the extra test is the new fixture from this PR and it passes; the seven failures are the same known MoltenVK expected set. No new failure or skip. Head fetched read-only, no rebase/edit/push. Synthetic shaders and counts only; no game files.

AI-assisted.

@mrbean56

Copy link
Copy Markdown

Tested 79be6dc5c9 on Linux (Debian 13), Python 3.13.5: pass, and the lowering is semantically equivalent while dropping the opcode.

  • pytest tests/semantic/test_pk_shift_16.py -q: 2 passed, 288 subtests (every op_sel/op_sel_hi combination × 6 value pairs × 3 ops, against a plain-Python reference). The same 288 pass with 03b09a08's translator, so the change is not a semantics fix — which matches the docstring's claim that the problem is the AMD Windows driver's compilation of the extract form.
  • Opcode count on a three-instruction probe: main emits 10 OpBitFieldUExtract + 2 OpBitFieldSExtract for the three v_pk_*rev_b16 forms; this head emits 0 of either. That is the whole point of the change (SHADER-777), and it is visible in the module.
  • tests/semantic/test_mutations.py: 2 failures, both v_mul_legacy_f32 drops zero-times-anything guard; the identical 2 fail on pristine 03b09a08, so pre-existing and unrelated.
  • The ashr branch reads correctly: the low half comes from an arithmetic shift of the top-placed half by 16 + amt masked to 16 bits (equivalent to shifting the low half by amt with sign extension, since the top half is exactly the low half's bits), and the high half is masked to bits 31:16 in place. lshr mirrors it; lshl shifts the top-placed half right by 16 first so the shifted-out bits land in the low half.
  • What I cannot check: the driver bug itself (AMD Windows 25.10.36/26.10.1, wave32 high half). Only the GPU fixture runtime/test/shaders/cs_w32_pk_shift.asm on that driver can confirm the workaround, and its hand-derived words are pinned by test_the_cs_w32_pk_shift_fixture.

No GPU, no game data. AI-assisted.

@Alpaca-exe

Copy link
Copy Markdown
Contributor

AI-assisted Windows hardware & semantic verification for SHADER-777 (79be6dc5c) on AMD Radeon RX 6750 XT (0x1002:0x73df, RDNA2, Windows 11, AMD proprietary driver 26.11.2 / 32.0.21045.29003, Vulkan 1.4.315, subgroups 32..64, VK_LOADER_LAYERS_DISABLE=~implicit~):

  • Semantic unit tests (test_pk_shift_16.py): Ran 2 tests -> OK (test_every_combination_matches_the_reference, test_the_cs_w32_pk_shift_fixture).
  • Full tests/semantic suite on 79be6dc5c (no external spirv-val on this host): Ran 7458 tests -> failures=7, errors=3, skipped=7, expected failures=1. Running those 4 failing/erroring modules (test_cmp_class_f64, test_loop_ballot_barrier, test_mutations pre-existing v_mul_legacy_f32 anchor, test_vcmp16_opsel missing spirv-val) directly on base 03b09a086 and upstream main d0835dd9f reproduces the exact same Ran 39 tests -> failures=7, errors=3; #4461's pk_lshl mutation check passes and introduces 0 regressions.
  • GPU execution (cs_w32_pk_shift): translated tests/semantic/fixtures/disasm/cs_w32_pk_shift.s with #4461's tools/rdna2spirv.py, assembled cleanly to Vulkan 1.2 SPIR-V via built-in tools/spirv_asm.py, and executed vk_compute_test.exe cs_w32_pk_shift.spv 1 dump under three subgroup modes:
    • VKC_SUBGROUP_SIZE=32 -> rc=0, 0 mismatches vs check.py expectations
    • VKC_SUBGROUP_SIZE=64 -> rc=0, 0 mismatches vs check.py expectations
    • default subgroup mode -> rc=0, 0 mismatches vs check.py expectations
    • Observed out[0..7]: 00000000 b2c5b2c5 fd96fd96 f658ff4d 1658074d 96283420 00003c00 b2c5e9a1 in all 3 modes.
  • Note: on AMD proprietary 26.11.2, origin/main's OpBitFieldSExtract lowering also outputs identical values (the miscompile reported by the author hit 25.10.36 / 26.10.1), while #4461's plain shift lowering executes identically across wave32, wave64, and default modes here.

@smowlyy

smowlyy commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

The miscompile goes back further than 25.10.36: the AMD Windows driver 24.10.24 has it too, on the same four wave32 programs, and this PR fixes all four. AI-assisted (Claude Code), synthetic programs only, no game files.

Hardware: AMD Ryzen 5 7600X, Windows 11 Home 26300. GPU: AMD Radeon(TM) Graphics iGPU (RDNA2 gfx1036, subgroup 32..64), AMD driver 24.10.24 (Vulkan driver 2.0.302). MSVC 14.44, Vulkan SDK 1.4.363.0, llvm-mc 21.1.8, Python 3.12.10. Implicit layers off, LEONIDA_VK_DEVICE=radeon, VK_LOADER_DRIVERS_SELECT=*amd*.

python -I tools/valu_fuzz.py --family f16 --programs 1557 --seed 1 --wave 32 (default translator oracle). Both runs use the same vk_compute_test, built at this head; only the tree the fuzzer and translator run from changes:

translator result
base 03b09a08 1553/1557 match; mismatches 1:534:32, 1:1146:32, 1:1540:32, 1:1556:32
this PR, 79be6dc5 1557/1557 match

Every mismatch is in a register last written by v_pk_ashrrev_i16, and it is the high half that's wrong. For example:

  • 1:1540:32 v33: got 0x745fbed8, want 0xbed8bed8 (op_sel:[0,1] op_sel_hi:[0,1]).
  • 1:1556:32 v22: got 0x3c000000…0x3f800000 across threads 0-7, want 0x00000000 (op_sel:[1,1] op_sel_hi:[1,1]).

Together with @BetterJdue's raw-ISA run (1557/1557 on the hardware itself), this points at the driver's compilation, not the GPU, from 24.10 through 26.10.

Whole-suite run of this head, python -I scripts/run_gpu_tests.py:

device mode result
Radeon iGPU, 24.10.24 native 101/211 passed, 110 skipped, 0 failed
Radeon iGPU, 24.10.24 LEONIDA_W64_EMULATE=1 101/211 passed, 110 skipped, 0 failed
RTX 5060 Ti, NVIDIA 610.88 native 165/211 passed, 46 skipped (wave64), 0 failed
RTX 5060 Ti, NVIDIA 610.88 LEONIDA_W64_EMULATE=1 196/211 passed, 15 skipped, 0 failed

The new cs_w32_pk_shift passes on all four runs, with 0x0 0xb2c5b2c5 0xfd96fd96 0xf658ff4d 0x1658074d 0x96283420 0x3c00 0xb2c5e9a1.

  • python -m unittest discover -s tests/semantic: 7458 tests. There are 7 failures and 2 errors, the same as on main 03b09a08 on this machine (test_loop_ballot_barrier, test_cmp_class_f64 rejected forms, the v_mul_legacy_f32 mutation anchor), as the description says.
  • python -I tests/test_shader_checks.py, python -I tests/test_lane_masks.py: 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.

Overlap to resolve: #4622 (fiestorra, opened after this) fixes the same SHADER-777 high-half read with a smaller change — shift + mask for the high half, leaving the low half's offset-0 OpBitFieldUExtract in place — and also updates the v_pk_lshlrev_b16 mutation anchor and the docs row. git merge-tree --write-tree refs/remotes/pr/4461 refs/remotes/pr/4622 -> conflict in docs/design.md, tests/semantic/test_mutations.py and tools/rdna2spirv.py.

This PR is the earlier claimant, removes the field extract for both halves and carries the GPU fixture cs_w32_pk_shift.asm plus a semantic test; #4622 is the smaller minimal form. Please have one close or drop its overlapping hunk so the coordinator is not choosing between two conflicting translations of the same fix.

@sebashord

Copy link
Copy Markdown

Tested 79be6dc5c9e13cd0c6086444f2e4f8cad9192481 on the driver of the original SHADER-777 finding: AMD Windows driver 25.10.36, Radeon iGPU of a Ryzen 7 9800X3D (gfx1036, 0x1002:0x13c0, subgroup 32..64), Windows 11 Pro build 26300.9974: pass, and the fix removes all four wave32 mismatches.

Fuzzer, same machine and driver, LEONIDA_VK_DEVICE=radeon
python -I tools/valu_fuzz.py --family f16 --seed 1 --programs 1557 --wave 32

Result
main d0835dd9 (old lowering) 1553/1557 match; mismatches 1:534:32, 1:1146:32, 1:1540:32, 1:1556:32, all in the high half of v_pk_ashrrev_i16 (thread 0 of 1:1556:32: got 0x3c000000, want 0x00000000, as in the finding)
this head 79be6dc5 1557/1557 match, 0 mismatched, 0 skipped

GPU suite at this head (python -I scripts/run_gpu_tests.py --require-pass, test programs rebuilt from this head with MSVC 14.44, one GPU at a time)

Device This head main d0835dd9
Radeon iGPU, 25.10.36, native wave64 196/211 passed, 15 skipped, 0 failed 195/210, 15 skipped
RTX 5070 Ti, 621.01 (subgroup 32..32), native 165/211, 46 skipped, 0 failed 164/210, 46 skipped
RTX 5070 Ti, LEONIDA_W64_EMULATE=1 196/211, 15 skipped, 0 failed 195/210, 15 skipped

The extra test is the new cs_w32_pk_shift, which passes in all three runs with the expected words; no new failure or skip.

Note: main is two commits ahead of this branch's base (03b09a08), so the fuzzer comparison is main vs this head, not base vs head.

Tools: llvm-mc 23.1.6 (conda-forge), Vulkan SDK 1.4.363.0, Python 3.11.9, VK_LOADER_LAYERS_DISABLE=~implicit~.

AI-assisted. Synthetic programs and counts only, no game files.

Created with Perplexity

@amarovndres

Copy link
Copy Markdown

Tested 79be6dc5 on 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): no regression on RDNA4, and, as its title says, it doesn't cover the v_pk_mul_f16 form from my finding.

  • valu_fuzz.py --family f16 --programs 2000, seeds 1-3, with this head's translator: wave32 5,999/6,000, wave64 6,000/6,000, the same as main 03b09a08 on this card. The one mismatch is still 3:1009:32 (v35 got 0xbcb64000, want 0xbcb02000), the v_pk_mul_f16 case.
  • The 4-instruction repro from the finding gives the same wrong word with this head as with main.

Scope on RDNA4, @Vonsek, in case it helps a follow-up. Each program: a 16-bit producer writes v31's high half through op_sel from the lane-derived v20, a consumer reads v31 into v35, then optionally v_mov_b32 v31, 0. That later write can't change v35, so v35 with and without it must be equal: a difference is a miscompile, no model needed.

producer (op_sel:[0,0,1]) → consumer wave32 wave64
v_max_i16, v_max_u16, v_add_nc_u16, v_sub_nc_u16, v_mul_lo_u16 → v_pk_mul_f16 ... op_sel_hi:[0,1] all 5 differ (64/64 threads) all equal
the same 5 → v_pk_add_f16, v_pk_add_u16, v_add_nc_u16 op_sel:[0,1,0], v_lshrrev_b32 16 all 20 equal all equal

So on this card the trigger is the v_pk_mul_f16 consumer in wave32, whichever op wrote the high half. Its SPIR-V differs from v_pk_add_f16's only in OpFMul vs OpFAdd and the f16 rounding sequence after it. Replacing the producer's two OpBitFieldSExtract(x, 0, 16) by (x << 16) >> 16 in the failing module (spirv-val clean) does not change the result, so this one isn't the bit-field-extract pattern your lowering removes.

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

@amarovndres

Copy link
Copy Markdown

@Vonsek: since this PR is scoped to the shifts, I'll look for a driver-proof lowering of the v_pk_mul_f16 case (wave32, RDNA4 repro above) on this card, as a follow-up to SHADER-777. If you'd rather carry it yourself or already have something, say so and I'll just test yours on RDNA4. If I get a lowering that holds here and on the fuzz seeds, I'll post it as a finding: with the data first rather than open a competing PR. AI-assisted (Claude Code).

@smowlyy

smowlyy commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Follow-up with two more seeds, on the same Radeon iGPU (gfx1036, AMD Windows driver 24.10.24) as my comment above. On main d0835dd9, seeds 2 and 3 have three more wave32 programs with the same miscompile. This PR's current head 96a59a89 fixes all three. AI-assisted (Claude Code), synthetic programs only.

python -I tools/valu_fuzz.py --family f16 --programs 1500 --seed 2|3 --wave 32, with the vk_compute_test built at d0835dd9 for both runs:

seed main d0835dd9 this PR 96a59a89
2 1 mismatch: 2:1385:32 1500/1500 match
3 2 mismatches: 3:848:32, 3:959:32 1500/1500 match

Each one is again the high half of a v_pk_ashrrev_i16:

  • 2:1385:32 v24, op_sel:[0,1] op_sel_hi:[0,1]: got 0x32f43c00, want 0x3c003c00 (thread 0).
  • 3:848:32 v26, same op_sel: got 0xb05184fc, want 0x84fc84fc.
  • 3:959:32 v25, op_sel:[1,1] op_sel_hi:[1,1]: got 0x3c000000, want 0. The wrong half then reaches v29 through v_sub_nc_u16_e64.

On main, --wave both at these seeds also ran the wave64 programs, and none of those mismatched. In total this driver now has 7 failing programs over seeds 1 to 3 on main, and 0 with this PR.

@kapitche

Copy link
Copy Markdown
Contributor

Tested 96a59a8 merged with main 98fa503 on Linux (WSL2), Python 3.12.16: pass (draft; GPU run not done)

  • test_pk_shift_16 2/2 OK, test_mutations 3/3 OK (the changed mutation entry is still killed), test_ps_semantics, test_negative_controls, test_value_semantics OK, regen_fixtures.py --check clean, tests/test_docs_*.py (10 files) OK.
  • Negative control: test_pk_shift_16 also passes with tools/rdna2spirv.py from main. That is expected (the CPU interpreter evaluates both forms the same; the bug is an AMD driver miscompile), so the semantic test pins the ISA semantics of the new lowering (all 16 op_sel/op_sel_hi combinations) but does not by itself show the SHADER-777 fix; only cs_w32_pk_shift on the AMD Windows driver does, and I skipped GPU runs.
  • Nit: in runtime/test/shaders/check.py and scripts/run_gpu_tests.py the new cs_w32_pk_shift entry was inserted between the "SHADER-99: -0 and NaN survive" comment and its cs_signed_zero_nan entry, so that comment now sits above the wrong line.

@vibryxcx

Copy link
Copy Markdown

Tested 96a59a8 on AMD Radeon RX 6800 (RDNA2, discrete, gfx1030), AMD 26.8.1 (Windows 32.0.21045.5002), Windows 11 Pro 25H2 (build 26200): pass.

tools/valu_fuzz.py --family f16 --programs 2000 --seed 1 --wave 32, native wave32 (no LEONIDA_W64_EMULATE), translator from this head:

mismatched matched
main before this PR 4 / 2000 (programs 1:534, 1:1146, 1:1540, 1:1556; all in the v_pk_ashrrev_i16 high half) 1996
this PR 0 / 2000 2000 (0 skipped, 0 not translated, 0 harness errors)

Only the fuzz was run here; I did not run the new cs_w32_pk_shift GPU fixture. No game files.

AI-assisted (Claude Code).

No textual conflicts remained after main moved (docs/design.md merged
cleanly).

AI-assisted.
@KubickNubick

Copy link
Copy Markdown

A standalone reproducer for this, from a different fuzz seed, and a check of this PR against it: this lowering fixes it; #4622's does not.

The description says the miscompile is "not reproducible with a small standalone shader". tools/valu_fuzz.py --family f16 --seed 4080 program 163 at 32 lanes fails on my Radeon, and cut down by hand it is two instructions:

; group: 64,1,1
; rsrc2: 0x8088
s_mov_b32 exec_lo, -1
v_mov_b32_e32 v21, 0x38003800
v_mov_b32_e32 v30, 0x4200bb50
v_mov_b32_e32 v33, 0xc29bb176
v_pk_min_f16 v33, v33, v30 op_sel:[1,0] op_sel_hi:[0,0]
v_pk_ashrrev_i16 v33, v21, v33 op_sel:[1,1] op_sel_hi:[1,1]
; then store v33

The min gives 0xbb50c29b (right on both GPUs when stored at that point). The shift count is 0 and both result halves read the high half, so v33 should be 0xbb50bb50. The Radeon returns 0xc29bbb50: the high result half is the low half of the source.

AMD Radeon iGPU of a Ryzen 9 9950X3D (RDNA2 gfx1036, Windows driver 32.0.21042.62, subgroup 32..64) next to an NVIDIA RTX 4080 (621.01), Windows 11 Pro 26300, vk_compute_test from main 3faac55, VKC_SUBGROUP_SIZE=32, same SPIR-V on both GPUs (spirv-val clean):

Translator full program 4080:163 min + ashr (above) v_pk_max_f16 + ashr min + ashr, shift count not a constant
main 3faac55 Radeon wrong in 5 registers Radeon 0xc29bbb50, want 0xbb50bb50 Radeon 0xbb50b176, want 0xb176b176 Radeon wrong, same words
this PR, 13144aa same on both GPUs right right right
#4622, a148559 Radeon wrong, same words as main wrong, same words wrong, same words wrong, same words

The RTX 4080 gives the wanted words in every cell. What narrows it:

  • It needs the packed min or max in front. v_pk_ashrrev_i16 alone on the same constant, or the min alone, is right on the Radeon.
  • It needs the arithmetic shift. v_pk_lshrrev_b16 with the same operands after the same min is right.
  • It needs both halves to read the high half (op_sel:[1,1] op_sel_hi:[1,1]). [1,0]/[1,0] and [1,0]/[1,1] are right.
  • A shift count the driver can't fold (v21 or-ed with v0 & 0) doesn't change it.

In the sweep this came from (seeds 4080 and 9950, 200 programs per wave, native and emulated, both GPUs) it is the only valu_fuzz --family f16 mismatch: 1 of 400 on the Radeon for seed 4080 in both modes, 0 for seed 9950, 0 on the RTX 4080.

If it is useful, I can turn the two instructions into a GPU fixture for runtime/test/shaders (expected word 0xbb50bb50; it would be red on main on this driver and green with this PR) as a follow-up that depends on this PR. Tell me if you would rather add it here yourself.

No game files; the program above is hand-cut from the fuzzer's synthetic output. AI-assisted (Claude Code ran the sweep and the reduction).

fiestorra added a commit to fiestorra/Leonida that referenced this pull request Nov 23, 2026
…d half readers

docs/design.md already says a high 16-bit half is read with `>> 16` alone, with no
narrowing after it, because AMD's Windows LLPC miscompiles that combination (ItsNotRexed#1506).
The three packed 16-bit shifts were the exception: they read the value half with
`OpBitField{S,U}Extract(s1, 16, 16)`. An AMD Windows driver 25.10.36 on a gfx1036
reads a field extract at offset 16 with the offset taken mod 16, so both result halves
got the low half of the source - 5 of 2000 wave32 programs of `tools/valu_fuzz.py
--family f16 --programs 2000 --seed 1` (@Darkz0rz, SHADER-777 in the coordination
issue). The same 2000 programs match 2000/2000 on a second gfx1036 (Radeon iGPU of a
Ryzen 7 7800X3D, Windows driver 31.0.24002.92), and the SPIR-V that run produced does
contain those offset-16 extracts. So this is a lowering that survives both drivers, not
a value change: BitField{S,U}Extract(x, 16, 16) equals `x >> 16` with the matching
right shift, checked over 200,010 inputs including 0, 1, 0xFFFF, 0x10000, 0x80000000,
0xFFFFFFFF, 0x7FFFFFFF and 0x8000. Extracts at offset 0 are left as they were.

Reading the halves through Gen.u16_half / Gen.i16_half also aligns the signed op with
the `%int` convention used everywhere else in the file: `v_pk_ashrrev_i16` now weights
its value as `%int` (as_int of the source, sign-extended), shifts with
`OpShiftRightArithmetic %int`, and only then bitcasts to `%uint` before the 0xFFFF
mask, so sign extension comes from the type instead of from a driver choosing to read
an arithmetic shift on an unsigned operand as arithmetic. For the shift amount a high
half reads `(s0 >> 16) & 15` and a low half keeps `OpBitFieldUExtract(s0, 0, 4)`; the
"16-bit high-half reads" row now says both, which is what the code does.

Counted over all 26 503 listings: 1 v_pk_lshlrev_b16 in one compute listing and 1
v_pk_lshrrev_b16 in another, no v_pk_ashrrev_i16 and no _sdwa form. Translating the
corpus on this branch and on main 902358c with the same flags gives 26 503 entries on
both sides with the same index.json sha256, 26 501 .spv identical, and the 2 that
differ differ only in shape: opcode histograms -2 OpBitFieldUExtract / +1 OpBitwiseAnd
/ +1 OpShiftRightLogical in one (instruction count unchanged) and -3 / +3 / +1 in the
other (one extra instruction, for a constant shift amount). The full tests/semantic
suite gives the same 6245 tests with failures=3, errors=2 as main, all five in
test_loop_ballot_barrier; test_pk16_diff, test_ps_semantics, test_value_semantics and
test_alu_pk_int16 are green. ps_ops4 and ps_ops6 are exact at 4096/4096 on the gfx1036
iGPU and on an NVIDIA RTX 4090 (driver 32.0.16.2124).

tests/test_u16_high_half.py gains the shape assertions for the three ops over four
op_sel/op_sel_hi combinations, validated with spirv-as and spirv-val, plus one case
pinning that every arithmetic shift in v_pk_ashrrev_i16 is %int; those four cases fail
on main and pass here.

Coordinated as SHADER-784 in ItsNotRexed#4571, verifying @Darkz0rz's SHADER-777. Overlaps ItsNotRexed#4461
(Vonsek, the earlier claim of the same item, with a broader lowering and a GPU fixture);
flagged on both.
@fiestorra

fiestorra commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Coordination, not a review. @Kam471 flagged that this and #4622 do the same item (SHADER-777) and conflict in
docs/design.md, tests/semantic/test_mutations.py and tools/rdna2spirv.py. You claimed it first (01:34) and your
version is the broader one — no bit-field extract anywhere, its own semantic test over all 16 op_sel combinations,
and a cs_w32_pk_shift GPU fixture, which mine has no equivalent of. Mine is the narrower high-half-only lowering,
plus the %int alignment of v_pk_ashrrev_i16 with the Gen.i16_half convention (#4622 review point) and shape
assertions added to the existing tests/test_u16_high_half.py.

I am not going to leave two PRs doing the same job. Say which one you want to keep and I will act on it: close
#4622, or drop my tools/rdna2spirv.py hunk and keep only the signedness alignment and the shape tests on top of
yours.

One measurement from here in case it is useful for your row: over all 26 503 game listings there is 1
v_pk_lshlrev_b16, 1 v_pk_lshrrev_b16 and no v_pk_ashrrev_i16 (no _sdwa form either), so whichever lowering
lands changes the shape of 2 files and the value of none. On this machine (Ryzen 7 7800X3D integrated iGPU (gfx1036, 0x1002:0x164e), Windows
driver 31.0.24002.92) the old lowering already matched 2000/2000 of tools/valu_fuzz.py --family f16 --programs 2000 --seed 1 --wave 32, and ps_ops4 / ps_ops6 are exact at 4096/4096 on it and on an RTX 4090 (driver
32.0.16.2124) — so I cannot confirm your 25.10.36 / 26.10.1 cases here, only that the replacement does not break the
programs I can run.

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

Independent verification on a second pair of GPUs and drivers, plus the whole corpus. AI-assisted (my own agent), run
locally at 13144aa5; your branch is two merges behind current main (merge-base ec34b54f, main is 3faac557), so
the parity numbers below are against main 902358c8, which is where I keep my own baseline.

Machine. Windows 11, Ryzen 7 7800X3D. GPU 1: Radeon 780M iGPU of that CPU (gfx1036, 0x1002:0x164e), Windows
driver 31.0.24002.92, compute subgroup 32..32 (this driver reports no 64-wide compute subgroup, so I cannot run your
program 378 at 64 lanes). GPU 2: NVIDIA GeForce RTX 4090 (Ada), driver 32.0.16.2124, subgroup 32..32. llvm-mc 21,
Vulkan SDK 1.4.363.0, harness built from your branch with cmake -S runtime -B runtime/build -G Ninja.

New GPU test. cs_w32_pk_shift PASS, cs_cvt_pk_u8 PASS, 2/2 on both devices:

device / driver cs_w32_pk_shift
Radeon 780M iGPU (gfx1036), 31.0.24002.92 PASS 0x0 0xb2c5b2c5 0xfd96fd96 0xf658ff4d 0x1658074d 0x96283420 0x3c00 0xb2c5e9a1
RTX 4090, 32.0.16.2124 PASS, same words

Two more driver combinations on top of your gfx1036 / 26.10.1 and the RTX 5080.

Whole game corpus, same flags as the build (--no-asm --wave64-emulate, 8 shards), your tools/rdna2spirv.py vs
main 902358c8.
26,503 entries on both sides, the same index.json sha256
7E9ACA940D0007A7399D9748861FE3751CF1736654249E1CE342628FA37861D6, 26,501 .spv byte-identical, 0 entries lost
or newly rejected. The 2 files that change are the only 2 listings in the corpus that hold a packed shift at all — over
all 26,503 listings: 1 v_pk_lshlrev_b16, 1 v_pk_lshrrev_b16, 0 v_pk_ashrrev_i16, no _sdwa form. Sizes
166,900 → 166,924 and 111,092 → 111,116 bytes: +24 bytes each, which is the wider amount/mask expression your row
describes, on real game input. My #4622 changes exactly the same 2 files in exactly the same way (shape only, one
smaller and one larger).

Translator suite. python -m unittest discover -s tests/semantic → 6247 tests, failures = 3, errors = 2, skipped =
1, expected failures = 1. Main 902358c8 on the same machine gives 6245 tests with the same failure and error counts;
the +2 are your new tests. Run on that one file alone in both trees, the failing set is identical and none of it
touches packed shifts:

SUBFAILED(mode='emulate_64_sg32')      test_divergent_loop_wave64_emulate_and_native_cs
SUBFAILED(mode='emulate_128_sg32')     test_divergent_loop_wave64_emulate_and_native_cs
SUBFAILED(mode='emulate_40_partial_sg32') test_divergent_loop_wave64_emulate_and_native_cs
SUBFAILED(emulate=True)                test_nested_divergent_loops_emulate
SUBFAILED(emulate=True)                test_bug4b_loop_carried_split_sgpr_pair_ballot
5 failed, 15 passed, 19 subtests passed

tests/semantic/test_pk_shift_16.py, tests/test_pk16_diff.py, tests/test_u16_high_half.py and the
docs/design.md row checks: 66 passed, 306 subtests. tests/semantic/test_mutations.py: 3 passed, 26 subtests — your
re-anchored v_pk_lshlrev_b16 amount mutation is caught. Note for when you rebase: on current main the
v_mul_legacy_f32 anchor in that file is stale (moved by c82fbe37), which adds two unrelated failures that
#3883/#4527 re-anchor; they are not yours.

One thing I would change, and it is the same point @tupacfan04 raised on my #4622. Your v_pk_ashrrev_i16 path
shifts as OpShiftRightArithmetic %uint: the sign extension comes from bit 31 of a word you deliberately filled to
bits [31:16], and whether an arithmetic shift of an unsigned operand replicates that bit is up to the compiler —
GLSL and MSL treat >> on uint as a logical shift, and the repo's own 16-bit signed reader (Gen.i16_half,
docs row "16-bit high-half reads", #1506) weights the value as %int for exactly that reason. Your fixture and your
fuzz programs pass on LLPC and on the RTX 5080, so this is a portability concern rather than a failure I can show on
hardware — but if both PRs landed with arithmetic shifts on %uint, the rule the contract table states would depend
on driver behaviour. Weighting it as %int (OpBitcast %int, shift, OpBitcast %uint before the mask) matches the
existing helper, matches what @tupacfan04 asked me for, and makes the two PRs agree on the one point where they
differ. My branch keeps the existing half readers and only changes the amount read; yours removes the bit-field
extracts everywhere, which is the wider change.

Not blocking: I have no claim on which of these two lands. I have said on #4622 and in #5256 that I will close it, or
drop my translator hunk and keep only the %int alignment plus the shape assertions in tests/test_u16_high_half.py,
whichever you and @Kam471 prefer — your version is the earlier claim and the broader test coverage.

@fiestorra

Copy link
Copy Markdown
Contributor

Small correction to my review above, which named my AMD GPU wrongly: the integrated graphics of the Ryzen 7 7800X3D is
the 2 CU RDNA2 part (0x1002:0x164e, gfx1036), not a Radeon 780M. Same machine, same driver 31.0.24002.92, same
results as measured — cs_w32_pk_shift PASS on it and on the RTX 4090, corpus and suite numbers unchanged. I could not
edit the review body itself (the API returned 404 for it), so the correction is here.

fiestorra added a commit to fiestorra/Leonida that referenced this pull request Nov 23, 2026
…d half readers

docs/design.md already says a high 16-bit half is read with `>> 16` alone, with no
narrowing after it, because AMD's Windows LLPC miscompiles that combination (ItsNotRexed#1506).
The three packed 16-bit shifts were the exception: they read the value half with
`OpBitField{S,U}Extract(s1, 16, 16)`. An AMD Windows driver 25.10.36 on a gfx1036
reads a field extract at offset 16 with the offset taken mod 16, so both result halves
got the low half of the source - 5 of 2000 wave32 programs of `tools/valu_fuzz.py
--family f16 --programs 2000 --seed 1` (@Darkz0rz, SHADER-777 in the coordination
issue). The same 2000 programs match 2000/2000 on a second gfx1036 (Radeon iGPU of a
Ryzen 7 7800X3D, Windows driver 31.0.24002.92), and the SPIR-V that run produced does
contain those offset-16 extracts. So this is a lowering that survives both drivers, not
a value change: BitField{S,U}Extract(x, 16, 16) equals `x >> 16` with the matching
right shift, checked over 200,010 inputs including 0, 1, 0xFFFF, 0x10000, 0x80000000,
0xFFFFFFFF, 0x7FFFFFFF and 0x8000. Extracts at offset 0 are left as they were.

Reading the halves through Gen.u16_half / Gen.i16_half also aligns the signed op with
the `%int` convention used everywhere else in the file: `v_pk_ashrrev_i16` now weights
its value as `%int` (as_int of the source, sign-extended), shifts with
`OpShiftRightArithmetic %int`, and only then bitcasts to `%uint` before the 0xFFFF
mask, so sign extension comes from the type instead of from a driver choosing to read
an arithmetic shift on an unsigned operand as arithmetic. For the shift amount a high
half reads `(s0 >> 16) & 15` and a low half keeps `OpBitFieldUExtract(s0, 0, 4)`; the
"16-bit high-half reads" row now says both, which is what the code does.

Counted over all 26 503 listings: 1 v_pk_lshlrev_b16 in one compute listing and 1
v_pk_lshrrev_b16 in another, no v_pk_ashrrev_i16 and no _sdwa form. Translating the
corpus on this branch and on main 902358c with the same flags gives 26 503 entries on
both sides with the same index.json sha256, 26 501 .spv identical, and the 2 that
differ differ only in shape: opcode histograms -2 OpBitFieldUExtract / +1 OpBitwiseAnd
/ +1 OpShiftRightLogical in one (instruction count unchanged) and -3 / +3 / +1 in the
other (one extra instruction, for a constant shift amount). The full tests/semantic
suite gives the same 6245 tests with failures=3, errors=2 as main, all five in
test_loop_ballot_barrier; test_pk16_diff, test_ps_semantics, test_value_semantics and
test_alu_pk_int16 are green. ps_ops4 and ps_ops6 are exact at 4096/4096 on the gfx1036
iGPU and on an NVIDIA RTX 4090 (driver 32.0.16.2124).

tests/test_u16_high_half.py gains the shape assertions for the three ops over four
op_sel/op_sel_hi combinations, validated with spirv-as and spirv-val, plus one case
pinning that every arithmetic shift in v_pk_ashrrev_i16 is %int; those four cases fail
on main and pass here.

Coordinated as SHADER-784 in ItsNotRexed#4571, verifying @Darkz0rz's SHADER-777. Overlaps ItsNotRexed#4461
(Vonsek, the earlier claim of the same item, with a broader lowering and a GPU fixture);
flagged on both.
@memorypressure

Copy link
Copy Markdown

Verified at 13144aa, and this is a FAIL-class close-recommendation: main's a148559 already fixes the same driver miscompile from the same evidence (AMD driver 25.10.36, valu_fuzz --family f16 --seed 1), re-anchoring the same test_mutations entry to '{half} = OpShiftRightLogical' — mutually exclusive with this PR's anchor. I verified the two lowerings are value-equivalent per half for every op_sel/op_sel_hi combination, so nothing is lost by keeping main's. The PR also still conflicts with current main in runtime/test/shaders/check.py, scripts/run_gpu_tests.py, tests/semantic/test_mutations.py and tools/rdna2spirv.py. What is worth salvaging rather than closing outright: runtime/test/shaders/cs_w32_pk_shift.asm plus its expected words (I recomputed all eight hand-derived words — all correct), the disasm fixture and regen_fixtures registration, and tests/semantic/test_pk_shift_16.py (2 tests OK at your head); the design.md row would need rewriting since main keeps offset-0 OpBitField*Extract. Your own suites were green at the head (pk_shift_16, mutations, alu_pk_int16, alu_int_minmax_shift_not, alu_add_u16, rule_gpu_fixtures, missing_encoding, docs guards), and test_rdna2_decode's single failure reproduces identically at the merge-base — not yours. Suggestion: close the translator part as superseded, salvage the GPU test onto main. AI-assisted: yes. Tested on Linux (RTX 5080 host, CPU-only run).

@KubickNubick

Copy link
Copy Markdown

A second case from another seed, in a different shape: here the wrong word is not the shift's result. tools/valu_fuzz.py --family f16 --seed 31337 program 168 at 32 lanes on main 1cc6e3c, reduced to five instructions (registers preset as that program presets them; v29 = 0xbc6ebc2a, v34 = 0x3a904b8f, v35 = 0x45c94000):

v_and_b32_e32 v7, 7, v0
v_or_b32_e32 v20, 0x3c00, v7
v_pk_add_f16 v33, v20, v26 op_sel:[0,1] op_sel_hi:[1,1]
v_pk_ashrrev_i16 v24, v34, v35 op_sel:[1,1] op_sel_hi:[0,1]
v_mul_lo_u16_e64 v24, v29, v33 op_sel:[0,0,0]

v24 should be the 16-bit product with a zero high half. The RTX 4080 (621.01) gives 0x0000d73c; the Radeon iGPU (gfx1036, 32.0.21042.62) gives 0x30fcd73c from the same SPIR-V module, which is the whole 32-bit product 0xbc2a * 0x42a6: the multiply's & 0xffff is gone. Dropping the v_pk_ashrrev_i16 (whose result the multiply overwrites) makes the Radeon right.

Translator Radeon v24
main 1cc6e3c 0x30fcd73c (wrong)
this PR, 13144aa 0x0000d73c (right)
#4622, 1d630d4 0x30fcd73c (wrong)

So this PR's lowering also removes a wrong result in an instruction it does not touch, and #4622's still does not help on this driver. In the sweep it came from (seeds 31337 and 20261123, 200 programs per wave) it is the Radeon's only valu_fuzz --family f16 mismatch; the RTX 4080 has none.

No game files; the program is hand-cut from the fuzzer's synthetic output. AI-assisted (Claude Code ran the sweep and the reduction).

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.