From 1f90096a22189ad74dd06e61c092a48c0c7ffcae Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Mon, 23 Nov 2026 02:29:33 -0500 Subject: [PATCH] asahi/compiler: extend occupancy-capped fragment sample hoisting to AGX3B agx_ilp_schedule() runs fragment sample hoisting on all AGX3 encodings, but occupancy-capped slack was previously gated exclusively on AGX_ISA_ENCODING_AGX3C (m5_fragment). On AGX3B (192-half register file, used by M4 family and A18 Pro): - Fragment blocks with >=4 multi-channel samples fell back to the fixed 16-half AGX_ILP_FS_SAMPLE_SLACK. Hoisting 4-6 vec3/vec4 texture samples (post-processing downsamples, 2D-array texel-fetch passes, deferred DRAM lighting, shadow PCF) exceeds 16 halves of pressure growth even when the resulting peak stays well inside the 104-half (1024-thread) occupancy bucket, causing the scheduler to reject hoisting and serialize samples one at a time. - AGX_OPCODE_IMAGE_LOAD was only recognized as a native SMP sample on AGX3C, even though AGX3B encodes read-only image loads as native SMP with the same latency profile. - Not all AGX3B fragment samples can overlap in flight: in agx3_pack_binary.c, only staging-bank-relocatable samples (plain 2D and uniform/immediate-descriptor 2D_ARRAY samples with auto or immediate LOD, plus 2D/2D_ARRAY/3D immediate-LOD texel/image loads) allocate disjoint staging banks. Non-relocatable forms (including register-descriptor 2D_ARRAY samples when no preamble promotes the descriptors, 3D/cube samples, gradient/register-LOD/dynamic-offset samples, and multisample/register-LOD fetches) stay pinned to staging bank 0, where hoisting only inflates GPR pressure and forces serializing agx3_retire_result_wait barriers between consecutive samples. Extend occupancy-capped fragment sample hoisting and native SMP IMAGE_LOAD recognition to AGX3B while matching its hardware and binary packer constraints: - Restrict AGX3B fragment sample counting and hoisting in ilp_is_sample() to staging-bank-relocatable samples/fetches. - Cap AGX3B fragment occupancy slack at 184 halves (the highest occupancy step inside the 192-half register file, also matching RA's 2D_MS_ARRAY cap) and honor narrower AGX3B cube-sample register budgets (112/160 halves) so hoisting never pushes cube shaders into spilling. - Compute program peak lazily on AGX3B only when a fragment block has >= AGX_ILP_FS_MIN_SAMPLES relocatable samples. - Leave AGX3C (M5), AGX3D, AGX2, and compute scheduling untouched. Signed-off-by: Diljit Singh --- src/asahi/compiler/agx_pressure_schedule.c | 126 +++++++- .../compiler/test/test-m5-copy-schedule.cpp | 274 ++++++++++++++++++ 2 files changed, 398 insertions(+), 2 deletions(-) diff --git a/src/asahi/compiler/agx_pressure_schedule.c b/src/asahi/compiler/agx_pressure_schedule.c index 296db820792f..98aedb5f67b8 100644 --- a/src/asahi/compiler/agx_pressure_schedule.c +++ b/src/asahi/compiler/agx_pressure_schedule.c @@ -649,15 +649,68 @@ agx_pressure_schedule(agx_context *ctx) /* Samples a fragment block may issue ahead of the first unissued one. */ #define AGX_ILP_FS_HOIST_SAMPLES 6 -/* A sample for the sample-issue schedule. M5 encodes a storage-image read as +/* Whether an AGX3B fragment sample or texel fetch can allocate disjoint + * staging banks in agx3_pack_binary.c and genuinely overlap in flight. + * Non-relocatable forms (register-descriptor 2D_ARRAY samples, 3D or cube + * samples, gradient/register-LOD/dynamic-offset samples, and multisample or + * register-LOD fetches) stay pinned to staging bank 0, where hoisting only + * inflates GPR pressure and forces serializing result-wait barriers between + * consecutive samples. */ +static bool +ilp_agx3b_fragment_sample_relocatable(const agx_instr *I) +{ + switch (I->op) { + case AGX_OPCODE_TEXTURE_SAMPLE: { + if (I->nr_srcs < 5 || I->query_lod || (I->offset && !I->offset_xyz)) + return false; + + const bool lod_ok = + I->lod_mode == AGX_LOD_MODE_AUTO_LOD || + ((I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS || + I->lod_mode == AGX_LOD_MODE_LOD_MIN) && + I->src[1].type == AGX_INDEX_IMMEDIATE); + if (!lod_ok) + return false; + + if (I->dim == AGX_DIM_2D) + return true; + + if (I->dim == AGX_DIM_2D_ARRAY) { + return (I->src[3].type == AGX_INDEX_UNIFORM || + I->src[3].type == AGX_INDEX_IMMEDIATE) && + (I->src[4].type == AGX_INDEX_UNIFORM || + I->src[4].type == AGX_INDEX_IMMEDIATE); + } + + return false; + } + case AGX_OPCODE_TEXTURE_LOAD: + case AGX_OPCODE_IMAGE_LOAD: + if (I->nr_srcs < 2 || + (I->dim != AGX_DIM_2D && I->dim != AGX_DIM_2D_ARRAY && + I->dim != AGX_DIM_3D)) + return false; + + return !(I->lod_mode == AGX_LOD_MODE_LOD_MIN && + I->src[1].type != AGX_INDEX_IMMEDIATE); + default: + return false; + } +} + +/* A sample for the sample-issue schedule. AGX3 encodes a storage-image read as * the same native SMP as a texture fetch (Apple's texture.read), so it has * the same latency to hide and is batched the same way. */ static bool ilp_is_sample(const agx_context *ctx, const agx_instr *I) { + if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B && + ctx->stage == MESA_SHADER_FRAGMENT) + return ilp_agx3b_fragment_sample_relocatable(I); + return I->op == AGX_OPCODE_TEXTURE_SAMPLE || I->op == AGX_OPCODE_TEXTURE_LOAD || - (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && + (agx_isa_is_agx3(ctx->key->dev.encoding) && I->op == AGX_OPCODE_IMAGE_LOAD); } @@ -1479,6 +1532,63 @@ ilp_block_peak(agx_context *ctx, agx_block *block, const uint8_t *size16, return out + peak; } +/* Highest occupancy step inside AGX3B's 192-half register file (also matching + * the 2D_MS_ARRAY cap in RA). */ +#define AGX_ILP_AGX3B_MAX_HALVES 184 + +static int +agx_ilp_agx3b_fragment_max_halves(const agx_context *ctx) +{ + /* Respect narrower AGX3B cube-sample register budgets (matching + * agx3_cube_register_budget in agx_register_allocate.c) so sample hoisting + * cannot push a cube shader above its reduced register file. Because + * agx3_reserve_cube_scratch runs after scheduling, cap cube-array and + * bias-min samples conservatively at 112 halves and plain cube samples at + * 160 halves (highest occupancy step inside the 176-half budget). */ + int max_halves = AGX_ILP_AGX3B_MAX_HALVES; + agx_foreach_instr_global(ctx, I) { + if (I->op != AGX_OPCODE_TEXTURE_SAMPLE || I->nr_srcs < 6 || + (I->dim != AGX_DIM_CUBE && I->dim != AGX_DIM_CUBE_ARRAY)) + continue; + + if (I->dim == AGX_DIM_CUBE_ARRAY || + I->lod_mode == AGX_LOD_MODE_AUTO_LOD_BIAS_MIN) + max_halves = MIN2(max_halves, 112); + else + max_halves = MIN2(max_halves, 160); + } + return max_halves; +} + +static signed +agx_ilp_agx3b_fragment_cap(agx_context *ctx, uint8_t **size16_out, void *memctx) +{ + uint8_t *size16 = *size16_out; + if (!size16) { + size16 = rzalloc_array(memctx, uint8_t, MAX2(ctx->alloc, 1)); + if (!size16) + return 0; + agx_foreach_instr_global(ctx, I) { + agx_foreach_ssa_dest(I, d) + size16[I->dest[d].value] = agx_index_size_16(I->dest[d]); + } + *size16_out = size16; + } + + signed program_peak = 0; + agx_foreach_block(ctx, block) + program_peak = MAX2(program_peak, + ilp_block_peak(ctx, block, size16, memctx)); + + const int max_halves = agx_ilp_agx3b_fragment_max_halves(ctx); + if (program_peak + AGX_ILP_M5_OCCUPANCY_MARGIN > max_halves) + return 0; + + return agx_occupancy_for_register_count( + program_peak + AGX_ILP_M5_OCCUPANCY_MARGIN).max_registers - + AGX_ILP_M5_OCCUPANCY_MARGIN; +} + void agx_ilp_schedule(agx_context *ctx) { @@ -1489,6 +1599,8 @@ agx_ilp_schedule(agx_context *ctx) struct sched_dag_state dag_state = {0}; bool native_uniforms = false; + bool agx3b_frag_cap_ready = false; + signed agx3b_frag_cap = 0; agx_foreach_instr_global(ctx, I) { native_uniforms |= native_uniform_write(I); } @@ -1726,6 +1838,16 @@ agx_ilp_schedule(agx_context *ctx) if (m5_fragment) { const signed peak = ilp_block_peak(ctx, block, size16, memctx); slack = MAX2(slack, m5_occupancy_cap - peak); + } else if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B) { + if (!agx3b_frag_cap_ready) { + agx3b_frag_cap = + agx_ilp_agx3b_fragment_cap(ctx, &size16, memctx); + agx3b_frag_cap_ready = true; + } + if (size16) { + const signed peak = ilp_block_peak(ctx, block, size16, memctx); + slack = MAX2(slack, agx3b_frag_cap - peak); + } } ilp_schedule_block(ctx, block, memctx, native_uniforms, alu_latency, 16 * alu_latency, slack, 0, false, diff --git a/src/asahi/compiler/test/test-m5-copy-schedule.cpp b/src/asahi/compiler/test/test-m5-copy-schedule.cpp index 98cdddd60cfb..8937cc828fc5 100644 --- a/src/asahi/compiler/test/test-m5-copy-schedule.cpp +++ b/src/asahi/compiler/test/test-m5-copy-schedule.cpp @@ -392,3 +392,277 @@ TEST(M5CopySchedule, ComputeDeviceLoadsFollowTheProgramPeak) ralloc_free(s.mem); } } + +namespace { + +enum frag_sample_kind { + FRAG_SAMPLE_TEX, + FRAG_SAMPLE_IMAGE_LOAD, + FRAG_SAMPLE_TEX_LOAD, +}; + +enum frag_desc_kind { + FRAG_DESC_IMM, + FRAG_DESC_UNIFORM, + FRAG_DESC_REG, +}; + +static unsigned +emit_frag_samples_and_count(enum agx_isa_encoding enc, enum agx_chip chip, + enum agx_dim dim, enum frag_sample_kind kind, + enum frag_desc_kind desc_kind, + enum agx_lod_mode lod_mode, bool reg_lod, + bool with_cube_array, unsigned prefix_live_32) +{ + copy_shader s; + copy_shader_init(&s, MESA_SHADER_FRAGMENT, false); + s.key.dev.encoding = enc; + s.key.dev.chip = chip; + + agx_builder *b = s.b; + if (prefix_live_32 > 0) { + /* Heavy entry block setting program_peak, followed by a light sample + * block so the program's occupancy/budget ceiling directly controls + * whether the light block's 5 vec4 samples may hoist. */ + agx_index held[96]; + assert(prefix_live_32 <= 96); + for (unsigned i = 0; i < prefix_live_32; ++i) + held[i] = agx_mov_imm(b, 32, 1000 + i); + agx_index psum = agx_mov_imm(b, 32, 0); + for (unsigned i = 0; i < prefix_live_32; ++i) + psum = agx_iadd(b, psum, held[i], 0); + agx_unit_test(b, psum); + + agx_block *samp_blk = agx_test_block(b->shader); + agx_block_add_successor(s.body, samp_blk); + s.body = samp_blk; + } + + b->cursor = agx_after_block(s.body); + agx_index uv = agx_preload(b, agx_register(2, AGX_SIZE_32)); + agx_index reg_desc = agx_preload(b, agx_register(6, AGX_SIZE_64)); + agx_index reg_smp = agx_preload(b, agx_register(10, AGX_SIZE_64)); + agx_index reg_lod_val = agx_preload(b, agx_register(14, AGX_SIZE_32)); + agx_index acc = agx_mov_imm(b, 32, 0); + + if (with_cube_array) { + /* Place a single cube-array sample in a predecessor block so the shader + * carries the 112-half cube-array register budget while the sample block + * holds 5 relocatable 2D samples. */ + agx_index c1 = agx_mov_imm(b, 32, 1); + agx_index c2 = agx_mov_imm(b, 32, 2); + agx_index c3 = agx_mov_imm(b, 32, 3); + agx_index ccoords = agx_vec_temp(b->shader, AGX_SIZE_32, 4); + agx_instr *cc = agx_collect_to(b, ccoords, 4); + cc->src[0] = uv; + cc->src[1] = c1; + cc->src[2] = c2; + cc->src[3] = c3; + agx_index ctex = agx_vec_temp(b->shader, AGX_SIZE_32, 4); + agx_texture_sample_to( + b, ctex, ccoords, agx_immediate(0), agx_immediate(0), + agx_immediate(0), agx_immediate(0), agx_null(), AGX_DIM_CUBE_ARRAY, + AGX_LOD_MODE_AUTO_LOD, 0xf, false, false, false, AGX_GATHER_NONE, + false); + agx_instr *csp = agx_split(b, 4, ctex); + for (unsigned k = 0; k < 4; ++k) + csp->dest[k] = agx_temp(b->shader, AGX_SIZE_32); + agx_unit_test(b, csp->dest[0]); + + agx_block *samp_blk = agx_test_block(b->shader); + agx_block_add_successor(s.body, samp_blk); + s.body = samp_blk; + b->cursor = agx_after_block(s.body); + } + + const unsigned coord_comps = + (dim == AGX_DIM_CUBE_ARRAY) ? 4 + : (dim == AGX_DIM_2D_ARRAY || dim == AGX_DIM_3D || dim == AGX_DIM_CUBE || + dim == AGX_DIM_2D_MS) + ? 3 + : 2; + for (unsigned i = 0; i < 5; ++i) { + agx_index csrc[4]; + csrc[0] = uv; + for (unsigned k = 1; k < coord_comps; ++k) + csrc[k] = agx_mov_imm(b, 32, i + k); + agx_index coords = agx_vec_temp(b->shader, AGX_SIZE_32, coord_comps); + agx_instr *c = agx_collect_to(b, coords, coord_comps); + for (unsigned k = 0; k < coord_comps; ++k) + c->src[k] = csrc[k]; + + agx_index lod = agx_immediate(0); + if (lod_mode == AGX_LOD_MODE_LOD_GRAD) { + agx_index gsrc[4]; + for (unsigned k = 0; k < 4; ++k) + gsrc[k] = agx_mov_imm(b, 32, k + 1); + lod = agx_vec_temp(b->shader, AGX_SIZE_32, 4); + agx_instr *gc = agx_collect_to(b, lod, 4); + for (unsigned k = 0; k < 4; ++k) + gc->src[k] = gsrc[k]; + } else if (reg_lod) { + lod = reg_lod_val; + } + + agx_index tex = (desc_kind == FRAG_DESC_UNIFORM) + ? agx_uniform(4, AGX_SIZE_64) + : (desc_kind == FRAG_DESC_REG) ? reg_desc + : agx_immediate(i); + agx_index smp = (desc_kind == FRAG_DESC_UNIFORM) + ? agx_uniform(8, AGX_SIZE_64) + : (desc_kind == FRAG_DESC_REG) ? reg_smp + : agx_immediate(0); + + agx_index texel = agx_vec_temp(b->shader, AGX_SIZE_32, 4); + if (kind == FRAG_SAMPLE_IMAGE_LOAD) { + agx_instr *ld = agx_image_load_to( + b, texel, coords, lod, agx_zero(), tex, agx_immediate(0), + agx_null(), dim, lod_mode, 0, false, false, false); + ld->mask = 0xf; + } else if (kind == FRAG_SAMPLE_TEX_LOAD) { + agx_texture_load_to( + b, texel, coords, lod, agx_zero(), tex, agx_immediate(0), + agx_null(), dim, lod_mode, 0xf, false, false); + } else { + agx_texture_sample_to( + b, texel, coords, lod, agx_immediate(0), tex, smp, agx_null(), dim, + lod_mode, 0xf, false, false, false, AGX_GATHER_NONE, false); + } + + agx_instr *sp = agx_split(b, 4, texel); + for (unsigned k = 0; k < 4; ++k) { + agx_index ch = agx_temp(b->shader, AGX_SIZE_32); + sp->dest[k] = ch; + acc = agx_fadd(b, acc, ch); + } + } + agx_unit_test(b, acc); + agx_validate(b->shader, "agx3b fragment schedule test"); + + agx_ilp_schedule(b->shader); + + unsigned samples_before_fadd = 0; + agx_foreach_instr_in_block(s.body, I) { + if (I->op == AGX_OPCODE_FADD) + break; + samples_before_fadd += + (I->op == AGX_OPCODE_TEXTURE_SAMPLE || + I->op == AGX_OPCODE_TEXTURE_LOAD || I->op == AGX_OPCODE_IMAGE_LOAD); + } + ralloc_free(s.mem); + return samples_before_fadd; +} + +} /* namespace */ + +/* AGX3B fragment shaders with >=4 multi-channel relocatable samples hoist all + * samples ahead of their consumers when the growth fits within the 1024-thread + * occupancy bucket (104 halves), even when it exceeds the fixed 16-half slack. */ +TEST(M5CopySchedule, AGX3BFragmentSamplesUseOccupancySlack) +{ + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D, + FRAG_SAMPLE_TEX, FRAG_DESC_IMM, AGX_LOD_MODE_AUTO_LOD, false, + false, 0), + 5u); + /* Uniform-descriptor 2D_ARRAY samples allocate disjoint staging banks on + * AGX3B (relocatable_2d_array) and hoist inside the occupancy bucket. */ + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D_ARRAY, + FRAG_SAMPLE_TEX, FRAG_DESC_UNIFORM, AGX_LOD_MODE_AUTO_LOD, + false, false, 0), + 5u); +} + +/* AGX3B fragment shaders recognize relocatable AGX_OPCODE_IMAGE_LOAD (2D and + * immediate-LOD 2D_ARRAY) as native SMP samples and hoist them within the + * occupancy step. */ +TEST(M5CopySchedule, AGX3BFragmentImageLoadsHoist) +{ + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D, + FRAG_SAMPLE_IMAGE_LOAD, FRAG_DESC_IMM, AGX_LOD_MODE_AUTO_LOD, + false, false, 0), + 5u); + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G16G, AGX_DIM_2D_ARRAY, + FRAG_SAMPLE_IMAGE_LOAD, FRAG_DESC_REG, AGX_LOD_MODE_AUTO_LOD, + false, false, 0), + 5u); +} + +/* Non-relocatable AGX3B fragment samples stay pinned to staging bank 0 in + * agx3_pack_binary.c, so hoisting them would only inflate GPRs and emit + * serializing wait barriers. Keep them in pressure order. */ +TEST(M5CopySchedule, AGX3BFragmentNonRelocatableSamplesStayInOrder) +{ + /* Register-descriptor 2D_ARRAY samples (e.g. G16G without preamble or + * bindless 2D_ARRAY) are non-relocatable on AGX3B. */ + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G16G, AGX_DIM_2D_ARRAY, + FRAG_SAMPLE_TEX, FRAG_DESC_REG, AGX_LOD_MODE_AUTO_LOD, false, + false, 0), + 1u); + /* 3D, cube-array, gradient-LOD samples, multisample image loads, and + * register-LOD texture loads also keep staging bank 0 on AGX3B. */ + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_3D, + FRAG_SAMPLE_TEX, FRAG_DESC_IMM, AGX_LOD_MODE_AUTO_LOD, false, + false, 0), + 1u); + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_CUBE_ARRAY, + FRAG_SAMPLE_TEX, FRAG_DESC_IMM, AGX_LOD_MODE_AUTO_LOD, false, + false, 0), + 1u); + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D, + FRAG_SAMPLE_TEX, FRAG_DESC_IMM, AGX_LOD_MODE_LOD_GRAD, false, + false, 0), + 1u); + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D_MS, + FRAG_SAMPLE_IMAGE_LOAD, FRAG_DESC_IMM, AGX_LOD_MODE_AUTO_LOD, + false, false, 0), + 1u); + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D, + FRAG_SAMPLE_TEX_LOAD, FRAG_DESC_IMM, AGX_LOD_MODE_LOD_MIN, true, + false, 0), + 1u); +} + +/* AGX3B fragment occupancy cap stops at 184 halves (highest step in the + * 192-half file): a program at ~160 halves may still hoist a light block + * inside the 184-half step, while one past 176 halves falls back to fixed + * 16-half slack and rejects the 5-sample hoist. */ +TEST(M5CopySchedule, AGX3BFragmentRespects184HalfCeiling) +{ + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D, + FRAG_SAMPLE_TEX, FRAG_DESC_IMM, AGX_LOD_MODE_AUTO_LOD, false, + false, 80), + 5u); + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D, + FRAG_SAMPLE_TEX, FRAG_DESC_IMM, AGX_LOD_MODE_AUTO_LOD, false, + false, 90), + 1u); +} + +/* AGX3B shaders with a cube-array sample carry a 112-half register budget: + * relocatable 2D samples in the same shader may hoist below 104 halves, but + * are refused when the program already exceeds the 112-half ceiling. */ +TEST(M5CopySchedule, AGX3BFragmentCubeBudgetRespected) +{ + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D, + FRAG_SAMPLE_TEX, FRAG_DESC_IMM, AGX_LOD_MODE_AUTO_LOD, false, + true, 0), + 5u); + EXPECT_EQ(emit_frag_samples_and_count( + AGX_ISA_ENCODING_AGX3B, AGX_CHIP_G17P, AGX_DIM_2D, + FRAG_SAMPLE_TEX, FRAG_DESC_IMM, AGX_LOD_MODE_AUTO_LOD, false, + true, 80), + 1u); +}