perf(shader): per-barrier UAV fence elision, aligned 16-bit buffer fast path, and exact integer peepholes #4037

Closed
jarvixaiassistant-glitch wants to merge 1 commit from upstream-pr/4037 into main
jarvixaiassistant-glitch commented 2026-11-30 15:03:21 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by jarvixaiassistant-glitch on 2026-11-30T15:03:21Z.


Summary

Three self-contained, exact victory-shader compiler optimizations (zero floating-point behavior changes, zero runtime coupling, minimal +15/-9 lines in lower.rs):

  1. Per-barrier UAV memory-fence reachability (emit/barrier_uav.rs)

    • Previously, s_barrier emitted dx.op.barrier bit 2 (UAVMemoryFence / MEMBAR.GL device stall on NVIDIA) — and self.memory_fence() on half_meet — on every s_barrier whenever whole-shader writes_uavs was true.
    • Adds a forward CFG reachability fixed-point pass (barrier_needs_uav_fence) computing the exact (block, inst) s_barrier sites reachable from any UAV store/atomic (Image/Buffer/Smem store/atomic, global_* non-load, flat_* store/atomic) without crossing an intervening emitted s_barrier.
    • Pure-LDS reduction barriers before a final UAV store (or consecutive barriers with no intervening UAV write) emit 1 | 8 (GroupSync | TGSMFence) instead of 1 | 2 | 8, while preserving bit 2 across loops, branch joins, and suppressed no_barrier blocks.
  2. Single-dword fast path for 2-byte aligned 16-bit buffer loads/stores (lower.rs, bitcode/opt.rs)

    • Adds Module::maybe_set(v, depth) conservative known-bits analysis (preserving guaranteed trailing zero bits through Add, Sub, and Mul even across modular wrap-around, e.g. (idx << 1) + 4, (idx * 6) + 2).
    • When bits == 16 and maybe_set(byte, 6) & 1 == 0, within = byte & 3 is guaranteed 0 or 2 (never 3), so a 16-bit access cannot straddle a 4-byte boundary. load_subword() emits a single dword load (omitting the second load + select) and buffer_store_short omits the second guarded atomicBinOp pair.
  3. Exact bit-level unpack-of-pack & equality-compare peepholes (bitcode/opt.rs)

    • Unpack-of-pack folding via maybe_set: drops dead sides of (x | y) / (x ^ y) under And, LShr, Shl, and Trunc (e.g. ((x & 0xFFFF) | (y << 16)) & 0xFFFF -> x & 0xFFFF, >> 16 -> y & 0xFFFF, trunc(x | (y << 16), i16) -> trunc(x, i16)).
    • Exact Cmp(EQ|NE) folding over Xor/Sub/Add: (x ^ y) ==/!= 0 -> x ==/!= y, (x - y) ==/!= 0 -> x ==/!= y, (x ^ k1) ==/!= k2 -> x ==/!= (k1 ^ k2), (x + k1) ==/!= k2 -> x ==/!= (k2 - k1).

Verification (Windows 11, VICTORY_WARP=1)

  • cargo clippy -p victory-shader --lib --locked -- -D warnings: 0 warnings.
  • xtask policy --staged: asset policy: 3 files ok.
  • VICTORY_WARP=1 cargo test -p victory-shader --lib --locked -- --skip hw_tests: 528 passed, including all 4 new barrier_uav CFG unit tests, maybe_set_alignment_and_unpack_cmp_peepholes, cbv_byte_and_halfword_loads_read_the_addressed_bytes, and every_pass_leaves_what_random_programs_store randomized parity fuzzing. (The 10 hardware-only WaveSize(32) tests failed only because VICTORY_WARP=1 forces 4-lane WARP, which refuses WaveSize(32).)
  • Clean-room GPL-3.0-or-later, zero game data / zero RVAs.
  • AI-assisted: yes
