Skip to content

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

Open
AscilCH wants to merge 1 commit into
fxceless:mainfrom
AscilCH:fix/shader-wave64-on-wave32-partial-group-base
Open

AscilCH wants to merge 1 commit into
fxceless:mainfrom
AscilCH:fix/shader-wave64-on-wave32-partial-group-base

Conversation

@AscilCH

@AscilCH AscilCH commented Nov 25, 2026

Copy link
Copy Markdown

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.
@avt0m4t48

Copy link
Copy Markdown

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

Copy link
Copy Markdown
Contributor

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

Copy link
Copy Markdown

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants