From 662a23a16b50a5b1d219f4c30c6f656ba460ff40 Mon Sep 17 00:00:00 2001 From: DjDeveloperr Date: Wed, 2 Dec 2026 21:51:32 -0500 Subject: [PATCH] agx: accept half-register counts in M5 right shifts agx_nir_opt_redundant_shift_masks() drops the mask of a shift count that is already small enough. The count is then often the zero extension of a half, such as gl_LocalInvocationIndex, and copy propagation folds that extension into the shift on M5 (it keeps it on G18G and AGX3B). Left shifts and extracts already take such a count. The plain right shift refused it, so `d >> gl_LocalInvocationIndex` failed to compile with "AGX3 binary packer refused bfeil". The encoding is known. The srl form of the agx3c table takes the count's width in a field of its own, f39, whatever the other operands and the extract width f57 are. Apple's extract of a word by a half count is that instruction with f39 = 1 (agx3_alu_bfeil_m5() in test-agx3-m5-instructions.c), and so is the all-half shift. agx3_alu_srl_reg() already encodes it, so let the packer pass the width on. The packer only takes a form it refused, so no shader that compiled before changes. G18G keeps the widening and the refusal. Fixes: fc22dafe903 ("agx: fold constant-XOR shuffles, enable 16-bit shuffle_xor, and strip redundant late shift masks") Signed-off-by: DjDeveloperr --- src/asahi/compiler/agx3_pack_binary.c | 10 ++- .../compiler/test/test-agx3-pack-binary.cpp | 78 +++++++++++++++++++ .../test/test-agx3c-subgroup-reduce.cpp | 41 ++++++++++ 3 files changed, 127 insertions(+), 2 deletions(-) diff --git a/src/asahi/compiler/agx3_pack_binary.c b/src/asahi/compiler/agx3_pack_binary.c index a980e277a4f..79f616e9f79 100644 --- a/src/asahi/compiler/agx3_pack_binary.c +++ b/src/asahi/compiler/agx3_pack_binary.c @@ -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), diff --git a/src/asahi/compiler/test/test-agx3-pack-binary.cpp b/src/asahi/compiler/test/test-agx3-pack-binary.cpp index 88c1fb9259d..576eaab7abf 100644 --- a/src/asahi/compiler/test/test-agx3-pack-binary.cpp +++ b/src/asahi/compiler/test/test-agx3-pack-binary.cpp @@ -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 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 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) { diff --git a/src/asahi/compiler/test/test-agx3c-subgroup-reduce.cpp b/src/asahi/compiler/test/test-agx3c-subgroup-reduce.cpp index b756ba916fe..d696006fb42 100644 --- a/src/asahi/compiler/test/test-agx3c-subgroup-reduce.cpp +++ b/src/asahi/compiler/test/test-agx3c-subgroup-reduce.cpp @@ -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(