Module: Mesa
Branch: main
Commit: d88b2e4ab5a770b8119f0e10d2d5389263cb0c36
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=d88b2e4ab5a770b8119f0e10d2d5389263cb0c36

Author: Timur Kristóf <[email protected]>
Date:   Sun Aug 14 15:15:18 2022 +0200

aco: Fix invalidated reference in branching sequence optimization.

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 <[email protected]>
Reviewed-by: Georg Lehmann <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/18049>

---

 src/amd/compiler/aco_ssa_elimination.cpp | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 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,8 +411,19 @@ try_optimize_branching_sequence(ssa_elimination_ctx& ctx, 
Block& block, const in
       exec_copy->operands[0] = Operand(exec, ctx.program->lane_mask);
    }
 
+   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. */
+      aco_ptr<Instruction>& branch = block.instructions.back();
+      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. */
+      /* 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<Instruction> copy(
          create_instruction<Pseudo_instruction>(aco_opcode::p_parallelcopy, 
Format::PSEUDO, 1, 1));
@@ -420,14 +431,6 @@ try_optimize_branching_sequence(ssa_elimination_ctx& ctx, 
Block& block, const in
       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. */
-      aco_ptr<Instruction>& branch = block.instructions.back();
-      if (branch->isBranch() && branch->operands.size() && 
branch->operands[0].physReg() == exec)
-         block.instructions.back().reset();
-   }
 }
 
 void

Reply via email to