Repository navigation
agx: AGX3 Dynamic Caching: last uses in divergent control flow, no static-split register budget - #603
Conversation
agx_opt_break_if moves a conditional break into the block after its if, but the edge to the break target stays on the emptied then block. Post-RA liveness therefore did not see what the loop exit reads at the break, and a value read at the exit but last read in the loop by the break's own compare got a last-use hint there. On AGX3 a last-use hint releases the register, so every thread taking the break could then read a released register at the loop exit. Take the break target's live-in as live at the break itself. A break's block is not a predecessor of its target, so with fused breaks the analysis repeats until nothing changes. Only hints that were wrong go away. Found while reviewing the G17P last-use change that follows. Signed-off-by: Diljit Singh <hey@djdev.me>
Divergent blocks got no last-use hints, because another side of the branch, run later with these threads masked off, may still read the register. Most of the work in loops and data-dependent branches therefore carried none: the large gpubench ray-tracing kernels had last-use hints on 0.4 to 2.6% of their register sources. Apple's AGX3 compiler marks last uses inside divergent control flow as densely as outside it, on about half of all register sources. On AGX3 a last-use hint releases the register for the whole SIMD-group: a wrong one destroys the value. So a divergent block now keeps every register a masked-off thread may still read. Such a thread waits at a block that program order has not reached yet: the target of an edge that jumps over this block or, inside a loop, a block the loop leaves to, while the loop runs again. The registers live into those pending blocks are the block's inactive set: its writes do not end them and none of its reads is their last use. Everything else gets last-use hints as in uniform control flow. Inactive sets and liveness feed each other and are solved together; both only grow, so the iteration ends. Blocks outside divergent control flow keep logical liveness. The pending sets grow with the square of the block count, so shaders with more than 4096 blocks keep no divergent hints. The scratch register agx_lower_divergent_shuffle routes register lane indices through must read zero in every inactive thread, as the shuffle adds the index up over the whole quad, so it never gets a hint. This follows the instruction set: both AGX3 encodings take it. AGX2's hint only discards the register-cache copy and keeps the old rule. Over the gpubench pipelines on the G17P drm-shim, last-use hints go from 36% to 44% of register sources, and from 29% to 40% inside divergent control flow (25 to 35% in the large ray-tracing kernels), with instructions, registers and spills unchanged. On the full static corpus (shader-db, Minecraft shader packs, gpubench; 10,982 shaders) no shader fails that did not before. Signed-off-by: Diljit Singh <hey@djdev.me>
The register budget of a compute shader comes from the AGX2 occupancy
table, so a 1024-thread workgroup gets 104 halves and spills the rest,
and any shader rematerialises constants into a reduced budget when that
reaches a smaller occupancy step ("save a wave"). When that reduced
budget cannot be met, the rematerialising spiller falls back to scratch
spills below the register file limit.
Both follow from AGX2's static register split: every program declares a
register count and the hardware divides its register file by it. The
AGX3B and AGX3C encodings declare no register count, in the program or
its launch; their on-chip storage is allocated as threads use it. There
is no occupancy step to reach, and Apple's AGX3 compiler only spills at
the register file limit. A 1024-thread workgroup with the whole file runs
correctly there, with its barriers, and faster than one spilling to 104
halves.
So on those encodings the budget is the register file alone: no
workgroup bound and no wave-saving rematerialisation. AGX2 and AGX3D,
which declares its register count per dispatch again, keep both.
Signed-off-by: Diljit Singh <hey@djdev.me>
M5 Pro (J714s, G17S B1) correctness row
Assisted-by: LLM (Google:Gemini 4 Neon) |
M5 Pro (J714s, G17S B1) register-pressure sweep (baseline driver)Ran a 1M-thread dependent-FMA compute microbenchmark (1 and 2 chains, 4096 steps/thread,
This directly confirms on M5 Pro hardware that AGX3C exhibits dynamic register allocation with no static-split occupancy cliffs up to 213 GPRs, and that the static 104-half workgroup cap causes ~2x throughput regressions via unnecessary spilling. Assisted-by: LLM (Google:Gemini 4 Neon) |
M4 Mac mini (J773g, T8132, G16G B0 / AGX3B) hardware A/B row
Assisted-by: LLM (Gemini:4 Neon) |
M5 Pro (J714s, G17S B1) Borealis performance A/B row
Assisted-by: LLM (Google:Gemini 4 Neon) |
M5 Pro (J714s, G17S B1) performance A/B row
Assisted-by: LLM (Google:Gemini 4 Neon) |
A18 Pro (J700, G17P B1 / AGX3B) hardware proofAll three AGX3 hardware rows (A18 Pro G17P below, M5 Pro G17S above, and M4 mini G16G above) are now complete with 0 regressions across conformance, performance, and kernel health. 1. Conformance (VK-GL-CTS 1.4.7.1, 85,556 cases — 0 Fail / 0 Crash / 0 Timeout)
2. Same-boot performance A/B (
|
RyanTheTide
left a comment
There was a problem hiding this comment.
No blockers found. The divergent last-use analysis looks right to me. I have one evidence question and a few small requests.
What I checked in physical_liveness() (src/asahi/compiler/agx_opt_register_cache.c:229), against how agx_compile.c builds the CFG:
- if/else: threads that go to the else wait at
else_blockwhile the then side runs, and threads that finish the then side wait atafter_blockwhile the else side runs. Both are covered by the forward-edge rule (blocks strictly between A and T). - Loops: the only back edge is the latch's (
emit_loopadds onlylatch -> loop_header, and NIR has nocontinuehere), and the exits are the break edges from body blocks. So every exit target ends up in the tail's pending set, and the loop rule spreads it over header..latch for the following iterations. With nested loops this composes: an inner-loop exit inside the outer loop is skipped byt >= h && t <= l, and an outer exit is spread over the inner loop too. cf_divergentcomes fromdivergent_nesting > 0, so uniform blocks nested in a divergent region also use physical liveness. Blocks outside any divergent region keep logical liveness, which is correct because every thread is there.- Writes in a divergent block don't end the inactive set, and the inactive set is added back after each instruction, so a reused register held by masked-off threads keeps no last use. A fused break ORs the target's live-in before its own sources, in both analyses.
- Shuffle scratch r1 (halves 2..3 on AGX3) never gets a hint while physical liveness is active. The 4096-block cap falls back to no divergent hints, and the free paths have no leaks.
- The new rules are selected by the instruction-set properties (
agx_isa_is_agx3,emit_agx3d), not by a chip list. There is no knob.
Findings:
- Evidence on the head (A18 Pro row). The J700 CTS row lists the driver as
git-aef3a57010, and the A/B uses base15fe5010. Neither ise5441540or its merge-base7ba29572, which the J773g and J714s rows used. Please confirm thataef3a57010carries the samesrc/asahi/compilertree ase5441540(an emptygit diff e5441540 aef3a57010 -- src/asahi/compileris enough). Otherwise rerun at leastdEQP-VK.reconvergence.*andsubgroupson the head. A wrong last-use hint destroys a live value, so the G17P row should be on the exact code. - AGX3D.
agx_divergent_last_use()(agx_opt_register_cache.c:378) is true for every AGX3 encoding, so G18G (emit_agx3d) also gets the divergent hints, whileagx_has_static_register_split()(agx_register_allocate.c:3030) keeps the static split there. No row covers AGX3D. Either say what shows that the AGX3D last-use hint releases the register the same way, or keep AGX3D on the old rule for divergent blocks until a G18 row exists (&& !ctx->key->dev.emit_agx3d, the same property commit 3 uses). - Compile time. The pending sets are n x n bits and the inactive/liveness fixpoint walks every pending target of every block on each pass, so the large RT kernels pay the most. The corpus run shows no new failures but gives no time. Please add the compile-time delta for the slowest few shaders in the 10,982-shader corpus (head vs base).
- Test gap. The loop test is a single loop. One nested-loop case would pin down the propagation step: a thread that has left the outer loop must keep its register while the inner loop runs again, and a register live only into the inner loop's exit must still get its last use after that exit. Both checks are cheap in
test-agx-register-cache.cpp. - Commit 1 is the same commit as #575. Please say in whichever lands second that the duplicate was dropped on rebase.
HW rows we can add:
- Neo (J700, G17P), this head, default kernel command line:
dEQP-VK.reconvergence.*(full),dEQP-VK.subgroups.*,dEQP-VK.compute.*,dEQP-VK.glsl.loops.*,dEQP-GLES3.functional.shaders.loops.*, plusregs.group local=1024 n=12/44A/B vs the merge-base. Pass: 0 Fail/Crash/Timeout vs base, CRC-identicalregs.*, 0 GPU recoveries or kernel GPU faults. - J616s (M4 Pro, also G17P): the same CTS subsets. Pass: identical to base.
- M1 Pro or M2 (AGX2; only commit 1 applies):
dEQP-VK.glsl.loops.*anddEQP-GLES3.functional.shaders.loops.*. Pass: identical to base. - An M3 (G15) row would close the remaining AGX3B family member if one is reachable; it is not required.
Assisted-by: LLM (Claude:Opus 5.5)
|
Thanks for the review! Answering the verification point and follow-up plan:
Assisted-by: LLM (Antigravity:Gemini 4 Neon) |
|
Merged into
Assisted-by: LLM (Google:Gemini 4 Neon) |
|
Mirror |
Resolve the agx_register_allocate.c conflict with #603 (Dynamic Caching): keep this branch's single agx_ra() (no retry without remat) and #603's agx_has_static_register_split() condition on the wave-saving remat, so AGX3B/AGX3C still skip it. Update two comments that still named agx_ra_impl(). Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
Problem
On AGX3 (M3, M4, A18 Pro, M5) no program and no launch declares a register count: on-chip storage is allocated as
threads use it, and a source operand marked as a register's last use releases that register for the whole SIMD-group.
Borealis and the shared AGX compiler still follow the AGX2 static split in two places:
agx_opt_register_cachemarks no last use in a divergent block,because a masked-off thread may still need the register. Nearly all work in loops and data-dependent branches
therefore carries none: the large gpubench ray-tracing kernels had last-use hints on 0.4–2.6 % of their register
sources. Native AGX3 shader output marks last uses on about half of all register sources, inside divergent control
flow as densely as outside it.
halves and spills the rest, and any shader rematerialises constants into a reduced budget to reach a smaller
occupancy step; when that budget is missed the rematerialising spiller falls back to scratch spills below the file
limit. AGX3B and AGX3C only need to spill at the register file limit.
Change
agx: keep the values a fused break leaves with live(all chips; the same commit as agx: keep the values a fused break leaves with live #575).agx_opt_break_ifleaves the exit edge on the emptied then block, so post-RA liveness missed what the loop exit reads at a fused break,
and that value could get a last-use hint at the break's compare.
agx: mark last uses in divergent control flow on AGX3. A divergent block keeps every register a masked-off threadmay still read: such a thread waits at a block program order has not reached yet (the target of an edge that jumps
over this block, or, inside a loop, a block the loop leaves to while the loop runs again). The registers live into
those pending blocks are the block's inactive set: its writes do not end them and none of its reads is their last
use. Inactive sets and liveness are solved together to a fixpoint; everything else gets last-use hints as in uniform
control flow. The divergent-shuffle scratch register (must stay zero in inactive lanes) never gets one. Selected by
the AGX3 instruction set (both AGX3B and AGX3C); AGX2's hint only discards the register-cache copy and keeps the old
rule. The
g17p_divergent_quad_loopbyte pin changes by five single-bit hints in its divergent loop body; withdivergent last uses off it is byte-identical to the old pin.
agx: drop the static register split's budget on AGX3. Where no register count is declared (AGX3B, AGX3C), thebudget is the register file alone: no workgroup register bound and no wave-saving rematerialisation. AGX2 and AGX3D
(which declares its register count per dispatch,
registers_16) keep both.No runtime switch and no chip list; selection follows directly from the instruction-set properties (
agx_isa_is_agx3and
emit_agx3d).Testing
meson test --suite asahi, fedora:44 container on heade5441540): 141 pass, 0 fail. Includesnew unit tests covering divergent if/else sharing a register, a loop's exited-thread register, a reuse no masked
thread needs, the shuffle scratch, the fused-break exit (fails before commit 1, passes after), and the
instruction-set selection.
gpubench, synthetic Vulkan): 0 new failures. Across gpubench pipelines plus
rt.rays/rt.scene, last-use hintsincrease 36.2 % -> 44.1 % of register sources and 29.0 % -> 39.7 % inside divergent control flow (large RT kernels
0.4/1.9/2.6 % -> 30.9/25.3/35.4 %), with instructions, registers, and spills unchanged. Dropping the static-split
budget removed 59 spills / 51 fills -> 0 on a 1024-thread shader-pack compute kernel and 14.7 % of corpus spills
overall.
reconvergence.*full 6,253/6,253 Pass,subgroups,compute,glsl,ray_query,ray_tracing_pipeline,mesh_shader,spirv_assembly,memory_model: 31,414 Pass, 20,458 NotSupported) + 33,684 GL/GLES cases (dEQP-GLES3,dEQP-GLES31,KHR-GL46:32,160 Pass, 1,504 NotSupported, 17 QualityWarning, 3 CompatibilityWarning) — 0 Fail, 0 Crash, 0 Timeout, 0 GPU
recoveries, 0 kernel faults, 0 AVC denials, 0 failed units.
regs.group local=1024, n=12-9.72 % GPU time (3880.1 -> 3503.0 us, 16/16CRC-identical) and
local=1024, n=44-9.28 % latency (34388.5 -> 31198.9 us); 103-rowgpubenchsuiteneutral overall (geomean 1.0020, median 1.0000; 93 win / 4 equal / 4 gap vs macOS 25G83, 1.304x overall).
regressions (all non-pass mesh
with_i16cases reproduced identically on base in 4/4 chunk reps and passed 22/22alone on both arms);
gpubenchB/H/B/H neutral within ±0.5 % across all 8 rows; Geekbench 6.7.1 Vulkan neutral(+0.28 %, all workloads within ±0.7 %); 0 kernel GPU errors.
(
reconvergence.*full 6,253, sampledsubgroups/compute/spirv_assembly11,756,vk-mustpass-short+vk-smoke26,226; 0 diffs, 0 Fail/Crash/Timeout),
vulkaninfo1,417/1,417 identical, 99-rowgpubenchABBA neutral (geomean0.9976), 0 kernel GPU errors.
Assisted-by: LLM (Antigravity:Gemini 4 Neon)