Skip to content

Recompiler: bounded split-wave64 spin default and early-exit-safe s_barrier - #798

Merged
theaustinayers merged 2 commits into
xcl-dev:mainfrom
gsucristo:recomp/safe-s-barrier-and-x64-spin
Nov 24, 2026
Merged

theaustinayers merged 2 commits into
xcl-dev:mainfrom
gsucristo:recomp/safe-s-barrier-and-x64-spin

Conversation

@gsucristo

Copy link
Copy Markdown
Contributor

Prevent GPU SM deadlocks in translated Compute and Mesh shaders on 32-lane GPUs (and under early wave exits) by defaulting split-wave64 exchanges to bounded arrival counters (XCL_X_SPIN 1) and emitting an early-exit-safe, bounded LDS arrival barrier (xcl_s_barrier) for multi-warp s_barrier.

On RDNA2, s_endpgm decrements the active workgroup wave count so surviving waves reaching s_barrier do not wait for already-exited waves. On PC D3D12/NVIDIA, GroupMemoryBarrierWithGroupSync() compiles to bar.sync 0, which hangs the SM permanently if any hardware warp exits early (s_endpgm), exhausts xcl_budget (LOOP_GUARD), or diverges in split-wave64 mode.

What is in it

  • Commit 1 (crates/xcl/src/gpu/recomp/emit.rs, tests.rs): default x64_spin() to true (XCL_X_SPIN 1, opt-out via XCL_WAVE64_SPIN=0) and update gpu_split_wave64_waves_diverge.
  • Commit 2 (crates/xcl/src/gpu/recomp/emit.rs, tests.rs): pre-scan for s_barrier in multi-warp Compute/Mesh shaders (compute_or_mesh_hw_waves() > 1) and emit xcl_s_barrier_init / xcl_s_barrier / xcl_s_barrier_exit backed by XCL_BAR_ALIVE + packed (gen: 20, arrived: 12) XCL_BAR_STATE bounded at 1 << 20 iterations (opt-out via XCL_SAFE_BARRIER=0); emit GroupMemoryBarrier() for single-warp Compute/Mesh s_barrier; add gpu_safe_s_barrier_multi_warp_and_early_endpgm testing 4-warp 2-gen LDS exchange (wave64 and wave32) and upper-wave early s_endpgm before two s_barriers.

Tested

  • NVIDIA GeForce RTX 4090 (driver 610.57.04), AMD Ryzen 9 7950X3D, Linux + Proton Experimental / vkd3d-proton (relaxed vtable check), rustc 1.98.1 (x86_64-pc-windows-msvc cross-compile), debug.
  • cargo test --workspace --no-fail-fast --target x86_64-pc-windows-msvc -- --test-threads=4 --skip a_freed_block_is_decommitted_before_its_address_is_reused: 700 passed, 4 failed (pre-existing Wine-only failures: hle::xaudio2::tests::xma2_source_voice_decodes_and_manages_lifetime, test_title_runs_and_game_folder_is_untouched, disk_free_space_ex_reports_pls_quota, set_predication_buffer_state_and_kickoff), 41 ignored, 1 filtered (a_freed_block_is_decommitted_before_its_address_is_reused).
  • SKIPPED GPU tests: gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware (no wave64).
  • New/updated GPU tests passed on RTX 4090: gpu_split_wave64_waves_diverge, gpu_safe_s_barrier_multi_warp_and_early_endpgm.

Regression run

  • Pending native Windows scripts/smoke.ps1 runs on a retail title (Linux/Proton environment only).

Not tested

  • Native Windows 10/11, AMD RDNA (native wave64 hardware), Mesh shader xcl_s_barrier dispatch on hardware (Compute path tested), scripts/smoke.ps1 retail regression run.

AI use

  • Antigravity (Google DeepMind; exact model version not exposed to the agent) — assisted with code, tests, and PR description; reviewed, built, and run on hardware by the author.

In split-wave64 mode (two hardware wave32s per console wave64), 1-wave64
threadgroups previously defaulted to `XCL_X_SPIN 0` (`GroupMemoryBarrierWithGroupSync()`),
which can permanently hang a GPU SM if the two hardware wave32 halves ever
diverge. Default `x64_spin()` to `true` (bounded arrival-counter exchange)
and allow opting back to group barriers with `XCL_WAVE64_SPIN=0`.
On RDNA2, `s_endpgm` removes the exiting wave from the workgroup's active
barrier count so surviving waves executing `s_barrier` do not wait for it.
On PC D3D12/NVIDIA, `GroupMemoryBarrierWithGroupSync()` compiles to
`bar.sync 0`, which hangs the SM if any hardware warp exits early via
`s_endpgm`, exhausts `xcl_budget`, or diverges after a split-wave64 desync.

