Shaders/lower: pin partial tail wave in wave64-on-wave32 wave_base() #561

Open
AscilCH wants to merge 1 commit from upstream-pr/561 into main
AscilCH commented 2026-11-25 02:17:10 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by AscilCH on 2026-11-25T02:17:10Z.


Problem

In crates/victory-shader/src/emit/lower.rs, wave_base() assigns console thread bases to 32-lane host waves running a wave64 compute shader by having every wave's lane 0 unconditionally execute atomicrmw add 32 on a shared LDS counter.

When the thread group size is not a multiple of 32 (e.g., [48, 1, 1], [6, 6, 1] = 36, [10, 10, 1] = 100), the hardware launches group / 32 full 32-lane waves plus one partial tail wave of group % 32 active lanes. Whenever the partial wave reaches atomicrmw add 32 before the last full wave:

  1. The partial wave claims a mid-group 32-thread range, leaving 32 - (group % 32) console threads completely unexecuted.
  2. A full wave receives base = (group / 32) * 32, pushing its upper lanes to console_thread >= group, so thread_id() wraps x = f % x into duplicate threads and computes out-of-bounds z.

Fix

  • In wave_base() (crates/victory-shader/src/emit/lower.rs):
    • For group <= 32, return 0 directly (only one host wave exists).
    • For group > 32 with !group.is_multiple_of(32), test whether all 32 lanes are active using dx.op.waveActiveBallot(true) (extract 0 == u32::MAX). Only full 32-lane waves add 32 to the LDS counter; the partial tail wave adds 0 and selects (group / 32) * 32 directly.
  • Add synthetic unit test partial_group_wave64_on_wave32_assigns_every_thread_once in emit/wave64_tests.rs (DXIL validation + hardware execution when running on a 32-lane host).

Verification

  • cargo test -p victory-shader --locked emit::wave64_tests: 6 passed, 0 failed
  • cargo test -p victory-shader --locked emit::lower::tests: 13 passed, 0 failed (1 pre-existing ignored)
  • cargo xtask policy: 169 files ok
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean
  • cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings: clean

(AI-assisted; synthetic unit tests only, no game data.)

