Skip to content

Commit cb1d8ec

Browse files
committed
Add minimum count threshold for indirect call promotion
This adds a new command-line option -icp-minimum-count-threshold to prevent promotion of indirect call candidates with very low absolute counts, even if they meet the percentage-based profitability thresholds. Currently, candidates with counts as low as 1-2 calls can be promoted if they represent a significant percentage of the call site. This can lead to unprofitable micro-optimizations that increase code size without meaningful performance benefits. The new threshold defaults to 0 to maintain backward compatibility but can be set to values like 5-20 for production use to filter out statistically insignificant candidates. Added comprehensive test coverage for the new threshold including negative tests (blocking low-count promotions), positive tests (allowing high-count promotions), and edge cases (threshold exactly at count value).
1 parent 26f3f24 commit cb1d8ec

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

llvm/lib/Analysis/IndirectCallPromotionAnalysis.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ static cl::opt<unsigned>
3737
cl::desc("The percentage threshold against total "
3838
"count for the promotion"));
3939

40+
// Set the minimum absolute count threshold for indirect call promotion.
41+
// Candidates with counts below this threshold will not be promoted.
42+
static cl::opt<unsigned> ICPMinimumCountThreshold(
43+
"icp-minimum-count-threshold", cl::init(0), cl::Hidden,
44+
cl::desc("Minimum absolute count for promotion candidate"));
45+
4046
// Set the maximum number of targets to promote for a single indirect-call
4147
// callsite.
4248
static cl::opt<unsigned>
@@ -51,7 +57,8 @@ cl::opt<unsigned> MaxNumVTableAnnotations(
5157
bool ICallPromotionAnalysis::isPromotionProfitable(uint64_t Count,
5258
uint64_t TotalCount,
5359
uint64_t RemainingCount) {
54-
return Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
60+
return Count >= ICPMinimumCountThreshold &&
61+
Count * 100 >= ICPRemainingPercentThreshold * RemainingCount &&
5562
Count * 100 >= ICPTotalPercentThreshold * TotalCount;
5663
}
5764

llvm/test/Transforms/PGOProfile/indirect_call_promotion.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
; RUN: opt < %s -passes=pgo-icall-prom -S -icp-total-percent-threshold=50 | FileCheck %s --check-prefix=ICALL-PROM
22
; RUN: opt < %s -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=0 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=PASS-REMARK
33
; RUN: opt < %s -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=20 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=PASS2-REMARK
4+
; Test minimum count threshold - should prevent func1 promotion (count 10 < threshold 15)
5+
; RUN: opt < %s -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-minimum-count-threshold=15 -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=0 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=MIN-COUNT-BLOCK
6+
; Test minimum count threshold - should allow func4 promotion (count 1030 > threshold 15)
7+
; RUN: opt < %s -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-minimum-count-threshold=15 -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=0 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=MIN-COUNT-ALLOW
8+
; Test edge case - threshold exactly at count value
9+
; RUN: opt < %s -passes=pgo-icall-prom -S -pass-remarks=pgo-icall-prom -icp-minimum-count-threshold=10 -icp-remaining-percent-threshold=0 -icp-total-percent-threshold=0 -icp-max-prom=4 2>&1 | FileCheck %s --check-prefix=MIN-COUNT-EDGE
410

511
; PASS-REMARK: remark: <unknown>:0:0: Promote indirect call to func4 with count 1030 out of 1600
612
; PASS-REMARK: remark: <unknown>:0:0: Promote indirect call to func2 with count 410 out of 570
@@ -12,6 +18,21 @@
1218
; PASS2-REMARK-NOT: remark: <unknown>:0:0: Promote indirect call to func3
1319
; PASS2-REMARK-NOT: remark: <unknown>:0:0: Promote indirect call to func1
1420

21+
; MIN-COUNT-BLOCK: remark: <unknown>:0:0: Promote indirect call to func4 with count 1030 out of 1600
22+
; MIN-COUNT-BLOCK: remark: <unknown>:0:0: Promote indirect call to func2 with count 410 out of 570
23+
; MIN-COUNT-BLOCK: remark: <unknown>:0:0: Promote indirect call to func3 with count 150 out of 160
24+
; MIN-COUNT-BLOCK-NOT: remark: <unknown>:0:0: Promote indirect call to func1
25+
26+
; MIN-COUNT-ALLOW: remark: <unknown>:0:0: Promote indirect call to func4 with count 1030 out of 1600
27+
; MIN-COUNT-ALLOW: remark: <unknown>:0:0: Promote indirect call to func2 with count 410 out of 570
28+
; MIN-COUNT-ALLOW: remark: <unknown>:0:0: Promote indirect call to func3 with count 150 out of 160
29+
; MIN-COUNT-ALLOW-NOT: remark: <unknown>:0:0: Promote indirect call to func1
30+
31+
; MIN-COUNT-EDGE: remark: <unknown>:0:0: Promote indirect call to func4 with count 1030 out of 1600
32+
; MIN-COUNT-EDGE: remark: <unknown>:0:0: Promote indirect call to func2 with count 410 out of 570
33+
; MIN-COUNT-EDGE: remark: <unknown>:0:0: Promote indirect call to func3 with count 150 out of 160
34+
; MIN-COUNT-EDGE: remark: <unknown>:0:0: Promote indirect call to func1 with count 10 out of 10
35+
1536
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
1637
target triple = "x86_64-unknown-linux-gnu"
1738

0 commit comments

Comments
 (0)