[llvm] [VPlan] Replace VPRecipeOrVPValue with VP2VP recipe simplification. (PR #76090)

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 28 06:38:33 PST 2023


================
@@ -806,6 +806,38 @@ static unsigned getOpcodeForRecipe(VPRecipeBase &R) {
 
 /// Try to simplify recipe \p R.
 static void simplifyRecipe(VPRecipeBase &R, VPTypeAnalysis &TypeInfo) {
+  // Try to remove redundant blend recipes.
+  if (auto *Blend = dyn_cast<VPBlendRecipe>(&R)) {
+    if (Blend->getNumIncomingValues() == 1) {
+      Blend->replaceAllUsesWith(Blend->getIncomingValue(0));
+      Blend->eraseFromParent();
+      return;
+    }
+
+    bool AllEqual = true;
+    for (unsigned I = 1; I != Blend->getNumIncomingValues(); ++I)
+      AllEqual &= Blend->getIncomingValue(0) == Blend->getIncomingValue(I);
+    if (AllEqual) {
+      Blend->replaceAllUsesWith(Blend->getIncomingValue(0));
+      Blend->eraseFromParent();
+      return;
+    }
+    if (Blend->getNumIncomingValues() != 2)
+      return;
+    auto IsInLoopReduction = [](VPValue *VPV) {
+      auto *PhiR = dyn_cast<VPReductionPHIRecipe>(VPV);
+      return PhiR && PhiR->isInLoop();
+    };
+    if (IsInLoopReduction(Blend->getIncomingValue(0))) {
+      Blend->replaceAllUsesWith(Blend->getIncomingValue(1));
+      Blend->eraseFromParent();
+    } else if (IsInLoopReduction(Blend->getIncomingValue(1))) {
+      Blend->replaceAllUsesWith(Blend->getIncomingValue(0));
+      Blend->eraseFromParent();
+    }
----------------
ayalz wrote:

The **mandatory** removal of a blend for conditional in-loop reductions should probably better be placed alongside its folding into Reduction recipe, by adjustRecipesForReductions(), than as a standalone redundancy elimination **optimization** here.

I.e., folding
```
  S = phi(init, S")
  S' = S + a[i]
  S" = Cond ? S' : S
```
into
```
  S = phi(init, S')  // in-loop Reduction Phi recipe
  Inc = Cond ? a[i] : identity
  S' = S + Inc   // in-loop Reduction recipe
  // S" = Cond ? S' : S  // when in-loop vectorized, drop or use AnyOf(Cond)
```

(This refers to 12fb133eba819)

Note that a similar blending-of-the-increment rather than of the post-incremented value, may also apply to non in-loop reductions - as an optional optimization. This could be useful, e.g., when a blend-of-load-with-identity can be replaced by setting the passthru operand of a masked load to `identity`, as in the above example.

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


More information about the llvm-commits mailing list