Skip to content

agx: Keep reorderable device loads in flight across threadgroup barriers - #1232

Merged
RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
DanteCpp:agx2-reorderable-loads-across-tg-barrier
Nov 27, 2026
Merged

RyanTheTide merged 1 commit into
aurora-silicon:aurora-wipfrom
DanteCpp:agx2-reorderable-loads-across-tg-barrier

Conversation

@DanteCpp

@DanteCpp DanteCpp commented Nov 27, 2026 •

Copy link
Copy Markdown

Problem

agx_insert_waits_local() unconditionally drains every pending scoreboard slot before AGX_OPCODE_THREADGROUP_BARRIER. In tiled compute shaders (GEMM, convolutions, stencils) that prefetch read-only device memory across a workgroup barrier, this forces a full device-load stall before threads even enter the barrier.

Change

  • src/asahi/compiler/agx_compile.c: set load->memory_can_reorder for nir_intrinsic_can_reorder(instr) && !nir_is_coherent(instr) on AGX2 as well as AGX3.
  • src/asahi/compiler/agx_spill.c: guard can_remat_load() explicitly on agx_isa_is_agx3() so AGX2 spilling behavior is unchanged.
  • src/asahi/compiler/agx_insert_waits.c: track drain_on_tg_barrier per scoreboard slot. Slots holding only reorderable read-only AGX_OPCODE_DEVICE_LOADs (which write only private registers and cannot race with threadgroup memory) stay in flight across AGX_OPCODE_THREADGROUP_BARRIER until their destination registers are consumed, a AGX_OPCODE_MEMORY_BARRIER is reached, or the block exits.
  • src/asahi/compiler/test/test-insert-waits.cpp & src/asahi/compiler/meson.build: add InsertWaits unit tests covering reorderable and non-reorderable device loads across THREADGROUP_BARRIER and MEMORY_BARRIER.

Testing

Tested on a 16-inch MacBook Pro M1 Max (J316c, G13X), kernel 7.3.0-ds4-ARCH:

  • Driver identities:
    • Arm A (origin/aurora-wip 13400438325): libvulkan_asahi.so sha256 d39ac03363d8a344844f4a0741550018b4b1b94c4f8bb427840a6c75691f15f4
    • Arm B (agx2-reorderable-loads-across-tg-barrier a4be23e33ae): libvulkan_asahi.so sha256 b53ceda085bd8b23eee5a124f8e24776386869e0a68c44d41755921de0be3548
  • Unit tests: agx_tests (OK, including all 3 new InsertWaits.* tests).
  • Vulkan CTS (deqp-vk, 0 regressions):
    • dEQP-VK.compute.pipeline.basic.*: 77 Pass, 0 Fail, 3 NotSupported
    • dEQP-VK.compute.pipeline.workgroup_memory_explicit_layout.*: 2,857 Pass, 0 Fail, 970 NotSupported
    • dEQP-VK.memory_model.message_passing.core11.u32.coherent.fence_fence.atomicwrite.workgroup.*: 9 Pass, 0 Fail, 16 NotSupported
  • Kernel health: 0 GPU kernel errors during all runs.
gpubench-vk alternating A/B (depth 4, 3 pairs x 5 runs, M1 Max J316c)
Test Arm A GPU (µs) Arm B GPU (µs) Delta
compute.conv2d 422.3 422.3 +0.02%
compute.stereo 4780.3 4781.0 +0.01%
compute.edge 113.7 114.0 +0.26%
compute.heavy 249.9 250.0 +0.05%
compute.fp16 67.6 68.9 +1.91%

Read-only non-coherent device loads (memory_can_reorder) write only private registers and cannot race with threadgroup memory. Set memory_can_reorder on AGX2 as well as AGX3 (keeping AGX3 load rematerialization guarded in agx_spill.c) and track per-slot drain_on_tg_barrier in agx_insert_waits() so threadgroup_barrier skips draining slots holding only reorderable device loads until their destination registers are consumed or a memory_barrier/block exit is reached.

Add InsertWaits unit tests covering threadgroup_barrier and memory_barrier handling for reorderable and non-reorderable device loads.

