rdna2spirv: 64-bit scalar data ops lower to 2x32-bit, no Int64 (SHADER-930) #224

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

@⁠Andretuta on GitHub · 2026-11-24 02:48 UTC · original
ubiquitarianism:old-pr/9958 → main · +566 −39 · 11 files · 1 commits · draft

Carried over from old PR 9958 by slipperysquid in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9958, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
slipperysquid: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Item: SHADER-930 (up for grabs in the coordination issue; the earlier claim by abnalstlll-byte on #5156 had no code)

What and why

SHADER-930 asks for a pure 2x32-bit lowering, with no Int64 capability, of the 64-bit scalar data ops s_brev_b64, s_bfe_u64, s_bfe_i64, s_bitset0_b64, s_bitset1_b64, s_flbit_i32_b64 and s_flbit_i32_i64, plus a hardware measurement.

I checked each op on main first. Five of the seven were already pure 2x32-bit. s_bfe_u64/s_bfe_i64 were not: they joined the pair into an OpTypeInt 64 value, did 64-bit shifts and set uses_psb. That put OpCapability Int64 and the PSB declarations into every module that used them, including a pixel shader that touches no memory.

op before (main) after
s_brev_b64 2x32 (OpBitReverse on each dword, swapped) unchanged; new edge tests and GPU fixture
s_bfe_u64 64-bit: OpTypeInt 64, OpShiftLeft/RightLogical %ulong, sets uses_psb, so Int64 + PSB 2x32: funnel shift right by the offset (all shift amounts 0..31), then OpBitFieldUExtract on each dword, count min(w,32) / w-min(w,32)
s_bfe_i64 64-bit: as above plus OpShiftRightArithmetic %long 2x32: same funnel, OpBitFieldSExtract on each dword; the high dword takes the low dword's sign when w <= 32
s_bitset0_b64 2x32 (bit S0 & 63, selected into one dword) unchanged; new edge tests and GPU fixture
s_bitset1_b64 2x32 unchanged; new no-Int64 test
s_flbit_i32_b64 2x32 (FindUMsb per dword) unchanged; new edge tests and GPU fixture
s_flbit_i32_i64 2x32 (~S0 when negative, then as b64) unchanged; new edge tests and GPU fixture

The semantics stay as before (RDNA2 ISA SOP1/SOP2, and the docs/design.md row): offset = S1[5:0], width = S1[22:16], a field past bit 63 stops there (width = min(width, 64 - offset)), s_bfe_i64 sign-extends from the top bit it keeps, and SCC = D != 0. The other six ops don't write SCC. In the new lowering no shift amount reaches 32, including on the discarded side of an OpSelect, because SPIR-V leaves that undefined.

Changes

  • tools/rdna2spirv.py: a new s_bfe_u64/s_bfe_i64 lowering. It no longer sets uses_psb.
  • tests/semantic/test_salu64_2x32.py (new):
    • None of the seven ops emits OpCapability Int64 or OpTypeInt 64, with SGPR-pair or constant sources.
    • s_bfe_u64/s_bfe_i64 value and SCC over offsets {0, 1, 31, 32, 33, 63} × widths {0, 1, 31, 32, 33, 63, 64, 65, 127} × 2 sources, with SCC preset to the opposite value.
    • Control bits S1[31:23] and S1[15:6] are ignored.
    • Both new GPU fixtures give their header words through spirv_mini_interp, and their header, check.py and run_gpu_tests.py expect the same words.
  • tests/semantic/test_salu_bfe_64.py: the two emission tests checked for %ulong/%long and uses_psb. They now check for the 32-bit ops and for no 64-bit type.
  • runtime/test/shaders/cs_w32_salu64_bfe.asm, cs_w32_salu64_bits.asm (new hand-written GPU fixtures; derivations in their headers) and their translator inputs in tests/semantic/fixtures/disasm/ (from regen_fixtures.py). Both are wired into runtime/test/shaders/check.py and scripts/run_gpu_tests.py.
  • tools/int_hw_probe.py: 11 more salu_64_data cases with the same edges, ready for a native run.
  • docs/design.md: the contract row for the 64-bit scalar data ops now covers the 2x32 lowering and how it was confirmed.

Tested

Linux, AMD Radeon RX 6800 XT and NVIDIA GeForce RTX 3070 in the same machine:

  • python -I scripts/run_gpu_tests.py --llvm-mc /usr/lib/llvm-21/bin/llvm-mc --filter cs_w32_salu64 (cs_w32_salu64, cs_w32_salu64_bfe, cs_w32_salu64_bits): 3 translated, 3 pass spirv-val, 3/3 PASS on each of:
    • AMD Radeon RX 6800 XT (RADV NAVI21, RDNA2 gfx1030), Mesa 26.2.7 (kisak-mesa PPA), Vulkan 1.4.354
    • NVIDIA GeForce RTX 3070, driver 595.99.02, Vulkan 1.4.329
    • llvmpipe, Mesa 26.2.7, LLVM 21.1.8
  • Negative control on the RX 6800 XT: making the funnel treat offset 32 as below 32 fails cs_w32_salu64_bfe with out[7] = 0x5CC0000E, the word its header predicts. Swapping the w <= 32 sign select fails 76 checks in test_salu64_2x32.py. Both were reverted.
  • python -m unittest discover -s tests/semantic -p test_salu64_2x32.py: 8 tests OK. test_salu*64*.py: all OK.
  • python -I scripts/run_semantic_tests.py tests/semantic: 460 files, 6054 tests in 77 s on 16 jobs. Two files fail: test_exec_computed (2 failures) and test_image_atomics_guard (47 failures). Both fail the same way on unmodified upstream/main (e56d9795), so this PR does not cause them. (Separately, python -I tests/test_shader_checks.py has 15 errors on main from a test harness stub that doesn't take cwd=; also not from this PR.)
  • python -I tools/int_hw_probe.py --group salu_64_data (translator side): 21/21 match. --self-test: OK. python -I tests/test_int_hw_probe.py: OK.
  • python -I tests/semantic/regen_fixtures.py --check: clean. python -m compileall -q make_game.py tools tests: clean. python -I scripts/check_no_game_data.py on every new file: clean.

The 64-bit SALU instructions have not been run natively in this PR; the GPU runs above run the translated SPIR-V. They were already measured natively on gfx1100 and gfx1035 (see the design.md row). tools/hw_oracle works on this machine (llvm-21 clang with amdgcn, ld.lld, libdrm_amdgpu 2.4.134, gcc), but the RX 6800 XT drives the desktop displays, and the DRM launcher refuses such a GPU unless LEONIDA_HW_DRM_ALLOW_DISPLAY=1 is set, because a hung kernel resets the GPU. To measure natively on gfx1030 with the new edge cases:

LEONIDA_HW_DRM_NODE=/dev/dri/renderD129 LEONIDA_HW_DRM_ALLOW_DISPLAY=1 LEONIDA_AMD_CLANG=/usr/lib/llvm-21/bin/clang \
  python -I tools/int_hw_probe.py --oracle both --wave both --group salu_64_data

The item names gfx1036. gfx1030 has the same RDNA2 ISA.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. The fixtures are small hand-written .asm.
  • The changed translator rule is in the contract table of docs/design.md, with how it was confirmed.

AI-assisted (Claude Code).

🤖 Generated with Claude Code

**@⁠Andretuta** on GitHub · 2026-11-24 02:48 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/224) `ubiquitarianism:old-pr/9958` → `main` · +566 −39 · 11 files · 1 commits · draft > Carried over from **old PR 9958** by `slipperysquid` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9958`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `slipperysquid`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): Item: SHADER-930 (up for grabs in the coordination issue; the earlier claim by `abnalstlll-byte` on `#5156` had no code) ### What and why SHADER-930 asks for a pure 2x32-bit lowering, with no `Int64` capability, of the 64-bit scalar data ops `s_brev_b64`, `s_bfe_u64`, `s_bfe_i64`, `s_bitset0_b64`, `s_bitset1_b64`, `s_flbit_i32_b64` and `s_flbit_i32_i64`, plus a hardware measurement. I checked each op on main first. Five of the seven were already pure 2x32-bit. `s_bfe_u64`/`s_bfe_i64` were not: they joined the pair into an `OpTypeInt 64` value, did 64-bit shifts and set `uses_psb`. That put `OpCapability Int64` and the PSB declarations into every module that used them, including a pixel shader that touches no memory. | op | before (main) | after | |---|---|---| | `s_brev_b64` | 2x32 (`OpBitReverse` on each dword, swapped) | unchanged; new edge tests and GPU fixture | | `s_bfe_u64` | **64-bit**: `OpTypeInt 64`, `OpShiftLeft/RightLogical %ulong`, sets `uses_psb`, so `Int64` + PSB | **2x32**: funnel shift right by the offset (all shift amounts 0..31), then `OpBitFieldUExtract` on each dword, count min(w,32) / w-min(w,32) | | `s_bfe_i64` | **64-bit**: as above plus `OpShiftRightArithmetic %long` | **2x32**: same funnel, `OpBitFieldSExtract` on each dword; the high dword takes the low dword's sign when w <= 32 | | `s_bitset0_b64` | 2x32 (bit `S0 & 63`, selected into one dword) | unchanged; new edge tests and GPU fixture | | `s_bitset1_b64` | 2x32 | unchanged; new no-Int64 test | | `s_flbit_i32_b64` | 2x32 (`FindUMsb` per dword) | unchanged; new edge tests and GPU fixture | | `s_flbit_i32_i64` | 2x32 (`~S0` when negative, then as b64) | unchanged; new edge tests and GPU fixture | The semantics stay as before (RDNA2 ISA SOP1/SOP2, and the `docs/design.md` row): offset = S1[5:0], width = S1[22:16], a field past bit 63 stops there (width = min(width, 64 - offset)), `s_bfe_i64` sign-extends from the top bit it keeps, and SCC = D != 0. The other six ops don't write SCC. In the new lowering no shift amount reaches 32, including on the discarded side of an `OpSelect`, because SPIR-V leaves that undefined. ### Changes - `tools/rdna2spirv.py`: a new `s_bfe_u64`/`s_bfe_i64` lowering. It no longer sets `uses_psb`. - `tests/semantic/test_salu64_2x32.py` (new): - None of the seven ops emits `OpCapability Int64` or `OpTypeInt 64`, with SGPR-pair or constant sources. - `s_bfe_u64`/`s_bfe_i64` value and SCC over offsets {0, 1, 31, 32, 33, 63} × widths {0, 1, 31, 32, 33, 63, 64, 65, 127} × 2 sources, with SCC preset to the opposite value. - Control bits S1[31:23] and S1[15:6] are ignored. - Both new GPU fixtures give their header words through `spirv_mini_interp`, and their header, `check.py` and `run_gpu_tests.py` expect the same words. - `tests/semantic/test_salu_bfe_64.py`: the two emission tests checked for `%ulong`/`%long` and `uses_psb`. They now check for the 32-bit ops and for no 64-bit type. - `runtime/test/shaders/cs_w32_salu64_bfe.asm`, `cs_w32_salu64_bits.asm` (new hand-written GPU fixtures; derivations in their headers) and their translator inputs in `tests/semantic/fixtures/disasm/` (from `regen_fixtures.py`). Both are wired into `runtime/test/shaders/check.py` and `scripts/run_gpu_tests.py`. - `tools/int_hw_probe.py`: 11 more `salu_64_data` cases with the same edges, ready for a native run. - `docs/design.md`: the contract row for the 64-bit scalar data ops now covers the 2x32 lowering and how it was confirmed. ### Tested Linux, AMD Radeon RX 6800 XT and NVIDIA GeForce RTX 3070 in the same machine: - `python -I scripts/run_gpu_tests.py --llvm-mc /usr/lib/llvm-21/bin/llvm-mc --filter cs_w32_salu64` (`cs_w32_salu64`, `cs_w32_salu64_bfe`, `cs_w32_salu64_bits`): 3 translated, 3 pass spirv-val, 3/3 PASS on each of: - AMD Radeon RX 6800 XT (RADV NAVI21, RDNA2 gfx1030), Mesa 26.2.7 (kisak-mesa PPA), Vulkan 1.4.354 - NVIDIA GeForce RTX 3070, driver 595.99.02, Vulkan 1.4.329 - llvmpipe, Mesa 26.2.7, LLVM 21.1.8 - Negative control on the RX 6800 XT: making the funnel treat offset 32 as below 32 fails `cs_w32_salu64_bfe` with out[7] = 0x5CC0000E, the word its header predicts. Swapping the w <= 32 sign select fails 76 checks in `test_salu64_2x32.py`. Both were reverted. - `python -m unittest discover -s tests/semantic -p test_salu64_2x32.py`: 8 tests OK. `test_salu*64*.py`: all OK. - `python -I scripts/run_semantic_tests.py tests/semantic`: 460 files, 6054 tests in 77 s on 16 jobs. Two files fail: `test_exec_computed` (2 failures) and `test_image_atomics_guard` (47 failures). Both fail the same way on unmodified `upstream/main` (e56d9795), so this PR does not cause them. (Separately, `python -I tests/test_shader_checks.py` has 15 errors on main from a test harness stub that doesn't take `cwd=`; also not from this PR.) - `python -I tools/int_hw_probe.py --group salu_64_data` (translator side): 21/21 match. `--self-test`: OK. `python -I tests/test_int_hw_probe.py`: OK. - `python -I tests/semantic/regen_fixtures.py --check`: clean. `python -m compileall -q make_game.py tools tests`: clean. `python -I scripts/check_no_game_data.py` on every new file: clean. The 64-bit SALU instructions have not been run natively in this PR; the GPU runs above run the translated SPIR-V. They were already measured natively on gfx1100 and gfx1035 (see the design.md row). `tools/hw_oracle` works on this machine (llvm-21 clang with amdgcn, ld.lld, libdrm_amdgpu 2.4.134, gcc), but the RX 6800 XT drives the desktop displays, and the DRM launcher refuses such a GPU unless `LEONIDA_HW_DRM_ALLOW_DISPLAY=1` is set, because a hung kernel resets the GPU. To measure natively on gfx1030 with the new edge cases: ```sh LEONIDA_HW_DRM_NODE=/dev/dri/renderD129 LEONIDA_HW_DRM_ALLOW_DISPLAY=1 LEONIDA_AMD_CLANG=/usr/lib/llvm-21/bin/clang \ python -I tools/int_hw_probe.py --oracle both --wave both --group salu_64_data ``` The item names gfx1036. gfx1030 has the same RDNA2 ISA. ### Checklist - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. The fixtures are small hand-written `.asm`. - [x] The changed translator rule is in the contract table of `docs/design.md`, with how it was confirmed. AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
s_bfe_u64 / s_bfe_i64 built a 64-bit OpTypeInt value and set uses_psb, so
every module using them declared Int64 and the PSB types. They now funnel-shift
the dword pair right by the offset (every shift amount 0..31) and keep
min(w, 32) bits of the low dword and the rest of the high dword with one 32-bit
OpBitField[US]Extract each; s_bfe_i64's high dword takes the low dword's sign
when w <= 32. Semantics and SCC are unchanged. s_brev_b64, s_bitset0/1_b64 and
s_flbit_i32_b64/i64 were already 2x32-bit.

- tests/semantic/test_salu64_2x32.py: no Int64 / OpTypeInt 64 for all seven
  ops; bfe value + SCC over offsets {0,1,31,32,33,63} x widths
  {0,1,31,32,33,63,64,65,127}; the two new GPU fixtures on the CPU.
- runtime/test/shaders/cs_w32_salu64_bfe.asm, cs_w32_salu64_bits.asm: GPU
  fixtures with edge cases, wired into check.py and run_gpu_tests.py; PASS on
  RX 6800 XT (RADV, Mesa 26.2.7), RTX 3070 (595.99.02) and llvmpipe.
- tools/int_hw_probe.py: 11 more salu_64_data edge cases for a native run.
- docs/design.md: the contract row says how this was confirmed.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 08:49:20 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-24 14:24 UTC · original

Independent Linux CPU check on draft head 135d44bee33909164cc693cc6689c7ab356b98f5: the new test_salu64_2x32.py passes 8/8, but it misses a signed-literal case affecting both new lowerings. Translating s_bfe_i64 s[4:5], 0xffffff9c, 0x200020 plus s_flbit_i32_i64 s8, 0xffffff9c and interpreting the SPIR-V yields (s8, s4, s5, SCC) = (0x20, 0, 0, 0). Replacing the literal spelling with equivalent decimal -100 yields (0x39, 0xffffffff, 0xffffffff, 1). LLVM emits the hexadecimal spelling for the negative 32-bit literal. The current code path appears to treat this encoding as a zero-extended 64-bit value; please add a regression for the canonical disassembly spelling before considering the lowering complete. Synthetic input only; no GPU or game data.

**@⁠KernelSmoker** on GitHub · 2026-11-24 14:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/224#issuecomment-7134317527) Independent Linux CPU check on draft head `135d44bee33909164cc693cc6689c7ab356b98f5`: the new `test_salu64_2x32.py` passes 8/8, but it misses a signed-literal case affecting both new lowerings. Translating `s_bfe_i64 s[4:5], 0xffffff9c, 0x200020` plus `s_flbit_i32_i64 s8, 0xffffff9c` and interpreting the SPIR-V yields `(s8, s4, s5, SCC) = (0x20, 0, 0, 0)`. Replacing the literal spelling with equivalent decimal `-100` yields `(0x39, 0xffffffff, 0xffffffff, 1)`. LLVM emits the hexadecimal spelling for the negative 32-bit literal. The current code path appears to treat this encoding as a zero-extended 64-bit value; please add a regression for the canonical disassembly spelling before considering the lowering complete. Synthetic input only; no GPU or game data.
This pull request has changes conflicting with the target branch.
  • docs/design.md
  • runtime/test/shaders/check.py
  • scripts/run_gpu_tests.py
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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