Skip to content

Commit c491949

Browse files
committed
[LV] Pick correct BB as insert point when fixing PHI for FORs.
Currently we fail to pick the right insertion point when PreviousLastPart of a first-order-recurrence is a PHI node not in the LoopVectorBody. This can happen when PreviousLastPart is produce in a predicated block. In that case, we should pick the insertion point in the BB the PHI is in. Fixes PR44020. Reviewers: hsaito, fhahn, Ayal, dorit Reviewed By: Ayal Differential Revision: https://reviews.llvm.org/D71071
1 parent 4ec7bb4 commit c491949

File tree

2 files changed

+123
-10
lines changed

2 files changed

+123
-10
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,17 +3556,27 @@ void InnerLoopVectorizer::fixFirstOrderRecurrence(PHINode *Phi) {
35563556
// among all unrolled iterations, due to the order of their construction.
35573557
Value *PreviousLastPart = getOrCreateVectorValue(Previous, UF - 1);
35583558

3559-
// Set the insertion point after the previous value if it is an instruction.
3559+
// Find and set the insertion point after the previous value if it is an
3560+
// instruction.
3561+
BasicBlock::iterator InsertPt;
35603562
// Note that the previous value may have been constant-folded so it is not
3561-
// guaranteed to be an instruction in the vector loop. Also, if the previous
3562-
// value is a phi node, we should insert after all the phi nodes to avoid
3563-
// breaking basic block verification.
3564-
if (LI->getLoopFor(LoopVectorBody)->isLoopInvariant(PreviousLastPart) ||
3565-
isa<PHINode>(PreviousLastPart))
3566-
Builder.SetInsertPoint(&*LoopVectorBody->getFirstInsertionPt());
3567-
else
3568-
Builder.SetInsertPoint(
3569-
&*++BasicBlock::iterator(cast<Instruction>(PreviousLastPart)));
3563+
// guaranteed to be an instruction in the vector loop.
3564+
// FIXME: Loop invariant values do not form recurrences. We should deal with
3565+
// them earlier.
3566+
if (LI->getLoopFor(LoopVectorBody)->isLoopInvariant(PreviousLastPart))
3567+
InsertPt = LoopVectorBody->getFirstInsertionPt();
3568+
else {
3569+
Instruction *PreviousInst = cast<Instruction>(PreviousLastPart);
3570+
if (isa<PHINode>(PreviousLastPart))
3571+
// If the previous value is a phi node, we should insert after all the phi
3572+
// nodes in the block containing the PHI to avoid breaking basic block
3573+
// verification. Note that the basic block may be different to
3574+
// LoopVectorBody, in case we predicate the loop.
3575+
InsertPt = PreviousInst->getParent()->getFirstInsertionPt();
3576+
else
3577+
InsertPt = ++PreviousInst->getIterator();
3578+
}
3579+
Builder.SetInsertPoint(&*InsertPt);
35703580

35713581
// We will construct a vector for the recurrence by combining the values for
35723582
// the current and previous iterations. This is the required shuffle mask.
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -loop-vectorize -mtriple=s390x-ibm-linux -mcpu=z13 -force-vector-width=2 -S %s | FileCheck %s
3+
4+
; Test case from PR44020.
5+
6+
; In func_21, %rec forms a first order recurrence and we predicate to avoid
7+
; scalar iteration overhead for a low trip count loop. Make sure we pick
8+
; the correct insertion point when fixing first order recurrences.
9+
10+
@A = external dso_local global [5 x i32], align 4
11+
@B = external dso_local global [5 x i32], align 4
12+
13+
define void @func_21() {
14+
; CHECK-LABEL: @func_21(
15+
; CHECK-NEXT: entry:
16+
; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
17+
; CHECK: vector.ph:
18+
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
19+
; CHECK: vector.body:
20+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[PRED_STORE_CONTINUE4:%.*]] ]
21+
; CHECK-NEXT: [[VECTOR_RECUR:%.*]] = phi <2 x i32> [ <i32 undef, i32 0>, [[VECTOR_PH]] ], [ [[TMP12:%.*]], [[PRED_STORE_CONTINUE4]] ]
22+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i64> undef, i64 [[INDEX]], i32 0
23+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i64> [[BROADCAST_SPLATINSERT]], <2 x i64> undef, <2 x i32> zeroinitializer
24+
; CHECK-NEXT: [[INDUCTION:%.*]] = add <2 x i64> [[BROADCAST_SPLAT]], <i64 0, i64 1>
25+
; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[INDEX]], 0
26+
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[INDEX]], 1
27+
; CHECK-NEXT: [[TMP2:%.*]] = icmp ule <2 x i64> [[INDUCTION]], <i64 4, i64 4>
28+
; CHECK-NEXT: [[TMP3:%.*]] = extractelement <2 x i1> [[TMP2]], i32 0
29+
; CHECK-NEXT: br i1 [[TMP3]], label [[PRED_LOAD_IF:%.*]], label [[PRED_LOAD_CONTINUE:%.*]]
30+
; CHECK: pred.load.if:
31+
; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds [5 x i32], [5 x i32]* @A, i64 0, i64 [[TMP0]]
32+
; CHECK-NEXT: [[TMP5:%.*]] = load i32, i32* [[TMP4]], align 4
33+
; CHECK-NEXT: [[TMP6:%.*]] = insertelement <2 x i32> undef, i32 [[TMP5]], i32 0
34+
; CHECK-NEXT: br label [[PRED_LOAD_CONTINUE]]
35+
; CHECK: pred.load.continue:
36+
; CHECK-NEXT: [[TMP7:%.*]] = phi <2 x i32> [ undef, [[VECTOR_BODY]] ], [ [[TMP6]], [[PRED_LOAD_IF]] ]
37+
; CHECK-NEXT: [[TMP8:%.*]] = extractelement <2 x i1> [[TMP2]], i32 1
38+
; CHECK-NEXT: br i1 [[TMP8]], label [[PRED_LOAD_IF1:%.*]], label [[PRED_LOAD_CONTINUE2:%.*]]
39+
; CHECK: pred.load.if1:
40+
; CHECK-NEXT: [[TMP9:%.*]] = getelementptr inbounds [5 x i32], [5 x i32]* @A, i64 0, i64 [[TMP1]]
41+
; CHECK-NEXT: [[TMP10:%.*]] = load i32, i32* [[TMP9]], align 4
42+
; CHECK-NEXT: [[TMP11:%.*]] = insertelement <2 x i32> [[TMP7]], i32 [[TMP10]], i32 1
43+
; CHECK-NEXT: br label [[PRED_LOAD_CONTINUE2]]
44+
; CHECK: pred.load.continue2:
45+
; CHECK-NEXT: [[TMP12]] = phi <2 x i32> [ [[TMP7]], [[PRED_LOAD_CONTINUE]] ], [ [[TMP11]], [[PRED_LOAD_IF1]] ]
46+
; CHECK-NEXT: [[TMP13:%.*]] = shufflevector <2 x i32> [[VECTOR_RECUR]], <2 x i32> [[TMP12]], <2 x i32> <i32 1, i32 2>
47+
; CHECK-NEXT: [[TMP14:%.*]] = extractelement <2 x i1> [[TMP2]], i32 0
48+
; CHECK-NEXT: br i1 [[TMP14]], label [[PRED_STORE_IF:%.*]], label [[PRED_STORE_CONTINUE:%.*]]
49+
; CHECK: pred.store.if:
50+
; CHECK-NEXT: [[TMP15:%.*]] = getelementptr inbounds [5 x i32], [5 x i32]* @B, i64 0, i64 [[TMP0]]
51+
; CHECK-NEXT: [[TMP16:%.*]] = extractelement <2 x i32> [[TMP13]], i32 0
52+
; CHECK-NEXT: store i32 [[TMP16]], i32* [[TMP15]], align 4
53+
; CHECK-NEXT: br label [[PRED_STORE_CONTINUE]]
54+
; CHECK: pred.store.continue:
55+
; CHECK-NEXT: [[TMP17:%.*]] = extractelement <2 x i1> [[TMP2]], i32 1
56+
; CHECK-NEXT: br i1 [[TMP17]], label [[PRED_STORE_IF3:%.*]], label [[PRED_STORE_CONTINUE4]]
57+
; CHECK: pred.store.if3:
58+
; CHECK-NEXT: [[TMP18:%.*]] = getelementptr inbounds [5 x i32], [5 x i32]* @B, i64 0, i64 [[TMP1]]
59+
; CHECK-NEXT: [[TMP19:%.*]] = extractelement <2 x i32> [[TMP13]], i32 1
60+
; CHECK-NEXT: store i32 [[TMP19]], i32* [[TMP18]], align 4
61+
; CHECK-NEXT: br label [[PRED_STORE_CONTINUE4]]
62+
; CHECK: pred.store.continue4:
63+
; CHECK-NEXT: [[INDEX_NEXT]] = add i64 [[INDEX]], 2
64+
; CHECK-NEXT: [[TMP20:%.*]] = icmp eq i64 [[INDEX_NEXT]], 6
65+
; CHECK-NEXT: br i1 [[TMP20]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop !0
66+
; CHECK: middle.block:
67+
; CHECK-NEXT: [[VECTOR_RECUR_EXTRACT:%.*]] = extractelement <2 x i32> [[TMP12]], i32 1
68+
; CHECK-NEXT: [[VECTOR_RECUR_EXTRACT_FOR_PHI:%.*]] = extractelement <2 x i32> [[TMP12]], i32 0
69+
; CHECK-NEXT: br i1 true, label [[EXIT:%.*]], label [[SCALAR_PH]]
70+
; CHECK: scalar.ph:
71+
; CHECK-NEXT: [[SCALAR_RECUR_INIT:%.*]] = phi i32 [ [[VECTOR_RECUR_EXTRACT]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
72+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ 6, [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY]] ]
73+
; CHECK-NEXT: br label [[LOOP:%.*]]
74+
; CHECK: loop:
75+
; CHECK-NEXT: [[SCALAR_RECUR:%.*]] = phi i32 [ [[SCALAR_RECUR_INIT]], [[SCALAR_PH]] ], [ [[LV:%.*]], [[LOOP]] ]
76+
; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[INDVARS_IV_NEXT:%.*]], [[LOOP]] ]
77+
; CHECK-NEXT: [[A_PTR:%.*]] = getelementptr inbounds [5 x i32], [5 x i32]* @A, i64 0, i64 [[INDVARS_IV]]
78+
; CHECK-NEXT: [[LV]] = load i32, i32* [[A_PTR]], align 4
79+
; CHECK-NEXT: [[B_PTR:%.*]] = getelementptr inbounds [5 x i32], [5 x i32]* @B, i64 0, i64 [[INDVARS_IV]]
80+
; CHECK-NEXT: store i32 [[SCALAR_RECUR]], i32* [[B_PTR]], align 4
81+
; CHECK-NEXT: [[INDVARS_IV_NEXT]] = add nuw nsw i64 [[INDVARS_IV]], 1
82+
; CHECK-NEXT: [[EXITCOND:%.*]] = icmp eq i64 [[INDVARS_IV_NEXT]], 5
83+
; CHECK-NEXT: br i1 [[EXITCOND]], label [[EXIT]], label [[LOOP]], !llvm.loop !2
84+
; CHECK: exit:
85+
; CHECK-NEXT: ret void
86+
;
87+
entry:
88+
br label %loop
89+
90+
loop: ; preds = %loop, %entry
91+
%rec = phi i32 [ 0, %entry], [ %lv, %loop ]
92+
%indvars.iv = phi i64 [ 0, %entry], [ %indvars.iv.next, %loop ]
93+
%A.ptr= getelementptr inbounds [5 x i32], [5 x i32]* @A, i64 0, i64 %indvars.iv
94+
%lv = load i32, i32* %A.ptr, align 4
95+
%B.ptr = getelementptr inbounds [5 x i32], [5 x i32]* @B, i64 0, i64 %indvars.iv
96+
store i32 %rec, i32* %B.ptr, align 4
97+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
98+
%exitcond = icmp eq i64 %indvars.iv.next, 5
99+
br i1 %exitcond, label %exit, label %loop
100+
101+
exit: ; preds = %loop
102+
ret void
103+
}

0 commit comments

Comments
 (0)