Skip to content
Merged
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
212 changes: 212 additions & 0 deletions src/asahi/compiler/agx_nir_lower_shared_bitsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1352,6 +1352,18 @@ known_zero_bits_u32(nir_scalar s, unsigned depth)
uint32_t low = (1u << k) - 1u;
return (known_zero_bits_u32(c, depth - 1) << k) | low;
}
bool unit_mul =
(nir_scalar_is_const(a) && nir_scalar_as_uint(a) == 1) ||
(nir_scalar_is_const(b) && nir_scalar_as_uint(b) == 1);
if (unit_mul) {
nir_scalar prod =
(nir_scalar_is_const(b) && nir_scalar_as_uint(b) == 1) ? a : b;
uint32_t kz_prod = known_zero_bits_u32(prod, depth - 1);
uint32_t kz_c = known_zero_bits_u32(c, depth - 1);
uint32_t kz_shifted = k == 0 ? kz_c : ((kz_c << k) | ((1u << k) - 1u));
if ((~kz_prod & ~kz_shifted) == 0)
return kz_prod & kz_shifted;
}
uint32_t tz_prod =
trailing_zeros_u32(a, depth - 1) + trailing_zeros_u32(b, depth - 1);
uint32_t tz_c = trailing_zeros_u32(c, depth - 1);
Expand Down Expand Up @@ -1957,6 +1969,91 @@ emit_bit_slice(nir_builder *b, const struct agx_bit_slice *sl)
return sl->dst_off ? nir_ishl_imm(b, v, sl->dst_off) : v;
}

static uint64_t
u32_upper_bound(nir_scalar s, unsigned depth)
{
const unsigned bits = s.def->bit_size;
if (bits != 16 && bits != 32)
return UINT32_MAX;
const uint64_t maxv = bits == 32 ? UINT32_MAX : UINT16_MAX;
if (nir_scalar_is_const(s))
return nir_scalar_as_uint(s) & maxv;
if (depth == 0 || !nir_scalar_is_alu(s))
return maxv;

switch (nir_scalar_alu_op(s)) {
case nir_op_mov:
return u32_upper_bound(nir_scalar_chase_alu_src(s, 0), depth - 1);
case nir_op_u2u16:
case nir_op_u2u32: {
uint64_t b = u32_upper_bound(nir_scalar_chase_alu_src(s, 0), depth - 1);
return b <= maxv ? b : maxv;
}
case nir_op_iand: {
uint64_t b0 = u32_upper_bound(nir_scalar_chase_alu_src(s, 0), depth - 1);
uint64_t b1 = u32_upper_bound(nir_scalar_chase_alu_src(s, 1), depth - 1);
return MIN2(b0, b1);
}
case nir_op_ushr: {
nir_scalar sh = nir_scalar_chase_alu_src(s, 1);
if (!nir_scalar_is_const(sh))
return maxv;
unsigned k = (unsigned)(nir_scalar_as_uint(sh) & (bits - 1));
return u32_upper_bound(nir_scalar_chase_alu_src(s, 0), depth - 1) >> k;
}
case nir_op_ishl: {
nir_scalar sh = nir_scalar_chase_alu_src(s, 1);
if (!nir_scalar_is_const(sh))
return maxv;
unsigned k = (unsigned)(nir_scalar_as_uint(sh) & (bits - 1));
uint64_t b = u32_upper_bound(nir_scalar_chase_alu_src(s, 0), depth - 1);
return b <= (maxv >> k) ? (b << k) : maxv;
}
case nir_op_iadd: {
uint64_t b0 = u32_upper_bound(nir_scalar_chase_alu_src(s, 0), depth - 1);
uint64_t b1 = u32_upper_bound(nir_scalar_chase_alu_src(s, 1), depth - 1);
return b0 <= maxv - b1 ? b0 + b1 : maxv;
}
case nir_op_imul:
case nir_op_amul: {
uint64_t b0 = u32_upper_bound(nir_scalar_chase_alu_src(s, 0), depth - 1);
uint64_t b1 = u32_upper_bound(nir_scalar_chase_alu_src(s, 1), depth - 1);
return (b0 == 0 || b1 <= maxv / b0) ? b0 * b1 : maxv;
}
case nir_op_imadshl_agx: {
nir_scalar sh = nir_scalar_chase_alu_src(s, 3);
if (!nir_scalar_is_const(sh))
return maxv;
unsigned k = (unsigned)(nir_scalar_as_uint(sh) & (bits - 1));
uint64_t b0 = u32_upper_bound(nir_scalar_chase_alu_src(s, 0), depth - 1);
uint64_t b1 = u32_upper_bound(nir_scalar_chase_alu_src(s, 1), depth - 1);
uint64_t b2 = u32_upper_bound(nir_scalar_chase_alu_src(s, 2), depth - 1);
if (b0 != 0 && b1 > maxv / b0)
return maxv;
uint64_t phi = b0 * b1;
if (b2 > (maxv >> k))
return maxv;
uint64_t shi = b2 << k;
return phi <= maxv - shi ? phi + shi : maxv;
}
case nir_op_ubitfield_extract: {
nir_scalar w = nir_scalar_chase_alu_src(s, 2);
if (!nir_scalar_is_const(w))
return maxv;
unsigned nbits = (unsigned)nir_scalar_as_uint(w);
if (nbits == 0)
return 0;
return nbits < bits ? ((1ull << nbits) - 1ull) : maxv;
}
case nir_op_bcsel: {
uint64_t b1 = u32_upper_bound(nir_scalar_chase_alu_src(s, 1), depth - 1);
uint64_t b2 = u32_upper_bound(nir_scalar_chase_alu_src(s, 2), depth - 1);
return MAX2(b1, b2);
}
default:
return maxv;
}
}
struct bitfield_slice_state {
struct hash_table *ranges;
nir_function_impl *impl;
Expand Down Expand Up @@ -2170,6 +2267,115 @@ opt_bitfield_slice_instr(nir_builder *b, nir_alu_instr *alu, void *data)
return true;
}

