Skip to content

Commit 8d6e867

Browse files
[LSR][term-fold] Ensure the simple recurrence is from the current loop (#83085)
If the phi node found by matchSimpleRecurrence is not from the current loop, then isAlmostDeadIV panics. With this patch we bail out early. Signed-off-by: Patrick O'Neill <[email protected]> --------- Signed-off-by: Patrick O'Neill <[email protected]>
1 parent bdfebc3 commit 8d6e867

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -6808,6 +6808,10 @@ canFoldTermCondOfLoop(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
68086808
if (!matchSimpleRecurrence(LHS, ToFold, ToFoldStart, ToFoldStep))
68096809
return std::nullopt;
68106810

6811+
// Ensure the simple recurrence is a part of the current loop.
6812+
if (ToFold->getParent() != L->getHeader())
6813+
return std::nullopt;
6814+
68116815
// If that IV isn't dead after we rewrite the exit condition in terms of
68126816
// another IV, there's no point in doing the transform.
68136817
if (!isAlmostDeadIV(ToFold, LoopLatch, TermCond))

llvm/lib/Transforms/Utils/LoopUtils.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ llvm::collectChildrenInLoop(DomTreeNode *N, const Loop *CurLoop) {
468468

469469
bool llvm::isAlmostDeadIV(PHINode *PN, BasicBlock *LatchBlock, Value *Cond) {
470470
int LatchIdx = PN->getBasicBlockIndex(LatchBlock);
471+
assert(LatchIdx != -1 && "LatchBlock is not a case in this PHINode");
471472
Value *IncV = PN->getIncomingValue(LatchIdx);
472473

473474
for (User *U : PN->users())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -loop-reduce -S -lsr-term-fold | FileCheck %s
3+
4+
; This test used to crash due to matchSimpleRecurrence matching the simple
5+
; recurrence in pn-loop when evaluating unrelated-loop. Since unrelated-loop
6+
; cannot jump to pn-node isAlmostDeadIV panics.
7+
define void @phi_node_different_bb() {
8+
; CHECK-LABEL: @phi_node_different_bb(
9+
; CHECK-NEXT: br label [[PN_LOOP:%.*]]
10+
; CHECK: pn-loop:
11+
; CHECK-NEXT: [[TMP1:%.*]] = phi i32 [ 1, [[TMP0:%.*]] ], [ [[TMP2:%.*]], [[PN_LOOP]] ]
12+
; CHECK-NEXT: [[TMP2]] = add i32 [[TMP1]], 1
13+
; CHECK-NEXT: [[TMP3:%.*]] = icmp ugt i32 [[TMP2]], 1
14+
; CHECK-NEXT: br i1 [[TMP3]], label [[PN_LOOP]], label [[UNRELATED_LOOP_PREHEADER:%.*]]
15+
; CHECK: unrelated-loop.preheader:
16+
; CHECK-NEXT: br label [[UNRELATED_LOOP:%.*]]
17+
; CHECK: unrelated-loop:
18+
; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i32 [[TMP2]], 0
19+
; CHECK-NEXT: br i1 [[TMP4]], label [[END:%.*]], label [[UNRELATED_LOOP]]
20+
; CHECK: end:
21+
; CHECK-NEXT: ret void
22+
;
23+
br label %pn-loop
24+
25+
pn-loop: ; preds = %pn-loop, %0
26+
%1 = phi i32 [ 1, %0 ], [ %2, %pn-loop ]
27+
%2 = add i32 %1, 1
28+
%3 = icmp ugt i32 %2, 1
29+
br i1 %3, label %pn-loop, label %unrelated-loop.preheader
30+
31+
unrelated-loop.preheader: ; preds = %pn-loop
32+
br label %unrelated-loop
33+
34+
unrelated-loop: ; preds = %unrelated-loop, %unrelated-loop.preheader
35+
%4 = icmp eq i32 %2, 0
36+
br i1 %4, label %end, label %unrelated-loop
37+
38+
end: ; preds = %unrelated-loop
39+
ret void
40+
}

0 commit comments

Comments
 (0)