Shaders: shorten long relative VGPR source selections #3630

Open
andrewpozdnakov7-jpg wants to merge 1 commit from upstream-pr/3630 into main
andrewpozdnakov7-jpg commented 2026-11-27 23:57:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T23:57:56Z.


The source read in v_movrels_b32 and v_movrelsd_b32 emits a compare-and-select chain. For eight or more candidates, pair neighboring sources and share one index-bit test per level. A final range check keeps the existing fallback. The short path still uses the linear selection.

On a generated compute shader selecting from 56 registers, DXC disassembly shows 57 to 8 integer comparisons, 182 to 139 value-producing instructions, and 3,344 to 2,912 container bytes. The 4-register DXIL is byte-identical. For a generated shader reading and writing through the same relative index, comparisons drop from 61 to 27.

With DXIL validation and app-local WARP, all 160 in-range dispatches matched main across seven source widths and the source-and-destination case. The included test reads every register from a nine-register selection, including the unpaired last one.

Coordination claim

AI-assisted with OpenAI Codex.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5534677508 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3630). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-27T23:57:56Z. --- The source read in v_movrels_b32 and v_movrelsd_b32 emits a compare-and-select chain. For eight or more candidates, pair neighboring sources and share one index-bit test per level. A final range check keeps the existing fallback. The short path still uses the linear selection. On a generated compute shader selecting from 56 registers, DXC disassembly shows 57 to 8 integer comparisons, 182 to 139 value-producing instructions, and 3,344 to 2,912 container bytes. The 4-register DXIL is byte-identical. For a generated shader reading and writing through the same relative index, comparisons drop from 61 to 27. With DXIL validation and app-local WARP, all 160 in-range dispatches matched main across seven source widths and the source-and-destination case. The included test reads every register from a nine-register selection, including the unpaired last one. [Coordination claim](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7230438530) AI-assisted with OpenAI Codex.
andrewpozdnakov7-jpg commented 2026-11-27 23:58:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T23:58:17Z.


On Windows I ran distinct binaries for main 63bcca4 and 7dfc01a with the same generated shaders, required DXIL validation and app-local WARP 1.0.22 / Agility 1.620.2. All 160 in-range dispatches matched, including odd-width lookups and the combined relative-source/relative-destination move. The new nine-register test passed on the PR head.

DXC disassembly for the 56-register test shows 57 to 8 integer comparisons and 3,344 to 2,912 bytes. The four-register test produced identical DXIL. These are emitted code counts from the generated examples.

The full local shader suite was 298 passed on main and 299 on the PR, with the same four failures on both (texture gather, both permute tests, and the edge-flagged triangle test). Strict shader Clippy with default and all features passed, and the asset policy check passed.

If you reuse the code, please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg [email protected] if folding it into another commit.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7230544454 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3630#issuecomment-7230544454). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-27T23:58:17Z. --- On Windows I ran distinct binaries for main 63bcca4 and 7dfc01a with the same generated shaders, required DXIL validation and app-local WARP 1.0.22 / Agility 1.620.2. All 160 in-range dispatches matched, including odd-width lookups and the combined relative-source/relative-destination move. The new nine-register test passed on the PR head. DXC disassembly for the 56-register test shows 57 to 8 integer comparisons and 3,344 to 2,912 bytes. The four-register test produced identical DXIL. These are emitted code counts from the generated examples. The full local shader suite was 298 passed on main and 299 on the PR, with the same four failures on both (texture gather, both permute tests, and the edge-flagged triangle test). Strict shader Clippy with default and all features passed, and the asset policy check passed. If you reuse the code, please retain the Git author on cherry-pick or rebase, or add Co-authored-by: andrewpozdnakov7-jpg <[email protected]> if folding it into another commit.
akimfomin commented 2026-11-28 00:28:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by akimfomin on 2026-11-28T00:28:24Z.


PortBridge compared this with KainanP’s independent M0 select tree in AnyXboxX #571. Both use shared tests of index bits. Here the lowering also guards the result with an unsigned range check. The AnyXboxX version stacks on its census and has a different receiving-tree conflict. An independent CPU check passed all in-range indexes for counts 1–256 and distinguished count-1 from count for sizes 7/8/9.

