Shaders: lower dynamic v_perm_b32 sign fills once per instruction #3600

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

@⁠Ingenioeren on GitHub · 2026-11-27 20:10 UTC · original
Ingenioeren:fix/perf-v-perm-b32-dxil → main · +109 −12 · 3 files · 1 commits · open

Dynamic v_perm_b32 now packs its four sign-fill bytes once and reuses the existing byte-index shift to select them. Previously, each of the four output bytes rebuilt all four sign fills and selected between them separately.

The one-thread synthetic shader drops from 158 to 107 emitted DXIL instructions: 51 fewer (32.3%), including its root-constant loads, UAV store, and block terminators. The test counter follows the bitcode writer's reachable-block and live-instruction plan. The test-only counter uses the same helper as #3592/#3564/#3525 so the PRs merge cleanly.

Candidate measurements (same synthetic shader harness; alternative prototypes discarded):

Lowering Before After
v_bfi_b32 22 21
v_sad_u8 55 51
dynamic v_perm_b32 (chosen) 158 107

No open PR changes the v_perm_b32 lowering (checked against the open PR list).

Bit identity follows by partitioning each unsigned selector byte: 0-7 use the existing ordinary-byte path; 8-11 index packed bytes that are FF exactly when the corresponding source bit (src1 bit 15/31, then src0 bit 15/31) is set; 12 remains zero; 13-255 remain FF. The four packed fields do not overlap. Arithmetic shifts replicate the intended bit, all shifts have defined amounts below 32, and dynamic shifts remain (selector & 3) * 8. All operands remain raw integer bits, including bit patterns representing NaNs or denormals. Literal/inline-constant selector lowering is untouched.

Validation commands:

cargo test --locked --offline -p victory-shader
cargo test --locked --offline -p victory-shader v_perm_b32_dynamic_instruction_count -- --nocapture
cargo test --locked --offline -p victory-shader v_perm_b32_dynamic_selectors_match_byte_reference -- --nocapture
cargo test --locked --offline -p victory-shader v_perm_b32_dynamic -- --nocapture
cargo clippy --locked --offline -p victory-shader --all-targets
cargo run --locked --offline -p xtask -- policy --staged
cargo fmt --all -- --check
  • Baseline 63bcca4: 300 passed, 2 failed, 3 ignored.
  • Final full run: 302 passed, 2 failed, 3 ignored; the same two failures, no new failures.
  • Both baseline failures are local dxil.dll PSV validation errors: emit::rect::tests::rectangles_get_their_fourth_corner and emit::tests::elements_sharing_a_row_keep_their_order.
  • The exact new instruction-count assertion fails with the original main lowering (158 versus expected 107) and passes with this change.
  • Before and after: all 4,352 CPU-reference cases matched on the AMD Radeon RX 6750 XT and WARP (8,704 device/case checks per version). Coverage includes every selector byte in every output position, all 16 relevant sign-bit combinations, and walking-bit/zero/all-ones source data. The synthetic DXIL also passed the available validator.
  • Clippy passed without warnings; staged policy passed for all three changed files. Formatting check reports existing repository-wide differences; no bulk formatting was applied.

Not tested: game shaders or game behavior, native GPU instruction counts, runtime performance, other adapters/operating systems, or an exhaustive enumeration of all 96 input bits. The three existing ignored tests were not enabled. The full command stops at the two unit-test failures, so integration/doc tests are not reached by that command.

AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren.

🤖 Generated with Claude Code

**@⁠Ingenioeren** on GitHub · 2026-11-27 20:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/3600) `Ingenioeren:fix/perf-v-perm-b32-dxil` → `main` · +109 −12 · 3 files · 1 commits · open Dynamic `v_perm_b32` now packs its four sign-fill bytes once and reuses the existing byte-index shift to select them. Previously, each of the four output bytes rebuilt all four sign fills and selected between them separately. The one-thread synthetic shader drops from **158 to 107 emitted DXIL instructions**: **51 fewer (32.3%)**, including its root-constant loads, UAV store, and block terminators. The test counter follows the bitcode writer's reachable-block and live-instruction plan. The test-only counter uses the same helper as #3592/#3564/#3525 so the PRs merge cleanly. Candidate measurements (same synthetic shader harness; alternative prototypes discarded): | Lowering | Before | After | | --- | ---: | ---: | | `v_bfi_b32` | 22 | 21 | | `v_sad_u8` | 55 | 51 | | dynamic `v_perm_b32` (chosen) | 158 | 107 | No open PR changes the `v_perm_b32` lowering (checked against the open PR list). Bit identity follows by partitioning each unsigned selector byte: 0-7 use the existing ordinary-byte path; 8-11 index packed bytes that are FF exactly when the corresponding source bit (src1 bit 15/31, then src0 bit 15/31) is set; 12 remains zero; 13-255 remain FF. The four packed fields do not overlap. Arithmetic shifts replicate the intended bit, all shifts have defined amounts below 32, and dynamic shifts remain `(selector & 3) * 8`. All operands remain raw integer bits, including bit patterns representing NaNs or denormals. Literal/inline-constant selector lowering is untouched. Validation commands: ```text cargo test --locked --offline -p victory-shader cargo test --locked --offline -p victory-shader v_perm_b32_dynamic_instruction_count -- --nocapture cargo test --locked --offline -p victory-shader v_perm_b32_dynamic_selectors_match_byte_reference -- --nocapture cargo test --locked --offline -p victory-shader v_perm_b32_dynamic -- --nocapture cargo clippy --locked --offline -p victory-shader --all-targets cargo run --locked --offline -p xtask -- policy --staged cargo fmt --all -- --check ``` - Baseline `63bcca4`: 300 passed, 2 failed, 3 ignored. - Final full run: 302 passed, 2 failed, 3 ignored; the same two failures, no new failures. - Both baseline failures are local `dxil.dll` PSV validation errors: `emit::rect::tests::rectangles_get_their_fourth_corner` and `emit::tests::elements_sharing_a_row_keep_their_order`. - The exact new instruction-count assertion fails with the original main lowering (158 versus expected 107) and passes with this change. - Before and after: all 4,352 CPU-reference cases matched on the AMD Radeon RX 6750 XT and WARP (8,704 device/case checks per version). Coverage includes every selector byte in every output position, all 16 relevant sign-bit combinations, and walking-bit/zero/all-ones source data. The synthetic DXIL also passed the available validator. - Clippy passed without warnings; staged policy passed for all three changed files. Formatting check reports existing repository-wide differences; no bulk formatting was applied. Not tested: game shaders or game behavior, native GPU instruction counts, runtime performance, other adapters/operating systems, or an exhaustive enumeration of all 96 input bits. The three existing ignored tests were not enabled. The full command stops at the two unit-test failures, so integration/doc tests are not reached by that command. AI assistance: prepared with OpenAI Codex under Claude Code's direction, reviewed by Ingenioeren. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-27 20:14:41 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-27 20:49 UTC · original

