-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[SCEV] Fix exit condition for recursive loop guard collection #120442
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
When assumptions are present Terms.size() does not actually count the number of conditions collected from dominating branches; introduce a separate counter.
@llvm/pr-subscribers-llvm-analysis Author: Julian Nagele (juliannagele) ChangesWhen assumptions are present Full diff: https://github.com/llvm/llvm-project/pull/120442.diff 1 Files Affected:
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index d55d09020fc147..6ba91c4417f724 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15753,6 +15753,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
// predecessors that can be found that have unique successors leading to the
// original header.
// TODO: share this logic with isLoopEntryGuardedByCond.
+ unsigned NumCollectedConditions = 0;
std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred, Block);
for (; Pair.first;
Pair = SE.getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
@@ -15767,7 +15768,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
// If we are recursively collecting guards stop after 2
// predecessors to limit compile-time impact for now.
- if (Depth > 0 && Terms.size() == 2)
+ if (Depth > 0 && ++NumCollectedConditions == 2)
break;
}
// Finally, if we stopped climbing the predecessor chain because
|
Could you add a test based on the reproducer from the issue? |
Ah good point, added! |
bb1: | ||
br label %bb2 | ||
|
||
bb2: |
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.
bb2: | |
inner.header: |
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.
Might be good to reorder and rename the blocks to make the sturcture easier to see in the test
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, simplified the test a bit more
bb5: | ||
br i1 false, label %bb6, label %bb5 | ||
|
||
bb6: |
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.
bb6: | |
outer.header: |
@@ -15767,7 +15768,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock( | |||
|
|||
// If we are recursively collecting guards stop after 2 | |||
// predecessors to limit compile-time impact for now. | |||
if (Depth > 0 && Terms.size() == 2) | |||
if (Depth > 0 && ++NumCollectedConditions == 2) |
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.
Ah I see, the issue is when we have cases where Terms
already has entries from assumes.
if (Depth > 0 && ++NumCollectedConditions == 2) | |
++ NumPredecessors; | |
if (Depth > 0 && NumVisitedPredecessors == 2) |
This aligns better with the comment immediately above. Might also be worth pulling out the increment of the condition to simplify for the reader
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.
Hm, I think it is actually counting the number of conditions, due to the early continue above, changed the comment instead (and pulled out the increment) :)
call void @llvm.assume(i1 false) | ||
call void @llvm.assume(i1 false) |
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.
call void @llvm.assume(i1 false) | |
call void @llvm.assume(i1 false) | |
call void @llvm.assume(i1 %c.1) | |
call void @llvm.assume(i1 %c.2) |
to avoid having immediate UB
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.
done
|
||
declare void @llvm.assume(i1) | ||
|
||
define void @pr120442() { |
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.
Can you add a brief comment explaining what the test checks?
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, added 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.
LG
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.
Can you please add this test to the same file as the other tests for this code in backedge-taken-count-guard-info-with-multiple-predecessors.ll?
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.
Sure, done
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.
…20442) When assumptions are present `Terms.size()` does not actually count the number of conditions collected from dominating branches; introduce a separate counter. Fixes llvm#120237 (cherry picked from commit acfd26a)
…9797) * [SCEV] Fix exit condition for recursive loop guard collection (llvm#120442) When assumptions are present `Terms.size()` does not actually count the number of conditions collected from dominating branches; introduce a separate counter. Fixes llvm#120237 (cherry picked from commit acfd26a) * [SCEV] Make sure starting block is marked as visited when recursively collecting loop guards. (llvm#120749) When `collectFromBlock` is called without a predecessor (in particular for loops that don't have a unique predecessor outside the loop) we never start climbing the predecessor chain, and thus don't mark the starting block as visited. Fixes llvm#120615. (cherry picked from commit f035351)
When assumptions are present
Terms.size()
does not actually count the number of conditions collected from dominating branches; introduce a separate counter.Fixes #120237