perf: v_perm_b32 moves bytes in groups; a run-time selector reads a 16-byte table (1.9x / up to 2.2x on a 4060 Laptop) #586

Open
KainanP wants to merge 6 commits from upstream-pr/586 into main
KainanP commented 2026-11-28 16:36:57 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-28T16:36:57Z.


Stacked on #585 (rndne), itself on #581 and #570. It uses #581's bench and #585's bulk test harness (exact_tests.rs). Only the last commit is new. Full victory-shader suite and pytest results: see #585.

What

Constant selector. The old form wrote 4 shifts, 4 masks, 3 shifts into place and 3 ors, whatever the bytes did. Now the work is grouped:

  • result bytes from the same source, moved by the same distance: one shift and one mask (no mask when the shift already clears every other bit);
  • sign bytes of one source bit: one arithmetic shift and a mask;
  • 0xFF bytes: one constant.

Instruction counts, from cost:

Constant selector Before After
byte swap 13 9
pack of two low halves 14 5
half + sign bytes 12 5
byte extract 2 2

A selector moved into a register from a literal also takes the constant form. The old run-time form happened to fold down to it; the new one's unsigned minimum is a host operation that does not fold, so it is caught explicitly.

Selector known only at run time. The old form compared each selector byte with 8, 9, 10, 11, 12 and above, each with its own select: 103 instructions. Now the selector bytes 0-15 index the 16 bytes of four dwords:

  • src1, src0;
  • the four sign bytes, built once;
  • 0xFFFFFF00 (12 → 0x00, 13-15 → 0xFF).

A byte above 15 counts as 15. Per result byte that is an unsigned minimum, a pick among the four dwords, a shift and a mask: 73 instructions. No 64-bit integers, so it still runs on hosts without INT64.

TRANSLATOR_VERSION 23.

Bench (RTX 4060 Laptop, 8 dependent chains x 256 rounds, median of 3 alternating rounds)

Form Before After HLSL Gain
run-time selector (v_perm_b32 v8, v8, v5, v6) 6.042 ms 3.148 ms — 1.92x
literal: pack lo 0x05040100 0.336 ms 0.152 ms 0.125 ms 2.2x
literal: sext16 0x0A0A0504 0.736 ms 0.465 ms (dxc folds the chain) 1.58x
literal: swap 0x04050607 0.201 ms 0.201 ms 0.188 ms 1.0x (the driver already made it a byte permute)
literal: ubyte 0x0C0C0C06 0.333 ms 0.333 ms 0.337 ms 1.0x

Where the before numbers come from:

  • The run-time row compares #585's binary with this one.
  • The literal-selector loops are new in this PR, so their before numbers come from the old lowering kept behind a test switch in the same binary. That is the same code as #585; the switch is not in the PR.
  • HLSL baselines were added to op_loop_ideal_bench.

Tests

  • perm_tests::perm_costs_one_shift_and_mask_per_byte_group (shape) fails on #585 ("a byte swap writes 13 instructions"). It asserts ≤ 9 for a swap, for the same swap moved into s9 or v9, and 5 / 2 / 5 / 73 for the other forms.

  • perm_tests::perm_with_a_run_time_selector_matches_rdna2 runs 139,264 lanes:

    • every value 0-255 of each selector byte, with the others random;
    • 2^17 selectors whose bytes are mostly 0-15.

    They are checked against RDNA 2's rule and the reference interpreter on all of them, on the RTX 4060 Laptop, Intel UHD (VICTORY_D3D12_ADAPTER=Intel) and WARP.

  • perm_tests::perm_with_a_constant_selector_matches_rdna2 takes 30 constant selectors (swaps, rotations, extracts with zero or sign bytes, packs, every code in every byte, 0x10/0x80/0xFF codes). Each runs as a literal and moved into s9, on 4,096 random pairs. They are checked against RDNA 2's rule and the interpreter on the three adapters.

