Skip to content

Commit b0536b5

Browse files
fhahnzmodem
authored andcommitted
[LV] Do not try to sink dead instructions.
Dead instructions do not need to be sunk. Currently we try and record the recipies for them, but there are no recipes emitted for them and there's nothing to sink. They can be removed from SinkAfter while marking them for recording. Fixes PR44634. Reviewers: rengolin, hsaito, fhahn, Ayal, gilr Reviewed By: gilr Differential Revision: https://reviews.llvm.org/D73423 (cherry picked from commit a911fef)
1 parent d275de3 commit b0536b5

File tree

3 files changed

+51
-8
lines changed

3 files changed

+51
-8
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationPlanner.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,10 @@ class LoopVectorizationPlanner {
279279

280280
/// Build a VPlan using VPRecipes according to the information gather by
281281
/// Legal. This method is only used for the legacy inner loop vectorizer.
282-
VPlanPtr
283-
buildVPlanWithVPRecipes(VFRange &Range, SmallPtrSetImpl<Value *> &NeedDef,
284-
SmallPtrSetImpl<Instruction *> &DeadInstructions);
282+
VPlanPtr buildVPlanWithVPRecipes(
283+
VFRange &Range, SmallPtrSetImpl<Value *> &NeedDef,
284+
SmallPtrSetImpl<Instruction *> &DeadInstructions,
285+
const DenseMap<Instruction *, Instruction *> &SinkAfter);
285286

286287
/// Build VPlans for power-of-2 VF's between \p MinVF and \p MaxVF inclusive,
287288
/// according to the information gathered by Legal when it checked if it is

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7118,25 +7118,29 @@ void LoopVectorizationPlanner::buildVPlansWithVPRecipes(unsigned MinVF,
71187118
SmallPtrSet<Instruction *, 4> DeadInstructions;
71197119
collectTriviallyDeadInstructions(DeadInstructions);
71207120

7121+
DenseMap<Instruction *, Instruction *> &SinkAfter = Legal->getSinkAfter();
7122+
// Dead instructions do not need sinking. Remove them from SinkAfter.
7123+
for (Instruction *I : DeadInstructions)
7124+
SinkAfter.erase(I);
7125+
71217126
for (unsigned VF = MinVF; VF < MaxVF + 1;) {
71227127
VFRange SubRange = {VF, MaxVF + 1};
7123-
VPlans.push_back(
7124-
buildVPlanWithVPRecipes(SubRange, NeedDef, DeadInstructions));
7128+
VPlans.push_back(buildVPlanWithVPRecipes(SubRange, NeedDef,
7129+
DeadInstructions, SinkAfter));
71257130
VF = SubRange.End;
71267131
}
71277132
}
71287133

71297134
VPlanPtr LoopVectorizationPlanner::buildVPlanWithVPRecipes(
71307135
VFRange &Range, SmallPtrSetImpl<Value *> &NeedDef,
7131-
SmallPtrSetImpl<Instruction *> &DeadInstructions) {
7136+
SmallPtrSetImpl<Instruction *> &DeadInstructions,
7137+
const DenseMap<Instruction *, Instruction *> &SinkAfter) {
71327138

71337139
// Hold a mapping from predicated instructions to their recipes, in order to
71347140
// fix their AlsoPack behavior if a user is determined to replicate and use a
71357141
// scalar instead of vector value.
71367142
DenseMap<Instruction *, VPReplicateRecipe *> PredInst2Recipe;
71377143

7138-
DenseMap<Instruction *, Instruction *> &SinkAfter = Legal->getSinkAfter();
7139-
71407144
SmallPtrSet<const InterleaveGroup<Instruction> *, 1> InterleaveGroups;
71417145

71427146
VPRecipeBuilder RecipeBuilder(OrigLoop, TLI, Legal, CM, Builder);

llvm/test/Transforms/LoopVectorize/first-order-recurrence.ll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,3 +607,41 @@ cond.end: ; preds = %for.body4, %cond.tr
607607
for.end12.loopexit: ; preds = %cond.end
608608
ret void
609609
}
610+
611+
; Dead instructions, like the exit condition are not part of the actual VPlan
612+
; and do not need to be sunk. PR44634.
613+
define void @sink_dead_inst() {
614+
; SINK-AFTER-LABEL: define void @sink_dead_inst(
615+
; SINK-AFTER-LABEL: vector.body: ; preds = %vector.body, %vector.ph
616+
; SINK-AFTER-NEXT: %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
617+
; SINK-AFTER-NEXT: %vec.ind = phi <4 x i16> [ <i16 -27, i16 -26, i16 -25, i16 -24>, %vector.ph ], [ %vec.ind.next, %vector.body ]
618+
; SINK-AFTER-NEXT: %vector.recur = phi <4 x i16> [ <i16 undef, i16 undef, i16 undef, i16 0>, %vector.ph ], [ %3, %vector.body ]
619+
; SINK-AFTER-NEXT: %vector.recur2 = phi <4 x i32> [ <i32 undef, i32 undef, i32 undef, i32 -27>, %vector.ph ], [ %1, %vector.body ]
620+
; SINK-AFTER-NEXT: %0 = add <4 x i16> %vec.ind, <i16 1, i16 1, i16 1, i16 1>
621+
; SINK-AFTER-NEXT: %1 = zext <4 x i16> %0 to <4 x i32>
622+
; SINK-AFTER-NEXT: %2 = shufflevector <4 x i32> %vector.recur2, <4 x i32> %1, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
623+
; SINK-AFTER-NEXT: %3 = add <4 x i16> %0, <i16 5, i16 5, i16 5, i16 5>
624+
; SINK-AFTER-NEXT: %4 = shufflevector <4 x i16> %vector.recur, <4 x i16> %3, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
625+
; SINK-AFTER-NEXT: %5 = sub <4 x i16> %4, <i16 10, i16 10, i16 10, i16 10>
626+
; SINK-AFTER-NEXT: %index.next = add i32 %index, 4
627+
; SINK-AFTER-NEXT: %vec.ind.next = add <4 x i16> %vec.ind, <i16 4, i16 4, i16 4, i16 4>
628+
; SINK-AFTER-NEXT: %6 = icmp eq i32 %index.next, 40
629+
; SINK-AFTER-NEXT: br i1 %6, label %middle.block, label %vector.body, !llvm.loop !43
630+
;
631+
entry:
632+
br label %for.cond
633+
634+
for.cond:
635+
%iv = phi i16 [ -27, %entry ], [ %iv.next, %for.cond ]
636+
%rec.1 = phi i16 [ 0, %entry ], [ %rec.1.prev, %for.cond ]
637+
%rec.2 = phi i32 [ -27, %entry ], [ %rec.2.prev, %for.cond ]
638+
%use.rec.1 = sub i16 %rec.1, 10
639+
%cmp = icmp eq i32 %rec.2, 15
640+
%iv.next = add i16 %iv, 1
641+
%rec.2.prev = zext i16 %iv.next to i32
642+
%rec.1.prev = add i16 %iv.next, 5
643+
br i1 %cmp, label %for.end, label %for.cond
644+
645+
for.end:
646+
ret void
647+
}

0 commit comments

Comments
 (0)