Shaders: lower align instructions through bounded shifts #3691

Open
Ingenioeren wants to merge 1 commit from upstream-pr/3691 into main
Ingenioeren commented 2026-11-28 07:27:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Ingenioeren on 2026-11-28T07:27:30Z.


v_alignbit_b32 and v_alignbyte_b32 currently mask the complementary shift, then compare/select away the high word when the shift is zero. Split that high-word shift into (a << 1) << (31 - sh): both individual shifts stay below 32, and together discard every bit at zero. This removes two written DXIL instructions from each dynamic-input form while preserving the existing shift masking.

Synthetic shader counts, including the same root-constant loads and live UAV store:

Instruction Before After
v_alignbit_b32 26 24
v_alignbyte_b32 27 25

The count regression fails on the original lowering at [26, 27] and passes at [24, 25]. The execution test uses an independent 64-bit concatenation reference: 18,432 inputs x two opcodes x four destinations x full/partial EXEC = 294,912 cases per adapter and revision. RX 6750 XT and WARP both match every result before and after, with no adapter differences. It covers every effective shift, ignored shift bits, single-bit/complemented sources, unsigned edge values, deterministic random inputs, all source/destination overlaps, and preservation of inactive destinations. Emitted DXIL validates with the local validator.

Validation (Windows, main 63bcca4):

  • cargo test -p victory-shader --locked --offline align_ -- --nocapture: tests-only control 1 passed / 1 expected count failure; final 2 passed.
  • cargo test -p victory-shader --locked --offline --no-fail-fast: baseline 300 passed / 2 failed / 3 ignored; final 302 passed / the same 2 failed / 3 ignored. Both failures are the existing local PSV validator mismatches in rectangles_get_their_fourth_corner and elements_sharing_a_row_keep_their_order. Integration tests: 3 passed / 1 ignored on both; doc tests: 0.
  • cargo clippy -p victory-shader --locked --offline --all-targets --all-features -- -D warnings: passes before and after.
  • cargo run -p xtask --locked --offline -- policy --staged: passes before (0 changed files) and after (3 changed files).
  • git diff --check: clean. Main already has formatting deviations; all three changed files retain exactly those deviations, with the added code formatted.

The test-only written-instruction counter is identical to #3600 and the subsequent lowering PRs; keep one copy when integrating. #2020 addresses the separate source-modifier handling of these opcodes; this change leaves that handling alone. Closed #1379's smaller hardware test is already in main.

The measured gain is written DXIL size (7.7% / 7.4% for these synthetic shaders). GPU timing, Linux/RADV, graphics/mesh and game behavior were not tested. No game files were used.

