SHADER-PK16-HALVES: the packed 16-bit shifts read their halves through the shared readers #423

Closed
github-bot wants to merge 1 commit from gh-pr/423/head into gh-pr/423/base
Owner

@⁠fiestorra on GitHub · 2026-11-24 04:58 UTC · original
fiestorra:carry/pk16-shared-halves → main · +54 −13 · 3 files · 1 commits · closed without merging

Claimed in the coordination issue as: SHADER-PK16-HALVES (claim comment).

Where this starts

The packed 16-bit shifts reading a half with a shift instead of a field extract at offset 16 landed as a148559ba, which is what the old #4622 asked for; that change is on main and is not re-opened here. This is the follow-up on top of it.

What is left after a148559ba is that the three packed shifts still read their value halves with their own private copy of the code, while every other 16-bit half reader in tools/rdna2spirv.py goes through Gen.u16_half / Gen.i16_half:

  • v_pk_lshlrev_b16 / v_pk_lshrrev_b16 / v_pk_ashrrev_i16 read the high half with OpShiftRight{Logical,Arithmetic} %uint s1 16 and the low half with OpBitField{S,U}Extract … 0 16, written inline.
  • For v_pk_ashrrev_i16 the value is weighted as %uint and then shifted with OpShiftRightArithmetic, so the sign extension the op needs comes from a consumer choosing to read an arithmetic shift on an unsigned operand as arithmetic, instead of from the type of the operand. Everywhere else in the file a signed 16-bit half is as_int of the word, shifted as %int, and bitcast back when the result is unsigned.

Both value halves now come from Gen.u16_half / Gen.i16_half, and the signed op weights its half as %int (as_int of the source, sign-extended), shifts with OpShiftRightArithmetic %int, and only then bitcasts to %uint before the 0xFFFF mask. The shift amount is unchanged: (s0 >> 16) & 15 for a high half, OpBitFieldUExtract(s0, 0, 4) for a low one. Extracts at offset 0 elsewhere in the file are left as they were. The "16-bit high-half reads" row of the contract table now says both halves come from the shared readers and what the signed form weighs as %int, which is what the code does.

This is a lowering and a consistency change, not a value change: the low 16 bits of the packed result are the same, which is why no semantic test changes its expected words. The reason to do it is that the offset-16 field extract is exactly the shape an AMD Windows driver 25.10.36 on a gfx1036 iGPU was measured reading with the offset taken mod 16 (5 of 2,000 wave32 programs of tools/valu_fuzz.py --family f16 --seed 1, measured by @⁠Darkz0rz on 2026-11-22; the same 2,000 programs matched 2000/2000 on a second gfx1036 with driver 31.0.24002.92), and a private variant of a read the file already has one agreed shape for is where that kind of difference survives.

Evidence on ece4ba5b3

Head 3a53caad8, one commit rebased onto ece4ba5b3: tools/rdna2spirv.py (27 insertions, 13 deletions), tests/test_u16_high_half.py (+38 lines), and one contract row in docs/design.md. The rebase was clean; docs/design.md took my row next to the ones this repository has since gained.

  • py -3.13 -m pytest tests/test_u16_high_half.py tests/test_pk16_diff.py -q — clean ece4ba5b3: 49 passed, 12 subtests passed in 15.22s. This branch: 53 passed, 12 subtests passed in 4.40s. Four tests added, none changed.
  • Negative control, which is the part that shows the new tests bite: same branch with tools/rdna2spirv.py taken from ece4ba5b3 (git checkout refs/main -- tools/rdna2spirv.py), then pytest tests/test_u16_high_half.py -q → 2 failed, 35 passed. The two failures are test_pk_shifts_read_the_half_they_shift_like_every_other_reader[v_pk_ashrrev_i16] and test_pk_ashrrev_i16_shifts_the_value_as_signed, and both assert that the emitted reads carry the signed type. Restoring the file returns that single file to 37 passed.
  • The only fixture listings in the tree that use any of these three ops are runtime/test/shaders/ps_ops4.asm and runtime/test/shaders/ps_ops6.asm (searched every .asm under runtime/test/shaders for v_pk_(lshlrev|lshrrev|ashrrev)_b?16 in both trees: the same two files). Translated through tools/rdna2spirv.py from the assembled listings on both sides: ps_ops4 6,820 → 6,812 bytes and ps_ops6 5,524 → 5,636 bytes, and the tool reports 1 translated, 1 pass spirv-val for all four runs (spirv-as/spirv-val from Vulkan SDK 1.4.363.0).
  • Full semantic suite (py -3.13 -I scripts/run_semantic_tests.py tests/semantic): this branch 465 files, 6103 tests, 0 files failed in 354 s with 16 jobs, with llvm-mc (the one scripts/build_llvm_mc.ps1 produced, LLVM 21.1.8) on PATH and spirv-as/spirv-val from Vulkan SDK 1.4.363.0 on PATH, and LLVM_MC unset. Nothing is red with my change in. Run with LLVM_MC set to a Windows path instead, the same tree reports 1 files failed: test_image_cube_store_glc, which is that file's own helper mangling the path and has nothing to do with this branch — #419 fixes it.
  • Docs battery (py -3.13 -I -m unittest discover -s tests -p "test_docs_*.py"): Ran 47 tests in 44.733s / OK on this branch and Ran 47 tests in 45.178s / OK on clean ece4ba5b3. Same 47 tests, no failures either side.

