4646#include < algorithm>
4747#include < cmath>
4848#include < optional>
49- #include < queue>
5049
5150using namespace llvm ;
5251using ProfCorrelatorKind = InstrProfCorrelator::ProfCorrelatorKind;
@@ -2846,9 +2845,8 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
28462845 auto FS = vfs::getRealFileSystem ();
28472846 auto ReaderOrErr = InstrProfReader::create (Filename, *FS);
28482847 std::vector<uint32_t > Cutoffs = std::move (DetailedSummaryCutoffs);
2849- if (ShowDetailedSummary && Cutoffs. empty ()) {
2848+ if (Cutoffs. empty () && (ShowDetailedSummary || ShowHotFuncList))
28502849 Cutoffs = ProfileSummaryBuilder::DefaultCutoffs;
2851- }
28522850 InstrProfSummaryBuilder Builder (std::move (Cutoffs));
28532851 if (Error E = ReaderOrErr.takeError ())
28542852 exitWithError (std::move (E), Filename);
@@ -2860,15 +2858,7 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
28602858 int NumVPKind = IPVK_Last - IPVK_First + 1 ;
28612859 std::vector<ValueSitesStats> VPStats (NumVPKind);
28622860
2863- auto MinCmp = [](const std::pair<std::string, uint64_t > &v1,
2864- const std::pair<std::string, uint64_t > &v2) {
2865- return v1.second > v2.second ;
2866- };
2867-
2868- std::priority_queue<std::pair<std::string, uint64_t >,
2869- std::vector<std::pair<std::string, uint64_t >>,
2870- decltype (MinCmp)>
2871- HottestFuncs (MinCmp);
2861+ std::vector<std::pair<StringRef, uint64_t >> NameAndMaxCount;
28722862
28732863 if (!TextFormat && OnlyListBelow) {
28742864 OS << " The list of functions with the maximum counter less than "
@@ -2942,15 +2932,8 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
29422932 if (OnlyListBelow)
29432933 continue ;
29442934
2945- if (TopNFunctions) {
2946- if (HottestFuncs.size () == TopNFunctions) {
2947- if (HottestFuncs.top ().second < FuncMax) {
2948- HottestFuncs.pop ();
2949- HottestFuncs.emplace (std::make_pair (std::string (Func.Name ), FuncMax));
2950- }
2951- } else
2952- HottestFuncs.emplace (std::make_pair (std::string (Func.Name ), FuncMax));
2953- }
2935+ if (TopNFunctions || ShowHotFuncList)
2936+ NameAndMaxCount.emplace_back (Func.Name , FuncMax);
29542937
29552938 if (Show) {
29562939 if (!ShownFunctions)
@@ -3029,16 +3012,27 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
30293012 << " ): " << PS->getNumFunctions () - BelowCutoffFunctions << " \n " ;
30303013 }
30313014
3015+ // Sort by MaxCount in decreasing order
3016+ llvm::stable_sort (NameAndMaxCount, [](const auto &L, const auto &R) {
3017+ return L.second > R.second ;
3018+ });
30323019 if (TopNFunctions) {
3033- std::vector<std::pair<std::string, uint64_t >> SortedHottestFuncs;
3034- while (!HottestFuncs.empty ()) {
3035- SortedHottestFuncs.emplace_back (HottestFuncs.top ());
3036- HottestFuncs.pop ();
3037- }
30383020 OS << " Top " << TopNFunctions
30393021 << " functions with the largest internal block counts: \n " ;
3040- for (auto &hotfunc : llvm::reverse (SortedHottestFuncs))
3041- OS << " " << hotfunc.first << " , max count = " << hotfunc.second << " \n " ;
3022+ auto TopFuncs = ArrayRef (NameAndMaxCount).take_front (TopNFunctions);
3023+ for (auto [Name, MaxCount] : TopFuncs)
3024+ OS << " " << Name << " , max count = " << MaxCount << " \n " ;
3025+ }
3026+
3027+ if (ShowHotFuncList) {
3028+ auto HotCountThreshold =
3029+ ProfileSummaryBuilder::getHotCountThreshold (PS->getDetailedSummary ());
3030+ OS << " # Hot count threshold: " << HotCountThreshold << " \n " ;
3031+ for (auto [Name, MaxCount] : NameAndMaxCount) {
3032+ if (MaxCount < HotCountThreshold)
3033+ break ;
3034+ OS << Name << " \n " ;
3035+ }
30423036 }
30433037
30443038 if (ShownFunctions && ShowIndirectCallTargets) {
0 commit comments