For multi-warp Compute and Mesh shaders containing `s_barrier`, emit an
early-exit-safe, bounded LDS arrival barrier (`xcl_s_barrier`) tracking live
warps in `XCL_BAR_ALIVE` and `(generation, arrived)` in `XCL_BAR_STATE`,
initialized at shader entry and decremented on exit (opt-out via
`XCL_SAFE_BARRIER=0`). Single-warp Compute/Mesh `s_barrier` emits
`GroupMemoryBarrier()`.
@MesshM

MesshM commented Nov 24, 2026

Copy link
Copy Markdown

Test run of 0c02b87b267142801e6843563988bf7ac5f6a2e7 on AMD RDNA4. I merged it onto current main e50836b locally (clean merge) and tested that tree.

Tested

  • AMD Radeon RX 9060 XT 16 GB (RDNA4, native wave64), driver 32.0.32029.2010; Ryzen 7 5700X, 64 GB RAM, Windows 11 Pro build 26200, Rust 1.100.0 stable-x86_64-pc-windows-msvc, debug
  • cargo build --workspace + cargo test --workspace -- --test-threads=4 on the merge: 705 passed, 0 failed, 41 ignored, exit code 0; xcl lib tests 664 passed, 0 failed, 21 ignored
  • 0 SKIPPED; gpu_safe_s_barrier_multi_warp_and_early_endpgm passes, natively in wave64 on this adapter.

Review: A question about the bounded barrier: it lets waves go on after a bounded wait instead of hanging the SM when a wave has exited early. If the bound can also be reached by a wave that is merely slow (a long divergent path before its s_barrier), the others would continue unsynchronised and read LDS that is not written yet, giving wrong results rather than a hang. Is the bound tied only to waves that have executed s_endpgm (counted), or is it a spin count? If it is a spin count, a log or counter when it expires would make such a case visible. The change also alters how multi-wave s_barrier is emitted on 64-lane GPUs like this one; the suite passes here.

Not tested: no retail title or regression run (I have no unpacked game), no XCL_SAMPLE_* tests, release build; the PR's head on its old base was not run, only its merge onto main.

Run and review by Claude Code (Claude Opus 5.5) on this machine; the review points were checked against the code.

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

On 0c02b87, from reading the code (not run):

When xcl_s_barrier hits the 1 << 20 poll bound (emit.rs:5410-5416), the waiting wave decrements XCL_BAR_ALIVE for
itself while it keeps running, advances the generation (which releases every wave waiting at this barrier before the slow
wave has arrived), and turns its own later barriers off. The slow wave's eventual arrival is counted in the next
generation, so every later barrier in the group releases one arrival early. The result is silently wrong LDS data with no
host-side signal, where main would wait or hang visibly. A wave stuck in a long but legitimate loop (LOOP_GUARD allows
65 536 iterations) can plausibly exceed 2^20 LDS-atomic polls. Could the timeout raise a sticky flag the runtime logs,
and keep the waiter counted? The same applies to the now default-on XCL_X_SPIN path (xcl_x_dead).

AI-assisted (Claude Code), read-only review.

@eizispark

Copy link
Copy Markdown

Hardware check of 0c02b87b267142801e6843563988bf7ac5f6a2e7 (verified clean merge with main e50836b): pass.

Tested

  • Hardware: AMD Radeon RX 9070 XT (RDNA4, driver 32.0.32015.16001), AMD Ryzen 7 9800X3D, 32 GB RAM, Windows 11, rustc 1.100.0 (0ef1bc386 2026-11-09), release + test profile
  • cargo test --workspace -- --test-threads=4: 702 passed, 3 failed, 41 ignored (GPU tests skipped on this adapter: none; unfiltered native Windows run — the 3 hle::d3d12x failures were shared-VRAM contention in placed_title_textures_share_vram_and_preserve_pixels plus 2 downstream mutex PoisonErrors; cargo test -p xcl --lib hle::d3d12x:: -- --test-threads=1: 105 passed, 0 failed, 1 ignored)
  • gpu::recomp::tests (gpu_split_wave64_waves_diverge, gpu_safe_s_barrier_multi_warp_and_early_endpgm): 2 passed, 0 failed on native RDNA4 wave64

Regression run
main (f4ce27c):

