[PATCH] D152733: [BOLT] Sort CallSiteInfo targets by symbol name in YAMLWriter

Amir Ayupov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 12 10:49:10 PDT 2023


Amir created this revision.
Amir added a reviewer: bolt.
Herald added subscribers: treapster, ayermolo, mgrang.
Herald added a reviewer: rafauler.
Herald added a reviewer: maksfb.
Herald added a project: All.
Amir requested review of this revision.
Herald added subscribers: llvm-commits, yota9.
Herald added a project: LLVM.

Align YAML and fdata profiles by sorting CallSiteInfo targets by symbol name,
aligning it to fdata. By default, YAML CallSiteInfo is sorted by function id,
which is the order of function in the binary.

Follow-up to D152731 <https://reviews.llvm.org/D152731>, aligning yaml vs fdata, and in turn all three between to
each other.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D152733

Files:
  bolt/lib/Profile/YAMLProfileWriter.cpp


Index: bolt/lib/Profile/YAMLProfileWriter.cpp
===================================================================
--- bolt/lib/Profile/YAMLProfileWriter.cpp
+++ bolt/lib/Profile/YAMLProfileWriter.cpp
@@ -57,6 +57,7 @@
       if (!BC.MIB->isCall(Instr) && !BC.MIB->isIndirectBranch(Instr))
         continue;
 
+      SmallVector<std::pair<StringRef, yaml::bolt::CallSiteInfo>> CSTargets;
       yaml::bolt::CallSiteInfo CSI;
       std::optional<uint32_t> Offset = BC.MIB->getOffset(Instr);
       if (!Offset || *Offset < BB->getInputOffset())
@@ -69,25 +70,31 @@
         if (!ICSP)
           continue;
         for (const IndirectCallProfile &CSP : ICSP.get()) {
+          StringRef TargetName = "";
           CSI.DestId = 0; // designated for unknown functions
           CSI.EntryDiscriminator = 0;
           if (CSP.Symbol) {
             const BinaryFunction *Callee = BC.getFunctionForSymbol(CSP.Symbol);
-            if (Callee)
+            if (Callee) {
               CSI.DestId = Callee->getFunctionNumber();
+              TargetName = Callee->getOneName();
+            }
           }
           CSI.Count = CSP.Count;
           CSI.Mispreds = CSP.Mispreds;
-          YamlBB.CallSites.push_back(CSI);
+          CSTargets.emplace_back(TargetName, CSI);
         }
       } else { // direct call or a tail call
         uint64_t EntryID = 0;
+        CSI.DestId = 0;
+        StringRef TargetName = "";
         const MCSymbol *CalleeSymbol = BC.MIB->getTargetSymbol(Instr);
         const BinaryFunction *const Callee =
             BC.getFunctionForSymbol(CalleeSymbol, &EntryID);
         if (Callee) {
           CSI.DestId = Callee->getFunctionNumber();
           CSI.EntryDiscriminator = EntryID;
+          TargetName = Callee->getOneName();
         }
 
         if (BC.MIB->getConditionalTailCall(Instr)) {
@@ -107,12 +114,18 @@
         }
 
         if (CSI.Count)
-          YamlBB.CallSites.emplace_back(CSI);
+          CSTargets.emplace_back(TargetName, CSI);
       }
+      // Sort targets in a similar way to getBranchData, see Location::operator<
+      llvm::sort(CSTargets, [](const auto &RHS, const auto &LHS) {
+        if (RHS.first != LHS.first)
+          return RHS.first < LHS.first;
+        return RHS.second.Offset < LHS.second.Offset;
+      });
+      for (auto &KV : CSTargets)
+        YamlBB.CallSites.push_back(KV.second);
     }
 
-    llvm::sort(YamlBB.CallSites);
-
     // Skip printing if there's no profile data for non-entry basic block.
     // Include landing pads with non-zero execution count.
     if (YamlBB.CallSites.empty() && !BB->isEntryPoint() &&


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D152733.530592.patch
Type: text/x-patch
Size: 2622 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230612/89a6626b/attachment.bin>


More information about the llvm-commits mailing list