[llvm] [VPlan] Introduce ComputeReductionResult VPInstruction opcode. (PR #70253)

Alexey Bataev via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 20 07:43:40 PST 2023


================
@@ -7769,6 +7592,46 @@ SCEV2ValueTy LoopVectorizationPlanner::executePlan(
 
   BestVPlan.execute(&State);
 
+  DenseMap<const RecurrenceDescriptor *, Value *> ReductionResumeValues;
+  BasicBlock *OrigScalarPH = OrigLoop->getLoopPreheader();
+  // Check if ResResult is a CompueReductionResult instruction, and if it is
+  // return the merge phi node for it.
+  auto GetMergePhiForReduction = [&State,
+                                  OrigScalarPH](VPInstruction *RedResult)
+      -> std::pair<const RecurrenceDescriptor *, PHINode *> {
+    if (!RedResult ||
+        RedResult->getOpcode() != VPInstruction::ComputeReductionResult)
+      return {nullptr, nullptr};
+
+    Value *FinalValue =
+        State.get(RedResult, VPIteration(State.UF - 1,
+                                         VPLane::getLastLaneForVF(State.VF)));
+    auto *PhiR = cast<VPReductionPHIRecipe>(RedResult->getOperand(0));
+    assert(FinalValue->hasOneUse() || PhiR->isInLoop() ||
+           PhiR->getRecurrenceDescriptor().IntermediateStore &&
+               "unexpected final value");
+
+    // Find the merge phi, which must be located in the preheader of the
+    // original scalar loop.
+    PHINode *MergePhi = nullptr;
+    for (Value *U : FinalValue->users()) {
+      auto *P = dyn_cast<PHINode>(U);
+      if (P && OrigScalarPH == P->getParent()) {
+        MergePhi = P;
+        break;
+      }
+    }
+    assert(MergePhi && "must have merge phi");
+    return {&PhiR->getRecurrenceDescriptor(), MergePhi};
+  };
+  for (VPRecipeBase &R : *cast<VPBasicBlock>(
+           BestVPlan.getVectorLoopRegion()->getSingleSuccessor())) {
+    const auto &[RedRdx, MergePhi] =
+        GetMergePhiForReduction(dyn_cast<VPInstruction>(&R));
----------------
alexey-bataev wrote:

```suggestion
    auto *I = dyn_cast<VPInstruction>(&R);
    if (!I)
      continue;
    const auto &[RedRdx, MergePhi] = GetMergePhiForReduction(I);
```

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


More information about the llvm-commits mailing list