rdna2spirv: the packed 16-bit shifts read their halves through the shared readers (SHADER-PK16-HALVES-CARRY) #1111

Open
github-bot wants to merge 1 commit from github-bot/gh-1111 into main AGit
Owner

@⁠klikso on GitHub · 2026-11-24 18:42 UTC · original
klikso:carry/shader-pk16-halves → main · +54 −13 · 3 files · 1 commits · open

Claimed in the coordination issue as: SHADER-PK16-HALVES-CARRY

What and why

This carries #423 by @⁠fiestorra onto current main: one commit, 3a53caad8, cherry-picked with its author kept and no change of mine. #423 was reviewed ("Looks good ... Ship", @⁠Oliwettoo) and then closed by its author on 24 November when he left the project; main still has the private copy of the half readers, and no open pull request carries the change.

v_pk_lshlrev_b16, v_pk_lshrrev_b16 and v_pk_ashrrev_i16 read their value halves with code of their own, while every other 16-bit half reader in tools/rdna2spirv.py goes through Gen.u16_half / Gen.i16_half. For v_pk_ashrrev_i16 the half was weighted as %uint and then shifted with OpShiftRightArithmetic, so the sign extension depended on how a consumer reads an arithmetic shift of an unsigned operand. Both halves now come from the shared readers, and the signed op shifts a sign-extended %int.

@⁠fiestorra: this is your work. If you want it back, say so and I will close this.

Tested

Windows 11 26200.9974, Python 3.13.14 (pytest 9.2.0), llvm-mc 23.1.6 (conda-forge, AMDGPU target), SPIRV-Tools v2026.4. Runtime test programs built from main 3aa975421 with MSVC 19.44; this change does not touch runtime/.

main with this commit
pytest tests/test_u16_high_half.py (with the two new cases) 35 pass, 2 fail 37 pass
python -I tests/semantic/test_valu_pk_shift_b16.py 8 pass 8 pass
run_gpu_tests.py --filter pk (native) 2/2 passed 2/2 passed

The first row's main column is the new test file run against main's translator: 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.

  • python -I scripts/check_no_game_data.py on the three changed files: no hits. git diff --check: clean.

GPUs tested: NVIDIA GeForce RTX 3060 Ti (Ampere), driver 621.14, Vulkan 1.4.357, Windows 11 26200

AI-assisted: yes (Claude Code; run and measured on the machine above)

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • 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: the commit updates the existing row in docs/design.md; no new rule.

🤖 Generated with Claude Code

**@⁠klikso** on GitHub · 2026-11-24 18:42 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1111) `klikso:carry/shader-pk16-halves` → `main` · +54 −13 · 3 files · 1 commits · open Claimed in the coordination issue as: SHADER-PK16-HALVES-CARRY ### What and why This carries #423 by @⁠fiestorra onto current `main`: one commit, `3a53caad8`, cherry-picked with its author kept and no change of mine. #423 was reviewed ("Looks good ... Ship", @⁠Oliwettoo) and then closed by its author on 24 November when he left the project; `main` still has the private copy of the half readers, and no open pull request carries the change. `v_pk_lshlrev_b16`, `v_pk_lshrrev_b16` and `v_pk_ashrrev_i16` read their value halves with code of their own, while every other 16-bit half reader in `tools/rdna2spirv.py` goes through `Gen.u16_half` / `Gen.i16_half`. For `v_pk_ashrrev_i16` the half was weighted as `%uint` and then shifted with `OpShiftRightArithmetic`, so the sign extension depended on how a consumer reads an arithmetic shift of an unsigned operand. Both halves now come from the shared readers, and the signed op shifts a sign-extended `%int`. @⁠fiestorra: this is your work. If you want it back, say so and I will close this. ### Tested Windows 11 26200.9974, Python 3.13.14 (pytest 9.2.0), `llvm-mc` 23.1.6 (conda-forge, AMDGPU target), SPIRV-Tools v2026.4. Runtime test programs built from `main` `3aa975421` with MSVC 19.44; this change does not touch `runtime/`. | | `main` | with this commit | |---|---|---| | `pytest tests/test_u16_high_half.py` (with the two new cases) | 35 pass, 2 fail | 37 pass | | `python -I tests/semantic/test_valu_pk_shift_b16.py` | 8 pass | 8 pass | | `run_gpu_tests.py --filter pk` (native) | 2/2 passed | 2/2 passed | The first row's `main` column is the new test file run against `main`'s translator: 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`. - `python -I scripts/check_no_game_data.py` on the three changed files: no hits. `git diff --check`: clean. GPUs tested: NVIDIA GeForce RTX 3060 Ti (Ampere), driver 621.14, Vulkan 1.4.357, Windows 11 26200 AI-assisted: yes (Claude Code; run and measured on the machine above) ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed: the commit updates the existing row in `docs/design.md`; no new rule. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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 09:38:44 +00:00
Author
Owner

