[llvm] [VPlan] Track VPValue names in VPlan. (PR #81411)

via llvm-commits llvm-commits at lists.llvm.org
Sun Feb 11 05:42:51 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Florian Hahn (fhahn)

<details>
<summary>Changes</summary>

This patch restructures the way names for printing VPValues are handled. It updates VPlan to track assigned names for VPValues (similar to how LLVMContext tracks names for LLVM IR Values). Tracking names in a single place allows to deduplicate names and ensure all names are unique.

Now there are the following cases:
 * If a VPValue has a defining recipe, its name is determined by checking its associated VPlan's mapping. The name is wrapped in vp<>. If no VPlan is associated, <badref> is used.

 * If a VPValue is a live-in with an underlying IR value, the name of the IR value is used and wrapped in ir<>.

 * If none of the above applies, the number assigned by VPSlotTracker is used wrapped in vp<> if an associated VPlan is provided or <badref> otherwise.

Note that contrary to LLMV's Value which always can retrieve LLVMContext, this is not possible for VPValues without defining recipe, so in some cases, the associated VPlan needs to be provided explicitly.

The new handling subsumes VPInstruction's manual handling of its name.

---

Patch is 133.80 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/81411.diff


24 Files Affected:

- (modified) llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h (+13-10) 
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+8-5) 
- (modified) llvm/lib/Transforms/Vectorize/VPlan.cpp (+71-11) 
- (modified) llvm/lib/Transforms/Vectorize/VPlan.h (+52-14) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp (+5-4) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp (+7-5) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp (+11-5) 
- (modified) llvm/lib/Transforms/Vectorize/VPlanValue.h (+19-1) 
- (modified) llvm/test/Transforms/LoopVectorize/AArch64/sve-tail-folding-forced.ll (+7-7) 
- (modified) llvm/test/Transforms/LoopVectorize/AArch64/sve-widen-gep.ll (+10-10) 
- (modified) llvm/test/Transforms/LoopVectorize/AArch64/synthesize-mask-for-call.ll (+42-42) 
- (modified) llvm/test/Transforms/LoopVectorize/AArch64/widen-call-with-intrinsic-or-libfunc.ll (+14-14) 
- (modified) llvm/test/Transforms/LoopVectorize/RISCV/riscv-vector-reverse.ll (+18-18) 
- (modified) llvm/test/Transforms/LoopVectorize/first-order-recurrence-chains-vplan.ll (+23-23) 
- (modified) llvm/test/Transforms/LoopVectorize/first-order-recurrence-sink-replicate-region.ll (+66-66) 
- (modified) llvm/test/Transforms/LoopVectorize/icmp-uniforms.ll (+5-5) 
- (modified) llvm/test/Transforms/LoopVectorize/interleave-and-scalarize-only.ll (+10-10) 
- (modified) llvm/test/Transforms/LoopVectorize/vplan-dot-printing.ll (+7-7) 
- (modified) llvm/test/Transforms/LoopVectorize/vplan-iv-transforms.ll (+3-3) 
- (modified) llvm/test/Transforms/LoopVectorize/vplan-printing-outer-loop.ll (+13-13) 
- (modified) llvm/test/Transforms/LoopVectorize/vplan-printing.ll (+126-126) 
- (modified) llvm/test/Transforms/LoopVectorize/vplan-sink-scalars-and-merge-vf1.ll (+4-4) 
- (modified) llvm/test/Transforms/LoopVectorize/vplan-sink-scalars-and-merge.ll (+125-125) 
- (modified) llvm/test/Transforms/LoopVectorize/vplan-unused-interleave-group.ll (+3-3) 


