fix(shader): give one-lane waves an elected atomic max per lane #12724
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12724"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-10-29T05:18:58Z.
What
On a one-lane wave, every guest lane is its own host invocation: vertex and local programs on a device without vertex subgroups (MoltenVK, lavapipe), and merged hull programs. There, a wave64 f32 max half-wave scan throws at its
v_permlanex16_b32:The wave elects the lanes holding the maximum and lets them write it with one atomic. If every active lane instead writes its own key with the same atomic, memory ends with the same maximum, as long as unsigned order on the key bits matches float order. That holds when every key is a non-negative number.
EmitHalfWaveReductiontherefore reads lane 31 as the invocation's own key and lane 63 as the identity (-inf) on a one-lane host, andv_permlanex16_b32keeps its own value. It does so only whenElectedAtomicMaxproves all of the following; otherwise both still throw as before:v_max_f32. Its result feeds only the compare against the same key and the data of abuffer_atomic_umaxorsmaxwhose result is unused. Its written-lane mask equals the scan's active mask.WaveUniformValues). For an operand that the elected EXEC selects, this is checked on the arm those lanes take.FPSaturate, non-negative constants, phis, f32 maxima with one such operand (asEmitMinMaxF32Valuelowers them, NaN and ±0 included), and selects. A select's other arm is dropped when the scan's lane mask implies its condition through the exec words. That does not apply to a select reached through a loop back edge, whose condition may belong to an earlier iteration.Denormal flushing matches: each lane's data is its flushed key, which the scan's own flush selects already build, and the console's maximum is the maximum of those flushed keys.
The cost is one atomic per lane instead of one per wave. The debt entry in
docs/dev/debt/shader.mdrecords what is accepted and what throws.ASTRO BOT (PPSA21564 01.007.000) uses this shape in at least nine vertex shaders. They are what stopped it on MoltenVK after the title. The proof is general and does not depend on that title.
Tested
Apple M5 Pro, macOS 27.2, MoltenVK from Vulkan SDK 1.4.363 (no vertex subgroups), x86-64 Release under Rosetta 2.
mainalone does not build on macOS. I ran the tests in a tree with the open macOS port PRs; the emitter change, the new test function and the new execution test are identical there.agc_shader_memory_tests, newverifySingleLaneElectedAtomicMax, on programs assembled withllvm-mc(gfx1013, wave64):umax,smax, an address held by the permutation register only in lanes without the atomic, and nos_cbranch_execz.buffer_atomic_add;agc_driver_vertex_elected_atomic_max_tests(new): draws 3, 21, 33, 48 and 129 vertices, which covers more than one wave. Each vertex has the keysaturate(id / 32), and the buffer must holdmin(1, (N-1)/32)and leave the next word alone. It passes withVK_LAYER_KHRONOS_validationloaded and noVUID-messages. It skips on devices with vertex subgroups.v_permlanex16_b32 … outside the 1-lane host subgroupfailures remains. The run now reaches about 400 s, where it stops at an unrelated sampler case of #12662.Not tested on Linux, Windows or lavapipe.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code