Skip to content

M1 and M2 build the GPU program again: a32_load_acq reads through Metal's volatile local, as a32_load does - #1274

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
nicolas-abril:fix-1154-metal-load-acq
Oct 2, 2026
Merged

nicolas-abril merged 1 commit into
bendlang:mainfrom
nicolas-abril:fix-1154-metal-load-acq

Conversation

@nicolas-abril

Copy link
Copy Markdown
Collaborator

Fixes #1154.

Cause

comp.ts already works around an Apple GPU compiler bug: "Metal's a32_load reads through a volatile local, or the M1 pipeline build dies." 87a1e9c (in 2.0.29) changed a32_load_acq from a32_load(p) to atomic_load_explicit(A32(p), ACQ). That gave the host a real acquire, since its FENCE() is a no-op, but it also skipped the volatile local on Metal. Since then, MTLCompilerService crashes building the pipeline for any program with a ! on M1 (g13, abort) and M2 (g14, SIGSEGV). M3 and M4 (g15/g16) are unaffected, which is why the gates and our M4s never saw it.

Fix

u32 v = DEVICE ? a32_load(p) : atomic_load_explicit(A32(p), ACQ);
  • Device: ACQ is RLX (Metal has no memory_order_acquire), so this is the same relaxed atomic load followed by the same seq_cst device fence. The IR has the same air.atomic.global.load and air.atomic.fence calls; the only addition is the store and reload through a thread-private local. clang drops the dead branch even at -O0. CUDA expands to the same text as before.
  • Host: keeps memory_order_acquire.

Verification

  • Bisect: offline with Apple's metal-tt, targeting applegpu_g14s with Metal 3.2 / macOS 15, the bisect lands on 87a1e9c. Restoring only the volatile local makes 87a1e9c, v2.0.32 and main build; removing only it from the last good commit makes that one crash.
  • Offline builds: the issue's program and all 21 GPU tests that emit a kernel build for g13, g14, g15 and g16 with this change. On main, all of them fail on g13 and g14.
  • M4 run: the issue's program prints 134209536.
  • Test gate: gates/test.ts passed 1526 / 1526 (together with an unrelated change in that run).
  • M4 performance: 16 GPU runtime benches, 180 main/fix pairs per bench, run back to back on the same node across all 48 minis: fix/main = 1.0013, 95% CI [0.9993, 1.0033]. Every bench is within ±1.4%, and all outputs are identical.
  • Not tested on M1/M2 hardware; only through the offline toolchain, whose compiler is newer than macOS 15.7.4's but hits the same crash on the same commits.

gates/repo.ts: comp.ts is already over its cap on main (64,274 > 64,000 ttok); this adds 9.

🤖 Generated with Claude Code

…al's volatile local, as a32_load does

87a1e9c turned a32_load_acq's a32_load(p) into atomic_load_explicit(A32(p), ACQ),
which gives the host its acquire but skips the volatile local Metal's a32_load
reads through. Without it the M1 (g13) and M2 (g14) pipeline builds die in
MTLCompilerService, so every program with a `!` fails to build there since
2.0.29. On the device ACQ is RLX, so a32_load is the same relaxed load and
FENCE; the host keeps its acquire.

Fixes bendlang#1154

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril merged commit 43efc78 into bendlang:main Oct 2, 2026
@nicolas-abril
nicolas-abril deleted the fix-1154-metal-load-acq branch October 2, 2026 17:37
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.

Metal regression: a small parallel sum fails shader compilation on M2 Pro / macOS 15.7.4 in 2.0.32

1 participant