From 96b3a744cf9fd1ebb8b1ba01779cb45af6e35f7f Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Wed, 25 Nov 2026 01:44:22 +1100 Subject: [PATCH] agx: classify helper/stack ops as barriers and serialize stack_adjust 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 --- src/asahi/compiler/agx_opcodes.py | 9 +- src/asahi/compiler/agx_pressure_schedule.c | 27 +++--- .../compiler/test/test-m5-copy-schedule.cpp | 85 +++++++++++++++++++ 3 files changed, 104 insertions(+), 17 deletions(-) diff --git a/src/asahi/compiler/agx_opcodes.py b/src/asahi/compiler/agx_opcodes.py index 621ecf0d16f3..4cc2c8df79f7 100644 --- a/src/asahi/compiler/agx_opcodes.py +++ b/src/asahi/compiler/agx_opcodes.py @@ -622,10 +622,13 @@ def memory_barrier(name, a, b, c): memory_barrier("device_barrier_2", 3, 2, 9) op("doorbell", (0x60020 | 0x28 << 32, 6, _), dests = 0, - can_eliminate = False, can_reorder = False, imms = [IMM]) + can_eliminate = False, can_reorder = False, imms = [IMM], + schedule_class = "barrier") -op("stack_unmap", (0x00075, 8, _), dests = 1, srcs = 0, can_eliminate = False, can_reorder = False, imms = [IMM]) -op("stack_map", (0x10075, 8, _), dests = 0, srcs = 1, can_eliminate = False, can_reorder = False, imms = [IMM]) +op("stack_unmap", (0x00075, 8, _), dests = 1, srcs = 0, can_eliminate = False, + can_reorder = False, imms = [IMM], schedule_class = "barrier") +op("stack_map", (0x10075, 8, _), dests = 0, srcs = 1, can_eliminate = False, + can_reorder = False, imms = [IMM], schedule_class = "barrier") op("stack_adjust", encoding = (0x10100b5, 8, _), dests = 0, srcs = 0, can_eliminate = False, can_reorder = False, diff --git a/src/asahi/compiler/agx_pressure_schedule.c b/src/asahi/compiler/agx_pressure_schedule.c index b152c59642ab..14038e071953 100644 --- a/src/asahi/compiler/agx_pressure_schedule.c +++ b/src/asahi/compiler/agx_pressure_schedule.c @@ -158,6 +158,7 @@ instr_mem_space(const agx_instr *I) case AGX_OPCODE_LOCAL_STORE: case AGX_OPCODE_LOCAL_ATOMIC: return SCHED_MEM_LOCAL; + case AGX_OPCODE_STACK_ADJUST: case AGX_OPCODE_STACK_LOAD: case AGX_OPCODE_STACK_STORE: return SCHED_MEM_STACK; @@ -1881,22 +1882,20 @@ agx_ilp_schedule(agx_context *ctx) } continue; } - /* The old ALU-only classifier predates native SCS traversal. On M5, - * treat every ordered memory operation and completion barrier as - * memory, keeping RT polling out of the ALU loop scheduling policy. */ - bool m5_ordered_memory = false; - if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C) { - agx_foreach_instr_in_block(block, I) { - const enum agx_schedule_class cls = - agx_opcodes_info[I->op].schedule_class; - m5_ordered_memory |= cls == AGX_SCHEDULE_CLASS_LOAD || - cls == AGX_SCHEDULE_CLASS_STORE || - cls == AGX_SCHEDULE_CLASS_ATOMIC || - cls == AGX_SCHEDULE_CLASS_BARRIER; - } + /* Treat every ordered memory operation and barrier as memory across all + * generations so pure-ALU loop scheduling never reorders protocol/helper + * barriers, stack adjustments, or RT polling blocks. */ + bool ordered_memory = false; + agx_foreach_instr_in_block(block, I) { + const enum agx_schedule_class cls = + agx_opcodes_info[I->op].schedule_class; + ordered_memory |= cls == AGX_SCHEDULE_CLASS_LOAD || + cls == AGX_SCHEDULE_CLASS_STORE || + cls == AGX_SCHEDULE_CLASS_ATOMIC || + cls == AGX_SCHEDULE_CLASS_BARRIER; } if (in_loop[block->index] && !ilp_block_has_memory(block) && - !m5_ordered_memory) { + !ordered_memory) { ilp_schedule_block(ctx, block, memctx, native_uniforms, alu_latency, 4 * alu_latency, AGX_ILP_SLACK, 0, NULL, is_agx3, false, 0, 0, false, false, false, &dag_state); diff --git a/src/asahi/compiler/test/test-m5-copy-schedule.cpp b/src/asahi/compiler/test/test-m5-copy-schedule.cpp index 2a8839d6f42b..964aafd6fd24 100644 --- a/src/asahi/compiler/test/test-m5-copy-schedule.cpp +++ b/src/asahi/compiler/test/test-m5-copy-schedule.cpp @@ -775,3 +775,88 @@ TEST(M5CopySchedule, AGX3BFragmentCubeBudgetRespected) true, 80), 1u); } + +/* Helper-kernel protocol ops (doorbell, stack_map, stack_unmap) must carry + * AGX_SCHEDULE_CLASS_BARRIER and never be reordered past get_sr_barrier inside + * AGX2 loop bodies (libagx_helper deadlock regression). */ +TEST(M5CopySchedule, AGX2HelperDoorbellOrderPreserved) +{ + EXPECT_EQ(agx_opcodes_info[AGX_OPCODE_DOORBELL].schedule_class, + AGX_SCHEDULE_CLASS_BARRIER); + EXPECT_EQ(agx_opcodes_info[AGX_OPCODE_STACK_MAP].schedule_class, + AGX_SCHEDULE_CLASS_BARRIER); + EXPECT_EQ(agx_opcodes_info[AGX_OPCODE_STACK_UNMAP].schedule_class, + AGX_SCHEDULE_CLASS_BARRIER); + + copy_shader s; + copy_shader_init(&s, MESA_SHADER_COMPUTE, true); + s.key.dev.encoding = AGX_ISA_ENCODING_AGX2; + s.key.dev.chip = AGX_CHIP_G13G; + + agx_builder *b = s.b; + b->cursor = agx_after_block(s.body); + agx_doorbell(b, 0x20); + agx_index op = agx_get_sr_barrier(b, 32, (enum agx_sr)0x90); + agx_index arg = agx_get_sr_barrier(b, 32, (enum agx_sr)0x92); + agx_index sum = agx_iadd(b, op, arg, 0); + agx_if_icmp(b, sum, agx_zero(), 1, AGX_ICOND_UEQ, false, s.body); + + agx_pressure_schedule(b->shader); + agx_ilp_schedule(b->shader); + + int doorbell_pos = -1, first_sr_pos = -1, pos = 0; + agx_foreach_instr_in_block(s.body, I) { + if (I->op == AGX_OPCODE_DOORBELL) + doorbell_pos = pos; + else if (I->op == AGX_OPCODE_GET_SR_BARRIER && first_sr_pos < 0) + first_sr_pos = pos; + ++pos; + } + EXPECT_GE(doorbell_pos, 0); + EXPECT_GT(first_sr_pos, doorbell_pos); + ralloc_free(s.mem); +} + +/* stack_adjust must live in SCHED_MEM_STACK so stack_load/stack_store stay + * strictly ordered between frame allocation and deallocation. */ +TEST(M5CopySchedule, StackAdjustSerializesStackAccesses) +{ + copy_shader s; + copy_shader_init(&s, MESA_SHADER_COMPUTE, false); + s.key.dev.encoding = AGX_ISA_ENCODING_AGX2; + s.key.dev.chip = AGX_CHIP_G13G; + + agx_builder *b = s.b; + b->cursor = agx_after_block(s.body); + agx_index val = agx_mov_imm(b, 32, 0x1234); + agx_index off = agx_zero(); + agx_stack_adjust(b, 16); + agx_stack_store(b, val, off, AGX_FORMAT_I32, 1); + agx_index loaded = agx_stack_load(b, off, AGX_FORMAT_I32, 1); + agx_stack_adjust(b, -16); + agx_device_store(b, loaded, agx_uniform(0, AGX_SIZE_64), agx_zero(), + AGX_FORMAT_I32, 1, 0, false); + + agx_pressure_schedule(b->shader); + agx_ilp_schedule(b->shader); + + int adj_alloc = -1, st_pos = -1, ld_pos = -1, adj_free = -1, pos = 0; + agx_foreach_instr_in_block(s.body, I) { + if (I->op == AGX_OPCODE_STACK_ADJUST) { + if (adj_alloc < 0) + adj_alloc = pos; + else + adj_free = pos; + } else if (I->op == AGX_OPCODE_STACK_STORE) { + st_pos = pos; + } else if (I->op == AGX_OPCODE_STACK_LOAD) { + ld_pos = pos; + } + ++pos; + } + EXPECT_GE(adj_alloc, 0); + EXPECT_GT(st_pos, adj_alloc); + EXPECT_GT(ld_pos, st_pos); + EXPECT_GT(adj_free, ld_pos); + ralloc_free(s.mem); +}