[PR #423] [CLOSED] SHADER-PK16-HALVES: the packed 16-bit shifts read their halves through the shared readers #474

Closed
opened 2026-11-26 20:18:00 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/423
Author: @fiestorra
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: carry/pk16-shared-halves


📝 Commits (1)

  • 3a53caa rdna2spirv: the packed 16-bit shifts read their halves with the shared half readers

📊 Changes

3 files changed (+54 additions, -13 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 tests/test_u16_high_half.py (+38 -0)
📝 tools/rdna2spirv.py (+15 -12)

📄 Description

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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/423 **Author:** [@fiestorra](https://github.com/fiestorra) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `carry/pk16-shared-halves` --- ### 📝 Commits (1) - [`3a53caa`](https://github.com/ubiquitarianism/leonida/commit/3a53caad8ea5dac6e012a08847bc32f37fe7053a) rdna2spirv: the packed 16-bit shifts read their halves with the shared half readers ### 📊 Changes **3 files changed** (+54 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `tests/test_u16_high_half.py` (+38 -0) 📝 `tools/rdna2spirv.py` (+15 -12) </details> ### 📄 Description Claimed in the coordination issue as: `SHADER-PK16-HALVES` ([claim comment](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7122051420)). ### 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:18:00 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#474
No description provided.