From 6d56c8bc02ac2a2720b2bc00e3c6f7c9d92175ec Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Wed, 30 Oct 2024 12:21:48 -0400 Subject: [PATCH] agx: fold zext into int sources Signed-off-by: Alyssa Rosenzweig Part-of: --- src/asahi/compiler/agx_optimizer.c | 42 +++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/asahi/compiler/agx_optimizer.c b/src/asahi/compiler/agx_optimizer.c index 059dd2086d9..3ab4a336769 100644 --- a/src/asahi/compiler/agx_optimizer.c +++ b/src/asahi/compiler/agx_optimizer.c @@ -248,6 +248,35 @@ agx_optimizer_fmov_rev(agx_instr *I, agx_instr *use) return true; } +static bool +agx_supports_zext(agx_instr *I, unsigned s) +{ + switch (I->op) { + case AGX_OPCODE_IADD: + case AGX_OPCODE_IMAD: + case AGX_OPCODE_ICMP: + case AGX_OPCODE_INTL: + case AGX_OPCODE_FFS: + case AGX_OPCODE_BITREV: + case AGX_OPCODE_BFI: + case AGX_OPCODE_BFEIL: + case AGX_OPCODE_EXTR: + case AGX_OPCODE_BITOP: + case AGX_OPCODE_WHILE_ICMP: + case AGX_OPCODE_IF_ICMP: + case AGX_OPCODE_ELSE_ICMP: + case AGX_OPCODE_BREAK_IF_ICMP: + case AGX_OPCODE_ICMP_BALLOT: + case AGX_OPCODE_ICMP_QUAD_BALLOT: + return true; + case AGX_OPCODE_ICMPSEL: + /* Only the comparisons can be extended, not the selection */ + return s < 2; + default: + return false; + } +} + static void agx_optimizer_copyprop(agx_context *ctx, agx_instr **defs, agx_instr *I) { @@ -264,7 +293,8 @@ agx_optimizer_copyprop(agx_context *ctx, agx_instr **defs, agx_instr *I) * RA pseudo instructions don't handle size conversions. This should be * refined in the future. */ - if (def->src[0].size != src.size) + if (def->src[0].size != src.size && + !(def->src[0].size < src.size && agx_supports_zext(I, s))) continue; /* Optimize split(64-bit uniform) so we can get better copyprop of the @@ -301,6 +331,16 @@ agx_optimizer_copyprop(agx_context *ctx, agx_instr **defs, agx_instr *I) continue; agx_replace_src(I, s, def->src[0]); + + /* If we are zero-extending into an instruction that distinguishes sign + * and zero extend, make sure we pick zero-extend. + */ + if (def->src[0].size < src.size && + (I->op == AGX_OPCODE_IMAD || I->op == AGX_OPCODE_IADD)) { + + assert(agx_supports_zext(I, s)); + I->src[s].abs = true; + } } }