``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
index a7ebf78e54ceb6..5aa4892bf0ffe5 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h
@@ -47,16 +47,19 @@ class VPBuilder {
   VPBasicBlock::iterator InsertPt = VPBasicBlock::iterator();
 
   /// Insert \p VPI in BB at InsertPt if BB is set.
-  VPInstruction *tryInsertInstruction(VPInstruction *VPI) {
-    if (BB)
+  VPInstruction *tryInsertInstruction(VPInstruction *VPI,
+                                      const Twine &Name = "") {
+    if (BB) {
       BB->insert(VPI, InsertPt);
+      VPI->setName(Name);
+    }
     return VPI;
   }
 
   VPInstruction *createInstruction(unsigned Opcode,
                                    ArrayRef<VPValue *> Operands, DebugLoc DL,
                                    const Twine &Name = "") {
-    return tryInsertInstruction(new VPInstruction(Opcode, Operands, DL, Name));
+    return tryInsertInstruction(new VPInstruction(Opcode, Operands, DL), Name);
   }
 
   VPInstruction *createInstruction(unsigned Opcode,
@@ -150,7 +153,7 @@ class VPBuilder {
                                      VPRecipeWithIRFlags::WrapFlagsTy WrapFlags,
                                      DebugLoc DL = {}, const Twine &Name = "") {
     return tryInsertInstruction(
-        new VPInstruction(Opcode, Operands, WrapFlags, DL, Name));
+        new VPInstruction(Opcode, Operands, WrapFlags, DL), Name);
   }
   VPValue *createNot(VPValue *Operand, DebugLoc DL = {},
                      const Twine &Name = "") {
@@ -170,12 +173,12 @@ class VPBuilder {
   VPValue *createSelect(VPValue *Cond, VPValue *TrueVal, VPValue *FalseVal,
                         DebugLoc DL = {}, const Twine &Name = "",
                         std::optional<FastMathFlags> FMFs = std::nullopt) {
-    auto *Select =
-        FMFs ? new VPInstruction(Instruction::Select, {Cond, TrueVal, FalseVal},
-                                 *FMFs, DL, Name)
-             : new VPInstruction(Instruction::Select, {Cond, TrueVal, FalseVal},
-                                 DL, Name);
-    return tryInsertInstruction(Select);
+    auto *Select = FMFs
+                       ? new VPInstruction(Instruction::Select,
+                                           {Cond, TrueVal, FalseVal}, *FMFs, DL)
+                       : new VPInstruction(Instruction::Select,
+                                           {Cond, TrueVal, FalseVal}, DL);
+    return tryInsertInstruction(Select, Name);
   }
 
   /// Create a new ICmp VPInstruction with predicate \p Pred and operands \p A
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 1a7b301c35f2b8..d08aa8fcb12c24 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -7262,7 +7262,7 @@ VPValue *VPBuilder::createICmp(CmpInst::Predicate Pred, VPValue *A, VPValue *B,
   assert(Pred >= CmpInst::FIRST_ICMP_PREDICATE &&
          Pred <= CmpInst::LAST_ICMP_PREDICATE && "invalid predicate");
   return tryInsertInstruction(
-      new VPInstruction(Instruction::ICmp, Pred, A, B, DL, Name));
+      new VPInstruction(Instruction::ICmp, Pred, A, B, DL), Name);
 }
 
 // This function will select a scalable VF if the target supports scalable
@@ -8610,11 +8610,11 @@ static void addCanonicalIVRecipes(VPlan &Plan, Type *IdxTy, bool HasNUW,
   // IV by VF * UF.
   auto *CanonicalIVIncrement =
       new VPInstruction(Instruction::Add, {CanonicalIVPHI, &Plan.getVFxUF()},
-                        {HasNUW, false}, DL, "index.next");
+                        {HasNUW, false}, DL);
   CanonicalIVPHI->addOperand(CanonicalIVIncrement);
 
   VPBasicBlock *EB = TopRegion->getExitingBasicBlock();
-  EB->appendRecipe(CanonicalIVIncrement);
+  EB->appendRecipe(CanonicalIVIncrement, "index.next");
 
   // Add the BranchOnCount VPInstruction to the latch.
   VPInstruction *BranchBack =
@@ -8781,8 +8781,9 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
                 CM.foldTailByMasking() || isa<TruncInst>(Instr)) &&
                "unexpected recipe needs moving");
         Recipe->insertBefore(*HeaderVPBB, HeaderVPBB->getFirstNonPhi());
+        Recipe->getVPSingleValue()->setName(Instr->getName());
       } else
-        VPBB->appendRecipe(Recipe);
+        VPBB->appendRecipe(Recipe, Instr->getName());
     }
 
     VPBlockUtils::insertBlockAfter(new VPBasicBlock(), VPBB);