<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:issue:5567830210 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4037). Originally posted by [jarvixaiassistant-glitch](https://github.com/jarvixaiassistant-glitch) on 2026-11-30T15:03:21Z. --- ## Summary Three self-contained, exact `victory-shader` compiler optimizations (zero floating-point behavior changes, zero runtime coupling, minimal `+15/-9` lines in `lower.rs`): 1. **Per-barrier UAV memory-fence reachability (`emit/barrier_uav.rs`)** - Previously, `s_barrier` emitted `dx.op.barrier` bit `2` (`UAVMemoryFence` / `MEMBAR.GL` device stall on NVIDIA) — and `self.memory_fence()` on `half_meet` — on *every* `s_barrier` whenever whole-shader `writes_uavs` was true. - Adds a forward CFG reachability fixed-point pass (`barrier_needs_uav_fence`) computing the exact `(block, inst)` `s_barrier` sites reachable from any UAV store/atomic (`Image`/`Buffer`/`Smem` store/atomic, `global_*` non-load, `flat_*` store/atomic) without crossing an intervening emitted `s_barrier`. - Pure-LDS reduction barriers before a final UAV store (or consecutive barriers with no intervening UAV write) emit `1 | 8` (`GroupSync | TGSMFence`) instead of `1 | 2 | 8`, while preserving bit `2` across loops, branch joins, and suppressed `no_barrier` blocks. 2. **Single-dword fast path for 2-byte aligned 16-bit buffer loads/stores (`lower.rs`, `bitcode/opt.rs`)** - Adds `Module::maybe_set(v, depth)` conservative known-bits analysis (preserving guaranteed trailing zero bits through `Add`, `Sub`, and `Mul` even across modular wrap-around, e.g. `(idx << 1) + 4`, `(idx * 6) + 2`). - When `bits == 16` and `maybe_set(byte, 6) & 1 == 0`, `within = byte & 3` is guaranteed `0` or `2` (never `3`), so a 16-bit access cannot straddle a 4-byte boundary. `load_subword()` emits a single dword load (omitting the second load + select) and `buffer_store_short` omits the second guarded `atomicBinOp` pair. 3. **Exact bit-level unpack-of-pack & equality-compare peepholes (`bitcode/opt.rs`)** - Unpack-of-pack folding via `maybe_set`: drops dead sides of `(x | y)` / `(x ^ y)` under `And`, `LShr`, `Shl`, and `Trunc` (e.g. `((x & 0xFFFF) | (y << 16)) & 0xFFFF -> x & 0xFFFF`, `>> 16 -> y & 0xFFFF`, `trunc(x | (y << 16), i16) -> trunc(x, i16)`). - Exact `Cmp(EQ|NE)` folding over `Xor`/`Sub`/`Add`: `(x ^ y) ==/!= 0 -> x ==/!= y`, `(x - y) ==/!= 0 -> x ==/!= y`, `(x ^ k1) ==/!= k2 -> x ==/!= (k1 ^ k2)`, `(x + k1) ==/!= k2 -> x ==/!= (k2 - k1)`. ## Verification (Windows 11, `VICTORY_WARP=1`) - `cargo clippy -p victory-shader --lib --locked -- -D warnings`: **0 warnings**. - `xtask policy --staged`: **`asset policy: 3 files ok`**. - `VICTORY_WARP=1 cargo test -p victory-shader --lib --locked -- --skip hw_tests`: **528 passed**, including all 4 new `barrier_uav` CFG unit tests, `maybe_set_alignment_and_unpack_cmp_peepholes`, `cbv_byte_and_halfword_loads_read_the_addressed_bytes`, and `every_pass_leaves_what_random_programs_store` randomized parity fuzzing. *(The 10 hardware-only `WaveSize(32)` tests failed only because `VICTORY_WARP=1` forces 4-lane WARP, which refuses `WaveSize(32)`.)* - Clean-room `GPL-3.0-or-later`, zero game data / zero RVAs. - `AI-assisted: yes`
trustno1-coder commented 2026-12-01 18:46:58 +00:00 (Migrated from github.com)

Imported from trustno1-coder/VIctory-NVIDIA. Originally posted by trustno1-coder on 2026-12-01T18:46:58Z.


Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

<!-- greyfinch-sync:trustno1-coder/VIctory-NVIDIA:comment:7321534644 --> Imported from [trustno1-coder/VIctory-NVIDIA](https://github.com/trustno1-coder/VIctory-NVIDIA/pull/4037#issuecomment-7321534644). Originally posted by [trustno1-coder](https://github.com/trustno1-coder) on 2026-12-01T18:46:58Z. --- Closing this as part of moving main to the texture and video-memory rework (v2026.12.01.01-test). A lot of the code changed, and this pull request no longer applies cleanly to it; it would need to be rewritten against the new main rather than rebased. Thank you for the work. If the change is still needed, please open a new pull request based on the current main.

Pull request closed

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
trustno1-coder/VIctory-NVIDIA!4037
No description provided.