[PATCH] D77577: [LV] Expose the primary IV as part of the plan, lit trip count (NFC).

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 6 12:31:02 PDT 2020


fhahn created this revision.
Herald added subscribers: rogfer01, rkruppe, bollu, hiraditya.
Herald added a reviewer: rengolin.
Herald added a project: LLVM.

VPRecipeBuilder::createBlockInMask currently directly uses
Legal->getPrimaryInduction(). This tightly couples mask code-generation
with Legal. This patch introduces a new PrimaryIV VPValue to the VPlan
(similar to the BackedgeTakenCount), which can be used for masking,
instead of querying legal directly.

This uncouples code-generation from Legal and allows for changing the
primary IV before code-generation.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77577

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


Index: llvm/lib/Transforms/Vectorize/VPlan.h
===================================================================
--- llvm/lib/Transforms/Vectorize/VPlan.h
+++ llvm/lib/Transforms/Vectorize/VPlan.h
@@ -332,6 +332,8 @@
   /// Hold the trip count of the scalar loop.
   Value *TripCount = nullptr;
 
+  Value *PrimaryIV = nullptr;
+
   /// Hold a pointer to InnerLoopVectorizer to reuse its IR generation methods.
   InnerLoopVectorizer *ILV;
 
@@ -1455,6 +1457,9 @@
   /// the tail.
   VPValue *BackedgeTakenCount = nullptr;
 
+  /// Represents the primary IV of the original loop.
+  VPValue *PrimaryIV = nullptr;
+
   /// Holds a mapping between Values and their corresponding VPValue inside
   /// VPlan.
   Value2VPValueTy Value2VPValue;
@@ -1475,10 +1480,12 @@
     if (Entry)
       VPBlockBase::deleteCFG(Entry);
     for (auto &MapEntry : Value2VPValue)
-      if (MapEntry.second != BackedgeTakenCount)
+      if (MapEntry.second != BackedgeTakenCount && MapEntry.second != PrimaryIV)
         delete MapEntry.second;
     if (BackedgeTakenCount)
       delete BackedgeTakenCount; // Delete once, if in Value2VPValue or not.
+    if (PrimaryIV)
+      delete PrimaryIV; // Delete once, if in Value2VPValue or not.
     for (VPValue *Def : VPExternalDefs)
       delete Def;
     for (VPValue *CBV : VPCBVs)
@@ -1504,6 +1511,12 @@
     return BackedgeTakenCount;
   }
 
+  VPValue *getOrCreatePrimaryIV() {
+    if (!PrimaryIV)
+      PrimaryIV = new VPValue();
+    return PrimaryIV;
+  }
+
   void addVF(unsigned VF) { VFs.insert(VF); }
 
   bool hasVF(unsigned VF) { return VFs.count(VF); }
Index: llvm/lib/Transforms/Vectorize/VPlan.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -444,6 +444,8 @@
                                    "trip.count.minus.1");
     Value2VPValue[TCMO] = BackedgeTakenCount;
   }
+  if (PrimaryIV && PrimaryIV->getNumUsers())
+    Value2VPValue[State->PrimaryIV] = PrimaryIV;
 
   // 0. Set the reverse mapping from VPValues to Values for code generation.
   for (auto &Entry : Value2VPValue)
Index: llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
===================================================================
--- llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -6584,6 +6584,7 @@
                          &ILV,   CallbackILV};
   State.CFG.PrevBB = ILV.createVectorizedLoopSkeleton();
   State.TripCount = ILV.getOrCreateTripCount(nullptr);
+  State.PrimaryIV = ILV.Legal->getPrimaryInduction();
 
   //===------------------------------------------------===//
   //
@@ -6770,7 +6771,7 @@
 
     // Introduce the early-exit compare IV <= BTC to form header block mask.
     // This is used instead of IV < TC because TC may wrap, unlike BTC.
-    VPValue *IV = Plan->getVPValue(Legal->getPrimaryInduction());
+    VPValue *IV = Plan->getOrCreatePrimaryIV();
     VPValue *BTC = Plan->getOrCreateBackedgeTakenCount();
     BlockMask = Builder.createNaryOp(VPInstruction::ICmpULE, {IV, BTC});
     return BlockMaskCache[BB] = BlockMask;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D77577.255436.patch
Type: text/x-patch
Size: 3151 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200406/71bd4dd9/attachment.bin>


More information about the llvm-commits mailing list