Skip to content

Commit 68ffed1

Browse files
committed
[IVDescriptors] Fix bug in checkOrderedReduction
The Exit instruction passed in for checking if it's an ordered reduction need not be an FPAdd operation. We need to bail out at that point instead of assuming it is an FPAdd (and hence has two operands). See added testcase. It crashes without the patch because the Exit instruction is a phi with exactly one operand. This latent bug was exposed by 95346ba which added support for multi-exit loops for vectorization. Reviewed-By: kmclaughlin Differential Revision: https://reviews.llvm.org/D106843
1 parent 796b84d commit 68ffed1

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

llvm/lib/Analysis/IVDescriptors.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,16 +199,14 @@ static bool checkOrderedReduction(RecurKind Kind, Instruction *ExactFPMathInst,
199199
if (Kind != RecurKind::FAdd)
200200
return false;
201201

202-
bool IsOrdered =
203-
Exit->getOpcode() == Instruction::FAdd && Exit == ExactFPMathInst;
202+
if (Exit->getOpcode() != Instruction::FAdd || Exit != ExactFPMathInst)
203+
return false;
204204

205205
// The only pattern accepted is the one in which the reduction PHI
206206
// is used as one of the operands of the exit instruction
207207
auto *LHS = Exit->getOperand(0);
208208
auto *RHS = Exit->getOperand(1);
209-
IsOrdered &= ((LHS == Phi) || (RHS == Phi));
210-
211-
if (!IsOrdered)
209+
if (LHS != Phi && RHS != Phi)
212210
return false;
213211

214212
LLVM_DEBUG(dbgs() << "LV: Found an ordered reduction: Phi: " << *Phi
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; REQUIRES: asserts
2+
; RUN: opt < %s -loop-vectorize -S | FileCheck %s
3+
4+
; CHECK-LABEL: quux
5+
define void @quux() {
6+
bb:
7+
br label %bb4
8+
9+
bb1: ; preds = %bb4
10+
%tmp = phi double [ %tmp6, %bb4 ]
11+
br i1 undef, label %bb4, label %bb2
12+
13+
bb2: ; preds = %bb1
14+
%tmp3 = phi double [ %tmp, %bb1 ]
15+
ret void
16+
17+
bb4: ; preds = %bb1, %bb
18+
%tmp5 = phi double [ 1.300000e+01, %bb ], [ %tmp, %bb1 ]
19+
%tmp6 = fadd double %tmp5, 1.000000e+00
20+
br label %bb1
21+
}

0 commit comments

Comments
 (0)