Skip to content

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

Draft
vakaobr wants to merge 2 commits into
xcl-dev:mainfrom
vakaobr:recompiler/mac-native-barrier
Draft

vakaobr wants to merge 2 commits into
xcl-dev:mainfrom
vakaobr:recompiler/mac-native-barrier

Conversation

@vakaobr

@vakaobr vakaobr commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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.

The default goes into main's safe_barrier_wanted(), so XCL_SAFE_BARRIER still decides the same way for both translators. The refusal of the split form's geometry ES phase (#1727's pattern) is unchanged on a Mac: only XCL_SAFE_BARRIER=0 lifts it, as on main.

Part of #258.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: safe_barrier_wanted(). XCL_SAFE_BARRIER=0 / 1 decides; by default the spin barrier everywhere except on a macOS host (util::mac_host, Memory: detect a macOS host under Wine (mac_host) #2361). New safe_barrier_set_off(): whether XCL_SAFE_BARRIER=0 is set.
  • crates/xcl/src/gpu/recomp/pso.rs: the compute build key's safe_barrier comes from safe_barrier_wanted(). It read the variable on its own, so on a Mac a default run and an XCL_SAFE_BARRIER=1 run would have shared keys.
  • crates/xcl/src/gpu/dxil/kernel.rs: translate_geometry refuses the split ES phase unless safe_barrier_set_off(), which is main's behaviour. Its two tests (lower_mem.rs, geometry_tests.rs) ask the same.
  • 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). Two tests that read the variable themselves now ask safe_barrier_wanted().

Tested

  • M5 Pro, macOS 27.2.1, CrossOver 26.3 (Wine) with D3DMetal, Rust 1.100, x86_64-pc-windows-gnu, debug, DXC 1.9.2609; head e69da9d on Memory: detect a macOS host under Wine (mac_host) #2361's 69c0b04 on main 9dfbe3e. Base: 69c0b04 with only this PR's tests.rs.
  • 57 tests: every test with barrier in its name, all of gpu::dxil::geometry_tests and gpu::dxil::robustness_tests, geometry_export_rejected, the compute build key tests, and the two that look for xcl_s_barrier() in the HLSL. Each ran in its own process (<lib test exe> --exact <test> --test-threads=1):
