Shaders: simplify the high-half offset in s_ff1_i32_b64 #3688

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

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


s_ff1_i32_b64 scans two dwords and offsets a high-half match by 32. Replace the high-half add/compare/select with FirstbitLo(high) | 32: results 0–31 become 32–63, and the 0xffffffff no-match sentinel stays unchanged. The low-half selection and SCC behavior stay the same. DXIL FirstbitLo contract.

A synthetic compute shader with two dynamic root constants and a live UAV store emits 23 → 21 DXIL instructions (8.7% fewer). This counts the whole written function, including setup and terminators; GPU time and game frame rate were not measured. The test-only counter is identical to #3600 and the subsequent lowering PRs.

Validation on Windows, RX 6750 XT and WARP, using synthetic inputs only:

  • cargo test --locked --offline -p victory-shader scalar_first_set_bit_64 -- --nocapture: tests-only control fails the count assertion at 23 versus 21; final passes both tests. The execution test passes before and after on both adapters: 14,720 inputs, three destination choices (separate, overlapping low, overlapping high), both SCC values, zero model mismatches or adapter differences. It includes every single-bit position, every pair of low/high first-bit positions, zero, complements, prefixes, and deterministic random inputs. The reference searches individual bits. DXIL validates.
  • cargo test --locked --offline -p victory-shader --no-fail-fast: baseline 300 passed / 2 known failures / 3 ignored; final 302 passed / the same 2 failures / 3 ignored. Failures are rectangles_get_their_fourth_corner and elements_sharing_a_row_keep_their_order (local dxil.dll PSV mismatch). Integration: 3 passed / 1 ignored; doc tests: 0.
  • cargo clippy --locked --offline -p victory-shader --all-targets --all-features -- -D warnings: passed before and after.
  • cargo run --locked --offline -p xtask -- policy --staged: passed, all 3 staged files.
  • git diff --cached --check: passed. Rustfmt comparisons retain exactly main's existing formatting deviations in all three changed files.

#148/#2063 add first-clear-bit support near this arm; this change only simplifies the existing first-set-bit lowering. Depending on merge order, the shared arm may need a small rebase.

No game files or game run. The new execution probe uses wave32. Other vendors, Linux/RADV, graphics and mesh execution were not tested.

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

<!-- greyfinch-sync:Marvisak/VIctory:issue:5537681209 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3688). Originally posted by [Ingenioeren](https://github.com/Ingenioeren) on 2026-11-28T07:01:30Z. --- `s_ff1_i32_b64` scans two dwords and offsets a high-half match by 32. Replace the high-half add/compare/select with `FirstbitLo(high) | 32`: results 0–31 become 32–63, and the `0xffffffff` no-match sentinel stays unchanged. The low-half selection and SCC behavior stay the same. [DXIL FirstbitLo contract](https://github.com/microsoft/DirectXShaderCompiler/blob/main/docs/DXIL.rst#firstbitlo). A synthetic compute shader with two dynamic root constants and a live UAV store emits **23 → 21 DXIL instructions (8.7% fewer)**. This counts the whole written function, including setup and terminators; GPU time and game frame rate were not measured. The test-only counter is identical to #3600 and the subsequent lowering PRs. Validation on Windows, RX 6750 XT and WARP, using synthetic inputs only: - `cargo test --locked --offline -p victory-shader scalar_first_set_bit_64 -- --nocapture`: tests-only control fails the count assertion at 23 versus 21; final passes both tests. The execution test passes before and after on both adapters: 14,720 inputs, three destination choices (separate, overlapping low, overlapping high), both SCC values, zero model mismatches or adapter differences. It includes every single-bit position, every pair of low/high first-bit positions, zero, complements, prefixes, and deterministic random inputs. The reference searches individual bits. DXIL validates. - `cargo test --locked --offline -p victory-shader --no-fail-fast`: baseline 300 passed / 2 known failures / 3 ignored; final 302 passed / the same 2 failures / 3 ignored. Failures are `rectangles_get_their_fourth_corner` and `elements_sharing_a_row_keep_their_order` (local dxil.dll PSV mismatch). Integration: 3 passed / 1 ignored; doc tests: 0. - `cargo clippy --locked --offline -p victory-shader --all-targets --all-features -- -D warnings`: passed before and after. - `cargo run --locked --offline -p xtask -- policy --staged`: passed, all 3 staged files. - `git diff --cached --check`: passed. Rustfmt comparisons retain exactly main's existing formatting deviations in all three changed files. #148/#2063 add first-clear-bit support near this arm; this change only simplifies the existing first-set-bit lowering. Depending on merge order, the shared arm may need a small rebase. No game files or game run. The new execution probe uses wave32. Other vendors, Linux/RADV, graphics and mesh execution were not tested. AI assistance: prepared with OpenAI Codex, reviewed by Ingenioeren.
oni-swr commented 2026-11-28 07:25:43 +00:00 (Migrated from github.com)

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


Independent Software WARP Verification

  • Base (main): 63bcca43c6a00a758510398fa43096ce7e4f7df0
  • PR #3688 head: d9e912fdc9984598308cba59f19e02b694f15636 (clean fast-forward merge; merge tree 54b7a5edcc041677b0c8612baeb59d334b1638cc; also merges cleanly with #3679 into tree 6526713a581880b3eeb40fb931a3d69cd1658847)
  • Environment: Windows software WARP only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1); DXGI reports Microsoft Basic Render Driver (VendorId=0x1414, DeviceId=0x008C, Flags=0x2)
  • DLLs: d3d10warp.dll 10.0.26100.9818, dxil.dll 1.8.2502.11

Results (base vs merged)

  • Base 63bcca43:
    • cargo test --locked -p victory-shader -j 4 --no-fail-fast: lib 298 passed, 4 failed, 3 ignored; integration (resource_tracking) 3 passed, 0 failed, 1 ignored; doc-tests 0. (The 4 lib failures are WARP-expected hardware-only tests: a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_mask_high_address_bits_and_divergent_branches, permutes_read_the_lane_named_and_zero_from_lanes_left_out.)
    • Regression control (PR tests on base lowering): scalar_first_set_bit_64_instruction_count failed as expected (23 vs 21 DXIL instructions); scalar_first_set_bit_64_matches_the_reference passed (14,720 inputs, 0 mismatches).
  • Merged #3688 (d9e912fd):
    • cargo test --locked -p victory-shader scalar_first_set_bit_64 -j 4 -- --nocapture: 2 passed, 0 failed (21 DXIL instructions; execution test 14,720 inputs, 0 mismatches).
    • cargo test --locked -p victory-shader -j 4 --no-fail-fast: lib 300 passed, 4 failed, 3 ignored (+2 new tests passing, exact same 4 pre-existing WARP-only failures); integration 3 passed, 0 failed, 1 ignored; doc-tests 0.
    • cargo clippy --locked -p victory-shader --all-targets --all-features -j 4 -- -D warnings: passed
    • cargo xtask policy: passed (191 files ok); crates/victory-shader/src/emit/lower.rs is 521,070 / 524,288 bytes (+507 bytes over base 520,563, leaving 3,218 bytes of headroom below the 512 KiB policy cap).

Diff Review

  • Semantics: DXIL FirstbitLo(high) returns 0..=31 when high != 0 (bit 5 is 0, so fb | 32 == fb + 32 in 32..=63) and 0xffffffff when high == 0 (bit 5 is already 1, so 0xffffffff | 32 == 0xffffffff). Low-half precedence (a != 0 ? fa : fb) and SCC preservation (unchanged by s_ff1_i32_b64, verified across both SCC states and overlapping destinations) are preserved.
  • Overlap: Adjacent to #148/#2063 (s_ff0_i32_b32/s_ff0_i32_b64, currently 160 commits behind main); only touches the existing s_ff1_i32_b64 arm.

