Skip to content

[BOLT] Don't remove nops in non-simple functions #101964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bolt/lib/Passes/BinaryPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "bolt/Core/ParallelUtilities.h"
#include "bolt/Passes/ReorderAlgorithm.h"
#include "bolt/Passes/ReorderFunctions.h"
#include "bolt/Utils/CommandLineOpts.h"
#include "llvm/Support/CommandLine.h"
#include <atomic>
#include <mutex>
Expand Down Expand Up @@ -1923,7 +1924,7 @@ Error RemoveNops::runOnFunctions(BinaryContext &BC) {
};

ParallelUtilities::PredicateTy SkipFunc = [&](const BinaryFunction &BF) {
return BF.shouldPreserveNops();
return BF.shouldPreserveNops() || (!opts::StrictMode && !BF.isSimple());
};

ParallelUtilities::runOnEachFunction(
Expand Down
38 changes: 38 additions & 0 deletions bolt/test/AArch64/non-simple-nops-preserve.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# This test checks that the nop instruction is preserved before the
# ADRRelaxation pass. Otherwise the ADRRelaxation pass would
# fail to replace nop+adr to adrp+add for non-simple function.

# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown \
# RUN: %s -o %t.o
# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
# RUN: llvm-bolt %t.exe -o %t.bolt --adr-relaxation=true \
# RUN: --print-cfg --print-only=_start | FileCheck %s
# RUN: llvm-objdump -d -j .text %t.bolt | \
# RUN: FileCheck --check-prefix=DISASMCHECK %s

# CHECK: Binary Function "_start" after building cfg
# CHECK: IsSimple : 0

# DISASMCHECK: {{.*}}<_start>:
# DISASMCHECK-NEXT: adrp
# DISASMCHECK-NEXT: add
# DISASMCHECK-NEXT: adr

.text
.align 4
.global test
.type test, %function
test:
ret
.size test, .-test

.global _start
.type _start, %function
_start:
nop
adr x0, test
adr x1, 1f
1:
mov x1, x0
br x0
.size _start, .-_start
9 changes: 5 additions & 4 deletions bolt/test/RISCV/reloc-label-diff.s
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang %cflags -o %t %s
// RUN: llvm-bolt -o %t.bolt %t
// RUN: llvm-bolt --strict -o %t.bolt %t
// RUN: llvm-readelf -x .data %t.bolt | FileCheck %s

.text
Expand All @@ -9,15 +9,16 @@
_start:
// Force BOLT into relocation mode
.reloc 0, R_RISCV_NONE
// BOLT removes this nop so the label difference is initially 8 but should be
// 4 after BOLT processes it.
// BOLT removes this nop so the label difference is initially 12 but should be
// 8 after BOLT processes it.
nop
beq x0, x0, _test_end
addi x1, x1, 1
_test_end:
ret
.size _start, .-_start

.data
// CHECK: Hex dump of section '.data':
// CHECK: 0x{{.*}} 04000000
// CHECK: 0x{{.*}} 08000000
.word _test_end - _start
Loading