[llvm] r261304 - Add profile summary support for sample profile.

Robert Lougher via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 24 12:18:08 PST 2016


Hi Diego,

Thanks for the information (and replying so quickly).  I was able to use
autofdo v0.13 to output the profile in text format and use it with LLVM.  I
did have to make a minor tweak to output a space before each sample line,
as the change to support hierarchical sample profiles (r248865) requires
the lines to be indented.

Thanks,
Rob.

On 23 February 2016 at 18:49, Diego Novillo <dnovillo at google.com> wrote:

> Yes, I'm working on it.
>
> It's going to take me a little longer because I want to avoid the dual
> implementation problem we have at the moment.  Currently, the Perf
> converter generates the binary LLVM format, but an older version of it (no
> summaries).  I am adding two things that will simplify future changes:
>
> 1- A reader for the text format generated by autofdo's sample_merger.
> This will give LLVM the capability of reading the text format generated by
> the autofdo tools.
>
> 2- On the autofdo tool side, I will make it optionally depend on LLVM
> itself.  This way the converter will be able to use LLVM's own ProfileData
> library, if it's present at configure time.
>
> One option you may try in the meantime is to use v0.13 of autofdo tool (
> https://github.com/google/autofdo/releases/tag/v0.13).  This used to emit
> the profile in LLVM's text format.  I believe that should still work.
>
>
> Diego.
>
>
> On Tue, Feb 23, 2016 at 1:42 PM, Robert Lougher via llvm-commits <
> llvm-commits at lists.llvm.org> wrote:
>
>> Hi,
>>
>> I've started playing around with sample-based profiling in the last day
>> or two.  According to the documentation, you can use the Linux perf
>> profiler and convert the output to LLVM sample profile format using the
>> AutoFDO "create_llvm_prof" converter.  However, this no longer works as the
>> converter hasn't been updated to reflect these changes.  Are there any
>> plans to fix AutoFDO?
>>
>> Thanks,
>> Rob.
>>
>>
>> On 19 February 2016 at 03:15, Easwaran Raman via llvm-commits <
>> llvm-commits at lists.llvm.org> wrote:
>>
>>> Author: eraman
>>> Date: Thu Feb 18 21:15:33 2016
>>> New Revision: 261304
>>>
>>> URL: http://llvm.org/viewvc/llvm-project?rev=261304&view=rev
>>> Log:
>>> Add profile summary support for sample profile.
>>>
>>> Differential Revision: http://reviews.llvm.org/D17178
>>>
>>>
>>> Modified:
>>>     llvm/trunk/include/llvm/ProfileData/InstrProf.h
>>>     llvm/trunk/include/llvm/ProfileData/ProfileCommon.h
>>>     llvm/trunk/include/llvm/ProfileData/SampleProf.h
>>>     llvm/trunk/include/llvm/ProfileData/SampleProfReader.h
>>>     llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h
>>>     llvm/trunk/lib/ProfileData/InstrProfReader.cpp
>>>     llvm/trunk/lib/ProfileData/InstrProfWriter.cpp
>>>     llvm/trunk/lib/ProfileData/ProfileSummary.cpp
>>>     llvm/trunk/lib/ProfileData/SampleProfReader.cpp
>>>     llvm/trunk/lib/ProfileData/SampleProfWriter.cpp
>>>     llvm/trunk/test/Transforms/SampleProfile/Inputs/fnptr.binprof
>>>     llvm/trunk/unittests/ProfileData/SampleProfTest.cpp
>>>
>>> Modified: llvm/trunk/include/llvm/ProfileData/InstrProf.h
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/InstrProf.h?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/include/llvm/ProfileData/InstrProf.h (original)
>>> +++ llvm/trunk/include/llvm/ProfileData/InstrProf.h Thu Feb 18 21:15:33
>>> 2016
>>> @@ -681,14 +681,6 @@ struct Header {
>>>    uint64_t HashOffset;
>>>  };
>>>
>>> -static const uint32_t SummaryCutoffs[] = {
>>> -    10000,  /*  1% */
>>> -    100000, /* 10% */
>>> -    200000, 300000, 400000, 500000, 600000, 500000, 600000, 700000,
>>> -    800000, 900000, 950000, 990000, 999000, 999900, 999990, 999999};
>>> -static const uint32_t NumSummaryCutoffs =
>>> -    sizeof(SummaryCutoffs) / sizeof(*SummaryCutoffs);
>>> -
>>>  // Profile summary data recorded in the profile data file in indexed
>>>  // format. It is introduced in version 4. The summary data follows
>>>  // right after the profile file header.
>>>
>>> Modified: llvm/trunk/include/llvm/ProfileData/ProfileCommon.h
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/ProfileCommon.h?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/include/llvm/ProfileData/ProfileCommon.h (original)
>>> +++ llvm/trunk/include/llvm/ProfileData/ProfileCommon.h Thu Feb 18
>>> 21:15:33 2016
>>> @@ -24,6 +24,9 @@ namespace llvm {
>>>  namespace IndexedInstrProf {
>>>  struct Summary;
>>>  }
>>> +namespace sampleprof {
>>> +class FunctionSamples;
>>> +}
>>>  struct InstrProfRecord;
>>>  // The profile summary is one or more (Cutoff, MinCount, NumCounts)
>>> triplets.
>>>  // The semantics of counts depend on the type of profile. For
>>> instrumentation
>>> @@ -55,12 +58,18 @@ protected:
>>>        : DetailedSummaryCutoffs(Cutoffs), TotalCount(0), MaxCount(0),
>>>          NumCounts(0) {}
>>>    ProfileSummary() : TotalCount(0), MaxCount(0), NumCounts(0) {}
>>> +  ProfileSummary(std::vector<ProfileSummaryEntry> DetailedSummary,
>>> +                 uint64_t TotalCount, uint64_t MaxCount, uint32_t
>>> NumCounts)
>>> +      : DetailedSummary(DetailedSummary), TotalCount(TotalCount),
>>> +        MaxCount(MaxCount), NumCounts(NumCounts) {}
>>>    inline void addCount(uint64_t Count);
>>>
>>>  public:
>>>    static const int Scale = 1000000;
>>>    inline std::vector<ProfileSummaryEntry> &getDetailedSummary();
>>>    void computeDetailedSummary();
>>> +  /// \brief A vector of useful cutoff values for detailed summary.
>>> +  static const std::vector<uint32_t> DefaultCutoffs;
>>>  };
>>>
>>>  class InstrProfSummary : public ProfileSummary {
>>> @@ -83,6 +92,28 @@ public:
>>>    uint64_t getMaxInternalBlockCount() { return MaxInternalBlockCount; }
>>>  };
>>>
>>> +class SampleProfileSummary : public ProfileSummary {
>>> +  uint64_t MaxHeadSamples;
>>> +  uint32_t NumFunctions;
>>> +
>>> +public:
>>> +  uint32_t getNumLinesWithSamples() { return NumCounts; }
>>> +  uint64_t getTotalSamples() { return TotalCount; }
>>> +  uint32_t getNumFunctions() { return NumFunctions; }
>>> +  uint64_t getMaxHeadSamples() { return MaxHeadSamples; }
>>> +  uint64_t getMaxSamplesPerLine() { return MaxCount; }
>>> +  void addRecord(const sampleprof::FunctionSamples &FS);
>>> +  SampleProfileSummary(std::vector<uint32_t> Cutoffs)
>>> +      : ProfileSummary(Cutoffs), MaxHeadSamples(0), NumFunctions(0) {}
>>> +  SampleProfileSummary(uint64_t TotalSamples, uint64_t
>>> MaxSamplesPerLine,
>>> +                       uint64_t MaxHeadSamples, int32_t
>>> NumLinesWithSamples,
>>> +                       uint32_t NumFunctions,
>>> +                       std::vector<ProfileSummaryEntry> DetailedSummary)
>>> +      : ProfileSummary(DetailedSummary, TotalSamples, MaxSamplesPerLine,
>>> +                       NumLinesWithSamples),
>>> +        MaxHeadSamples(MaxHeadSamples), NumFunctions(NumFunctions) {}
>>> +};
>>> +
>>>  // This is called when a count is seen in the profile.
>>>  void ProfileSummary::addCount(uint64_t Count) {
>>>    TotalCount += Count;
>>>
>>> Modified: llvm/trunk/include/llvm/ProfileData/SampleProf.h
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProf.h?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/include/llvm/ProfileData/SampleProf.h (original)
>>> +++ llvm/trunk/include/llvm/ProfileData/SampleProf.h Thu Feb 18 21:15:33
>>> 2016
>>> @@ -74,7 +74,7 @@ static inline uint64_t SPMagic() {
>>>           uint64_t('2') << (64 - 56) | uint64_t(0xff);
>>>  }
>>>
>>> -static inline uint64_t SPVersion() { return 102; }
>>> +static inline uint64_t SPVersion() { return 103; }
>>>
>>>  /// Represents the relative location of an instruction.
>>>  ///
>>>
>>> Modified: llvm/trunk/include/llvm/ProfileData/SampleProfReader.h
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfReader.h?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/include/llvm/ProfileData/SampleProfReader.h (original)
>>> +++ llvm/trunk/include/llvm/ProfileData/SampleProfReader.h Thu Feb 18
>>> 21:15:33 2016
>>> @@ -129,6 +129,30 @@
>>>  // VERSION (uint32_t)
>>>  //    File format version number computed by SPVersion()
>>>  //
>>> +// SUMMARY
>>> +//    TOTAL_COUNT (uint64_t)
>>> +//        Total number of samples in the profile.
>>> +//    MAX_COUNT (uint64_t)
>>> +//        Maximum value of samples on a line.
>>> +//    MAX_HEAD_SAMPLES (uint64_t)
>>> +//        Maximum number of head samples.
>>> +//    NUM_COUNTS (uint64_t)
>>> +//        Number of lines with samples.
>>> +//    NUM_FUNCTIONS (uint64_t)
>>> +//        Number of functions with samples.
>>> +//    NUM_DETAILED_SUMMARY_ENTRIES (size_t)
>>> +//        Number of entries in detailed summary
>>> +//    DETAILED_SUMMARY
>>> +//        A list of detailed summary entry. Each entry consists of
>>> +//        CUTOFF (uint32_t)
>>> +//            Required percentile of total sample count expressed as a
>>> fraction
>>> +//            multiplied by 1000000.
>>> +//        MIN_COUNT (uint64_t)
>>> +//            The minimum number of samples required to reach the target
>>> +//            CUTOFF.
>>> +//        NUM_COUNTS (uint64_t)
>>> +//            Number of samples to get to the desrired percentile.
>>> +//
>>>  // NAME TABLE
>>>  //    SIZE (uint32_t)
>>>  //        Number of entries in the name table.
>>> @@ -190,6 +214,7 @@
>>>  #include "llvm/IR/DiagnosticInfo.h"
>>>  #include "llvm/IR/Function.h"
>>>  #include "llvm/IR/LLVMContext.h"
>>> +#include "llvm/ProfileData/ProfileCommon.h"
>>>  #include "llvm/ProfileData/SampleProf.h"
>>>  #include "llvm/Support/Debug.h"
>>>  #include "llvm/Support/ErrorHandling.h"
>>> @@ -270,6 +295,9 @@ public:
>>>    static ErrorOr<std::unique_ptr<SampleProfileReader>>
>>>    create(std::unique_ptr<MemoryBuffer> &B, LLVMContext &C);
>>>
>>> +  /// \brief Return the profile summary.
>>> +  SampleProfileSummary &getSummary() { return *(Summary.get()); }
>>> +
>>>  protected:
>>>    /// \brief Map every function to its associated profile.
>>>    ///
>>> @@ -283,6 +311,12 @@ protected:
>>>
>>>    /// \brief Memory buffer holding the profile file.
>>>    std::unique_ptr<MemoryBuffer> Buffer;
>>> +
>>> +  /// \brief Profile summary information.
>>> +  std::unique_ptr<SampleProfileSummary> Summary;
>>> +
>>> +  /// \brief Compute summary for this profile.
>>> +  void computeSummary();
>>>  };
>>>
>>>  class SampleProfileReaderText : public SampleProfileReader {
>>> @@ -348,6 +382,12 @@ protected:
>>>
>>>    /// Function name table.
>>>    std::vector<StringRef> NameTable;
>>> +
>>> +private:
>>> +  std::error_code readSummaryEntry(std::vector<ProfileSummaryEntry>
>>> &Entries);
>>> +
>>> +  /// \brief Read profile summary.
>>> +  std::error_code readSummary();
>>>  };
>>>
>>>  typedef SmallVector<FunctionSamples *, 10> InlineCallStack;
>>>
>>> Modified: llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h (original)
>>> +++ llvm/trunk/include/llvm/ProfileData/SampleProfWriter.h Thu Feb 18
>>> 21:15:33 2016
>>> @@ -15,6 +15,7 @@
>>>
>>>  #include "llvm/ADT/MapVector.h"
>>>  #include "llvm/ADT/StringRef.h"
>>> +#include "llvm/ProfileData/ProfileCommon.h"
>>>  #include "llvm/ProfileData/SampleProf.h"
>>>  #include "llvm/Support/ErrorOr.h"
>>>  #include "llvm/Support/FileSystem.h"
>>> @@ -42,7 +43,6 @@ public:
>>>    std::error_code write(const StringMap<FunctionSamples> &ProfileMap) {
>>>      if (std::error_code EC = writeHeader(ProfileMap))
>>>        return EC;
>>> -
>>>      for (const auto &I : ProfileMap) {
>>>        StringRef FName = I.first();
>>>        const FunctionSamples &Profile = I.second;
>>> @@ -75,6 +75,12 @@ protected:
>>>
>>>    /// \brief Output stream where to emit the profile to.
>>>    std::unique_ptr<raw_ostream> OutputStream;
>>> +
>>> +  /// \brief Profile summary.
>>> +  std::unique_ptr<SampleProfileSummary> Summary;
>>> +
>>> +  /// \brief Compute summary for this profile.
>>> +  void computeSummary(const StringMap<FunctionSamples> &ProfileMap);
>>>  };
>>>
>>>  /// \brief Sample-based profile writer (text format).
>>> @@ -113,6 +119,7 @@ protected:
>>>
>>>    std::error_code
>>>    writeHeader(const StringMap<FunctionSamples> &ProfileMap) override;
>>> +  std::error_code writeSummary();
>>>    std::error_code writeNameIdx(StringRef FName);
>>>    std::error_code writeBody(StringRef FName, const FunctionSamples &S);
>>>
>>>
>>> Modified: llvm/trunk/lib/ProfileData/InstrProfReader.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/InstrProfReader.cpp?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/lib/ProfileData/InstrProfReader.cpp (original)
>>> +++ llvm/trunk/lib/ProfileData/InstrProfReader.cpp Thu Feb 18 21:15:33
>>> 2016
>>> @@ -595,9 +595,8 @@ IndexedInstrProfReader::readSummary(Inde
>>>    } else {
>>>      // For older version of profile data, we need to compute on the fly:
>>>      using namespace IndexedInstrProf;
>>> -    std::vector<uint32_t> Cutoffs(&SummaryCutoffs[0],
>>> -                                  &SummaryCutoffs[NumSummaryCutoffs]);
>>> -    this->Summary = llvm::make_unique<InstrProfSummary>(Cutoffs);
>>> +    this->Summary =
>>> +
>>> llvm::make_unique<InstrProfSummary>(ProfileSummary::DefaultCutoffs);
>>>      this->Summary->computeDetailedSummary();
>>>      return Cur;
>>>    }
>>>
>>> Modified: llvm/trunk/lib/ProfileData/InstrProfWriter.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/InstrProfWriter.cpp?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/lib/ProfileData/InstrProfWriter.cpp (original)
>>> +++ llvm/trunk/lib/ProfileData/InstrProfWriter.cpp Thu Feb 18 21:15:33
>>> 2016
>>> @@ -217,9 +217,7 @@ void InstrProfWriter::writeImpl(ProfOStr
>>>    OnDiskChainedHashTableGenerator<InstrProfRecordWriterTrait> Generator;
>>>
>>>    using namespace IndexedInstrProf;
>>> -  std::vector<uint32_t> Cutoffs(&SummaryCutoffs[0],
>>> -                                &SummaryCutoffs[NumSummaryCutoffs]);
>>> -  InstrProfSummary PS(Cutoffs);
>>> +  InstrProfSummary PS(ProfileSummary::DefaultCutoffs);
>>>    InfoObj->TheProfileSummary = &PS;
>>>
>>>    // Populate the hash table generator.
>>> @@ -249,7 +247,7 @@ void InstrProfWriter::writeImpl(ProfOStr
>>>    OS.write(0);
>>>
>>>    // Reserve space to write profile summary data.
>>> -  uint32_t NumEntries = Cutoffs.size();
>>> +  uint32_t NumEntries = ProfileSummary::DefaultCutoffs.size();
>>>    uint32_t SummarySize = Summary::getSize(Summary::NumKinds,
>>> NumEntries);
>>>    // Remember the summary offset.
>>>    uint64_t SummaryOffset = OS.tell();
>>>
>>> Modified: llvm/trunk/lib/ProfileData/ProfileSummary.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/ProfileSummary.cpp?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/lib/ProfileData/ProfileSummary.cpp (original)
>>> +++ llvm/trunk/lib/ProfileData/ProfileSummary.cpp Thu Feb 18 21:15:33
>>> 2016
>>> @@ -11,17 +11,36 @@
>>>  //
>>>
>>>  //===----------------------------------------------------------------------===//
>>>
>>> -#include "llvm/ProfileData/ProfileCommon.h"
>>>  #include "llvm/ProfileData/InstrProf.h"
>>> +#include "llvm/ProfileData/ProfileCommon.h"
>>> +#include "llvm/ProfileData/SampleProf.h"
>>>
>>>  using namespace llvm;
>>>
>>> +// A set of cutoff values. Each value, when divided by
>>> ProfileSummary::Scale
>>> +// (which is 1000000) is a desired percentile of total counts.
>>> +const std::vector<uint32_t> ProfileSummary::DefaultCutoffs(
>>> +    {10000,  /*  1% */
>>> +     100000, /* 10% */
>>> +     200000, 300000, 400000, 500000, 600000, 500000, 600000, 700000,
>>> 800000,
>>> +     900000, 950000, 990000, 999000, 999900, 999990, 999999});
>>> +
>>>  void InstrProfSummary::addRecord(const InstrProfRecord &R) {
>>>    addEntryCount(R.Counts[0]);
>>>    for (size_t I = 1, E = R.Counts.size(); I < E; ++I)
>>>      addInternalCount(R.Counts[I]);
>>>  }
>>>
>>> +// To compute the detailed summary, we consider each line containing
>>> samples as
>>> +// equivalent to a block with a count in the instrumented profile.
>>> +void SampleProfileSummary::addRecord(const sampleprof::FunctionSamples
>>> &FS) {
>>> +  NumFunctions++;
>>> +  if (FS.getHeadSamples() > MaxHeadSamples)
>>> +    MaxHeadSamples = FS.getHeadSamples();
>>> +  for (const auto &I : FS.getBodySamples())
>>> +    addCount(I.second.getSamples());
>>> +}
>>> +
>>>  // The argument to this method is a vector of cutoff percentages and
>>> the return
>>>  // value is a vector of (Cutoff, MinCount, NumCounts) triplets.
>>>  void ProfileSummary::computeDetailedSummary() {
>>>
>>> Modified: llvm/trunk/lib/ProfileData/SampleProfReader.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfReader.cpp?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/lib/ProfileData/SampleProfReader.cpp (original)
>>> +++ llvm/trunk/lib/ProfileData/SampleProfReader.cpp Thu Feb 18 21:15:33
>>> 2016
>>> @@ -22,6 +22,7 @@
>>>
>>>  #include "llvm/ProfileData/SampleProfReader.h"
>>>  #include "llvm/ADT/DenseMap.h"
>>> +#include "llvm/ADT/STLExtras.h"
>>>  #include "llvm/ADT/SmallVector.h"
>>>  #include "llvm/Support/Debug.h"
>>>  #include "llvm/Support/ErrorOr.h"
>>> @@ -220,6 +221,8 @@ std::error_code SampleProfileReaderText:
>>>        }
>>>      }
>>>    }
>>> +  if (Result == sampleprof_error::success)
>>> +    computeSummary();
>>>
>>>    return Result;
>>>  }
>>> @@ -400,6 +403,9 @@ std::error_code SampleProfileReaderBinar
>>>    else if (*Version != SPVersion())
>>>      return sampleprof_error::unsupported_version;
>>>
>>> +  if (std::error_code EC = readSummary())
>>> +    return EC;
>>> +
>>>    // Read the name table.
>>>    auto Size = readNumber<uint32_t>();
>>>    if (std::error_code EC = Size.getError())
>>> @@ -415,6 +421,62 @@ std::error_code SampleProfileReaderBinar
>>>    return sampleprof_error::success;
>>>  }
>>>
>>> +std::error_code SampleProfileReaderBinary::readSummaryEntry(
>>> +    std::vector<ProfileSummaryEntry> &Entries) {
>>> +  auto Cutoff = readNumber<uint64_t>();
>>> +  if (std::error_code EC = Cutoff.getError())
>>> +    return EC;
>>> +
>>> +  auto MinBlockCount = readNumber<uint64_t>();
>>> +  if (std::error_code EC = MinBlockCount.getError())
>>> +    return EC;
>>> +
>>> +  auto NumBlocks = readNumber<uint64_t>();
>>> +  if (std::error_code EC = NumBlocks.getError())
>>> +    return EC;
>>> +
>>> +  Entries.emplace_back(*Cutoff, *MinBlockCount, *NumBlocks);
>>> +  return sampleprof_error::success;
>>> +}
>>> +
>>> +std::error_code SampleProfileReaderBinary::readSummary() {
>>> +  auto TotalCount = readNumber<uint64_t>();
>>> +  if (std::error_code EC = TotalCount.getError())
>>> +    return EC;
>>> +
>>> +  auto MaxBlockCount = readNumber<uint64_t>();
>>> +  if (std::error_code EC = MaxBlockCount.getError())
>>> +    return EC;
>>> +
>>> +  auto MaxFunctionCount = readNumber<uint64_t>();
>>> +  if (std::error_code EC = MaxFunctionCount.getError())
>>> +    return EC;
>>> +
>>> +  auto NumBlocks = readNumber<uint64_t>();
>>> +  if (std::error_code EC = NumBlocks.getError())
>>> +    return EC;
>>> +
>>> +  auto NumFunctions = readNumber<uint64_t>();
>>> +  if (std::error_code EC = NumFunctions.getError())
>>> +    return EC;
>>> +
>>> +  auto NumSummaryEntries = readNumber<uint64_t>();
>>> +  if (std::error_code EC = NumSummaryEntries.getError())
>>> +    return EC;
>>> +
>>> +  std::vector<ProfileSummaryEntry> Entries;
>>> +  for (unsigned i = 0; i < *NumSummaryEntries; i++) {
>>> +    std::error_code EC = readSummaryEntry(Entries);
>>> +    if (EC != sampleprof_error::success)
>>> +      return EC;
>>> +  }
>>> +  Summary = llvm::make_unique<SampleProfileSummary>(
>>> +      *TotalCount, *MaxBlockCount, *MaxFunctionCount, *NumBlocks,
>>> *NumFunctions,
>>> +      Entries);
>>> +
>>> +  return sampleprof_error::success;
>>> +}
>>> +
>>>  bool SampleProfileReaderBinary::hasFormat(const MemoryBuffer &Buffer) {
>>>    const uint8_t *Data =
>>>        reinterpret_cast<const uint8_t *>(Buffer.getBufferStart());
>>> @@ -518,6 +580,7 @@ std::error_code SampleProfileReaderGCC::
>>>      if (std::error_code EC = readOneFunctionProfile(Stack, true, 0))
>>>        return EC;
>>>
>>> +  computeSummary();
>>>    return sampleprof_error::success;
>>>  }
>>>
>>> @@ -725,3 +788,14 @@ SampleProfileReader::create(std::unique_
>>>
>>>    return std::move(Reader);
>>>  }
>>> +
>>> +// For text and GCC file formats, we compute the summary after reading
>>> the
>>> +// profile. Binary format has the profile summary in its header.
>>> +void SampleProfileReader::computeSummary() {
>>> +  Summary.reset(new
>>> SampleProfileSummary(ProfileSummary::DefaultCutoffs));
>>> +  for (const auto &I : Profiles) {
>>> +    const FunctionSamples &Profile = I.second;
>>> +    Summary->addRecord(Profile);
>>> +  }
>>> +  Summary->computeDetailedSummary();
>>> +}
>>>
>>> Modified: llvm/trunk/lib/ProfileData/SampleProfWriter.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/ProfileData/SampleProfWriter.cpp?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/lib/ProfileData/SampleProfWriter.cpp (original)
>>> +++ llvm/trunk/lib/ProfileData/SampleProfWriter.cpp Thu Feb 18 21:15:33
>>> 2016
>>> @@ -120,6 +120,10 @@ std::error_code SampleProfileWriterBinar
>>>    encodeULEB128(SPMagic(), OS);
>>>    encodeULEB128(SPVersion(), OS);
>>>
>>> +  computeSummary(ProfileMap);
>>> +  if (auto EC = writeSummary())
>>> +    return EC;
>>> +
>>>    // Generate the name table for all the functions referenced in the
>>> profile.
>>>    for (const auto &I : ProfileMap) {
>>>      addName(I.first());
>>> @@ -132,10 +136,25 @@ std::error_code SampleProfileWriterBinar
>>>      OS << N.first;
>>>      encodeULEB128(0, OS);
>>>    }
>>> -
>>>    return sampleprof_error::success;
>>>  }
>>>
>>> +std::error_code SampleProfileWriterBinary::writeSummary() {
>>> +  auto &OS = *OutputStream;
>>> +  encodeULEB128(Summary->getTotalSamples(), OS);
>>> +  encodeULEB128(Summary->getMaxSamplesPerLine(), OS);
>>> +  encodeULEB128(Summary->getMaxHeadSamples(), OS);
>>> +  encodeULEB128(Summary->getNumLinesWithSamples(), OS);
>>> +  encodeULEB128(Summary->getNumFunctions(), OS);
>>> +  std::vector<ProfileSummaryEntry> &Entries =
>>> Summary->getDetailedSummary();
>>> +  encodeULEB128(Entries.size(), OS);
>>> +  for (auto Entry : Entries) {
>>> +    encodeULEB128(Entry.Cutoff, OS);
>>> +    encodeULEB128(Entry.MinCount, OS);
>>> +    encodeULEB128(Entry.NumCounts, OS);
>>> +  }
>>> +  return sampleprof_error::success;
>>> +}
>>>  std::error_code SampleProfileWriterBinary::writeBody(StringRef FName,
>>>                                                       const
>>> FunctionSamples &S) {
>>>    auto &OS = *OutputStream;
>>> @@ -238,3 +257,13 @@ SampleProfileWriter::create(std::unique_
>>>
>>>    return std::move(Writer);
>>>  }
>>> +
>>> +void SampleProfileWriter::computeSummary(
>>> +    const StringMap<FunctionSamples> &ProfileMap) {
>>> +  Summary.reset(new
>>> SampleProfileSummary(ProfileSummary::DefaultCutoffs));
>>> +  for (const auto &I : ProfileMap) {
>>> +    const FunctionSamples &Profile = I.second;
>>> +    Summary->addRecord(Profile);
>>> +  }
>>> +  Summary->computeDetailedSummary();
>>> +}
>>>
>>> Modified: llvm/trunk/test/Transforms/SampleProfile/Inputs/fnptr.binprof
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SampleProfile/Inputs/fnptr.binprof?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> Binary files
>>> llvm/trunk/test/Transforms/SampleProfile/Inputs/fnptr.binprof (original)
>>> and llvm/trunk/test/Transforms/SampleProfile/Inputs/fnptr.binprof Thu Feb
>>> 18 21:15:33 2016 differ
>>>
>>> Modified: llvm/trunk/unittests/ProfileData/SampleProfTest.cpp
>>> URL:
>>> http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ProfileData/SampleProfTest.cpp?rev=261304&r1=261303&r2=261304&view=diff
>>>
>>> ==============================================================================
>>> --- llvm/trunk/unittests/ProfileData/SampleProfTest.cpp (original)
>>> +++ llvm/trunk/unittests/ProfileData/SampleProfTest.cpp Thu Feb 18
>>> 21:15:33 2016
>>> @@ -55,6 +55,10 @@ struct SampleProfTest : ::testing::Test
>>>      FooSamples.addTotalSamples(7711);
>>>      FooSamples.addHeadSamples(610);
>>>      FooSamples.addBodySamples(1, 0, 610);
>>> +    FooSamples.addBodySamples(2, 0, 600);
>>> +    FooSamples.addBodySamples(4, 0, 60000);
>>> +    FooSamples.addBodySamples(8, 0, 60351);
>>> +    FooSamples.addBodySamples(10, 0, 605);
>>>
>>>      StringRef BarName("_Z3bari");
>>>      FunctionSamples BarSamples;
>>> @@ -88,6 +92,32 @@ struct SampleProfTest : ::testing::Test
>>>      FunctionSamples &ReadBarSamples = ReadProfiles[BarName];
>>>      ASSERT_EQ(20301u, ReadBarSamples.getTotalSamples());
>>>      ASSERT_EQ(1437u, ReadBarSamples.getHeadSamples());
>>> +
>>> +    SampleProfileSummary &Summary = Reader->getSummary();
>>> +    ASSERT_EQ(123603u, Summary.getTotalSamples());
>>> +    ASSERT_EQ(6u, Summary.getNumLinesWithSamples());
>>> +    ASSERT_EQ(2u, Summary.getNumFunctions());
>>> +    ASSERT_EQ(1437u, Summary.getMaxHeadSamples());
>>> +    ASSERT_EQ(60351u, Summary.getMaxSamplesPerLine());
>>> +
>>> +    std::vector<ProfileSummaryEntry> &Details =
>>> Summary.getDetailedSummary();
>>> +    uint32_t Cutoff = 800000;
>>> +    auto Predicate = [&Cutoff](const ProfileSummaryEntry &PE) {
>>> +      return PE.Cutoff == Cutoff;
>>> +    };
>>> +    auto EightyPerc = std::find_if(Details.begin(), Details.end(),
>>> Predicate);
>>> +    Cutoff = 900000;
>>> +    auto NinetyPerc = std::find_if(Details.begin(), Details.end(),
>>> Predicate);
>>> +    Cutoff = 950000;
>>> +    auto NinetyFivePerc =
>>> +        std::find_if(Details.begin(), Details.end(), Predicate);
>>> +    Cutoff = 990000;
>>> +    auto NinetyNinePerc =
>>> +        std::find_if(Details.begin(), Details.end(), Predicate);
>>> +    ASSERT_EQ(60000u, EightyPerc->MinCount);
>>> +    ASSERT_EQ(60000u, NinetyPerc->MinCount);
>>> +    ASSERT_EQ(60000u, NinetyFivePerc->MinCount);
>>> +    ASSERT_EQ(610u, NinetyNinePerc->MinCount);
>>>    }
>>>  };
>>>
>>>
>>>
>>> _______________________________________________
>>> llvm-commits mailing list
>>> llvm-commits at lists.llvm.org
>>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>>
>>
>>
>> _______________________________________________
>> llvm-commits mailing list
>> llvm-commits at lists.llvm.org
>> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160224/81d3487e/attachment.html>


More information about the llvm-commits mailing list