Skip to content

xshader: lower constant-lane v_readlane_b32 for Wave32 and portable Wave64 - #185

Open
Vladimir-Human wants to merge 4 commits into
brcly:mainfrom
Vladimir-Human:xshader/readlane-wave-reduction
Open

Vladimir-Human wants to merge 4 commits into
brcly:mainfrom
Vladimir-Human:xshader/readlane-wave-reduction

Conversation

@Vladimir-Human

Copy link
Copy Markdown
Contributor

AI-assisted PR.

Summary

Lowers constant-lane v_readlane_b32 wave reductions in tools/shader-compiler:

  • Emits direct scalarized WaveReadLaneAt when the wave execution context and SGPR/SCC/Exec operands are proven uniform across a dataflow + control-dependence fixpoint, falling back cleanly to VGPR-indexed tb_swizzle LDS exchange otherwise.
  • Enforces Wave32/Wave64 mask width checks, s_bitset*/s_cmov*/Sopk/Smem uniformity rules, ExecLo/ExecHi conflict checks, v_mbcnt [WaveSize(32)] WaveActiveBallot handling, guarded partial-group OOB loads between unconditional barriers, and tb_guard loop checks rejecting preceding/nested non-uniform loops.
  • All modified Rust files remain <= 1,200 lines with zero inline #[cfg(test)] modules (exec_group.rs untouched).

Verification (commit 4ecbc63c)

  • Python hygiene & shader checks (test_repository_hygiene.py, test_translator_checks.py, test_wave_extrema.py, test_dpp8_exchange.py): 13 passed, 0 failed
  • cargo test --manifest-path tools/shader-compiler/Cargo.toml: 318 passed, 0 failed, 0 ignored (316 lib + 2 integration)
  • DXC (cs_6_6/cs_6_0) + hardware D3D12 readback (RTX 4070 SUPER, 10de:2783, readlane_probe + compute.cpp): 15/15 cases passed, 5,760/5,760 words exact
  • git merge-tree --write-tree clean against origin/main and PR #176.

…ave64

Lower non-spill constant-lane and upper-bound-proven SGPR v_readlane_b32
instructions in compute shaders with exact RDNA2 EXEC-ignoring semantics:

- Separate SGPR spill VGPRs (written by v_writelane_b32) from cross-lane
  VGPR broadcasts so shaders with both spills and constant readlanes compile.
- On 1D Wave32 groups where forward CFG analysis proves all lanes active in
  EXEC and uniform control flow/selector, emit zero-overhead WaveReadLaneAt
  with [WaveSize(32)].
- When EXEC may have disabled the target lane, on 2D/3D or partial groups,
  or for portable Wave64 running on Wave32 hosts, stage through groupshared
  tb_swizzle hoisted outside skip guards using the guest wave stride (32 or 64)
  without requiring [WaveSize(64)].
- Verify structured CFG and loop uniformity before allowing barrier/wave
  exchanges, and explicitly refuse out-of-range lanes, unproven dynamic SGPR
  selectors, non-uniform loops, interleaved loops, and early s_endpgm.
- F1: Reject non-uniform forward branches skipping an entire enclosing
  barrier loop (from < l && to > l), and reject barrier loops following
  non-uniform loops that could desync the function-wide tb_guard counter.
- F2: Couple analyze_uniformity with control dependence so any SGPR/SCC
  written inside a non-uniform branch or loop span is tainted to fixpoint.
- F3: Check dst reads on SOP1 RMW ops (s_bitset0/1, s_cmov), invert SCC
  tracking to explicit preserve/write allowlists (defaulting unknown ops
  to tainted SCC), and restrict uniform SMEM to loads with NULL/SGPR soffset.
- F4: Match all 32/64-bit scalar ops touching EXEC in wave-width marker
  detection and enforce conflict/width checks on count <= 32 groups too.
- F5: Restrict the indexed_lane tb_lane v_mbcnt fast path to -1 masks,
  preserving WaveActiveBallot for Wave32 EXEC masks (with WaveSize(32))
  and refusing unmodeled lane-mask v_mbcnt under indexed_lane.
- F6: Guard hoisted emit_indexed_read loads on active skip flags while
  keeping both barriers unconditional so skipped lanes never read OOB on
  partial groups.
- Minor: Reject incomplete or CLAMP/OMOD-flagged VOP3 v_readlane_b32.
@brcly

brcly commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Thanks for this. I trial-merged it onto main (4cb6a4a) and ran the full check, which passed: 348 Python tests, 313 shader translator tests, 65 guest checks, the D3D12 regression controls and the translator GPU checks.

Before merging, I'd like to understand a tail latency change in the Lanes benchmark. I ran python tuxbox.py verify --only lanes-benchmark 15 times on each build.

Reading (µs) main median (min–max) With this PR, median (min–max)
submit 208.6 (202.4–215.1) 208.9 (204.6–211.4)
roundtrip 236.4 (229.5–253.3) 233.6 (227.6–246.1)
copy64k 386.1 (384.2–398.8) 385.9 (383.5–395.6)
frame 230.4 (223.6–236.4) 227.7 (222.1–234.0)
frame p99 363.0 (298.9–379.7) 366.8 (298.1–966.1)
shader pipeline 193.1 (170.8–208.9) 190.3 (173.6–199.2)
DXIL pipeline 130.1 (124.5–140.2) 126.8 (120.3–129.8)

The medians match, but frame p99 went over 450 µs in 5 of 15 runs with this PR and 0 of 15 on main:

  • main: 298, 325, 329, 335, 342, 351, 357, 363, 364, 367, 371, 374, 375, 378, 379
  • This PR: 298, 317, 322, 340, 353, 353, 365, 366, 371, 373, 489, 494, 568, 787, 966

The full check's own benchmark run on the merged tree also read 725.6 µs. The runs for each build were done in blocks (5, then 10), not strictly alternated. I haven't found the cause. The new wake-up path for waiting queues is the first place I'd look, but that's a guess.

Could you check whether you see the same spread over repeated runs, and either fix it or explain it?

On the elision logic: from reading it, only monotone waits at list start are skipped, and only with no tracked or in-flight write to the address. I found draws, dispatches, copies, markers, queries and bundles all recording writes. One case I don't see covered is the CPU lowering a label between submission and GPU execution. Is that something you considered?

I didn't test this in a retail title.

Host: NVIDIA GeForce RTX 4070 Ti SUPER (10de:2705), driver 620.73.04, Proton Experimental 11.0-20261027b, Linux 7.3.5, AMD Ryzen 7 9800X3D.

@brcly

brcly commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

This ended up being noise, ignore me. will merge this and others soon. thank you.

clquwu added a commit to clquwu/leonida-ultimate that referenced this pull request Nov 25, 2026
…rrier fence, W14-07 CAS stores, W14-08 wave64 readlane

Implements Wave 14 Lane 14C shader stability fixes:
- W14-01 (--store-page-guard, adapted from ethan00013/keylime@650550ffa, GPL-3.0): emit lazy %store_addr_valid(heap_base, va) checking the 64 KiB page-validity bitmap at heap_base+0x170000 and GPBM control header at heap_base+0x180000 (runtime/src/gpu_page_bitmap.h) on PSB stores/atomics; default off, zero bloat on read-only shaders.
- W14-05 (adapted from ethan00013/keylime@0afb8154c5, GPL-3.0): per-function tripped flag in add_loop_guards so hitting a nested inner loop cap breaks enclosing loops instead of multiplying limit*limit.
- W14-06 (adapted from Marvisak/VIctory#1799 & five-star-recomp@320612a4d7, GPL-3.0): downgrade s_barrier inside switch/dispatch form, divergent guards, or non-top-level mesh blocks to OpMemoryBarrier while preserving uniform top-level CS OpControlBarrier.
- W14-07 (adapted from Marvisak/VIctory#2208, GPL-3.0): replace OpAtomicAnd+OpAtomicOr in global_sub and buffer_sub with OpAtomicCompareExchange CAS loops.
- W14-08 (adapted from fxceless/victory-fork#354 GPL-3.0 & brcly/TuxBox#185 Apache-2.0): tighten uniform_vgprs/wave64_halves implicit-read tracking (DPP mods, vlist, block-0 back-edges, v_fmac_f64, v_dot*c_u32_*, v_cndmask_b16) and constant-lane/uniform v_readlane_b32 lowering.
- Add synthetic semantic & spirv-val regression suite tests/semantic/test_w14_shader_stability.py (zero game data).
@tsunderejay

Copy link
Copy Markdown

Automated review against current main found blocking correctness risks before merge: (1) wave width is inferred from b32/b64 EXEC operand width, which is not conclusive and can silently select Wave32 for a Wave64 program; (2) the unproven-selector fallback can use a hardcoded 64-lane base without bounds checks and can read stale values for branch-skipped lanes; (3) new readlane refusals may be swallowed by the lone-lane retry; (4) scalar memory loads are treated as workgroup-uniform for loop-convergence proofs, which can introduce barrier deadlocks. Please address these with regressions before merging.

- Wave width inference: remove inconclusive 32-bit EXEC_LO heuristic from
  has_wave32_markers, treat any ExecHi operand as a Wave64 marker, and refuse
  ambiguous 32-bit EXEC_LO operations lacking conclusive Wave32/Wave64 proof.
- Unproven-selector fallback: gate swizzle_group_reads / GROUP_READ strictly
  on complete 64-lane groups (count % 64 == 0, guest_width == 64, structured
  compute swizzle), publish every lane's actual VGPR unconditionally before
  the barrier in emit_compute_swizzle(GROUP_READ), guard only the load on
  active skip flags, and mask the selector with & 63u.
- Lone-lane retry: propagate cross-lane v_readlane_b32 refusals directly in
  translate_outputs instead of falling back to lone_lane::retry.
- SMEM uniformity: treat Enc::Smem scalar loads conservatively as non-uniform
  in analyze_uniformity_pass so mutable/coherent scalar loads cannot feed
  barrier loops enclosing v_readlane_b32.
@Vladimir-Human

Copy link
Copy Markdown
Contributor Author

@tsunderejay Thank you for the detailed review — pushed monotonic fix commit 543999bd on top of 4ecbc63c addressing all 4 blocking correctness findings with regression tests:

  1. Wave width inference (indexed_lane.rs):

    • Removed the inconclusive _b32/_u32/_i32 touches_exec clause from has_wave32_markers so a Wave64 shader inspecting exec_lo with s_mov_b32/s_and_b32 can no longer be misclassified as Wave32.
    • Treated any Opnd::ExecHi reference as a Wave64 marker, and explicitly refused ambiguous 32-bit exec_lo instructions lacking conclusive Wave32 or Wave64 proof (wave64_exec_lo_b32_inspect_keeps_wave64_and_ambiguous_b32_exec_refuses).
  2. Unproven-selector fallback & GROUP_READ (pipeline.rs, indexed_lane.rs, exchange.rs):

    • Restricted swizzle_group_reads, can_swizzle_fallback, group_reads, and emit_compute_swizzle(GROUP_READ) strictly to proven-safe compute_swizzle shapes (count % 64 == 0, 1..=1024, guest_width == 64, scan_width.is_none(), !dispatched).
    • Updated emit_compute_swizzle(GROUP_READ) to publish tb_swizzle[tb_lane] = <src0>; unconditionally before the first GroupMemoryBarrierWithGroupSync(), guard only the read/result assignment on active branches, and mask the selector with & 63u (unproven_swizzle_fallback_refuses_partial_groups_and_preserves_skipped_lanes).
  3. lone_lane::retry swallowing readlane refusals (pipeline.rs, indexed_lane.rs):

    • Short-circuited translate_outputs to return first directly whenever indexed_lane::has_non_spill_readlane(&original) is true so cross-lane v_readlane_b32 errors propagate cleanly instead of falling through to per-lane retry (refused_readlane_is_not_swallowed_by_lone_lane_retry).
  4. Enc::Smem uniformity (indexed_lane.rs):

    • Updated analyze_uniformity_pass so Enc::Smem (s_load_*, s_buffer_load_*) conservatively taints written SGPRs as non-uniform, preventing scalar memory load loops from proving convergence around barriers (smem_loads_are_not_treated_as_workgroup_uniform_for_readlane_loops).

Verification (543999bd6fafa17f)

  • Clean git merge-tree against origin/main (cb8c38b) and PR #201 (079143b1).
  • python -m unittest discover -s tests -p "test_repository_hygiene.py" -v: 4/4 passed (max .rs = 1,106 lines; indexed_lane.rs = 1,031; tests/hlsl/indexed_lane.rs = 930).
  • cargo test --manifest-path tools/shader-compiler/Cargo.toml: 320 lib + 2 integration passed; 0 failed.
  • readlane_probe (15/15) + DXC 10.0.26100.0 cs_6_6 compile: 15/15 passed.
  • cargo test --manifest-path runtime/d3d12/Cargo.toml -- --test-threads=1: 111/111 passed.
  • python build.py lanes + lanes.exe --adapter 10de:2783 --selftest all: passed (12/12).

@brcly

brcly commented Nov 26, 2026

Copy link
Copy Markdown
Owner

The 128 lost shaders are mostly from the early return in translate_outputs for has_non_spill_readlane: disabling it leaves 4 lost and 26 changed, with none gained.

Please suppress the fallbacks only when the lane read itself is what failed, and fall back for the 4 that lack a lane bound.

On recorded requests the PR doesn’t translate any of the 278 shaders refused for lane reads, so could you show a case it fixes, ideally a GPU check where main computes the wrong result?

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.

3 participants