[llvm] [nfc][llvm-profdata]Refactor llvm-profdata showInstrProfile (PR #71328)

Mingming Liu via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 6 13:38:49 PST 2023


================
@@ -2420,14 +2429,97 @@ static void showValueSitesStats(raw_fd_ostream &OS, uint32_t VK,
   }
 }
 
-static int showInstrProfile(
-    const std::string &Filename, bool ShowCounts, uint32_t TopN,
-    bool ShowIndirectCallTargets, bool ShowMemOPSizes, bool ShowDetailedSummary,
-    std::vector<uint32_t> DetailedSummaryCutoffs, bool ShowAllFunctions,
-    bool ShowCS, uint64_t ValueCutoff, bool OnlyListBelow,
-    const std::string &ShowFunction, bool TextFormat, bool ShowBinaryIds,
-    bool ShowCovered, bool ShowProfileVersion, bool ShowTemporalProfTraces,
-    ShowFormat SFormat, raw_fd_ostream &OS) {
+static void
+showFuncPseudoCounters(const NamedInstrProfRecord &FuncRecord,
+                       const InstrProfRecord::CountPseudoKind PseudoKind,
+                       size_t &ShownFunctions, raw_fd_ostream &OS) {
+  if (!ShownFunctions)
+    OS << "Counters:\n";
+  ++ShownFunctions;
+  OS << "  " << FuncRecord.Name << ":\n"
+     << "    Hash: " << format("0x%016" PRIx64, FuncRecord.Hash) << "\n"
+     << "    Counters: " << FuncRecord.Counts.size();
+  if (PseudoKind == InstrProfRecord::PseudoHot)
+    OS << "    <PseudoHot>\n";
+  else if (PseudoKind == InstrProfRecord::PseudoWarm)
+    OS << "    <PseudoWarm>\n";
+  else
+    llvm_unreachable("Unknown PseudoKind");
+}
+
+static void showFuncInstrProfile(const NamedInstrProfRecord &Func,
+                                 const bool IsIRInstr,
+                                 const InstrProfilePerFuncOptions &Options,
+                                 InstrProfSymtab *Symtab,
+                                 std::vector<ValueSitesStats> &VPStats,
+                                 size_t &ShownFunctions, raw_fd_ostream &OS) {
+  if (!ShownFunctions)
+    OS << "Counters:\n";
+
+  ++ShownFunctions;
+
+  OS << "  " << Func.Name << ":\n"
+     << "    Hash: " << format("0x%016" PRIx64, Func.Hash) << "\n"
+     << "    Counters: " << Func.Counts.size() << "\n";
+  if (!IsIRInstr)
+    OS << "    Function count: " << Func.Counts[0] << "\n";
+
+  if (Options.ShowIndirectCallTargets)
+    OS << "    Indirect Call Site Count: "
+       << Func.getNumValueSites(IPVK_IndirectCallTarget) << "\n";
+
+  uint32_t NumMemOPCalls = Func.getNumValueSites(IPVK_MemOPSize);
+  if (Options.ShowMemOPSizes && NumMemOPCalls > 0)
+    OS << "    Number of Memory Intrinsics Calls: " << NumMemOPCalls << "\n";
+
+  if (Options.ShowCounts) {
+    OS << "    Block counts: [";
+    size_t Start = (IsIRInstr ? 0 : 1);
+    for (size_t I = Start, E = Func.Counts.size(); I < E; ++I) {
+      OS << (I == Start ? "" : ", ") << Func.Counts[I];
+    }
+    OS << "]\n";
+  }
+
+  if (Options.ShowIndirectCallTargets) {
+    OS << "    Indirect Target Results:\n";
+    traverseAndShowAllValueSites(Func, IPVK_IndirectCallTarget,
+                                 VPStats[IPVK_IndirectCallTarget], OS, Symtab);
+  }
+
+  if (Options.ShowMemOPSizes && NumMemOPCalls > 0) {
+    OS << "    Memory Intrinsic Size Results:\n";
+    traverseAndShowAllValueSites(Func, IPVK_MemOPSize, VPStats[IPVK_MemOPSize],
+                                 OS, nullptr);
+  }
+}
+
+static void showValueProfileStats(size_t ShownFunctions,
+                                  bool ShowIndirectCallTargets,
+                                  bool ShowMemOPSizes,
+                                  std::vector<ValueSitesStats> &VPStats,
+                                  raw_fd_ostream &OS) {
+  if (ShownFunctions && ShowIndirectCallTargets) {
+    OS << "Statistics for indirect call sites profile:\n";
+    showValueSitesStats(OS, IPVK_IndirectCallTarget,
+                        VPStats[IPVK_IndirectCallTarget]);
+  }
+
+  if (ShownFunctions && ShowMemOPSizes) {
+    OS << "Statistics for memory intrinsic calls sizes profile:\n";
+    showValueSitesStats(OS, IPVK_MemOPSize, VPStats[IPVK_MemOPSize]);
+  }
+}
+
+static int
+showInstrProfile(const std::string &Filename, uint32_t TopN,
----------------
minglotus-6 wrote:

> I wonder if factoring all the show... APIs into a class would make more sense - do some of the parameters make sense as shared state between these functions?

To share some context of this NFC, the immediate (and motivating) use case `InstrProfilePerFuncOptions` is to allow showing one new type of value profiles without increasing the length of function arguments. 

`InstrProfileShowOptions` (a profile-level show options) would make sense as a separate patch. How to factor them is a more open-ended question. With all (nearly scalar) options, some options could be used together (e.g.,  `TopN` is meaningful to show function profiles but not meaningful in temporal profile traces)  while other options are more independent of each other.


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


More information about the llvm-commits mailing list