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
10 changes: 8 additions & 2 deletions src/asahi/compiler/agx3_pack_binary.c
Original file line number Diff line number Diff line change
Expand Up @@ -24140,12 +24140,18 @@ agx3_pack_binary(agx_context *ctx, struct util_dynarray *emission)
value.memory || value.abs || value.neg || value.kill ||
(value.size != AGX_SIZE_16 && value.size != AGX_SIZE_32) ||
agx_channels(value) != 1 || shift.memory ||
shift.size != AGX_SIZE_32 || agx_channels(shift) != 1 ||
(shift.size != AGX_SIZE_32 &&
(shift.size != AGX_SIZE_16 || agx_is_g18g(&ctx->key->dev))) ||
agx_channels(shift) != 1 ||
shift.abs || shift.neg || shift.kill ||
(value.cache && value.discard) || (shift.cache && shift.discard))
AGX3_REFUSE(I);
/* A count widened from a half arrives as that half: f39 is the
* count's own width, as in the extract by a half count below.
* G18G keeps the widening (agx_optimizer_copyprop). */
const uint16_t src[2] = {value.value, shift.value};
const bool narrow[2] = {value.size == AGX_SIZE_16, false};
const bool narrow[2] = {value.size == AGX_SIZE_16,
shift.size == AGX_SIZE_16};
const enum agx3_alu_hint hints[2] = {
agx3_post_ra_hint(value),
shift.cache ? AGX3_ALU_HINT_NONE : agx3_post_ra_hint(shift),
Expand Down
78 changes: 78 additions & 0 deletions src/asahi/compiler/test/test-agx3-pack-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13051,6 +13051,84 @@ TEST_F(AGX3PackBinary, NativeBfeilExtractPreservesSourceLifetime)
}
}

/* Apple's one-bit extract of a word by a half count, the witness of
* agx3_alu_bfeil_m5() in test-agx3-m5-instructions.c without its producer wait:
* f39 is the count's width and f57 the extract width. A plain right shift is
* the same instruction with f57 clear. */
TEST_F(AGX3PackBinary, M5RightShiftReadsHalfCountLikeTheNativeExtract)
{
struct agx3_encoded_end end;
ASSERT_TRUE(agx3_pack_end(AGX_ISA_ENCODING_AGX3C, &end));

for (bool extract : {false, true}) {
agx_builder *b = builder(AGX_ISA_ENCODING_AGX3C);
agx_index value = agx_register(0, AGX_SIZE_32);
agx_index count = agx_register(4, AGX_SIZE_16);
value.discard = count.discard = true;
agx_bfeil_to(b, agx_register(0, AGX_SIZE_32), agx_immediate(0), value,
count, extract ? 1 : 0);
agx_stop(b);

const uint8_t native[] = {
0x27, 0x00, 0x00, 0x1a, 0x26, 0x00, 0xa1,
0x02, 0x08, 0x01, 0x20, 0x34, 0x00, (uint8_t)(extract ? 0x10 : 0x00),
};
std::vector<uint8_t> want(std::begin(native), std::end(native));
append(want, end.bytes, end.length);
EXPECT_EQ(pack(b), want) << (extract ? "extract" : "shift");
}
}

