From e7ffc799d1a41788bcab27ee3058318f1acb246b Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Wed, 30 Aug 2023 09:58:04 -0400 Subject: [PATCH] agx: Fuse conditions into if's Simple greedy thing that has the potential to inflate register pressure but reduces instructions. Thanks to the recent loop work that turns if { break } into while_icmp, this also implicitly handles fusing conditions into loops, which is what actually prompted this. Surprisingly, this helps register pressure on my shader-db (no change to thread count), I guess by eliminating the boolean temps in case where the sources are used multiple times. total instructions in shared programs: 1786561 -> 1784943 (-0.09%) instructions in affected programs: 128557 -> 126939 (-1.26%) helped: 474 HURT: 13 Instructions are helped. total bytes in shared programs: 11733236 -> 11720734 (-0.11%) bytes in affected programs: 976034 -> 963532 (-1.28%) helped: 521 HURT: 13 Bytes are helped. total halfregs in shared programs: 474245 -> 474094 (-0.03%) halfregs in affected programs: 1869 -> 1718 (-8.08%) helped: 28 HURT: 7 Halfregs are helped. Signed-off-by: Alyssa Rosenzweig Part-of: --- src/asahi/compiler/agx_optimizer.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/asahi/compiler/agx_optimizer.c b/src/asahi/compiler/agx_optimizer.c index bd74af7..88149ef 100644 --- a/src/asahi/compiler/agx_optimizer.c +++ b/src/asahi/compiler/agx_optimizer.c @@ -243,6 +243,38 @@ agx_optimizer_copyprop(agx_instr **defs, agx_instr *I) } } +/* + * Fuse conditions into if. Specifically, acts on if_icmp and fuses: + * + * if_icmp(cmp(x, y, *), 0, ne) -> if_cmp(x, y, *) + */ +static void +agx_optimizer_if_cmp(agx_instr **defs, agx_instr *I) +{ + /* Check for unfused if */ + if (!agx_is_equiv(I->src[1], agx_zero()) || I->icond != AGX_ICOND_UEQ || + !I->invert_cond || I->src[0].type != AGX_INDEX_NORMAL) + return; + + /* Check for condition */ + agx_instr *def = defs[I->src[0].value]; + if (def->op != AGX_OPCODE_ICMP && def->op != AGX_OPCODE_FCMP) + return; + + /* Fuse */ + I->src[0] = def->src[0]; + I->src[1] = def->src[1]; + I->invert_cond = def->invert_cond; + + if (def->op == AGX_OPCODE_ICMP) { + I->op = AGX_OPCODE_IF_ICMP; + I->icond = def->icond; + } else { + I->op = AGX_OPCODE_IF_FCMP; + I->fcond = def->fcond; + } +} + static void agx_optimizer_forward(agx_context *ctx) { @@ -269,6 +301,9 @@ agx_optimizer_forward(agx_context *ctx) I->op != AGX_OPCODE_UNIFORM_STORE && I->op != AGX_OPCODE_BLOCK_IMAGE_STORE) agx_optimizer_inline_imm(defs, I, info.nr_srcs, info.is_float); + + if (I->op == AGX_OPCODE_IF_ICMP) + agx_optimizer_if_cmp(defs, I); } free(defs); -- 2.7.4