Skip to content

Commit e2e6062

Browse files
committed
[LoopVectorize] Allow Early-Exit Loop Vectorization with EVL
This patch enables vectorization of early-exit loops when tailfolding with EVL. Although these early-exit loops do not require a scalar epilogue, CM_ScalarEpilogueAllowed skips setTailFoldingStyles in the current path, preventiing tryAddExplicitVectorLength() from being invoked. To ensure early-exit loops are vectorized with EVL transform, both `-force-tail-folding-style=data-with-evl` and `-prefer-predicate-over-epilogue=predicate-dont-vectorize` must be used. This patch updates the check on bottom-test loop and ensures the tailfolding style is applied.
1 parent 7ae7585 commit e2e6062

File tree

2 files changed

+117
-6
lines changed

2 files changed

+117
-6
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4038,10 +4038,12 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
40384038
}
40394039

40404040
// The only loops we can vectorize without a scalar epilogue, are loops with
4041-
// a bottom-test and a single exiting block. We'd have to handle the fact
4042-
// that not every instruction executes on the last iteration. This will
4043-
// require a lane mask which varies through the vector loop body. (TODO)
4044-
if (TheLoop->getExitingBlock() != TheLoop->getLoopLatch()) {
4041+
// a bottom-test and a single exiting block or those with early exits. We'd
4042+
// have to handle the fact that not every instruction executes on the last
4043+
// iteration. This will require a lane mask which varies through the vector
4044+
// loop body. (TODO)
4045+
if ((TheLoop->getExitingBlock() != TheLoop->getLoopLatch()) &&
4046+
!Legal->hasUncountableEarlyExit()) {
40454047
// If there was a tail-folding hint/switch, but we can't fold the tail by
40464048
// masking, fallback to a vectorization with a scalar epilogue.
40474049
if (ScalarEpilogueStatus == CM_ScalarEpilogueNotNeededUsePredicate) {
@@ -4092,8 +4094,9 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
40924094
// uncountable exits whilst also ensuring the symbolic maximum and known
40934095
// back-edge taken count remain identical for loops with countable exits.
40944096
const SCEV *BackedgeTakenCount = PSE.getSymbolicMaxBackedgeTakenCount();
4095-
assert(BackedgeTakenCount == PSE.getBackedgeTakenCount() &&
4096-
"Invalid loop count");
4097+
assert(Legal->hasUncountableEarlyExit() ||
4098+
(BackedgeTakenCount == PSE.getBackedgeTakenCount()) &&
4099+
"Invalid loop count");
40974100
const SCEV *ExitCount = SE->getAddExpr(
40984101
BackedgeTakenCount, SE->getOne(BackedgeTakenCount->getType()));
40994102
const SCEV *Rem = SE->getURemExpr(
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt -passes=loop-vectorize -force-tail-folding-style=data-with-evl -prefer-predicate-over-epilogue=predicate-dont-vectorize \
3+
; RUN: -mtriple=riscv64 -mattr=+v -S -enable-early-exit-vectorization %s | FileCheck %s
4+
5+
; REQUIRES: asserts
6+
7+
declare void @init(ptr)
8+
9+
define i64 @multi_exiting_to_different_exits_live_in_exit_values() {
10+
; CHECK-LABEL: define i64 @multi_exiting_to_different_exits_live_in_exit_values(
11+
; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
12+
; CHECK-NEXT: [[ENTRY:.*]]:
13+
; CHECK-NEXT: [[SRC:%.*]] = alloca [128 x i32], align 4
14+
; CHECK-NEXT: call void @init(ptr [[SRC]])
15+
; CHECK-NEXT: br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
16+
; CHECK: [[VECTOR_PH]]:
17+
; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
18+
; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[TMP0]], 4
19+
; CHECK-NEXT: [[TMP2:%.*]] = sub i64 [[TMP1]], 1
20+
; CHECK-NEXT: [[N_RND_UP:%.*]] = add i64 128, [[TMP2]]
21+
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[N_RND_UP]], [[TMP1]]
22+
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N_RND_UP]], [[N_MOD_VF]]
23+
; CHECK-NEXT: [[TMP3:%.*]] = call i64 @llvm.vscale.i64()
24+
; CHECK-NEXT: [[TMP4:%.*]] = mul i64 [[TMP3]], 4
25+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
26+
; CHECK: [[VECTOR_BODY]]:
27+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
28+
; CHECK-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], %[[VECTOR_BODY]] ]
29+
; CHECK-NEXT: [[AVL:%.*]] = sub i64 128, [[EVL_BASED_IV]]
30+
; CHECK-NEXT: [[TMP5:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 4, i1 true)
31+
; CHECK-NEXT: [[IV:%.*]] = add i64 [[EVL_BASED_IV]], 0
32+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i64> poison, i64 [[EVL_BASED_IV]], i64 0
33+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i64> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i64> poison, <vscale x 4 x i32> zeroinitializer
34+
; CHECK-NEXT: [[TMP7:%.*]] = call <vscale x 4 x i64> @llvm.stepvector.nxv4i64()
35+
; CHECK-NEXT: [[TMP8:%.*]] = add <vscale x 4 x i64> zeroinitializer, [[TMP7]]
36+
; CHECK-NEXT: [[VEC_IV:%.*]] = add <vscale x 4 x i64> [[BROADCAST_SPLAT]], [[TMP8]]
37+
; CHECK-NEXT: [[TMP9:%.*]] = icmp ule <vscale x 4 x i64> [[VEC_IV]], splat (i64 127)
38+
; CHECK-NEXT: [[GEP_SRC:%.*]] = getelementptr inbounds i32, ptr [[SRC]], i64 [[IV]]
39+
; CHECK-NEXT: [[TMP11:%.*]] = getelementptr inbounds i32, ptr [[GEP_SRC]], i32 0
40+
; CHECK-NEXT: [[VP_OP_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.vp.load.nxv4i32.p0(ptr align 4 [[TMP11]], <vscale x 4 x i1> splat (i1 true), i32 [[TMP5]])
41+
; CHECK-NEXT: [[TMP12:%.*]] = icmp eq <vscale x 4 x i32> [[VP_OP_LOAD]], splat (i32 10)
42+
; CHECK-NEXT: [[TMP13:%.*]] = xor <vscale x 4 x i1> [[TMP12]], splat (i1 true)
43+
; CHECK-NEXT: [[TMP14:%.*]] = select <vscale x 4 x i1> [[TMP9]], <vscale x 4 x i1> [[TMP13]], <vscale x 4 x i1> zeroinitializer
44+
; CHECK-NEXT: [[TMP15:%.*]] = zext i32 [[TMP5]] to i64
45+
; CHECK-NEXT: [[INDEX_EVL_NEXT]] = add nuw i64 [[TMP15]], [[EVL_BASED_IV]]
46+
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP4]]
47+
; CHECK-NEXT: [[TMP16:%.*]] = xor <vscale x 4 x i1> [[TMP14]], splat (i1 true)
48+
; CHECK-NEXT: [[TMP17:%.*]] = call i1 @llvm.vector.reduce.or.nxv4i1(<vscale x 4 x i1> [[TMP16]])
49+
; CHECK-NEXT: [[TMP18:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
50+
; CHECK-NEXT: [[TMP19:%.*]] = or i1 [[TMP17]], [[TMP18]]
51+
; CHECK-NEXT: br i1 [[TMP19]], label %[[MIDDLE_SPLIT:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
52+
; CHECK: [[MIDDLE_SPLIT]]:
53+
; CHECK-NEXT: br i1 [[TMP17]], label %[[VECTOR_EARLY_EXIT:.*]], label %[[MIDDLE_BLOCK:.*]]
54+
; CHECK: [[MIDDLE_BLOCK]]:
55+
; CHECK-NEXT: br i1 true, label %[[E2:.*]], label %[[SCALAR_PH]]
56+
; CHECK: [[VECTOR_EARLY_EXIT]]:
57+
; CHECK-NEXT: br label %[[E1:.*]]
58+
; CHECK: [[SCALAR_PH]]:
59+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[N_VEC]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
60+
; CHECK-NEXT: br label %[[LOOP_HEADER:.*]]
61+
; CHECK: [[LOOP_HEADER]]:
62+
; CHECK-NEXT: [[IV1:%.*]] = phi i64 [ [[INC:%.*]], %[[LOOP_LATCH:.*]] ], [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ]
63+
; CHECK-NEXT: [[GEP_SRC1:%.*]] = getelementptr inbounds i32, ptr [[SRC]], i64 [[IV1]]
64+
; CHECK-NEXT: [[L:%.*]] = load i32, ptr [[GEP_SRC1]], align 4
65+
; CHECK-NEXT: [[C_1:%.*]] = icmp eq i32 [[L]], 10
66+
; CHECK-NEXT: br i1 [[C_1]], label %[[E1]], label %[[LOOP_LATCH]]
67+
; CHECK: [[LOOP_LATCH]]:
68+
; CHECK-NEXT: [[INC]] = add nuw i64 [[IV1]], 1
69+
; CHECK-NEXT: [[C_2:%.*]] = icmp eq i64 [[INC]], 128
70+
; CHECK-NEXT: br i1 [[C_2]], label %[[E2]], label %[[LOOP_HEADER]], !llvm.loop [[LOOP3:![0-9]+]]
71+
; CHECK: [[E1]]:
72+
; CHECK-NEXT: [[P1:%.*]] = phi i64 [ 0, %[[LOOP_HEADER]] ], [ 0, %[[VECTOR_EARLY_EXIT]] ]
73+
; CHECK-NEXT: ret i64 [[P1]]
74+
; CHECK: [[E2]]:
75+
; CHECK-NEXT: [[P2:%.*]] = phi i64 [ 1, %[[LOOP_LATCH]] ], [ 1, %[[MIDDLE_BLOCK]] ]
76+
; CHECK-NEXT: ret i64 [[P2]]
77+
;
78+
entry:
79+
%src = alloca [128 x i32]
80+
call void @init(ptr %src)
81+
br label %loop.header
82+
83+
loop.header:
84+
%iv = phi i64 [ %inc, %loop.latch ], [ 0, %entry ]
85+
%gep.src = getelementptr inbounds i32, ptr %src, i64 %iv
86+
%l = load i32, ptr %gep.src
87+
%c.1 = icmp eq i32 %l, 10
88+
br i1 %c.1, label %e1, label %loop.latch
89+
90+
loop.latch:
91+
%inc = add nuw i64 %iv, 1
92+
%c.2 = icmp eq i64 %inc, 128
93+
br i1 %c.2, label %e2, label %loop.header
94+
95+
e1:
96+
%p1 = phi i64 [ 0, %loop.header ]
97+
ret i64 %p1
98+
99+
e2:
100+
%p2 = phi i64 [ 1, %loop.latch ]
101+
ret i64 %p2
102+
}
103+
;.
104+
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
105+
; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
106+
; CHECK: [[META2]] = !{!"llvm.loop.unroll.runtime.disable"}
107+
; CHECK: [[LOOP3]] = distinct !{[[LOOP3]], [[META2]], [[META1]]}
108+
;.

0 commit comments

Comments
 (0)