[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.
This commit is contained in:
ShatianWang 2024-08-21 00:35:07 -04:00 committed by GitHub
parent 35cec805bf
commit cbd302410e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 47 additions and 1 deletions

View File

@ -336,7 +336,8 @@ void BinaryFunction::inferFallThroughCounts() {
if (SuccBI.Count == 0) {
SuccBI.Count = Inferred;
SuccBI.MispredictedCount = BinaryBasicBlock::COUNT_INFERRED;
Succ->ExecutionCount += Inferred;
Succ->ExecutionCount =
std::max(Succ->getKnownExecutionCount(), Inferred);
}
}
}

View File

@ -0,0 +1,45 @@
## Test that infer-fall-throughs would correctly infer the wrong fall-through
## edge count in the example
# RUN: llvm-mc --filetype=obj --triple x86_64-unknown-unknown %s -o %t.o
# RUN: link_fdata %s %t.o %t.fdata
# RUN: llvm-strip --strip-unneeded %t.o
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
# RUN: llvm-bolt %t.exe -o %t.bolt \
# RUN: --print-estimate-edge-counts --data=%t.fdata \
# RUN: 2>&1 | FileCheck --check-prefix=WITHOUTINFERENCE %s
# RUN: llvm-bolt %t.exe -o %t.bolt --infer-fall-throughs \
# RUN: --print-estimate-edge-counts --data=%t.fdata \
# RUN: 2>&1 | FileCheck --check-prefix=CORRECTINFERENCE %s
# WITHOUTINFERENCE: Binary Function "main" after estimate-edge-counts
# WITHOUTINFERENCE: {{^\.Ltmp0}}
# WITHOUTINFERENCE: Successors: .Ltmp1 (mispreds: 0, count: 10), .LFT0 (mispreds: 0, count: 0)
# WITHOUTINFERENCE: {{^\.LFT0}}
# WITHOUTINFERENCE: Exec Count : 490
# CORRECTINFERENCE: Binary Function "main" after estimate-edge-counts
# CORRECTINFERENCE: {{^\.Ltmp0}}
# CORRECTINFERENCE: Successors: .Ltmp1 (mispreds: 0, count: 10), .LFT0 (inferred count: 490)
# CORRECTINFERENCE: {{^\.LFT0}}
# CORRECTINFERENCE: Exec Count : 490
.globl main
.type main, @function
main:
LLmain_LLstart:
jmp LLstart
# FDATA: 1 main #LLmain_LLstart# 1 main #LLstart# 0 500
LLstart:
jge LLexit
# FDATA: 1 main #LLstart# 1 main #LLexit# 0 10
# FDATA: 1 main #LLstart# 1 main #LLmore# 0 0
LLmore:
movl $5, %eax
# FDATA: 1 main #LLmore# 1 main #LLexit# 0 490
LLexit:
ret
.LLmain_end:
.size main, .LLmain_end-main