[PATCH] D108147: [SamplePGO][NFC] Dump function profiles in order

Hongtao Yu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 16 10:13:53 PDT 2021


hoy created this revision.
Herald added subscribers: modimo, wenlei, hiraditya.
hoy requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Sample profiles are stored in a string map which is basically an unordered map. Printing out profiles by simply walking the string map doesn't enforce an order. I'm sorting the map in the decreasing order of total samples to enable a more stable dump, which is good for comparing two dumps.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D108147

Files:
  llvm/lib/ProfileData/SampleProfReader.cpp
  llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected


Index: llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected
===================================================================
--- llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected
+++ llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected
@@ -20,14 +20,14 @@
     }
     No inlined callsites in this function
 }
-Function: _Z3fooi: 15422, 1220, 1 sampled lines
+Function: _Z3bari: 40602, 2874, 1 sampled lines
 Samples collected in the function's body {
-  1: 1220
+  1: 2874
 }
 No inlined callsites in this function
-Function: _Z3bari: 40602, 2874, 1 sampled lines
+Function: _Z3fooi: 15422, 1220, 1 sampled lines
 Samples collected in the function's body {
-  1: 2874
+  1: 1220
 }
 No inlined callsites in this function
 ======== Dump profile symbol list ========
Index: llvm/lib/ProfileData/SampleProfReader.cpp
===================================================================
--- llvm/lib/ProfileData/SampleProfReader.cpp
+++ llvm/lib/ProfileData/SampleProfReader.cpp
@@ -66,8 +66,22 @@
 
 /// Dump all the function profiles found on stream \p OS.
 void SampleProfileReader::dump(raw_ostream &OS) {
-  for (const auto &I : Profiles)
-    dumpFunctionProfile(I.getKey(), OS);
+  // Sort the ProfileMap by total samples.
+  typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
+  std::vector<NameFunctionSamples> V;
+  for (const auto &I : Profiles) {
+    assert(I.getKey() == I.second.getNameWithContext() &&
+           "Inconsistent profile map");
+    V.push_back(std::make_pair(I.second.getNameWithContext(), &I.second));
+  }
+  llvm::stable_sort(
+      V, [](const NameFunctionSamples &A, const NameFunctionSamples &B) {
+        if (A.second->getTotalSamples() == B.second->getTotalSamples())
+          return A.first > B.first;
+        return A.second->getTotalSamples() > B.second->getTotalSamples();
+      });
+  for (const auto &I : V)
+    dumpFunctionProfile(I.first, OS);
 }
 
 /// Parse \p Input as function head.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D108147.366667.patch
Type: text/x-patch
Size: 2006 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210816/b7c206ce/attachment.bin>


More information about the llvm-commits mailing list