/* Every width of the value and the count is one f24/f39 choice of the same
* form, at any half register. G18G keeps the count's explicit widening
* (agx_optimizer_copyprop), so only its all-half shift reads a half count. */
TEST_F(AGX3PackBinary, M5RightShiftTakesEitherCountWidth)
{
struct agx3_encoded_end end;
ASSERT_TRUE(agx3_pack_end(AGX_ISA_ENCODING_AGX3C, &end));

for (bool g18g : {false, true}) {
key.dev.emit_agx3d = g18g;
for (unsigned widths = 0; widths < 8; ++widths) {
const bool half[] = {(widths & 1) != 0, (widths & 2) != 0};
const bool dest_half = (widths & 4) != 0;
/* A word value does not narrow into a half result. */
if (dest_half && !half[0])
continue;

agx_builder *b = builder(AGX_ISA_ENCODING_AGX3C);
const uint16_t src[] = {(uint16_t)(half[0] ? 3 : 2),
(uint16_t)(half[1] ? 7 : 6)};
agx_index value =
agx_register(src[0], half[0] ? AGX_SIZE_16 : AGX_SIZE_32);
agx_index count =
agx_register(src[1], half[1] ? AGX_SIZE_16 : AGX_SIZE_32);
count.discard = true;
agx_bfeil_to(b,
agx_register(8, dest_half ? AGX_SIZE_16 : AGX_SIZE_32),
agx_immediate(0), value, count, 0);
agx_stop(b);

const bool all_half = dest_half && half[0] && half[1];
if (g18g && half[1] && !all_half) {
EXPECT_TRUE(pack(b, false).empty()) << widths;
continue;
}

const enum agx3_alu_hint hints[] = {AGX3_ALU_HINT_NONE,
AGX3_ALU_HINT_DISCARD};
uint8_t bytes[AGX3_ALU_MAX_LENGTH], length;
ASSERT_TRUE(agx3_alu_srl_reg(AGX_ISA_ENCODING_AGX3C, 8, dest_half,
false, src, half, hints, 0, bytes,
&length));
std::vector<uint8_t> want;
append(want, bytes, length);
append(want, end.bytes, end.length);
EXPECT_EQ(pack(b), want) << widths << " g18g=" << g18g;
}
}
}

TEST_F(AGX3PackBinary, HalfBfeilRetainsCachedDynamicShift)
{
auto build = [this](bool cache_shift) {
Expand Down
41 changes: 41 additions & 0 deletions src/asahi/compiler/test/test-agx3c-subgroup-reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,47 @@ TEST(AGX3SubgroupShuffleXor, FoldsConstantXorShuffle32And16OnAGX3B)
ralloc_free(b.shader);
}

/* Once the pass drops `count & 31`, copy propagation hands the shift the half
* the count was widened from. Every target has to encode the result. */
TEST(AGX3SubgroupShuffleXor, ShiftsByStrippedHalfCountsCompile)
{
for (unsigned chip = 0; chip < AGX_CHIP_UNKNOWN; ++chip) {
for (nir_op op : {nir_op_ushr, nir_op_ishl, nir_op_ishr}) {
for (bool sysval : {false, true}) {
nir_builder b = nir_builder_init_simple_shader(
MESA_SHADER_COMPUTE, &agx_nir_options, "half-shift-count");
b.shader->info.internal = false;
b.shader->info.io_lowered = true;
b.shader->info.workgroup_size[0] = 32;
b.shader->info.workgroup_size[1] = 1;
b.shader->info.workgroup_size[2] = 1;

nir_def *addr = nir_load_preamble(&b, 1, 64, .base = 16);
nir_def *val = nir_load_global(&b, 1, 32, addr, .align_mul = 4);
nir_def *count =
sysval ? nir_load_local_invocation_index(&b)
: nir_u2u32(&b, nir_iand_imm(
&b, nir_load_global(&b, 1, 16, addr, .align_mul = 4),
15));
nir_def *res = nir_build_alu2(&b, op, val, count);
nir_store_global(&b, res, addr, .align_mul = 4);

struct agx_shader_key key = {};
agx_device_key_set_target(&key.dev, (enum agx_chip)chip);
key.reserved_preamble = 24;
struct agx_shader_part out = {};
EXPECT_TRUE(agx_compile_shader_nir(b.shader, &key, &out))
<< agx_chip_infos[chip].name << " " << nir_op_infos[op].name
<< (sysval ? " local index" : " ushort");
EXPECT_NE(out.binary, nullptr);

free(out.binary);
ralloc_free(b.shader);
}
}
}
}

TEST(AGX3SubgroupShuffleXor, StripsRedundantLateShiftMask)
{
nir_builder b = nir_builder_init_simple_shader(
Expand Down