-
Notifications
You must be signed in to change notification settings - Fork 15.9k
[IR][InstCombine] Fix O(n^2) complexity in SliceUpIllegalIntegerPHI #175468
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
Conversation
|
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 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. |
|
@llvm/pr-subscribers-llvm-ir @llvm/pr-subscribers-llvm-transforms Author: Kirill Pertsev (kika) ChangesFixes #175465 This is all based on my understanding of the LLVM codebase and reading comments in the code. InstCombine passThe catchswitch predecessor check added in LLVM 15 was placed inside the per-PHI processing loop, causing getFirstInsertionPt() to be called repeatedly for the same predecessor blocks across all PHIs in a network. For code patterns with many PHIs sharing common predecessors (e.g., Rust drop flags for large structs), this resulted in O(PHIs × blocks) complexity. Move the check outside the loop by collecting all unique predecessor blocks first and checking each only once, reducing complexity to O(unique blocks). Should fix rust-lang/rust#129713 Full diff: https://github.com/llvm/llvm-project/pull/175468.diff 1 Files Affected:
diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index ba1865a2b5469..78d45b691c8fc 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -1129,13 +1129,6 @@ Instruction *InstCombinerImpl::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) {
return nullptr;
}
- // If the incoming value is a PHI node before a catchswitch, we cannot
- // extract the value within that BB because we cannot insert any non-PHI
- // instructions in the BB.
- for (auto *Pred : PN->blocks())
- if (Pred->getFirstInsertionPt() == Pred->end())
- return nullptr;
-
for (User *U : PN->users()) {
Instruction *UserI = cast<Instruction>(U);
@@ -1172,6 +1165,21 @@ Instruction *InstCombinerImpl::SliceUpIllegalIntegerPHI(PHINode &FirstPhi) {
if (PHIUsers.empty())
return replaceInstUsesWith(FirstPhi, PoisonValue::get(FirstPhi.getType()));
+ // If the incoming value is a PHI node before a catchswitch, we cannot
+ // extract the value within that BB because we cannot insert any non-PHI
+ // instructions in the BB. Check all unique predecessor blocks once.
+ {
+ SmallPtrSet<BasicBlock *, 16> CheckedBlocks;
+ for (PHINode *PN : PHIsToSlice) {
+ for (BasicBlock *Pred : PN->blocks()) {
+ if (CheckedBlocks.insert(Pred).second) {
+ if (Pred->getFirstInsertionPt() == Pred->end())
+ return nullptr;
+ }
+ }
+ }
+ }
+
// If this phi node is transformable, create new PHIs for all the pieces
// extracted out of it. First, sort the users by their offset and size.
array_pod_sort(PHIUsers.begin(), PHIUsers.end());
|
nikic
left a comment
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.
This does fix the problem, but I'd like to suggest an alternative: There is really no reason why this check should be expensive in the first place. Checking whether a block has an insertion point is really just "is the terminator catchswitch" (or maybe phrased more generically, "is the terminator an EH pad"), which is a cheap O(1) operation.
The only reason this ends up expensive here is that finding the insertion point (if it exists) requires scanning through all the phi nodes in the block. But just checking if there is one is cheap.
What I'd suggest doing is add a method BasicBlock::hasInsertionPt() and then use it here (can then keep the code in the original position). Then we can also update other occurrences of this pattern like these:
| if (isa<PHINode>(Inst) && BB->getFirstInsertionPt() == BB->end()) |
| PN.getParent()->getFirstInsertionPt() != PN.getParent()->end()) { |
llvm-project/llvm/lib/Transforms/Scalar/SROA.cpp
Line 1385 in a5fa246
| I.getParent()->getFirstInsertionPt() == I.getParent()->end()) |
I haven't checked if any of those could have compile-time scalability issues, but better safe than sorry.
nikic
left a comment
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.
LGTM
Oops, inline is not necessary in the header Co-authored-by: Nikita Popov <github@npopov.com>
|
@kika 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! |
…lvm#175468) Add a hasInsertionPt() helper, which is equivalent to getFirstInsertionPt() != end(), but performs the check in O(1) instead of O(n). In particular, this avoids quadratic complexity inside SliceUpIllegalIntegerPHI(). Fixes llvm#175465. Should fix rust-lang/rust#129713.
Add a hasInsertionPt() helper, which is equivalent to getFirstInsertionPt() != end(), but performs the check in O(1) instead of O(n). In particular, this avoids quadratic complexity inside SliceUpIllegalIntegerPHI().
Fixes #175465.
Should fix rust-lang/rust#129713.