Skip to content

Commit 60ed236

Browse files
[LV][EVL]Explicitly model AVL as sub, original TC, EVL_PHI.
Patch explicitly models AVL as sub original TC, EVL_PHI instead of having it in EXPLICIT-VECTOR-LENGTH VPInstruction. Required for correct safe dependence distance suport. Reviewers: fhahn, ayalz Reviewed By: ayalz Pull Request: #108869
1 parent 5ef02a3 commit 60ed236

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -477,27 +477,19 @@ Value *VPInstruction::generate(VPTransformState &State) {
477477
return Builder.CreateSelect(Cmp, Sub, Zero);
478478
}
479479
case VPInstruction::ExplicitVectorLength: {
480-
// Compute EVL
481-
auto GetEVL = [=](VPTransformState &State, Value *AVL) {
482-
assert(AVL->getType()->isIntegerTy() &&
483-
"Requested vector length should be an integer.");
484-
485-
// TODO: Add support for MaxSafeDist for correct loop emission.
486-
assert(State.VF.isScalable() && "Expected scalable vector factor.");
487-
Value *VFArg = State.Builder.getInt32(State.VF.getKnownMinValue());
488-
489-
Value *EVL = State.Builder.CreateIntrinsic(
490-
State.Builder.getInt32Ty(), Intrinsic::experimental_get_vector_length,
491-
{AVL, VFArg, State.Builder.getTrue()});
492-
return EVL;
493-
};
494480
// TODO: Restructure this code with an explicit remainder loop, vsetvli can
495481
// be outside of the main loop.
496-
// Compute VTC - IV as the AVL (requested vector length).
497-
Value *Index = State.get(getOperand(0), VPIteration(0, 0));
498-
Value *TripCount = State.get(getOperand(1), VPIteration(0, 0));
499-
Value *AVL = State.Builder.CreateSub(TripCount, Index);
500-
Value *EVL = GetEVL(State, AVL);
482+
Value *AVL = State.get(getOperand(0), VPIteration(0, 0));
483+
// Compute EVL
484+
assert(AVL->getType()->isIntegerTy() &&
485+
"Requested vector length should be an integer.");
486+
487+
assert(State.VF.isScalable() && "Expected scalable vector factor.");
488+
Value *VFArg = State.Builder.getInt32(State.VF.getKnownMinValue());
489+
490+
Value *EVL = State.Builder.CreateIntrinsic(
491+
State.Builder.getInt32Ty(), Intrinsic::experimental_get_vector_length,
492+
{AVL, VFArg, State.Builder.getTrue()});
501493
return EVL;
502494
}
503495
case VPInstruction::CanonicalIVIncrementForPart: {

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,8 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
14231423
/// ...
14241424
/// %EVLPhi = EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI [ %StartV, %vector.ph ],
14251425
/// [ %NextEVLIV, %vector.body ]
1426-
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %EVLPhi, original TC
1426+
/// %AVL = sub original TC, %EVLPhi
1427+
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %AVL
14271428
/// ...
14281429
/// %NextEVLIV = add IVSize (cast i32 %VPEVVL to IVSize), %EVLPhi
14291430
/// ...
@@ -1453,9 +1454,15 @@ bool VPlanTransforms::tryAddExplicitVectorLength(VPlan &Plan) {
14531454
// Create the ExplicitVectorLengthPhi recipe in the main loop.
14541455
auto *EVLPhi = new VPEVLBasedIVPHIRecipe(StartV, DebugLoc());
14551456
EVLPhi->insertAfter(CanonicalIVPHI);
1456-
auto *VPEVL = new VPInstruction(VPInstruction::ExplicitVectorLength,
1457-
{EVLPhi, Plan.getTripCount()});
1458-
VPEVL->insertBefore(*Header, Header->getFirstNonPhi());
1457+
// TODO: Add support for MaxSafeDist for correct loop emission.
1458+
// Compute original TC - IV as the AVL (application vector length).
1459+
auto *AVL = new VPInstruction(
1460+
Instruction::Sub, {Plan.getTripCount(), EVLPhi},
1461+
DebugLoc(), "avl");
1462+
AVL->insertBefore(*Header, Header->getFirstNonPhi());
1463+
auto *VPEVL =
1464+
new VPInstruction(VPInstruction::ExplicitVectorLength, AVL, DebugLoc());
1465+
VPEVL->insertAfter(AVL);
14591466

14601467
auto *CanonicalIVIncrement =
14611468
cast<VPInstruction>(CanonicalIVPHI->getBackedgeValue());

llvm/test/Transforms/LoopVectorize/RISCV/vplan-vp-intrinsics-reduction.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ define i32 @reduction(ptr %a, i64 %n, i32 %start) {
3939
; IF-EVL-INLOOP-NEXT: EMIT vp<[[IV:%[0-9]+]]> = CANONICAL-INDUCTION
4040
; IF-EVL-INLOOP-NEXT: EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI vp<[[EVL_PHI:%[0-9]+]]> = phi ir<0>, vp<[[IV_NEXT:%[0-9]+]]>
4141
; IF-EVL-INLOOP-NEXT: WIDEN-REDUCTION-PHI ir<[[RDX_PHI:%.+]]> = phi ir<%start>, ir<[[RDX_NEXT:%.+]]>
42-
; IF-EVL-INLOOP-NEXT: EMIT vp<[[EVL:%.+]]> = EXPLICIT-VECTOR-LENGTH vp<[[EVL_PHI]]>, ir<%n>
42+
; IF-EVL-INLOOP-NEXT: EMIT vp<[[AVL:%.+]]> = sub ir<%n>, vp<[[EVL_PHI]]>
43+
; IF-EVL-INLOOP-NEXT: EMIT vp<[[EVL:%.+]]> = EXPLICIT-VECTOR-LENGTH vp<[[AVL]]>
4344
; IF-EVL-INLOOP-NEXT: vp<[[ST:%[0-9]+]]> = SCALAR-STEPS vp<[[EVL_PHI]]>, ir<1>
4445
; IF-EVL-INLOOP-NEXT: CLONE ir<[[GEP1:%.+]]> = getelementptr inbounds ir<%a>, vp<[[ST]]>
4546
; IF-EVL-INLOOP-NEXT: vp<[[PTR1:%[0-9]+]]> = vector-pointer ir<[[GEP1]]>

llvm/test/Transforms/LoopVectorize/RISCV/vplan-vp-intrinsics.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ define void @foo(ptr noalias %a, ptr noalias %b, ptr noalias %c, i64 %N) {
2323
; IF-EVL-NEXT: vector.body:
2424
; IF-EVL-NEXT: EMIT vp<[[IV:%[0-9]+]]> = CANONICAL-INDUCTION
2525
; IF-EVL-NEXT: EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI vp<[[EVL_PHI:%[0-9]+]]> = phi ir<0>, vp<[[IV_NEXT:%[0-9]+]]>
26-
; IF-EVL-NEXT: EMIT vp<[[EVL:%.+]]> = EXPLICIT-VECTOR-LENGTH vp<[[EVL_PHI]]>, ir<%N>
26+
; IF-EVL-NEXT: EMIT vp<[[AVL:%.+]]> = sub ir<%N>, vp<[[EVL_PHI]]>
27+
; IF-EVL-NEXT: EMIT vp<[[EVL:%.+]]> = EXPLICIT-VECTOR-LENGTH vp<[[AVL]]>
2728
; IF-EVL-NEXT: vp<[[ST:%[0-9]+]]> = SCALAR-STEPS vp<[[EVL_PHI]]>, ir<1>
2829
; IF-EVL-NEXT: CLONE ir<[[GEP1:%.+]]> = getelementptr inbounds ir<%b>, vp<[[ST]]>
2930
; IF-EVL-NEXT: vp<[[PTR1:%[0-9]+]]> = vector-pointer ir<[[GEP1]]>

0 commit comments

Comments
 (0)