diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index b9cc7bf6e46..85e5ce13f30 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -16440,6 +16440,283 @@ agx_nir_fragment_output_temporaries(nir_shader *nir) return true; } +struct w8_sum_load_entry { + nir_intrinsic_instr *load; + unsigned seen_mask; + nir_def *u2u32_defs[4]; + unsigned nr_defs; +}; + +static bool +is_packable_w8_sum_load(nir_intrinsic_instr *load, nir_block *block) +{ + if ((load->intrinsic != nir_intrinsic_load_agx && + load->intrinsic != nir_intrinsic_load_constant_agx) || + load->instr.block != block || load->def.bit_size != 8 || + (load->def.num_components != 3 && load->def.num_components != 4) || + nir_intrinsic_format(load) != PIPE_FORMAT_R8_UINT || + (nir_intrinsic_base(load) & (AGX_NIR_MEMORY_PACKED_COUNT_MASK | + AGX_NIR_MEMORY_UNPACK_W8_WORD)) || + (nir_intrinsic_access(load) & (ACCESS_VOLATILE | ACCESS_COHERENT))) + return false; + + const unsigned nr = load->def.num_components; + unsigned seen = 0; + nir_foreach_use_including_if(use, &load->def) { + if (nir_src_is_if(use)) + return false; + nir_instr *parent = nir_src_use_instr(use); + if (parent->block != block || parent->type != nir_instr_type_alu) + return false; + nir_alu_instr *alu = nir_instr_as_alu(parent); + if (alu->op != nir_op_u2u32 || alu->def.bit_size != 32 || + alu->def.num_components != 1) + return false; + unsigned comp = alu->src[0].swizzle[0]; + if (comp >= nr || (seen & BITFIELD_BIT(comp))) + return false; + seen |= BITFIELD_BIT(comp); + if (!list_is_singular(&alu->def.uses)) + return false; + nir_src *add_use = list_first_entry(&alu->def.uses, nir_src, use_link); + if (nir_src_is_if(add_use)) + return false; + nir_instr *add_instr = nir_src_use_instr(add_use); + if (add_instr->block != block || add_instr->type != nir_instr_type_alu) + return false; + nir_alu_instr *add = nir_instr_as_alu(add_instr); + if (add->op != nir_op_iadd || add->def.bit_size != 32 || + add->def.num_components != 1) + return false; + } + return seen == BITFIELD_MASK(nr); +} + +static bool +is_single_use_block_iadd32(nir_def *def, nir_block *block) +{ + if (def->bit_size != 32 || def->num_components != 1 || + nir_def_instr(def)->block != block || + nir_def_instr(def)->type != nir_instr_type_alu || + !list_is_singular(&def->uses)) + return false; + nir_alu_instr *alu = nir_instr_as_alu(nir_def_instr(def)); + return alu->op == nir_op_iadd; +} + +static int +w8_sum_load_cmp(const void *pa, const void *pb) +{ + const nir_intrinsic_instr *a = *(const nir_intrinsic_instr *const *)pa; + const nir_intrinsic_instr *b = *(const nir_intrinsic_instr *const *)pb; + return (a->instr.index > b->instr.index) - (a->instr.index < b->instr.index); +} + +/* Fold horizontal sums of unsigned bytes loaded from 3- or 4-byte packed + * AGX3 I8 memory transactions using bit-exact 16-bit-lane SWAR: + * even = w & 0x00ff00ff ([b0, b2] in two 16-bit lanes) + * odd = (w >> 8) & 0x00ff00ff ([b1, b3] for nr=4; & 0xff for nr=3) + * Up to 32 words (max 32 * 510 = 16320 <= 65535 per 16-bit lane) accumulate + * in one packed 32-bit sum without carry between halves, then reduce once + * with (s & 0xffff) + (s >> 16). Preserves exact mod-2^32 semantics for all + * inputs and lets consecutive loads issue before unpacking. */ +bool +agx_nir_fold_w8_byte_sum(nir_shader *nir) +{ + bool progress = false; + + nir_foreach_function_impl(impl, nir) { + if (impl->function->is_preamble) { + nir_no_progress(impl); + continue; + } + + nir_index_instrs(impl); + bool impl_progress = false; + + nir_foreach_block(block, impl) { + nir_loop *outer_loop = NULL; + for (nir_cf_node *p = block->cf_node.parent; p; p = p->parent) { + if (p->type == nir_cf_node_loop) + outer_loop = nir_cf_node_as_loop(p); + } + nir_def *hoisted_mask = NULL; + + nir_foreach_instr_safe(instr, block) { + if (instr->type != nir_instr_type_alu) + continue; + nir_alu_instr *root = nir_instr_as_alu(instr); + if (root->op != nir_op_iadd || root->def.bit_size != 32 || + root->def.num_components != 1) + continue; + + /* Only start from maximal iadd trees within this block. */ + if (list_is_singular(&root->def.uses)) { + nir_src *u = list_first_entry(&root->def.uses, nir_src, use_link); + if (!nir_src_is_if(u)) { + nir_instr *ui = nir_src_use_instr(u); + if (ui->block == block && ui->type == nir_instr_type_alu) { + nir_alu_instr *ua = nir_instr_as_alu(ui); + if (ua->op == nir_op_iadd && ua->def.bit_size == 32 && + ua->def.num_components == 1) + continue; + } + } + } + + nir_scalar stack[128]; + nir_scalar root_value = nir_get_scalar(&root->def, 0); + unsigned sp = 0; + stack[sp++] = nir_scalar_chase_alu_src(root_value, 0); + stack[sp++] = nir_scalar_chase_alu_src(root_value, 1); + + nir_scalar extras[128]; + unsigned nr_extras = 0; + struct w8_sum_load_entry entries[32]; + unsigned nr_entries = 0; + bool overflow = false; + + while (sp > 0) { + nir_scalar leaf = stack[--sp]; + nir_def *d = leaf.def; + if (is_single_use_block_iadd32(d, block)) { + if (sp + 2 > ARRAY_SIZE(stack)) { + overflow = true; + break; + } + stack[sp++] = nir_scalar_chase_alu_src(leaf, 0); + stack[sp++] = nir_scalar_chase_alu_src(leaf, 1); + continue; + } + if (nir_def_instr(d)->block == block && + nir_def_instr(d)->type == nir_instr_type_alu) { + nir_alu_instr *a = nir_instr_as_alu(nir_def_instr(d)); + if (a->op == nir_op_u2u32 && a->def.bit_size == 32 && + a->def.num_components == 1 && + nir_def_instr(a->src[0].src.ssa)->type == + nir_instr_type_intrinsic) { + nir_intrinsic_instr *ld = + nir_instr_as_intrinsic(nir_def_instr(a->src[0].src.ssa)); + if (is_packable_w8_sum_load(ld, block)) { + unsigned idx = 0; + for (; idx < nr_entries; ++idx) { + if (entries[idx].load == ld) + break; + } + if (idx == nr_entries) { + if (nr_entries >= ARRAY_SIZE(entries)) { + overflow = true; + break; + } + entries[nr_entries++] = (struct w8_sum_load_entry){ + .load = ld, + }; + } + unsigned comp = a->src[0].swizzle[0]; + entries[idx].seen_mask |= BITFIELD_BIT(comp); + if (entries[idx].nr_defs < 4) + entries[idx].u2u32_defs[entries[idx].nr_defs++] = d; + continue; + } + } + } + if (nr_extras >= ARRAY_SIZE(extras)) { + overflow = true; + break; + } + extras[nr_extras++] = leaf; + } + if (overflow || nr_entries == 0) + continue; + + nir_intrinsic_instr *complete[32]; + unsigned nr_complete = 0; + bool has_vec4 = false; + for (unsigned i = 0; i < nr_entries; ++i) { + unsigned nr = entries[i].load->def.num_components; + if (entries[i].seen_mask == BITFIELD_MASK(nr)) { + complete[nr_complete++] = entries[i].load; + if (nr == 4) + has_vec4 = true; + } else { + for (unsigned j = 0; j < entries[i].nr_defs; ++j) { + if (nr_extras >= ARRAY_SIZE(extras)) { + overflow = true; + break; + } + extras[nr_extras++] = + nir_get_scalar(entries[i].u2u32_defs[j], 0); + } + } + } + if (overflow || (!has_vec4 && nr_complete < 2)) + continue; + + qsort(complete, nr_complete, sizeof(complete[0]), w8_sum_load_cmp); + + if (!hoisted_mask) { + nir_builder bm = outer_loop + ? nir_builder_at(nir_before_cf_node(&outer_loop->cf_node)) + : nir_builder_at(nir_before_instr(&root->instr)); + hoisted_mask = nir_imm_int(&bm, 0x00ff00ff); + } + + nir_builder b = nir_builder_at(nir_before_instr(&root->instr)); + nir_def *packed_sum = NULL; + for (unsigned i = 0; i < nr_complete; ++i) { + nir_intrinsic_instr *orig = complete[i]; + const unsigned nr = orig->def.num_components; + nir_builder bl = nir_builder_at(nir_before_instr(&orig->instr)); + nir_intrinsic_instr *pl = + nir_intrinsic_instr_create(nir, orig->intrinsic); + pl->num_components = 1; + pl->src[0] = nir_src_for_ssa(orig->src[0].ssa); + pl->src[1] = nir_src_for_ssa(orig->src[1].ssa); + nir_intrinsic_set_base( + pl, nir_intrinsic_base(orig) | + (nr << AGX_NIR_MEMORY_PACKED_COUNT_SHIFT)); + nir_intrinsic_set_format(pl, nir_intrinsic_format(orig)); + nir_intrinsic_set_sign_extend(pl, nir_intrinsic_sign_extend(orig)); + nir_intrinsic_set_access(pl, nir_intrinsic_access(orig)); + nir_def_init(&pl->instr, &pl->def, 1, 32); + nir_builder_instr_insert(&bl, &pl->instr); + + nir_def *w = &pl->def; + nir_def *even = nir_iand(&b, w, hoisted_mask); + nir_def *shifted = nir_ushr_imm(&b, w, 8); + nir_def *odd = (nr == 4) + ? nir_iand(&b, shifted, hoisted_mask) + : nir_iand_imm(&b, shifted, 0xff); + nir_def *pair = nir_iadd(&b, even, odd); + packed_sum = packed_sum ? nir_iadd(&b, packed_sum, pair) : pair; + } + + nir_def *lo = nir_iand_imm(&b, packed_sum, 0xffff); + nir_def *hi = nir_ushr_imm(&b, packed_sum, 16); + + nir_def *total = NULL; + for (unsigned i = 0; i < nr_extras; ++i) { + nir_def *addend = nir_channel(&b, extras[i].def, extras[i].comp); + total = total ? nir_iadd(&b, total, addend) : addend; + } + total = total ? nir_iadd(&b, total, lo) : lo; + total = nir_iadd(&b, total, hi); + + nir_def_replace(&root->def, total); + impl_progress = true; + progress = true; + } + } + + if (impl_progress) + nir_progress(true, impl, nir_metadata_control_flow); + else + nir_no_progress(impl); + } + + return progress; +} + /* Fold 8-to-32 unsigned conversion into the selected result type. The * 3- or 4-byte transaction is still determined by the format and lane count. * agx_emit_load() then extracts the lanes from the byte-packed word that a @@ -21559,6 +21836,17 @@ agx_compile_shader_nir_internal(nir_shader *nir, struct agx_shader_key *key, } } + if ((key->dev.encoding == AGX_ISA_ENCODING_AGX3B || + key->dev.encoding == AGX_ISA_ENCODING_AGX3C) && + !key->dev.emit_agx3d) { + bool swar = false; + NIR_PASS(swar, nir, agx_nir_fold_w8_byte_sum); + if (swar) { + NIR_PASS(_, nir, nir_opt_dce); + nir_divergence_analysis(nir); + } + } + /* Every AGX3B part returns a 3- or 4-lane W8 load as one byte-packed * word (the W8 path of agx_emit_load()), which the fold extracts with * integer shifts. AGX2 returns one byte per 16-bit lane, so there is no diff --git a/src/asahi/compiler/agx_nir.h b/src/asahi/compiler/agx_nir.h index 3558a6a8f4a..260340e74d9 100644 --- a/src/asahi/compiler/agx_nir.h +++ b/src/asahi/compiler/agx_nir.h @@ -452,6 +452,7 @@ bool agx_nir_shrink_memory_loads(struct nir_shader *nir, bool byte_offsets); bool agx_nir_merge_global_words(struct nir_shader *shader); bool agx_nir_opt_shifted_index(struct nir_shader *shader); bool agx_nir_hoist_readonly_loop_loads(struct nir_shader *shader); +bool agx_nir_fold_w8_byte_sum(struct nir_shader *nir); bool agx_nir_narrow_single_subgroup_barriers(struct nir_shader *shader); bool agx_nir_narrow_subgroup0_barriers(struct nir_shader *shader); bool agx_nir_opt_uniform_stride(struct nir_shader *shader); diff --git a/src/asahi/compiler/test/test-agx3-vision-opts.cpp b/src/asahi/compiler/test/test-agx3-vision-opts.cpp index c3f662f89ff..e25e57474c4 100644 --- a/src/asahi/compiler/test/test-agx3-vision-opts.cpp +++ b/src/asahi/compiler/test/test-agx3-vision-opts.cpp @@ -2834,3 +2834,151 @@ TEST(AGX3VisionOpts, Wave8FoldSignedPow2Idiv) ralloc_free(b.shader); } } + +TEST(AGX3VisionOpts, Wave8SwarW8ByteSumWithVectorAddends) +{ + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "wave8_swar_vector_addends"); + nir_def *base = nir_load_push_constant(&b, 1, 64, nir_imm_int(&b, 0)); + nir_def *index = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 8)); + nir_def *bytes = nir_load_agx(&b, 4, 8, base, index, + .base = AGX_NIR_MEMORY_INDEX_BYTES, + .format = PIPE_FORMAT_R8_UINT); + nir_def *words = nir_load_global(&b, 4, 32, base, .align_mul = 4); + nir_def *sum = nir_channel(&b, words, 0); + for (unsigned c = 1; c < 4; ++c) + sum = nir_iadd(&b, sum, nir_channel(&b, words, c)); + for (unsigned c = 0; c < 4; ++c) + sum = nir_iadd(&b, sum, nir_u2u32(&b, nir_channel(&b, bytes, c))); + nir_intrinsic_instr *store = nir_store_global(&b, sum, base, .align_mul = 4); + + NIR_PASS(_, b.shader, nir_opt_copy_prop); + EXPECT_TRUE(agx_nir_fold_w8_byte_sum(b.shader)); + EXPECT_EQ(store->src[0].ssa->num_components, 1u); + if (store->src[0].ssa->num_components == 1) { + NIR_PASS(_, b.shader, nir_opt_dce); + nir_validate_shader(b.shader, "wave8 SWAR vector addends"); + } + ralloc_free(b.shader); +} + +TEST(AGX3VisionOpts, Wave8SwarW8ByteSum) +{ + /* Positive 1: two u8x4 R8_UINT loads summed into an accumulator -> SWAR. */ + { + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "wave8_swar_u8x4"); + nir_def *base = nir_load_push_constant(&b, 1, 64, nir_imm_int(&b, 0)); + nir_def *idx = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 8)); + nir_def *acc = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 12)); + + nir_def *l0 = nir_load_agx(&b, 4, 8, base, idx, + .base = AGX_NIR_MEMORY_INDEX_BYTES, + .format = PIPE_FORMAT_R8_UINT); + nir_def *l1 = nir_load_agx(&b, 4, 8, base, nir_iadd_imm(&b, idx, 4), + .base = AGX_NIR_MEMORY_INDEX_BYTES, + .format = PIPE_FORMAT_R8_UINT); + for (unsigned c = 0; c < 4; ++c) + acc = nir_iadd(&b, acc, nir_u2u32(&b, nir_channel(&b, l0, c))); + for (unsigned c = 0; c < 4; ++c) + acc = nir_iadd(&b, acc, nir_u2u32(&b, nir_channel(&b, l1, c))); + nir_store_global(&b, acc, base, .align_mul = 4); + + NIR_PASS(_, b.shader, nir_opt_copy_prop); + EXPECT_TRUE(agx_nir_fold_w8_byte_sum(b.shader)); + NIR_PASS(_, b.shader, nir_opt_dce); + nir_validate_shader(b.shader, "wave8 swar u8x4"); + + unsigned packed_loads = 0, old_u8_loads = 0; + nir_foreach_block(block, b.impl) { + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + if (intr->intrinsic != nir_intrinsic_load_agx) + continue; + if (intr->def.bit_size == 32 && intr->def.num_components == 1 && + ((nir_intrinsic_base(intr) & AGX_NIR_MEMORY_PACKED_COUNT_MASK) >> + AGX_NIR_MEMORY_PACKED_COUNT_SHIFT) == 4) + packed_loads++; + else if (intr->def.bit_size == 8) + old_u8_loads++; + } + } + EXPECT_EQ(packed_loads, 2u); + EXPECT_EQ(old_u8_loads, 0u); + ralloc_free(b.shader); + } + + /* Positive 2: two u8x3 R8_UINT loads summed without initial acc -> SWAR. */ + { + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "wave8_swar_u8x3"); + nir_def *base = nir_load_push_constant(&b, 1, 64, nir_imm_int(&b, 0)); + nir_def *idx = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 8)); + + nir_def *l0 = nir_load_agx(&b, 3, 8, base, idx, + .base = AGX_NIR_MEMORY_INDEX_BYTES, + .format = PIPE_FORMAT_R8_UINT); + nir_def *l1 = nir_load_agx(&b, 3, 8, base, nir_iadd_imm(&b, idx, 16), + .base = AGX_NIR_MEMORY_INDEX_BYTES, + .format = PIPE_FORMAT_R8_UINT); + nir_def *sum = nir_iadd(&b, nir_u2u32(&b, nir_channel(&b, l0, 0)), + nir_u2u32(&b, nir_channel(&b, l0, 1))); + sum = nir_iadd(&b, sum, nir_u2u32(&b, nir_channel(&b, l0, 2))); + for (unsigned c = 0; c < 3; ++c) + sum = nir_iadd(&b, sum, nir_u2u32(&b, nir_channel(&b, l1, c))); + nir_store_global(&b, sum, base, .align_mul = 4); + + NIR_PASS(_, b.shader, nir_opt_copy_prop); + EXPECT_TRUE(agx_nir_fold_w8_byte_sum(b.shader)); + NIR_PASS(_, b.shader, nir_opt_dce); + nir_validate_shader(b.shader, "wave8 swar u8x3"); + + unsigned packed_loads = 0; + nir_foreach_block(block, b.impl) { + nir_foreach_instr(instr, block) { + if (instr->type == nir_instr_type_intrinsic) { + nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr); + if (intr->intrinsic == nir_intrinsic_load_agx && + ((nir_intrinsic_base(intr) & AGX_NIR_MEMORY_PACKED_COUNT_MASK) >> + AGX_NIR_MEMORY_PACKED_COUNT_SHIFT) == 3) + packed_loads++; + } + } + } + EXPECT_EQ(packed_loads, 2u); + ralloc_free(b.shader); + } + + /* Negative: single u8x3 load, coherent u8x4 load, or partial lane use must stay intact. */ + { + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "wave8_swar_neg"); + nir_def *base = nir_load_push_constant(&b, 1, 64, nir_imm_int(&b, 0)); + nir_def *idx = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 8)); + + nir_def *s3 = nir_load_agx(&b, 3, 8, base, idx, + .base = AGX_NIR_MEMORY_INDEX_BYTES, + .format = PIPE_FORMAT_R8_UINT); + nir_def *coh = nir_load_agx(&b, 4, 8, base, nir_iadd_imm(&b, idx, 4), + .access = ACCESS_COHERENT, + .base = AGX_NIR_MEMORY_INDEX_BYTES, + .format = PIPE_FORMAT_R8_UINT); + nir_def *part = nir_load_agx(&b, 4, 8, base, nir_iadd_imm(&b, idx, 8), + .base = AGX_NIR_MEMORY_INDEX_BYTES, + .format = PIPE_FORMAT_R8_UINT); + nir_def *acc = nir_u2u32(&b, nir_channel(&b, s3, 0)); + acc = nir_iadd(&b, acc, nir_u2u32(&b, nir_channel(&b, s3, 1))); + acc = nir_iadd(&b, acc, nir_u2u32(&b, nir_channel(&b, s3, 2))); + for (unsigned c = 0; c < 4; ++c) + acc = nir_iadd(&b, acc, nir_u2u32(&b, nir_channel(&b, coh, c))); + acc = nir_iadd(&b, acc, nir_u2u32(&b, nir_channel(&b, part, 0))); + acc = nir_iadd(&b, acc, nir_u2u32(&b, nir_channel(&b, part, 1))); + nir_store_global(&b, acc, base, .align_mul = 4); + + NIR_PASS(_, b.shader, nir_opt_copy_prop); + EXPECT_FALSE(agx_nir_fold_w8_byte_sum(b.shader)); + ralloc_free(b.shader); + } +}