@⁠abnalstlll-byte on GitHub · 2026-11-24 18:48 UTC · original · approved these changes

Reviewed and verified. Clean refactoring unifying _pk_lshlrev_b16, _pk_lshrrev_b16, and _pk_ashrrev_i16 to use shared u16_half/i16_half helpers. Ensures _pk_ashrrev_i16 shifts an explicitly sign-extended %int through OpShiftRightArithmetic. Verified against est_u16_high_half.py and est_valu_pk_shift_b16.py. 100% clean-room compliant.

**@⁠abnalstlll-byte** on GitHub · 2026-11-24 18:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1111#pullrequestreview-6045326344) · **approved these changes** Reviewed and verified. Clean refactoring unifying _pk_lshlrev_b16, _pk_lshrrev_b16, and _pk_ashrrev_i16 to use shared u16_half/i16_half helpers. Ensures _pk_ashrrev_i16 shifts an explicitly sign-extended %int through OpShiftRightArithmetic. Verified against est_u16_high_half.py and est_valu_pk_shift_b16.py. 100% clean-room compliant.
Author
Owner

@⁠Deedlyy on GitHub · 2026-11-24 20:16 UTC · original

Recommendation: [MERGE]

Reasoning: Carries #423 (reviewed "Ship"): the packed 16-bit shifts now read their halves through the shared Gen.u16_half/i16_half readers instead of a private copy. Small refactor with a new test (test_u16_high_half.py); low risk. CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with main. Merge once the queued checks are green.

Impact level: 3/10

**@⁠Deedlyy** on GitHub · 2026-11-24 20:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1111#issuecomment-7143295574) **Recommendation:** [MERGE] **Reasoning:** Carries #423 (reviewed "Ship"): the packed 16-bit shifts now read their halves through the shared `Gen.u16_half`/`i16_half` readers instead of a private copy. Small refactor with a new test (`test_u16_high_half.py`); low risk. CI (label, change-scope, no-game-data and the Runtime jobs) is still queued awaiting maintainer workflow approval, so nothing has run in CI yet; GitHub reports no merge conflict with `main`. Merge once the queued checks are green. **Impact level:** 3/10
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 18:43 UTC · original · approved these changes

Approving. Merged into current main (b9320a1) with no conflicts. tests/test_u16_high_half.py passes (37 tests), and the full translator suite gives 490 files / 6,292 tests with only test_int32_to_f64 failing, the same baseline failure as main here (Linux, Python 3.13).

Taking both halves from u16_half/i16_half and doing v_pk_ashrrev_i16 as OpShiftRightArithmetic %int on the sign-extended half makes the sign extension follow from the type. Before, it relied on a compiler treating an arithmetic shift of a %uint as signed. The logical shifts are unchanged.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 18:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1111#pullrequestreview-6063096655) · **approved these changes** Approving. Merged into current main (b9320a1) with no conflicts. `tests/test_u16_high_half.py` passes (37 tests), and the full translator suite gives 490 files / 6,292 tests with only `test_int32_to_f64` failing, the same baseline failure as main here (Linux, Python 3.13). Taking both halves from `u16_half`/`i16_half` and doing `v_pk_ashrrev_i16` as `OpShiftRightArithmetic %int` on the sign-extended half makes the sign extension follow from the type. Before, it relied on a compiler treating an arithmetic shift of a `%uint` as signed. The logical shifts are unchanged. AI-assisted (Claude).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1111/head:github-bot/gh-1111'
git switch 'github-bot/gh-1111'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1111'
git switch 'github-bot/gh-1111'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1111'
git switch 'github-bot/gh-1111'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1111'
git switch 'main'
git merge --squash 'github-bot/gh-1111'
git switch 'main'
git merge --ff-only 'github-bot/gh-1111'
git switch 'main'
git merge 'github-bot/gh-1111'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.