diff --git a/llvm/lib/ProfileData/SampleProfReader.cpp b/llvm/lib/ProfileData/SampleProfReader.cpp --- a/llvm/lib/ProfileData/SampleProfReader.cpp +++ b/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 NameFunctionSamples; + std::vector 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. diff --git a/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected b/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected --- a/llvm/test/tools/llvm-profdata/Inputs/profile-symbol-list.expected +++ b/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 ========