Skip to content

Conversation

@liusy58
Copy link
Contributor

@liusy58 liusy58 commented Oct 29, 2024

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.

@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added the BOLT label Oct 29, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 29, 2024

@llvm/pr-subscribers-bolt

Author: Nicholas (liusy58)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/114015.diff

1 Files Affected:

  • (modified) bolt/lib/Passes/LongJmp.cpp (+3-3)
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;

@liusy58 liusy58 changed the title [BOLT][AArch64]lookupStubFromGroup Should Check the Last Element Instead of Returning nullptr When lower_bound Reaches End Iterator [BOLT][AArch64] Return Closest Element Instead of nullptr in lookupStubFromGroup Oct 29, 2024
@liusy58 liusy58 changed the title [BOLT][AArch64] Return Closest Element Instead of nullptr in lookupStubFromGroup [BOLT][AArch64] Check Last Element Instead of Returning nullptr in lookupStubFromGroup Oct 29, 2024
Copy link
Member

@paschalis-mpeis paschalis-mpeis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patch! At a quick glance this seems to be a valid edge case.
It happens when all candidates (sorted stubs) are smaller than the input DotAddress?

In theory, there can be a pathological case where each DotAddress happens to be bigger during lookup, causing the creation of new stubs each time.

Have you encountered any such extremes? And can you come up with a test case?

@paschalis-mpeis
Copy link
Member

Sharing some stats based on a follow-up investigation I did as part of issue #99848
I saw 30% reduction of local cold stubs in LongJmp in the issue's complex binary.


Details:
When applying this patch and having the now-merged #96609 fix on, this case was encountered 118 times.
All 118 cases were in range and all concerned local cold stubs.

This caused a total of 388 cold stubs to be created, 121 down from 509 (which is just #96609, without #114015; see table here at section 3).

The extra 3 stubs that were not created (ie the remainder of 121-118), were local cold stubs that happen to remain in the range of PCOffset, probably due to the overall reduction of stubs.

Copy link
Member

@paschalis-mpeis paschalis-mpeis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test. LGTM!

Please consider addressing the below nits.

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.
Copy link
Member

@paschalis-mpeis paschalis-mpeis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, another nit I've missed.

Consider renaming your test file to something like long-jmp-one-stub.s

@liusy58
Copy link
Contributor Author

liusy58 commented Dec 12, 2024

Okay, I have updated the file name.

@paschalis-mpeis
Copy link
Member

Thanks @liusy58 that is some fast action!

Not sure if I'm missing something, I still see the file as bolt/test/AArch64/one-stub.s.
Since you are still at it, you may remove -nostdlib from RUN line 6.

@liusy58
Copy link
Contributor Author

liusy58 commented Dec 12, 2024

Ok, I will fix it right away.

@liusy58
Copy link
Contributor Author

liusy58 commented Dec 12, 2024

Removing -nostdlib may get some errors:

ld.lld: error: duplicate symbol: _start

@paschalis-mpeis
Copy link
Member

Removing -nostdlib may get some errors

Are you sure? I thought testing like llvm-lit bolt/test/AArch64/long-jmp-one-stub.s would work as it's included on AArch64's config.

@liusy58
Copy link
Contributor Author

liusy58 commented Dec 12, 2024

Let me try it!

@liusy58
Copy link
Contributor Author

liusy58 commented Dec 12, 2024

@paschalis-mpeis I have updated again!

@paschalis-mpeis
Copy link
Member

Great, thanks for all the changes!

Give it a bit of time in case Meta has any comments. If not, you may merge by EOD tomorrow?

@liusy58
Copy link
Contributor Author

liusy58 commented Dec 12, 2024

Thank you!

@liusy58
Copy link
Contributor Author

liusy58 commented Dec 12, 2024

I might have messed up the commit, so I opened another PR. Sorry about that...

#119710

@paschalis-mpeis
Copy link
Member

Sorry, what was 'messed up' with this commit? I don't see an issue.

@liusy58 liusy58 closed this Dec 14, 2024
@liusy58 liusy58 reopened this Dec 14, 2024
@liusy58 liusy58 closed this Dec 14, 2024
@liusy58 liusy58 reopened this Dec 14, 2024
@paschalis-mpeis paschalis-mpeis merged commit 671095b into llvm:main Dec 16, 2024
14 checks passed
@github-actions
Copy link

@liusy58 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants