Skip to content

fix(shader): 64-bit atomics no longer lose updates under heavy lock contention - #306

Merged
pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
hackgods:fix/atomic-lock-spin-bound
Nov 26, 2026
Merged

pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
hackgods:fix/atomic-lock-spin-bound

Conversation

@hackgods

Copy link
Copy Markdown
Contributor

Fixes the lost 64-bit adds patcliftongc reported on #240. The rounds are not the cause: the turns lose updates the same way. A lane that waits out the spin bound goes on without the lock, and its read-modify-write can then overlap another lane's.

What changed

  • Compute and mesh programs wait up to 2^22 tries for a lock another wave holds (ATOMIC_LOCK_SPINS), up from 2^16. Pixel and the other drawn stages keep 2^16 (ATOMIC_LOCK_SPINS_DRAWN).
  • After a submission the host GPU ended (Host::lost), the host frees every lock word and logs how many were left taken.
  • docs/macos.md and the module notes ("Atomic locks") describe both bounds and the freeing.

Why

Reproduction on an M5 Pro (64 GB, macOS 27.2.1, CrossOver 26.3 Wine, D3DMetal 4.0 beta 3, main 31c5047): the shader of image_atomic_adds_of_64_bits_apply_once, with 2,048 groups adding into one texel. The test as it is (8 groups) passed 40 of 40 runs here. The M2 Max hands the lock over more slowly relative to its compare-exchange tries, so it reaches the bound with fewer groups.

Spin bound Turns Rounds
2^16 (today) 2 of 100 runs lost adds (118 in all) 1 of 40 runs lost adds (61)
2^8 20 of 20 runs lost adds 20 of 20 runs lost adds
2^22 0 of 100 0 of 100

Every loss was a whole number of adds (61 × 0xFFFF_FFFF, for example), so whole read-modify-writes were lost, not torn dwords. patcliftongc's two failures (19 and 58 adds lost of 256) fit the same pattern.

Why not 2^22 everywhere. macOS ends a draw whose pixels run about 50 ms. That loses everything the draw drew and can stop the queue (docs/macos.md, "Metal ends long draws"), which is worse than one lost update. macOS has never ended a compute dispatch for running long, even one of 29 s, so there the longer wait costs time only. The failing test is a compute shader.

Why free the locks. The bound's other job is a lock nobody frees, which happens when the GPU ends a shader while one of its lanes holds a lock. The lock words were zeroed only when their buffer was made, so such a lock stayed taken for the rest of the run, and every later atomic on it waited out its bound. After an ended submission nothing runs, so the host can free them all. That also keeps the longer compute bound from turning a stuck lock into a long stall.

How tested

  • Before and after, on the M5 Pro above: cargo test --workspace 541 passed, 0 failed both times. tools/macos/test-wine.sh --workspace --no-fail-fast 1,093 → 1,095 passed, 0 failed (the two new tests).
  • New tests: atomic_lock_bound_follows_the_stage, freeing_atomic_locks_keeps_pages_and_read_marks, and the ignored D3DMetal stress image_atomic_adds_of_64_bits_apply_once_under_contention (2,048 groups, 20 runs in each form). The stress passed 6 of 6 invocations. With compute put back on 2^16 it failed 2 of 5, once in turns and once in rounds. The adds test's shader and checks moved into helpers both tests use.
  • cargo clippy -p victory-shader -p victory-gpu --all-targets --target x86_64-pc-windows-gnullvm -- -D warnings and cargo xtask policy pass. Only changed lines are formatted.
  • Not tested in the game. The bound only changes what happens after 65,536 failed tries, so ordinary frames should not change.

It does not touch the rounds switch, which is still opt-in. The default change you asked for on #240 comes as its own PR.

A possible follow-up, not in this PR: a counter of lanes that gave up, so a diagnostics run shows whether the game ever reaches the bound.

Thanks to patcliftongc for the report and the exact reproduction command.

🤖 Generated with Claude Code