@@ -8842,6 +8843,8 @@ LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(VFRange &Range) {
           J++;
         }
         MemberR->eraseFromParent();
+        if (!Member->getType()->isVoidTy())
+          VPIG->getVPValue(J - 1)->setName(Member->getName());
       }
   }
 
@@ -9097,7 +9100,7 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
       // ensure that it comes after all of it's inputs, including CondOp.
       // Note that this transformation may leave over dead recipes (including
       // CurrentLink), which will be cleaned by a later VPlan transform.
-      LinkVPBB->appendRecipe(RedRecipe);
+      LinkVPBB->appendRecipe(RedRecipe, CurrentLinkI->getName());
       CurrentLink->replaceAllUsesWith(RedRecipe);
       PreviousLink = RedRecipe;
     }
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index 2c0daa82afa59f..bb603653f37ddc 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -588,6 +588,15 @@ bool VPBasicBlock::isExiting() const {
   return getParent()->getExitingBasicBlock() == this;
 }
 
+void VPBasicBlock::appendRecipe(VPRecipeBase *Recipe, const Twine &Name) {
+  insert(Recipe, end());
+  if (Recipe->getNumDefinedValues() == 1)
+    Recipe->getVPSingleValue()->setName(Name);
+  else
+    assert(Name.str().empty() && "Provided a name for a recipe that does not "
+                                 "define exactly a single VPValue");
+}
+
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void VPBlockBase::printSuccessors(raw_ostream &O, const Twine &Indent) const {
   if (getSuccessors().empty()) {
@@ -1053,8 +1062,10 @@ static void remapOperands(VPBlockBase *Entry, VPBlockBase *NewEntry,
       assert(OldR.getNumDefinedValues() == NewR.getNumDefinedValues() &&
              "recipes must define the same number of operands");
       for (const auto &[OldV, NewV] :
-           zip(OldR.definedValues(), NewR.definedValues()))
+           zip(OldR.definedValues(), NewR.definedValues())) {
         Old2NewVPValues[OldV] = NewV;
+        NewV->setName(OldV->getName());
+      }
     }
   }
 
@@ -1112,6 +1123,26 @@ VPlan *VPlan::duplicate() {
   return NewPlan;
 }
 
