Skip to content

Commit

Permalink
[SamplePGO][NFC] Dump function profiles in order
Browse files Browse the repository at this point in the history
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.

Reviewed By: wenlei, wlei

Differential Revision: https://reviews.llvm.org/D108147
  • Loading branch information
htyu committed Aug 17, 2021
1 parent 9ed4a94 commit f27fee6
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
5 changes: 5 additions & 0 deletions llvm/include/llvm/ProfileData/SampleProf.h
Expand Up @@ -961,6 +961,11 @@ class FunctionSamples {

raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);

using NameFunctionSamples = std::pair<StringRef, const FunctionSamples *>;

void sortFuncProfiles(const StringMap<FunctionSamples> &ProfileMap,
std::vector<NameFunctionSamples> &SortedProfiles);

/// Sort a LocationT->SampleT map by LocationT.
///
/// It produces a sorted list of <LocationT, SampleT> records by ascending
Expand Down
17 changes: 17 additions & 0 deletions llvm/lib/ProfileData/SampleProf.cpp
Expand Up @@ -198,6 +198,23 @@ raw_ostream &llvm::sampleprof::operator<<(raw_ostream &OS,
return OS;
}

void sampleprof::sortFuncProfiles(
const StringMap<FunctionSamples> &ProfileMap,
std::vector<NameFunctionSamples> &SortedProfiles) {
for (const auto &I : ProfileMap) {
assert(I.getKey() == I.second.getNameWithContext() &&
"Inconsistent profile map");
SortedProfiles.push_back(
std::make_pair(I.second.getNameWithContext(), &I.second));
}
llvm::stable_sort(SortedProfiles, [](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();
});
}

unsigned FunctionSamples::getOffset(const DILocation *DIL) {
return (DIL->getLine() - DIL->getScope()->getSubprogram()->getLine()) &
0xffff;
Expand Down
6 changes: 4 additions & 2 deletions llvm/lib/ProfileData/SampleProfReader.cpp
Expand Up @@ -66,8 +66,10 @@ void SampleProfileReader::dumpFunctionProfile(StringRef FName,

/// 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);
std::vector<NameFunctionSamples> V;
sortFuncProfiles(Profiles, V);
for (const auto &I : V)
dumpFunctionProfile(I.first, OS);
}

/// Parse \p Input as function head.
Expand Down
15 changes: 1 addition & 14 deletions llvm/lib/ProfileData/SampleProfWriter.cpp
Expand Up @@ -43,21 +43,8 @@ using namespace sampleprof;

std::error_code SampleProfileWriter::writeFuncProfiles(
const StringMap<FunctionSamples> &ProfileMap) {
// Sort the ProfileMap by total samples.
typedef std::pair<StringRef, const FunctionSamples *> NameFunctionSamples;
std::vector<NameFunctionSamples> V;
for (const auto &I : ProfileMap) {
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();
});

sortFuncProfiles(ProfileMap, V);
for (const auto &I : V) {
if (std::error_code EC = writeSample(*I.second))
return EC;
Expand Down
Expand Up @@ -20,14 +20,14 @@ Samples collected in inlined callsites {
}
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 ========
Expand Down

0 comments on commit f27fee6

Please sign in to comment.