xcl-smoke v1
commit: f4ce27ceb6d1b0576d0c659b713b674778bf1968
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1667 (PresentX 1667)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14847 MB

PR (0c02b87):

xcl-smoke v1
commit: 0c02b87b267142801e6843563988bf7ac5f6a2e7
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1653 (PresentX 1653)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14863 MB

Not tested

  • wave32-only GPUs (NVIDIA / Intel); mesh-shader xcl_s_barrier hardware dispatch; gameplay past the menus

AI use

  • Antigravity (Gemini 4 Carbon) ran the build, test suite, and scripts/smoke.ps1 harness on this machine and formatted this report.

@mastrr9

mastrr9 commented Nov 24, 2026

Copy link
Copy Markdown

Independent native Windows NVIDIA RTX 5080 (wave32) + D3D12 debug layer build/test run and code review at head 0c02b87b267142801e6843563988bf7ac5f6a2e7.

Code Review

  • Checked the LDS layout math in x64_lds + safe_barrier_prelude: XCL_XS + XCL_XC end at 8189, XCL_BAR_ALIVE = 8189, XCL_BAR_STATE = 8190, XCL_XW = 8191 (no slot overlap), and in the non-x64 LDS case XCL_LDS_TOP = 8188u keeps title LDS accesses below 8189..8190.
  • Checked Flow::End lowering in recomp/mod.rs: sets pc = 0xFFFFFFFFu; rather than return;, so early s_endpgm falls cleanly into xcl_s_barrier_exit() at the end of main().
  • Both gpu_safe_s_barrier_multi_warp_and_early_endpgm and gpu_split_wave64_waves_diverge passed cleanly on wave32 NVIDIA hardware (where split_wave64 + XCL_X_SPIN 1 + xcl_s_barrier() actually exercise the multi-warp wave32 path).

Tested

  • Environment: NVIDIA GeForce RTX 5080 (16 GB, wave32, driver 617.42 / 32.0.16.1742), Windows 11 Pro 25H2 (26200.9974), D3D12 debug layer installed (d3d12SDKLayers.dll), rustc 1.100.0 (0ef1bc386 2026-11-09) MSVC, DXC from Windows SDK 10.0.26100.0, debug build.
  • Command: cargo build --workspace && cargo test --workspace -- --test-threads=4
  • Result: 705 passed, 0 failed, 41 ignored (0 new warnings).
  • PR-specific tests (2/2 ok, executed on RTX 5080, not skipped):
    • gpu::recomp::tests::gpu_safe_s_barrier_multi_warp_and_early_endpgm
    • gpu::recomp::tests::gpu_split_wave64_waves_diverge
  • SKIPPED (2):
    • gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask (no wave64)
    • gpu::recomp::tests::gpu_wave32_program_in_wave64_hardware (no wave64)

Not tested

  • AMD/wave64 hardware (already covered above), release build, and retail titles.

AI use: Antigravity (Gemini 4 Neon reviewed the diff; Gemini 4 Hydrogen ran the RTX 5080 workspace tests) drafted this report.

@theaustinayers
theaustinayers merged commit 6c98f92 into xcl-dev:main Nov 24, 2026
1 check failed
CalvyPZ pushed a commit to CalvyPZ/xcl that referenced this pull request Nov 24, 2026
PR xcl-dev#798 (066ae5b, "Recompiler: default split-wave64 exchange to bounded
spin") made the counter variant the default for every group and made
XCL_WAVE64_SPIN=0 the way back to group barriers. The note still described
the state before it, in three places and in the opposite direction:

- the "one console wave per group" bullet presented
  GroupMemoryBarrierWithGroupSync as what those groups use;
- the counter bullet said "XCL_WAVE64_SPIN=1 forces this variant everywhere",
  so a reader takes the bounded wait to be opt-in;
- hazard 5 said "only the multi-wave variant spins".

`emit.rs`, `x64_spin`, reads the variable as
`map_or(true, |v| v.trim() != "0")`: unset means spin. Its own doc comment
says so, and `tests.rs` reads it the same way.

This matters beyond tidiness. As written, the note tells a reader on a
wave32 GPU that the bounded wait (2^20 polls, then `xcl_x_dead` and
half-local values) is off for their one-wave groups, when it is on for every
group. That is the first thing one would want to rule out when reading a
load-sensitive failure in this family, such as xcl-dev#847.

