Skip to content

[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

Merged
merged 4 commits into from
Dec 20, 2024

Conversation

juliannagele
Copy link
Member

@juliannagele juliannagele commented Dec 18, 2024

When assumptions are present Terms.size() does not actually count the number of conditions collected from dominating branches; introduce a separate counter.

Fixes #120237

When assumptions are present Terms.size() does not actually count the
number of conditions collected from dominating branches; introduce a
separate counter.
@juliannagele juliannagele requested a review from fhahn December 18, 2024 15:44
@juliannagele juliannagele requested a review from nikic as a code owner December 18, 2024 15:44
@juliannagele juliannagele linked an issue Dec 18, 2024 that may be closed by this pull request
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Dec 18, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2024

@llvm/pr-subscribers-llvm-analysis

Author: Julian Nagele (juliannagele)

Changes

When assumptions are present Terms.size() does not actually count the number of conditions collected from dominating branches; introduce a separate counter.


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

1 Files Affected:

  • (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+2-1)
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

@fhahn
Copy link
Contributor

fhahn commented Dec 18, 2024

Could you add a test based on the reproducer from the issue?

@juliannagele
Copy link
Member Author

Could you add a test based on the reproducer from the issue?

Ah good point, added!

bb1:
br label %bb2

bb2:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bb2:
inner.header:

Copy link
Contributor

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

Copy link
Member Author

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:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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)
Copy link
Contributor

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.

Suggested change
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

Copy link
Member Author

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) :)

Comment on lines 16 to 17
call void @llvm.assume(i1 false)
call void @llvm.assume(i1 false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
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

Copy link
Member Author

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() {
Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks, added a comment

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LG

Copy link
Contributor

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?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, done

Copy link
Contributor

@fhahn fhahn left a comment

Choose a reason for hiding this comment

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

LGTM, thanks!

Could you add

Fixes #120237

to the PR description to make sure the issue will get automatically closed when this lands?

Please also check if #120615 is also fixed by this

@juliannagele juliannagele merged commit acfd26a into llvm:main Dec 20, 2024
8 checks passed
juliannagele added a commit to juliannagele/llvm-project that referenced this pull request Jan 6, 2025
…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)
juliannagele added a commit to swiftlang/llvm-project that referenced this pull request Jan 6, 2025
…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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis Includes value tracking, cost tables and constant folding
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Program terminated with signal: SIGKILL Compiler returned: 137
4 participants