+void VPlan::setName(const VPValue *V, const Twine &Name) {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  std::string N = Name.str();
+  if (N.empty())
+    return;
+  assert(!VPValue2Name.contains(V));
+  std::string CurrName = N;
+
+  if (UsedNames.contains(N))
+    CurrName = N + ".1";
+  unsigned Cnt = 2;
+  while (UsedNames.contains(CurrName)) {
+    CurrName = N + "." + std::to_string(Cnt);
+    Cnt += 1;
+  }
+  VPValue2Name[V] = CurrName;
+  UsedNames.insert(CurrName);
+#endif
+}
+
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 
 Twine VPlanPrinter::getUID(const VPBlockBase *Block) {
@@ -1298,18 +1329,12 @@ void VPValue::replaceUsesWithIf(
 
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void VPValue::printAsOperand(raw_ostream &OS, VPSlotTracker &Tracker) const {
-  if (const Value *UV = getUnderlyingValue()) {
+  if (!getDefiningRecipe() && getUnderlyingValue()) {
     OS << "ir<";
-    UV->printAsOperand(OS, false);
+    getUnderlyingValue()->printAsOperand(OS, false);
     OS << ">";
-    return;
-  }
-
-  unsigned Slot = Tracker.getSlot(this);
-  if (Slot == unsigned(-1))
-    OS << "<badref>";
-  else
-    OS << "vp<%" << Tracker.getSlot(this) << ">";
+  } else
+    OS << Tracker.getName(this);
 }
 
 void VPUser::printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const {
@@ -1319,6 +1344,31 @@ void VPUser::printOperands(raw_ostream &O, VPSlotTracker &SlotTracker) const {
 }
 #endif
 
+std::string VPValue::getName() {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  if (auto *Def = getDefiningRecipe())
+    return Def->getParent()->getPlan()->getName(this);
+  assert(isLiveIn() && "called getName() on unsupported VPValue");
+  return getLiveInIRValue()->getName().str();
+#else
+  return "";
+#endif
+}
+
+void VPValue::setName(const Twine &Name) {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  auto *Def = getDefiningRecipe();
+  Def->getParent()->getPlan()->setName(this, Name);
+#endif
+}
+
+void VPValue::takeName(const VPValue *Src) {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  auto *Def = getDefiningRecipe();
+  Def->getParent()->getPlan()->takeName(Src, this);
+#endif
+}
+
 void VPInterleavedAccessInfo::visitRegion(VPRegionBlock *Region,
                                           Old2NewTy &Old2New,
                                           InterleavedAccessInfo &IAI) {
@@ -1397,6 +1447,16 @@ void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) {
       assignSlot(Def);
 }
 
+std::string VPSlotTracker::getName(const VPValue *V) const {
+  if (!Plan)
+    return "<badref>";
+  std::string N = Plan->getName(V);
+  if (!N.empty())
+    return (Twine("vp<%") + N + ">").str();
+
+  return (Twine("vp<%") + std::to_string(getSlot(V)) + ">").str();
+}
+
 bool vputils::onlyFirstLaneUsed(const VPValue *Def) {
   return all_of(Def->users(),
                 [Def](const VPUser *U) { return U->onlyFirstLaneUsed(Def); });
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.h b/llvm/lib/Transforms/Vectorize/VPlan.h
index 162a3c4b195e53..08c0041f23ae07 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.h
+++ b/llvm/lib/Transforms/Vectorize/VPlan.h
@@ -32,6 +32,7 @@
 #include "llvm/ADT/SmallBitVector.h"
 #include "llvm/ADT/SmallPtrSet.h"
 #include "llvm/ADT/SmallVector.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/ADT/ilist.h"
 #include "llvm/ADT/ilist_node.h"
@@ -1162,9 +1163,6 @@ class VPInstruction : public VPRecipeWithIRFlags {
   typedef unsigned char OpcodeTy;
   OpcodeTy Opcode;
 
-  /// An optional name that can be used for the generated IR instruction.
-  const std::string Name;
-
   /// Utility method serving execute(): generates a single instance of the
   /// modeled instruction. \returns the generated value for \p Part.
   /// In some cases an existing value is returned rather than a generated
@@ -1181,31 +1179,30 @@ class VPInstruction : public VPRecipeWithIRFlags {
   void setUnderlyingInstr(Instruction *I) { setUnderlyingValue(I); }
 
 public:
-  VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands, DebugLoc DL,
-                const Twine &Name = "")
+  VPInstruction(unsigned Opcode, ArrayRef<VPValue *> Operands, DebugLoc DL)
       : VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, DL),
-        Opcode(Opcode), Name(Name.str()) {}
+        Opcode(Opcode) {}
 
   VPInstruction(unsigned Opcode, std::initializer_list<VPValue *> Operands,
-                DebugLoc DL = {}, const Twine &Name = "")
-      : VPInstruction(Opcode, ArrayRef<VPValue *>(Operands), DL, Name) {}
+                DebugLoc DL = {})
+      : VPInstruction(Opcode, ArrayRef<VPValue *>(Operands), DL) {}
 
   VPInstruction(unsigned Opcode, CmpInst::Predicate Pred, VPValue *A,
-                VPValue *B, DebugLoc DL = {}, const Twine &Name = "");
+                VPValue *B, DebugLoc DL = {});
 
   VPInstruction(unsigned Opcode, std::initializer_list<VPValue *> Operands,
-                WrapFlagsTy WrapFlags, DebugLoc DL = {}, const Twine &Name = "")
+                WrapFlagsTy WrapFlags, DebugLoc DL = {})
       : VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, WrapFlags, DL),
-        Opcode(Opcode), Name(Name.str()) {}
+        Opcode(Opcode) {}
 
   VPInstruction(unsigned Opcode, std::initializer_list<VPValue *> Operands,
-                FastMathFlags FMFs, DebugLoc DL = {}, const Twine &Name = "");
+                FastMathFlags FMFs, DebugLoc DL = {});
 
   VP_CLASSOF_IMPL(VPDef::VPInstructionSC)
 
   VPRecipeBase *clone() override {
     SmallVector<VPValue *, 2> Operands(operands());
-    auto *New = new VPInstruction(Opcode, Operands, getDebugLoc(), Name);
+    auto *New = new VPInstruction(Opcode, Operands, getDebugLoc());
     New->transferFlags(*this);
     return New;
   }
