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