[llvm] [VPlan] Introduce ExitPhi VPInstruction, use to create phi for FOR. (PR #94760)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 8 06:00:58 PDT 2024


================
@@ -8635,6 +8604,49 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
          "VPBasicBlock");
   RecipeBuilder.fixHeaderPhis();
 
+  auto *MiddleVPBB =
+      cast<VPBasicBlock>(Plan->getVectorLoopRegion()->getSingleSuccessor());
+
+  VPBasicBlock *ScalarPH = nullptr;
+  for (VPBlockBase *Succ : MiddleVPBB->getSuccessors()) {
+    auto *VPBB = dyn_cast<VPBasicBlock>(Succ);
+    if (VPBB && !isa<VPIRBasicBlock>(VPBB)) {
+      ScalarPH = VPBB;
+      break;
+    }
+  }
+
+  if (ScalarPH) {
+    for (auto &H : HeaderVPBB->phis()) {
+      auto *FOR = dyn_cast<VPFirstOrderRecurrencePHIRecipe>(&H);
+      if (!FOR)
+        continue;
+      VPBuilder B(ScalarPH);
+      VPBuilder MiddleBuilder;
+      // Set insert point so new recipes are inserted before terminator and
+      // condition, if there is either the former or both.
+      if (MiddleVPBB->getNumSuccessors() != 2)
+        MiddleBuilder.setInsertPoint(MiddleVPBB);
+      else if (isa<VPInstruction>(MiddleVPBB->getTerminator()->getOperand(0)))
+        MiddleBuilder.setInsertPoint(
+            &*std::prev(MiddleVPBB->getTerminator()->getIterator()));
+      else
+        MiddleBuilder.setInsertPoint(MiddleVPBB->getTerminator());
----------------
ayalz wrote:

```suggestion
      VPBuilder MiddleBuilder(MiddleVPBB);
      // Reset insert point so new recipes are inserted before terminator and
      // condition, if there is either the former or both.
      if (auto *Terminator = MiddleVPBB->getTerminator()) {
        auto *Condition = dyn_cast<VPInstruction>(Terminator->getOperand(0));
        assert((!Condition || Condition->parent() == MiddleVPBB) && "Condition expected in MiddleVPBB");
        MiddleBuilder.setInsertPoint(Condition ? Condition : Terminator);
      }
```

https://github.com/llvm/llvm-project/pull/94760


More information about the llvm-commits mailing list