diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 90ee795e50ec..95f6eba1909a 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -8146,7 +8146,8 @@ agx_g17p_shift_boundary(agx_builder *b, unsigned shift) static agx_index agx_g17p_ushr_imm(agx_builder *b, agx_index value, unsigned shift) { - return agx_ushr(b, value, agx_g17p_shift_boundary(b, shift)); + assert(shift >= 1 && shift <= 31); + return agx_ushr(b, value, agx_immediate(shift)); } static agx_index @@ -8237,8 +8238,8 @@ static agx_instr * agx_emit_g17p_ufind_msb(agx_builder *b, agx_index dst, agx_index value) { /* FT1 on G17P exposes a non-architectural logarithmic intermediate. Find - * the top bit with the integer forms already qualified on hardware: - * smear the top bit downward, popcount the resulting prefix with SWAR, + * the top bit with the grounded integer forms: smear the top bit downward + * with immediate SRL + OR, count the resulting prefix with native POPCOUNT, * then subtract one. Zero smears/counts to zero and therefore returns -1 * without a compare or select. */ assert(value.size == AGX_SIZE_32 && dst.size == AGX_SIZE_32); @@ -8248,22 +8249,8 @@ agx_emit_g17p_ufind_msb(agx_builder *b, agx_index dst, agx_index value) for (unsigned i = 0; i < ARRAY_SIZE(smear_shifts); ++i) scan = agx_or(b, scan, agx_g17p_ushr_imm(b, scan, smear_shifts[i])); - agx_index odd = agx_g17p_and_imm( - b, agx_g17p_ushr_imm(b, scan, 1), UINT32_C(0x55555555)); - scan = agx_iadd(b, scan, agx_neg(odd), 0); - - agx_index pairs_lo = agx_g17p_and_imm(b, scan, UINT32_C(0x33333333)); - agx_index pairs_hi = agx_g17p_and_imm( - b, agx_g17p_ushr_imm(b, scan, 2), UINT32_C(0x33333333)); - scan = agx_iadd(b, pairs_lo, pairs_hi, 0); - - scan = agx_g17p_and_imm( - b, agx_iadd(b, scan, agx_g17p_ushr_imm(b, scan, 4), 0), - UINT32_C(0x0f0f0f0f)); - scan = agx_iadd(b, scan, agx_g17p_ushr_imm(b, scan, 8), 0); - scan = agx_iadd(b, scan, agx_g17p_ushr_imm(b, scan, 16), 0); - - agx_index count = agx_g17p_and_imm(b, scan, 0x3f); + agx_index count = agx_temp(b->shader, AGX_SIZE_32); + agx_popcount_to(b, count, scan); return agx_iadd_to(b, dst, count, agx_neg(agx_immediate(1)), 0); } diff --git a/src/asahi/compiler/agx_nir_algebraic.py b/src/asahi/compiler/agx_nir_algebraic.py index 9d168c3594ec..49345c0d9c6a 100644 --- a/src/asahi/compiler/agx_nir_algebraic.py +++ b/src/asahi/compiler/agx_nir_algebraic.py @@ -723,11 +723,26 @@ def run(): m5_narrow_to_float).render()) print(nir_algebraic.AlgebraicPass("agx_nir_reassoc_const_offsets", reassoc_const_offsets).render()) + # nir_opt_idiv_const lowers signed idiv(a, 2^k) to + # imul(imin(imax(a, -1), 1), ushr(iabs(a), k)), which takes 9 AGX ops. + # Fold to the exact 3- or 4-op sign-bias shift (bit-exact for all int32_t, + # including INT32_MIN). + fold_signed_pow2_idiv = [] + for k in range(1, 31): + bias = ('ushr', 'a', 31) if k == 1 else ('ushr', ('ishr', 'a', 31), 32 - k) + repl = ('ishr', ('iadd', 'a', bias), k) + for sign in (('imin', ('imax', 'a@32', -1), 1), + ('imax', ('imin', 'a@32', 1), -1)): + fold_signed_pow2_idiv += [ + (('imul@32', sign, ('ushr', ('iabs', 'a@32'), k)), repl), + ] + print(nir_algebraic.AlgebraicPass("agx_nir_fuse_algebraic_late", fuse_extr + fuse_ubfe + fuse_imad + ixor_bcsel + fold_aligned_bounds + fold_byte_swap + - fold_selected_f2i32).render()) + fold_selected_f2i32 + + fold_signed_pow2_idiv).render()) if __name__ == '__main__': diff --git a/src/asahi/compiler/agx_nir_lower_subgroups.c b/src/asahi/compiler/agx_nir_lower_subgroups.c index 024fc5c336dd..3a0d32de2ab8 100644 --- a/src/asahi/compiler/agx_nir_lower_subgroups.c +++ b/src/asahi/compiler/agx_nir_lower_subgroups.c @@ -24,8 +24,151 @@ struct agx_subgroup_lower_options { bool g17p_clustered_float; bool native_shuffle_xor; bool m5_float_minmax; + bool emulates_ufind_msb; }; +static nir_intrinsic_instr * +ballot_true_low_word(nir_scalar s) +{ + if (s.comp != 0 || nir_def_instr(s.def)->type != nir_instr_type_intrinsic) + return NULL; + + nir_intrinsic_instr *ballot = nir_instr_as_intrinsic(nir_def_instr(s.def)); + if (ballot->intrinsic != nir_intrinsic_ballot || + !nir_src_is_const(ballot->src[0]) || !nir_src_as_bool(ballot->src[0])) + return NULL; + + return ballot; +} + +static nir_def * +active_rank_at_ballot(nir_builder *b, nir_intrinsic_instr *ballot) +{ + /* A consumer in divergent control flow may have a narrower active mask. */ + b->cursor = nir_before_instr(&ballot->instr); + return nir_u2u32(b, nir_load_active_subgroup_invocation_agx(b, 16)); +} + +static bool +is_subgroup_invocation_mod32(nir_scalar s) +{ + if (nir_scalar_is_alu(s) && nir_scalar_alu_op(s) == nir_op_iand) { + nir_scalar a = nir_scalar_chase_alu_src(s, 0); + nir_scalar b = nir_scalar_chase_alu_src(s, 1); + if (nir_scalar_is_const(b) && (nir_scalar_as_uint(b) & 31) == 31) + s = a; + else if (nir_scalar_is_const(a) && (nir_scalar_as_uint(a) & 31) == 31) + s = b; + } + nir_instr *instr = nir_def_instr(s.def); + return s.comp == 0 && instr->type == nir_instr_type_intrinsic && + nir_instr_as_intrinsic(instr)->intrinsic == + nir_intrinsic_load_subgroup_invocation; +} + +static bool +is_subgroup_lt_mask(nir_scalar s) +{ + if (!nir_scalar_is_alu(s)) + return false; + + /* Pattern 1 (nir_lower_subgroups): inot(ishl(-1, lane & 31)) */ + if (nir_scalar_alu_op(s) == nir_op_inot) { + nir_scalar sh = nir_scalar_chase_alu_src(s, 0); + if (nir_scalar_is_alu(sh) && nir_scalar_alu_op(sh) == nir_op_ishl) { + nir_scalar base = nir_scalar_chase_alu_src(sh, 0); + nir_scalar cnt = nir_scalar_chase_alu_src(sh, 1); + if (nir_scalar_is_const(base) && + nir_scalar_as_uint(base) == UINT32_MAX && + is_subgroup_invocation_mod32(cnt)) + return true; + } + } + + /* Pattern 2 (nir_opt_algebraic): iadd(ishl(1, lane & 31), -1) */ + if (nir_scalar_alu_op(s) == nir_op_iadd) { + for (unsigned i = 0; i < 2; ++i) { + nir_scalar c = nir_scalar_chase_alu_src(s, i); + nir_scalar sh = nir_scalar_chase_alu_src(s, 1 - i); + if (nir_scalar_is_const(c) && nir_scalar_as_uint(c) == UINT32_MAX && + nir_scalar_is_alu(sh) && nir_scalar_alu_op(sh) == nir_op_ishl) { + nir_scalar base = nir_scalar_chase_alu_src(sh, 0); + nir_scalar cnt = nir_scalar_chase_alu_src(sh, 1); + if (nir_scalar_is_const(base) && nir_scalar_as_uint(base) == 1 && + is_subgroup_invocation_mod32(cnt)) + return true; + } + } + } + + return false; +} + +static bool +opt_compute_ballot_lt_mask_count(nir_builder *b, nir_instr *instr, + UNUSED void *data) +{ + if (instr->type != nir_instr_type_alu) + return false; + nir_alu_instr *alu = nir_instr_as_alu(instr); + if (alu->op != nir_op_bit_count || alu->def.bit_size != 32 || + alu->def.num_components != 1) + return false; + + nir_scalar s = nir_scalar_chase_alu_src(nir_get_scalar(&alu->def, 0), 0); + if (!nir_scalar_is_alu(s) || nir_scalar_alu_op(s) != nir_op_iand || + s.def->bit_size != 32) + return false; + + nir_scalar a = nir_scalar_chase_alu_src(s, 0); + nir_scalar b_s = nir_scalar_chase_alu_src(s, 1); + nir_intrinsic_instr *ballot = ballot_true_low_word(a); + if (!ballot || !is_subgroup_lt_mask(b_s)) { + ballot = ballot_true_low_word(b_s); + if (!ballot || !is_subgroup_lt_mask(a)) + return false; + } + + nir_def *active_id = active_rank_at_ballot(b, ballot); + nir_def_rewrite_uses(&alu->def, active_id); + nir_instr_remove(instr); + return true; +} + +static bool +opt_compute_ballot_bit_count(nir_builder *b, nir_intrinsic_instr *intr, + UNUSED void *data) +{ + if (intr->intrinsic != nir_intrinsic_ballot_bit_count_exclusive && + intr->intrinsic != nir_intrinsic_ballot_bit_count_inclusive) + return false; + + if (intr->def.bit_size != 32) + return false; + + nir_def *mask = intr->src[0].ssa; + nir_intrinsic_instr *ballot = + ballot_true_low_word(nir_scalar_resolved(mask, 0)); + if (!ballot) + return false; + + for (unsigned i = 1; i < mask->num_components; ++i) { + nir_scalar c = nir_scalar_resolved(mask, i); + if (c.def == &ballot->def && c.comp != 0) + continue; + if (!nir_scalar_is_const(c) || nir_scalar_as_uint(c) != 0) + return false; + } + + nir_def *active_id = active_rank_at_ballot(b, ballot); + if (intr->intrinsic == nir_intrinsic_ballot_bit_count_inclusive) + active_id = nir_iadd_imm(b, active_id, 1); + + nir_def_rewrite_uses(&intr->def, active_id); + nir_instr_remove(&intr->instr); + return true; +} + /* M5 shuffles a 32-bit value across the subgroup by a constant lane XOR in * one instruction (simd_shuffle_xor with an immediate), where the general * shuffle is four broadcasts and three selects. The mask is usually a @@ -74,10 +217,17 @@ lower(nir_builder *b, nir_intrinsic_instr *intr, void *data) } case nir_intrinsic_first_invocation: { + const struct agx_subgroup_lower_options *agx_options = data; nir_def *active_id = nir_load_active_subgroup_invocation_agx(b, 16); nir_def *is_first = nir_ieq_imm(b, active_id, 0); nir_def *first_bit = nir_ballot(b, 1, 32, is_first); - nir_def_rewrite_uses(&intr->def, nir_ufind_msb(b, first_bit)); + /* first_bit is guaranteed to be a single bit (1u << k) because the + * calling lane is active, so popcount(first_bit - 1) == k in 2 ops + * without emulating ufind_msb on AGX3B. */ + nir_def *lane = agx_options->emulates_ufind_msb + ? nir_bit_count(b, nir_iadd_imm(b, first_bit, -1)) + : nir_ufind_msb(b, first_bit); + nir_def_rewrite_uses(&intr->def, lane); return true; } @@ -102,8 +252,17 @@ lower(nir_builder *b, nir_intrinsic_instr *intr, void *data) * * ballot(x != broadcast(x, ffs(ballot(true)))) == 0 */ - nir_def *active_mask = nir_ballot(b, 1, 32, nir_imm_true(b)); - nir_def *active_bit = nir_ufind_msb(b, active_mask); + const struct agx_subgroup_lower_options *agx_options = data; + nir_def *active_bit; + if (agx_options->emulates_ufind_msb) { + nir_def *active_id = nir_load_active_subgroup_invocation_agx(b, 16); + nir_def *first_bit = + nir_ballot(b, 1, 32, nir_ieq_imm(b, active_id, 0)); + active_bit = nir_bit_count(b, nir_iadd_imm(b, first_bit, -1)); + } else { + nir_def *active_mask = nir_ballot(b, 1, 32, nir_imm_true(b)); + active_bit = nir_ufind_msb(b, active_mask); + } nir_def *other = nir_read_invocation(b, intr->src[0].ssa, active_bit); nir_def *is_ne; @@ -324,13 +483,15 @@ agx_nir_flush_subgroup_minmax(nir_shader *s) static bool agx_nir_lower_subgroups_internal(nir_shader *s, bool g17p_clustered_float, bool optimize_ballot_count, - bool native_shuffle_xor, bool m5_float_minmax) + bool native_shuffle_xor, bool m5_float_minmax, + bool emulates_ufind_msb) { const struct agx_subgroup_lower_options agx_options = { .optimize_ballot_count = optimize_ballot_count, .g17p_clustered_float = g17p_clustered_float, .native_shuffle_xor = native_shuffle_xor, .m5_float_minmax = m5_float_minmax, + .emulates_ufind_msb = emulates_ufind_msb, }; /* First, do as much common lowering as we can */ @@ -352,8 +513,24 @@ agx_nir_lower_subgroups_internal(nir_shader *s, bool g17p_clustered_float, bool progress = agx_nir_flush_subgroup_minmax(s); + /* In compute shaders (no helper invocations), the exclusive/inclusive bit + * count of ballot(true) is the hardware active-lane index (+1 for + * inclusive). Lower before nir_lower_subgroups expands it to a ballot + + * shift + mask + popcount sequence. */ + if (mesa_shader_stage_is_compute(s->info.stage)) { + progress |= nir_shader_intrinsics_pass( + s, opt_compute_ballot_bit_count, nir_metadata_control_flow, NULL); + } + progress |= nir_lower_subgroups(s, &opts); + /* Also catch ballot(true) & lt_mask -> popcount after nir_lower_subgroups + * (which also covers shaders that construct gl_SubgroupLtMask manually). */ + if (mesa_shader_stage_is_compute(s->info.stage)) { + progress |= nir_shader_instructions_pass( + s, opt_compute_ballot_lt_mask_count, nir_metadata_control_flow, NULL); + } + /* Then do AGX-only lowerings on top */ progress |= nir_shader_intrinsics_pass(s, lower, nir_metadata_control_flow, (void *)&agx_options); @@ -364,13 +541,13 @@ agx_nir_lower_subgroups_internal(nir_shader *s, bool g17p_clustered_float, bool agx_nir_lower_subgroups(nir_shader *s) { - return agx_nir_lower_subgroups_internal(s, false, true, false, false); + return agx_nir_lower_subgroups_internal(s, false, true, false, false, false); } bool agx_nir_lower_subgroups_g17p(nir_shader *s) { - return agx_nir_lower_subgroups_internal(s, true, false, false, false); + return agx_nir_lower_subgroups_internal(s, true, false, false, false, true); } bool @@ -389,7 +566,8 @@ agx_nir_lower_subgroups_m5(nir_shader *s, bool native_shuffle_xor, return agx_nir_lower_subgroups_internal( s, false, s->info.stage != MESA_SHADER_FRAGMENT, - native_shuffle_xor && compute, native_float_minmax && compute && !ftz32); + native_shuffle_xor && compute, native_float_minmax && compute && !ftz32, + false); } static bool diff --git a/src/asahi/compiler/test/test-agx3-vision-opts.cpp b/src/asahi/compiler/test/test-agx3-vision-opts.cpp index ee333d9ea5f1..c293dac14564 100644 --- a/src/asahi/compiler/test/test-agx3-vision-opts.cpp +++ b/src/asahi/compiler/test/test-agx3-vision-opts.cpp @@ -4662,3 +4662,228 @@ TEST(AGX3VisionOpts, Rotated8BitInnerNonZeroStart) ralloc_free(ref); ralloc_free(opt); } + +/* --- Wave 8: AGX3B subgroup ballot/lt-mask & ufind_msb, signed pow2 idiv, + * store_global vectorization, dominating bound guard & single-arm loop unswitch --- */ + +TEST(AGX3VisionOpts, Wave8BallotRankUsesProducerMaskAndLowWord) +{ + enum class CountForm { Lowered, Exclusive, Inclusive }; + for (CountForm form : {CountForm::Lowered, CountForm::Exclusive, + CountForm::Inclusive}) { + SCOPED_TRACE(static_cast(form)); + for (unsigned word : {0u, 1u}) { + SCOPED_TRACE(word); + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "wave8_ballot_rank"); + nir_def *lane = nir_load_subgroup_invocation(&b); + nir_def *ballot = nir_ballot(&b, 4, 32, nir_imm_true(&b)); + nir_block *ballot_block = nir_def_instr(ballot)->block; + nir_def *mask_word = nir_channel(&b, ballot, word); + + nir_push_if(&b, nir_ine_imm(&b, nir_iand_imm(&b, lane, 1), 0)); + nir_def *count; + if (form == CountForm::Lowered) { + nir_def *lt_mask = + nir_inot(&b, nir_ishl(&b, nir_imm_int(&b, -1), lane)); + count = nir_bit_count(&b, nir_iand(&b, mask_word, lt_mask)); + } else { + nir_def *zero = nir_imm_int(&b, 0); + nir_def *mask = nir_vec4(&b, mask_word, zero, zero, zero); + count = form == CountForm::Exclusive + ? nir_ballot_bit_count_exclusive(&b, mask) + : nir_ballot_bit_count_inclusive(&b, mask); + } + nir_store_global(&b, count, nir_imm_int64(&b, 0x1000), .align_mul = 4); + nir_pop_if(&b, NULL); + + agx_nir_lower_subgroups_g17p(b.shader); + NIR_PASS(_, b.shader, nir_opt_dce); + nir_validate_shader(b.shader, "wave8 ballot rank"); + + unsigned ranks = 0; + nir_foreach_block(block, b.impl) { + nir_foreach_instr(instr, block) { + if (instr->type == nir_instr_type_intrinsic && + nir_instr_as_intrinsic(instr)->intrinsic == + nir_intrinsic_load_active_subgroup_invocation_agx) { + EXPECT_EQ(block, ballot_block); + ranks++; + } + } + } + EXPECT_EQ(ranks, word == 0 ? 1u : 0u); + ralloc_free(b.shader); + } + } +} + +TEST(AGX3VisionOpts, Wave8Agx3bSubgroupBallotLtMaskAndFirstInv) +{ + /* Compute shader: bit_count(ballot(true) & ~(0xffffffff << (lane & 31))) + * and bit_count(ballot(true) & ((1 << (lane & 31)) - 1)) must lower to + * load_active_subgroup_invocation_agx with zero remaining ballots. */ + for (unsigned form = 0; form < 2; form++) { + SCOPED_TRACE(form); + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "wave8_ballot_lt"); + nir_def *lane = nir_load_subgroup_invocation(&b); + nir_def *lane_m = nir_iand_imm(&b, lane, 31); + nir_def *lt_mask = form == 0 + ? nir_inot(&b, nir_ishl(&b, nir_imm_int(&b, -1), lane_m)) + : nir_iadd_imm(&b, nir_ishl(&b, nir_imm_int(&b, 1), lane_m), -1); + nir_def *bal = nir_ballot(&b, 1, 32, nir_imm_true(&b)); + nir_def *cnt = nir_bit_count(&b, nir_iand(&b, bal, lt_mask)); + nir_store_global(&b, cnt, nir_imm_int64(&b, 0x1000), .align_mul = 4); + + agx_nir_lower_subgroups_g17p(b.shader); + NIR_PASS(_, b.shader, nir_opt_dce); + nir_validate_shader(b.shader, "wave8 ballot lt compute"); + + unsigned ballots = 0, active_inv = 0; + nir_foreach_block(block, b.impl) { + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + nir_intrinsic_op op = nir_instr_as_intrinsic(instr)->intrinsic; + ballots += (op == nir_intrinsic_ballot); + active_inv += (op == nir_intrinsic_load_active_subgroup_invocation_agx); + } + } + EXPECT_EQ(ballots, 0u); + EXPECT_EQ(active_inv, 1u); + ralloc_free(b.shader); + } + + /* Negative: fragment shader (helper lanes may exist) MUST NOT trigger. */ + { + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_FRAGMENT, &agx_nir_options, "wave8_ballot_lt_fs"); + nir_def *lane = nir_load_subgroup_invocation(&b); + nir_def *lt_mask = nir_inot(&b, nir_ishl(&b, nir_imm_int(&b, -1), lane)); + nir_def *bal = nir_ballot(&b, 1, 32, nir_imm_true(&b)); + nir_def *cnt = nir_bit_count(&b, nir_iand(&b, bal, lt_mask)); + nir_store_global(&b, cnt, nir_imm_int64(&b, 0x1000), .align_mul = 4); + + agx_nir_lower_subgroups_g17p(b.shader); + NIR_PASS(_, b.shader, nir_opt_dce); + nir_validate_shader(b.shader, "wave8 ballot lt fragment"); + + unsigned ballots = 0, active_inv = 0; + nir_foreach_block(block, b.impl) { + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_intrinsic) + continue; + nir_intrinsic_op op = nir_instr_as_intrinsic(instr)->intrinsic; + ballots += (op == nir_intrinsic_ballot); + active_inv += (op == nir_intrinsic_load_active_subgroup_invocation_agx); + } + } + EXPECT_EQ(ballots, 1u); + EXPECT_EQ(active_inv, 0u); + ralloc_free(b.shader); + } + + /* AGX3B emulates_ufind_msb=true: first_invocation and vote_ieq lower via + * bit_count(first_bit - 1) with zero ufind_msb/find_lsb ops. */ + { + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "wave8_first_inv_agx3b"); + nir_def *v = nir_load_global(&b, 1, 32, nir_imm_int64(&b, 0x1000), .align_mul = 4); + nir_def *first = nir_read_first_invocation(&b, v); + nir_def *eq = nir_b2i32(&b, nir_vote_ieq(&b, 1, v)); + nir_store_global(&b, nir_iadd(&b, first, eq), nir_imm_int64(&b, 0x2000), .align_mul = 4); + + agx_nir_lower_subgroups_g17p(b.shader); + NIR_PASS(_, b.shader, nir_opt_dce); + nir_validate_shader(b.shader, "wave8 first_inv agx3b"); + + unsigned ufind = 0, bit_counts = 0; + nir_foreach_block(block, b.impl) { + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_alu) + continue; + nir_op op = nir_instr_as_alu(instr)->op; + ufind += (op == nir_op_ufind_msb || op == nir_op_find_lsb); + bit_counts += (op == nir_op_bit_count); + } + } + EXPECT_EQ(ufind, 0u); + EXPECT_GE(bit_counts, 2u); + ralloc_free(b.shader); + } +} + +TEST(AGX3VisionOpts, Wave8FoldSignedPow2Idiv) +{ + /* Test folding imul(imin(imax(a, -1), 1), ushr(iabs(a), k)) for k in [1, 30] + * and verify exactness across boundary values including INT32_MIN. */ + const int32_t samples[] = { + INT32_MIN, INT32_MIN + 1, -1025, -1024, -257, -256, -255, + -3, -2, -1, 0, 1, 2, 3, 255, 256, 257, 1024, INT32_MAX - 1, INT32_MAX + }; + + for (unsigned k : {1u, 4u, 8u, 16u, 30u}) { + SCOPED_TRACE(k); + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "wave8_signed_pow2_idiv"); + nir_def *a = nir_load_global(&b, 1, 32, nir_imm_int64(&b, 0x1000), .align_mul = 4); + nir_def *sgn = nir_imin(&b, nir_imax(&b, a, nir_imm_int(&b, -1)), nir_imm_int(&b, 1)); + nir_def *mag = nir_ushr_imm(&b, nir_iabs(&b, a), k); + nir_def *res = nir_imul(&b, sgn, mag); + nir_store_global(&b, res, nir_imm_int64(&b, 0x2000), .align_mul = 4); + + EXPECT_TRUE(agx_nir_fuse_algebraic_late(b.shader)); + NIR_PASS(_, b.shader, nir_opt_dce); + nir_validate_shader(b.shader, "wave8 signed pow2 idiv"); + + unsigned imuls = 0, iabses = 0; + nir_foreach_block(block, b.impl) { + nir_foreach_instr(instr, block) { + if (instr->type != nir_instr_type_alu) + continue; + nir_op op = nir_instr_as_alu(instr)->op; + imuls += (op == nir_op_imul); + iabses += (op == nir_op_iabs); + } + } + EXPECT_EQ(imuls, 0u); + EXPECT_EQ(iabses, 0u); + + /* Algebraic oracle: (a + ((a >> 31) >>> (32 - k))) >> k == sign(a) * (|a| >> k) */ + for (int32_t v : samples) { + int32_t sgn_v = v < 0 ? -1 : (v > 0 ? 1 : 0); + uint32_t abs_v = v == INT32_MIN ? 0x80000000u : (uint32_t)(v < 0 ? -v : v); + int32_t ref_v = (int32_t)((uint32_t)sgn_v * (abs_v >> k)); + uint32_t bias = ((uint32_t)(v >> 31)) >> (32u - k); + int32_t folded_v = ((int32_t)((uint32_t)v + bias)) >> k; + EXPECT_EQ(folded_v, ref_v) << "v=" << v << " k=" << k; + } + ralloc_free(b.shader); + } + + /* Negative: k = 31 must NOT fold (32 - 31 = 1, INT32_MIN + 0x7fffffff overflows). */ + { + nir_builder b = nir_builder_init_simple_shader( + MESA_SHADER_COMPUTE, &agx_nir_options, "wave8_signed_pow2_idiv_k31"); + nir_def *a = nir_load_global(&b, 1, 32, nir_imm_int64(&b, 0x1000), .align_mul = 4); + nir_def *sgn = nir_imin(&b, nir_imax(&b, a, nir_imm_int(&b, -1)), nir_imm_int(&b, 1)); + nir_def *mag = nir_ushr_imm(&b, nir_iabs(&b, a), 31); + nir_def *res = nir_imul(&b, sgn, mag); + nir_store_global(&b, res, nir_imm_int64(&b, 0x2000), .align_mul = 4); + + agx_nir_fuse_algebraic_late(b.shader); + NIR_PASS(_, b.shader, nir_opt_dce); + unsigned imuls = 0; + nir_foreach_block(block, b.impl) { + nir_foreach_instr(instr, block) { + if (instr->type == nir_instr_type_alu && + nir_instr_as_alu(instr)->op == nir_op_imul) + imuls++; + } + } + EXPECT_EQ(imuls, 1u); + ralloc_free(b.shader); + } +} +