@@ -2653,7 +2650,7 @@ class VPBasicBlock : public VPBlockBase {
 
   /// Augment the existing recipes of a VPBasicBlock with an additional
   /// \p Recipe as the last recipe.
-  void appendRecipe(VPRecipeBase *Recipe) { insert(Recipe, end()); }
+  void appendRecipe(VPRecipeBase *Recipe, const Twine &Name = "");
 
   /// The method which generates the output IR instructions that correspond to
   /// this VPBasicBlock, thereby "executing" the VPlan.
@@ -2876,6 +2873,11 @@ class VPlan {
   /// been modeled in VPlan directly.
   DenseMap<const SCEV *, VPValue *> SCEVToExpansion;
 
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+  DenseMap<const VPValue *, std::string> VPValue2Name;
+  StringSet<> UsedNames;
+#endif
+
 public:
   /// Construct a VPlan with original preheader \p Preheader, trip count \p TC
   /// and \p Entry to the plan. At the moment, \p Preheader and \p Entry need to
@@ -3016,6 +3018,42 @@ class VPlan {
   LLVM_DUMP_METHOD void dump() const;
 #endif
 
+  /// Set the name for \p V to \p Name if it is not empty. \p V must not have a
+  /// name assigned already.
+  void setName(const VPValue *V, const Twine &Name);
+
+  /// Remove the assigned name for \p V, if there is one.
+  void removeName(const VPValue *V) {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+    if (VPValue2Name.contains(V)) {
+      UsedNames.erase(VPValue2Name[V]);
+      VPValue2Name.erase(V);
+    }
+#endif
+  }
+
+  /// Take the name for \p Src and move it to \p Dst. The name of \p Src will be
+  /// empty afterwards.
+  void takeName(const VPValue *Src, const VPValue *Dst) {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+    std::string N = VPValue2Name.lookup(Src);
+    if (N.empty())
+      return;
+    VPValue2Name[Dst] = N;
+    VPValue2Name.erase(Src);
+#endif
+  }
+
+  /// Return the name assigned to \p V or an empty string if no name has been
+  /// assigned.
+  std::string getName(const VPValue *V) const {
+#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
+    return VPValue2Name.lookup(V);
+#else
+    return "";
+#endif
+  }
+
   /// Returns a range mapping the values the range \p Operands to their
   /// corresponding VPValues.
   iterator_range<mapped_iterator<Use *, std::function<VPValue *(Value *)>>>
diff --git a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp
index 94456bf858d9c5..f5b34a740a21b0 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanHCFGBuilder.cpp
@@ -297,7 +297,8 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
       if (Br->isConditional()) {
         VPValue *Cond = getOrCreateVPOperand(Br->getCondition());
         VPBB->appendRecipe(
-            new VPInstruction(VPInstruction::BranchOnCond, {Cond}));
+            new VPInstruction(VPInstruction::BranchOnCond, {Cond}),
+            Inst->getName());
       }
 
       // Skip the rest of the Instruction processing for Branch instructions.
@@ -310,7 +311,7 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
       // an empty VPInstruction that we will fix once the whole plain CFG has
       // been built.
       NewVPV = new VPWidenPHIRecipe(Phi);
-      VPBB->appendRecipe(cast<VPWidenPHIRecipe>(NewVPV));
+      VPBB->appendRecipe(cast<VPWidenPHIRecipe>(NewVPV), Inst->getName());
       PhisToFix.push_back(Phi);
     } else {
       // Translate LLVM-IR operands into VPValue operands and set them in the
@@ -321,8 +322,8 @@ void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
 
       // Build VPInstruction for any arbitrary Instruction without specific
       // representation in VPlan.
-      NewVPV = cast<VPInstruction>(
-          VPIRBuilder.createNaryOp(Inst->getOpcode(), VPOperands, Inst));
+      NewVPV = cast<VPInstruction>(VPIRBuilder.createNaryOp(
+          Inst->getOpcode(), VPOperands, Inst, Inst->getName()));
     }
 
     IRDef2VPValue[Inst] = NewVPV;
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 9ee0cb2bd61530..c24e0d72c222f7 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -226,6 +226,8 @@ void VPRecipeBase::removeFromParent() {
 
 iplist<VPRecipeBase>::iterator VPRecipeBase::eraseFromParent() {
   assert(getParent() && "Recipe not in any VPBasicBlock");
+  for (auto *V : definedValues())
+    getParent()->getPlan()->removeName(V);
   return getParent()->getRecipeList().erase(getIterator());
 }
 
@@ -255,20 +257,19 @@ FastMathFlags VPRecipeWithIRFlags::getFastMathFlags() const {
 }
 
 VPInstruction::VPInstruction(unsigned Opcode, CmpInst::Predicate Pred,
-                             VPValue *A, VPValue *B, DebugLoc DL,
-                             const Twine &Name)
+                             VPValue *A, VPValue *B, DebugLoc DL)
     : VPRecipeWithIRFlags(VPDef::VPInstructionSC, ArrayRef<VPValue *>({A, B}),
                           Pred, DL),
-      Opcode(Opcode), Name(Name.str()) {
+      Opcode(Opcode) {
   assert(Opcode == Instruction::ICmp &&
          "only ICmp predicates supported at the moment");
 }
 
 VPInstruction::VPInstruction(unsigned Opcode,
                              std::initializer_list<VPValue *> Operands,
-                             FastMathFlags FMFs, DebugLoc DL, const Twine &Name)
+                             FastMathFlags FMFs, DebugLoc DL)
     : VPRecipeWithIRFlags(VPDef::VPInstructionSC, Operands, FMFs, DL),
-      Opcode(Opcode), Name(Name.str()) {
+      Opcode(Opcode) {
   // Make sure the VPInstruction is a floating-point operation.
   assert(isFPMathOp() && "this op can't take fast-math flags");
 }
@@ -277,6 +278,7 @@ Value *VPInstruction::generateInstruction(VPTransformState &State,
                                           unsigned Part) {
   IRBuilderBase &Builder = State.Builder;
   Builder.SetCurrentDebugLocation(getDebugLoc());
+  std::string Name = getParent()->getPlan()->getName(this);
 
   if (Instruction::isBinaryOp(getOpcode())) {
     if (Part != 0 && vputils::onlyFirstPartUsed(this))
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
index 71f5285f90236b..bb1db219704439 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp
@@ -88,9 +88,10 @@ void VPlanTransforms::VPInstructionsToVPRecipes(
       }
 
       NewRecipe->insertBefore(&Ingredient);
-      if (NewRecipe->getNumDefinedValues() == 1)
+      if (NewRecipe->getNumDefinedValues() == 1) {
         VPV->replaceAllUsesWith(NewRecipe->getVPSingleValue());
-      else
+        NewRecipe->getVPSingleValue()->takeName(VPV);
+      } else
         assert(NewRecipe->getNumDefinedValues() == 0 &&
                "Only recpies with zero or one defined values expected");
       Ingredient.eraseFromParent();
@@ -163,6 +164,8 @@ static bool sinkScalarOperands(VPlan &Plan) {
       // TODO: add ".cloned" suffix to name of Clone's VPValue.
 
       Clone->insertBefore(SinkCandidate);
+      if (!I->getName().empty())
+        Clone->setName(I->getName() + ".cloned");
       SinkCandidate->replaceUsesWithIf(Clone, [SinkTo](VPUser &U, unsigned) {
         return cast<VPRecipeBase>(&U)->getParent() != SinkTo;
       });
@@ -318,6 +321,8 @@ static VPRegionBlock *createReplicateRegion(VPReplicateRecipe *PredRecipe,
     PHIRecipe->setOperand(0, RecipeWithoutMask);
   }
   PredRecipe->eraseFromParent();
+  Plan.setName(RecipeWithoutMask,
+               RecipeWithoutMask->getUnderlyingValue()->getName());
   auto *Exiting = new VPBasicBlock(Twine(RegionName) + ".continue", PHIRecipe);
   VPRegionBlock *Region = new VPRegionBlock(Entry, Exiting, RegionName, true);
 
@@ -1177,10 +1182,11 @@ void VPl...
[truncated]

``````````

</details>


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


More information about the llvm-commits mailing list