Skip to content

Commit 2847e15

Browse files
authored
[PGO] Fix incorrect count threshold calculation when 0% cutoff (llvm#117359)
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 handling the 0th percentile case in the getEntryForPercentile method. This ensures that when the -profile-summary-cutoff-hot (or -cold) option is set to 0, no sample counts are treated as hot (or all sample counts are treated as cold).
1 parent 2c05d02 commit 2847e15

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

llvm/lib/ProfileData/ProfileSummaryBuilder.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,18 @@ static const uint32_t DefaultCutoffsData[] = {
7777
const ArrayRef<uint32_t> ProfileSummaryBuilder::DefaultCutoffs =
7878
DefaultCutoffsData;
7979

80+
// An entry for the 0th percentile to correctly calculate hot/cold count
81+
// thresholds when -profile-summary-cutoff-hot/cold is 0. If the hot cutoff is
82+
// 0, no sample counts are treated as hot. If the cold cutoff is 0, all sample
83+
// counts are treated as cold. Assumes there is no UINT64_MAX sample counts.
84+
static const ProfileSummaryEntry ZeroCutoffEntry = {0, UINT64_MAX, 0};
85+
8086
const ProfileSummaryEntry &
8187
ProfileSummaryBuilder::getEntryForPercentile(const SummaryEntryVector &DS,
8288
uint64_t Percentile) {
89+
if (Percentile == 0)
90+
return ZeroCutoffEntry;
91+
8392
auto It = partition_point(DS, [=](const ProfileSummaryEntry &Entry) {
8493
return Entry.Cutoff < Percentile;
8594
});

llvm/test/Analysis/ProfileSummary/basic.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
; RUN: opt < %s -disable-output -profile-summary-hot-count=500 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=OVERRIDE-HOT
33
; RUN: opt < %s -disable-output -profile-summary-cold-count=0 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=OVERRIDE-COLD
44
; RUN: opt < %s -disable-output -profile-summary-cold-count=200 -profile-summary-hot-count=1000 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=OVERRIDE-BOTH
5+
; RUN: opt < %s -disable-output -profile-summary-cutoff-hot=0 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=HOT-CUTOFF-0
6+
; RUN: opt < %s -disable-output -profile-summary-cutoff-cold=0 -profile-summary-hot-count=18446744073709551615 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=COLD-CUTOFF-0
57

68
define void @f1() !prof !20 {
79
; CHECK-LABEL: f1 :hot
810
; OVERRIDE-HOT-LABEL: f1
911
; OVERRIDE-COLD-LABEL: f1 :hot
1012
; OVERRIDE-BOTH-LABEL: f1
13+
; HOT-CUTOFF-0-LABEL: f1{{$}}
14+
; COLD-CUTOFF-0-LABEL: f1 :cold
1115

1216
ret void
1317
}
@@ -17,6 +21,8 @@ define void @f2() !prof !21 {
1721
; OVERRIDE-HOT-LABEL: f2 :cold
1822
; OVERRIDE-COLD-LABEL: f2
1923
; OVERRIDE-BOTH-LABEL: f2
24+
; HOT-CUTOFF-0-LABEL: f2 :cold
25+
; COLD-CUTOFF-0-LABEL: f2 :cold
2026

2127
ret void
2228
}
@@ -26,6 +32,8 @@ define void @f3() !prof !22 {
2632
; OVERRIDE-HOT-LABEL: f3
2733
; OVERRIDE-COLD-LABEL: f3
2834
; OVERRIDE-BOTH-LABEL: f3
35+
; HOT-CUTOFF-0-LABEL: f3{{$}}
36+
; COLD-CUTOFF-0-LABEL: f3 :cold
2937

3038
ret void
3139
}

0 commit comments

Comments
 (0)