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
Vonsek wants to merge 1 commit into
ItsNotRexed:mainfrom
Vonsek:fix/pk-shift-driver-proof
Draft

Vonsek wants to merge 1 commit into
ItsNotRexed:mainfrom
Vonsek:fix/pk-shift-driver-proof

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 23, 2026

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

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

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