Signed-off-by: Dante Loi <danteloi1993@gmail.com>
@DanteCpp
DanteCpp requested review from a team and DjDeveloperr as code owners November 27, 2026 15:01
@RyanTheTide

Copy link
Copy Markdown

a4be23e33aed J316s (M1 Pro / G13S C0, 7.3.0-aurora-fleet89) + J700 (Neo / G17P B1, 7.3.0-aurora-fleet89) + J615 (M3 Air / G15G C0, 7.3.0-aurora-fleet90-airs2) PASS: Borealis exact-head overlay ICD (VK_DRIVER_FILES, -Dbuildtype=release -Db_ndebug=false, stock /usr/lib64/libvulkan_asahi.so verified byte-identical before/after on all 3 Macs); meson test --suite asahi 215/215 PASS; deqp-vk 435 PASS / 0 FAIL on J316s (api.smoke.* 8/8, compute.pipeline.basic.* 78/81, compute.* 1,10 328/5485, synchronization.basic.* 21/29) and 447 PASS / 0 FAIL on both J700 and J615 (api.smoke.* 8/8, compute.pipeline.basic.* 80/81, compute.* 1,10 330/5485, synchronization.basic.* 29/29); 0 GPU faults/recoveries, Enforcing, 0 failed units, 0 new AVC.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: FINISH, then merge at a4be23e33aed

Blockers: none

Checked:

  • Read the whole diff and tried to break the barrier change:
    • Only slots that hold nothing but DEVICE_LOADs with memory_can_reorder skip the drain at THREADGROUP_BARRIER. The flag sticks: once any other async op (local load or store, image op, a non-reorderable or coherent device load) lands in the slot, the slot drains. It is cleared only when the slot is waited on. MEMORY_BARRIER still drains everything.
    • memory_can_reorder requires nir_intrinsic_can_reorder(), which rules out volatile loads and needs ACCESS_CAN_REORDER (nir_opt_access: nothing in the shader writes that memory), and it also requires !nir_is_coherent(). A load issued before a workgroup barrier that reads memory nobody in the dispatch writes can complete after the barrier without changing the result. Threadgroup memory goes through LOCAL_LOAD/LOCAL_STORE, which always drain, so shared-memory aliasing cannot reach this path.
    • Register hazards are unchanged: RAW and WAW on the in-flight destination still wait, and the slot still drains at block end (non-exit blocks and preambles), so nothing stays in flight across a CFG edge.
    • The wait pass runs only for AGX_ISA_ENCODING_AGX2 (agx_compile.c:16130), so AGX3 output is unchanged even though AGX3 already set the flag.
    • The other readers of memory_can_reorder: agx_pressure_schedule.c:270 only acts on IMAGE_LOAD; the load merge at agx_compile.c:13777 ANDs the flag across the merged words; spill remat is now explicitly limited to AGX3 (agx_spill.c:427). So turning the flag on for AGX2 changes nothing else.
  • Head merged into aurora-wip 743fa0d3 (clean; none of the touched files changed since your base 13400438): local drm-shim gate 338 Ok / 0 Fail. The existing keyless spill/remat tests still pass with the new !ctx->key guard.
  • Negative check: with agx_insert_waits.c reverted and the tests kept, InsertWaits.ReorderableDeviceLoadStaysInFlightAcrossThreadgroupBarrier fails (337/1). Non-blocking: the tests set memory_can_reorder by hand, so nothing covers the agx_compile.c:2456 line that now sets it on AGX2. A NIR-level case (a load_global with ACCESS_CAN_REORDER, then a barrier, then a use) would pin it, and so would a mixed-slot case (reorderable plus non-reorderable in the same slot).
  • Rows at this head: present: J316c M1 Max G13X (yours), J316s M1 Pro G13S, J700 G17P and J615 G15G (comment above; the AGX3 rows run unchanged code). Missing: an AGX2 row of another generation (G14G M2 or G13G M1). The change affects every AGX2 chip, and both AGX2 rows so far are G13X. Your own "still to run" double-buffered tiled compute A/B would also show whether this gains anything: the gpubench deltas in the body are all within noise.

