Skip to content

Commit e7b19aa

Browse files
committed
[IVDesc] Check loop-preheader for loop-legality when pass-remarks enabled
When `-pass-remarks=loop-vectorize` is specified, the subsequent logic is executed to display detailed debug messages even if no PreHeader exists in the loop. Therefore, an assert occurs when the `getLoopPreHeader()` function is called. This commit resolves that issue. Fixed: #165377
1 parent 4b6c608 commit e7b19aa

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,9 @@ bool RecurrenceDescriptor::AddReductionVar(
222222

223223
// Obtain the reduction start value from the value that comes from the loop
224224
// preheader.
225-
Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
225+
Value *RdxStart;
226+
if (TheLoop->getLoopPreheader())
227+
RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
226228

227229
// ExitInstruction is the single value which is used outside the loop.
228230
// We only allow for a single reduction value to be used outside the loop.
@@ -587,6 +589,9 @@ bool RecurrenceDescriptor::AddReductionVar(
587589
collectCastInstrs(TheLoop, ExitInstruction, RecurrenceType, CastInsts,
588590
MinWidthCastToRecurrenceType);
589591

592+
if (!RdxStart)
593+
return false;
594+
590595
// We found a reduction var if we have reached the original phi node and we
591596
// only have a single instruction with out-of-loop users.
592597

@@ -1536,6 +1541,11 @@ bool InductionDescriptor::isInductionPHI(
15361541
return false;
15371542
}
15381543

1544+
if (!AR->getLoop()->getLoopPreheader()) {
1545+
LLVM_DEBUG(dbgs() << "LV: Loop doesn't have a legal pre-header.\n");
1546+
return false;
1547+
}
1548+
15391549
// This function assumes that InductionPhi is called only on Phi nodes
15401550
// present inside loop headers. Check for the same, and throw an assert if
15411551
// the current Phi is not present inside the loop header.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
; RUN: opt < %s -passes=loop-vectorize -pass-remarks=loop-vectorize -debug -disable-output 2>&1 | FileCheck %s
2+
; REQUIRES: asserts
3+
4+
; Make sure LV legal bails out when the loop doesn't have a legal pre-header.
5+
; CHECK-LABEL: 'not_exist_preheader'
6+
; CHECK: LV: Not vectorizing: Loop doesn't have a legal pre-header.
7+
define void @not_exist_preheader(ptr %currMB, i1 %arg, ptr %arg2) nounwind uwtable {
8+
9+
start.exit:
10+
indirectbr ptr %arg2, [label %0, label %for.bodyprime]
11+
12+
0:
13+
unreachable
14+
15+
for.bodyprime:
16+
%i.057375 = phi i32 [0, %start.exit], [%1, %for.bodyprime]
17+
%arrayidx8prime = getelementptr inbounds i32, ptr %currMB, i32 %i.057375
18+
store i32 0, ptr %arrayidx8prime, align 4
19+
%1 = add i32 %i.057375, 1
20+
%cmp5prime = icmp slt i32 %1, 4
21+
br i1 %cmp5prime, label %for.bodyprime, label %exit
22+
23+
exit:
24+
ret void
25+
}

0 commit comments

Comments
 (0)