Skip to content

Commit 87e9f4a

Browse files
committed
Fix for single-iteration EVL loop
1 parent dde1d2c commit 87e9f4a

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
@@ -2393,8 +2393,19 @@ void VPlanTransforms::simplifyEVLIVs(VPlan &Plan) {
23932393
EVLPhi->replaceAllUsesWith(ScalarR);
23942394
EVLPhi->eraseFromParent();
23952395

2396-
// Find the latch-exiting block and convert to variable-length stepping.
2397-
// Before: (branch-on-count CanonicalIVInc, VectorTripCount)
2396+
// Replace CanonicalIVInc with EVL-PHI increment
2397+
VPRecipeBase *CanonicalIV = &*Entry->begin();
2398+
assert(dyn_cast<VPPhi>(CanonicalIV) && "Unexpected canoincal iv");
2399+
VPValue *Backedge = CanonicalIV->getOperand(1);
2400+
Backedge->replaceAllUsesWith(EVLIncrement);
2401+
2402+
// Remove unused phi
2403+
VPRecipeBase *CanonicalIVIncrement = Backedge->getDefiningRecipe();
2404+
CanonicalIVIncrement->eraseFromParent();
2405+
CanonicalIV->eraseFromParent();
2406+
2407+
// Find the latch-exiting block and replace use of VectorTripCount
2408+
// Before: (branch-on-count EVLIVInc, VectorTripCount)
23982409
// After: (branch-on-count EVLIVInc, TripCount)
23992410
auto Range =
24002411
VPBlockUtils::blocksOnly<VPBasicBlock>(vp_depth_first_shallow(Entry));
@@ -2403,20 +2414,20 @@ void VPlanTransforms::simplifyEVLIVs(VPlan &Plan) {
24032414
[&Entry](VPBlockBase *Succ) { return Succ == Entry; });
24042415
});
24052416
assert((It != Range.end()) && "LatchExiting is not found");
2417+
24062418
VPBasicBlock *LatchExiting = *It;
2419+
24072420
auto *LatchExitingBr = cast<VPInstruction>(LatchExiting->getTerminator());
2408-
VPValue *ScalarIVInc;
2421+
2422+
// Skip single-iteration loop region
2423+
if (match(LatchExitingBr, m_BranchOnCond(m_True())))
2424+
return;
24092425
assert(LatchExitingBr &&
24102426
match(LatchExitingBr,
2411-
m_BranchOnCount(m_VPValue(ScalarIVInc),
2427+
m_BranchOnCount(m_VPValue(EVLIncrement),
24122428
m_Specific(&Plan.getVectorTripCount()))) &&
24132429
"Unexpected terminator in EVL loop");
24142430
LatchExitingBr->setOperand(1, Plan.getTripCount());
2415-
ScalarIVInc->replaceAllUsesWith(EVLIncrement);
2416-
VPRecipeBase *IVIncR = ScalarIVInc->getDefiningRecipe();
2417-
VPRecipeBase *ScalarIV = IVIncR->getOperand(0)->getDefiningRecipe();
2418-
IVIncR->eraseFromParent();
2419-
ScalarIV->eraseFromParent();
24202431
}
24212432

24222433
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)