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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 12 09:00:57 PDT 2024


================
@@ -1373,32 +1362,73 @@ VPInterleavedAccessInfo::VPInterleavedAccessInfo(VPlan &Plan,
   visitRegion(Plan.getVectorLoopRegion(), Old2New, IAI);
 }
 
-void VPSlotTracker::assignSlot(const VPValue *V) {
-  if (V->getUnderlyingValue())
+void VPSlotTracker::assignSlotOrName(const VPValue *V) {
+  if (auto *UV = V->getUnderlyingValue()) {
+    std::string Name;
+    raw_string_ostream S(Name);
+    UV->printAsOperand(S, false);
+    deduplicateName(V, Name);
     return;
+  }
   assert(!Slots.contains(V) && "VPValue already has a slot!");
   Slots[V] = NextSlot++;
 }
 
-void VPSlotTracker::assignSlots(const VPlan &Plan) {
+void VPSlotTracker::assignSlotsOrNames(const VPlan &Plan) {
   if (Plan.VFxUF.getNumUsers() > 0)
-    assignSlot(&Plan.VFxUF);
-  assignSlot(&Plan.VectorTripCount);
+    assignSlotOrName(&Plan.VFxUF);
+  assignSlotOrName(&Plan.VectorTripCount);
   if (Plan.BackedgeTakenCount)
-    assignSlot(Plan.BackedgeTakenCount);
-  assignSlots(Plan.getPreheader());
+    assignSlotOrName(Plan.BackedgeTakenCount);
+  for (VPValue *LI : Plan.VPLiveInsToFree)
+    assignSlotOrName(LI);
+  assignSlotsOrNames(Plan.getPreheader());
 
   ReversePostOrderTraversal<VPBlockDeepTraversalWrapper<const VPBlockBase *>>
       RPOT(VPBlockDeepTraversalWrapper<const VPBlockBase *>(Plan.getEntry()));
   for (const VPBasicBlock *VPBB :
        VPBlockUtils::blocksOnly<const VPBasicBlock>(RPOT))
-    assignSlots(VPBB);
+    assignSlotsOrNames(VPBB);
 }
 
-void VPSlotTracker::assignSlots(const VPBasicBlock *VPBB) {
+void VPSlotTracker::assignSlotsOrNames(const VPBasicBlock *VPBB) {
   for (const VPRecipeBase &Recipe : *VPBB)
     for (VPValue *Def : Recipe.definedValues())
-      assignSlot(Def);
+      assignSlotOrName(Def);
+}
+
+void VPSlotTracker::deduplicateName(const VPValue *V, StringRef Name) {
+  assert(!Name.empty() && "Name cannot be be empty.");
+  std::string NewName = Name.str();
+  const auto &[A, AssignedInserted] = AssignedNames.insert({V, NewName});
+  if (!AssignedInserted || V->isLiveIn())
+    return;
+
+  const auto &[C, UseInserted] = NameUseCount.insert({NewName, 0});
+  if (!UseInserted) {
+    C->second++;
+    NewName = NewName + "." + std::to_string(C->second);
+    A->second = NewName;
+  }
+}
+
+std::string VPSlotTracker::getName(const VPValue *V) const {
----------------
fhahn wrote:

Updated to construct all names on initial assignment. We still need to handle the case where no names were assigned for cases where the slot tracker was constructed without VPlan, e.g. when printing a recipe via `dump()` that hasn't been inserted in a plan in a debugger.

For now, the code still retains the logic to use the underlying value name here, even if no name is assigned. Could be handled by updating VPSlotTracker's constructor to take a VPValue and assign a name/slot to 
1. the VPValue only, if it doesn't have a defining recipe
2. the defining recipe only, if it hasn't been inserted in a VPlan
3. the whole VPlan otherwise.

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


More information about the llvm-commits mailing list