Skip to content

Commit b62dc0b

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 b62dc0b

File tree

6 files changed

+65
-3
lines changed

6 files changed

+65
-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);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
; RUN: opt < %s -disable-output -passes=print-profile-summary -S 2>&1 | FileCheck %s
2+
; RUN: opt < %s -disable-output -profile-summary-cutoff-hot=0 -passes=print-profile-summary -S 2>&1 | FileCheck %s -check-prefixes=HOT-CUTOFF-0
3+
; 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
4+
5+
define void @f1() !prof !20 {
6+
; CHECK-LABEL: f1 :hot
7+
; HOT-CUTOFF-0-LABEL: f1{{$}}
8+
; COLD-CUTOFF-0-LABEL: f1 :cold
9+
10+
ret void
11+
}
12+
13+
define void @f2() !prof !21 {
14+
; CHECK-LABEL: f2 :cold
15+
; HOT-CUTOFF-0-LABEL: f2 :cold
16+
; COLD-CUTOFF-0-LABEL: f2 :cold
17+
18+
ret void
19+
}
20+
21+
define void @f3() !prof !22 {
22+
; CHECK-LABEL: f3 :hot
23+
; HOT-CUTOFF-0-LABEL: f3{{$}}
24+
; COLD-CUTOFF-0-LABEL: f3 :cold
25+
26+
ret void
27+
}
28+
29+
!llvm.module.flags = !{!1}
30+
!20 = !{!"function_entry_count", i64 400}
31+
!21 = !{!"function_entry_count", i64 1}
32+
!22 = !{!"function_entry_count", i64 100}
33+
34+
!1 = !{i32 1, !"ProfileSummary", !2}
35+
!2 = !{!3, !4, !5, !6, !7, !8, !9, !10}
36+
!3 = !{!"ProfileFormat", !"InstrProf"}
37+
!4 = !{!"TotalCount", i64 10000}
38+
!5 = !{!"MaxCount", i64 10}
39+
!6 = !{!"MaxInternalCount", i64 1}
40+
!7 = !{!"MaxFunctionCount", i64 1000}
41+
!8 = !{!"NumCounts", i64 3}
42+
!9 = !{!"NumFunctions", i64 3}
43+
!10 = !{!"DetailedSummary", !11}
44+
!11 = !{!12, !13, !14, !15}
45+
!12 = !{i32 0, i64 18446744073709551615, i32 0}
46+
!13 = !{i32 10000, i64 100, i32 1}
47+
!14 = !{i32 990000, i64 100, i32 1}
48+
!15 = !{i32 999999, i64 1, i32 2}

llvm/test/tools/llvm-profdata/cs-sample-nested-profile.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ RUN: llvm-profdata show -sample -detailed-summary %t3.proftext | FileCheck %s -c
157157
; SUMMARY-NEXT: Total number of blocks: 16
158158
; SUMMARY-NEXT: Total count: 772562
159159
; SUMMARY-NEXT: Detailed summary:
160+
; SUMMARY-NEXT: 0 blocks with count >= 18446744073709551615 account for 0 percentage of the total counts.
160161
; SUMMARY-NEXT: 1 blocks with count >= 362830 account for 1 percentage of the total counts.
161162
; SUMMARY-NEXT: 1 blocks with count >= 362830 account for 10 percentage of the total counts.
162163
; SUMMARY-NEXT: 1 blocks with count >= 362830 account for 20 percentage of the total counts.
@@ -181,6 +182,7 @@ RUN: llvm-profdata show -sample -detailed-summary %t3.proftext | FileCheck %s -c
181182
; SUMMARY-NEST-NEXT: Total number of blocks: 15
182183
; SUMMARY-NEST-NEXT: Total count: 772504
183184
; SUMMARY-NEST-NEXT: Detailed summary:
185+
; SUMMARY-NEST-NEXT: 0 blocks with count >= 18446744073709551615 account for 0 percentage of the total counts.
184186
; SUMMARY-NEST-NEXT: 1 blocks with count >= 362830 account for 1 percentage of the total counts.
185187
; SUMMARY-NEST-NEXT: 1 blocks with count >= 362830 account for 10 percentage of the total counts.
186188
; SUMMARY-NEST-NEXT: 1 blocks with count >= 362830 account for 20 percentage of the total counts.

llvm/test/tools/llvm-profdata/sample-summary.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
; CHECK-NEXT: Total number of blocks: 11
77
; CHECK-NEXT: Total count: 12943
88
; CHECK-NEXT: Detailed summary:
9+
; CHECK-NEXT: 0 blocks with count >= 18446744073709551615 account for 0 percentage of the total counts.
910
; CHECK-NEXT: 1 blocks with count >= 2080 account for 1 percentage of the total counts.
1011
; CHECK-NEXT: 1 blocks with count >= 2080 account for 10 percentage of the total counts.
1112
; CHECK-NEXT: 2 blocks with count >= 2064 account for 20 percentage of the total counts.

llvm/test/tools/llvm-profdata/suppl-instr-with-sample.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ MIX5-NEXT: Maximum internal block count: 2000
9898
MIX5-NEXT: Total number of blocks: 9
9999
MIX5-NEXT: Total count: 6525
100100
MIX5-NEXT: Detailed summary:
101+
MIX5-NEXT: 0 blocks with count >= 18446744073709551615 account for 0 percentage of the total counts.
101102
MIX5-NEXT: 1 blocks with count >= 3000 account for 1 percentage of the total counts.
102103
MIX5-NEXT: 1 blocks with count >= 3000 account for 10 percentage of the total counts.
103104
MIX5-NEXT: 1 blocks with count >= 3000 account for 20 percentage of the total counts.

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)