Your WARP runs already covered 160 in-range dispatches. If the hardware contract defines the first excluded index and high M0 bits, those are the useful additional controls. Otherwise keep the zero fallback assertion as a property of the translated code. This records the overlap without duplicating either implementation.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7230938853 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3630#issuecomment-7230938853). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-28T00:28:24Z. --- PortBridge compared this with KainanP’s independent M0 select tree in [AnyXboxX #571](https://github.com/LooperSalty/AnyXboxX/pull/571). Both use shared tests of index bits. Here the lowering also guards the result with an unsigned range check. The AnyXboxX version stacks on its census and has a different receiving-tree conflict. [An independent CPU check](https://github.com/akimfomin/PortBridge/blob/589c62c5db98b8d893885ab46cf97f56ac4b5ea0/audit/evidence/decision-followthrough/parallel-ops-2026-11-28/decision-checks.py) passed all in-range indexes for counts 1–256 and distinguished `count-1` from `count` for sizes 7/8/9. Your WARP runs already covered 160 in-range dispatches. If the hardware contract defines the first excluded index and high M0 bits, those are the useful additional controls. Otherwise keep the zero fallback assertion as a property of the translated code. This records the overlap without duplicating either implementation.
oni-swr commented 2026-11-28 00:31:25 +00:00 (Migrated from github.com)

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


Independent verification (AI-assisted: Claude Sonnet 5.5). Claim: #1633 (https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7230653883).

Revisions: base main 63bcca43c6a00a758510398fa43096ce7e4f7df0, PR head 7dfc01a60a93ee7a084f027d261a70f9ddb38e1d (one commit on top of base, so the trial merge is a fast-forward; diff touches only lower.rs and valu_tests.rs).

Environment: Windows 11 (26200), debug build, software adapter only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1; no hardware GPU used). App-local WARP d3d10warp.dll 1.0.22.0.20261027.1 loaded from the test exe directory, and dxil.dll 1.8.2502.11 (Windows SDK 10.0.26100) loaded, confirmed from the running process's module list. The DXIL validator runs on every generated shader in these tests (expect("DXIL validation failed")).

cargo test -p victory-shader --locked --lib --features host -j 4 (passed / failed / ignored):

  • base: 296 / 6 / 3
  • PR head: 297 / 6 / 3 (+1 is the new test)
  • The same 6 fail on both: hosts_that_do_not_fuse_are_named, 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, wave64_permutes_stay_within_each_32_lane_half, edge_flagged_triangles_draw_as_unflagged_ones. They are WARP-specific and unrelated to this change.
  • relative_source_selects_all_nine_registers passes on the PR head.

Control: the new nine-register test also passes on unmodified base, so this is a semantics-preserving lowering optimization, not a bug fix. Emitted DXIL size from a synthetic probe test (temporary, not committed; 64-VGPR file, relative-source read with 57 candidates, DXIL-validated on both): base 3360 bytes, PR 2920 bytes (-13%). The probe did not count comparisons, so the size is the only number I am claiming.

Other gates (PR head): cargo clippy -p victory-shader --all-targets --all-features -j 4 -- -D warnings clean. cargo xtask policy passed ("191 files ok"); cargo rejects -j on the xtask alias, so I set CARGO_BUILD_JOBS=4 for it. lower.rs is 522,162 bytes on the PR head (520,563 on base), under the 524,288-byte (512 KiB) policy limit and 2,126 bytes below it.

Diff review: no correctness issues found.

  • Candidates are collected the same way as before (sreg..=255, stop at the first unreadable VGPR).
  • Below 8 candidates the original compare-and-select chain is kept; at 8 or more a log2 select tree is used with one m0 & bit != 0 test per level.
  • The in-place pairing (sources[i] written after 2i and 2i+1 are read) is safe.
  • An odd tail stays unpaired. For an in-range index the tail's missing partner is never needed, because the index bit is 0 whenever the partner is absent.
  • Out-of-range m0 (>= count, including large values with high bits set) is caught by one unsigned m0 < count compare and yields 0, which matches the old fallback. Count 0 and count 1 take the linear path.
  • v_movrels_b32 and v_movrelsd_b32 share this source branch; v_movreld_b32 is untouched.
  • The new test is 9 candidates (odd width, above the threshold) with all 9 indices checked. The new test does not itself exercise out-of-range indices or the exact 7/8 boundary (I did not audit the other relative-move tests for these). A cheap addition would be one case with k = 9 or larger expecting 0. Non-blocking.

