[llvm] [LV] Fix miscompile with conditional scalar assignment + tail folding (PR #182492)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 20 05:26:33 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-vectorizers
Author: Benjamin Maxwell (MacDue)
<details>
<summary>Changes</summary>
Previously, we could miscompile when vectorizing conditional scalar assignments with forced tail folding, as the backegde select could be based on the header mask, not the assignment conditional.
This resulted in a number of failures in the LLVM test suite when building with `-O3 -march=armv8-a+sve -mllvm -prefer-predicate-over-epilogue=predicate-dont-vectorize`.
The patch reworks `handleFindLastReductions()` to correctly handle tail folding.
---
Patch is 68.86 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/182492.diff
7 Files Affected:
- (modified) llvm/lib/Transforms/Vectorize/LoopVectorize.cpp (+9-3)
- (modified) llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp (+27-9)
- (modified) llvm/lib/Transforms/Vectorize/VPlanTransforms.h (+1-1)
- (added) llvm/test/Transforms/LoopVectorize/AArch64/conditional-scalar-assignment-fold-tail.ll (+141)
- (modified) llvm/test/Transforms/LoopVectorize/VPlan/conditional-scalar-assignment-vplan.ll (+178-2)
- (modified) llvm/test/Transforms/LoopVectorize/iv-select-cmp-decreasing.ll (+40-326)
- (modified) llvm/test/Transforms/LoopVectorize/iv-select-cmp-fold-tail.ll (+74-10)
``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
index 6299e8c2dbd32..f87ce76d2689b 100644
--- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
+++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
@@ -8369,7 +8369,8 @@ VPlanPtr LoopVectorizationPlanner::tryToBuildVPlanWithVPRecipes(
return nullptr;
// Create whole-vector selects for find-last recurrences.
- if (!RUN_VPLAN_PASS(VPlanTransforms::handleFindLastReductions, *Plan))
+ if (!RUN_VPLAN_PASS(VPlanTransforms::handleFindLastReductions, *Plan,
+ CM.foldTailByMasking()))
return nullptr;
// Create partial reduction recipes for scaled reductions and transform
@@ -8486,6 +8487,7 @@ void LoopVectorizationPlanner::addReductionResultComputation(
if (!PhiR || isa<VPIRValue>(PhiR->getOperand(1)))
continue;
+ RecurKind RecurrenceKind = PhiR->getRecurrenceKind();
const RecurrenceDescriptor &RdxDesc = Legal->getRecurrenceDescriptor(
cast<PHINode>(PhiR->getUnderlyingInstr()));
Type *PhiTy = TypeInfo.inferScalarType(PhiR);
@@ -8510,7 +8512,12 @@ void LoopVectorizationPlanner::addReductionResultComputation(
m_VPInstruction<VPInstruction::ComputeAnyOfResult>(),
m_VPInstruction<VPInstruction::ComputeReductionResult>()));
});
- if (CM.usePredicatedReductionSelect())
+
+ // Note: For FindLast recurrences we prefer a predicated select to
+ // simplify matching in handleFindLastReductions(), rather than handle
+ // multiple cases.
+ if (CM.usePredicatedReductionSelect() ||
+ RecurrenceDescriptor::isFindLastRecurrenceKind(RecurrenceKind))
PhiR->setOperand(1, NewExitingVPV);
}
@@ -8530,7 +8537,6 @@ void LoopVectorizationPlanner::addReductionResultComputation(
VPInstruction *FinalReductionResult;
VPBuilder::InsertPointGuard Guard(Builder);
Builder.setInsertPoint(MiddleVPBB, IP);
- RecurKind RecurrenceKind = PhiR->getRecurrenceKind();
// For AnyOf reductions, find the select among PhiR's users. This is used
// both to find NewVal for ComputeAnyOfResult and to adjust the reduction.
VPRecipeBase *AnyOfSelect = nullptr;
diff --git a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
index 1af7392b904da..bf6d2704dd230 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanConstruction.cpp
@@ -1329,7 +1329,7 @@ bool VPlanTransforms::handleMaxMinNumReductions(VPlan &Plan) {
return true;
}
-bool VPlanTransforms::handleFindLastReductions(VPlan &Plan) {
+bool VPlanTransforms::handleFindLastReductions(VPlan &Plan, bool FoldTail) {
if (Plan.hasScalarVFOnly())
return false;
@@ -1354,6 +1354,7 @@ bool VPlanTransforms::handleFindLastReductions(VPlan &Plan) {
// ...extract-last-active replaces compute-reduction-result.
// result = extract-last-active vp<new.data>, vp<new.mask>, ir<default.val>
+ VPValue *HeaderMask = FoldTail ? vputils::findHeaderMask(Plan) : nullptr;
for (auto &Phi : Plan.getVectorLoopRegion()->getEntryBasicBlock()->phis()) {
auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&Phi);
if (!PhiR || !RecurrenceDescriptor::isFindLastRecurrenceKind(
@@ -1361,7 +1362,16 @@ bool VPlanTransforms::handleFindLastReductions(VPlan &Plan) {
continue;
// Find the condition for the select/blend.
- auto *SelectR = cast<VPSingleDefRecipe>(&PhiR->getBackedgeRecipe());
+ VPValue *BackedgeSelect = PhiR->getBackedgeRecipe().getVPSingleValue();
+ VPValue *CondSelect = BackedgeSelect;
+
+ // If there's a header mask the backedge select will not be the find-last
+ // select.
+ if (HeaderMask && !match(BackedgeSelect,
+ m_Select(m_Specific(HeaderMask),
+ m_VPValue(CondSelect), m_Specific(PhiR))))
+ return false;
+
VPValue *Cond = nullptr, *Op1 = nullptr, *Op2 = nullptr;
// If we're matching a blend rather than a select, there should be one
@@ -1385,11 +1395,23 @@ bool VPlanTransforms::handleFindLastReductions(VPlan &Plan) {
return NumIncomingDataValues == 1;
};
+ VPSingleDefRecipe *SelectR =
+ cast<VPSingleDefRecipe>(CondSelect->getDefiningRecipe());
if (!match(SelectR,
m_Select(m_VPValue(Cond), m_VPValue(Op1), m_VPValue(Op2))) &&
!MatchBlend(SelectR))
return false;
+ assert(Cond != HeaderMask && "Cond must not be HeaderMask");
+
+ // Find final reduction computation and replace it with an
+ // extract.last.active intrinsic.
+ auto *RdxResult =
+ vputils::findUserOf<VPInstruction::ComputeReductionResult>(
+ BackedgeSelect);
+ if (!RdxResult)
+ return false;
+
// Add mask phi.
VPBuilder Builder = VPBuilder::getToInsertAfter(PhiR);
auto *MaskPHI = new VPWidenPHIRecipe(nullptr, /*Start=*/Plan.getFalse());
@@ -1406,6 +1428,9 @@ bool VPlanTransforms::handleFindLastReductions(VPlan &Plan) {
}
assert(Op2 == PhiR && "data value must be selected if Cond is true");
+ if (HeaderMask)
+ Cond = Builder.createAnd(Cond, HeaderMask);
+
VPValue *AnyOf = Builder.createNaryOp(VPInstruction::AnyOf, {Cond});
VPValue *MaskSelect = Builder.createSelect(AnyOf, Cond, MaskPHI);
MaskPHI->addOperand(MaskSelect);
@@ -1416,13 +1441,6 @@ bool VPlanTransforms::handleFindLastReductions(VPlan &Plan) {
SelectR->replaceAllUsesWith(DataSelect);
SelectR->eraseFromParent();
- // Find final reduction computation and replace it with an
- // extract.last.active intrinsic.
- auto *RdxResult =
- vputils::findUserOf<VPInstruction::ComputeReductionResult>(DataSelect);
- // TODO: Handle tail-folding.
- if (!RdxResult)
- return false;
Builder.setInsertPoint(RdxResult);
auto *ExtractLastActive =
Builder.createNaryOp(VPInstruction::ExtractLastActive,
diff --git a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
index f2dfc166cecc9..3b5e746bddefc 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanTransforms.h
@@ -219,7 +219,7 @@ struct VPlanTransforms {
/// for entire vectors for both the latest mask containing at least one active
/// element and the corresponding data vector. Return false if this attempt
/// was unsuccessful.
- static bool handleFindLastReductions(VPlan &Plan);
+ static bool handleFindLastReductions(VPlan &Plan, bool FoldTail);
/// Clear NSW/NUW flags from reduction instructions if necessary.
static void clearReductionWrapFlags(VPlan &Plan);
diff --git a/llvm/test/Transforms/LoopVectorize/AArch64/conditional-scalar-assignment-fold-tail.ll b/llvm/test/Transforms/LoopVectorize/AArch64/conditional-scalar-assignment-fold-tail.ll
new file mode 100644
index 0000000000000..3b16ad0413bc1
--- /dev/null
+++ b/llvm/test/Transforms/LoopVectorize/AArch64/conditional-scalar-assignment-fold-tail.ll
@@ -0,0 +1,141 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals none --version 6
+; RUN: opt -passes=loop-vectorize -prefer-predicate-over-epilogue=predicate-dont-vectorize -mattr=+sve -S < %s 2>&1 | FileCheck %s --check-prefix=SVE
+
+target triple = "aarch64-linux-gnu"
+
+define i32 @simple_csa_int_select(i64 %N, ptr %data, i32 %a) {
+; SVE-LABEL: define i32 @simple_csa_int_select(
+; SVE-SAME: i64 [[N:%.*]], ptr [[DATA:%.*]], i32 [[A:%.*]]) #[[ATTR0:[0-9]+]] {
+; SVE-NEXT: [[ENTRY:.*:]]
+; SVE-NEXT: br label %[[VECTOR_PH:.*]]
+; SVE: [[VECTOR_PH]]:
+; SVE-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
+; SVE-NEXT: [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
+; SVE-NEXT: [[TMP11:%.*]] = sub i64 [[N]], [[TMP3]]
+; SVE-NEXT: [[TMP16:%.*]] = icmp ugt i64 [[N]], [[TMP3]]
+; SVE-NEXT: [[TMP4:%.*]] = select i1 [[TMP16]], i64 [[TMP11]], i64 0
+; SVE-NEXT: [[ACTIVE_LANE_MASK_ENTRY:%.*]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 0, i64 [[N]])
+; SVE-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[A]], i64 0
+; SVE-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; SVE-NEXT: br label %[[VECTOR_BODY:.*]]
+; SVE: [[VECTOR_BODY]]:
+; SVE-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; SVE-NEXT: [[ACTIVE_LANE_MASK:%.*]] = phi <vscale x 4 x i1> [ [[ACTIVE_LANE_MASK_ENTRY]], %[[VECTOR_PH]] ], [ [[ACTIVE_LANE_MASK_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; SVE-NEXT: [[VEC_PHI:%.*]] = phi <vscale x 4 x i32> [ splat (i32 -1), %[[VECTOR_PH]] ], [ [[TMP17:%.*]], %[[VECTOR_BODY]] ]
+; SVE-NEXT: [[TMP5:%.*]] = phi <vscale x 4 x i1> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP9:%.*]], %[[VECTOR_BODY]] ]
+; SVE-NEXT: [[TMP6:%.*]] = getelementptr inbounds i32, ptr [[DATA]], i64 [[INDEX]]
+; SVE-NEXT: [[WIDE_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.masked.load.nxv4i32.p0(ptr align 4 [[TMP6]], <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> poison)
+; SVE-NEXT: [[TMP13:%.*]] = icmp slt <vscale x 4 x i32> [[BROADCAST_SPLAT]], [[WIDE_LOAD]]
+; SVE-NEXT: [[TMP18:%.*]] = and <vscale x 4 x i1> [[TMP13]], [[ACTIVE_LANE_MASK]]
+; SVE-NEXT: [[TMP7:%.*]] = freeze <vscale x 4 x i1> [[TMP18]]
+; SVE-NEXT: [[TMP8:%.*]] = call i1 @llvm.vector.reduce.or.nxv4i1(<vscale x 4 x i1> [[TMP7]])
+; SVE-NEXT: [[TMP9]] = select i1 [[TMP8]], <vscale x 4 x i1> [[TMP18]], <vscale x 4 x i1> [[TMP5]]
+; SVE-NEXT: [[TMP10:%.*]] = select i1 [[TMP8]], <vscale x 4 x i32> [[WIDE_LOAD]], <vscale x 4 x i32> [[VEC_PHI]]
+; SVE-NEXT: [[TMP17]] = select <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> [[TMP10]], <vscale x 4 x i32> [[VEC_PHI]]
+; SVE-NEXT: [[INDEX_NEXT]] = add i64 [[INDEX]], [[TMP3]]
+; SVE-NEXT: [[ACTIVE_LANE_MASK_NEXT]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 [[INDEX]], i64 [[TMP4]])
+; SVE-NEXT: [[TMP14:%.*]] = extractelement <vscale x 4 x i1> [[ACTIVE_LANE_MASK_NEXT]], i32 0
+; SVE-NEXT: [[TMP15:%.*]] = xor i1 [[TMP14]], true
+; SVE-NEXT: br i1 [[TMP15]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
+; SVE: [[MIDDLE_BLOCK]]:
+; SVE-NEXT: [[TMP12:%.*]] = call i32 @llvm.experimental.vector.extract.last.active.nxv4i32(<vscale x 4 x i32> [[TMP10]], <vscale x 4 x i1> [[TMP9]], i32 -1)
+; SVE-NEXT: br label %[[LOOP:.*]]
+; SVE: [[LOOP]]:
+; SVE-NEXT: ret i32 [[TMP12]]
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
+ %data.phi = phi i32 [ -1, %entry ], [ %select.data, %loop ]
+ %ld.addr = getelementptr inbounds i32, ptr %data, i64 %iv
+ %ld = load i32, ptr %ld.addr, align 4
+ %select.cmp = icmp slt i32 %a, %ld
+ %select.data = select i1 %select.cmp, i32 %ld, i32 %data.phi
+ %iv.next = add nuw nsw i64 %iv, 1
+ %exit.cmp = icmp eq i64 %iv.next, %N
+ br i1 %exit.cmp, label %exit, label %loop
+
+exit:
+ ret i32 %select.data
+}
+; This test is derived from the following C program:
+; int simple_csa_int_load(int* a, int* b, int default_val, int N, int threshold) {
+; int result = default_val;
+; for (int i = 0; i < N; ++i)
+; if (a[i] > threshold)
+; result = b[i];
+; return result;
+; }
+define i32 @simple_csa_int_load(ptr noalias %a, ptr noalias %b, i32 %default_val, i64 %N, i32 %threshold) {
+; SVE-LABEL: define i32 @simple_csa_int_load(
+; SVE-SAME: ptr noalias [[A:%.*]], ptr noalias [[B:%.*]], i32 [[DEFAULT_VAL:%.*]], i64 [[N:%.*]], i32 [[THRESHOLD:%.*]]) #[[ATTR0]] {
+; SVE-NEXT: [[ENTRY:.*:]]
+; SVE-NEXT: br label %[[VECTOR_PH:.*]]
+; SVE: [[VECTOR_PH]]:
+; SVE-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
+; SVE-NEXT: [[TMP3:%.*]] = shl nuw i64 [[TMP2]], 2
+; SVE-NEXT: [[TMP12:%.*]] = sub i64 [[N]], [[TMP3]]
+; SVE-NEXT: [[TMP18:%.*]] = icmp ugt i64 [[N]], [[TMP3]]
+; SVE-NEXT: [[TMP4:%.*]] = select i1 [[TMP18]], i64 [[TMP12]], i64 0
+; SVE-NEXT: [[ACTIVE_LANE_MASK_ENTRY:%.*]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 0, i64 [[N]])
+; SVE-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[THRESHOLD]], i64 0
+; SVE-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; SVE-NEXT: [[BROADCAST_SPLATINSERT1:%.*]] = insertelement <vscale x 4 x i32> poison, i32 [[DEFAULT_VAL]], i64 0
+; SVE-NEXT: [[BROADCAST_SPLAT2:%.*]] = shufflevector <vscale x 4 x i32> [[BROADCAST_SPLATINSERT1]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
+; SVE-NEXT: br label %[[VECTOR_BODY:.*]]
+; SVE: [[VECTOR_BODY]]:
+; SVE-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; SVE-NEXT: [[ACTIVE_LANE_MASK:%.*]] = phi <vscale x 4 x i1> [ [[ACTIVE_LANE_MASK_ENTRY]], %[[VECTOR_PH]] ], [ [[ACTIVE_LANE_MASK_NEXT:%.*]], %[[VECTOR_BODY]] ]
+; SVE-NEXT: [[VEC_PHI:%.*]] = phi <vscale x 4 x i32> [ [[BROADCAST_SPLAT2]], %[[VECTOR_PH]] ], [ [[TMP15:%.*]], %[[VECTOR_BODY]] ]
+; SVE-NEXT: [[TMP19:%.*]] = phi <vscale x 4 x i1> [ zeroinitializer, %[[VECTOR_PH]] ], [ [[TMP10:%.*]], %[[VECTOR_BODY]] ]
+; SVE-NEXT: [[TMP5:%.*]] = getelementptr inbounds nuw i32, ptr [[A]], i64 [[INDEX]]
+; SVE-NEXT: [[WIDE_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.masked.load.nxv4i32.p0(ptr align 4 [[TMP5]], <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> poison)
+; SVE-NEXT: [[TMP6:%.*]] = icmp sgt <vscale x 4 x i32> [[WIDE_LOAD]], [[BROADCAST_SPLAT]]
+; SVE-NEXT: [[TMP20:%.*]] = select <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i1> [[TMP6]], <vscale x 4 x i1> zeroinitializer
+; SVE-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[B]], i64 [[INDEX]]
+; SVE-NEXT: [[WIDE_MASKED_LOAD:%.*]] = call <vscale x 4 x i32> @llvm.masked.load.nxv4i32.p0(ptr align 4 [[TMP7]], <vscale x 4 x i1> [[TMP20]], <vscale x 4 x i32> poison)
+; SVE-NEXT: [[TMP21:%.*]] = and <vscale x 4 x i1> [[TMP20]], [[ACTIVE_LANE_MASK]]
+; SVE-NEXT: [[TMP8:%.*]] = freeze <vscale x 4 x i1> [[TMP21]]
+; SVE-NEXT: [[TMP9:%.*]] = call i1 @llvm.vector.reduce.or.nxv4i1(<vscale x 4 x i1> [[TMP8]])
+; SVE-NEXT: [[TMP10]] = select i1 [[TMP9]], <vscale x 4 x i1> [[TMP21]], <vscale x 4 x i1> [[TMP19]]
+; SVE-NEXT: [[TMP11:%.*]] = select i1 [[TMP9]], <vscale x 4 x i32> [[WIDE_MASKED_LOAD]], <vscale x 4 x i32> [[VEC_PHI]]
+; SVE-NEXT: [[TMP15]] = select <vscale x 4 x i1> [[ACTIVE_LANE_MASK]], <vscale x 4 x i32> [[TMP11]], <vscale x 4 x i32> [[VEC_PHI]]
+; SVE-NEXT: [[INDEX_NEXT]] = add i64 [[INDEX]], [[TMP3]]
+; SVE-NEXT: [[ACTIVE_LANE_MASK_NEXT]] = call <vscale x 4 x i1> @llvm.get.active.lane.mask.nxv4i1.i64(i64 [[INDEX]], i64 [[TMP4]])
+; SVE-NEXT: [[TMP16:%.*]] = extractelement <vscale x 4 x i1> [[ACTIVE_LANE_MASK_NEXT]], i32 0
+; SVE-NEXT: [[TMP17:%.*]] = xor i1 [[TMP16]], true
+; SVE-NEXT: br i1 [[TMP17]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP3:![0-9]+]]
+; SVE: [[MIDDLE_BLOCK]]:
+; SVE-NEXT: [[TMP13:%.*]] = extractelement <vscale x 4 x i32> [[BROADCAST_SPLAT2]], i32 0
+; SVE-NEXT: [[TMP14:%.*]] = call i32 @llvm.experimental.vector.extract.last.active.nxv4i32(<vscale x 4 x i32> [[TMP11]], <vscale x 4 x i1> [[TMP10]], i32 [[TMP13]])
+; SVE-NEXT: br label %[[LATCH:.*]]
+; SVE: [[LATCH]]:
+; SVE-NEXT: ret i32 [[TMP14]]
+;
+entry:
+ br label %loop
+
+loop:
+ %iv = phi i64 [ 0, %entry ], [ %iv.next, %latch ]
+ %data.phi = phi i32 [ %default_val, %entry ], [ %select.data, %latch ]
+ %a.addr = getelementptr inbounds nuw i32, ptr %a, i64 %iv
+ %ld.a = load i32, ptr %a.addr, align 4
+ %if.cond = icmp sgt i32 %ld.a, %threshold
+ br i1 %if.cond, label %if.then, label %latch
+
+if.then:
+ %b.addr = getelementptr inbounds nuw i32, ptr %b, i64 %iv
+ %ld.b = load i32, ptr %b.addr, align 4
+ br label %latch
+
+latch:
+ %select.data = phi i32 [ %ld.b, %if.then ], [ %data.phi, %loop ]
+ %iv.next = add nuw nsw i64 %iv, 1
+ %exit.cmp = icmp eq i64 %iv.next, %N
+ br i1 %exit.cmp, label %exit, label %loop
+
+exit:
+ ret i32 %select.data
+}
diff --git a/llvm/test/Transforms/LoopVectorize/VPlan/conditional-scalar-assignment-vplan.ll b/llvm/test/Transforms/LoopVectorize/VPlan/conditional-scalar-assignment-vplan.ll
index 5e29bfdbd6e81..60515910b6364 100644
--- a/llvm/test/Transforms/LoopVectorize/VPlan/conditional-scalar-assignment-vplan.ll
+++ b/llvm/test/Transforms/LoopVectorize/VPlan/conditional-scalar-assignment-vplan.ll
@@ -1,6 +1,9 @@
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
; RUN: opt -passes=loop-vectorize -vplan-print-after="optimize$" \
; RUN: -force-vector-width=4 -disable-output 2>&1 < %s | FileCheck %s
+; RUN: opt -passes=loop-vectorize -vplan-print-after="optimize$" \
+; RUN: -force-vector-width=4 -prefer-predicate-over-epilogue=predicate-dont-vectorize \
+; RUN: -disable-output 2>&1 < %s | FileCheck %s --check-prefix=CHECK-TF
; This function is derived from the following C program:
; int simple_csa_int_select(int N, int *data, int a) {
@@ -12,7 +15,7 @@
; return t;
; }
define i32 @simple_csa_int_select(i64 %N, ptr %data, i32 %a) {
-; CHECK-LABEL: 'simple_csa_int_select'
+; CHECK-LABEL: VPlan for loop in 'simple_csa_int_select'
; CHECK: VPlan 'Initial VPlan for VF={4},UF>=1' {
; CHECK-NEXT: Live-in vp<[[VP0:%[0-9]+]]> = VF
; CHECK-NEXT: Live-in vp<[[VP1:%[0-9]+]]> = VF * UF
@@ -71,6 +74,84 @@ define i32 @simple_csa_int_select(i64 %N, ptr %data, i32 %a) {
; CHECK-NEXT: No successors
; CHECK-NEXT: }
;
+; CHECK-TF-LABEL: VPlan for loop in 'simple_csa_int_select'
+; CHECK-TF: VPlan 'Initial VPlan for VF={4},UF>=1' {
+; CHECK-TF-NEXT: Live-in vp<[[VP0:%[0-9]+]]> = VF
+; CHECK-TF-NEXT: Live-in vp<[[VP1:%[0-9]+]]> = VF * UF
+; CHECK-TF-NEXT: Live-in vp<[[VP2:%[0-9]+]]> = vector-trip-count
+; CHECK-TF-NEXT: Live-in vp<[[VP3:%[0-9]+]]> = backedge-taken count
+; CHECK-TF-NEXT: Live-in ir<%N> = original trip-count
+; CHECK-TF-EMPTY:
+; CHECK-TF-NEXT: ir-bb<entry>:
+; CHECK-TF-NEXT: Successor(s): scalar.ph, vector.ph
+; CHECK-TF-EMPTY:
+; CHECK-TF-NEXT: vector.ph:
+; CHECK-TF-NEXT: Successor(s): vector loop
+; CHECK-TF-EMPTY:
+; CHECK-TF-NEXT: <x1> vector loop: {
+; CHECK-TF-NEXT: vector.body:
+; CHECK-TF-NEXT: EMIT vp<[[VP4:%[0-9]+]]> = CANONICAL-INDUCTION ir<0>, vp<%index.next>
+; CHECK-TF-NEXT: ir<%iv> = WIDEN-INDUCTION ir<0>, ir<1>, vp<[[VP0]]>
+; CHECK-TF-NEXT: WIDEN-REDUCTION-PHI ir<%data.phi> = phi ir<-1>, vp<[[VP13:%[0-9]+]]>
+; CHECK-TF-NEXT: WIDEN-PHI vp<[[VP5:%[0-9]+]]> = phi [ ir<false>, vector.ph ], [ vp<[[VP11:%[0-9]+]]>, loop.0 ]
+; CHECK-TF-NEXT: EMIT vp<[[VP6:%[0-9]+]]> = icmp ule ir<%iv>, vp<[[VP3]]>
+; CHECK-TF-NEXT: Successor(s): pred.load
+; CHECK-TF-EMPTY:
+; CHECK-TF-NEXT: <xVFxUF> pred.load: {
+; CHECK-TF-NEXT: pred.load.entry:
+; CHECK-TF-NEXT: BRANCH-ON-MASK vp<[[VP6]]>
+; CHECK-TF-NEXT: Successor(s): pred.load.if, pred.load.continue
+; CHECK-TF-EMPTY:
+; CHECK-TF-NEXT: pred.load.if:
+; CHECK-TF-NEXT: vp<[[VP7:%[0-9]+]]> = SCALAR-STEPS vp<[[VP4]]>, ir<1>, vp<[[VP0]]>
+; CHECK-TF-NEXT: REPLICATE ir<%ld.addr> = getelementptr inbounds ir<%data>, vp<[[VP7]]>
+; CHECK-TF-NEXT: REPLICATE ir<%ld> = load ir<%ld.addr> (S->V)
+; CHECK-TF-NEXT: Successor(s): pred.load.continue
+; CHECK-TF-EMPTY:
+; CHECK-TF-NEXT: pred.load.continue:
+; CHECK-TF-NEXT: PHI-PREDICATED-INSTRUCTION vp<[[VP8:%[0-9]+]]> = ir<%ld>
+; CHECK-TF-NEXT: No successors
+; CHECK-TF-NEXT: }
+; CHECK-TF-NEXT: Successor(s): loop.0
+; CHECK-TF-EMPTY:
+; CHECK-TF-NEXT: loop.0:
+; CHECK-TF-NEXT: WIDEN ir<%select.cmp> = icmp slt ir<%a>, vp<[[VP8]]>
+; CHECK-TF-NEXT: EMIT vp<[[VP9:%[0-9]+]]> = and ir<%select.cmp>, vp<[[VP6]]>
+; CHECK-TF-NEXT: EMIT vp<[[VP10:%[0-9]+]]> = any-of vp<[[VP9]]>
+; CHECK-TF-NEXT: EMIT vp<[[VP11]]> = select vp<[[VP10]]>, vp<[[VP9]]>, vp<[[VP5]]>
+; CHECK-TF-NEXT: EMIT vp<[[VP12:%[0-9]+]]> = select vp<[[VP10]...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/182492
More information about the llvm-commits
mailing list