Recompiler: on a macOS host, s_barrier is the native group barrier by default #2363

Open
github-bot wants to merge 2 commits from github-bot/gh-2363 into main AGit
Owner

@⁠vakaobr on GitHub · 2026-11-25 16:41 UTC · original
vakaobr:recompiler/mac-native-barrier → main · +110 −4 · 3 files · 2 commits · draft

Stacked on #2361; only the top commit belongs here.

On a macOS host (CrossOver / D3DMetal), multi-wave Compute and Mesh s_barriers now use the native GroupMemoryBarrierWithGroupSync by default instead of the emulated spin barrier xcl_s_barrier. On D3DMetal, in a 1024-thread group in the split form, the waves spinning in xcl_s_barrier starve the one wave still working (its loop ran about 1000 times slower) until the spin's 2^20-poll bound gives up and threads read LDS before it is written: wrong output, or the hang at world entry reported in #258. The native barrier was correct in every case I tried, including waves that already ran s_endpgm, LDS exchange across the split-wave64 halves, barriers in loops and partial console waves. Windows keeps the spin barrier.

Part of #258.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: safe_barrier_enabled(). XCL_SAFE_BARRIER=0 / 1 decides; by default the spin barrier everywhere except on a macOS host (util::mac_host, #2361).
  • crates/xcl/src/gpu/recomp/tests.rs: new GPU test gpu_s_barrier_1024_threads_waits_for_working_waves (1024 threads, every wave runs 1000 v_readfirstlanes, then reads the LDS word of the farthest wave after the barrier); gpu_safe_s_barrier_multi_warp_and_early_endpgm asks safe_barrier_enabled() whether to expect xcl_s_barrier().

