Skip to content

Commit 49f6f4c

Browse files
committed
[BOLT] Don't remove nops in non-simple functions
Follow the logic of https://reviews.llvm.org/D143887 patch (fixed later by llvm#71377) we don't want to remove nops in non-simple function just in case there is undetected jump table to minimize chances to break offsets in it.
1 parent 72b73e2 commit 49f6f4c

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

bolt/lib/Passes/BinaryPasses.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "bolt/Core/ParallelUtilities.h"
1616
#include "bolt/Passes/ReorderAlgorithm.h"
1717
#include "bolt/Passes/ReorderFunctions.h"
18+
#include "bolt/Utils/CommandLineOpts.h"
1819
#include "llvm/Support/CommandLine.h"
1920
#include <atomic>
2021
#include <mutex>
@@ -1923,7 +1924,7 @@ Error RemoveNops::runOnFunctions(BinaryContext &BC) {
19231924
};
19241925

19251926
ParallelUtilities::PredicateTy SkipFunc = [&](const BinaryFunction &BF) {
1926-
return BF.shouldPreserveNops();
1927+
return BF.shouldPreserveNops() || (!opts::StrictMode && !BF.isSimple());
19271928
};
19281929

19291930
ParallelUtilities::runOnEachFunction(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# This test checks that the nop instruction is preserved before the
2+
# ADRRelaxation pass. Otherwise the ADRRelaxation pass would
3+
# fail to replace nop+adr to adrp+add for non-simple function.
4+
5+
# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown \
6+
# RUN: %s -o %t.o
7+
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
8+
# RUN: llvm-bolt %t.exe -o %t.bolt --adr-relaxation=true \
9+
# RUN: --print-cfg --print-only=_start | FileCheck %s
10+
# RUN: llvm-objdump -d -j .text %t.bolt | \
11+
# RUN: FileCheck --check-prefix=DISASMCHECK %s
12+
13+
# CHECK: Binary Function "_start" after building cfg
14+
# CHECK: IsSimple : 0
15+
16+
# DISASMCHECK: {{.*}}<_start>:
17+
# DISASMCHECK-NEXT: adrp
18+
# DISASMCHECK-NEXT: add
19+
# DISASMCHECK-NEXT: adr
20+
21+
.text
22+
.align 4
23+
.global test
24+
.type test, %function
25+
test:
26+
ret
27+
.size test, .-test
28+
29+
.global _start
30+
.type _start, %function
31+
_start:
32+
nop
33+
adr x0, test
34+
adr x1, 1f
35+
1:
36+
mov x1, x0
37+
br x0
38+
.size _start, .-_start

bolt/test/RISCV/reloc-label-diff.s

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %clang %cflags -o %t %s
2-
// RUN: llvm-bolt -o %t.bolt %t
2+
// RUN: llvm-bolt --strict -o %t.bolt %t
33
// RUN: llvm-readelf -x .data %t.bolt | FileCheck %s
44

55
.text
@@ -9,15 +9,16 @@
99
_start:
1010
// Force BOLT into relocation mode
1111
.reloc 0, R_RISCV_NONE
12-
// BOLT removes this nop so the label difference is initially 8 but should be
13-
// 4 after BOLT processes it.
12+
// BOLT removes this nop so the label difference is initially 12 but should be
13+
// 8 after BOLT processes it.
1414
nop
1515
beq x0, x0, _test_end
16+
addi x1, x1, 1
1617
_test_end:
1718
ret
1819
.size _start, .-_start
1920

2021
.data
2122
// CHECK: Hex dump of section '.data':
22-
// CHECK: 0x{{.*}} 04000000
23+
// CHECK: 0x{{.*}} 08000000
2324
.word _test_end - _start

0 commit comments

Comments
 (0)