From d88b2e4ab5a770b8119f0e10d2d5389263cb0c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timur=20Krist=C3=B3f?= Date: Sun, 14 Aug 2022 15:15:18 +0200 Subject: [PATCH] aco: Fix invalidated reference in branching sequence optimization. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Inserting in the instructions vector may invalidate the exec_val reference, so do that last. Fixes: baab6f18c91 ("aco: Optimize branching sequence during SSA elimination.") Signed-off-by: Timur Kristóf Reviewed-by: Georg Lehmann Part-of: --- src/amd/compiler/aco_ssa_elimination.cpp | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/amd/compiler/aco_ssa_elimination.cpp b/src/amd/compiler/aco_ssa_elimination.cpp index 3f18b09f812..a077df6505f 100644 --- a/src/amd/compiler/aco_ssa_elimination.cpp +++ b/src/amd/compiler/aco_ssa_elimination.cpp @@ -411,16 +411,6 @@ try_optimize_branching_sequence(ssa_elimination_ctx& ctx, Block& block, const in exec_copy->operands[0] = Operand(exec, ctx.program->lane_mask); } - if (save_original_exec) { - /* Insert a new instruction that saves the original exec before it is overwritten. */ - const auto it = std::next(block.instructions.begin(), save_original_exec_idx); - aco_ptr copy( - create_instruction(aco_opcode::p_parallelcopy, Format::PSEUDO, 1, 1)); - copy->definitions[0] = exec_copy_def; - copy->operands[0] = Operand(exec, ctx.program->lane_mask); - block.instructions.insert(it, std::move(copy)); - } - if (exec_val->opcode == aco_opcode::p_parallelcopy && exec_val->operands[0].isConstant() && exec_val->operands[0].constantValue()) { /* Remove the branch instruction when exec is constant non-zero. */ @@ -428,6 +418,19 @@ try_optimize_branching_sequence(ssa_elimination_ctx& ctx, Block& block, const in if (branch->isBranch() && branch->operands.size() && branch->operands[0].physReg() == exec) block.instructions.back().reset(); } + + if (save_original_exec) { + /* Insert a new instruction that saves the original exec before it is overwritten. + * Do this last, because inserting in the instructions vector may invalidate the exec_val + * reference. + */ + const auto it = std::next(block.instructions.begin(), save_original_exec_idx); + aco_ptr copy( + create_instruction(aco_opcode::p_parallelcopy, Format::PSEUDO, 1, 1)); + copy->definitions[0] = exec_copy_def; + copy->operands[0] = Operand(exec, ctx.program->lane_mask); + block.instructions.insert(it, std::move(copy)); + } } void