Verified by Gemini 4 Neon (Antigravity) on software WARP.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7235932916 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3688#issuecomment-7235932916). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-28T07:25:43Z. --- ### Independent Software WARP Verification - **Base (`main`)**: `63bcca43c6a00a758510398fa43096ce7e4f7df0` - **PR `#3688` head**: `d9e912fdc9984598308cba59f19e02b694f15636` (clean fast-forward merge; merge tree `54b7a5edcc041677b0c8612baeb59d334b1638cc`; also merges cleanly with `#3679` into tree `6526713a581880b3eeb40fb931a3d69cd1658847`) - **Environment**: Windows software WARP only (`VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`); DXGI reports `Microsoft Basic Render Driver (VendorId=0x1414, DeviceId=0x008C, Flags=0x2)` - **DLLs**: `d3d10warp.dll` `10.0.26100.9818`, `dxil.dll` `1.8.2502.11` #### Results (base vs merged) - **Base `63bcca43`**: - `cargo test --locked -p victory-shader -j 4 --no-fail-fast`: lib **298 passed, 4 failed, 3 ignored**; integration (`resource_tracking`) **3 passed, 0 failed, 1 ignored**; doc-tests **0**. (The 4 lib failures are WARP-expected hardware-only tests: `a_mask_joined_with_data_at_a_uniform_loop_is_its_number`, `gathers_at_a_level_match_the_hardware`, `permutes_mask_high_address_bits_and_divergent_branches`, `permutes_read_the_lane_named_and_zero_from_lanes_left_out`.) - Regression control (PR tests on base lowering): `scalar_first_set_bit_64_instruction_count` **failed as expected** (`23` vs `21` DXIL instructions); `scalar_first_set_bit_64_matches_the_reference` **passed** (14,720 inputs, 0 mismatches). - **Merged `#3688` (`d9e912fd`)**: - `cargo test --locked -p victory-shader scalar_first_set_bit_64 -j 4 -- --nocapture`: **2 passed, 0 failed** (`21` DXIL instructions; execution test 14,720 inputs, 0 mismatches). - `cargo test --locked -p victory-shader -j 4 --no-fail-fast`: lib **300 passed, 4 failed, 3 ignored** (+2 new tests passing, exact same 4 pre-existing WARP-only failures); integration **3 passed, 0 failed, 1 ignored**; doc-tests **0**. - `cargo clippy --locked -p victory-shader --all-targets --all-features -j 4 -- -D warnings`: **passed** - `cargo xtask policy`: **passed** (`191 files ok`); `crates/victory-shader/src/emit/lower.rs` is **521,070 / 524,288 bytes** (+507 bytes over base `520,563`, leaving **3,218 bytes** of headroom below the 512 KiB policy cap). #### Diff Review - **Semantics**: DXIL `FirstbitLo(high)` returns `0..=31` when `high != 0` (bit 5 is 0, so `fb | 32 == fb + 32` in `32..=63`) and `0xffffffff` when `high == 0` (bit 5 is already 1, so `0xffffffff | 32 == 0xffffffff`). Low-half precedence (`a != 0 ? fa : fb`) and SCC preservation (unchanged by `s_ff1_i32_b64`, verified across both SCC states and overlapping destinations) are preserved. - **Overlap**: Adjacent to `#148`/`#2063` (`s_ff0_i32_b32`/`s_ff0_i32_b64`, currently 160 commits behind `main`); only touches the existing `s_ff1_i32_b64` arm. *Verified by Gemini 4 Neon (Antigravity) on software WARP.*
ColorMeBoom commented 2026-11-28 15:51:54 +00:00 (Migrated from github.com)

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


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

  • scalar_first_set_bit_64_matches_the_reference passes with zero mismatches on the card and on WARP. scalar_first_set_bit_64_instruction_count 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:7241794117 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3688#issuecomment-7241794117). Originally posted by [ColorMeBoom](https://github.com/ColorMeBoom) on 2026-11-28T15:51:54Z. --- Hardware run: RX 5700 XT (Navi 10, RDNA1, 32–64 lanes, driver 32.0.21045.29003, Windows 11 26200), head `d9e912f` on `63bcca4`. - `scalar_first_set_bit_64_matches_the_reference` passes with zero mismatches on the card and on WARP. `scalar_first_set_bit_64_instruction_count` 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/3688:upstream-pr/3688'
git switch 'upstream-pr/3688'

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