Repository navigation
fix(shader): 64-bit atomics no longer lose updates under heavy lock contention - #306
Conversation
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>
|
I ran the same generated 2,048-group ADD oracle on an M4 Pro with CrossOver
The two head mismatches were 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. |
|
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:
If you have time, could you run your 2,048-group oracle, or Prepared with Claude Code. Synthetic shaders only, no game data. |
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
ATOMIC_LOCK_SPINS), up from 2^16. Pixel and the other drawn stages keep 2^16 (ATOMIC_LOCK_SPINS_DRAWN).Host::lost), the host frees every lock word and logs how many were left taken.docs/macos.mdand 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,
main31c5047): the shader ofimage_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.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
cargo test --workspace541 passed, 0 failed both times.tools/macos/test-wine.sh --workspace --no-fail-fast1,093 → 1,095 passed, 0 failed (the two new tests).atomic_lock_bound_follows_the_stage,freeing_atomic_locks_keeps_pages_and_read_marks, and the ignored D3DMetal stressimage_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 warningsandcargo xtask policypass. Only changed lines are formatted.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