Skip to content

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

Open
Ingenioeren wants to merge 1 commit into
Marvisak:mainfrom
Ingenioeren:fix/perf-v-perm-b32-dxil
Open

Ingenioeren wants to merge 1 commit into
Marvisak:mainfrom
Ingenioeren:fix/perf-v-perm-b32-dxil

Conversation

@Ingenioeren

Copy link
Copy Markdown
Contributor

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

@oni-swr

oni-swr commented Nov 27, 2026

Copy link
Copy Markdown

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

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

AI-assisted (Claude Sonnet 5.5)

@ColorMeBoom

Copy link
Copy Markdown

Hardware run on AMD, since the report so far is WARP only: RX 5700 XT (Navi 10, RDNA1, driver 32.0.21045.29003, Windows 11 26200), head 6f771e6 on 63bcca4.

  • v_perm_b32_dynamic_selectors_match_byte_reference passes: v_perm_b32: hardware: 4352 cases matched.
  • Full victory-shader --lib --features host: 304 passed, 0 failed, 3 ignored.

AI use: Claude Code (Claude Opus 5.5).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants