diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index fe9512862639..c49858c5151f 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -1094,9 +1094,10 @@ agx_unpack_integer_8_agx3(agx_builder *b, agx_index dest, { agx_index channels[4] = {agx_null()}; - /* AGX3C: unpack each byte in one 16-bit operation: even bytes mask + /* AGX3B/AGX3C: unpack each byte in one 16-bit operation: even bytes mask * the low byte and odd bytes shift right by 8, clearing the upper byte. */ - if (b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && + if ((b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3B || + b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3C) && !b->shader->key->dev.emit_agx3d) { for (unsigned i = 0; i < nr_channels; ++i) { channels[i] = (i & 1) ? agx_ushr(b, halfs[i / 2], agx_immediate(8)) @@ -2143,6 +2144,30 @@ agx_emit_store(agx_builder *b, nir_intrinsic_instr *instr) assert(nr_packed == DIV_ROUND_UP(nr, 2)); value = agx_emit_collect(b, nr_packed, packed); mask = BITFIELD_MASK(nr); + } else if ((b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3B || + b->shader->key->dev.encoding == AGX_ISA_ENCODING_AGX3C) && + !b->shader->key->dev.emit_agx3d && + fmt == AGX_FORMAT_I8 && + nir_src_bit_size(instr->src[0]) == 8 && + nir_src_num_components(instr->src[0]) == 1) { + /* A scalar W8 device_store writes only the low 8 bits of its 16-bit + * source register, so skip an explicit u2u8 0xff mask when feeding one. */ + nir_scalar sc_val = nir_scalar_resolved(instr->src[0].ssa, 0); + if (nir_scalar_is_alu(sc_val) && + nir_scalar_alu_op(sc_val) == nir_op_u2u8) { + nir_scalar wide = nir_scalar_chase_alu_src(sc_val, 0); + if (wide.def->num_components == 1 && wide.def->bit_size == 16) { + value = agx_def_index(wide.def); + } else if (wide.def->num_components == 1 && wide.def->bit_size == 32) { + value = agx_temp(b->shader, AGX_SIZE_16); + agx_subdivide_to(b, value, agx_def_index(wide.def), 0); + } else { + value = agx_recollect_vector(b, instr->src[0]); + } + } else { + value = agx_recollect_vector(b, instr->src[0]); + } + mask = 1; } else { value = agx_recollect_vector(b, instr->src[0]); mask = BITFIELD_MASK(nir_src_num_components(instr->src[0])); @@ -4761,17 +4786,20 @@ agx_lower_agx3_half_converts(agx_context *ctx) I->src[0].value != AGX_CONVERT_S8_TO_F))) continue; - /* M5 converts a signed byte to a half or a float natively (every - * byte value at every position of a half, both result widths, tested - * on G17C). G18 keeps the halfword fallback below. */ - if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && !is_g18g && + /* AGX3B/AGX3C convert signed bytes to f16/f32 and unsigned bytes to f32 + * natively (verified by exhaustive sweep of every byte value and + * position on G17C and G17P B1 hardware). G18 keeps the halfword + * fallback below. */ + if ((ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B || + ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C) && !is_g18g && I->src[0].value == AGX_CONVERT_S8_TO_F && I->round == AGX_ROUND_RTE && (I->dest[0].size == AGX_SIZE_16 || I->dest[0].size == AGX_SIZE_32) && I->src[1].size == AGX_SIZE_16 && !I->src[1].abs && !I->src[1].neg && !I->src[1].memory) continue; - if (ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && !is_g18g && + if ((ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B || + ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C) && !is_g18g && I->src[0].value == AGX_CONVERT_U8_TO_F && I->round == AGX_ROUND_RTE && I->dest[0].size == AGX_SIZE_32 && I->src[1].size == AGX_SIZE_16 && !I->src[1].abs && !I->src[1].neg && !I->src[1].memory) @@ -16563,11 +16591,23 @@ agx_compile_shader_nir_once(nir_shader *nir, struct agx_shader_key *key, assign_coefficient_regs(nir, &info->varyings.fs); } - if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C) { + const bool agx3b_compute = + key->dev.encoding == AGX_ISA_ENCODING_AGX3B && !key->dev.emit_agx3d && + mesa_shader_stage_is_compute(nir->info.stage); + + if (agx3b_compute) { + bool unswitched_if = false; + NIR_PASS(unswitched_if, nir, agx_nir_opt_unswitch_invariant_if); + if (unswitched_if) + nir_divergence_analysis(nir); + } + + if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C || agx3b_compute) { /* Hoist only what the register file can hold through the loop: the * file a workgroup allows (agx_ra), less what spilling reserves. */ - unsigned regs = ROUND_DOWN_TO(AGX3C_NUM_REGS, 8); + unsigned regs = ROUND_DOWN_TO( + agx3b_compute ? AGX3B_NUM_REGS : AGX3C_NUM_REGS, 8); if (mesa_shader_stage_is_compute(nir->info.stage)) { const unsigned threads = nir->info.workgroup_size_variable ? 1024 @@ -16576,7 +16616,19 @@ agx_compile_shader_nir_once(nir_shader *nir, struct agx_shader_key *key, regs = MIN2(regs, agx_max_registers_for_occupancy(threads)); } - NIR_PASS(_, nir, agx_nir_opt_licm, regs - 8); + unsigned licm_budget = regs - 8; + bool licm_speculative = false; + if (agx3b_compute) { + /* Empirically cap AGX3B compute LICM at 64 half-registers: while + * agx_max_registers_for_occupancy allows up to 104 halves, capping + * hoisted live-in demand at 64 halves avoids Dynamic Caching and + * register-pressure regressions measured on G17P compute loops. + * Enable speculative pure-ALU/preamble hoisting on non-internal + * compute shaders. */ + licm_budget = MIN2(licm_budget, 64); + licm_speculative = !nir->info.internal; + } + NIR_PASS(_, nir, agx_nir_opt_licm, licm_budget, licm_speculative); } if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C) { @@ -16585,7 +16637,7 @@ agx_compile_shader_nir_once(nir_shader *nir, struct agx_shader_key *key, NIR_PASS(_, nir, nir_opt_dce); } - if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C) { + if (key->dev.encoding == AGX_ISA_ENCODING_AGX3C || agx3b_compute) { NIR_PASS(_, nir, agx_nir_opt_canonical_minmax); NIR_PASS(_, nir, nir_opt_dce); } diff --git a/src/asahi/compiler/agx_nir.h b/src/asahi/compiler/agx_nir.h index 200994b1b5be..89f99b39c452 100644 --- a/src/asahi/compiler/agx_nir.h +++ b/src/asahi/compiler/agx_nir.h @@ -94,7 +94,7 @@ struct nir_def *agx_nir_quad_ddy(struct nir_builder *b, struct nir_def *v); * still guarantees it for any AGX3 target. */ bool agx_nir_lower_shared_element_index(struct nir_shader *s); bool agx_nir_opt_shared_byte_offsets(struct nir_shader *s); -bool agx_nir_opt_licm(struct nir_shader *shader, unsigned budget); +bool agx_nir_opt_licm(struct nir_shader *shader, unsigned budget, bool speculative); bool agx_nir_lower_fminmax(struct nir_shader *s, bool ftz_only); bool agx_nir_lower_fminmax_signed_zero(struct nir_shader *s, bool ftz_only); bool agx_nir_lower_word_bounds(struct nir_shader *s); @@ -373,6 +373,7 @@ bool agx_nir_opt_uniform_stride(struct nir_shader *shader); bool agx_nir_fold_m5_offsets(struct nir_shader *shader); bool agx_nir_unroll_runtime_loops(struct nir_shader *shader, unsigned factor, unsigned max_instrs, unsigned *widest); +bool agx_nir_opt_unswitch_invariant_if(struct nir_shader *shader); bool agx_nir_unroll_and_jam(struct nir_shader *shader, unsigned factor, unsigned *widest); bool agx_nir_opt_u8_absdiff_fp(struct nir_shader *shader); diff --git a/src/asahi/compiler/agx_nir_opt_guard_selected_loads.c b/src/asahi/compiler/agx_nir_opt_guard_selected_loads.c index aaaa31203d12..2ffdcabac8b7 100644 --- a/src/asahi/compiler/agx_nir_opt_guard_selected_loads.c +++ b/src/asahi/compiler/agx_nir_opt_guard_selected_loads.c @@ -503,6 +503,17 @@ same_base_ptr(nir_def *a, nir_def *b) nir_intrinsic_base(ina) == nir_intrinsic_base(inb)) return true; } + if (ia->type == nir_instr_type_alu && ib->type == nir_instr_type_alu) { + nir_alu_instr *aa = nir_instr_as_alu(ia); + nir_alu_instr *ab = nir_instr_as_alu(ib); + if (aa->op == nir_op_pack_64_2x32_split && + ab->op == nir_op_pack_64_2x32_split) { + return atom_cmp(scalar_atom(nir_scalar_resolved(aa->src[0].src.ssa, aa->src[0].swizzle[0])), + scalar_atom(nir_scalar_resolved(ab->src[0].src.ssa, ab->src[0].swizzle[0]))) == 0 && + atom_cmp(scalar_atom(nir_scalar_resolved(aa->src[1].src.ssa, aa->src[1].swizzle[0])), + scalar_atom(nir_scalar_resolved(ab->src[1].src.ssa, ab->src[1].swizzle[0]))) == 0; + } + } return false; } diff --git a/src/asahi/compiler/agx_nir_opt_licm.c b/src/asahi/compiler/agx_nir_opt_licm.c index d1588fee180d..798a18ffd383 100644 --- a/src/asahi/compiler/agx_nir_opt_licm.c +++ b/src/asahi/compiler/agx_nir_opt_licm.c @@ -155,9 +155,48 @@ instr_invariant(nir_instr *instr, struct invariance *inv) } } +/* Pure, side-effect-free instructions safe to hoist speculatively out of + * loops whose body follows a header exit test (e.g. `if (i >= n) break;`). + */ +static bool +instr_speculatable(nir_instr *instr, nir_loop *loop) +{ + /* Only speculatively hoist from top-level loop blocks (e.g. loop body + * following a header break), not from inside conditional branches. + */ + if (instr->block->cf_node.parent != &loop->cf_node) + return false; + + if (instr->type == nir_instr_type_intrinsic) + return nir_instr_as_intrinsic(instr)->intrinsic == + nir_intrinsic_load_preamble; + + if (instr->type != nir_instr_type_alu) + return false; + + switch (nir_instr_as_alu(instr)->op) { + case nir_op_idiv: + case nir_op_udiv: + case nir_op_imod: + case nir_op_umod: + case nir_op_irem: + return false; + default: + return true; + } +} + +static bool +agx_licm_filter(nir_instr *instr, nir_loop *loop, bool block_dominates_exit) +{ + return block_dominates_exit || instr_speculatable(instr, loop); +} + static bool -loop_hoists(nir_loop *loop) +loop_hoists(nir_loop *loop, bool *has_terminate) { + *has_terminate = false; + if (!nir_loop_has_back_edge(loop)) return false; @@ -171,7 +210,7 @@ loop_hoists(nir_loop *loop) nir_intrinsic_terminate || nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_terminate_if)) - return false; + *has_terminate = true; } } @@ -205,7 +244,8 @@ use_stays_in_loop(struct licm_estimate *est, nir_src *use, nir_loop *loop) /* Halves of hoisted results that instructions staying in the loop read */ static unsigned -loop_hoist_cost(struct licm_estimate *est, unsigned ssa_alloc, nir_loop *loop) +loop_hoist_cost(struct licm_estimate *est, unsigned ssa_alloc, nir_loop *loop, + bool has_terminate, bool speculative) { struct invariance inv = {est, loop}; nir_block *exit = nir_loop_successor_block(loop); @@ -214,12 +254,12 @@ loop_hoist_cost(struct licm_estimate *est, unsigned ssa_alloc, nir_loop *loop) memset(est->invariant, 0, BITSET_BYTES(ssa_alloc)); nir_foreach_block_in_cf_node(block, &loop->cf_node) { - if (!nir_block_dominates(block, exit)) - continue; + bool dom_exit = !has_terminate && nir_block_dominates(block, exit); nir_foreach_instr(instr, block) { nir_def *def = nir_instr_def(instr); - if (def && instr_invariant(instr, &inv)) + if (def && instr_invariant(instr, &inv) && + (dom_exit || (speculative && instr_speculatable(instr, loop)))) BITSET_SET(est->invariant, def->index); } } @@ -244,27 +284,29 @@ loop_hoist_cost(struct licm_estimate *est, unsigned ssa_alloc, nir_loop *loop) static bool loops_fit(struct licm_estimate *est, unsigned ssa_alloc, - struct exec_list *list, unsigned budget) + struct exec_list *list, unsigned budget, bool speculative) { foreach_list_typed(nir_cf_node, node, node, list) { switch (node->type) { case nir_cf_node_if: { nir_if *nif = nir_cf_node_as_if(node); - if (!loops_fit(est, ssa_alloc, &nif->then_list, budget) || - !loops_fit(est, ssa_alloc, &nif->else_list, budget)) + if (!loops_fit(est, ssa_alloc, &nif->then_list, budget, speculative) || + !loops_fit(est, ssa_alloc, &nif->else_list, budget, speculative)) return false; break; } case nir_cf_node_loop: { nir_loop *loop = nir_cf_node_as_loop(node); - if (loop_hoists(loop)) { - unsigned cost = loop_hoist_cost(est, ssa_alloc, loop); + bool has_terminate = false; + if (loop_hoists(loop, &has_terminate) && (!has_terminate || speculative)) { + unsigned cost = + loop_hoist_cost(est, ssa_alloc, loop, has_terminate, speculative); if (cost && loop_peak(est, loop) + cost > budget) return false; } - if (!loops_fit(est, ssa_alloc, &loop->body, budget)) + if (!loops_fit(est, ssa_alloc, &loop->body, budget, speculative)) return false; break; } @@ -282,7 +324,7 @@ loops_fit(struct licm_estimate *est, unsigned ssa_alloc, * halves with the invariants hoisted. */ bool -agx_nir_opt_licm(nir_shader *shader, unsigned budget) +agx_nir_opt_licm(nir_shader *shader, unsigned budget, bool speculative) { nir_foreach_function_impl(impl, shader) { nir_metadata_require(impl, nir_metadata_block_index | @@ -302,12 +344,12 @@ agx_nir_opt_licm(nir_shader *shader, unsigned budget) nir_foreach_def(instr, record_halves, &est); } - bool fits = loops_fit(&est, impl->ssa_alloc, &impl->body, budget); + bool fits = loops_fit(&est, impl->ssa_alloc, &impl->body, budget, speculative); ralloc_free(mem_ctx); if (!fits) return false; } - return nir_opt_licm(shader, NULL); + return nir_opt_licm(shader, speculative ? agx_licm_filter : NULL); } diff --git a/src/asahi/compiler/agx_nir_unroll_runtime_loops.c b/src/asahi/compiler/agx_nir_unroll_runtime_loops.c index e646edd293b5..03ac7009633e 100644 --- a/src/asahi/compiler/agx_nir_unroll_runtime_loops.c +++ b/src/asahi/compiler/agx_nir_unroll_runtime_loops.c @@ -1645,3 +1645,239 @@ agx_nir_unroll_runtime_loops(nir_shader *shader, unsigned factor, } return any; } + +static bool +pure_loop_invariant(nir_def *def, nir_loop *loop, unsigned depth) +{ + if (def_outside_loop(def, loop)) + return true; + if (depth == 0) + return false; + nir_instr *instr = nir_def_instr(def); + switch (instr->type) { + case nir_instr_type_load_const: + case nir_instr_type_undef: + return true; + case nir_instr_type_alu: { + nir_alu_instr *alu = nir_instr_as_alu(instr); + for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; ++i) + if (!pure_loop_invariant(alu->src[i].src.ssa, loop, depth - 1)) + return false; + return true; + } + case nir_instr_type_intrinsic: { + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + if (intr->intrinsic == nir_intrinsic_load_preamble || + intr->intrinsic == nir_intrinsic_load_push_constant) { + const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic]; + for (unsigned i = 0; i < info->num_srcs; ++i) + if (!pure_loop_invariant(intr->src[i].ssa, loop, depth - 1)) + return false; + return true; + } + return false; + } + default: + return false; + } +} + +static bool +cf_list_has_jump(struct exec_list *list) +{ + foreach_list_typed(nir_cf_node, node, node, list) { + if (node->type == nir_cf_node_block) { + if (nir_block_ends_in_jump(nir_cf_node_as_block(node))) + return true; + } else if (node->type == nir_cf_node_if) { + nir_if *nif = nir_cf_node_as_if(node); + if (cf_list_has_jump(&nif->then_list) || + cf_list_has_jump(&nif->else_list)) + return true; + } else if (node->type == nir_cf_node_loop) { + if (cf_list_has_jump(&nir_cf_node_as_loop(node)->body)) + return true; + } + } + return false; +} + +static void +count_arm_work(struct exec_list *list, nir_loop *loop, unsigned *total, + unsigned *inv_alu) +{ + foreach_list_typed(nir_cf_node, node, node, list) { + if (node->type == nir_cf_node_block) { + nir_foreach_instr(instr, nir_cf_node_as_block(node)) { + if (instr->type == nir_instr_type_load_const || + instr->type == nir_instr_type_undef || + instr->type == nir_instr_type_phi) + continue; + (*total)++; + if (instr->type == nir_instr_type_alu && + pure_loop_invariant(&nir_instr_as_alu(instr)->def, loop, 8)) + (*inv_alu)++; + } + } else if (node->type == nir_cf_node_if) { + nir_if *nif = nir_cf_node_as_if(node); + count_arm_work(&nif->then_list, loop, total, inv_alu); + count_arm_work(&nif->else_list, loop, total, inv_alu); + } + } +} + +/* Duplicating a loop around an invariant condition uses the same + * extract/clone/reinsert transform as outer-loop splitting, so reuse its + * structural guards: no derefs/barriers/atomics/subgroups/return/halt + * (splittable_outer), no non-ALU live-outs (live_outs_are_alu), and no phis + * in the loop successor block (which would keep stale break-block sources + * once the loop moves into outer_if->then_list). + * + * The 192-instruction cap is 3/4 of the 256-instruction outer-loop split cap + * so duplicating an innermost loop stays well within instruction-cache limits. */ +static bool +loop_safe_to_unswitch_if(nir_loop *loop) +{ + if (!nir_loop_has_back_edge(loop) || loop_has_nested_loop(&loop->body)) + return false; + if (nir_loop_has_continue_construct(loop) || count_instrs(loop) > 192) + return false; + + nir_block *after = nir_cf_node_as_block(nir_cf_node_next(&loop->cf_node)); + if (nir_block_first_instr(after) && + nir_block_first_instr(after)->type == nir_instr_type_phi) + return false; + + if (!splittable_outer(loop) || !live_outs_are_alu(loop)) + return false; + + nir_foreach_block_in_cf_node(block, &loop->cf_node) { + if (nir_block_ends_in_return_or_halt(block)) + return false; + } + return true; +} + +bool +agx_nir_opt_unswitch_invariant_if(nir_shader *shader) +{ + bool any = false; + + nir_foreach_function_impl(impl, shader) { + nir_metadata_require(impl, nir_metadata_block_index); + nir_index_ssa_defs(impl); + + struct util_dynarray loops; + util_dynarray_init(&loops, NULL); + collect_loops(&impl->body, &loops); + + bool impl_progress = false; + nir_builder b = nir_builder_create(impl); + + util_dynarray_foreach(&loops, nir_loop *, lp) { + nir_loop *loop = *lp; + if (!loop_safe_to_unswitch_if(loop)) + continue; + + nir_if *target = NULL; + unsigned target_ord = 0, ord = 0; + foreach_list_typed(nir_cf_node, node, node, &loop->body) { + if (node->type != nir_cf_node_if) + continue; + nir_if *nif = nir_cf_node_as_if(node); + unsigned cur_ord = ord++; + if (cf_list_has_jump(&nif->then_list) || + cf_list_has_jump(&nif->else_list)) + continue; + /* Depth 8 matches outer_invariant() in outer-loop splitting. */ + if (!pure_loop_invariant(nif->condition.ssa, loop, 8)) + continue; + + /* Require at least one substantial arm (>= 12 ops, so a small + * select/copy is not worth duplicating the loop for) and at least + * 2 loop-invariant ALU/preamble ops exposed across the arms for + * subsequent LICM. */ + unsigned t_tot = 0, t_inv = 0, e_tot = 0, e_inv = 0; + count_arm_work(&nif->then_list, loop, &t_tot, &t_inv); + count_arm_work(&nif->else_list, loop, &e_tot, &e_inv); + if ((t_tot < 12 && e_tot < 12) || (t_inv + e_inv) < 2) + continue; + + target = nif; + target_ord = cur_ord; + break; + } + if (!target) + continue; + + b.cursor = nir_before_cf_node(&loop->cf_node); + struct hash_table *remap = _mesa_pointer_hash_table_create(NULL); + nir_def *cond = remat_outer(&b, target->condition.ssa, loop, remap); + _mesa_hash_table_destroy(remap, NULL); + + nir_foreach_block_in_cf_node(block, &loop->cf_node) + nir_lower_phis_to_regs_block(block, true); + nir_foreach_block_in_cf_node(block, &loop->cf_node) + nir_lower_ssa_defs_to_regs_block(block); + + b.cursor = nir_before_cf_node(&loop->cf_node); + nir_if *outer_if = nir_push_if(&b, cond); + nir_pop_if(&b, outer_if); + + nir_cf_list orig_list, clone_list; + nir_cf_extract(&orig_list, nir_before_cf_node(&loop->cf_node), + nir_after_cf_node(&loop->cf_node)); + struct hash_table *cmap = _mesa_pointer_hash_table_create(NULL); + nir_cf_list_clone(&clone_list, &orig_list, &outer_if->cf_node, cmap); + _mesa_hash_table_destroy(cmap, NULL); + + nir_cf_reinsert(&orig_list, nir_before_cf_list(&outer_if->then_list)); + nir_cf_reinsert(&clone_list, nir_before_cf_list(&outer_if->else_list)); + + for (unsigned arm = 0; arm < 2; ++arm) { + struct exec_list *arm_list = + arm == 0 ? &outer_if->then_list : &outer_if->else_list; + nir_loop *arm_loop = NULL; + foreach_list_typed(nir_cf_node, n, node, arm_list) { + if (n->type == nir_cf_node_loop) { + arm_loop = nir_cf_node_as_loop(n); + break; + } + } + assert(arm_loop); + unsigned k = 0; + foreach_list_typed(nir_cf_node, n, node, &arm_loop->body) { + if (n->type != nir_cf_node_if) + continue; + if (k++ == target_ord) { + nir_if *arm_if = nir_cf_node_as_if(n); + b.cursor = nir_before_cf_node(&arm_if->cf_node); + nir_src_rewrite(&arm_if->condition, + nir_imm_bool(&b, arm == 0)); + break; + } + } + } + + impl_progress = true; + /* Re-index after modifying CF before considering outer loops. */ + nir_progress(true, impl, nir_metadata_none); + nir_lower_reg_intrinsics_to_ssa_impl(impl); + nir_metadata_require(impl, nir_metadata_block_index); + nir_index_ssa_defs(impl); + } + + util_dynarray_fini(&loops); + if (impl_progress) + any = true; + } + + if (any) { + NIR_PASS(_, shader, nir_opt_dead_cf); + NIR_PASS(_, shader, nir_opt_copy_prop); + NIR_PASS(_, shader, nir_opt_dce); + NIR_PASS(_, shader, nir_opt_cse); + } + + return any; +} diff --git a/src/asahi/compiler/agx_optimizer.c b/src/asahi/compiler/agx_optimizer.c index 73cc1cf8f0e1..5ad70ad0be3f 100644 --- a/src/asahi/compiler/agx_optimizer.c +++ b/src/asahi/compiler/agx_optimizer.c @@ -962,7 +962,9 @@ agx_optimizer_copyprop(agx_context *ctx, agx_instr **defs, agx_instr *I) /* Preserve an aligned 64-bit device-load base assembled from * adjacent uniform words. The native form reads those same words. */ - if (ctx->key && I->op == AGX_OPCODE_DEVICE_LOAD && s == 0 && + if (ctx->key && + ((I->op == AGX_OPCODE_DEVICE_LOAD && s == 0) || + (I->op == AGX_OPCODE_DEVICE_STORE && s == 1)) && src.size == AGX_SIZE_64 && agx_channels(src) == 1 && !src.abs && !src.neg && !src.memory && def->op == AGX_OPCODE_COLLECT && def->nr_srcs == 2 && @@ -971,26 +973,22 @@ agx_optimizer_copyprop(agx_context *ctx, agx_instr **defs, agx_instr *I) !def->saturate) { const agx_index lo = def->src[0], hi = def->src[1]; const bool m5_uniform_base = - ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C; + ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3C && + I->op == AGX_OPCODE_DEVICE_LOAD; - /* On AGX3B a scalar W8 load and four packed W8 components both - * return one physical word, and the LDDEV encoding reads an aligned - * base directly from the low uniform file. Both are properties of - * the encoding, so this holds on every AGX3B part. */ - const bool agx3b_w8_uniform_base = + /* On AGX3B, LDDEV and STDEV both encode an aligned 64-bit base + * directly from the low uniform file across all widths and masks. */ + const bool agx3b_uniform_base = ctx->key->dev.encoding == AGX_ISA_ENCODING_AGX3B && + !ctx->key->dev.emit_agx3d && ctx->stage == MESA_SHADER_COMPUTE && !ctx->is_preamble && ctx->nir && !ctx->nir->info.internal && !ctx->key->secondary && - I->format == AGX_FORMAT_I8 && !I->coherent && - I->nr_dests == 1 && I->dest[0].size == AGX_SIZE_32 && - agx_channels(I->dest[0]) == 1 && - ((!I->memory_is_packed && I->mask == 0x1) || - (I->memory_is_packed && I->mask == 0xf)) && + !I->coherent && !src.kill && !src.cache && !src.discard && !lo.kill && !lo.cache && !lo.discard && !hi.kill && !hi.cache && !hi.discard; - if ((m5_uniform_base || agx3b_w8_uniform_base) && + if ((m5_uniform_base || agx3b_uniform_base) && lo.type == AGX_INDEX_UNIFORM && hi.type == AGX_INDEX_UNIFORM && lo.size == AGX_SIZE_32 && hi.size == AGX_SIZE_32 && agx_channels(lo) == 1 && agx_channels(hi) == 1 && diff --git a/src/asahi/compiler/test/test-agx3-vision-opts.cpp b/src/asahi/compiler/test/test-agx3-vision-opts.cpp index c06b7c4199ae..85f6914788fd 100644 --- a/src/asahi/compiler/test/test-agx3-vision-opts.cpp +++ b/src/asahi/compiler/test/test-agx3-vision-opts.cpp @@ -6,6 +6,7 @@ #include "util/format/u_format.h" extern "C" { #include "agx_nir.h" +#include "agx_compiler.h" } static nir_builder @@ -1515,3 +1516,483 @@ TEST(AGX3VisionOpts, Exact3x3OuterPeelAndGuards) ralloc_free(b.shader); } } + +TEST(AGX3VisionOpts, Wave4InvariantIfUnswitch) +{ + for (int mode = 0; mode < 4; ++mode) { + /* mode 0: positive (invariant if with >= 12 work instrs & >= 2 invariant ALU) + * mode 1: negative (loop-variant condition) + * mode 2: negative (arm contains break) + * mode 3: negative (loop contains barrier) + */ + nir_builder b = make_compute_shader("wave4_test"); + nir_def *base = nir_load_preamble(&b, 1, 64, .base = 0); + nir_def *out = nir_load_preamble(&b, 1, 64, .base = 4); + nir_def *n = nir_load_preamble(&b, 1, 32, .base = 8); + nir_def *y = nir_load_preamble(&b, 1, 32, .base = 10); + nir_def *stride = nir_load_preamble(&b, 1, 32, .base = 12); + nir_def *flag = nir_ine_imm(&b, nir_load_preamble(&b, 1, 32, .base = 14), 0); + + nir_variable *x_var = + nir_local_variable_create(b.impl, glsl_int_type(), "x"); + nir_store_var(&b, x_var, nir_imm_int(&b, 0), 1); + + nir_loop *loop = nir_push_loop(&b); + { + nir_def *x = nir_load_var(&b, x_var); + nir_if *ex = nir_push_if(&b, nir_ige(&b, x, n)); + nir_jump(&b, nir_jump_break); + nir_pop_if(&b, ex); + + if (mode == 3) { + nir_barrier(&b, .execution_scope = SCOPE_WORKGROUP, + .memory_scope = SCOPE_WORKGROUP, + .memory_semantics = NIR_MEMORY_ACQ_REL, + .memory_modes = nir_var_mem_shared); + } + + nir_def *cond = (mode == 1) ? nir_ilt(&b, x, y) : flag; + nir_if *nif = nir_push_if(&b, cond); + { + if (mode == 2) { + nir_jump(&b, nir_jump_break); + } else { + nir_def *yf = nir_i2f32(&b, y); + nir_def *ys = nir_fmul_imm(&b, yf, 0.5f); + nir_def *y0 = nir_f2i32(&b, ys); + nir_def *y1 = nir_iadd_imm(&b, y0, 1); + nir_def *r0 = nir_imul(&b, y0, stride); + nir_def *r1 = nir_imul(&b, y1, stride); + nir_def *xf = nir_i2f32(&b, x); + nir_def *xs = nir_fmul_imm(&b, xf, 0.5f); + nir_def *x0 = nir_f2i32(&b, xs); + nir_def *v0 = nir_load_agx(&b, 1, 8, base, nir_iadd(&b, r0, x0), + .access = (gl_access_qualifier)(ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER), + .format = PIPE_FORMAT_R8_UINT); + nir_def *v1 = nir_load_agx(&b, 1, 8, base, nir_iadd(&b, r1, x0), + .access = (gl_access_qualifier)(ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER), + .format = PIPE_FORMAT_R8_UINT); + nir_def *sum = nir_iadd(&b, nir_u2u32(&b, v0), nir_u2u32(&b, v1)); + nir_def *avg = nir_ushr_imm(&b, sum, 1); + nir_store_agx(&b, nir_u2u8(&b, avg), out, nir_iadd(&b, nir_imul(&b, y, stride), x), + .format = PIPE_FORMAT_R8_UINT); + } + } + nir_push_else(&b, nif); + { + nir_def *idx = nir_iadd(&b, nir_imul(&b, y, stride), x); + nir_def *v = nir_load_agx(&b, 1, 8, base, idx, + .access = (gl_access_qualifier)(ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER), + .format = PIPE_FORMAT_R8_UINT); + nir_store_agx(&b, v, out, idx, .format = PIPE_FORMAT_R8_UINT); + } + nir_pop_if(&b, nif); + + nir_store_var(&b, x_var, nir_iadd_imm(&b, x, 1), 1); + } + nir_pop_loop(&b, loop); + + NIR_PASS(_, b.shader, nir_lower_vars_to_ssa); + NIR_PASS(_, b.shader, nir_opt_dce); + bool changed = agx_nir_opt_unswitch_invariant_if(b.shader); + nir_validate_shader(b.shader, "unswitch invariant if"); + + unsigned loops = 0, max_depth = 0; + count_loop_shape(b.impl, &loops, &max_depth); + if (mode == 0) { + EXPECT_TRUE(changed); + EXPECT_EQ(loops, 2u); + EXPECT_EQ(max_depth, 1u); + } else { + EXPECT_FALSE(changed); + EXPECT_EQ(loops, 1u); + } + ralloc_free(b.shader); + } +} + +TEST(AGX3VisionOpts, Wave4Pack64StoreLoadForward) +{ + for (int match = 0; match < 2; ++match) { + nir_builder b = make_compute_shader("wave4_test"); + nir_def *lo0 = nir_load_preamble(&b, 1, 32, .base = 0); + nir_def *hi0 = nir_load_preamble(&b, 1, 32, .base = 2); + nir_def *lo1 = nir_load_preamble(&b, 1, 32, .base = 0); + nir_def *hi1 = nir_load_preamble(&b, 1, 32, .base = match ? 2 : 4); + nir_def *base0 = nir_pack_64_2x32_split(&b, lo0, hi0); + nir_def *base1 = nir_pack_64_2x32_split(&b, lo1, hi1); + nir_def *idx = nir_load_preamble(&b, 1, 32, .base = 8); + nir_def *val = nir_u2u8(&b, nir_load_preamble(&b, 1, 32, .base = 10)); + nir_def *out = nir_load_preamble(&b, 1, 64, .base = 12); + + nir_store_agx(&b, val, base0, idx, .format = PIPE_FORMAT_R8_UINT); + nir_def *reloaded = nir_load_agx(&b, 1, 8, base1, idx, + .format = PIPE_FORMAT_R8_UINT); + nir_store_agx(&b, reloaded, out, idx, .format = PIPE_FORMAT_R8_UINT); + + bool changed = agx_nir_opt_forward_merge_loads(b.shader); + nir_opt_dce(b.shader); + nir_validate_shader(b.shader, "pack64 store load forward"); + + if (match) { + EXPECT_TRUE(changed); + EXPECT_EQ(count_intrinsics(b.shader, nir_intrinsic_load_agx), 0u); + } else { + EXPECT_FALSE(changed); + EXPECT_EQ(count_intrinsics(b.shader, nir_intrinsic_load_agx), 1u); + } + ralloc_free(b.shader); + } +} + +TEST(AGX3VisionOpts, Wave4SpeculativeLicm) +{ + for (int spec = 0; spec < 2; ++spec) { + nir_builder b = make_compute_shader("wave4_test"); + nir_def *base = nir_load_preamble(&b, 1, 64, .base = 0); + nir_def *out = nir_load_preamble(&b, 1, 64, .base = 4); + nir_def *n = nir_load_preamble(&b, 1, 32, .base = 8); + nir_def *y = nir_load_preamble(&b, 1, 32, .base = 10); + nir_def *stride = nir_load_preamble(&b, 1, 32, .base = 12); + + nir_variable *x_var = + nir_local_variable_create(b.impl, glsl_int_type(), "x"); + nir_store_var(&b, x_var, nir_imm_int(&b, 0), 1); + + nir_loop *loop = nir_push_loop(&b); + { + nir_def *x = nir_load_var(&b, x_var); + nir_if *ex = nir_push_if(&b, nir_ige(&b, x, n)); + nir_jump(&b, nir_jump_break); + nir_pop_if(&b, ex); + + /* Top-level invariant ALU: should hoist only when spec == 1. */ + nir_def *row = nir_imul(&b, y, stride); + /* Top-level invariant load: must NEVER speculatively hoist past break. */ + nir_def *inv_ld = nir_load_agx(&b, 1, 32, base, row, + .access = (gl_access_qualifier)(ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER), + .format = PIPE_FORMAT_R32_UINT); + /* Nested-if invariant ALU: must NEVER speculatively hoist from branch. */ + nir_variable *acc = + nir_local_variable_create(b.impl, glsl_uint_type(), "acc"); + nir_store_var(&b, acc, inv_ld, 1); + nir_if *cond = nir_push_if(&b, nir_ieq_imm(&b, x, 0)); + { + nir_def *cold = nir_iadd_imm(&b, nir_imul(&b, y, y), 7); + nir_store_var(&b, acc, nir_iadd(&b, inv_ld, cold), 1); + } + nir_pop_if(&b, cond); + + nir_store_agx(&b, nir_load_var(&b, acc), out, nir_iadd(&b, row, x), + .format = PIPE_FORMAT_R32_UINT); + nir_store_var(&b, x_var, nir_iadd_imm(&b, x, 1), 1); + } + nir_pop_loop(&b, loop); + + NIR_PASS(_, b.shader, nir_lower_vars_to_ssa); + NIR_PASS(_, b.shader, nir_opt_dce); + agx_nir_opt_licm(b.shader, 64, spec != 0); + nir_validate_shader(b.shader, "speculative licm"); + + unsigned imul_in_loop = 0, loads_in_loop = 0; + nir_foreach_block(block, b.impl) { + bool in_loop = false; + for (nir_cf_node *n = block->cf_node.parent; n; n = n->parent) { + if (n->type == nir_cf_node_loop) + in_loop = true; + } + if (!in_loop) + continue; + nir_foreach_instr(instr, block) { + if (instr->type == nir_instr_type_alu && + nir_instr_as_alu(instr)->op == nir_op_imul) + imul_in_loop++; + if (instr->type == nir_instr_type_intrinsic && + nir_instr_as_intrinsic(instr)->intrinsic == nir_intrinsic_load_agx) + loads_in_loop++; + } + } + + /* Invariant load must always stay inside the loop. */ + EXPECT_EQ(loads_in_loop, 1u); + /* Top-level imul hoists only when speculative; nested-if imul stays. */ + EXPECT_EQ(imul_in_loop, spec ? 1u : 2u); + ralloc_free(b.shader); + } +} + +TEST(AGX3VisionOpts, Wave4InvariantIfUnswitchLiveOutAndTwoBreaks) +{ + /* Case A: ALU live-out value used after the loop -> unswitches cleanly and + * preserves valid NIR. */ + { + nir_builder b = make_compute_shader("unswitch_live_out"); + nir_def *base = nir_load_preamble(&b, 1, 64, .base = 0); + nir_def *out_base = nir_load_preamble(&b, 1, 64, .base = 4); + nir_def *flag = nir_load_preamble(&b, 1, 32, .base = 8); + nir_def *k = nir_load_preamble(&b, 1, 32, .base = 10); + nir_def *n = nir_load_preamble(&b, 1, 32, .base = 12); + nir_def *cond = nir_ine_imm(&b, flag, 0); + + nir_variable *iv_v = nir_local_variable_create(b.impl, glsl_uint_type(), "iv"); + nir_variable *acc_v = nir_local_variable_create(b.impl, glsl_uint_type(), "acc"); + nir_store_var(&b, iv_v, nir_imm_int(&b, 0), 1); + nir_store_var(&b, acc_v, nir_imm_int(&b, 0), 1); + + nir_loop *loop = nir_push_loop(&b); + nir_def *iv = nir_load_var(&b, iv_v); + nir_if *brk = nir_push_if(&b, nir_ige(&b, iv, n)); + nir_jump(&b, nir_jump_break); + nir_pop_if(&b, brk); + + nir_if *inv_if = nir_push_if(&b, cond); + nir_def *t = nir_iadd_imm(&b, k, 3); + nir_def *t2 = nir_imul_imm(&b, t, 5); + nir_def *v = nir_load_agx(&b, 1, 32, base, iv, + .access = (gl_access_qualifier)(ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER), + .format = PIPE_FORMAT_R32_UINT); + for (int i = 0; i < 12; ++i) + v = nir_iadd(&b, v, t2); + nir_store_var(&b, acc_v, nir_iadd(&b, nir_load_var(&b, acc_v), v), 1); + nir_push_else(&b, inv_if); + nir_store_var(&b, acc_v, nir_iadd(&b, nir_load_var(&b, acc_v), k), 1); + nir_pop_if(&b, inv_if); + + nir_store_var(&b, iv_v, nir_iadd_imm(&b, iv, 1), 1); + nir_pop_loop(&b, loop); + + /* Live-out ALU accumulator used after the loop */ + nir_def *final_acc = nir_load_var(&b, acc_v); + nir_store_agx(&b, final_acc, out_base, nir_imm_int(&b, 0), + .format = PIPE_FORMAT_R32_UINT); + + nir_lower_vars_to_ssa(b.shader); + nir_opt_dce(b.shader); + nir_validate_shader(b.shader, "before unswitch live_out"); + bool progress = agx_nir_opt_unswitch_invariant_if(b.shader); + nir_validate_shader(b.shader, "after unswitch live_out"); + EXPECT_TRUE(progress); + ralloc_free(b.shader); + } + + /* Case B: Loop with two breaks feeding a successor-block phi -> must be + * refused safely so the exit phi never references stale break blocks. */ + { + nir_builder b = make_compute_shader("unswitch_two_breaks"); + nir_def *base = nir_load_preamble(&b, 1, 64, .base = 0); + nir_def *out_base = nir_load_preamble(&b, 1, 64, .base = 4); + nir_def *flag = nir_load_preamble(&b, 1, 32, .base = 8); + nir_def *k = nir_load_preamble(&b, 1, 32, .base = 10); + nir_def *n = nir_load_preamble(&b, 1, 32, .base = 12); + nir_def *cond = nir_ine_imm(&b, flag, 0); + + nir_variable *iv_v = nir_local_variable_create(b.impl, glsl_uint_type(), "iv"); + nir_variable *res_v = nir_local_variable_create(b.impl, glsl_uint_type(), "res"); + nir_store_var(&b, iv_v, nir_imm_int(&b, 0), 1); + nir_store_var(&b, res_v, nir_imm_int(&b, 0), 1); + + nir_loop *loop = nir_push_loop(&b); + nir_def *iv = nir_load_var(&b, iv_v); + nir_if *brk1 = nir_push_if(&b, nir_ige(&b, iv, n)); + nir_store_var(&b, res_v, nir_imm_int(&b, 1), 1); + nir_jump(&b, nir_jump_break); + nir_pop_if(&b, brk1); + + nir_if *inv_if = nir_push_if(&b, cond); + nir_def *t = nir_iadd_imm(&b, k, 3); + nir_def *t2 = nir_imul_imm(&b, t, 5); + nir_def *v = nir_load_agx(&b, 1, 32, base, iv, + .access = (gl_access_qualifier)(ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER), + .format = PIPE_FORMAT_R32_UINT); + for (int i = 0; i < 12; ++i) + v = nir_iadd(&b, v, t2); + nir_store_var(&b, res_v, v, 1); + nir_push_else(&b, inv_if); + nir_store_var(&b, res_v, k, 1); + nir_pop_if(&b, inv_if); + + nir_if *brk2 = nir_push_if(&b, nir_ieq_imm(&b, nir_load_var(&b, res_v), 42)); + nir_store_var(&b, res_v, nir_imm_int(&b, 2), 1); + nir_jump(&b, nir_jump_break); + nir_pop_if(&b, brk2); + + nir_store_var(&b, iv_v, nir_iadd_imm(&b, iv, 1), 1); + nir_pop_loop(&b, loop); + + nir_store_agx(&b, nir_load_var(&b, res_v), out_base, nir_imm_int(&b, 0), + .format = PIPE_FORMAT_R32_UINT); + + nir_lower_vars_to_ssa(b.shader); + nir_opt_dce(b.shader); + nir_validate_shader(b.shader, "before unswitch two_breaks"); + bool progress = agx_nir_opt_unswitch_invariant_if(b.shader); + nir_validate_shader(b.shader, "after unswitch two_breaks"); + EXPECT_FALSE(progress); + ralloc_free(b.shader); + } +} + +TEST(AGX3VisionOpts, Wave4Agx3bCodegenW8ConvertAndUniformBase) +{ + unsigned instrs_by_mode[4] = {}; + + for (int mode = 0; mode < 4; ++mode) { + /* mode 0: aligned low uniform bases (u0:u1, u4:u5), R8_UINT vec4 unpack + u2f32 + u2u8 store + * mode 1: aligned low uniform bases, R8_SINT -> i2f16 & i2f32 + * mode 2: high/unaligned uniform bases (u2:u3, u256:u257) + * mode 3: coherent store with aligned low uniform base + */ + nir_builder b = make_compute_shader("wave4_test"); + b.shader->info.internal = false; + int in_base_u = (mode == 2) ? 2 : 0; + int out_base_u = (mode == 2) ? 256 : 4; + nir_def *in_base = nir_pack_64_2x32_split( + &b, nir_load_preamble(&b, 1, 32, .base = in_base_u), + nir_load_preamble(&b, 1, 32, .base = in_base_u + 2)); + nir_def *out_base = nir_pack_64_2x32_split( + &b, nir_load_preamble(&b, 1, 32, .base = out_base_u), + nir_load_preamble(&b, 1, 32, .base = out_base_u + 2)); + nir_def *idx = nir_load_preamble(&b, 1, 32, .base = 12); + + if (mode == 1) { + nir_def *sv = nir_load_agx(&b, 2, 8, in_base, idx, + .access = (gl_access_qualifier)(ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER), + .format = PIPE_FORMAT_R8_UINT); + nir_def *f16 = nir_i2f16(&b, nir_channel(&b, sv, 0)); + nir_def *f32 = nir_i2f32(&b, nir_channel(&b, sv, 1)); + nir_def *sum = nir_fadd(&b, nir_f2f32(&b, f16), f32); + nir_store_agx(&b, sum, out_base, idx, .format = PIPE_FORMAT_R32_UINT); + } else { + nir_def *uv = nir_load_agx(&b, 4, 8, in_base, idx, + .access = (gl_access_qualifier)(ACCESS_NON_WRITEABLE | ACCESS_CAN_REORDER), + .format = PIPE_FORMAT_R8_UINT); + nir_def *acc = nir_u2f32(&b, nir_channel(&b, uv, 0)); + acc = nir_fadd(&b, acc, nir_u2f32(&b, nir_channel(&b, uv, 1))); + acc = nir_fadd(&b, acc, nir_u2f32(&b, nir_channel(&b, uv, 2))); + acc = nir_fadd(&b, acc, nir_u2f32(&b, nir_channel(&b, uv, 3))); + nir_def *byte_out = nir_u2u8(&b, nir_f2u32(&b, acc)); + gl_access_qualifier st_acc = + (mode == 3) ? ACCESS_COHERENT : (gl_access_qualifier)0; + nir_store_agx(&b, byte_out, out_base, idx, + .access = st_acc, .format = PIPE_FORMAT_R8_UINT); + } + + struct agx_shader_key key = {}; + key.dev.chip = AGX_CHIP_G17P; + key.dev.encoding = AGX_ISA_ENCODING_AGX3B; + key.has_scratch = true; + key.no_preamble = true; + + agx_preprocess_nir(b.shader); + struct agx_shader_part part = {}; + EXPECT_TRUE(agx_compile_shader_nir(b.shader, &key, &part)); + EXPECT_GT(part.info.main_size, 0u); + EXPECT_EQ(part.info.scratch_size, 0u); + instrs_by_mode[mode] = part.info.stats.instrs; + free(part.binary); + ralloc_free(b.shader); + } + + /* Mode 0 (folded aligned low uniforms) must emit fewer instructions than + * mode 2 (unaligned/high uniform bases) and mode 3 (coherent store). */ + EXPECT_LT(instrs_by_mode[0], instrs_by_mode[2]); + EXPECT_LT(instrs_by_mode[0], instrs_by_mode[3]); + + /* Direct IR verification of AGX3B uniform-base folding: + * aligned low uniforms fold on non-coherent load/store, while unaligned, + * high (>= 256), and coherent stores must not fold. */ + for (int tc = 0; tc < 4; ++tc) { + nir_builder nb = make_compute_shader("uniform_base_ir"); + nb.shader->info.internal = false; + void *mem_ctx = ralloc_context(NULL); + struct agx_shader_key key = {}; + key.dev.chip = AGX_CHIP_G17P; + key.dev.encoding = AGX_ISA_ENCODING_AGX3B; + + agx_context *ctx = rzalloc(mem_ctx, agx_context); + ctx->nir = nb.shader; + ctx->stage = MESA_SHADER_COMPUTE; + ctx->key = &key; + list_inithead(&ctx->blocks); + + agx_block *blk = rzalloc(ctx, agx_block); + list_inithead(&blk->instructions); + list_addtail(&blk->link, &ctx->blocks); + + agx_builder ab = agx_init_builder(ctx, agx_after_block(blk)); + unsigned lo_u = (tc == 1) ? 2 : (tc == 2) ? 256 : 4; + agx_index base = agx_temp(ctx, AGX_SIZE_64); + agx_instr *col = agx_collect_to(&ab, base, 2); + col->src[0] = agx_uniform(lo_u, AGX_SIZE_32); + col->src[1] = agx_uniform(lo_u + 2, AGX_SIZE_32); + + agx_index idx = agx_temp(ctx, AGX_SIZE_32); + agx_mov_imm_to(&ab, idx, 0); + agx_index val = agx_temp(ctx, AGX_SIZE_32); + agx_mov_imm_to(&ab, val, 1); + + agx_instr *ld = agx_device_load_to(&ab, agx_temp(ctx, AGX_SIZE_32), base, + idx, AGX_FORMAT_I32, 0x1, 0, false); + agx_instr *st = agx_device_store(&ab, val, base, idx, AGX_FORMAT_I32, + 0x1, 0, tc == 3 /* coherent */); + agx_optimizer_forward(ctx); + + if (tc == 0) { + /* Aligned low uniform, non-coherent: both load and store fold. */ + EXPECT_EQ(ld->src[0].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(ld->src[0].value, 4u); + EXPECT_EQ(ld->src[0].size, AGX_SIZE_64); + EXPECT_EQ(st->src[1].type, AGX_INDEX_UNIFORM); + EXPECT_EQ(st->src[1].value, 4u); + EXPECT_EQ(st->src[1].size, AGX_SIZE_64); + } else if (tc == 1 || tc == 2) { + /* Unaligned (u2:u3) or high (u256:u257): neither folds. */ + EXPECT_NE(ld->src[0].type, AGX_INDEX_UNIFORM); + EXPECT_NE(st->src[1].type, AGX_INDEX_UNIFORM); + } else { + /* Coherent store: load still folds, coherent store does not. */ + EXPECT_EQ(ld->src[0].type, AGX_INDEX_UNIFORM); + EXPECT_NE(st->src[1].type, AGX_INDEX_UNIFORM); + } + + ralloc_free(mem_ctx); + ralloc_free(nb.shader); + } + + /* Verify scalar W8 store peels the u2u8 0xff mask on AGX3B: + * mode 0: store_agx(R8_UINT, u2u8(x)) peels the 0xff mask (only the two + * uniform->GPR movs remain: alu == 2). + * mode 1: store_agx(R8_UINT, u2u8(iand(x, 0x7f))) keeps the explicit + * 0x7f mask (alu == 3). + * mode 2: store_agx(R16_UINT, u2u16(u2u8(x))) cannot peel the 0xff mask + * because a W16 store writes all 16 bits (alu == 4). */ + unsigned alu_cnt[3] = {0, 0, 0}; + for (int mode = 0; mode < 3; ++mode) { + nir_builder b = make_compute_shader("w8_store_peel"); + b.shader->info.internal = false; + nir_def *out_base = nir_load_preamble(&b, 1, 64, .base = 0); + nir_def *idx = nir_load_preamble(&b, 1, 32, .base = 4); + nir_def *val32 = nir_load_preamble(&b, 1, 32, .base = 6); + if (mode == 1) + val32 = nir_iand_imm(&b, val32, 0x7f); + nir_def *val8 = nir_u2u8(&b, val32); + if (mode == 2) + nir_store_agx(&b, nir_u2u16(&b, val8), out_base, idx, .format = PIPE_FORMAT_R16_UINT); + else + nir_store_agx(&b, val8, out_base, idx, .format = PIPE_FORMAT_R8_UINT); + + struct agx_shader_key key = {}; + key.dev.chip = AGX_CHIP_G17P; + key.dev.encoding = AGX_ISA_ENCODING_AGX3B; + key.no_preamble = true; + agx_preprocess_nir(b.shader); + struct agx_shader_part part = {}; + EXPECT_TRUE(agx_compile_shader_nir(b.shader, &key, &part)); + alu_cnt[mode] = part.info.stats.alu; + free(part.binary); + ralloc_free(b.shader); + } + EXPECT_EQ(alu_cnt[0], 2u); + EXPECT_EQ(alu_cnt[1], 3u); + EXPECT_EQ(alu_cnt[2], 4u); +}