[llvm] [BOLT][AArch64] Check Last Element Instead of Returning `nullptr` in `lookupStubFromGroup` (PR #114015)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 11 21:39:34 PST 2024
https://github.com/liusy58 updated https://github.com/llvm/llvm-project/pull/114015
>From a23d0f8aca58b7d62e87937469a52f77d3d4ecff Mon Sep 17 00:00:00 2001
From: liusy58 <liusy58 at linux.alibaba.com>
Date: Tue, 29 Oct 2024 16:09:31 +0800
Subject: [PATCH] [BOLT] Fix bug in `lookupStubFromGroup`
The current implementation of `lookupStubFromGroup` is incorrect. The function
is intended to find and return the closest stub using `lower_bound`, which
identifies the first element in a sorted range that is not less than a specified
value. However, if such an element is not found within `Candidates` and the list
is not empty, the function returns `nullptr`. Instead, it should check whether
the last element satisfies the condition.
---
bolt/lib/Passes/LongJmp.cpp | 6 +++---
bolt/test/AArch64/one-stub.s | 36 ++++++++++++++++++++++++++++++++++++
2 files changed, 39 insertions(+), 3 deletions(-)
create mode 100644 bolt/test/AArch64/one-stub.s
diff --git a/bolt/lib/Passes/LongJmp.cpp b/bolt/lib/Passes/LongJmp.cpp
index 0b2d00300f46b9..77acd2decc9bd9 100644
--- a/bolt/lib/Passes/LongJmp.cpp
+++ b/bolt/lib/Passes/LongJmp.cpp
@@ -130,9 +130,9 @@ BinaryBasicBlock *LongJmpPass::lookupStubFromGroup(
const std::pair<uint64_t, BinaryBasicBlock *> &RHS) {
return LHS.first < RHS.first;
});
- if (Cand == Candidates.end())
- return nullptr;
- if (Cand != Candidates.begin()) {
+ if (Cand == Candidates.end()) {
+ Cand = std::prev(Cand);
+ } else if (Cand != Candidates.begin()) {
const StubTy *LeftCand = std::prev(Cand);
if (Cand->first - DotAddress > DotAddress - LeftCand->first)
Cand = LeftCand;
diff --git a/bolt/test/AArch64/one-stub.s b/bolt/test/AArch64/one-stub.s
new file mode 100644
index 00000000000000..ed9929e85a71a0
--- /dev/null
+++ b/bolt/test/AArch64/one-stub.s
@@ -0,0 +1,36 @@
+# REQUIRES: system-linux, asserts
+# RUN: llvm-mc -filetype=obj -triple aarch64-unknown-unknown %s -o %t.o
+# RUN: %clang %cflags -O0 -fPIC -pie %t.o -o %t.exe -nostdlib -Wl,-q
+# RUN: link_fdata %s %t.o %t.fdata
+# RUN: llvm-bolt %t.exe -o %t.bolt \
+# RUN: --data %t.fdata | FileCheck %s
+# CHECK: BOLT-INFO: Inserted 1 stubs in the hot area and 0 stubs in the cold area.
+
+.section .text
+.global _start
+.global dummy
+.global func
+
+ .align 4
+ .global _start
+ .type _start, %function
+_start:
+# FDATA: 0 [unknown] 0 1 _start 0 0 100
+ bl func
+ bl func
+ ret
+ .space 0x8000000
+ .global func
+ .type func, %function
+func:
+ add x0, x0, #1
+ ret
+
+ .global dummy
+ .type dummy, %function
+dummy:
+# FDATA: 0 [unknown] 0 1 dummy 0 0 42
+ add x0, x0, #1
+ ret
+
+.reloc 0, R_AARCH64_NONE
\ No newline at end of file
More information about the llvm-commits
mailing list