Skip to content

Commit 63bfa54

Browse files
committed
[VPlan] Introduce cannotHoistOrSinkRecipe
Factor out common code to determine legality of hoisting and sinking. In the case of the additonal use-sites, functional changes, if any, would amount to esoteric bugs being fixed.
1 parent 623227f commit 63bfa54

File tree

3 files changed

+31
-55
lines changed

3 files changed

+31
-55
lines changed

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ bool VPRecipeBase::mayWriteToMemory() const {
7070
return cast<VPWidenIntrinsicRecipe>(this)->mayWriteToMemory();
7171
case VPCanonicalIVPHISC:
7272
case VPBranchOnMaskSC:
73+
case VPDerivedIVSC:
7374
case VPFirstOrderRecurrencePHISC:
7475
case VPReductionPHISC:
7576
case VPScalarIVStepsSC:
@@ -86,6 +87,7 @@ bool VPRecipeBase::mayWriteToMemory() const {
8687
case VPWidenLoadEVLSC:
8788
case VPWidenLoadSC:
8889
case VPWidenPHISC:
90+
case VPWidenPointerInductionSC:
8991
case VPWidenSC:
9092
case VPWidenSelectSC: {
9193
const Instruction *I =
@@ -119,6 +121,7 @@ bool VPRecipeBase::mayReadFromMemory() const {
119121
case VPWidenIntrinsicSC:
120122
return cast<VPWidenIntrinsicRecipe>(this)->mayReadFromMemory();
121123
case VPBranchOnMaskSC:
124+
case VPDerivedIVSC:
122125
case VPFirstOrderRecurrencePHISC:
123126
case VPPredInstPHISC:
124127
case VPScalarIVStepsSC:
@@ -134,6 +137,7 @@ bool VPRecipeBase::mayReadFromMemory() const {
134137
case VPWidenGEPSC:
135138
case VPWidenIntOrFpInductionSC:
136139
case VPWidenPHISC:
140+
case VPWidenPointerInductionSC:
137141
case VPWidenSC:
138142
case VPWidenSelectSC: {
139143
const Instruction *I =

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,24 @@ bool VPlanTransforms::tryToConvertVPInstructionsToVPRecipes(
130130
return true;
131131
}
132132

133+
/// Return true if we do not know how to (mechanically) hoist or sink \p R out
134+
/// of a loop region.
135+
static bool cannotHoistOrSinkRecipe(const VPRecipeBase &R) {
136+
// Assumes don't alias anything or throw; as long as they're guaranteed to
137+
// execute, they're safe to hoist.
138+
if (match(&R, m_Intrinsic<Intrinsic::assume>()))
139+
return false;
140+
141+
// TODO: Relax checks in the future, e.g. we could also hoist reads, if their
142+
// memory location is not modified in the vector loop.
143+
if (R.mayHaveSideEffects() || R.mayReadFromMemory() || R.isPhi())
144+
return true;
145+
146+
// Allocas cannot be hoisted.
147+
auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
148+
return RepR && RepR->getOpcode() == Instruction::Alloca;
149+
}
150+
133151
static bool sinkScalarOperands(VPlan &Plan) {
134152
auto Iter = vp_depth_first_deep(Plan.getEntry());
135153
bool Changed = false;
@@ -1825,7 +1843,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
18251843
VPDT.properlyDominates(Previous, SinkCandidate))
18261844
return true;
18271845

1828-
if (SinkCandidate->mayHaveSideEffects())
1846+
if (cannotHoistOrSinkRecipe(*SinkCandidate))
18291847
return false;
18301848

18311849
WorkList.push_back(SinkCandidate);
@@ -1865,7 +1883,7 @@ sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR,
18651883
static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
18661884
VPRecipeBase *Previous,
18671885
VPDominatorTree &VPDT) {
1868-
if (Previous->mayHaveSideEffects() || Previous->mayReadFromMemory())
1886+
if (cannotHoistOrSinkRecipe(*Previous))
18691887
return false;
18701888

18711889
// Collect recipes that need hoisting.
@@ -1912,11 +1930,6 @@ static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
19121930
return nullptr;
19131931
return HoistCandidate;
19141932
};
1915-
auto CanHoist = [&](VPRecipeBase *HoistCandidate) {
1916-
// Avoid hoisting candidates with side-effects, as we do not yet analyze
1917-
// associated dependencies.
1918-
return !HoistCandidate->mayHaveSideEffects();
1919-
};
19201933

19211934
if (!NeedsHoisting(Previous->getVPSingleValue()))
19221935
return true;
@@ -1928,7 +1941,7 @@ static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR,
19281941
VPRecipeBase *Current = HoistCandidates[I];
19291942
assert(Current->getNumDefinedValues() == 1 &&
19301943
"only recipes with a single defined value expected");
1931-
if (!CanHoist(Current))
1944+
if (cannotHoistOrSinkRecipe(*Current))
19321945
return false;
19331946

19341947
for (VPValue *Op : Current->operands()) {
@@ -2143,24 +2156,6 @@ void VPlanTransforms::cse(VPlan &Plan) {
21432156
static void licm(VPlan &Plan) {
21442157
VPBasicBlock *Preheader = Plan.getVectorPreheader();
21452158

2146-
// Return true if we do not know how to (mechanically) hoist a given recipe
2147-
// out of a loop region.
2148-
auto CannotHoistRecipe = [](VPRecipeBase &R) {
2149-
// Assumes don't alias anything or throw; as long as they're guaranteed to
2150-
// execute, they're safe to hoist.
2151-
if (match(&R, m_Intrinsic<Intrinsic::assume>()))
2152-
return false;
2153-
2154-
// TODO: Relax checks in the future, e.g. we could also hoist reads, if
2155-
// their memory location is not modified in the vector loop.
2156-
if (R.mayHaveSideEffects() || R.mayReadFromMemory() || R.isPhi())
2157-
return true;
2158-
2159-
// Allocas cannot be hoisted.
2160-
auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
2161-
return RepR && RepR->getOpcode() == Instruction::Alloca;
2162-
};
2163-
21642159
// Hoist any loop invariant recipes from the vector loop region to the
21652160
// preheader. Preform a shallow traversal of the vector loop region, to
21662161
// exclude recipes in replicate regions. Since the top-level blocks in the
@@ -2172,7 +2167,7 @@ static void licm(VPlan &Plan) {
21722167
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
21732168
vp_depth_first_shallow(LoopRegion->getEntry()))) {
21742169
for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
2175-
if (CannotHoistRecipe(R))
2170+
if (cannotHoistOrSinkRecipe(R))
21762171
continue;
21772172
if (any_of(R.operands(), [](VPValue *Op) {
21782173
return !Op->isDefinedOutsideLoopRegions();

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

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -429,41 +429,18 @@ exit:
429429
define void @hoist_previous_value_and_operand_load(ptr %dst) {
430430
; CHECK-LABEL: @hoist_previous_value_and_operand_load(
431431
; CHECK-NEXT: entry:
432-
; CHECK-NEXT: br label [[VECTOR_PH:%.*]]
433-
; CHECK: vector.ph:
434-
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
435-
; CHECK: vector.body:
436-
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
437-
; CHECK-NEXT: [[VECTOR_RECUR:%.*]] = phi <4 x i32> [ <i32 poison, i32 poison, i32 poison, i32 1>, [[VECTOR_PH]] ], [ [[BROADCAST_SPLAT:%.*]], [[VECTOR_BODY]] ]
438-
; CHECK-NEXT: [[VECTOR_RECUR1:%.*]] = phi <4 x i32> [ <i32 poison, i32 poison, i32 poison, i32 0>, [[VECTOR_PH]] ], [ [[TMP3:%.*]], [[VECTOR_BODY]] ]
439-
; CHECK-NEXT: [[OFFSET_IDX:%.*]] = add i64 1, [[INDEX]]
440-
; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds i32, ptr [[DST:%.*]], i64 [[OFFSET_IDX]]
441-
; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[DST]], align 4
442-
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[TMP1]], i64 0
443-
; CHECK-NEXT: [[BROADCAST_SPLAT]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
444-
; CHECK-NEXT: [[TMP2:%.*]] = shufflevector <4 x i32> [[VECTOR_RECUR]], <4 x i32> [[BROADCAST_SPLAT]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
445-
; CHECK-NEXT: [[TMP3]] = or <4 x i32> [[TMP2]], splat (i32 3)
446-
; CHECK-NEXT: [[TMP4:%.*]] = shufflevector <4 x i32> [[VECTOR_RECUR1]], <4 x i32> [[TMP3]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
447-
; CHECK-NEXT: store <4 x i32> [[TMP4]], ptr [[TMP0]], align 4
448-
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 4
449-
; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i64 [[INDEX_NEXT]], 336
450-
; CHECK-NEXT: br i1 [[TMP5]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP7:![0-9]+]]
451-
; CHECK: middle.block:
452-
; CHECK-NEXT: [[VECTOR_RECUR_EXTRACT:%.*]] = extractelement <4 x i32> [[TMP3]], i32 3
453-
; CHECK-NEXT: br label [[SCALAR_PH:%.*]]
454-
; CHECK: scalar.ph:
455432
; CHECK-NEXT: br label [[LOOP:%.*]]
456433
; CHECK: loop:
457-
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 337, [[SCALAR_PH]] ], [ [[ADD:%.*]], [[LOOP]] ]
458-
; CHECK-NEXT: [[FOR_1:%.*]] = phi i32 [ [[TMP1]], [[SCALAR_PH]] ], [ [[LOAD:%.*]], [[LOOP]] ]
459-
; CHECK-NEXT: [[FOR_2:%.*]] = phi i32 [ [[VECTOR_RECUR_EXTRACT]], [[SCALAR_PH]] ], [ [[OR:%.*]], [[LOOP]] ]
434+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ 1, [[ENTRY:%.*]] ], [ [[ADD:%.*]], [[LOOP]] ]
435+
; CHECK-NEXT: [[FOR_1:%.*]] = phi i32 [ 1, [[ENTRY]] ], [ [[LOAD:%.*]], [[LOOP]] ]
436+
; CHECK-NEXT: [[FOR_2:%.*]] = phi i32 [ 0, [[ENTRY]] ], [ [[OR:%.*]], [[LOOP]] ]
460437
; CHECK-NEXT: [[OR]] = or i32 [[FOR_1]], 3
461438
; CHECK-NEXT: [[ADD]] = add i64 [[IV]], 1
462-
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, ptr [[DST]], i64 [[IV]]
439+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds i32, ptr [[DST:%.*]], i64 [[IV]]
463440
; CHECK-NEXT: store i32 [[FOR_2]], ptr [[GEP]], align 4
464441
; CHECK-NEXT: [[ICMP:%.*]] = icmp ult i64 [[IV]], 337
465442
; CHECK-NEXT: [[LOAD]] = load i32, ptr [[DST]], align 4
466-
; CHECK-NEXT: br i1 [[ICMP]], label [[LOOP]], label [[EXIT:%.*]], !llvm.loop [[LOOP8:![0-9]+]]
443+
; CHECK-NEXT: br i1 [[ICMP]], label [[LOOP]], label [[EXIT:%.*]]
467444
; CHECK: exit:
468445
; CHECK-NEXT: ret void
469446
;

0 commit comments

Comments
 (0)