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
22 changes: 16 additions & 6 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -13433,8 +13433,12 @@ agx_shift_upper_bound(nir_shader *shader, struct hash_table *ranges,
}

static bool
opt_redundant_shift_mask_alu(nir_builder *b, nir_alu_instr *alu, void *data)
opt_redundant_shift_mask(nir_builder *b, nir_instr *instr, 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_iand || alu->def.num_components != 1 ||
alu->def.bit_size != 32)
return false;
Expand All @@ -13459,13 +13463,19 @@ opt_redundant_shift_mask_alu(nir_builder *b, nir_alu_instr *alu, void *data)
return false;
}

static bool
bool
agx_nir_opt_redundant_shift_masks(nir_shader *shader)
{
struct hash_table *ranges = _mesa_pointer_hash_table_create(NULL);
bool progress = nir_shader_alu_pass(shader, opt_redundant_shift_mask_alu,
nir_metadata_control_flow, ranges);
_mesa_hash_table_destroy(ranges, NULL);
/* nir_unsigned_upper_bound() caches by SSA index, which every
* implementation numbers from zero, so each gets a table of its own.
*/
bool progress = false;
nir_foreach_function_impl(impl, shader) {
struct hash_table *ranges = _mesa_pointer_hash_table_create(NULL);
progress |= nir_function_instructions_pass(
impl, opt_redundant_shift_mask, nir_metadata_control_flow, ranges);
_mesa_hash_table_destroy(ranges, NULL);
}
return progress;
}

Expand Down
1 change: 1 addition & 0 deletions src/asahi/compiler/agx_nir.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ bool agx_nir_opt_shared_index_reassoc(struct nir_shader *s);
#define G13X_I32_SHARED_INDEX_UNIT_B 16
bool agx_nir_opt_g13x_shared_offsets(struct nir_shader *s);
bool agx_nir_opt_bitfield_slices(struct nir_shader *s);
bool agx_nir_opt_redundant_shift_masks(struct nir_shader *shader);
bool agx_nir_reassociate_loop_invariant_eq(struct nir_shader *shader);
bool agx_nir_opt_sink_past_loops(struct nir_shader *shader);
bool agx_nir_opt_licm(struct nir_shader *shader, unsigned budget,
Expand Down
29 changes: 25 additions & 4 deletions src/asahi/compiler/agx_nir_lower_shared_bitsize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1957,10 +1957,29 @@ 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;
}

struct bitfield_slice_state {
struct hash_table *ranges;
nir_function_impl *impl;
};

/* nir_unsigned_upper_bound() caches by SSA index, which every implementation
* numbers from zero, so each one starts with an empty table. Otherwise a bound
* found in the main shader is used for an unrelated value of the preamble. */
static struct hash_table *
bitfield_slice_ranges(struct bitfield_slice_state *state,
nir_function_impl *impl)
{
if (state->impl != impl) {
_mesa_hash_table_clear(state->ranges, NULL);
state->impl = impl;
}
return state->ranges;
}

static bool
opt_bitfield_slice_instr(nir_builder *b, nir_alu_instr *alu, void *data)
{
struct hash_table *ranges = data;
struct hash_table *ranges = bitfield_slice_ranges(data, b->impl);
if (alu->def.bit_size != 32 || alu->def.num_components != 1)
return false;

Expand Down Expand Up @@ -2154,15 +2173,17 @@ opt_bitfield_slice_instr(nir_builder *b, nir_alu_instr *alu, void *data)
bool
agx_nir_opt_bitfield_slices(nir_shader *shader)
{
struct hash_table *ranges = _mesa_pointer_hash_table_create(NULL);
struct bitfield_slice_state state = {
.ranges = _mesa_pointer_hash_table_create(NULL),
};
bool progress = false;
for (unsigned pass = 0; pass < 2; pass++) {
bool p = nir_shader_alu_pass(shader, opt_bitfield_slice_instr,
nir_metadata_control_flow, ranges);
nir_metadata_control_flow, &state);
progress |= p;
if (!p)
break;
}
_mesa_hash_table_destroy(ranges, NULL);
_mesa_hash_table_destroy(state.ranges, NULL);
return progress;
}
37 changes: 37 additions & 0 deletions src/asahi/compiler/test/test-agx3c-subgroup-reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,43 @@ TEST(AGX3SubgroupShuffleXor, StripsRedundantLateShiftMask)
ralloc_free(b.shader);
}