/* 3. Fuse disjoint ior(a, b << sh) (1 <= sh <= 4) into a single shifted add
* imadshl_agx(a, 1, b, sh) when the shifted operand is single-use and the bit
* positions cannot overlap. Runs after slice merging so inner ior nodes do not
* hide adjacent bitfield slices from collect_add_or_terms(). */
static bool
opt_disjoint_ior_instr(nir_builder *b, nir_alu_instr *alu, void *data)
{
struct hash_table *ranges = bitfield_slice_ranges(data, b->impl);
if (alu->op != nir_op_ior || alu->def.bit_size != 32 ||
alu->def.num_components != 1)
return false;

b->cursor = nir_before_instr(&alu->instr);

nir_scalar s[2] = {
nir_scalar_resolved(alu->src[0].src.ssa, alu->src[0].swizzle[0]),
nir_scalar_resolved(alu->src[1].src.ssa, alu->src[1].swizzle[0]),
};
for (unsigned side = 0; side < 2; side++) {
nir_scalar sh_s = s[side];
nir_scalar other = s[1 - side];
if (!nir_scalar_is_alu(sh_s) || sh_s.def->num_components != 1 ||
!list_is_singular(&sh_s.def->uses))
continue;
nir_scalar base;
unsigned k = 0;
nir_op sop = nir_scalar_alu_op(sh_s);
/* 3b. Fuse disjoint ior(a, iand(ushr(x, s), ((1<<w)-1)<<tz)) with
* 1 <= tz <= 4 into imadshl_agx(a, 1, ubfx(x, s + tz, w), tz)
* when s + tz + w <= 32 is provably non-wrapping (eliminates the
* separate mask instruction and fuses shift+or into shifted iadd). */
if (sop == nir_op_iand) {
nir_scalar a0 = nir_scalar_chase_alu_src(sh_s, 0);
nir_scalar a1 = nir_scalar_chase_alu_src(sh_s, 1);
nir_scalar inner, mc;
if (nir_scalar_is_const(a1) && !nir_scalar_is_const(a0)) {
inner = a0;
mc = a1;
} else if (nir_scalar_is_const(a0) && !nir_scalar_is_const(a1)) {
inner = a1;
mc = a0;
} else {
continue;
}
uint32_t mask = (uint32_t)nir_scalar_as_uint(mc);
if (mask == 0)
continue;
unsigned tz = (unsigned)__builtin_ctz(mask);
uint32_t low_mask = mask >> tz;
if (tz < 1 || tz > 4 || low_mask >= UINT32_MAX ||
!util_is_power_of_two_nonzero(low_mask + 1u))
continue;
unsigned w = (unsigned)(ffsll((long long)(low_mask + 1u)) - 1);
if (w == 0 || tz + w >= 32)
continue;
if ((~known_zero_bits_u32(other, 5) & mask) != 0)
continue;
if (!nir_scalar_is_alu(inner) ||
nir_scalar_alu_op(inner) != nir_op_ushr ||
inner.def->num_components != 1 ||
!list_is_singular(&inner.def->uses))
continue;
nir_scalar x = nir_scalar_chase_alu_src(inner, 0);
nir_scalar shift_s = nir_scalar_chase_alu_src(inner, 1);
uint64_t s_hi = MIN2(
(uint64_t)nir_unsigned_upper_bound(b->shader, ranges, shift_s),
u32_upper_bound(shift_s, 8));
if (s_hi > 32u - (tz + w))
continue;
nir_def *off =
nir_iadd_imm(b, nir_mov_scalar(b, shift_s), (int)tz);
nir_def *ext = nir_ubitfield_extract(
b, nir_mov_scalar(b, x), off, nir_imm_int(b, w));
nir_def *rep = nir_imadshl_agx(
b, nir_mov_scalar(b, other), nir_imm_int(b, 1), ext,
nir_imm_int(b, tz));
nir_def_replace(&alu->def, rep);
return true;
}
if (sop == nir_op_ishl) {
nir_scalar ks = nir_scalar_chase_alu_src(sh_s, 1);
if (!nir_scalar_is_const(ks))
continue;
k = (unsigned)(nir_scalar_as_uint(ks) & 31);
base = nir_scalar_chase_alu_src(sh_s, 0);
} else {
nir_scalar ma, mb;
unsigned msh = 0;
if (!is_imadshl_add_shift(sh_s, &ma, &mb, &msh) ||
!nir_scalar_is_const(ma) || nir_scalar_as_uint(ma) != 0)
continue;
k = msh;
base = mb;
}
if (k < 1 || k > 4)
continue;
uint32_t nz_other = ~known_zero_bits_u32(other, 5);
uint32_t nz_shifted = (~known_zero_bits_u32(base, 5)) << k;
if ((nz_other & nz_shifted) == 0) {
nir_def *rep = nir_imadshl_agx(
b, nir_mov_scalar(b, other), nir_imm_int(b, 1),
nir_mov_scalar(b, base), nir_imm_int(b, k));
nir_def_replace(&alu->def, rep);
return true;
}
}
return false;
}

bool
agx_nir_opt_bitfield_slices(nir_shader *shader)
{
Expand All @@ -2184,6 +2390,12 @@ agx_nir_opt_bitfield_slices(nir_shader *shader)
if (!p)
break;
}
if (progress) {
nir_opt_dce(shader);
_mesa_hash_table_clear(state.ranges, NULL);
}
progress |= nir_shader_alu_pass(shader, opt_disjoint_ior_instr,
nir_metadata_control_flow, &state);
_mesa_hash_table_destroy(state.ranges, NULL);
return progress;
}
Loading