Tested on one desktop machine, Windows build 26300, CPython 3.13.14, MSVC 14.44.35207 (used only for the unrelated runtime build in the same worktree; nothing in this PR needs a built runtime). No GPU was used for anything measured here — the translator and its tests are CPU-side; the GPU-side value comparison of the two listings was done on 2026-11-23 on the previous base and is described above only as background. Written with an AI assistant.

Supersedes the branch carried in the private tracker as #4622's follow-up.

**@⁠fiestorra** on GitHub · 2026-11-24 04:58 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/423) `fiestorra:carry/pk16-shared-halves` → `main` · +54 −13 · 3 files · 1 commits · closed without merging Claimed in the coordination issue as: `SHADER-PK16-HALVES` ([claim comment](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14)). ### Where this starts The packed 16-bit shifts reading a half with a shift instead of a field extract at offset 16 landed as `a148559ba`, which is what the old #4622 asked for; that change is on `main` and is not re-opened here. This is the follow-up on top of it. What is left after `a148559ba` is that the three packed shifts still read their value halves with their own private copy of the code, while every other 16-bit half reader in `tools/rdna2spirv.py` goes through `Gen.u16_half` / `Gen.i16_half`: * `v_pk_lshlrev_b16` / `v_pk_lshrrev_b16` / `v_pk_ashrrev_i16` read the high half with `OpShiftRight{Logical,Arithmetic} %uint s1 16` and the low half with `OpBitField{S,U}Extract … 0 16`, written inline. * For `v_pk_ashrrev_i16` the value is weighted as `%uint` and then shifted with `OpShiftRightArithmetic`, so the sign extension the op needs comes from a consumer choosing to read an arithmetic shift on an unsigned operand as arithmetic, instead of from the type of the operand. Everywhere else in the file a signed 16-bit half is `as_int` of the word, shifted as `%int`, and bitcast back when the result is unsigned. Both value halves now come from `Gen.u16_half` / `Gen.i16_half`, and the signed op weights its half as `%int` (`as_int` of the source, sign-extended), shifts with `OpShiftRightArithmetic %int`, and only then bitcasts to `%uint` before the `0xFFFF` mask. The shift amount is unchanged: `(s0 >> 16) & 15` for a high half, `OpBitFieldUExtract(s0, 0, 4)` for a low one. Extracts at offset 0 elsewhere in the file are left as they were. The "16-bit high-half reads" row of the contract table now says both halves come from the shared readers and what the signed form weighs as `%int`, which is what the code does. This is a lowering and a consistency change, not a value change: the low 16 bits of the packed result are the same, which is why no semantic test changes its expected words. The reason to do it is that the offset-16 field extract is exactly the shape an AMD Windows driver 25.10.36 on a `gfx1036` iGPU was measured reading with the offset taken mod 16 (5 of 2,000 wave32 programs of `tools/valu_fuzz.py --family f16 --seed 1`, measured by @⁠Darkz0rz on 2026-11-22; the same 2,000 programs matched 2000/2000 on a second `gfx1036` with driver 31.0.24002.92), and a private variant of a read the file already has one agreed shape for is where that kind of difference survives. ### Evidence on `ece4ba5b3` Head `3a53caad8`, one commit rebased onto `ece4ba5b3`: `tools/rdna2spirv.py` (27 insertions, 13 deletions), `tests/test_u16_high_half.py` (+38 lines), and one contract row in `docs/design.md`. The rebase was clean; `docs/design.md` took my row next to the ones this repository has since gained. * `py -3.13 -m pytest tests/test_u16_high_half.py tests/test_pk16_diff.py -q` — clean `ece4ba5b3`: `49 passed, 12 subtests passed in 15.22s`. This branch: `53 passed, 12 subtests passed in 4.40s`. Four tests added, none changed. * Negative control, which is the part that shows the new tests bite: same branch with `tools/rdna2spirv.py` taken from `ece4ba5b3` (`git checkout refs/main -- tools/rdna2spirv.py`), then `pytest tests/test_u16_high_half.py -q` → `2 failed, 35 passed`. The two failures are `test_pk_shifts_read_the_half_they_shift_like_every_other_reader[v_pk_ashrrev_i16]` and `test_pk_ashrrev_i16_shifts_the_value_as_signed`, and both assert that the emitted reads carry the signed type. Restoring the file returns that single file to 37 passed. * The only fixture listings in the tree that use any of these three ops are `runtime/test/shaders/ps_ops4.asm` and `runtime/test/shaders/ps_ops6.asm` (searched every `.asm` under `runtime/test/shaders` for `v_pk_(lshlrev|lshrrev|ashrrev)_b?16` in both trees: the same two files). Translated through `tools/rdna2spirv.py` from the assembled listings on both sides: `ps_ops4` 6,820 → 6,812 bytes and `ps_ops6` 5,524 → 5,636 bytes, and the tool reports `1 translated, 1 pass spirv-val` for all four runs (`spirv-as`/`spirv-val` from Vulkan SDK 1.4.363.0). * Full semantic suite (`py -3.13 -I scripts/run_semantic_tests.py tests/semantic`): this branch `465 files, 6103 tests, 0 files failed in 354 s with 16 jobs`, with `llvm-mc` (the one `scripts/build_llvm_mc.ps1` produced, LLVM 21.1.8) on `PATH` and `spirv-as`/`spirv-val` from Vulkan SDK 1.4.363.0 on `PATH`, and `LLVM_MC` unset. Nothing is red with my change in. Run with `LLVM_MC` set to a Windows path instead, the same tree reports `1 files failed: test_image_cube_store_glc`, which is that file's own helper mangling the path and has nothing to do with this branch — #419 fixes it. * Docs battery (`py -3.13 -I -m unittest discover -s tests -p "test_docs_*.py"`): `Ran 47 tests in 44.733s / OK` on this branch and `Ran 47 tests in 45.178s / OK` on clean `ece4ba5b3`. Same 47 tests, no failures either side. Tested on one desktop machine, Windows build 26300, CPython 3.13.14, MSVC 14.44.35207 (used only for the unrelated runtime build in the same worktree; nothing in this PR needs a built runtime). No GPU was used for anything measured here — the translator and its tests are CPU-side; the GPU-side value comparison of the two listings was done on 2026-11-23 on the previous base and is described above only as background. Written with an AI assistant. Supersedes the branch carried in the private tracker as #4622's follow-up.
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 (#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 902358c8 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 #4571, verifying @Darkz0rz's SHADER-777. Overlaps #4461
(Vonsek, the earlier claim of the same item, with a broader lowering and a GPU fixture);
flagged on both.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:59:22 +00:00
github-bot closed this pull request 2026-11-26 08:59:22 +00:00
Author
Owner

@⁠Oliwettoo on GitHub · 2026-11-24 10:37 UTC · original

Looks good. The packed shifts now share the readers the other 16-bit ops use (Gen.u16_half/Gen.i16_half), and v_pk_ashrrev_i16 shifts a sign-extended %int, so nothing depends on how a driver reads an arithmetic shift of a %uint. With the PR on current main, tests/test_u16_high_half.py (pytest, 37 passed) and tests/semantic/test_valu_pk_shift_b16.py pass. git merge-tree shows no conflict with #422 or #426, which touch the same op. Ship. AI-assisted review.

**@⁠Oliwettoo** on GitHub · 2026-11-24 10:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/423#issuecomment-7129021817) Looks good. The packed shifts now share the readers the other 16-bit ops use (`Gen.u16_half`/`Gen.i16_half`), and `v_pk_ashrrev_i16` shifts a sign-extended `%int`, so nothing depends on how a driver reads an arithmetic shift of a `%uint`. With the PR on current `main`, `tests/test_u16_high_half.py` (pytest, 37 passed) and `tests/semantic/test_valu_pk_shift_b16.py` pass. `git merge-tree` shows no conflict with #422 or #426, which touch the same op. Ship. AI-assisted review.
Author
Owner

@⁠fiestorra on GitHub · 2026-11-24 12:19 UTC · original

Closing this. I am moving my work to a different project and will not continue here. Oliwettoo's review said it looked good, and that is worth keeping on the record, but I am not going to rebase or land it. The half-sharing change stays unlanded and the branch will be deleted shortly.

Written with an AI assistant.

**@⁠fiestorra** on GitHub · 2026-11-24 12:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/423#issuecomment-7131273737) Closing this. I am moving my work to a different project and will not continue here. Oliwettoo's review said it looked good, and that is worth keeping on the record, but I am not going to rebase or land it. The half-sharing change stays unlanded and the branch will be deleted shortly. Written with an AI assistant.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.