Skip to content

Commit 8786429

Browse files
authored
[BOLT] Fix preserved offset in fixDoubleJumps (#92485)
1 parent 0cd2bf3 commit 8786429

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

bolt/lib/Passes/BinaryPasses.cpp

+9-5
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,8 @@ static uint64_t fixDoubleJumps(BinaryFunction &Function, bool MarkInvalid) {
674674
MCPlusBuilder *MIB = Function.getBinaryContext().MIB.get();
675675
for (BinaryBasicBlock &BB : Function) {
676676
auto checkAndPatch = [&](BinaryBasicBlock *Pred, BinaryBasicBlock *Succ,
677-
const MCSymbol *SuccSym) {
677+
const MCSymbol *SuccSym,
678+
std::optional<uint32_t> Offset) {
678679
// Ignore infinite loop jumps or fallthrough tail jumps.
679680
if (Pred == Succ || Succ == &BB)
680681
return false;
@@ -715,9 +716,11 @@ static uint64_t fixDoubleJumps(BinaryFunction &Function, bool MarkInvalid) {
715716
Pred->removeSuccessor(&BB);
716717
Pred->eraseInstruction(Pred->findInstruction(Branch));
717718
Pred->addTailCallInstruction(SuccSym);
718-
MCInst *TailCall = Pred->getLastNonPseudoInstr();
719-
assert(TailCall);
720-
MIB->setOffset(*TailCall, BB.getOffset());
719+
if (Offset) {
720+
MCInst *TailCall = Pred->getLastNonPseudoInstr();
721+
assert(TailCall);
722+
MIB->setOffset(*TailCall, *Offset);
723+
}
721724
} else {
722725
return false;
723726
}
@@ -760,7 +763,8 @@ static uint64_t fixDoubleJumps(BinaryFunction &Function, bool MarkInvalid) {
760763
if (Pred->getSuccessor() == &BB ||
761764
(Pred->getConditionalSuccessor(true) == &BB && !IsTailCall) ||
762765
Pred->getConditionalSuccessor(false) == &BB)
763-
if (checkAndPatch(Pred, Succ, SuccSym) && MarkInvalid)
766+
if (checkAndPatch(Pred, Succ, SuccSym, MIB->getOffset(*Inst)) &&
767+
MarkInvalid)
764768
BB.markValid(BB.pred_size() != 0 || BB.isLandingPad() ||
765769
BB.isEntryPoint());
766770
}

bolt/test/X86/bb-with-two-tail-calls.s

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# This reproduces a bug with dynostats when trying to compute branch stats
22
# at a block with two tails calls (one conditional and one unconditional).
33

4-
# REQUIRES: system-linux
5-
64
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown \
75
# RUN: %s -o %t.o
86
# RUN: link_fdata %s %t.o %t.fdata
@@ -13,7 +11,7 @@
1311
# CHECK-NOT: Assertion `BranchInfo.size() == 2 && "could only be called for blocks with 2 successors"' failed.
1412
# Two tail calls in the same basic block after SCTC:
1513
# CHECK: {{.*}}: ja {{.*}} # TAILCALL # Offset: 7 # CTCTakenCount: 4
16-
# CHECK-NEXT: {{.*}}: jmp {{.*}} # TAILCALL # Offset: 12
14+
# CHECK-NEXT: {{.*}}: jmp {{.*}} # TAILCALL # Offset: 13
1715

1816
.globl _start
1917
_start:
@@ -23,7 +21,9 @@ a: ja b
2321
x: ret
2422
# FDATA: 1 _start #a# 1 _start #b# 2 4
2523
b: jmp e
26-
c: jmp f
24+
c:
25+
.nops 1
26+
jmp f
2727

2828
.globl e
2929
e:

0 commit comments

Comments
 (0)