aco: re-use existing phi instruction when lowering boolean phis
authorDaniel Schürmann <daniel@schuermann.dev>
Mon, 7 Oct 2019 00:32:54 +0000 (02:32 +0200)
committerDaniel Schürmann <daniel@schuermann.dev>
Wed, 9 Oct 2019 15:50:23 +0000 (17:50 +0200)
Reviewed-by: Rhys Perry <pendingchaos02@gmail.com>
src/amd/compiler/aco_lower_bool_phis.cpp

index 8cfc0eee0cdf7ceac374cbf6ec91f7d86701848b..7a4940ab1c83e5c66aebe19503b7a6d17f7ff020 100644 (file)
@@ -136,11 +136,12 @@ void insert_before_logical_end(Block *block, aco_ptr<Instruction> instr)
       block->instructions.insert(std::prev(it.base()), std::move(instr));
 }
 
-aco_ptr<Instruction> lower_divergent_bool_phi(Program *program, Block *block, aco_ptr<Instruction>& phi)
+void lower_divergent_bool_phi(Program *program, Block *block, aco_ptr<Instruction>& phi)
 {
    Builder bld(program);
 
    ssa_state state;
+   state.latest[block->index] = phi->definitions[0].tempId();
    for (unsigned i = 0; i < phi->operands.size(); i++) {
       Block *pred = &program->blocks[block->logical_preds[i]];
 
@@ -177,7 +178,20 @@ aco_ptr<Instruction> lower_divergent_bool_phi(Program *program, Block *block, ac
       }
    }
 
-   return bld.sop1(aco_opcode::s_mov_b64, phi->definitions[0], get_ssa(program, block->index, &state)).get_ptr();
+   unsigned num_preds = block->linear_preds.size();
+   if (phi->operands.size() != num_preds) {
+      Pseudo_instruction* new_phi{create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi, Format::PSEUDO, num_preds, 1)};
+      new_phi->definitions[0] = phi->definitions[0];
+      phi.reset(new_phi);
+   } else {
+      phi->opcode = aco_opcode::p_linear_phi;
+   }
+   assert(phi->operands.size() == num_preds);
+
+   for (unsigned i = 0; i < num_preds; i++)
+      phi->operands[i] = get_ssa(program, block->linear_preds[i], &state);
+
+   return;
 }
 
 void lower_linear_bool_phi(Program *program, Block *block, aco_ptr<Instruction>& phi)
@@ -213,7 +227,8 @@ void lower_bool_phis(Program* program)
          if (phi->opcode != aco_opcode::p_phi && phi->opcode != aco_opcode::p_linear_phi)
             break;
          if (phi->opcode == aco_opcode::p_phi && phi->definitions[0].regClass() == s2) {
-            non_phi.emplace_back(std::move(lower_divergent_bool_phi(program, &block, phi)));
+            lower_divergent_bool_phi(program, &block, phi);
+            block.instructions.emplace_back(std::move(phi));
          } else if (phi->opcode == aco_opcode::p_linear_phi && phi->definitions[0].regClass() == s1) {
             /* if it's a valid non-boolean phi, this should be a no-op */
             lower_linear_bool_phi(program, &block, phi);