Skip to content

agx: classify helper/stack ops as barriers and serialize stack_adjust - #951

Merged
DjDeveloperr merged 1 commit into
aurora-wipfrom
fix/agx-scheduler-helper-stack-barriers
Nov 24, 2026
Merged

DjDeveloperr merged 1 commit into
aurora-wipfrom
fix/agx-scheduler-helper-stack-barriers

Conversation

@RyanTheTide

Copy link
Copy Markdown

Problem

On aurora-wip (38833c9317b), native Godot 4.7.2 Forward+ and Proton D3D12/Vulkan apps deadlock the GPU with continuous timeouts on M1 Pro (G13S) and M2 Air (G14G). Bisection pinned this to #927 (ec4746c2006), which enabled agx_ilp_schedule() on AGX2:

  1. In src/asahi/compiler/agx_opcodes.py, doorbell, stack_map, and stack_unmap set can_reorder = False but left schedule_class at its default (none).
  2. In src/asahi/compiler/agx_pressure_schedule.c, agx_ilp_schedule() only checked schedule_class for ordered memory and barriers on AGX_ISA_ENCODING_AGX3C. On AGX2, the pure-ALU loop scheduler reordered libagx_helper's main loop block, hoisting get_sr_barrier (helper_op_id) above doorbell(DB_NEXT) and deadlocking hardware stack/threadgroup allocation requests.
  3. Separately, instr_mem_space() in agx_pressure_schedule.c omitted AGX_OPCODE_STACK_ADJUST from SCHED_MEM_STACK, leaving stack_load and stack_store unordered relative to stack frame adjustment.

Change

  • Set schedule_class = "barrier" on doorbell, stack_map, and stack_unmap in src/asahi/compiler/agx_opcodes.py.
  • Map AGX_OPCODE_STACK_ADJUST to SCHED_MEM_STACK in instr_mem_space() (src/asahi/compiler/agx_pressure_schedule.c).
  • Remove the AGX_ISA_ENCODING_AGX3C chip gate on ordered_memory in agx_ilp_schedule() so the pure-ALU loop scheduler skips blocks containing any ordered LOAD/STORE/ATOMIC/BARRIER uniformly across all Apple GPU generations.
  • Add regression unit tests in src/asahi/compiler/test/test-m5-copy-schedule.cpp (AGX2HelperDoorbellOrderPreserved, StackAdjustSerializesStackAccesses).

Tested: J316s (M1 Pro G13S, fleet87), J413 (M2 Air G14G, fleet87), Neo (J700 A18 Pro G17P, fleet87) at 96b3a74; host meson test --suite asahi 258/258 pass
Risk: two-way · J316s, J413, Neo, and all Apple GPU generations (restores barrier/stack scheduling order in shared compiler)

Still to run

  • AGX3C (M5) regression check when an M5 machine is available (ordered_memory was already active on AGX3C, so behavior there is preserved).
Exact-head hardware A/B proof (base 38833c9 vs head 96b3a74)
Device GPU Kernel Base 38833c9317b Godot 4.7.2 Forward+ Fix 96b3a744cf9 Godot 4.7.2 Forward+ Fix 96b3a744cf9 Godot 4.7.2 Mobile dEQP-VK CTS / Faults
J316s (m1pro) M1 Pro (G13S C0) 7.3.0-aurora-fleet87 HANG (41 GPU timeouts in 15s, 0 frames) PASS 3/3: 59.99 / 60.30 / 59.84 fps, 64/64 px PASS: 112.32 fps, 64/64 px workgroup_memory 10/11 (0 fail), arrays.unnamed_parameter 30/30 (0 fail); 0 new faults, AVC=0
J413 (m2) M2 Air (G14G B0) 7.3.0-aurora-fleet87 HANG (38 GPU timeouts in 15s, 0 frames) PASS: 35.50 fps, 64/64 px PASS: 68.68 fps, 64/64 px 0 new faults
Neo (j700) A18 Pro (G17P B1) 7.3.0-aurora-fleet87 — (AGX3 regression) PASS: 39.55 fps, 64/64 px PASS: 55.69 fps, 64/64 px workgroup_memory 10/11 (0 fail); 0 faults

