From 55130069b8bbb7abf4de31f4925be9fdfc11101f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Sch=C3=BCrmann?= Date: Mon, 8 Apr 2024 13:47:25 +0200 Subject: [PATCH] aco/vn: copy-propagate trivial phis Totals from 154 (0.19% of 79395) affected shaders: (GFX11) Instrs: 102420 -> 101702 (-0.70%); split: -0.71%, +0.01% CodeSize: 534060 -> 530620 (-0.64%); split: -0.65%, +0.01% Latency: 560180 -> 559723 (-0.08%); split: -0.10%, +0.01% InvThroughput: 62769 -> 61708 (-1.69%); split: -1.72%, +0.03% Copies: 6929 -> 6260 (-9.66%); split: -9.68%, +0.03% Branches: 1636 -> 1609 (-1.65%) PreVGPRs: 5913 -> 5906 (-0.12%) VALU: 52681 -> 52012 (-1.27%); split: -1.27%, +0.00% Part-of: --- src/amd/compiler/aco_opt_value_numbering.cpp | 29 +++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/amd/compiler/aco_opt_value_numbering.cpp b/src/amd/compiler/aco_opt_value_numbering.cpp index 4e3e3548429..4f46f386d69 100644 --- a/src/amd/compiler/aco_opt_value_numbering.cpp +++ b/src/amd/compiler/aco_opt_value_numbering.cpp @@ -313,6 +313,21 @@ can_eliminate(aco_ptr& instr) return true; } +bool +is_trivial_phi(Block& block, Instruction* instr) +{ + if (!is_phi(instr)) + return false; + + /* Logical LCSSA phis must be kept in order to prevent the optimizer + * from doing invalid transformations. */ + if (instr->opcode == aco_opcode::p_phi && (block.kind & block_kind_loop_exit)) + return false; + + return std::all_of(instr->operands.begin(), instr->operands.end(), + [&](Operand& op) { return op == instr->operands[0]; }); +} + void process_block(vn_ctx& ctx, Block& block) { @@ -333,14 +348,9 @@ process_block(vn_ctx& ctx, Block& block) instr->opcode == aco_opcode::p_demote_to_helper || instr->opcode == aco_opcode::p_end_wqm) ctx.exec_id++; - if (!can_eliminate(instr)) { - new_instructions.emplace_back(std::move(instr)); - continue; - } - /* simple copy-propagation through renaming */ bool copy_instr = - instr->opcode == aco_opcode::p_parallelcopy || + is_trivial_phi(block, instr.get()) || instr->opcode == aco_opcode::p_parallelcopy || (instr->opcode == aco_opcode::p_create_vector && instr->operands.size() == 1); if (copy_instr && !instr->definitions[0].isFixed() && instr->operands[0].isTemp() && instr->operands[0].regClass() == instr->definitions[0].regClass()) { @@ -348,6 +358,11 @@ process_block(vn_ctx& ctx, Block& block) continue; } + if (!can_eliminate(instr)) { + new_instructions.emplace_back(std::move(instr)); + continue; + } + instr->pass_flags = ctx.exec_id; std::pair res = ctx.expr_values.emplace(instr.get(), block.index); @@ -388,7 +403,7 @@ void rename_phi_operands(Block& block, aco::unordered_map& renames) { for (aco_ptr& phi : block.instructions) { - if (phi->opcode != aco_opcode::p_phi && phi->opcode != aco_opcode::p_linear_phi) + if (!is_phi(phi)) break; for (Operand& op : phi->operands) {