Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
186 changes: 185 additions & 1 deletion src/asahi/compiler/agx_nir_lower_shared_bitsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,92 @@ 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;
}
}

static bool
opt_bitfield_slice_instr(nir_builder *b, nir_alu_instr *alu, void *data)
{
Expand Down Expand Up @@ -2127,8 +2213,106 @@ opt_bitfield_slice_instr(nir_builder *b, nir_alu_instr *alu, void *data)
}
} while (changed);

if (!merged_any)
if (!merged_any) {
/* 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. */
if (alu->op == nir_op_ior) {
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;
}

nir_def *acc = NULL;
for (unsigned i = 0; i < n; i++) {
Expand Down
61 changes: 61 additions & 0 deletions src/asahi/compiler/test/test-shared-byte-offsets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2137,3 +2137,64 @@ TEST(SharedByteOffsets, SinkEpilogueGatedToAgx3b)
free(out_3c.binary);
ralloc_free(s_3c);
}

/* Disjoint ior(a, b << k) (1 <= k <= 4) and ior(a, (x >> s) & mask) with a
* proven non-wrapping shift fuse into imadshl_agx(a, 1, ..., k); overlapping
* bits or an unbounded shift that could wrap across 32 bits stay untouched. */
TEST(SharedByteOffsets, BitfieldSlicesDisjointIorShlAndShiftedMask)
{
nir_builder b = nir_builder_init_simple_shader(MESA_SHADER_COMPUTE, NULL,
"disjoint-ior-shl-mask");
nir_def *x = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 0), .range = 4);
nir_def *y = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 4), .range = 4);
nir_def *idx = nir_load_push_constant(&b, 1, 32, nir_imm_int(&b, 8), .range = 4);

/* Case 1: (x & 0x0f0f) | ((y & 0x0303) << 4) -> imadshl_agx */
nir_def *lo = nir_iand_imm(&b, x, 0x0f0fu);
nir_def *hi = nir_ishl_imm(&b, nir_iand_imm(&b, y, 0x0303u), 4);
nir_def *r1 = nir_ior(&b, lo, hi);

/* Case 2: ubfx(x, 0, 4) | ((y >> s_safe) & 0x30) where s_safe <= 26 */
nir_def *s_base = nir_ishl_imm(&b, nir_ubitfield_extract_imm(&b, idx, 0, 2), 3);
nir_def *s_safe = nir_bcsel(&b, nir_ult_imm(&b, idx, 4), s_base,
nir_iadd_imm(&b, s_base, 2));
nir_def *a2 = nir_ubitfield_extract_imm(&b, x, 0, 4);
nir_def *b2 = nir_iand_imm(&b, nir_ushr(&b, y, s_safe), 0x30u);
nir_def *r2 = nir_ior(&b, a2, b2);

/* Case 3 (negative): unbounded shift idx could be 28 -> must NOT fuse. */
nir_def *a3 = nir_ubitfield_extract_imm(&b, x, 0, 4);
nir_def *b3 = nir_iand_imm(&b, nir_ushr(&b, y, idx), 0x30u);
nir_def *r3 = nir_ior(&b, a3, b3);

/* Case 4 (negative): overlapping bit positions (bit 4) -> must NOT fuse. */
nir_def *lo4 = nir_iand_imm(&b, x, 0x1fu);
nir_def *hi4 = nir_ishl_imm(&b, nir_iand_imm(&b, y, 0x0fu), 4);
nir_def *r4 = nir_ior(&b, lo4, hi4);

nir_store_global(&b, r1, nir_imm_int64(&b, 0), .align_mul = 4);
nir_store_global(&b, r2, nir_imm_int64(&b, 4), .align_mul = 4);
nir_store_global(&b, r3, nir_imm_int64(&b, 8), .align_mul = 4);
nir_store_global(&b, r4, nir_imm_int64(&b, 12), .align_mul = 4);

EXPECT_TRUE(agx_nir_opt_bitfield_slices(b.shader));
nir_opt_dce(b.shader);
nir_validate_shader(b.shader, "disjoint-ior-shl-mask");

unsigned iors = 0, imadshls = 0;
nir_foreach_block(blk, b.impl) {
nir_foreach_instr(instr, blk) {
if (instr->type != nir_instr_type_alu)
continue;
nir_op op = nir_instr_as_alu(instr)->op;
if (op == nir_op_ior)
iors++;
else if (op == nir_op_imadshl_agx)
imadshls++;
}
}
/* Cases 1 and 2 fused to imadshl_agx; Cases 3 and 4 kept their ior. */
EXPECT_EQ(imadshls, 2u);
EXPECT_EQ(iors, 2u);
ralloc_free(b.shader);
}