Skip to content

feat(shader): selective acyclic predication, f16 reductions, and uniform-VGPR analysis for wave64 on 32-lane hosts - #440

Merged
pragyaangaur merged 5 commits into
pragyaangaur:mainfrom
gdemontalivet:feat/macos-halves-predication
Nov 27, 2026
Merged

pragyaangaur merged 5 commits into
pragyaangaur:mainfrom
gdemontalivet:feat/macos-halves-predication

Conversation

@gdemontalivet

Copy link
Copy Markdown
Contributor

Summary

  • Adds selective acyclic divergent-branch predication (crates/victory-shader/src/emit/lower/predicate.rs) for wave64-on-32-lane (halves / one_wave_lds) compute shaders (bounded to <= 64 blocks per SESE diamond, strictly acyclic — divergent loops untouched), preserving group-uniform control flow across early-out bounds checks (if (tid >= N) return;) and acyclic if/else diamonds so exchange_halves and split_wave_barriers remain deadlock-free while masking inactive-lane side effects.
  • Adds Lower::reduce_half supporting 16-bit Shape::Half(Op::AddH) wave64 reductions across both 32-lane halves.
  • Upgrades first_lane_across(x, active) to work under arbitrary per-lane active masks (exact even when all 32 lanes of the lower half are inactive), teaches in_step() to propagate group-uniform VGPRs through v_readfirstlane_b32, and seeds lane_uniform() with wave64 geometry-uniform v1/v2 and global_load_*.
  • Keeps crates/victory-shader/src/emit/lower.rs at 511,933 B (+162 B net, 12.3 KiB below the 524,288 B cap).

Verification (Apple Silicon D3DMetal)

  • Live D3DMetal (victory-shader halves_tests::): 26/26 passed (0 lanes differ from the console on every test, including 4 new GPU readback tests covering early-out bounds checks, acyclic if/else diamonds, upper-half-only v_readfirstlane_b32, split-wave LDS, Shape::Half(Op::AddH), and buffer-loaded uniform loop trip counts).
  • cargo xtask policy, cargo test -p victory-shader (206 passed), and both gnullvm clippy builds: 0 warnings.

@gdemontalivet

Copy link
Copy Markdown
Contributor Author

Extended feat/macos-halves-predication with d8e487d5 + 8e7040a6 (crates/victory-shader/src/emit/lower.rs = 513,173 B, under the 524,288 B cap):

  • One-sided SkipGuard predication: rewrites only the branch exit so uniform loops/branches inside if (cond) { ... } keep their native CFG under in_step.
  • Lockstep predicated divergent loops (LockstepLoop + group_any_true): lowers wave64 scalarization loops into group-uniform lockstep loops reusing half_words (zero extra LDS bytes) and ensures WaveSize(32) is set whenever synced LDS shaders use lockstep loops.
  • Scalar mask_number fallback + transitive mask-source discovery + epilogue trampoline EarlyOut and image_load/image_sample/image_gather + v_mov tid-alias uniformity propagation.

Verification:

  • cargo xtask policy: passed (263 files ok)
  • cargo test -p victory-shader --lib: 206 passed; 0 failed
  • cargo clippy --target x86_64-pc-windows-gnullvm --workspace --all-targets -- -D warnings (default and --features diagnostics): clean
  • Live Wine/D3DMetal (tools/macos/test-wine.sh -p victory-shader --lib -- --test-threads=1 halves_tests::): 30 passed; 0 failed (0 lanes differ from the console on every test, including multi-wave lockstep scalarization loops)
  • Full archive pre-translation on Apple Silicon M5 Max (wave32 host): 25,985 containers translated with 0 panics, refused count reduced from 449 → 357 → 337 (-112 shaders rescued total), resolving remaining strand-hair compute refusals and blocky perimeter hair tiles.

@pragyaangaur

Copy link
Copy Markdown
Owner

Thanks for this. Many pull requests landed today, and this one now conflicts with main. Could you merge or rebase main into it, rerun the checks and ping here? I will review and merge it as soon as CI is green.

@pragyaangaur
pragyaangaur merged commit 509fcbf into pragyaangaur:main Nov 27, 2026
1 check passed
@hackgods

Copy link
Copy Markdown
Contributor

Two findings from merging #435's tests on top of this (M5 Pro, Wine + D3DMetal, synthetic tests only):

  1. A lane write lost in a predicated block. In one of feat(shader): retry refused 64-thread wave64 compute with whole-wave control flow #435's test programs, a v_writelane_b32 writes into a lane the predicated block leaves inactive. The console writes that lane whatever EXEC holds, but the predicated block's merge drops the write: lanes 0–19 and 40 read 0 instead of 55. A minimal reproduction is feat(shader): retry refused 64-thread wave64 compute with whole-wave control flow #435's halves_tests case with a v_writelane under divergent EXEC. Our local fix refuses such a write inside a predicated block (reason "a lane write in a predicated block"), so the whole-wave retry from feat(shader): retry refused 64-thread wave64 compute with whole-wave control flow #435 takes the program and matches the console again: hackgods@97f3b1b. A more targeted fix would keep v_writelane/v_writelane-like writes out of the predication or merge them unconditionally.
  2. 16-bit reduction order. The f16 wave sum under predication adds in its own order and gives 2049 (0x6801). The console's order rounds to 2048 (0x6800) twice. Only matters for exactness tests, but it means f16 reductions under predication aren't bit-exact.

Happy to turn (1) into a PR against main with a dedicated test if that's useful.

Prepared with Claude Code.

@cirddan

cirddan commented Nov 27, 2026

Copy link
Copy Markdown

A follow-up on the strand-hair shaders this made translate: in CS_VisHair (b6df66af105d8b2d) the lanes predicated off by an early exit still run the later pixel loop until LOOP_LIMIT, because the loop's exit test does not include the predicate. On an M5 it was ~0.29 s of GPU per world frame. #610 makes such loops exit for lanes that were not active at entry, only where nothing else in the loop depends on them (no barrier, wave/quad op or implicit-derivative sample).

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