The old sentence "the test suite passes with it too" is dropped rather than
carried over to the other direction: on an RTX 5070 Ti,
`cargo test -p xcl --lib gpu::recomp -- --test-threads=4` is not stable in
either mode (four runs each: 0, 6, 5, 0 failures with the default and 1, 7,
1, 7 with XCL_WAVE64_SPIN=0). The failures are the family of xcl-dev#343 plus
DXGI_ERROR_DEVICE_REMOVED collateral, and they do not separate by the
variable, so the note should not claim an outcome either way.

Documentation only; the emitter is unchanged.

Assisted-by: Claude Code (Claude Opus 5)

Co-Authored-By: Claude Opus 5 <[email protected]>
upipibi added a commit to upipibi/xcl that referenced this pull request Nov 25, 2026
…v#798)

The Synchronisation section still named the group barrier as the form
for a group of one console wave and XCL_WAVE64_SPIN=1 as the switch to
the counters. Since xcl-dev#798 the HLSL translation uses the counters in every
group and XCL_WAVE64_SPIN=0 brings the barrier back; the direct DXIL
backend still uses the barrier there (Partner::Present) and does not
read the variable.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
grahamster2 added a commit to grahamster2/xcl that referenced this pull request Nov 25, 2026
…arrier

In the HLSL split-wave64 form a group of one console wave (64 threads or
fewer) is just the two 32-lane halves, and they take the same branches
(on exchanged, wave-uniform state), so both reach every exchange. Since
xcl-dev#798 they met through the bounded counter spin there too, which costs
about 2.3x the group barrier per exchange on Ampere (xcl-dev#1244: 13.2 ms vs
5.7 ms on an RTX 3080 in bench_split_wave64, 64-thread groups, one
exchange per iteration; 3.6x vs 1.55x wave32 on an RTX 3070).

x64_spin_for picks the form: groups of more than one console wave keep
the counters (a barrier would wait for other console waves); one-wave
groups now get the barrier, as the direct DXIL translator does
(Partner::Present, xcl-dev#1426). XCL_WAVE64_SPIN=1 forces the counters
everywhere, for debugging a desynchronised pair (wrong output instead
of a hang); =0 keeps the barrier, as before.

Assisted-by: Claude Code (Claude Opus 5.5)
grahamster2 added a commit to grahamster2/xcl that referenced this pull request Nov 26, 2026
…(XCL_WAVE64_BARRIER=1)

In the HLSL split-wave64 form a group of one console wave (64 threads or
fewer) is just the two 32-lane halves, and they take the same branches
(on exchanged, wave-uniform state), so both reach every exchange. The
counter spin they use by default (xcl-dev#798) costs about 2.3x the group
barrier per exchange on Ampere (xcl-dev#1244), but the barrier is unbounded: a
pair that desynchronises (a plain scalar load that sees two values)
hangs the dispatch, where the counters give up after a bounded wait.

So the counters stay the default and the barrier is opt-in:
XCL_WAVE64_BARRIER=1 (or XCL_WAVE64_SPIN=0, as before) selects it for
groups of 64 threads or fewer; XCL_WAVE64_SPIN set to any other value
still forces the counters and wins. Groups of several console waves
always use the counters. x64_spin_for holds the rule and is unit
tested; XCL_WAVE64_BARRIER bypasses the shader cache like
XCL_WAVE64_SPIN. A GPU test covers one-wave groups that end early
under the barrier.
@Prime1997 Prime1997 mentioned this pull request Nov 26, 2026
koboshchan pushed a commit to koboshchan/xcl that referenced this pull request Nov 26, 2026
…arrier

In the HLSL split-wave64 form a group of one console wave (64 threads or
fewer) is just the two 32-lane halves, and they take the same branches
(on exchanged, wave-uniform state), so both reach every exchange. Since
xcl-dev#798 they met through the bounded counter spin there too, which costs
about 2.3x the group barrier per exchange on Ampere (xcl-dev#1244: 13.2 ms vs
5.7 ms on an RTX 3080 in bench_split_wave64, 64-thread groups, one
exchange per iteration; 3.6x vs 1.55x wave32 on an RTX 3070).

x64_spin_for picks the form: groups of more than one console wave keep
the counters (a barrier would wait for other console waves); one-wave
groups now get the barrier, as the direct DXIL translator does
(Partner::Present, xcl-dev#1426). XCL_WAVE64_SPIN=1 forces the counters
everywhere, for debugging a desynchronised pair (wrong output instead
of a hang); =0 keeps the barrier, as before.

Assisted-by: Claude Code (Claude Opus 5.5)
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.

6 participants