AI use: written with Claude Code (Claude Opus 5.5): lowering, tests, measurements on my machine. The benches ran on my RTX 4060 Laptop, the parity runs also on Intel UHD and WARP.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5542348586 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/586). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-28T16:36:57Z. --- **Stacked on #585** (rndne), itself on #581 and #570. It uses #581's bench and #585's bulk test harness (`exact_tests.rs`). Only the last commit is new. Full `victory-shader` suite and pytest results: see #585. ## What **Constant selector.** The old form wrote 4 shifts, 4 masks, 3 shifts into place and 3 ors, whatever the bytes did. Now the work is grouped: - result bytes from the same source, moved by the same distance: one shift and one mask (no mask when the shift already clears every other bit); - sign bytes of one source bit: one arithmetic shift and a mask; - 0xFF bytes: one constant. Instruction counts, from `cost`: | Constant selector | Before | After | |---|---|---| | byte swap | 13 | 9 | | pack of two low halves | 14 | 5 | | half + sign bytes | 12 | 5 | | byte extract | 2 | 2 | A selector moved into a register from a literal also takes the constant form. The old run-time form happened to fold down to it; the new one's unsigned minimum is a host operation that does not fold, so it is caught explicitly. **Selector known only at run time.** The old form compared each selector byte with 8, 9, 10, 11, 12 and above, each with its own select: 103 instructions. Now the selector bytes 0-15 index the 16 bytes of four dwords: - src1, src0; - the four sign bytes, built once; - `0xFFFFFF00` (12 → 0x00, 13-15 → 0xFF). A byte above 15 counts as 15. Per result byte that is an unsigned minimum, a pick among the four dwords, a shift and a mask: 73 instructions. No 64-bit integers, so it still runs on hosts without INT64. `TRANSLATOR_VERSION` 23. ## Bench (RTX 4060 Laptop, 8 dependent chains x 256 rounds, median of 3 alternating rounds) | Form | Before | After | HLSL | Gain | |---|---|---|---|---| | run-time selector (`v_perm_b32 v8, v8, v5, v6`) | 6.042 ms | 3.148 ms | — | **1.92x** | | literal: pack lo `0x05040100` | 0.336 ms | 0.152 ms | 0.125 ms | **2.2x** | | literal: sext16 `0x0A0A0504` | 0.736 ms | 0.465 ms | (dxc folds the chain) | **1.58x** | | literal: swap `0x04050607` | 0.201 ms | 0.201 ms | 0.188 ms | 1.0x (the driver already made it a byte permute) | | literal: ubyte `0x0C0C0C06` | 0.333 ms | 0.333 ms | 0.337 ms | 1.0x | Where the before numbers come from: - The run-time row compares #585's binary with this one. - The literal-selector loops are new in this PR, so their before numbers come from the old lowering kept behind a test switch in the same binary. That is the same code as #585; the switch is not in the PR. - HLSL baselines were added to `op_loop_ideal_bench`. ## Tests - `perm_tests::perm_costs_one_shift_and_mask_per_byte_group` (shape) **fails on #585** ("a byte swap writes 13 instructions"). It asserts ≤ 9 for a swap, for the same swap moved into s9 or v9, and 5 / 2 / 5 / 73 for the other forms. - `perm_tests::perm_with_a_run_time_selector_matches_rdna2` runs 139,264 lanes: - every value 0-255 of each selector byte, with the others random; - 2^17 selectors whose bytes are mostly 0-15. They are checked against RDNA 2's rule and **the reference interpreter on all of them**, on the RTX 4060 Laptop, Intel UHD (`VICTORY_D3D12_ADAPTER=Intel`) and WARP. - `perm_tests::perm_with_a_constant_selector_matches_rdna2` takes 30 constant selectors (swaps, rotations, extracts with zero or sign bytes, packs, every code in every byte, 0x10/0x80/0xFF codes). Each runs as a literal and moved into s9, on 4,096 random pairs. They are checked against RDNA 2's rule and the interpreter on the three adapters. _AI use: written with Claude Code (Claude Opus 5.5): lowering, tests, measurements on my machine. The benches ran on my RTX 4060 Laptop, the parity runs also on Intel UHD and WARP._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/586:upstream-pr/586'
git switch 'upstream-pr/586'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/586'
git switch 'upstream-pr/586'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/586'
git switch 'upstream-pr/586'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/586'
git switch 'main'
git merge --squash 'upstream-pr/586'
git switch 'main'
git merge --ff-only 'upstream-pr/586'
git switch 'main'
git merge 'upstream-pr/586'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
LooperSalty/AnyXboxX!586
No description provided.