Skip to content

Commit e1a572b

Browse files
committed
[profdata] Use --hot-func-list to show all hot functions
1 parent 4dc6dfd commit e1a572b

File tree

3 files changed

+62
-31
lines changed

3 files changed

+62
-31
lines changed

llvm/test/tools/llvm-profdata/c-general.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ SWITCHES-LABEL: Functions shown: 1
2222
CHECK-LABEL: Total functions: 12
2323
CHECK-NEXT: Maximum function count: 1
2424
CHECK-NEXT: Maximum internal block count: 100
25-
TOPN: boolean_operators, max count = 100
26-
TOPN-NEXT: simple_loops, max count = 100
27-
TOPN-NEXT: conditionals, max count = 100
25+
TOPN: simple_loops, max count = 100
26+
TOPN-NEXT: conditionals, max count = 100
27+
TOPN-NEXT: boolean_operators, max count = 100
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# RUN: llvm-profdata show %s --hot-func-list | FileCheck %s
2+
3+
# CHECK: # Hot count threshold: 101
4+
# CHECK: hot_b
5+
# CHECK: hot_a
6+
# CHECK: hot_c
7+
8+
:ir
9+
hot_a
10+
# Func Hash:
11+
0x1234
12+
# Num Counters:
13+
1
14+
# Counter Values:
15+
101
16+
17+
hot_b
18+
0x5678
19+
1
20+
202
21+
22+
hot_c
23+
0x5678
24+
1
25+
101
26+
27+
cold_d
28+
0xabcd
29+
1
30+
1
31+
32+
cold_e
33+
0xefff
34+
1
35+
0

llvm/tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,9 +2846,9 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
28462846
auto FS = vfs::getRealFileSystem();
28472847
auto ReaderOrErr = InstrProfReader::create(Filename, *FS);
28482848
std::vector<uint32_t> Cutoffs = std::move(DetailedSummaryCutoffs);
2849-
if (ShowDetailedSummary && Cutoffs.empty()) {
2850-
Cutoffs = ProfileSummaryBuilder::DefaultCutoffs;
2851-
}
2849+
if (Cutoffs.empty())
2850+
if (ShowDetailedSummary || ShowHotFuncList)
2851+
Cutoffs = ProfileSummaryBuilder::DefaultCutoffs;
28522852
InstrProfSummaryBuilder Builder(std::move(Cutoffs));
28532853
if (Error E = ReaderOrErr.takeError())
28542854
exitWithError(std::move(E), Filename);
@@ -2860,15 +2860,7 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
28602860
int NumVPKind = IPVK_Last - IPVK_First + 1;
28612861
std::vector<ValueSitesStats> VPStats(NumVPKind);
28622862

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);
2863+
std::vector<std::pair<uint64_t, uint64_t>> NameRefAndMaxCount;
28722864

28732865
if (!TextFormat && OnlyListBelow) {
28742866
OS << "The list of functions with the maximum counter less than "
@@ -2942,15 +2934,9 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
29422934
if (OnlyListBelow)
29432935
continue;
29442936

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-
}
2937+
if (TopNFunctions || ShowHotFuncList)
2938+
NameRefAndMaxCount.emplace_back(IndexedInstrProf::ComputeHash(Func.Name),
2939+
FuncMax);
29542940

29552941
if (Show) {
29562942
if (!ShownFunctions)
@@ -3029,16 +3015,26 @@ static int showInstrProfile(ShowFormat SFormat, raw_fd_ostream &OS) {
30293015
<< "): " << PS->getNumFunctions() - BelowCutoffFunctions << "\n";
30303016
}
30313017

3018+
// Sort by MaxCount in decreasing order
3019+
llvm::stable_sort(NameRefAndMaxCount, [](const auto &L, const auto &R) {
3020+
return L.second > R.second;
3021+
});
30323022
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-
}
30383023
OS << "Top " << TopNFunctions
30393024
<< " functions with the largest internal block counts: \n";
3040-
for (auto &hotfunc : llvm::reverse(SortedHottestFuncs))
3041-
OS << " " << hotfunc.first << ", max count = " << hotfunc.second << "\n";
3025+
auto TopFuncs = ArrayRef(NameRefAndMaxCount).take_front(TopNFunctions);
3026+
for (auto [NameRef, MaxCount] : TopFuncs)
3027+
OS << " " << Reader->getSymtab().getFuncOrVarName(NameRef)
3028+
<< ", max count = " << MaxCount << "\n";
3029+
}
3030+
3031+
if (ShowHotFuncList) {
3032+
auto HotCountThreshold =
3033+
ProfileSummaryBuilder::getHotCountThreshold(PS->getDetailedSummary());
3034+
OS << "# Hot count threshold: " << HotCountThreshold << "\n";
3035+
for (auto [NameRef, MaxCount] : NameRefAndMaxCount)
3036+
if (MaxCount >= HotCountThreshold)
3037+
OS << Reader->getSymtab().getFuncOrVarName(NameRef) << "\n";
30423038
}
30433039

30443040
if (ShownFunctions && ShowIndirectCallTargets) {

0 commit comments

Comments
 (0)