Skip to content

rdna2spirv: the packed 16-bit shifts read a half with a shift, not a field extract - #4622

Open
fiestorra wants to merge 1 commit into
ItsNotRexed:mainfrom
fiestorra:shader-packed-shifts-shift-and-mask
Open

fiestorra wants to merge 1 commit into
ItsNotRexed:mainfrom
fiestorra:shader-packed-shifts-shift-and-mask

Conversation

@fiestorra

@fiestorra fiestorra commented Nov 23, 2026 •

Copy link
Copy Markdown

Coordinated as SHADER-784 in #4571 — the check of
SHADER-777, @Darkz0rz's finding that a packed v_pk_ashrrev_i16 comes back wrong on an AMD gfx1036 with driver
25.10.36. The claim and the measurement are posted as
comment 7088898718 and
comment 7089629163 (in #4257, which
moved to #4421 and then to #4571 while I was running them).

What changes

docs/design.md ("16-bit high-half reads", #1506) already says that a high 16-bit half is read with >> 16 alone,
with no & 0xFFFF and no OpBitFieldSExtract … 0 16 after it, because AMD's Windows LLPC drops the shift in that
combination. The three packed 16-bit shifts were the only place that did it the other way: v_pk_lshlrev_b16,
v_pk_lshrrev_b16 and v_pk_ashrrev_i16 took the shift amount with OpBitFieldUExtract(s0, 16, 4) and the value
with OpBitField{S,U}Extract(s1, 16, 16). They now take a half with OpShiftRightLogical/OpShiftRightArithmetic
by 16 (and & 15 for the amount); extracts at offset 0 are untouched, and the arithmetic/logical choice for the
value is the one the op needs.

How it was confirmed

The driver behaviour is real, and it is driver-version dependent. @Darkz0rz measured, on a gfx1036 with AMD Windows
driver 25.10.36, 5 of 2,000 tools/valu_fuzz.py --family f16 --programs 2000 --seed 1 programs wrong in wave32 and
none in native wave64: the high result half came back as the low half of the source, which is the offset taken
mod 16 — the same class as the note already in tools/rdna2spirv.py about Intel's driver.

It does not appear on my gfx1036. The same command (--programs 2000 --seed 1 --wave 32, translator at
03b09a08) on the Radeon iGPU of a Ryzen 7 7800X3D, Windows driver 31.0.24002.92: 2000/2000 programs that ran match; 0 mismatched, 0 skipped, 0 not translated, 0 harness errors, and 0 skipped means vk_compute_test pinned
32-lane subgroups. It covers the programs the finding names, and I checked that the SPIR-V that run emitted
OpBitFieldSExtract %uint x 0x10 0x10 for one of them, so the field-extract form really was compiled there. The
change is therefore a lowering that survives both drivers, not a value change.

The two forms give the same value. Over 200,010 inputs (0, 1, 0xFFFF, 0x10000, 0x80000000, 0xFFFFFFFF,
0x7FFFFFFF, 0x8000 and 200 000 random): BitField{S,U}Extract(x, 16, 16) equals x >> 16 with the matching
OpShiftRight{Arithmetic,Logical}, BitFieldUExtract(x, 16, 4) equals (x >> 16) & 15, and — left as it was —
BitFieldSExtract(x, 0, 16) equals asr(lshl(x, 16), 16).

Counted over the corpus

All 26 503 listings contain 1 v_pk_lshlrev_b16 in 1 compute listing (op_sel_hi:[0,1]) and 1 v_pk_lshrrev_b16
in 1 other (op_sel:[1,0] op_sel_hi:[0,0]), no v_pk_ashrrev_i16 and no _sdwa form; both currently emit an
extract at offset 16, so on the affected driver exactly those two compute shaders would be wrong.

Translating all 26 503 listings with the same flags (--wave64-emulate, 8 shards, --no-asm) — once with main
aa2988cc/03b09a08 (the translator is identical between them, git diff is empty), once with this branch — gives
26 503 translated and 26 473 passing spirv-val in both, with the same 30 rejects, and file by file
26 471 of the 26 473 .spv identical in size and SHA-256, with index.json byte-identical. The 2 that differ
are the 2 with those instructions. Disassembling them before and after, the only difference is the replaced extract:
in one, OpBitFieldUExtract %uint x 16 16 becomes OpShiftRightLogical %uint x 16 (4 bytes shorter); in the other,
the amount OpBitFieldUExtract %uint %uint_8 16 4 becomes OpShiftRightLogical %uint %uint_8 16 + & 15 and every
other step of the two halves is unchanged.

Tested

  • run_gpu_tests.py --filter ops: 11/12 pass on the gfx1036 iGPU (driver 31.0.24002.92) and 11/12 on an NVIDIA
    RTX 4090 (driver 32.0.16.2124)
    , ps_ops4 and ps_ops6 (the fixtures with v_pk_lshlrev/lshrrev/ashrrev,
    including op_sel:[1,0,0] op_sel_hi:[0,0,0]) 4096/4096 pixels exact on both. cs_wm_half_ops is skipped on both
    devices for the same reason it is before this change (no 64-wide compute subgroup pinned).
  • python -m unittest discover -s tests/semantic on the branch and, in the same tree with the change stashed, on
    main: 7456 tests, 7 failures and 2 errors in both, the same tests (test_mutations ×2 for the
    v_mul_legacy_f32 anchor, test_loop_ballot_barrier ×3 failures + 2 errors, test_cmp_class_f64 ×2),
    skipped=1, expected failures=1. So this adds no failure and breaks no packed-shift test.
  • tests/test_pk16_diff.py 15/15 and tests/semantic/test_alu_pk_int16.py, test_ps_semantics,
    test_value_semantics, test_negative_controls pass. tests/semantic/test_mutations.py keeps detecting the
    "reads the low shift amount for both halves" mutation with its anchor moved to the new code.
  • tests/test_u16_high_half.py: 32 pass, 1 fail identically before and after —
    test_d16_buffer_store_no_redundant_extract_after_shr16 fails at spirv-val with Operand '135[%135]' requires a previous definition on a buffer_store_format_d16 fixture, unrelated to this change and reported as a finding.
  • scripts/check_no_game_data.py, tests/semantic/regen_fixtures.py --check,
    python -m compileall -q make_game.py tools tests: clean.

Related, not dependencies

AI-assisted: an agent ran the measurements and wrote the change; the doc row follows from them. GPUs and drivers are
named above.

…field extract

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 took the shift
amount with `OpBitFieldUExtract(s0, 16, 4)` and the value 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, and BitFieldUExtract(x,
16, 4) equals `(x >> 16) & 15`; checked over 200,010 inputs including 0, 1,
0xFFFF, 0x10000, 0x80000000, 0xFFFFFFFF, 0x7FFFFFFF and 0x8000. Extracts at
offset 0 are left as they were.

Coordinated as SHADER-784 in ItsNotRexed#4571, verifying SHADER-777. Counted over all 26 503
listings: 1 v_pk_lshlrev_b16 in 1 compute listing and 1 v_pk_lshrrev_b16 in another,
no v_pk_ashrrev_i16 and no
_sdwa form. Translating the corpus before and after with the same flags gives
26 503 translated and 26 473 valid in both, with 26 471 of the .spv identical
in size and SHA-256; the two that change are the two holding those instructions,
and their differences are only the replaced extracts. ps_ops4 and ps_ops6 pass
on that gfx1036 iGPU and on an NVIDIA RTX 4090 (driver 32.0.16.2124).
@fiestorra
fiestorra force-pushed the shader-packed-shifts-shift-and-mask branch from cd68a2e to a148559 Compare November 23, 2026 03:21
@shdann

shdann commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Tested a148559 on Windows 11 with an RTX 4090 (32.0.16.2101) and AMD Radeon Graphics (32.0.21043.5001): the packed-shift checks pass.

I translated a hand-written compute listing with all three packed shifts. It uses all 16 combinations of the two source selectors for each instruction. I assembled the translator output with tools/spirv_asm.py and ran vk_compute_test shift.spv 1 dump with VKC_SUBGROUP_SIZE=32. I compared each output word with a separate integer calculation. All 144 outputs match on each GPU. The input pairs were fffe0011/80017fff, 0001000f/0001beef, and ffff0000/fffe8000.

The same inputs also pass on the merge base 03b09a08 with these drivers. As a negative control, I changed the high shift-count read from bit 16 to bit 0. The RTX 4090 then returned fffefffe for a left shift that requires c000fffe.

The focused Python checks passed on CPython 3.14.7: 69 unittest cases across the packed integer, differential and semantic tests, plus 33 pytest cases in test_u16_high_half.py. I used synthetic inputs only.

I also ran the three tests in tests/semantic/test_mutations.py on both commits. Two tests fail on each commit because the v_mul_legacy_f32 mutation anchor is absent. The packed-shift mutation is detected.

AI-assisted.

@SweatierKey

Copy link
Copy Markdown
Contributor

Tested a148559b on Linux (x86_64, Python 3.13.5, SPIRV-Tools on PATH, CPU/no-GPU): pass (code read + unit tests + spirv-val + interpreter check).

What I ran:

  • git diff --check d0835dd9...HEAD: clean.
  • PYTHONPATH=tests/semantic python3 -m unittest -v tests/semantic/test_ps_semantics.py: 7/7 OK (including test_ps_ops4).
  • Translated all three packed 16-bit shifts (v_pk_lshlrev_b16, v_pk_lshrrev_b16, v_pk_ashrrev_i16) with mixed op_sel / op_sel_hi ([0,0]/[1,1], [1,1]/[0,0], [0,1]/[1,0]), verified zero OpBitField{S,U}Extract at offset 16 (%c10), validated with spirv-as --target-env vulkan1.2 + spirv-val --target-env vulkan1.2, and verified exact bit-level output via spirv_mini_interp.
  • Confirmed the updated v_pk_lshlrev_b16 anchor in tests/semantic/test_mutations.py uniquely matches tools/rdna2spirv.py.

Code review:

  • Replacing offset-16 OpBitFieldUExtract / OpBitFieldSExtract with OpShiftRightLogical (+ & 15 for shift amount) and OpShiftRightArithmetic/OpShiftRightLogical by 16 aligns v_pk_*rev_b16/i16 with the rest of the translator's high-half lowering (avoiding the AMD 25.10.36 mod-16 miscompile) with identical mathematical semantics.

@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 tests/semantic/test_mutations.py at a148559b on Windows 11 with LLVM_MC set: fails 2 of 3 tests, but both failures are the pre-existing v_mul_legacy_f32 anchor, not this change. The same two failures reproduce on main d0835dd9 with no PR applied:

AssertionError: mutation anchor must occur exactly once:
'        elif op == "v_mul_legacy_f32": ... self.emit(f"{r} = OpSelect %float {either} %f0 {m}")'

That anchor still describes the pre-flush v_mul_legacy_f32 block (c82fbe37 moved it), and #3883 / #4527 re-anchor it. The packed-shift mutation this PR does re-anchor is caught (no failure names v_pk_lshlrev_b16), so the translator change itself is consistent. git diff --check clean.

Duplicate: #4461 (Vonsek, opened 01:34) lowers the same three packed shifts with no bit-field extract at all, adds a GPU fixture (cs_w32_pk_shift.asm), a semantic test and its own mutation anchor. git merge-tree refs/remotes/pr/4461 refs/remotes/pr/4622 -> conflict in docs/design.md, tests/semantic/test_mutations.py and tools/rdna2spirv.py. #4461 is the earlier claimant (SHADER-777) and the broader fix; this PR is the smaller high-half-only lower. One of the two should close or drop its overlapping hunk — flagged on #4461 too.

@tupacfan04

Copy link
Copy Markdown
Contributor

APPROVE. compileall clean; test_pk16_diff 15/15, test_rdna2spirv_decode 24/24, test_u16_high_half OK, and the pk mutation anchor 3/3 (the two test_mutations failures are the pre-existing v_mul_legacy_f32 anchor). Negative controls: patching the high-half read back to cu(0) fails 3, and restoring main's OpBitField*Extract block passes everything. Merge clean.

One thing to reconcile: tools/rdna2spirv.py:5819-5820 emits OpShiftRightArithmetic %uint, while the project's high-half convention is %int plus as_int (:518, docs/design.md:407). Sign-extension of negative i16 halves then relies on drivers ignoring the result type's signedness. The contract row itself says %uint, so it is self-consistent but contradicts the Gen.i16_half rule in the same table - pick one and align the row.

No spirv-val/llvm-mc here, so test_translate.py mostly skips.

AI-assisted review, no game files, no GPU.

@kapitche

Copy link
Copy Markdown
Contributor

Tested a148559 merged with main d0835dd on WSL2 Ubuntu, Python 3.12.16: pass for the change, one pre-existing red.

  • test_mutations.py: 3 tests, 2 failures, both for the mutation v_mul_legacy_f32 drops zero-times-anything guard (anchor not found exactly once). The same 2 failures occur on a clean main worktree, so they are not from this PR; the updated v_pk_lshlrev_b16 mutation passes.
  • Related modules, PR head vs main: test_negative_controls.py 1 OK / 1 OK, test_ps_semantics.py 7 OK / 7 OK, test_value_semantics.py 39 OK / 39 OK, tests/test_pk16_diff.py OK. So the new lowering changes no result these cover.
  • Negative control: main's tools/rdna2spirv.py with the PR's test_mutations.py gives 4 failures (the 2 above plus the new v_pk_lshlrev_b16 anchor, absent from main's translator).
  • Read: the high-half OpShiftRightArithmetic %uint for the signed forms sign-extends the same as the old OpBitFieldSExtract(x, 16, 16); the low half still uses the offset-0 extract. I did not run the AMD driver reproduction (no gfx1036 here).

@theswiss24

Copy link
Copy Markdown

Tested a148559b (merged onto main d0835dd9) on two GPUs in a Dell Latitude 5501 (Linux Mint 22.3), test programs built from main d0835dd9 (this PR changes no C++), llvm-mc 20.1.2, LEONIDA_W64_EMULATE=1: pass. Every test has the same result as main on each device:

GPU driver run_gpu_tests.py
NVIDIA MX150 (Pascal) 580.178.04 185/210 passed, 25 skipped, 0 failed
Intel UHD 630 (Gen9.5, subgroup 8..32) Mesa ANV 25.2.8 181/210 passed, 27 skipped, 2 failed, the same 2 as main (cs_fmt_int_narrow, my finding in #4571, and cs_signed_zero_nan, the known Mesa 25.2 NIR issue)

No game files.

Tested and written by Claude (Claude Code), posting from @theswiss24's account.

@Atomic258

Copy link
Copy Markdown

Tested head a148559b — Windows, CPython 3.15.2, CPU only, synthetic fixtures, no game files: pass (with a note).

  • python -m unittest discover -s tests/semantic -p "test_mutations.py" -> 2 failures.
  • Control (main's tools/rdna2spirv.py + this test file): 4 failures — the two extra are this PR's new v_pk_lshlrev_b16 shift mutation case, whose anchor does not exist on main, so the coverage discriminates.
  • The 2 failures on the PR head are identical on main and pre-existing: stale mutation anchors. Example: the v_mul_legacy_f32 anchor expects the comment "Legacy multiply treats 0 * anything (inf/NaN included) as 0." but main's tools/rdna2spirv.py line 7587 now reads "..., after the denormal flush." — the anchor was never updated. Not introduced by this PR (also seen on a Linux box).
  • git diff --check and scripts/check_no_game_data.py clean.

AI-assisted (Devin).

@tupacfan04

Copy link
Copy Markdown
Contributor

One more point from a second check: nothing pins the value-path fix. The only new check is the shift-amount mutation (tests/semantic/test_mutations.py:62-65); reverting the value path (tools/rdna2spirv.py:5817-5823) to the old OpBitField{S,U}Extract ... 16 16 leaves test_ps_semantics, test_value_semantics and test_alu_pk_int16 all green, because the change is value-preserving by design. A shape assertion for ps_ops6 (no offset-16 extract, arithmetic shift present) would make the fix observable.

Also, docs/design.md:407 says the shift amount always comes from OpShiftRightLogical ... 16 then & 15, but the op_sel 0 half still uses OpBitFieldUExtract ... 0 4 (:5816) - align the row.

The pre-existing v_mul_legacy_f32 anchor failure in tests/semantic/test_mutations.py:80-88 reproduces on main (tools/rdna2spirv.py:7567 now flushes), so it is not this PR's.

AI-assisted review, no game files, no GPU.

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.

8 participants