Aggregate/synthetic numbers only; no game files or retail data involved.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7230980976 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3630#issuecomment-7230980976). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-28T00:31:25Z. --- Independent verification (AI-assisted: Claude Sonnet 5.5). Claim: #1633 (https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7230653883). **Revisions:** base `main` `63bcca43c6a00a758510398fa43096ce7e4f7df0`, PR head `7dfc01a60a93ee7a084f027d261a70f9ddb38e1d` (one commit on top of base, so the trial merge is a fast-forward; diff touches only `lower.rs` and `valu_tests.rs`). **Environment:** Windows 11 (26200), debug build, software adapter only (`VICTORY_WARP=1`, `VICTORY_D3D12_WARP=1`; no hardware GPU used). App-local WARP `d3d10warp.dll` 1.0.22.0.20261027.1 loaded from the test exe directory, and `dxil.dll` 1.8.2502.11 (Windows SDK 10.0.26100) loaded, confirmed from the running process's module list. The DXIL validator runs on every generated shader in these tests (`expect("DXIL validation failed")`). **`cargo test -p victory-shader --locked --lib --features host -j 4` (passed / failed / ignored):** - base: 296 / 6 / 3 - PR head: 297 / 6 / 3 (+1 is the new test) - The same 6 fail on both: `hosts_that_do_not_fuse_are_named`, `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`, `wave64_permutes_stay_within_each_32_lane_half`, `edge_flagged_triangles_draw_as_unflagged_ones`. They are WARP-specific and unrelated to this change. - `relative_source_selects_all_nine_registers` passes on the PR head. **Control:** the new nine-register test also passes on unmodified base, so this is a semantics-preserving lowering optimization, not a bug fix. Emitted DXIL size from a synthetic probe test (temporary, not committed; 64-VGPR file, relative-source read with 57 candidates, DXIL-validated on both): base 3360 bytes, PR 2920 bytes (-13%). The probe did not count comparisons, so the size is the only number I am claiming. **Other gates (PR head):** `cargo clippy -p victory-shader --all-targets --all-features -j 4 -- -D warnings` clean. `cargo xtask policy` passed ("191 files ok"); cargo rejects `-j` on the xtask alias, so I set `CARGO_BUILD_JOBS=4` for it. `lower.rs` is 522,162 bytes on the PR head (520,563 on base), under the 524,288-byte (512 KiB) policy limit and 2,126 bytes below it. **Diff review:** no correctness issues found. - Candidates are collected the same way as before (`sreg..=255`, stop at the first unreadable VGPR). - Below 8 candidates the original compare-and-select chain is kept; at 8 or more a log2 select tree is used with one `m0 & bit != 0` test per level. - The in-place pairing (`sources[i]` written after `2i` and `2i+1` are read) is safe. - An odd tail stays unpaired. For an in-range index the tail's missing partner is never needed, because the index bit is 0 whenever the partner is absent. - Out-of-range `m0` (>= count, including large values with high bits set) is caught by one unsigned `m0 < count` compare and yields 0, which matches the old fallback. Count 0 and count 1 take the linear path. - `v_movrels_b32` and `v_movrelsd_b32` share this source branch; `v_movreld_b32` is untouched. - The new test is 9 candidates (odd width, above the threshold) with all 9 indices checked. The new test does not itself exercise out-of-range indices or the exact 7/8 boundary (I did not audit the other relative-move tests for these). A cheap addition would be one case with `k = 9` or larger expecting 0. Non-blocking. Aggregate/synthetic numbers only; no game files or retail data involved.
andrewpozdnakov7-jpg commented 2026-11-28 00:35:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T00:35:10Z.


I ran the suggested boundary controls on main 63bcca4 and 7dfc01a with required DXIL validation and app-local WARP. For 7, 8 and 9 candidates, both revisions read the first and last in-range registers and returned zero at the first excluded index and the high-bit controls. All 24 readbacks per revision matched, including 0x100 and 0x80000000.

This verifies preservation of the existing translation fallback across the linear-to-tree threshold. The native register semantics questions remain in #3602 and #1340. The committed nine-register test continues to check the valid selection tree.

Thanks for the independent size measurement and threshold review.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7231030168 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3630#issuecomment-7231030168). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-28T00:35:10Z. --- I ran the suggested boundary controls on main 63bcca4 and 7dfc01a with required DXIL validation and app-local WARP. For 7, 8 and 9 candidates, both revisions read the first and last in-range registers and returned zero at the first excluded index and the high-bit controls. All 24 readbacks per revision matched, including 0x100 and 0x80000000. This verifies preservation of the existing translation fallback across the linear-to-tree threshold. The native register semantics questions remain in #3602 and #1340. The committed nine-register test continues to check the valid selection tree. Thanks for the independent size measurement and threshold review.
futurepro62 commented 2026-11-29 20:18:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by futurepro62 on 2026-11-29T20:18:18Z.


NVIDIA full-suite run, to go with the reports above by oni-swr, since none of them names a GPU run. Tested exact head 7dfc01a (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 867 passed, 0 failed, 10 ignored.
  • The new tests ran and passed: relative_source_selects_all_nine_registers.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7261971775 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/3630#issuecomment-7261971775). Originally posted by [futurepro62](https://github.com/futurepro62) on 2026-11-29T20:18:18Z. --- NVIDIA full-suite run, to go with the reports above by oni-swr, since none of them names a GPU run. Tested exact head `7dfc01a` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **867 passed, 0 failed, 10 ignored**. - The new tests ran and passed: `relative_source_selects_all_nine_registers`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
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/3630:upstream-pr/3630'
git switch 'upstream-pr/3630'

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