Independent verification (Windows 11 Pro 26200, D3D12 WARP software adapter only, dxil.dll 1.8.2502.11 from SDK 26100).

  • Tested head 6f771e68 against base main 63bcca43 (both unchanged when tested).
  • cargo test -p victory-shader --lib: head 300 passed / 4 failed / 3 ignored; base 298 passed / 4 failed / 3 ignored. The same 4 wave-op tests (synthetic_tests gathers/permutes/mask-at-loop) fail on both, so they predate this PR; no new failures. The 2 new tests pass on head. Our 4 failures differ from the 2 listed in the PR body, so the failing set depends on the environment.
  • Control: only the PR's test and counter hunks on base. v_perm_b32_dynamic_instruction_count fails with 158 vs expected 107. That confirms 158 -> 107 emitted DXIL instructions (-51). v_perm_b32_dynamic_selectors_match_byte_reference passes on base too (4352 cases matched on both backends the harness reports), so it guards results rather than size, as intended.
  • cargo clippy -p victory-shader --all-targets -- -D warnings on head: clean.
  • Review: no defects found. Sign-fill byte order is right: packed byte 0/1 = src1 bit 15/31 and byte 2/3 = src0 bit 15/31, which matches selectors 8/9/10/11. The reused shift (sel & 3) * 8 indexes those bytes correctly. Selectors 0-7 take the ordinary path (sel < 8, unsigned), 8-11 take the packed fill, 12 gives 0x00, and 13 and up give 0xFF. The AShr shifts are 31 and the Shl is 16, so all amounts are defined.
  • Size: head lower.rs is 522,080 bytes (limit 524,288). main + #3597 + #3602 + #3587 + #3600 merges textually clean to 523,671 bytes, which is 617 bytes under the limit. #3608 is based on main, not on this PR, so it does not stack.

Not tested: game shaders, native GPU, other adapters.

AI-assisted (Claude Sonnet 5.5)

**@⁠oni-swr** on GitHub · 2026-11-27 20:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/3600#issuecomment-7227871346) Independent verification (Windows 11 Pro 26200, D3D12 WARP software adapter only, dxil.dll 1.8.2502.11 from SDK 26100). - Tested head 6f771e68 against base main 63bcca43 (both unchanged when tested). - `cargo test -p victory-shader --lib`: head 300 passed / 4 failed / 3 ignored; base 298 passed / 4 failed / 3 ignored. The same 4 wave-op tests (`synthetic_tests` gathers/permutes/mask-at-loop) fail on both, so they predate this PR; no new failures. The 2 new tests pass on head. Our 4 failures differ from the 2 listed in the PR body, so the failing set depends on the environment. - Control: only the PR's test and counter hunks on base. `v_perm_b32_dynamic_instruction_count` fails with 158 vs expected 107. That confirms 158 -> 107 emitted DXIL instructions (-51). `v_perm_b32_dynamic_selectors_match_byte_reference` passes on base too (4352 cases matched on both backends the harness reports), so it guards results rather than size, as intended. - `cargo clippy -p victory-shader --all-targets -- -D warnings` on head: clean. - Review: no defects found. Sign-fill byte order is right: packed byte 0/1 = src1 bit 15/31 and byte 2/3 = src0 bit 15/31, which matches selectors 8/9/10/11. The reused shift `(sel & 3) * 8` indexes those bytes correctly. Selectors 0-7 take the ordinary path (`sel < 8`, unsigned), 8-11 take the packed fill, 12 gives 0x00, and 13 and up give 0xFF. The AShr shifts are 31 and the Shl is 16, so all amounts are defined. - Size: head `lower.rs` is 522,080 bytes (limit 524,288). main + #3597 + #3602 + #3587 + #3600 merges textually clean to 523,671 bytes, which is 617 bytes under the limit. #3608 is based on main, not on this PR, so it does not stack. Not tested: game shaders, native GPU, other adapters. AI-assisted (Claude Sonnet 5.5)
This pull request can be merged automatically.
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/3600/head:github-bot/gh-3600'
git switch 'github-bot/gh-3600'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3600'
git switch 'github-bot/gh-3600'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3600'
git switch 'github-bot/gh-3600'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3600'
git switch 'main'
git merge --squash 'github-bot/gh-3600'
git switch 'main'
git merge --ff-only 'github-bot/gh-3600'
git switch 'main'
git merge 'github-bot/gh-3600'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!3600
No description provided.