Exact-head binaries tested (rebuilt with matching asahi_clc / libagx_shaders.h):

  • Base 38833c9317b: sha256:727780af66574b85c84f9040f3b5214a873852e0bfa0315a522f179970b3fcc1
  • Fix 96b3a744cf9: sha256:d62e9538695194d9578da3f37ab7ad1c0bacab2c12cb7ab344fb0193306323bb

Host unit tests:

  • meson test -C build --suite asahi: 258/258 PASS, 0 Fail (agx_tests includes M5CopySchedule.AGX2HelperDoorbellOrderPreserved and M5CopySchedule.StackAdjustSerializesStackAccesses).

In agx_opcodes.py, doorbell, stack_map, and stack_unmap carried
can_reorder = False but left schedule_class at its default (none), and
agx_ilp_schedule() only checked schedule_class for ordered memory and
barriers on AGX3C. After AGX2 enabled agx_ilp_schedule(), the pure-ALU
loop scheduler hoisted get_sr_barrier (helper_op_id) above
doorbell(DB_NEXT) in libagx_helper, deadlocking stack/threadgroup
allocation requests with continuous GPU timeouts on M1/M2 (reproduced
deterministically on Godot 4.7.2 Forward+ and Proton D3D12/Vulkan on
J316s G13S).

In addition, agx_pressure_schedule.c's instr_mem_space() omitted
AGX_OPCODE_STACK_ADJUST from SCHED_MEM_STACK, leaving stack_load and
stack_store unordered relative to stack frame adjustment.

- Give doorbell, stack_map, and stack_unmap schedule_class = barrier.
- Map AGX_OPCODE_STACK_ADJUST to SCHED_MEM_STACK in instr_mem_space().
- Apply the ordered_memory/barrier guard in agx_ilp_schedule()'s
  pure-ALU loop path uniformly across all Apple GPU generations instead
  of gating it on AGX3C.
- Add unit tests in test-m5-copy-schedule.cpp covering helper doorbell
  ordering in AGX2 loops and stack_adjust serialization.

Signed-off-by: Ryan Murray <ryan@aurorasilicon.org>
@RyanTheTide
RyanTheTide requested review from a team and DjDeveloperr as code owners November 24, 2026 14:49

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Verdict: MERGE at 96b3a744cf9f

Blockers: none

Checked: the root cause holds and the fix covers it. create_dag() builds ordering edges from schedule_class alone and ignores can_reorder. So the old doorbell / stack_map / stack_unmap (class none) had no ordering edge against the get_sr_barrier helper-op/arg reads, and any scheduler could move them past those reads. As barrier they now join the full chain. doorbell is only emitted by libagx/helper.cl, so the new class costs nothing in other shaders. Every other pass that reads schedule_class (register cache, M5 word-load window, publication/texture passes, RA) already excluded these ops through can_reorder = False / can_eliminate = False, so nothing else changes. stack_adjust is class store. Before this change it was ordered only against DEVICE-space ops, so stack_load / stack_store could cross it. In SCHED_MEM_STACK it sits in the stack load/store chain. Dropping the AGX3C encoding gate removes a chip gate. The PR merges cleanly into the current aurora-wip (f9b9a5435cf). No PR CI is reported on this branch, so I built and ran the tests myself (results below).

Non-blocking, fine as follow-ups:

  1. test-m5-copy-schedule.cpp StackAdjustSerializesStackAccesses still passes when the AGX_OPCODE_STACK_ADJUST case is removed from instr_mem_space(), so it does not guard that hunk. The block is not a loop and the pressure scheduler keeps the source order here. A test that checks the DAG edge directly, or that gives the scheduler a reason to hoist the store, would catch it.
  2. Removing the gate is not needed for this bug: the barrier class alone keeps the doorbell order, and so does the gate removal alone (mutations below). It is safe, but on AGX2/AGX3B loop ILP is now skipped for any loop block that has a barrier, get_sr_barrier, stack_adjust, a uniform/state store or a pixel wait/signal, and #927 did not measure that. A before/after on one #927 compute workload (Geekbench compute or the xorshift microbench) on J316s or J413, plus a Neo base fps next to the 39.55 / 55.69 numbers, would show the #927 gain survives.
Evidence

