Skip to content

Commit cbd3024

Browse files
authored
[BOLT] Improve BinaryFunction::inferFallThroughCounts() (#105450)
This PR improves how basic block execution count is updated when using the BOLT option `-infer-fall-throughs`. Previously, if a 0-count fall-through edge is assigned a positive inferred count N, then the successor block's execution count will be incremented by N. Since the successor's execution count is calculated using information besides inflow sum (such as outflow sum), it likely is already correct, and incrementing it by an additional N would be wrong. This PR improves how the successor's execution count is updated by using the max over its current count and N.
1 parent 35cec80 commit cbd3024

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

bolt/lib/Core/BinaryFunctionProfile.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,8 @@ void BinaryFunction::inferFallThroughCounts() {
336336
if (SuccBI.Count == 0) {
337337
SuccBI.Count = Inferred;
338338
SuccBI.MispredictedCount = BinaryBasicBlock::COUNT_INFERRED;
339-
Succ->ExecutionCount += Inferred;
339+
Succ->ExecutionCount =
340+
std::max(Succ->getKnownExecutionCount(), Inferred);
340341
}
341342
}
342343
}

bolt/test/X86/infer-fall-throughs.s

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Test that infer-fall-throughs would correctly infer the wrong fall-through
2+
## edge count in the example
3+
4+
# RUN: llvm-mc --filetype=obj --triple x86_64-unknown-unknown %s -o %t.o
5+
# RUN: link_fdata %s %t.o %t.fdata
6+
# RUN: llvm-strip --strip-unneeded %t.o
7+
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
8+
# RUN: llvm-bolt %t.exe -o %t.bolt \
9+
# RUN: --print-estimate-edge-counts --data=%t.fdata \
10+
# RUN: 2>&1 | FileCheck --check-prefix=WITHOUTINFERENCE %s
11+
# RUN: llvm-bolt %t.exe -o %t.bolt --infer-fall-throughs \
12+
# RUN: --print-estimate-edge-counts --data=%t.fdata \
13+
# RUN: 2>&1 | FileCheck --check-prefix=CORRECTINFERENCE %s
14+
15+
16+
# WITHOUTINFERENCE: Binary Function "main" after estimate-edge-counts
17+
# WITHOUTINFERENCE: {{^\.Ltmp0}}
18+
# WITHOUTINFERENCE: Successors: .Ltmp1 (mispreds: 0, count: 10), .LFT0 (mispreds: 0, count: 0)
19+
# WITHOUTINFERENCE: {{^\.LFT0}}
20+
# WITHOUTINFERENCE: Exec Count : 490
21+
22+
# CORRECTINFERENCE: Binary Function "main" after estimate-edge-counts
23+
# CORRECTINFERENCE: {{^\.Ltmp0}}
24+
# CORRECTINFERENCE: Successors: .Ltmp1 (mispreds: 0, count: 10), .LFT0 (inferred count: 490)
25+
# CORRECTINFERENCE: {{^\.LFT0}}
26+
# CORRECTINFERENCE: Exec Count : 490
27+
28+
29+
.globl main
30+
.type main, @function
31+
main:
32+
LLmain_LLstart:
33+
jmp LLstart
34+
# FDATA: 1 main #LLmain_LLstart# 1 main #LLstart# 0 500
35+
LLstart:
36+
jge LLexit
37+
# FDATA: 1 main #LLstart# 1 main #LLexit# 0 10
38+
# FDATA: 1 main #LLstart# 1 main #LLmore# 0 0
39+
LLmore:
40+
movl $5, %eax
41+
# FDATA: 1 main #LLmore# 1 main #LLexit# 0 490
42+
LLexit:
43+
ret
44+
.LLmain_end:
45+
.size main, .LLmain_end-main

0 commit comments

Comments
 (0)