/* Every implementation numbers its SSA defs from zero and the range cache is
* keyed by index, so a bound proven in the main shader must not answer for the
* def with the same index in the preamble. AGX shifts by 32 and up give zero,
* so dropping the mask of an unbounded shift count changes the result. */
TEST(AGX3SubgroupShuffleXor, ShiftMaskRangesStayInTheirImpl)
{
nir_builder b = nir_builder_init_simple_shader(
MESA_SHADER_COMPUTE, &agx_nir_options, "late-shift-mask-two-impls");
b.shader->info.workgroup_size[0] = 16;
b.shader->info.workgroup_size[1] = 1;
b.shader->info.workgroup_size[2] = 1;

nir_def *bounded = nir_load_local_invocation_index(&b);
nir_intrinsic_instr *main = nir_store_global(
&b, nir_iand_imm(&b, bounded, 31), nir_imm_int64(&b, 0), .align_mul = 4);

nir_builder p =
nir_builder_at(nir_after_impl(nir_shader_get_preamble(b.shader)));
nir_def *unbounded = agx_test_load_preamble(&p, 1, 32, 0);
nir_intrinsic_instr *preamble =
agx_test_store_preamble(&p, nir_iand_imm(&p, unbounded, 31), 2);
ASSERT_EQ(bounded->index, unbounded->index);

EXPECT_TRUE(agx_nir_opt_redundant_shift_masks(b.shader));
nir_validate_shader(b.shader, "late-shift-mask-two-impls result");

/* The main shader's mask is redundant, the preamble's is not. */
EXPECT_EQ(nir_scalar_chase_movs(nir_get_scalar(main->src[0].ssa, 0)).def,
bounded);
nir_scalar count =
nir_scalar_chase_movs(nir_get_scalar(preamble->src[0].ssa, 0));
EXPECT_NE(count.def, unbounded);
ASSERT_TRUE(nir_scalar_is_alu(count));
EXPECT_EQ(nir_scalar_alu_op(count), nir_op_iand);
ralloc_free(b.shader);
}

struct agx3b_decoded_counts {
unsigned instrs;
unsigned srediadd;
Expand Down
42 changes: 42 additions & 0 deletions src/asahi/compiler/test/test-shared-byte-offsets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,48 @@ TEST(SharedByteOffsets, BitfieldSlicesOverBudgetPreservesValue)
ralloc_free(b.shader);
}

/* Every implementation numbers its SSA defs from zero and the range cache is
* keyed by index, so a bound proven in the main shader must not answer for the
* def with the same index in the preamble: ushr(ishl(x, 12), 4) of an unbounded
* x would keep the bits that x << 12 drops. */
TEST(SharedByteOffsets, BitfieldSlicesRangesStayInTheirImpl)
{
for (bool imadshl : {false, true}) {
nir_builder b = workgroup_shader("bitfield-slices-two-impls");
nir_builder p = nir_builder_at(
nir_after_impl(nir_shader_get_preamble(b.shader)));

auto build = [&](nir_builder *c, uint32_t mask) {
nir_def *x = nir_load_push_constant(c, 1, 32, nir_imm_int(c, 0),
.range = 4);
nir_def *v = nir_iand_imm(c, x, mask);
nir_def *shl = imadshl ? nir_imadshl_agx(c, nir_imm_int(c, 0),
nir_imm_int(c, 1), v,
nir_imm_int(c, 4))
: nir_ishl_imm(c, v, 12);
return nir_store_global(c, nir_ushr_imm(c, shl, imadshl ? 2 : 4),
nir_imm_int64(c, 0), .align_mul = 4);
};
nir_intrinsic_instr *main = build(&b, 0xffu);
nir_intrinsic_instr *preamble = build(&p, 0x7fffffffu);

const uint32_t x = 0xffffffffu;
const uint32_t main_before = eval_u32_nir(main->src[0].ssa, &x);
const uint32_t preamble_before = eval_u32_nir(preamble->src[0].ssa, &x);
ASSERT_EQ(main->src[0].ssa->index, preamble->src[0].ssa->index);

EXPECT_TRUE(agx_nir_opt_bitfield_slices(b.shader));
nir_opt_dce(b.shader);
nir_validate_shader(b.shader, "bitfield-slices-two-impls result");

EXPECT_EQ(nir_def_as_alu(main->src[0].ssa)->op, nir_op_ishl);
EXPECT_EQ(nir_def_as_alu(preamble->src[0].ssa)->op, nir_op_ushr);
EXPECT_EQ(eval_u32_nir(main->src[0].ssa, &x), main_before);
EXPECT_EQ(eval_u32_nir(preamble->src[0].ssa, &x), preamble_before);
ralloc_free(b.shader);
}
}

/* imadshl_agx(a, 1, b, k) and iand(ushr(x, k), mask) merge into one extract. */
TEST(SharedByteOffsets, BitfieldSlicesImadshlAndShiftedMask)
{
Expand Down