hackgods and others added 2 commits November 26, 2026 18:32
A lane that waits out the spin bound goes on without the lock, and its
read-modify-write can then overlap another lane's, losing an update. At
2^16 tries this happened under heavy contention in both forms: 2,048
groups adding into one texel lost whole adds in 2 of 100 runs in turns
and 1 of 40 in rounds on an M5 Pro, and patcliftongc saw it with 8 groups
on an M2 Max (pragyaangaur#240). At 2^22 no run lost an add.

Compute and mesh programs now wait up to 2^22 tries (ATOMIC_LOCK_SPINS):
macOS never ends a compute dispatch for running long. The other stages
keep 2^16 (ATOMIC_LOCK_SPINS_DRAWN): macOS ends a draw whose pixels run
about 50 ms, and an ended draw loses all it drew.

Tests: atomic_lock_bound_follows_the_stage, and the ignored D3DMetal
stress image_atomic_adds_of_64_bits_apply_once_under_contention, which
failed 2 of 5 times with compute back on 2^16. The adds test's shader and
checks move into helpers that both use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…nded

A shader the host GPU ends while one of its lanes holds a lock never
frees it, and the lock words were zeroed only when their buffer was made,
so that lock stayed taken for the rest of the run and every later atomic
on it waited out its bound and went on without it. After a submission the
GPU ended (Host::lost), nothing runs, so the host now frees every lock
word and logs how many were taken. This also keeps the longer compute
bound from turning a stuck lock into a long stall.

Test: freeing_atomic_locks_keeps_pages_and_read_marks. docs/macos.md
describes both bounds and the freeing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@evan-kolberg

Copy link
Copy Markdown
Contributor

I ran the same generated 2,048-group ADD oracle on an M4 Pro with CrossOver 26.3.0.39832 and mapped D3DMetal 4.0b3. Both revisions used the public oracle and the same test wrapper to record 20 dispatch/readback trials per lock mode.

Compute lock mode Base 31c5047f Head 2e02b838 Shared ADDs lost in the head mismatch
Turns 7/20 exact 19/20 exact 5
Rounds 15/20 exact 19/20 exact 6

The two head mismatches were 0x0000fffaffff0005 for turns and 0x0000fff9ffff0006 for rounds, with 0x0000ffffffff0000 expected. All 262,144 lock and mark words read zero in every trial. Every per-lane texel matched at the head. One base turns trial also had a per-lane mismatch.

The head reduced failures in this sample. Both modes still produced a shared-texel mismatch at 2,048 groups on this M4 Pro. The fixture covers compute ADD, and queue cleanup and pixel UMAX have separate coverage requirements.

@pragyaangaur
pragyaangaur merged commit 8e8608c into pragyaangaur:main Nov 26, 2026
1 check passed
@hackgods

Copy link
Copy Markdown
Contributor Author

Thank you, evan-kolberg. That's a useful result: #306 cut the M4 Pro failures from 13 and 5 in 20 to 1 and 1, but didn't remove them.

On the M5 Pro, every lost add came from a lane that gave up its lock. With compute put back on 2^16 tries, each failing run had lanes that gave up behind it, and no run lost more adds than lanes gave up. But under that contention a try takes about 1.5 µs here, so 2^22 tries is several seconds for a dispatch of about 0.1 s. Your remaining losses might therefore not be the bound. #363 makes that visible: a lane that gives up now counts itself, at no cost (it rides on the atomic that frees the lock), and the stress test's message says which case it is:

  • the shared texel (N lanes gave up their lock): the bound still runs out on the M4 Pro, and compute needs a longer one.
  • X adds lost but 0 lanes gave up their lock: a different fault, which no bound will fix.

If you have time, could you run your 2,048-group oracle, or image_atomic_adds_of_64_bits_apply_once_under_contention with --ignored, on #363's head? Either case decides the next step.

Prepared with Claude Code. Synthetic shaders only, no game data.

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.

3 participants