Skip to content

[SCEV] Do not attempt to collect loop guards for loops without predecessor. #123662

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 2 commits into from
Jan 22, 2025

Conversation

juliannagele
Copy link
Member

Attempting to collect loop guards for loops without a predecessor can lead to non-terminating recursion trying to construct a SCEV.

Fixes #122913.

@juliannagele juliannagele requested a review from fhahn January 20, 2025 18:57
@juliannagele juliannagele requested a review from nikic as a code owner January 20, 2025 18:57
@llvmbot llvmbot added the llvm:analysis Includes value tracking, cost tables and constant folding label Jan 20, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 20, 2025

@llvm/pr-subscribers-llvm-analysis

Author: Julian Nagele (juliannagele)

Changes

Attempting to collect loop guards for loops without a predecessor can lead to non-terminating recursion trying to construct a SCEV.

Fixes #122913.


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

2 Files Affected:

  • (modified) llvm/lib/Analysis/ScalarEvolution.cpp (+2)
  • (modified) llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll (+28)
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 7673c354817579..210c7cab965edb 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15328,6 +15328,8 @@ ScalarEvolution::LoopGuards::collect(const Loop *L, ScalarEvolution &SE) {
   BasicBlock *Header = L->getHeader();
   BasicBlock *Pred = L->getLoopPredecessor();
   LoopGuards Guards(SE);
+  if (!Pred)
+    return Guards;
   SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
   collectFromBlock(SE, Guards, Header, Pred, VisitedBlocks);
   return Guards;
diff --git a/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll b/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
index 46dccf454f21ac..6b100bfe5a8701 100644
--- a/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
+++ b/llvm/test/Analysis/ScalarEvolution/backedge-taken-count-guard-info-with-multiple-predecessors.ll
@@ -336,3 +336,31 @@ exit:
   ret void
 
 }
+
+; Checks correct traversal for loops without a unique predecessor
+; outside the loop.
+define void @pr122913() {
+; CHECK-LABEL: pr122913
+; CHECK-NEXT:  Determining loop execution counts for: @pr122913
+; CHECK-NEXT:  Loop %header: backedge-taken count is i1 false
+; CHECK-NEXT:  Loop %header: constant max backedge-taken count is i1 false
+; CHECK-NEXT:  Loop %header: symbolic max backedge-taken count is i1 false
+; CHECK-NEXT:  Loop %header: Trip multiple is 1
+entry:
+  br label %header
+
+bb:
+  br i1 1, label %exit, label %header
+
+header:
+  %0 = phi i32 [ %1, %body ], [ 0, %bb ], [ 0, %entry ]
+  br label %body
+
+body:
+  %1 = add i32 %0, 1
+  %2 = icmp ult i32 %1, 0
+  br i1 %2, label %header, label %exit
+
+exit:
+  ret void
+}

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

@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!


; Checks correct traversal for loops without a unique predecessor
; outside the loop.
define void @pr122913() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can the crash also be reproduced without one of the predecessors being unreachable?

Would be good to add a test case where bb is reachable if possible

Copy link
Collaborator

@danilaml danilaml Jan 22, 2025

Choose a reason for hiding this comment

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

define void @pr122913() {
entry:
  br i1 1, label %bb, label %header

bb:
  br i1 1, label %exit, label %header

header:
  %0 = phi i32 [ %1, %body ], [ 0, %bb ], [ 0, %entry ]
  br label %body

body:
  %1 = add i32 %0, 1
  %2 = icmp ult i32 %1, 0
  br i1 %2, label %header, label %exit

exit:
  ret void
}

Seems to crash in the same way: https://godbolt.org/z/8rdhM11vh

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! -- updated the test

@juliannagele juliannagele merged commit 137d706 into llvm:main Jan 22, 2025
8 checks passed
juliannagele added a commit to juliannagele/llvm-project that referenced this pull request Jan 23, 2025
…essor. (llvm#123662)

Attempting to collect loop guards for loops without a predecessor can
lead to non-terminating recursion trying to construct a SCEV.

Fixes llvm#122913.

(cherry picked from commit 137d706)
juliannagele added a commit to swiftlang/llvm-project that referenced this pull request Jan 29, 2025
…essor. (llvm#123662) (#9876)

Attempting to collect loop guards for loops without a predecessor can
lead to non-terminating recursion trying to construct a SCEV.

Fixes llvm#122913.

(cherry picked from commit 137d706)
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.

[SCEV] Another SEGV/stack overflow in LoopGuards
5 participants