Tested

  • MacBook Pro M5 Pro, macOS 27.2.1, CrossOver 26.3.0, D3DMetal 3.0, Rust 1.100.0 x86_64-pc-windows-gnu, debug, DXC 1.9.2609, at c917219 (on #2361's 6d6a3e3, on main 65fc114).
  • Built with cargo test -p xcl --lib --target x86_64-pc-windows-gnu --no-run; each test in its own process: wine --bottle xcl-test <exe> --exact <test> --test-threads=1 --nocapture.
  • The 4 tests with barrier in their name, on the base commit (with only the new test added) and on this commit:
    • gpu_s_barrier_1024_threads_waits_for_working_waves: base fails (64 threads read their partner's LDS word before it was written), this commit passes. With XCL_SAFE_BARRIER=1 on this commit it fails again (128 threads).
    • gpu_safe_s_barrier_multi_warp_and_early_endpgm: passes on both, and with XCL_SAFE_BARRIER=1.
    • split_wave64_dead_scc_skips_exchange_barrier: passes on both.
    • gpu::dxil::ops_tests::groupshared_barrier_and_atomics_run: fails on both, with the same assertion (ops_tests.rs:398); it does not go through the HLSL emitter.
  • No SKIPPED tests.

Regression run

  • None: I have no title of my own to run scripts/smoke.ps1 on.

Not tested

  • Windows: the default there is unchanged by design (mac_host() is false), but I did not run it.
  • The whole suite; only the 4 tests above.
  • Any title, including the #258 world-entry hang.
  • The Direct DXIL backend's barrier (#1793) does not use this default.
  • The translation cache does not key on XCL_SAFE_BARRIER (#1409; #1413 turns it off while the variable is set). emit.rs is in the translator's source hash, so this commit itself does not reuse old entries.

AI use

  • Claude Code with Claude Opus 5.5 found the cause on D3DMetal, wrote the change and the test, ported them onto main, checked open PRs for overlap, ran the tests above and wrote this description.
**@⁠vakaobr** on GitHub · 2026-11-25 16:41 UTC · [original](https://github.com/xcl-dev/xcl/pull/2363) `vakaobr:recompiler/mac-native-barrier` → `main` · +110 −4 · 3 files · 2 commits · draft Stacked on #2361; only the top commit belongs here. On a macOS host (CrossOver / D3DMetal), multi-wave Compute and Mesh `s_barrier`s now use the native `GroupMemoryBarrierWithGroupSync` by default instead of the emulated spin barrier `xcl_s_barrier`. On D3DMetal, in a 1024-thread group in the split form, the waves spinning in `xcl_s_barrier` starve the one wave still working (its loop ran about 1000 times slower) until the spin's 2^20-poll bound gives up and threads read LDS before it is written: wrong output, or the hang at world entry reported in #258. The native barrier was correct in every case I tried, including waves that already ran `s_endpgm`, LDS exchange across the split-wave64 halves, barriers in loops and partial console waves. Windows keeps the spin barrier. Part of #258. **What is in it** - `crates/xcl/src/gpu/recomp/emit.rs`: `safe_barrier_enabled()`. `XCL_SAFE_BARRIER=0` / `1` decides; by default the spin barrier everywhere except on a macOS host (`util::mac_host`, #2361). - `crates/xcl/src/gpu/recomp/tests.rs`: new GPU test `gpu_s_barrier_1024_threads_waits_for_working_waves` (1024 threads, every wave runs 1000 `v_readfirstlane`s, then reads the LDS word of the farthest wave after the barrier); `gpu_safe_s_barrier_multi_warp_and_early_endpgm` asks `safe_barrier_enabled()` whether to expect `xcl_s_barrier()`. **Tested** - MacBook Pro M5 Pro, macOS 27.2.1, CrossOver 26.3.0, D3DMetal 3.0, Rust 1.100.0 `x86_64-pc-windows-gnu`, debug, DXC 1.9.2609, at `c917219` (on #2361's `6d6a3e3`, on `main` `65fc114`). - Built with `cargo test -p xcl --lib --target x86_64-pc-windows-gnu --no-run`; each test in its own process: `wine --bottle xcl-test <exe> --exact <test> --test-threads=1 --nocapture`. - The 4 tests with `barrier` in their name, on the base commit (with only the new test added) and on this commit: - `gpu_s_barrier_1024_threads_waits_for_working_waves`: base fails (64 threads read their partner's LDS word before it was written), this commit passes. With `XCL_SAFE_BARRIER=1` on this commit it fails again (128 threads). - `gpu_safe_s_barrier_multi_warp_and_early_endpgm`: passes on both, and with `XCL_SAFE_BARRIER=1`. - `split_wave64_dead_scc_skips_exchange_barrier`: passes on both. - `gpu::dxil::ops_tests::groupshared_barrier_and_atomics_run`: fails on both, with the same assertion (`ops_tests.rs:398`); it does not go through the HLSL emitter. - No SKIPPED tests. **Regression run** - None: I have no title of my own to run `scripts/smoke.ps1` on. **Not tested** - Windows: the default there is unchanged by design (`mac_host()` is false), but I did not run it. - The whole suite; only the 4 tests above. - Any title, including the #258 world-entry hang. - The Direct DXIL backend's barrier (#1793) does not use this default. - The translation cache does not key on `XCL_SAFE_BARRIER` (#1409; #1413 turns it off while the variable is set). `emit.rs` is in the translator's source hash, so this commit itself does not reuse old entries. **AI use** - Claude Code with Claude Opus 5.5 found the cause on D3DMetal, wrote the change and the test, ported them onto `main`, checked open PRs for overlap, ran the tests above and wrote this description.
util::mac_host(): the process runs under Wine on a Mac (CrossOver or the
Game Porting Toolkit), where D3D12 is Apple's D3DMetal and the title's
code runs through Rosetta 2. ntdll's wine_get_host_version names the
host's system; "Darwin" is a Mac. XCL_MAC_HOST=1 / 0 overrides it.

Nothing calls it yet. The Mac changes that need to know they run there
(the barrier default, title textures on shared pages) build on it; until
now only Ahmet-Dedeler's mac7 branch had it. Part of #258.
xcl_s_barrier (the LDS spin barrier for multi-wave Compute / Mesh groups)
starves the waves it waits for on D3DMetal: in a 1024-thread group in the
split form, the one wave still working ran about 1000 times slower while
the others spun, until the 2^20-poll bound gave up and the barrier let
threads read LDS early (wrong output; a hang at world entry was reported
in #258).  D3DMetal's GroupMemoryBarrierWithGroupSync is correct in every
case tried, including waves that already ran s_endpgm, LDS exchange
across the two halves, barriers in loops and partial console waves.

safe_barrier_enabled(): XCL_SAFE_BARRIER=0 / 1 decides; by default the
spin barrier everywhere but on a macOS host (util::mac_host).  Windows is
unchanged.

gpu_s_barrier_1024_threads_waits_for_working_waves fails without this on
the Mac (64 of 1024 threads read their partner's LDS word before it was
written) and passes with it; with XCL_SAFE_BARRIER=1 it fails again.

Part of #258.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:28:59 +00:00
Author
Owner

@⁠DavidM1911 on GitHub · 2026-11-25 18:50 UTC · original

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

**@⁠DavidM1911** on GitHub · 2026-11-25 18:50 UTC · [original](https://github.com/xcl-dev/xcl/pull/2363#issuecomment-7171469587) Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
This pull request has changes conflicting with the target branch.
  • crates/xcl/src/gpu/recomp/tests.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2363/head:github-bot/gh-2363'
git switch 'github-bot/gh-2363'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2363'
git switch 'github-bot/gh-2363'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2363'
git switch 'github-bot/gh-2363'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2363'
git switch 'main'
git merge --squash 'github-bot/gh-2363'
git switch 'main'
git merge --ff-only 'github-bot/gh-2363'
git switch 'main'
git merge 'github-bot/gh-2363'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.