To finish: a G14G (or G13G) row at a4be23e33aed with compute.pipeline.*, memory_model.message_passing.*workgroup* and synchronization.basic.*. Then merge.

Evidence
  • Gate (head merged into aurora-wip 743fa0d, tree 691e9d2a): Ok: 338 Fail: 0
  • Mutation (agx_insert_waits.c reverted, tests kept): Ok: 337 Fail: 1, [ FAILED ] InsertWaits.ReorderableDeviceLoadStaysInFlightAcrossThreadgroupBarrier
  • git diff --stat 13400438..743fa0d3 -- (the 5 touched files): empty

@RyanTheTide

Copy link
Copy Markdown

a4be23e33aed J413 (M2 Air, Apple M2 / G14G B0, 7.3.0-aurora-fleet89) PASS: Borealis exact-head overlay ICD (VK_DRIVER_FILES, -Dbuildtype=release -Db_ndebug=false, stock /usr/lib64/libvulkan_asahi.so verified byte-identical before/after); deqp-vk 744 PASS / 0 FAIL (api.smoke.* 8/8, compute.pipeline.basic.* 78/81, compute.* 1,10 328/5485, memory_model.* 1,10 282/1839, synchronization.basic.* 21/29, synchronization.op.single_queue.barrier.*compute* 1,5 27/55); 0 GPU faults/recoveries, Enforcing, 0 failed units, 0 new AVC.

@RyanTheTide
RyanTheTide merged commit 5843c63 into aurora-silicon:aurora-wip Nov 27, 2026
joshuaswarren pushed a commit that referenced this pull request Nov 27, 2026
…ait slots on G14X

Add two AGX2 compute-shader scheduling passes gated via agx_chip_caps on G14X (Apple M2 Pro/Max/Ultra), with no new runtime switches:

1. AGX_CAP_COMPUTE_HOIST_LOAD_ADDRS (1u << 28; agx_hoist_loads in agx_pressure_schedule.c, run after agx_ilp_schedule on AGX2 compute): hoists non-coherent device_loads together with up to 3 pure SSA address-producing predecessors (depth <= 4) up to 64 instructions earlier while maintaining a strictly-earlier SSA dominance invariant.

2. AGX_CAP_COMPUTE_SMART_WAITS (1u << 27; agx_insert_waits_local in agx_insert_waits.c): precomputes per-block first-use positions in a single O(N) backward pass (respecting #1232 reorderable loads across threadgroup barriers) and partitions the two hardware scoreboard slots by first-use distance to eliminate mid-burst AGX_MAX_PENDING=8 slot-0 overflow stalls.

Rebased cleanly onto aurora-wip 7617c83 (preserving #1232 drain_on_tg_barrier and #1233 G13X CDM defer flush).

Author-Model: google/gemini-4-neon
joshuaswarren added a commit that referenced this pull request Nov 27, 2026
…ait slots on G14X

Add two AGX2 compute-shader scheduling passes gated via agx_chip_caps on G14X (Apple M2 Pro/Max/Ultra), with no new runtime switches:

1. AGX_CAP_COMPUTE_HOIST_LOAD_ADDRS (1u << 28; agx_hoist_loads in agx_pressure_schedule.c, run after agx_ilp_schedule on AGX2 compute): hoists non-coherent device_loads together with up to 3 pure SSA address-producing predecessors (depth <= 4) up to 64 instructions earlier while maintaining a strictly-earlier SSA dominance invariant.

2. AGX_CAP_COMPUTE_SMART_WAITS (1u << 27; agx_insert_waits_local in agx_insert_waits.c): precomputes per-block first-use positions in a single O(N) backward pass (respecting #1232 reorderable loads across threadgroup barriers) and partitions the two hardware scoreboard slots by first-use distance to eliminate mid-burst AGX_MAX_PENDING=8 slot-0 overflow stalls.

Rebased cleanly onto aurora-wip 7617c83 (preserving #1232 drain_on_tg_barrier and #1233 G13X CDM defer flush).

Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
@DanteCpp
DanteCpp deleted the agx2-reorderable-loads-across-tg-barrier branch December 1, 2026 09:14
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.

2 participants