Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 6 additions & 19 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand Down
17 changes: 16 additions & 1 deletion src/asahi/compiler/agx_nir_algebraic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down
192 changes: 185 additions & 7 deletions src/asahi/compiler/agx_nir_lower_subgroups.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;

Expand Down Expand Up @@ -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 */
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand Down
Loading