Skip to content

Commit be19824

Browse files
author
Ken Matsui
committed
[PGO] Fix incorrect count threshold calculation when 0% cutoff
DefaultCutoffsData does not have an entry for the 0th percentile. As a result, when the getEntryForPercentile method is called with a percentile argument of 0, it returns a ProfileSummaryEntry for the 1st percentile instead. This behavior affects the threshold calculations, such as getHotCountThreshold, causing them to incorrectly identify some sample profile counts as hot when they should not be. This patch addresses the issue by adding an entry for the 0th percentile to DetailedSummary. This ensures that when the -profile-summary-cutoff-hot (or -cold) option is set to 0, samples are not incorrectly recognized as hot or cold.
1 parent 4389220 commit be19824

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

llvm/lib/ProfileData/ProfileSummaryBuilder.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ cl::opt<uint64_t> ProfileSummaryColdCount(
7070
// A set of cutoff values. Each value, when divided by ProfileSummary::Scale
7171
// (which is 1000000) is a desired percentile of total counts.
7272
static const uint32_t DefaultCutoffsData[] = {
73+
0, /* 0% */
7374
10000, /* 1% */
7475
100000, /* 10% */
7576
200000, 300000, 400000, 500000, 600000, 700000, 800000,
@@ -134,13 +135,22 @@ void ProfileSummaryBuilder::computeDetailedSummary() {
134135
if (DetailedSummaryCutoffs.empty())
135136
return;
136137
llvm::sort(DetailedSummaryCutoffs);
138+
139+
size_t StartIdx = 0;
140+
if (DetailedSummaryCutoffs.front() == 0) {
141+
// Put an entry for the 0th percentile. Assume there is no UINT64_MAX
142+
// sample count.
143+
DetailedSummary.emplace_back(0, UINT64_MAX, 0);
144+
StartIdx = 1;
145+
}
146+
137147
auto Iter = CountFrequencies.begin();
138148
const auto End = CountFrequencies.end();
139149

140150
uint32_t CountsSeen = 0;
141151
uint64_t CurrSum = 0, Count = 0;
142152

143-
for (const uint32_t Cutoff : DetailedSummaryCutoffs) {
153+
for (const uint32_t Cutoff : drop_begin(DetailedSummaryCutoffs, StartIdx)) {
144154
assert(Cutoff <= 999999);
145155
APInt Temp(128, TotalCount);
146156
APInt N(128, Cutoff);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,8 +1111,8 @@ static void updateInstrProfileEntry(InstrProfileEntry &IFE, bool SetToHot,
11111111
});
11121112
}
11131113

1114-
const uint64_t ColdPercentileIdx = 15;
1115-
const uint64_t HotPercentileIdx = 11;
1114+
const uint64_t ColdPercentileIdx = 16;
1115+
const uint64_t HotPercentileIdx = 12;
11161116

11171117
using sampleprof::FSDiscriminatorPass;
11181118

0 commit comments

Comments
 (0)