Host build: agx_tests at 96b3a744cf9, built in the aurora/mesa-xbuild-x86 image (release, b_ndebug=false, asahi gallium+vulkan, mesa-clc + precomp).

  • Full agx_tests at head: 1375 passed, 1 skipped (NativeFMA.ExactDyadicOracle), 0 failed.

Mutation runs. Each one rebuilds agx_tests and runs the 2 new tests:

Mutation AGX2HelperDoorbellOrderPreserved (order assertion only) StackAdjustSerializesStackAccesses
head PASS PASS
opcodes.py reverted, scheduler at head PASS (gate removal alone also skips loop ILP here) PASS
opcodes.py + agx_pressure_schedule.c at base 38833c9317b FAIL first_sr_pos > doorbell_pos, actual 0 vs 1 PASS
barrier class kept, AGX3C gate restored PASS (barrier class alone keeps the order) PASS
only case AGX_OPCODE_STACK_ADJUST: removed PASS PASS (mutation not killed)

The class EXPECT_EQs at the top of the doorbell test catch the opcodes.py revert on their own, so the doorbell test as written does guard the class change.

Ops that now skip AGX2/AGX3B loop ILP (class load/store/atomic/barrier, not in ilp_block_has_memory()) include threadgroup_barrier, memory_barrier*, device_barrier_*, image_barrier_*, get_sr_barrier, stack_adjust, stack_map/unmap, doorbell, wait_pix/signal_pix, tex_state_store, sampler_state_store. The rest are AGX3/M5-only.

@DjDeveloperr DjDeveloperr 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: MERGE at 96b3a744cf9f

Blockers: none

Checked:

  • src/asahi/compiler/agx_opcodes.py:624-631: doorbell, stack_unmap, and stack_map now carry schedule_class = "barrier", so create_dag() serializes them against all memory spaces (SCHED_MEM_DEVICE, SCHED_MEM_LOCAL, SCHED_MEM_STACK), coverage, and other barriers (get_sr_barrier, memory_barrier, etc.) across both agx_pressure_schedule() and agx_ilp_schedule() (including non-loop compute-load hoisting in libagx_helper's OP_STACK_ALLOC/OP_STACK_FREE blocks). Audited all other schedule_class consumers (agx_compile.c, agx_opt_licm.c, agx_opt_cse.c, agx_dce.c, agx_optimizer.c, agx_opt_register_cache.c, agx_register_allocate.c, agx_insert_waits.c) with no regressions.
  • src/asahi/compiler/agx_pressure_schedule.c:161-164,1888-1898: AGX_OPCODE_STACK_ADJUST (schedule_class = "store") now maps to SCHED_MEM_STACK in instr_mem_space(), serializing stack frame adjustments with stack_load/stack_store via memory_store[SCHED_MEM_STACK]. Removing the AGX_ISA_ENCODING_AGX3C gate on ordered_memory applies the pure-ALU loop guard uniformly across all Apple GPU generations (G13–G18) while keeping !ilp_block_has_memory(block) for NONE/COVERAGE texture and tile ops. agx_chip.h is untouched.
  • src/asahi/compiler/test/test-m5-copy-schedule.cpp:782-818: AGX2HelperDoorbellOrderPreserved directly reproduces the pre-fix agx_ilp_schedule() inversion (hoisting get_sr_barrier above doorbell by critical-path height in an AGX2 loop block) and guards all three schedule_class assignments.
  • Policy §1–§6: removes a chip gate (§1), clean production fix (§2–§3), human author/committer + DCO Signed-off-by with zero AI attribution across commit/title/body/diff (§4), exact-head 96b3a744cf9 hardware A/B on J316s (G13S), J413 (G14G), and Neo (G17P) plus host meson test --suite asahi 258/258 pass and Tested:/Risk: lines (§5), clean naming (§6). Mergeable against aurora-wip (cf5ce9d902c, zero overlapping files with the 80 intervening commits).

Non-blocking notes:

  • src/asahi/compiler/test/test-m5-copy-schedule.cpp:824-862 (StackAdjustSerializesStackAccesses): currently passes even without the AGX_OPCODE_STACK_ADJUST fix because orig_max_pressure == 2 causes pressure_schedule_block() to bail out (max_pressure >= orig_max_pressure), and agx_ilp_schedule() skips non-loop compute blocks with 0 device/local loads and 0 samples. Adding a second overlapping mov_imm + stack_store before stack_adjust(16) (raising orig_max_pressure to 4 so pressure_schedule_block() actively reschedules down to 2) would make it fail without the fix.
  • src/asahi/compiler/agx_pressure_schedule.c:1474: comment on ilp_single_tile_blend() still mentions DOORBELL as an example of schedule_class = NONE.
Evidence
  • PR head verified live before posting: 96b3a744cf9fd1ebb8b1ba01779cb45af6e35f7f (OPEN, non-draft, MERGEABLE into aurora-wip @ cf5ce9d902c).
  • CI status: no check runs reported on branch (same across recent aurora-silicon/mesa PRs); merge gate is review approval.
  • Hardware A/B at 96b3a744cf9 (per PR evidence): J316s G13S and J413 G14G Godot 4.7.2 Forward+ deterministic GPU hangs (41 and 38 timeouts in 15s) resolved to steady passes with 0 new faults and AVC=0; Neo G17P regression pass; host meson test --suite asahi 258/258 pass.

@DjDeveloperr
DjDeveloperr merged commit 62ccb5f into aurora-wip Nov 24, 2026
@DjDeveloperr

Copy link
Copy Markdown

Merged into aurora-wip at 96b3a744cf9f.

Verification summary:

  • Peer review: approved at exact head 96b3a744cf9f (deep audit of agx_opcodes.py, agx_pressure_schedule.c, and all schedule_class consumers; mutation-tested 1,375/0 agx_tests).
  • Hardware A/B at 96b3a744cf9f: resolved deterministic AGX2 GPU hangs on Apple M1 Pro (G13S) and Apple M2 Air (G14G) with 0 faults and 0 AVCs; Apple A18 Pro (G17P) regression pass.
  • Fork-mirror CI (DjDeveloperr/mesa): asahi run 45418930169 PASS, aurora-agent-policy run 45418930326 PASS.

@joshuaswarren joshuaswarren 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 at 96b3a74: APPROVED.

Blockers

None.

Checked

  • doorbell, stack_map, stack_unmap now carry schedule_class = "barrier" in agx_opcodes.py; stack_adjust joins SCHED_MEM_STACK in instr_mem_space, so the pressure scheduler creates ordering dependencies between frame alloc/free and the stack accesses between them.
  • The m5_ordered_memory AGX3C-only gate is widened to ordered_memory across all encodings: any loop block containing a load/store/atomic/barrier-class op now takes the ordered-memory path instead of the pure-ALU ILP scheduling policy. On AGX2 this is what keeps doorbell/get_sr_barrier protocol sequences from being reordered inside loop bodies — the reported deadlock.
  • can_reorder = False was already set on these opcodes but only constrained the earlier scheduling pass, not the ILP loop scheduler — consistent with the hang appearing only under specific loop workloads.
  • Both regression tests run at AGX_ISA_ENCODING_AGX2 / AGX_CHIP_G13G: one asserts doorbell stays ordered before get_sr_barrier, the other asserts strict stack_adjust → stack_store → stack_load → stack_adjust ordering. Both pass on the built tree.
  • Trade-off: AGX2 loop blocks with any ordered-memory op now skip the aggressive ALU-only ILP policy. Correctness over scheduling freedom; the alternative is a deadlocked queue.
Evidence
  • Exact head: 96b3a744cf9fd1ebb8b1ba01779cb45af6e35f7f.
  • aarch64 cross build with asserts on (-Db_ndebug=false), clean: libvulkan_asahi.so produced.
  • asahi+poly suite on Apple MacBook Pro (14-inch, M2 Max, 2023) / AGX2 G14: 183/183 (178 asahi + 5 poly), includes agx_tests with both new ordering tests.
  • GPU stress under the built ICD (VK_ICD_FILENAMES → build artifact): Qwen3.8-9B-Distill-mlx-4Bit decode, 256 tokens — completed cleanly, 43.8 tok/s, rc=0.
  • dmesg sweep after the run: no agx faults, GPU timeouts, or resets.

@RyanTheTide
RyanTheTide deleted the fix/agx-scheduler-helper-stack-barriers branch November 27, 2026 13:20
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