Skip to content

perf(shader): the atomic locks bench times the turn and round forms - #261

Merged
pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
GDemay:perf/atomic-locks-bench
Nov 26, 2026
Merged

pragyaangaur merged 2 commits into
pragyaangaur:mainfrom
GDemay:perf/atomic-locks-bench

Conversation

@GDemay

@GDemay GDemay commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Issue #240 measured the emulated 64-bit image atomics (locked_atomic64, VICTORY_ATOMIC_LOCKS) at ~6 s of every 7.4 s world frame on an M2 Pro — VICTORY_ATOMIC_LOCKS=0 runs 1.5 s frames but draws wrong (cut-out geometry: fences, hair, small props). The issue's disassembly also found the title uses one 64-bit image atomic: image_atomic_umax of keys into R32G32_UINT texels, all lanes of a wave, in alpha pre-pass pixel shaders. A faster exact form is being written on the issue (the round form, announced 2026-11-26 09:34Z).

This is the complementary piece: not a form, but the bench both that form and any other will be measured on — the method of the pagetable, extents and tex_refresh benches, applied to the lock loop. The round form below is the announced design as described on #240; when the landed code differs, it can update the model so both forms are measured on one stand.

The model

One wave of 32 lanes executes one umax's op stream on CPU: the coordinate hash into the ATOMIC_LOCKS lock words, the spin's compare-exchange, the fenced read-modify-write, the and release — the emitted ones (victory-shader lower.rs, "Atomic locks"). Two forms:

Four shapes, three from the runtime's own test (image_atomics_of_64_bits, "as the alpha prepasses use them"): every lane into one texel; each lane its own texel; the test's program of one colliding and one own umax per lane; and a quarter-res prepass (four lanes per texel) between them.

Exactness, asserted every run

Both forms are checked against a plain sequential umax at every shape: the same texels, the same old values lane by lane, every lock word freed (bench_atomic_locks_exact). That is the bar #240's thread set for any faster form — the returned old values included.

Measured, Wine/Rosetta

