-
Notifications
You must be signed in to change notification settings - Fork 15k
[BOLT] Refactor handling of branch targets. NFCI #165828
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
Merged
Merged
+37
−26
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Refactor code that verifies external branch destinations and creates secondary entry points.
|
@llvm/pr-subscribers-bolt Author: Maksim Panchenko (maksfb) ChangesRefactor code that verifies external branch destinations and creates secondary entry points. Full diff: https://github.com/llvm/llvm-project/pull/165828.diff 4 Files Affected:
diff --git a/bolt/include/bolt/Core/BinaryContext.h b/bolt/include/bolt/Core/BinaryContext.h
index 5cbc28fb38a33..085c0265de3ed 100644
--- a/bolt/include/bolt/Core/BinaryContext.h
+++ b/bolt/include/bolt/Core/BinaryContext.h
@@ -932,6 +932,16 @@ class BinaryContext {
std::pair<const MCSymbol *, uint64_t>
handleAddressRef(uint64_t Address, BinaryFunction &BF, bool IsPCRel);
+ /// When \p Address inside function \p BF is a target of a control transfer
+ /// instruction (branch) from another function, return a corresponding symbol
+ /// that should be used by the branch. For example, main or secondary entry
+ /// point.
+ ///
+ /// If \p Address is an invalid destination, such as a constant island, return
+ /// nullptr and mark \p BF as ignored, since we cannot properly handle a
+ /// branch to a constant island.
+ MCSymbol *handleExternalBranchTarget(uint64_t Address, BinaryFunction &BF);
+
/// Analyze memory contents at the given \p Address and return the type of
/// memory contents (such as a possible jump table).
MemoryContentsType analyzeMemoryAt(uint64_t Address, BinaryFunction &BF);
diff --git a/bolt/lib/Core/BinaryContext.cpp b/bolt/lib/Core/BinaryContext.cpp
index c33540ada8a05..a383ced1712e3 100644
--- a/bolt/lib/Core/BinaryContext.cpp
+++ b/bolt/lib/Core/BinaryContext.cpp
@@ -518,6 +518,23 @@ BinaryContext::handleAddressRef(uint64_t Address, BinaryFunction &BF,
return std::make_pair(TargetSymbol, 0);
}
+MCSymbol *BinaryContext::handleExternalBranchTarget(uint64_t Address,
+ BinaryFunction &BF) {
+ if (BF.isInConstantIsland(Address)) {
+ BF.setIgnored();
+ this->outs() << "BOLT-WARNING: ignoring entry point at address 0x"
+ << Twine::utohexstr(Address)
+ << " in constant island of function " << BF << '\n';
+ return nullptr;
+ }
+
+ const uint64_t Offset = Address - BF.getAddress();
+ assert(Offset < BF.getSize() &&
+ "Address should be inside the referenced function");
+
+ return Offset ? BF.addEntryPointAtOffset(Offset) : BF.getSymbol();
+}
+
MemoryContentsType BinaryContext::analyzeMemoryAt(uint64_t Address,
BinaryFunction &BF) {
if (!isX86())
@@ -1399,17 +1416,10 @@ void BinaryContext::processInterproceduralReferences() {
<< Function.getPrintName() << " and "
<< TargetFunction->getPrintName() << '\n';
}
- if (uint64_t Offset = Address - TargetFunction->getAddress()) {
- if (!TargetFunction->isInConstantIsland(Address)) {
- TargetFunction->addEntryPointAtOffset(Offset);
- } else {
- TargetFunction->setIgnored();
- this->outs() << "BOLT-WARNING: Ignoring entry point at address 0x"
- << Twine::utohexstr(Address)
- << " in constant island of function " << *TargetFunction
- << '\n';
- }
- }
+
+ // Create an extra entry point if needed. Can also render the target
+ // function ignored if the reference is invalid.
+ handleExternalBranchTarget(Address, *TargetFunction);
continue;
}
diff --git a/bolt/lib/Core/BinaryFunction.cpp b/bolt/lib/Core/BinaryFunction.cpp
index fbe186454351c..ddaad6eef6140 100644
--- a/bolt/lib/Core/BinaryFunction.cpp
+++ b/bolt/lib/Core/BinaryFunction.cpp
@@ -1697,21 +1697,12 @@ bool BinaryFunction::scanExternalRefs() {
if (!TargetFunction || ignoreFunctionRef(*TargetFunction))
continue;
- const uint64_t FunctionOffset =
- TargetAddress - TargetFunction->getAddress();
- if (!TargetFunction->isInConstantIsland(TargetAddress)) {
- BranchTargetSymbol =
- FunctionOffset
- ? TargetFunction->addEntryPointAtOffset(FunctionOffset)
- : TargetFunction->getSymbol();
- } else {
- TargetFunction->setIgnored();
- BC.outs() << "BOLT-WARNING: Ignoring entry point at address 0x"
- << Twine::utohexstr(Address)
- << " in constant island of function " << *TargetFunction
- << '\n';
+ // Get a reference symbol for the function when address is a valid code
+ // reference.
+ BranchTargetSymbol =
+ BC.handleExternalBranchTarget(TargetAddress, *TargetFunction);
+ if (!BranchTargetSymbol)
continue;
- }
}
// Can't find more references. Not creating relocations since we are not
diff --git a/bolt/test/AArch64/constant-island-entry.s b/bolt/test/AArch64/constant-island-entry.s
index 7f8449deea130..a82b876fde46d 100644
--- a/bolt/test/AArch64/constant-island-entry.s
+++ b/bolt/test/AArch64/constant-island-entry.s
@@ -10,7 +10,7 @@
## Skip caller to check the identical warning is triggered from ScanExternalRefs().
# RUN: llvm-bolt %t.exe -o %t.bolt -skip-funcs=caller 2>&1 | FileCheck %s
-# CHECK: BOLT-WARNING: Ignoring entry point at address 0x{{[0-9a-f]+}} in constant island of function func
+# CHECK: BOLT-WARNING: ignoring entry point at address 0x{{[0-9a-f]+}} in constant island of function func
.globl func
.type func, %function
|
Jinjie-Huang
approved these changes
Oct 31, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! LGTM
aaupov
approved these changes
Oct 31, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refactor code that verifies external branch destinations and creates secondary entry points.