[PATCH] D117140: [LV] Always create VPWidenCanonicalIVRecipe, optimize away later.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 12 12:17:44 PST 2022


fhahn created this revision.
fhahn added reviewers: Ayal, gilr, rengolin.
Herald added subscribers: rogfer01, hiraditya.
fhahn requested review of this revision.
Herald added a subscriber: vkmr.
Herald added a project: LLVM.

This patch updates createBlockInMask to always generate
VPWidenCanonicalIVRecipe and adds a transform to optimize it away later,
if it is not needed.

This is a step towards breaking up VPWidenIntOrFpInductionRecipe and
explicitly distinguishing between vector phis and scalarizing.

Split off from D116123 <https://reviews.llvm.org/D116123>.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117140

Files:
  llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
  llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
  llvm/lib/Transforms/Vectorize/VPlanTransforms.h


Index: llvm/lib/Transforms/Vectorize/VPlanTransforms.h
===================================================================
--- llvm/lib/Transforms/Vectorize/VPlanTransforms.h
+++ llvm/lib/Transforms/Vectorize/VPlanTransforms.h
@@ -45,6 +45,12 @@
   /// in the vectorized loop. There is no need to vectorize the cast - the same
   /// value can be used for both the phi and casts in the vector loop.
   static void removeRedundantInductionCasts(VPlan &Plan);
+
+  /// Try to replace VPWidenCanonicalIVRecipes with the primary IV recipe, if it
+  /// exists.
+  static void
+  removeRedundantVPWidenCanonicalIVRecipe(VPlan &Plan,
+                                          Value *OriginalCanonicalIV);
 };
 
 } // namespace llvm
Index: llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -324,3 +324,32 @@
     E.first->eraseFromParent();
   }
 }
+
+void VPlanTransforms::removeRedundantVPWidenCanonicalIVRecipe(
+    VPlan &Plan, Value *OriginalCanonicalIV) {
+  if (!OriginalCanonicalIV)
+    return;
+
+  VPBasicBlock *HeaderVPBB = Plan.getVectorLoopRegion()->getEntryBasicBlock();
+  VPWidenCanonicalIVRecipe *WidenNewIV = nullptr;
+  VPWidenIntOrFpInductionRecipe *WidenOriginalIV = nullptr;
+  for (VPRecipeBase &Phi : *HeaderVPBB) {
+    if (auto *IV = dyn_cast<VPWidenIntOrFpInductionRecipe>(&Phi)) {
+      // If the induction recipe is for the primary induction  use it directly.
+      if (IV->getUnderlyingValue() == OriginalCanonicalIV)
+        WidenOriginalIV = IV;
+      continue;
+    }
+
+    if (auto *W = dyn_cast<VPWidenCanonicalIVRecipe>(&Phi))
+      WidenNewIV = W;
+  }
+
+  if (!WidenNewIV)
+    return;
+
+  if (WidenOriginalIV) {
+    WidenNewIV->replaceAllUsesWith(WidenOriginalIV);
+    WidenNewIV->eraseFromParent();
+  }
+}
Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -8412,15 +8412,8 @@
     assert(CM.foldTailByMasking() && "must fold the tail");
     VPBasicBlock *HeaderVPBB = Plan->getEntry()->getEntryBasicBlock();
     auto NewInsertionPoint = HeaderVPBB->getFirstNonPhi();
-
-    VPValue *IV = nullptr;
-    if (Legal->getPrimaryInduction())
-      IV = Plan->getOrAddVPValue(Legal->getPrimaryInduction());
-    else {
-      auto *IVRecipe = new VPWidenCanonicalIVRecipe();
-      HeaderVPBB->insert(IVRecipe, NewInsertionPoint);
-      IV = IVRecipe;
-    }
+    auto *IV = new VPWidenCanonicalIVRecipe();
+    HeaderVPBB->insert(IV, HeaderVPBB->getFirstNonPhi());
 
     VPBuilder::InsertPointGuard Guard(Builder);
     Builder.setInsertPoint(HeaderVPBB, NewInsertionPoint);
@@ -9195,6 +9188,9 @@
     }
   }
 
+  // Check if \p I has only scalar uses.
+  VPlanTransforms::removeRedundantVPWidenCanonicalIVRecipe(
+      *Plan, Legal->getPrimaryInduction());
   VPlanTransforms::removeRedundantInductionCasts(*Plan);
 
   // Now that sink-after is done, move induction recipes for optimized truncates


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117140.399418.patch
Type: text/x-patch
Size: 3206 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220112/988754ff/attachment.bin>


More information about the llvm-commits mailing list