6 runs of the committed bench (profile atomic_locks_wine in the campaign's records), median ns per wave visit:

ns a visit one texel 4 lanes/texel own texel the test's program
turn (today) 175.7 162.7 118.7 284.1
round (announced) 6712.2 701.6 95.2 6804.7

The counted op stream per visit (deterministic, printed by the bench):

barriers lock attempts rounds
turn, every shape 64 32 32
round, one texel 64 528 32
round, 4 lanes/texel 8 80 4
round, own texel 2 32 1

Readings:

  • The turn form's cost is unconditional: it walks all 32 lane indices and pays two barriers per active lane at every shape. That is the whole difference the announced form attacks.
  • The round form collapses the structure at spread: one round and two barriers when the wave's locks are distinct, eight at four-to-a-texel.
  • At full collision it pays more than today's, not the same: it keeps the turn form's 64 barriers and 32 rounds and adds the attempts of every lane that did not win — 528 compare-exchanges, 16.5x today's lock-word traffic. The attempt storm is the model's reading of the announcement's "one attempt per unfinished lane per round"; if the landed form attempts less, the model follows it.
  • The barrier is a sequentially-consistent fence on CPU — near-free here, the serializing term on the GPU — so the counts carry the structure that transfers and the ns the op streams. No other wave runs in the model, so the turn lane's bounded spin under a foreign lock is not timed; it is the same in both forms.

Reproducing

~/.cargo/bin/cargo bench --target x86_64-pc-windows-gnullvm -p victory-formats --bench atomic_locks --no-run
WINEPREFIX=$PWD/target/wineprefix WINEDEBUG=-all /opt/homebrew/bin/wine target/x86_64-pc-windows-gnullvm/release/deps/atomic_locks-*.exe

Natively, cargo bench -p victory-formats --bench atomic_locks runs the same bench (native µs swing with core scheduling and code layout around the fences; Wine/Rosetta is the stable flavor).

Verification

  • Native suite 525 passed / 0 failed / 17 ignored, identical to the clean-HEAD baseline stash-verified before the change (525 / 0 / 17 — upstream's own count moved from 522 with shader: image stores of some channels, and image_store_mip to level 0 (port of Marvisak/VIctory#1955) #248's image-store tests).
  • cargo build -p victory-gpu --target x86_64-pc-windows-gnullvm green.
  • Wine victory-gpu suite (WINE=/opt/homebrew/bin/wine tools/macos/test-wine.sh -p victory-gpu --no-fail-fast): 319 passed / 2 failed / 9 ignored — the two failures are feat(gpu): add opt-in mach_vm_remap zero-copy title buffer aliasing #207's own alias tests (aliased_shadow_refreshes_widened_rgb32_view, aliased_gpu_writes_notify_overlapping_texture_tracker at alias.rs:420/630), failing identically at clean HEAD, stash-verified; this PR touches no victory-gpu code.
  • cargo clippy --workspace --all-targets --locked --target x86_64-pc-windows-gnullvm -- -D warnings exit 0, and the same with --all-features.
  • perfops/bench.sh drift: 525 / 0 / 17, guard and texture metrics flat within the documented noise band (re-run per the monitoring protocol to confirm).

Issue pragyaangaur#240 measured the emulated 64-bit image atomics at ~6 s of every
7.4 s world frame on an M2 Pro, and its disassembly of the title's
shaders found the one operation that costs it: an unsigned maximum of
64-bit keys into R32G32_UINT texels, all lanes of a wave, in alpha
pre-pass pixel shaders. A faster exact form is being written on the
issue. This adds the bench that form and any other will be measured
against, before either lands — the method of the pagetable, extents and
tex_refresh benches, applied to the lock loop.

One wave of 32 lanes executes the lock loop's op stream on CPU: the
lock words (ATOMIC_LOCKS, hashed from the coordinates), the spin's
compare-exchange, the fenced read-modify-write and the `and` release
are the emitted ones (lower.rs, "Atomic locks"). Two forms run at four
shapes. The turn form emitted today walks every lane index, in turn
one lane alone taking its lock, reading, writing and freeing it — two
barriers each — whatever the other lanes' locks. The round form
announced on pragyaangaur#240 has every lane not finished attempt its lock each
round, the winners reading and writing in the same round, one fenced
region. Three shapes are the runtime's own test's
(image_atomics_of_64_bits, "as the alpha prepasses use them"): every
lane into one texel, each lane its own, and the test's program of one
colliding and one own umax; a quarter-res prepass sits between them.

Both forms are asserted against a plain sequential umax at every shape:
the same texels, the same old values lane by lane, every lock word
freed — the exactness bar pragyaangaur#240's thread set for any faster form. The
counted op stream prints beside the timings: the turn form pays two
barriers per active lane at every shape; the round form two per round.

The barrier is a sequentially-consistent fence on CPU, near-free where
the GPU's threadgroup barrier serializes memory traffic, so the counts
carry the structure and the ns the op streams. No other wave runs in
the model, so the turn lane's bounded spin is not timed; it is the same
in both forms.

Co-Authored-By: GLM 5.3 <noreply@z.ai>
PR pragyaangaur#257 lands a different round form than pragyaangaur#240 announced: four rounds
capped, one attempt per lane not done in each, nobody spinning — then
today's turn loop whole, for whoever the rounds leave, its finished
lanes passing through. The bench's round model was the announcement's
unbounded rounds; this makes it the code that shipped.

The worst case it models shrinks with the cap: at one texel for every
lane, 150 attempts (122 in the rounds, 28 turns) against the modeled
528, the barriers unchanged at today's 64 — 4.7x today's lock-word
traffic, not the announcement's 16.5x. A wave of distinct locks still
finishes in the first round, paying three empty rounds for having no
wave operation to leave by; four lanes per texel is unchanged, the cap
exactly covering it.

Co-Authored-By: GLM 5.3 <noreply@z.ai>
@GDemay

GDemay commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Model update: PR #257 landed a different round form than #240 announced, and the bench now models what shipped (2cbc7fb).

#257 is rounds-then-turn-fallback: a new loop capped at ATOMIC_LOCK_ROUNDS = 4 — per round every lane whose done flag is false makes exactly one compare-exchange, winners read-modify-write in that round, nobody spins — and then today's turn loop unchanged, entered per-lane by whoever the rounds left (no wave operation anywhere). The announcement's form, which the first commit of this PR modeled, was unbounded rounds with turn-lane priority inside them. The counts change accordingly:

counts per wave visit one texel 4 lanes/texel own texel the test's program
turn (today) 64 / 32 / 32 64 / 32 / 32 64 / 32 / 32 128 / 64 / 64
round, as first modeled (the announcement) 64 / 528 / 32 8 / 80 / 4 2 / 32 / 1 66 / 560 / 33
round, as #257 lands (now modeled) 64 / 150 / 36 8 / 80 / 4 2 / 32 / 4 66 / 182 / 40

(barriers / lock attempts / loop iterations)

Wine/Rosetta medians of the landed shape, 3 runs: turn 174.7 / 162.9 / 123.5 / 290.6 ns; round 1839.9 (one texel — the announcement model read 6712, the 4-round cap cuts the worst case's attempt storm 3.5x) / 733.8 (pool) / 110.1 (own — three empty rounds after the first, branches only) / 1851.5 (the test's program).

So the ruler's readings, corrected: the turn form's cost is unconditional (64 barriers at every shape); the landed rounds collapse the structure at spread (2 barriers, one round) and exactly cover the four-lanes-per-texel prepass; at full collision the landed form still pays today's 64 barriers plus a 118-attempt storm on top — 4.7x today's lock-word traffic, not the 16.5x the announcement's unbounded rounds would have paid. The exactness assertions cover the landed form unchanged: same texels, same per-lane old values, every lock word freed, against a plain umax.

@pragyaangaur
pragyaangaur merged commit 6182e6a into pragyaangaur:main Nov 26, 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.

3 participants