[llvm] r253568 - SamplePGO - Sort samples by source location when emitting as text.

Diego Novillo via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 19 07:33:08 PST 2015


Author: dnovillo
Date: Thu Nov 19 09:33:08 2015
New Revision: 253568

URL: http://llvm.org/viewvc/llvm-project?rev=253568&view=rev
Log:
SamplePGO - Sort samples by source location when emitting as text.

When dumping function samples or writing them out as text format, it
helps if the samples are emitted sorted by source location. The sorting
of the maps is a bit slow, so we only do it on demand.

Modified:
    llvm/trunk/include/llvm/ProfileData/SampleProf.h
    llvm/trunk/lib/ProfileData/SampleProf.cpp
    llvm/trunk/lib/ProfileData/SampleProfWriter.cpp
    llvm/trunk/test/tools/llvm-profdata/gcc-gcov-sample-profile.test
    llvm/trunk/test/tools/llvm-profdata/inline-samples.test

Modified: llvm/trunk/include/llvm/ProfileData/SampleProf.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProf.h?rev=253568&r1=253567&r2=253568&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ProfileData/SampleProf.h (original)
+++ llvm/trunk/include/llvm/ProfileData/SampleProf.h Thu Nov 19 09:33:08 2015
@@ -77,6 +77,10 @@ struct LineLocation {
   LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {}
   void print(raw_ostream &OS) const;
   void dump() const;
+  bool operator<(const LineLocation &O) const {
+    return LineOffset < O.LineOffset ||
+           (LineOffset == O.LineOffset && Discriminator < O.Discriminator);
+  }
 
   uint32_t LineOffset;
   uint32_t Discriminator;
@@ -335,6 +339,29 @@ private:
 
 raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);
 
+/// Sort a LocationT->SampleT map by LocationT.
+///
+/// It produces a sorted list of <LocationT, SampleT> records by ascending
+/// order of LocationT.
+template <class LocationT, class SampleT> class SampleSorter {
+public:
+  typedef detail::DenseMapPair<LocationT, SampleT> SamplesWithLoc;
+  typedef SmallVector<const SamplesWithLoc *, 20> SamplesWithLocList;
+
+  SampleSorter(const DenseMap<LocationT, SampleT> &Samples) {
+    for (const auto &I : Samples)
+      V.push_back(&I);
+    std::stable_sort(V.begin(), V.end(),
+                     [](const SamplesWithLoc *A, const SamplesWithLoc *B) {
+                       return A->first < B->first;
+                     });
+  }
+  const SamplesWithLocList &get() const { return V; }
+
+private:
+  SamplesWithLocList V;
+};
+
 } // end namespace sampleprof
 
 } // end namespace llvm

Modified: llvm/trunk/lib/ProfileData/SampleProf.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProf.cpp?rev=253568&r1=253567&r2=253568&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/SampleProf.cpp (original)
+++ llvm/trunk/lib/ProfileData/SampleProf.cpp Thu Nov 19 09:33:08 2015
@@ -108,15 +108,18 @@ void FunctionSamples::print(raw_ostream
   OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size()
      << " sampled lines\n";
 
-  for (const auto &SI : BodySamples) {
+  SampleSorter<LineLocation, SampleRecord> SortedBodySamples(BodySamples);
+  for (const auto &SI : SortedBodySamples.get()) {
     OS.indent(Indent);
-    OS << SI.first << ": " << SI.second;
+    OS << SI->first << ": " << SI->second;
   }
 
-  for (const auto &CS : CallsiteSamples) {
+  SampleSorter<CallsiteLocation, FunctionSamples> SortedCallsiteSamples(
+      CallsiteSamples);
+  for (const auto &CS : SortedCallsiteSamples.get()) {
     OS.indent(Indent);
-    OS << CS.first << ": ";
-    CS.second.print(OS, Indent + 2);
+    OS << CS->first << ": ";
+    CS->second.print(OS, Indent + 2);
   }
 }
 

Modified: llvm/trunk/lib/ProfileData/SampleProfWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfWriter.cpp?rev=253568&r1=253567&r2=253568&view=diff
==============================================================================
--- llvm/trunk/lib/ProfileData/SampleProfWriter.cpp (original)
+++ llvm/trunk/lib/ProfileData/SampleProfWriter.cpp Thu Nov 19 09:33:08 2015
@@ -32,7 +32,7 @@ using namespace llvm;
 /// \brief Write samples to a text file.
 ///
 /// Note: it may be tempting to implement this in terms of
-/// FunctionSamples::dump().  Please don't.  The dump functionality is intended
+/// FunctionSamples::print().  Please don't.  The dump functionality is intended
 /// for debugging and has no specified form.
 ///
 /// The format used here is more structured and deliberate because
@@ -44,9 +44,10 @@ std::error_code SampleProfileWriterText:
     OS << ":" << S.getHeadSamples();
   OS << "\n";
 
-  for (const auto &I : S.getBodySamples()) {
-    LineLocation Loc = I.first;
-    const SampleRecord &Sample = I.second;
+  SampleSorter<LineLocation, SampleRecord> SortedSamples(S.getBodySamples());
+  for (const auto &I : SortedSamples.get()) {
+    LineLocation Loc = I->first;
+    const SampleRecord &Sample = I->second;
     OS.indent(Indent + 1);
     if (Loc.Discriminator == 0)
       OS << Loc.LineOffset << ": ";
@@ -60,10 +61,12 @@ std::error_code SampleProfileWriterText:
     OS << "\n";
   }
 
+  SampleSorter<CallsiteLocation, FunctionSamples> SortedCallsiteSamples(
+      S.getCallsiteSamples());
   Indent += 1;
-  for (const auto &I : S.getCallsiteSamples()) {
-    CallsiteLocation Loc = I.first;
-    const FunctionSamples &CalleeSamples = I.second;
+  for (const auto &I : SortedCallsiteSamples.get()) {
+    CallsiteLocation Loc = I->first;
+    const FunctionSamples &CalleeSamples = I->second;
     OS.indent(Indent);
     if (Loc.Discriminator == 0)
       OS << Loc.LineOffset << ": ";

Modified: llvm/trunk/test/tools/llvm-profdata/gcc-gcov-sample-profile.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/gcc-gcov-sample-profile.test?rev=253568&r1=253567&r2=253568&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/gcc-gcov-sample-profile.test (original)
+++ llvm/trunk/test/tools/llvm-profdata/gcc-gcov-sample-profile.test Thu Nov 19 09:33:08 2015
@@ -11,8 +11,8 @@ RUN: llvm-profdata show --sample %p/Inpu
 SHOW1: Function: main: 364084, 0, 6 sampled lines
 SHOW1: 2.3: inlined callee: _Z3fool: 243786, 0, 3 sampled lines
 SHOW1:   1.3: inlined callee: _Z3bari: 0, 0, 2 sampled lines
-SHOW1:   1.8: inlined callee: _Z3bari: 0, 0, 2 sampled lines
 SHOW1:   1.7: inlined callee: _Z3bari: 98558, 0, 2 sampled lines
+SHOW1:   1.8: inlined callee: _Z3bari: 0, 0, 2 sampled lines
 
 2- Convert the profile to text encoding and check that they are both
    identical.

Modified: llvm/trunk/test/tools/llvm-profdata/inline-samples.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-profdata/inline-samples.test?rev=253568&r1=253567&r2=253568&view=diff
==============================================================================
--- llvm/trunk/test/tools/llvm-profdata/inline-samples.test (original)
+++ llvm/trunk/test/tools/llvm-profdata/inline-samples.test Thu Nov 19 09:33:08 2015
@@ -10,9 +10,9 @@ RUN: llvm-profdata show --sample %t.prof
 SHOW1: Function: main: 366846, 0, 6 sampled lines
 SHOW1: 2.3: inlined callee: _Z3fool: 246044, 0, 3 sampled lines
 SHOW1:   1.3: inlined callee: _Z3bari: 0, 0, 2 sampled lines
-SHOW1:   1.8: inlined callee: _Z3bari: 0, 0, 2 sampled lines
 SHOW1:   1.7: inlined callee: _Z3bari: 99492, 0, 2 sampled lines
 SHOW1:     1.2: 46732
+SHOW1:   1.8: inlined callee: _Z3bari: 0, 0, 2 sampled lines
 
 3- Convert the binary profile to text encoding and check that they are both
    identical.




More information about the llvm-commits mailing list