run passed failed ignored
base 54 2 1
this PR 55 1 1
this PR, XCL_SAFE_BARRIER=1 54 2 1
base, XCL_SAFE_BARRIER=0 55 1 1
this PR, XCL_SAFE_BARRIER=0 55 1 1
  • gpu_s_barrier_1024_threads_waits_for_working_waves: fails on the base (128 threads read their partner's LDS word before it was written); passes with this PR, and with XCL_SAFE_BARRIER=0 on either; fails with XCL_SAFE_BARRIER=1 (128 threads).
  • gpu::dxil::ops_tests::groupshared_barrier_and_atomics_run fails in all five runs on the same assertion (ops_tests.rs:398); this switch does not reach it.
  • The ignored test is programs_left_to_the_hlsl_path_for_their_barrier_translate_and_compile (XCL_SAMPLE_BARRIER). No SKIPPED tests.

Regression run

  • None: I have no title of my own to run scripts/smoke.ps1 on. On Windows mac_host() is false, so the default there is unchanged.

Not tested

AI use

  • Claude Code with Claude Opus 5.5 found the cause on D3DMetal, wrote the change and the test, rebased it onto current main, ran the tests above and wrote this description. I checked it before posting.

@DavidM1911

Copy link
Copy Markdown
Contributor

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

Thanks!

vakaobr added a commit to vakaobr/xcl that referenced this pull request Nov 26, 2026
# Conflicts:
#	crates/xcl/src/gpu/recomp/tests.rs
@github-actions

Copy link
Copy Markdown

Nothing has happened on this PR for 3 days, so it is marked stale and will be closed in 4 days. A push or a comment takes the mark off: rebase it onto main if it conflicts, and say in one line what it still fixes. If it only waits for a maintainer, a comment saying so is enough.

@github-actions github-actions Bot added the stale No activity for 3 days; closed 4 days later unless something happens label Nov 30, 2026
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 xcl-dev#258.
@vakaobr

vakaobr commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Please keep this open: main now uses safe_barrier_wanted in the direct translator too, so changing its default on a Mac reaches further than this PR tested. I'll rework and re-test it on current main.

… default

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 xcl-dev#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_wanted(): XCL_SAFE_BARRIER=0 / 1 decides; by default the
spin barrier everywhere but on a macOS host (util::mac_host).  It keeps
deciding for both translators (the HLSL emitter and the direct
translator's s_barrier) and for the compute build key, which read the
variable on its own until now.  translate_geometry's refusal of the
split form's ES phase (xcl-dev#1727's pattern) is unchanged: only
XCL_SAFE_BARRIER=0 lifts it (safe_barrier_set_off), on a Mac too.
Windows is unchanged.

gpu_s_barrier_1024_threads_waits_for_working_waves fails without this on
the Mac (128 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 xcl-dev#258.
@vakaobr
vakaobr force-pushed the recompiler/mac-native-barrier branch from c917219 to e69da9d Compare November 30, 2026 14:42
@vakaobr

vakaobr commented Nov 30, 2026

Copy link
Copy Markdown
Contributor Author

Reworked on current main (e69da9d): the default now goes through safe_barrier_wanted(), so it reaches both translators and the build key; the split-geometry refusal stays as on main. Description updated with the test runs.

@github-actions github-actions Bot added area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:nvidia Code is specific to NVIDIA / wave32 GPUs (scope tag; see needs:nvidia-test) labels Nov 30, 2026
@Foniass

Foniass commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Triage (#1607) – head e69da9d

Stacked on #2361 (mac_host). Only the top commit, e69da9d, belongs to this PR. No other open or merged PR changes the s_barrier default on a Mac. #1793 and #1413 touch the safe barrier but do not change its default.
The PR routes the compute build key (pso.rs) through safe_barrier_wanted(), which fixes a key that would otherwise not see the Mac default. A new safe_barrier_set_off() keeps translate_geometry's refusal of the split ES phase unchanged, and on Windows nothing changes.
XCL_MAC_HOST now changes the translated HLSL but is not in cache_allowed (pso.rs) or the shaders.rs env-var list, unlike XCL_SAFE_BARRIER.
Recommendation: keep open.

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

vakaobr added a commit to vakaobr/xcl that referenced this pull request Dec 1, 2026
Conflicts in 25 files. Where the fork carried an early copy of an upstream
change (the shader preparation PR, the copy back after a submission, the
static sampler across branches, PR xcl-dev#1329), upstream's later form is kept.
Fork-only changes are ported onto upstream's new code:

- Shaders / xcl run: on a Mac compute pipelines are prepared in the title's
  process (default_workers), preparation is left out without DXC (outside a
  pipeline corpus), the launcher watch passes its handle to before_guest.
  The fork's MiB worker count is dropped for upstream's 231c4f1.
- Recompiler: one safe_barrier_wanted() with the Mac default, also read by
  the direct DXIL translator and ComputePolicyKey; safe_barrier_set_off()
  keeps upstream's split-geometry refusal unless XCL_SAFE_BARRIER=0 (as in
  xcl-dev#2363). The translated cache keeps its emitter-switch key.
- D3D12.X: stall-report hooks moved into SubmissionGate, the new dispatch
  signature and State::wait_for_ms; the Mac GPU-wait cap sits in
  take_expired_gpu_wait (a capped wait now stops the title like any
  timed-out wait unless XCL_WAIT_TIMEOUT_CONTINUE=1); late-release ring and
  native-host Release on upstream's res_release (forget_released_as);
  level tiles on upstream's title-queue upload path and with_title_bytes.
- sparse.rs is upstream's file plus the fork's stall report line.

Test title built from this tree, run under CrossOver 26.3 / D3DMetal
(bottle xcl-agent-1, xcl run <game> -- --hello): 50 passed, failures 0.
Full unit suite not run.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:recompiler RDNA2 decoder, HLSL/direct-DXIL recompiler, wave64/wave32 area:tests Unit, integration, e2e tests, or testtitle gpu:nvidia Code is specific to NVIDIA / wave32 GPUs (scope tag; see needs:nvidia-test) stale No activity for 3 days; closed 4 days later unless something happens

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants