Skip to content

On MSL 3.2+, heap_free reads the error flag with a coherent volatile load, not an atomic one (#1143) - #1299

Merged
nicolas-abril merged 1 commit into
bendlang:mainfrom
nicolas-abril:fix-1143-heap-free-err
Oct 6, 2026
Merged

nicolas-abril merged 1 commit into
bendlang:mainfrom
nicolas-abril:fix-1143-heap-free-err

Conversation

@nicolas-abril

@nicolas-abril nicolas-abril commented Oct 3, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #1143 with its first option. Needs a check on an M1 or M2 before merging (see below).

heap_free checks the error flag on every free, and on the device that check is an atomic load of one shared word, which lanes are served one at a time. On MSL 3.2+, where DEV is coherent(device), it now reads the flag with a volatile load. That keeps the check but lets the read come from cache. Below 3.2, and on the host and CUDA, err_peek is err_seen, unchanged.

Speed (M4, wall time, --gpu, 20 alternating rounds, same outputs):

bench main this PR ratio
tree-bitonic (768MB) 1.756s 1.662s 1.06x
tree-radix 1.182s 1.160s 1.02x

The issue measured 1.12x and 1.07x in GPU time on an M5.

Error path: I ran both benches out of heap (--gpu 512MB) 150 times each, with the same volatile read patched into main's emitted C:

build bad outcomes
main 1 / 300 (1 hang)
volatile read 3 / 300 (3 hangs)

Counting earlier, smaller runs too, main had 2 bad outcomes in ~400 and the volatile read 5 in ~384, which is within chance (Fisher's exact p ≈ 0.3). Main's out-of-heap path occasionally faults or hangs on its own; that's #1300. Removing the check instead and guarding only the sink page went bad in 19 of 60 runs, so the check itself has to stay.

Before merging:

  • M1/M2: this is a new load form (air.load.device_coherent.volatile), and a32_load's comment says M1/M2 pipeline builds die on some load forms. Nobody has built a ! program on an M1 or M2 with this change; it needs one there.

Gates (rebased on main at 1487e77d): repo 50 / 50, with comp.ts at 63,507 ttok, under its 64,000 cap (main is 63,464); test 1577 / 1577. perf scores 121 to 124 / 124 from run to run, on this PR and on main alike, failing on the same cells (the short GPU runs of nbody, mandelbrot and merkle, and raytrace's compiler time). ping was skipped: the site repo isn't checked out on the machine that ran them.

🤖 Generated with Claude Code

@Lorenzobattistela Lorenzobattistela self-assigned this Oct 5, 2026

@Lorenzobattistela Lorenzobattistela left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran the gates on this PR merged onto main 95217e9c: repo 50/50, ping 47/47, test 1559/1559 and perf 124/124. I didn't run gates/safe.ts, since this only touches the runtime. The cap note is out of date: since #1281, comp.ts merged comes to 63,027 / 64,000 ttok, so nothing needs trimming.

The M1/M2 build is still the blocker. a32_load reads through a volatile local because, as its comment says, "Metal's loads read through a volatile local, or the M1 and M2 pipeline builds die". err_peek instead reads device memory directly with *(volatile DEV u32*). gpu_desc sets no language version, so an M1 or M2 on macOS 15+ compiles at MSL 3.2+ and takes this load. heap_free is on nearly every path, so if this form is one that kills the build, every --gpu program fails on those machines. The gate minis are all M4, so the gates can't catch this; it needs a run on a real M1 or M2 before merging, as you note.

A small one: err_peek does a plain read of a word that err_post writes with an atomic CAS. MSL's model calls that a data race. It works on current Apple GPUs, and your out-of-heap runs show no difference from main beyond chance. But it's the one place the runtime reads a shared word outside a32_*, so it deserves a line at the macro saying why that's safe here.

The perf gate only checks against the pins, so I haven't measured the 1.06x / 1.02x myself.

…load, not an atomic one (bendlang#1143)

Every free on every lane read the one error word atomically; atomics on
one address are served one at a time. MSL 3.2's coherent device memory
lets a volatile load see another lane's post, so heap_free keeps its
check at the cost of a cached read. Below 3.2, and on the host and CUDA,
it is err_seen as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@nicolas-abril
nicolas-abril force-pushed the fix-1143-heap-free-err branch from d814ba8 to 5bf7fe7 Compare October 6, 2026 12:28
@nicolas-abril
nicolas-abril merged commit 7ed3424 into bendlang:main Oct 6, 2026
1 check passed
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: heap_free's error check is an atomic load of one word on every free, 7-12% of the allocating tree benches

2 participants