diff options
author | Diego Novillo <dnovillo@google.com> | 2014-11-01 00:56:55 +0000 |
---|---|---|
committer | Diego Novillo <dnovillo@google.com> | 2014-11-01 00:56:55 +0000 |
commit | d5336ae269d86af133cb465e1b0dba38121f11c2 (patch) | |
tree | 705c53dae847beba892284f1db28aa96b31d93e3 /llvm/lib/ProfileData | |
parent | a33cd6aa27ab02514224dd461504e10cc0e14fac (diff) | |
download | bcm5719-llvm-d5336ae269d86af133cb465e1b0dba38121f11c2.tar.gz bcm5719-llvm-d5336ae269d86af133cb465e1b0dba38121f11c2.zip |
Add show and merge tools for sample PGO profiles.
Summary:
This patch extends the 'show' and 'merge' commands in llvm-profdata to handle
sample PGO formats. Using the 'merge' command it is now possible to convert
one sample PGO format to another.
The only format that is currently not working is 'gcc'. I still need to
implement support for it in lib/ProfileData.
The changes in the sample profile support classes are needed for the
merge operation.
Reviewers: bogner
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D6065
llvm-svn: 221032
Diffstat (limited to 'llvm/lib/ProfileData')
-rw-r--r-- | llvm/lib/ProfileData/SampleProf.cpp | 2 | ||||
-rw-r--r-- | llvm/lib/ProfileData/SampleProfReader.cpp | 46 | ||||
-rw-r--r-- | llvm/lib/ProfileData/SampleProfWriter.cpp | 66 |
3 files changed, 57 insertions, 57 deletions
diff --git a/llvm/lib/ProfileData/SampleProf.cpp b/llvm/lib/ProfileData/SampleProf.cpp index 8bd2249f57e..920c48a2464 100644 --- a/llvm/lib/ProfileData/SampleProf.cpp +++ b/llvm/lib/ProfileData/SampleProf.cpp @@ -36,6 +36,8 @@ class SampleProfErrorCategoryType : public std::error_category { return "Truncated profile data"; case sampleprof_error::malformed: return "Malformed profile data"; + case sampleprof_error::unrecognized_format: + return "Unrecognized profile encoding format"; } llvm_unreachable("A value of sampleprof_error has no message."); } diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp index df4be83f5f8..a6e4e0ce502 100644 --- a/llvm/lib/ProfileData/SampleProfReader.cpp +++ b/llvm/lib/ProfileData/SampleProfReader.cpp @@ -95,7 +95,6 @@ //===----------------------------------------------------------------------===// #include "llvm/ProfileData/SampleProfReader.h" -#include "llvm/ProfileData/SampleProfWriter.h" // REMOVE #include "llvm/Support/Debug.h" #include "llvm/Support/ErrorOr.h" #include "llvm/Support/LEB128.h" @@ -112,50 +111,36 @@ using namespace llvm; void FunctionSamples::print(raw_ostream &OS) { OS << TotalSamples << ", " << TotalHeadSamples << ", " << BodySamples.size() << " sampled lines\n"; - for (BodySampleMap::const_iterator SI = BodySamples.begin(), - SE = BodySamples.end(); - SI != SE; ++SI) { - LineLocation Loc = SI->first; - SampleRecord Sample = SI->second; + for (const auto &SI : BodySamples) { + LineLocation Loc = SI.first; + const SampleRecord &Sample = SI.second; OS << "\tline offset: " << Loc.LineOffset << ", discriminator: " << Loc.Discriminator << ", number of samples: " << Sample.getSamples(); if (Sample.hasCalls()) { OS << ", calls:"; - for (SampleRecord::CallTargetList::const_iterator - I = Sample.getCallTargets().begin(), - E = Sample.getCallTargets().end(); - I != E; ++I) - OS << " " << (*I).first << ":" << (*I).second; + for (const auto &I : Sample.getCallTargets()) + OS << " " << I.first() << ":" << I.second; } OS << "\n"; } OS << "\n"; } -/// \brief Print the function profile for \p FName on stream \p OS. +/// \brief Dump the function profile for \p FName. /// -/// \param OS Stream to emit the output to. /// \param FName Name of the function to print. -void SampleProfileReader::printFunctionProfile(raw_ostream &OS, - StringRef FName) { +/// \param OS Stream to emit the output to. +void SampleProfileReader::dumpFunctionProfile(StringRef FName, + raw_ostream &OS) { OS << "Function: " << FName << ": "; Profiles[FName].print(OS); } -/// \brief Dump the function profile for \p FName. -/// -/// \param FName Name of the function to print. -void SampleProfileReader::dumpFunctionProfile(StringRef FName) { - printFunctionProfile(dbgs(), FName); -} - -/// \brief Dump all the function profiles found. -void SampleProfileReader::dump() { - for (StringMap<FunctionSamples>::const_iterator I = Profiles.begin(), - E = Profiles.end(); - I != E; ++I) - dumpFunctionProfile(I->getKey()); +/// \brief 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); } /// \brief Load samples from a text file. @@ -245,8 +230,7 @@ std::error_code SampleProfileReaderText::read() { return sampleprof_error::success; } -template <typename T> -ErrorOr<T> SampleProfileReaderBinary::readNumber() { +template <typename T> ErrorOr<T> SampleProfileReaderBinary::readNumber() { unsigned NumBytesRead = 0; std::error_code EC; uint64_t Val = decodeULEB128(Data, &NumBytesRead); @@ -396,7 +380,7 @@ setupMemoryBuffer(std::string Filename, std::unique_ptr<MemoryBuffer> &Buffer) { /// /// \returns an error code indicating the status of the created reader. std::error_code -SampleProfileReader::create(std::string Filename, +SampleProfileReader::create(StringRef Filename, std::unique_ptr<SampleProfileReader> &Reader, LLVMContext &C) { std::unique_ptr<MemoryBuffer> Buffer; diff --git a/llvm/lib/ProfileData/SampleProfWriter.cpp b/llvm/lib/ProfileData/SampleProfWriter.cpp index ab2e7617762..49d6fdbf4f7 100644 --- a/llvm/lib/ProfileData/SampleProfWriter.cpp +++ b/llvm/lib/ProfileData/SampleProfWriter.cpp @@ -30,19 +30,16 @@ using namespace llvm::sampleprof; using namespace llvm; /// \brief Write samples to a text file. -bool SampleProfileWriterText::write(const Function &F, - const FunctionSamples &S) { +bool SampleProfileWriterText::write(StringRef FName, const FunctionSamples &S) { if (S.empty()) return true; - OS << F.getName() << ":" << S.getTotalSamples() << ":" << S.getHeadSamples() + OS << FName << ":" << S.getTotalSamples() << ":" << S.getHeadSamples() << "\n"; - for (BodySampleMap::const_iterator I = S.getBodySamples().begin(), - E = S.getBodySamples().end(); - I != E; ++I) { - LineLocation Loc = I->first; - SampleRecord Sample = I->second; + for (const auto &I : S.getBodySamples()) { + LineLocation Loc = I.first; + const SampleRecord &Sample = I.second; if (Loc.Discriminator == 0) OS << Loc.LineOffset << ": "; else @@ -50,11 +47,8 @@ bool SampleProfileWriterText::write(const Function &F, OS << Sample.getSamples(); - for (SampleRecord::CallTargetList::const_iterator - I = Sample.getCallTargets().begin(), - E = Sample.getCallTargets().end(); - I != E; ++I) - OS << " " << (*I).first << ":" << (*I).second; + for (const auto &J : Sample.getCallTargets()) + OS << " " << J.first() << ":" << J.second; OS << "\n"; } @@ -75,31 +69,26 @@ SampleProfileWriterBinary::SampleProfileWriterBinary(StringRef F, /// \brief Write samples to a binary file. /// /// \returns true if the samples were written successfully, false otherwise. -bool SampleProfileWriterBinary::write(const Function &F, +bool SampleProfileWriterBinary::write(StringRef FName, const FunctionSamples &S) { if (S.empty()) return true; - OS << F.getName(); + OS << FName; encodeULEB128(0, OS); encodeULEB128(S.getTotalSamples(), OS); encodeULEB128(S.getHeadSamples(), OS); encodeULEB128(S.getBodySamples().size(), OS); - for (BodySampleMap::const_iterator I = S.getBodySamples().begin(), - E = S.getBodySamples().end(); - I != E; ++I) { - LineLocation Loc = I->first; - SampleRecord Sample = I->second; + for (const auto &I : S.getBodySamples()) { + LineLocation Loc = I.first; + const SampleRecord &Sample = I.second; encodeULEB128(Loc.LineOffset, OS); encodeULEB128(Loc.Discriminator, OS); encodeULEB128(Sample.getSamples(), OS); encodeULEB128(Sample.getCallTargets().size(), OS); - for (SampleRecord::CallTargetList::const_iterator - I = Sample.getCallTargets().begin(), - E = Sample.getCallTargets().end(); - I != E; ++I) { - std::string Callee = (*I).first; - unsigned CalleeSamples = (*I).second; + for (const auto &J : Sample.getCallTargets()) { + std::string Callee = J.first(); + unsigned CalleeSamples = J.second; OS << Callee; encodeULEB128(0, OS); encodeULEB128(CalleeSamples, OS); @@ -108,3 +97,28 @@ bool SampleProfileWriterBinary::write(const Function &F, return true; } + +/// \brief Create a sample profile writer based on the specified format. +/// +/// \param Filename The file to create. +/// +/// \param Writer The writer to instantiate according to the specified format. +/// +/// \param Format Encoding format for the profile file. +/// +/// \returns an error code indicating the status of the created writer. +std::error_code +SampleProfileWriter::create(StringRef Filename, + std::unique_ptr<SampleProfileWriter> &Writer, + SampleProfileFormat Format) { + std::error_code EC; + + if (Format == SPF_Binary) + Writer.reset(new SampleProfileWriterBinary(Filename, EC)); + else if (Format == SPF_Text) + Writer.reset(new SampleProfileWriterText(Filename, EC)); + else + EC = sampleprof_error::unrecognized_format; + + return EC; +} |