Skip to content

Commit 4bd4bf3

Browse files
committed
Fix for single-iteration EVL loop
1 parent b1ec544 commit 4bd4bf3

File tree

2 files changed

+88
-9
lines changed

2 files changed

+88
-9
lines changed

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,8 +2385,19 @@ void VPlanTransforms::simplifyEVLIVs(VPlan &Plan) {
23852385
EVLPhi->replaceAllUsesWith(ScalarR);
23862386
EVLPhi->eraseFromParent();
23872387

2388-
// Find the latch-exiting block and convert to variable-length stepping.
2389-
// Before: (branch-on-count CanonicalIVInc, VectorTripCount)
2388+
// Replace CanonicalIVInc with EVL-PHI increment
2389+
VPRecipeBase *CanonicalIV = &*Entry->begin();
2390+
assert(dyn_cast<VPPhi>(CanonicalIV) && "Unexpected canoincal iv");
2391+
VPValue *Backedge = CanonicalIV->getOperand(1);
2392+
Backedge->replaceAllUsesWith(EVLIncrement);
2393+
2394+
// Remove unused phi
2395+
VPRecipeBase *CanonicalIVIncrement = Backedge->getDefiningRecipe();
2396+
CanonicalIVIncrement->eraseFromParent();
2397+
CanonicalIV->eraseFromParent();
2398+
2399+
// Find the latch-exiting block and replace use of VectorTripCount
2400+
// Before: (branch-on-count EVLIVInc, VectorTripCount)
23902401
// After: (branch-on-count EVLIVInc, TripCount)
23912402
auto Range =
23922403
VPBlockUtils::blocksOnly<VPBasicBlock>(vp_depth_first_shallow(Entry));
@@ -2395,20 +2406,20 @@ void VPlanTransforms::simplifyEVLIVs(VPlan &Plan) {
23952406
[&Entry](VPBlockBase *Succ) { return Succ == Entry; });
23962407
});
23972408
assert((It != Range.end()) && "LatchExiting is not found");
2409+
23982410
VPBasicBlock *LatchExiting = *It;
2411+
23992412
auto *LatchExitingBr = cast<VPInstruction>(LatchExiting->getTerminator());
2400-
VPValue *ScalarIVInc;
2413+
2414+
// Skip single-iteration loop region
2415+
if (match(LatchExitingBr, m_BranchOnCond(m_True())))
2416+
return;
24012417
assert(LatchExitingBr &&
24022418
match(LatchExitingBr,
2403-
m_BranchOnCount(m_VPValue(ScalarIVInc),
2419+
m_BranchOnCount(m_VPValue(EVLIncrement),
24042420
m_Specific(&Plan.getVectorTripCount()))) &&
24052421
"Unexpected terminator in EVL loop");
24062422
LatchExitingBr->setOperand(1, Plan.getTripCount());
2407-
ScalarIVInc->replaceAllUsesWith(EVLIncrement);
2408-
VPRecipeBase *IVIncR = ScalarIVInc->getDefiningRecipe();
2409-
VPRecipeBase *ScalarIV = IVIncR->getOperand(0)->getDefiningRecipe();
2410-
IVIncR->eraseFromParent();
2411-
ScalarIV->eraseFromParent();
24122423
}
24132424

24142425
void VPlanTransforms::dropPoisonGeneratingRecipes(
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt %s -S -mtriple riscv64 -passes=loop-vectorize --prefer-predicate-over-epilogue=predicate-dont-vectorize -force-tail-folding-style=data-with-evl -riscv-v-min-trip-count=0 -force-target-instruction-cost=1 -mattr=+v | FileCheck %s
3+
4+
; Check canonical-iv is removed in single-iteration loop
5+
define void @foo(ptr %arg) #0 {
6+
; CHECK-LABEL: define void @foo(
7+
; CHECK-SAME: ptr [[ARG:%.*]]) #[[ATTR0:[0-9]+]] {
8+
; CHECK-NEXT: [[ENTRY:.*]]:
9+
; CHECK-NEXT: br i1 false, label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
10+
; CHECK: [[VECTOR_PH]]:
11+
; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
12+
; CHECK-NEXT: [[TMP1:%.*]] = mul nuw i64 [[TMP0]], 2
13+
; CHECK-NEXT: [[TMP2:%.*]] = sub i64 [[TMP1]], 1
14+
; CHECK-NEXT: [[N_RND_UP:%.*]] = add i64 3, [[TMP2]]
15+
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[N_RND_UP]], [[TMP1]]
16+
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N_RND_UP]], [[N_MOD_VF]]
17+
; CHECK-NEXT: [[TMP3:%.*]] = call i64 @llvm.vscale.i64()
18+
; CHECK-NEXT: [[TMP4:%.*]] = mul nuw i64 [[TMP3]], 2
19+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
20+
; CHECK: [[VECTOR_BODY]]:
21+
; CHECK-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], %[[VECTOR_BODY]] ]
22+
; CHECK-NEXT: [[AVL:%.*]] = sub i64 3, [[EVL_BASED_IV]]
23+
; CHECK-NEXT: [[TMP5:%.*]] = call i32 @llvm.experimental.get.vector.length.i64(i64 [[AVL]], i32 2, i1 true)
24+
; CHECK-NEXT: [[TMP6:%.*]] = getelementptr [3 x i64], ptr [[ARG]], i64 0, i64 [[EVL_BASED_IV]]
25+
; CHECK-NEXT: [[TMP7:%.*]] = getelementptr i64, ptr [[TMP6]], i32 0
26+
; CHECK-NEXT: call void @llvm.vp.store.nxv2i64.p0(<vscale x 2 x i64> zeroinitializer, ptr align 8 [[TMP7]], <vscale x 2 x i1> splat (i1 true), i32 [[TMP5]])
27+
; CHECK-NEXT: [[TMP8:%.*]] = zext i32 [[TMP5]] to i64
28+
; CHECK-NEXT: [[INDEX_EVL_NEXT]] = add nuw i64 [[TMP8]], [[EVL_BASED_IV]]
29+
; CHECK-NEXT: br i1 true, label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
30+
; CHECK: [[MIDDLE_BLOCK]]:
31+
; CHECK-NEXT: br label %[[EXIT:.*]]
32+
; CHECK: [[SCALAR_PH]]:
33+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ 0, %[[ENTRY]] ]
34+
; CHECK-NEXT: br label %[[LOOP:.*]]
35+
; CHECK: [[LOOP]]:
36+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
37+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr [3 x i64], ptr [[ARG]], i64 0, i64 [[IV]]
38+
; CHECK-NEXT: store i64 0, ptr [[GEP]], align 8
39+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 1
40+
; CHECK-NEXT: [[COND:%.*]] = icmp eq i64 [[IV_NEXT]], 3
41+
; CHECK-NEXT: br i1 [[COND]], label %[[EXIT]], label %[[LOOP]], !llvm.loop [[LOOP4:![0-9]+]]
42+
; CHECK: [[EXIT]]:
43+
; CHECK-NEXT: ret void
44+
;
45+
entry:
46+
br label %loop
47+
48+
loop:
49+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
50+
%gep = getelementptr [3 x i64], ptr %arg, i64 0, i64 %iv
51+
store i64 0, ptr %gep, align 8
52+
%iv.next = add i64 %iv, 1
53+
%cond = icmp eq i64 %iv.next, 3
54+
br i1 %cond, label %exit, label %loop
55+
56+
exit:
57+
ret void
58+
}
59+
60+
attributes #0 = { vscale_range(2,2) }
61+
62+
;.
63+
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]], [[META3:![0-9]+]]}
64+
; CHECK: [[META1]] = !{!"llvm.loop.isvectorized", i32 1}
65+
; CHECK: [[META2]] = !{!"llvm.loop.isvectorized.tailfoldingstyle", !"evl"}
66+
; CHECK: [[META3]] = !{!"llvm.loop.unroll.runtime.disable"}
67+
; CHECK: [[LOOP4]] = distinct !{[[LOOP4]], [[META3]], [[META1]]}
68+
;.

0 commit comments

Comments
 (0)