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

Open
github-bot wants to merge 1 commit from github-bot/gh-561 into main AGit
Owner

@⁠AscilCH on GitHub · 2026-11-25 02:17 UTC · original
AscilCH:fix/shader-wave64-on-wave32-partial-group-base → main · +96 −5 · 2 files · 1 commits · open

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.)

**@⁠AscilCH** on GitHub · 2026-11-25 02:17 UTC · [original](https://github.com/fxceless/victory-fork/pull/561) `AscilCH:fix/shader-wave64-on-wave32-partial-group-base` → `main` · +96 −5 · 2 files · 1 commits · open ## 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.)*
- When downgrading a console wave64 compute shader to 32-lane host waves
  (`wave_base()`), a thread group whose size is not a multiple of 32 launches
  `group / 32` full 32-lane waves plus a partial tail wave of `group % 32`
  active lanes. Previously every wave unconditionally fetch-added 32 from the
  shared LDS counter, so whenever the partial wave ran ahead of a full wave,
  console threads inside the group were skipped and a full wave received
  indices >= `group` (wrapping `thread_id()` into duplicates and OOB `z`).
- For `group <= 32`, return base 0 directly without LDS allocation or barrier.
- For `group > 32 && !group.is_multiple_of(32)`, check whether all 32 lanes
  are active via `dx.op.waveActiveBallot(true)` (`mask == 0xFFFF_FFFF`): only
  full 32-lane waves increment the LDS counter, while the partial tail wave
  takes `(group / 32) * 32` directly.
- Add synthetic validation and GPU execution test for a 48-thread wave64
  compute shader lowered to 32-lane waves.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:46:23 +00:00
Author
Owner

@⁠avt0m4t48 on GitHub · 2026-11-25 02:51 UTC · original

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.

**@⁠avt0m4t48** on GitHub · 2026-11-25 02:51 UTC · [original](https://github.com/fxceless/victory-fork/pull/561#issuecomment-7151267958) 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.
Author
Owner

@⁠Dathinel on GitHub · 2026-11-25 19:21 UTC · original

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.

**@⁠Dathinel** on GitHub · 2026-11-25 19:21 UTC · [original](https://github.com/fxceless/victory-fork/pull/561#issuecomment-7172190602) 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.
Author
Owner

@⁠HAL9000RELOADED on GitHub · 2026-11-25 19:29 UTC · original

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 (#129).

**@⁠HAL9000RELOADED** on GitHub · 2026-11-25 19:29 UTC · [original](https://github.com/fxceless/victory-fork/pull/561#issuecomment-7172375200) 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://git.gtahub.st/github/fxceless--victory-fork/pulls/129).
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/wave64_tests.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/561/head:github-bot/gh-561'
git switch 'github-bot/gh-561'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-561'
git switch 'github-bot/gh-561'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-561'
git switch 'github-bot/gh-561'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-561'
git switch 'main'
git merge --squash 'github-bot/gh-561'
git switch 'main'
git merge --ff-only 'github-bot/gh-561'
git switch 'main'
git merge 'github-bot/gh-561'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.