AI assistance: prepared with OpenAI Codex, reviewed by Ingenioeren.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5537872475 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3691). Originally posted by [Ingenioeren](https://github.com/Ingenioeren) on 2026-11-28T07:27:30Z. --- `v_alignbit_b32` and `v_alignbyte_b32` currently mask the complementary shift, then compare/select away the high word when the shift is zero. Split that high-word shift into `(a << 1) << (31 - sh)`: both individual shifts stay below 32, and together discard every bit at zero. This removes two written DXIL instructions from each dynamic-input form while preserving the existing shift masking. Synthetic shader counts, including the same root-constant loads and live UAV store: | Instruction | Before | After | |---|---:|---:| | `v_alignbit_b32` | 26 | 24 | | `v_alignbyte_b32` | 27 | 25 | The count regression fails on the original lowering at `[26, 27]` and passes at `[24, 25]`. The execution test uses an independent 64-bit concatenation reference: 18,432 inputs x two opcodes x four destinations x full/partial EXEC = 294,912 cases per adapter and revision. RX 6750 XT and WARP both match every result before and after, with no adapter differences. It covers every effective shift, ignored shift bits, single-bit/complemented sources, unsigned edge values, deterministic random inputs, all source/destination overlaps, and preservation of inactive destinations. Emitted DXIL validates with the local validator. Validation (Windows, main `63bcca4`): - `cargo test -p victory-shader --locked --offline align_ -- --nocapture`: tests-only control 1 passed / 1 expected count failure; final 2 passed. - `cargo test -p victory-shader --locked --offline --no-fail-fast`: baseline 300 passed / 2 failed / 3 ignored; final 302 passed / the same 2 failed / 3 ignored. Both failures are the existing local PSV validator mismatches in `rectangles_get_their_fourth_corner` and `elements_sharing_a_row_keep_their_order`. Integration tests: 3 passed / 1 ignored on both; doc tests: 0. - `cargo clippy -p victory-shader --locked --offline --all-targets --all-features -- -D warnings`: passes before and after. - `cargo run -p xtask --locked --offline -- policy --staged`: passes before (0 changed files) and after (3 changed files). - `git diff --check`: clean. Main already has formatting deviations; all three changed files retain exactly those deviations, with the added code formatted. The test-only written-instruction counter is identical to #3600 and the subsequent lowering PRs; keep one copy when integrating. #2020 addresses the separate source-modifier handling of these opcodes; this change leaves that handling alone. Closed #1379's smaller hardware test is already in main. The measured gain is written DXIL size (7.7% / 7.4% for these synthetic shaders). GPU timing, Linux/RADV, graphics/mesh and game behavior were not tested. No game files were used. AI assistance: prepared with OpenAI Codex, reviewed by Ingenioeren.
oni-swr commented 2026-11-28 07:54:49 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-28T07:54:49Z.


Independent software WARP verification (1127bv, VICTORY_WARP=1 + VICTORY_D3D12_WARP=1, no physical GPU touched):

  • Revisions: base origin/main 63bcca43c6a00a758510398fa43096ce7e4f7df0, PR head 63b43325d7699b25e1db1a7a9040d26a3f462b20, clean merge tree 1925dcc31cd8e514b560856cd32a1de8d945dc08 (git diff --check clean).
  • Runtime: d3d10warp.dll 10.0.26100.9818, dxil.dll 1.8.2502.11 (DXIL validation active).
  • Tests-only control on main 63bcca43 (cargo test -p victory-shader --locked --offline align_ -- --nocapture): 1 passed / 1 failed — align_instructions_use_bounded_shifts failed as expected at [26, 27] vs [24, 25]; align_matches_concatenation_with_overlapping_destinations_and_exec passed all 294,912 cases (0 mismatches).
  • Merged #3691 targeted checks:
    • cargo test -p victory-shader --locked --offline align_ -- --nocapture: 2 passed / 0 failed (align 0x14e: 24, align 0x14f: 25; 294,912 WARP cases, 0 mismatches).
    • cargo test -p victory-shader --locked --offline --features diagnostics align_ -- --nocapture: 2 passed / 0 failed (same counts and 0 mismatches).
  • Full victory-shader suite (cargo test -p victory-shader --locked --offline --no-fail-fast -- --nocapture):
    • Baseline main 63bcca43: unit 298 passed / 4 failed / 3 ignored; integration 3 passed / 0 failed / 1 ignored; doc-tests 0.
    • Merged #3691: unit 300 passed / 4 failed / 3 ignored; integration 3 passed / 0 failed / 1 ignored; doc-tests 0.
    • All 4 failures on both baseline and merged are pre-existing WARP-only hardware-test failures under VICTORY_WARP=1 (emit::synthetic_tests::{a_mask_joined_with_data_at_a_uniform_loop_is_its_number, permutes_mask_high_address_bits_and_divergent_branches, permutes_read_the_lane_named_and_zero_from_lanes_left_out} require wave-32 pipelines unsupported by WARP; gathers_at_a_level_match_the_hardware differs on WARP). Zero PSV validator failures with dxil.dll 1.8.2502.11.
  • Lint, policy, and file size:
    • cargo clippy -p victory-shader --locked --offline --all-targets --all-features -- -D warnings: pass.
    • cargo clippy -p victory-shader --locked --offline --all-targets -- -D warnings: pass.
    • cargo run -p xtask --locked --offline -- policy (191 files ok) and -- policy --staged (3 files ok): pass.
    • crates/victory-shader/src/emit/lower.rs: 521,458 B (< 524,288 B limit; 2,830 B headroom).
  • Diff review: (a << 1) << (31 - sh) keeps both shift counts in [0, 31], cleanly handles sh=0 ((a << 1) << 31 == 0), sh=31, sh>=32 masking (sh & 31 for v_alignbit_b32, (sh & 3) << 3 for v_alignbyte_b32), inactive lanes / EXEC preservation, and source/destination overlaps. #2020 source-modifier handling is untouched. Test-only written_instructions helper overlaps with #3600/#3688 (keep one copy when integrating).

AI disclosure: independent verification assisted by Gemini 4 Neon.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7236241448 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3691#issuecomment-7236241448). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-28T07:54:49Z. --- Independent software WARP verification (`1127bv`, `VICTORY_WARP=1` + `VICTORY_D3D12_WARP=1`, no physical GPU touched): - **Revisions**: base `origin/main` `63bcca43c6a00a758510398fa43096ce7e4f7df0`, PR head `63b43325d7699b25e1db1a7a9040d26a3f462b20`, clean merge tree `1925dcc31cd8e514b560856cd32a1de8d945dc08` (`git diff --check` clean). - **Runtime**: `d3d10warp.dll` `10.0.26100.9818`, `dxil.dll` `1.8.2502.11` (DXIL validation active). - **Tests-only control on `main` `63bcca43`** (`cargo test -p victory-shader --locked --offline align_ -- --nocapture`): 1 passed / 1 failed — `align_instructions_use_bounded_shifts` failed as expected at `[26, 27]` vs `[24, 25]`; `align_matches_concatenation_with_overlapping_destinations_and_exec` passed all 294,912 cases (0 mismatches). - **Merged `#3691` targeted checks**: - `cargo test -p victory-shader --locked --offline align_ -- --nocapture`: 2 passed / 0 failed (`align 0x14e: 24`, `align 0x14f: 25`; 294,912 WARP cases, 0 mismatches). - `cargo test -p victory-shader --locked --offline --features diagnostics align_ -- --nocapture`: 2 passed / 0 failed (same counts and 0 mismatches). - **Full `victory-shader` suite** (`cargo test -p victory-shader --locked --offline --no-fail-fast -- --nocapture`): - Baseline `main` `63bcca43`: unit 298 passed / 4 failed / 3 ignored; integration 3 passed / 0 failed / 1 ignored; doc-tests 0. - Merged `#3691`: unit 300 passed / 4 failed / 3 ignored; integration 3 passed / 0 failed / 1 ignored; doc-tests 0. - All 4 failures on both baseline and merged are pre-existing WARP-only hardware-test failures under `VICTORY_WARP=1` (`emit::synthetic_tests::{a_mask_joined_with_data_at_a_uniform_loop_is_its_number, permutes_mask_high_address_bits_and_divergent_branches, permutes_read_the_lane_named_and_zero_from_lanes_left_out}` require wave-32 pipelines unsupported by WARP; `gathers_at_a_level_match_the_hardware` differs on WARP). Zero PSV validator failures with `dxil.dll 1.8.2502.11`. - **Lint, policy, and file size**: - `cargo clippy -p victory-shader --locked --offline --all-targets --all-features -- -D warnings`: pass. - `cargo clippy -p victory-shader --locked --offline --all-targets -- -D warnings`: pass. - `cargo run -p xtask --locked --offline -- policy` (`191 files ok`) and `-- policy --staged` (`3 files ok`): pass. - `crates/victory-shader/src/emit/lower.rs`: `521,458` B (`< 524,288` B limit; 2,830 B headroom). - **Diff review**: `(a << 1) << (31 - sh)` keeps both shift counts in `[0, 31]`, cleanly handles `sh=0` (`(a << 1) << 31 == 0`), `sh=31`, `sh>=32` masking (`sh & 31` for `v_alignbit_b32`, `(sh & 3) << 3` for `v_alignbyte_b32`), inactive lanes / EXEC preservation, and source/destination overlaps. `#2020` source-modifier handling is untouched. Test-only `written_instructions` helper overlaps with `#3600`/`#3688` (keep one copy when integrating). AI disclosure: independent verification assisted by Gemini 4 Neon.
ColorMeBoom commented 2026-11-28 15:51:53 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ColorMeBoom on 2026-11-28T15:51:53Z.


Hardware run: RX 5700 XT (Navi 10, RDNA1, 32–64 lanes, driver 32.0.21045.29003, Windows 11 26200), head 63b4332 on 63bcca4.

  • align_matches_concatenation_with_overlapping_destinations_and_exec passes. All 16 rows of v_alignbit_b32 and v_alignbyte_b32 (18,432 inputs each, overlapping destinations and partial EXEC) have zero mismatches on the card and on WARP. align_instructions_use_bounded_shifts passes.
  • Full victory-shader --lib --features host: 304 passed, 0 failed, 3 ignored.

AI use: Claude Code (Claude Opus 5.5).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7241793904 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3691#issuecomment-7241793904). Originally posted by [ColorMeBoom](https://github.com/ColorMeBoom) on 2026-11-28T15:51:53Z. --- Hardware run: RX 5700 XT (Navi 10, RDNA1, 32–64 lanes, driver 32.0.21045.29003, Windows 11 26200), head `63b4332` on `63bcca4`. - `align_matches_concatenation_with_overlapping_destinations_and_exec` passes. All 16 rows of `v_alignbit_b32` and `v_alignbyte_b32` (18,432 inputs each, overlapping destinations and partial EXEC) have zero mismatches on the card and on WARP. `align_instructions_use_bounded_shifts` passes. - Full `victory-shader --lib --features host`: 304 passed, 0 failed, 3 ignored. AI use: Claude Code (Claude Opus 5.5).
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/3691:upstream-pr/3691'
git switch 'upstream-pr/3691'

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/3691'
git switch 'upstream-pr/3691'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3691'
git switch 'upstream-pr/3691'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3691'
git switch 'main'
git merge --squash 'upstream-pr/3691'
git switch 'main'
git merge --ff-only 'upstream-pr/3691'
git switch 'main'
git merge 'upstream-pr/3691'
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
Marvisak/VIctory!3691
No description provided.