<!-- greyfinch-sync:fxceless/victory-fork:issue:6595630846 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/561). Originally posted by [AscilCH](https://github.com/AscilCH) on 2026-11-25T02:17:10Z. --- ## Problem In `crates/victory-shader/src/emit/lower.rs`, `wave_base()` assigns console thread bases to 32-lane host waves running a wave64 compute shader by having every wave's lane 0 unconditionally execute `atomicrmw add 32` on a shared LDS counter. When the thread group size is not a multiple of 32 (e.g., `[48, 1, 1]`, `[6, 6, 1]` = 36, `[10, 10, 1]` = 100), the hardware launches `group / 32` full 32-lane waves plus one partial tail wave of `group % 32` active lanes. Whenever the partial wave reaches `atomicrmw add 32` before the last full wave: 1. The partial wave claims a mid-group 32-thread range, leaving `32 - (group % 32)` console threads completely unexecuted. 2. A full wave receives `base = (group / 32) * 32`, pushing its upper lanes to `console_thread >= group`, so `thread_id()` wraps `x = f % x` into duplicate threads and computes out-of-bounds `z`. ## Fix - In `wave_base()` (`crates/victory-shader/src/emit/lower.rs`): - For `group <= 32`, return `0` directly (only one host wave exists). - For `group > 32` with `!group.is_multiple_of(32)`, test whether all 32 lanes are active using `dx.op.waveActiveBallot(true)` (`extract 0 == u32::MAX`). Only full 32-lane waves add `32` to the LDS counter; the partial tail wave adds `0` and selects `(group / 32) * 32` directly. - Add synthetic unit test `partial_group_wave64_on_wave32_assigns_every_thread_once` in `emit/wave64_tests.rs` (DXIL validation + hardware execution when running on a 32-lane host). ## Verification - `cargo test -p victory-shader --locked emit::wave64_tests`: 6 passed, 0 failed - `cargo test -p victory-shader --locked emit::lower::tests`: 13 passed, 0 failed (1 pre-existing ignored) - `cargo xtask policy`: 169 files ok - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`: clean - `cargo clippy -p victory-shader --all-targets --all-features --locked -- -D warnings`: clean *(AI-assisted; synthetic unit tests only, no game data.)*
avt0m4t48 commented 2026-11-25 03:06:36 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by avt0m4t48 on 2026-11-25T02:51:03Z.


I independently checked the emitted DXIL at 745305ce7bda7a59c38e2b2c3432280ab8271108 against c96b4f2b8365bf43d54ab074940923ef8267f4c6 with an original synthetic compute shader that feeds a quad swizzle into an indexed UAV store.

For emulated wave64 groups of 36 (6×6), 48, and 100 (10×10), the base gives every host wave a 32-thread slot. The PR emits a full-wave ballot check on the counter increment and pins the partial wave to 32, 32, and 96 respectively. Modeling the emitted counter logic across the distinct orders in which the tail can arrive gave complete thread-index coverage in 2/2, 2/2, and 4/4 orders on the PR, versus one order per group on the base. The 16- and 32-thread cases emit neither the LDS counter nor its barrier on the PR. The 64-thread emulated control and the native wave32 and wave64 controls emitted byte-identical binaries across both revisions.

Synthetic shader words (with the same raw UAV descriptor in s[8:11] across cases): BE850380 F4080202 FA000000 D8D4801B 03000000 34080082 E0701000 80020304 BF810000.

The unmodified PR head passed the Linux shader suite (171 passed, 2 ignored) and cargo check -p victory-shader --locked --tests --target x86_64-pc-windows-msvc. This check covers translation and the counter model. Windows execution, Microsoft DXIL validation, and GPU readback are still unverified here.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7151267958 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/561#issuecomment-7151267958). Originally posted by [avt0m4t48](https://github.com/avt0m4t48) on 2026-11-25T02:51:03Z. --- I independently checked the emitted DXIL at `745305ce7bda7a59c38e2b2c3432280ab8271108` against `c96b4f2b8365bf43d54ab074940923ef8267f4c6` with an original synthetic compute shader that feeds a quad swizzle into an indexed UAV store. For emulated wave64 groups of 36 (6×6), 48, and 100 (10×10), the base gives every host wave a 32-thread slot. The PR emits a full-wave ballot check on the counter increment and pins the partial wave to 32, 32, and 96 respectively. Modeling the emitted counter logic across the distinct orders in which the tail can arrive gave complete thread-index coverage in 2/2, 2/2, and 4/4 orders on the PR, versus one order per group on the base. The 16- and 32-thread cases emit neither the LDS counter nor its barrier on the PR. The 64-thread emulated control and the native wave32 and wave64 controls emitted byte-identical binaries across both revisions. Synthetic shader words (with the same raw UAV descriptor in `s[8:11]` across cases): `BE850380 F4080202 FA000000 D8D4801B 03000000 34080082 E0701000 80020304 BF810000`. The unmodified PR head passed the Linux shader suite (171 passed, 2 ignored) and `cargo check -p victory-shader --locked --tests --target x86_64-pc-windows-msvc`. This check covers translation and the counter model. Windows execution, Microsoft DXIL validation, and GPU readback are still unverified here.
Dathinel commented 2026-11-25 20:09:25 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by Dathinel on 2026-11-25T19:21:48Z.


Hardware readback on an RTX 3050 Laptop (4 GB, driver 32.0.16.1062), with the patch from 745305c applied to d510ee4. The PR's partial_group_wave64_on_wave32_assigns_every_thread_once test passes.

I also tested a separate synthetic wave64 program: each thread initializes its output register to 0xffffffff, executes v_mov_b32_dpp row_shr:1 with row mask zero, and stores the register at its thread index in a zero-initialized UAV. Every slot should contain the sentinel. On the base, the 48-thread dispatch left 16 incorrect slots (16–31). With this PR all 48 slots held the sentinel. Group sizes 33, 63 and 80 also passed that control with the patch.

Partial row write masks are a separate issue. With 64 threads and row mask 3, the upper half still writes in this build. The zero-mask control above isolates thread coverage from that row-selection issue.

AI-assisted test; synthetic data only.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7172190602 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/561#issuecomment-7172190602). Originally posted by [Dathinel](https://github.com/Dathinel) on 2026-11-25T19:21:48Z. --- Hardware readback on an RTX 3050 Laptop (4 GB, driver 32.0.16.1062), with the patch from `745305c` applied to `d510ee4`. The PR's `partial_group_wave64_on_wave32_assigns_every_thread_once` test passes. I also tested a separate synthetic wave64 program: each thread initializes its output register to `0xffffffff`, executes `v_mov_b32_dpp row_shr:1` with row mask zero, and stores the register at its thread index in a zero-initialized UAV. Every slot should contain the sentinel. On the base, the 48-thread dispatch left 16 incorrect slots (16–31). With this PR all 48 slots held the sentinel. Group sizes 33, 63 and 80 also passed that control with the patch. Partial *row write masks* are a separate issue. With 64 threads and row mask 3, the upper half still writes in this build. The zero-mask control above isolates thread coverage from that row-selection issue. AI-assisted test; synthetic data only.
HAL9000RELOADED commented 2026-11-25 20:09:27 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by HAL9000RELOADED on 2026-11-25T19:29:42Z.


RX 7900 XTX (RDNA3), Windows 10 + #402 for SM 6.6, main d510ee4: the new test partial_group_wave64_on_wave32_assigns_every_thread_once passes on the card with this PR, and fails with the test alone on the base. No other test changed. Run table: #129 (https://github.com/fxceless/victory-fork/pull/129#issuecomment-7172358511).

<!-- greyfinch-sync:fxceless/victory-fork:comment:7172375200 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/561#issuecomment-7172375200). Originally posted by [HAL9000RELOADED](https://github.com/HAL9000RELOADED) on 2026-11-25T19:29:42Z. --- RX 7900 XTX (RDNA3), Windows 10 + #402 for SM 6.6, main `d510ee4`: the new test `partial_group_wave64_on_wave32_assigns_every_thread_once` passes on the card with this PR, and fails with the test alone on the base. No other test changed. Run table: #129 (https://github.com/fxceless/victory-fork/pull/129#issuecomment-7172358511).
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/561:upstream-pr/561'
git switch 'upstream-pr/561'

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