Skip to content

Commit 2404477

Browse files
authored
LoopVectorize: Add better heuristic for vectorized epilogue skip test (#72589)
This is a follow-up to PR #72450 correcting the branch_weights used for the test whether the vectorized epilogue loop should be skipped.
1 parent 47a3ad5 commit 2404477

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,6 @@ static constexpr uint32_t MemCheckBypassWeights[] = {1, 127};
407407
// Likelyhood of bypassing the vectorized loop because there are zero trips left
408408
// after prolog. See `emitIterationCountCheck`.
409409
static constexpr uint32_t MinItersBypassWeights[] = {1, 127};
410-
// Likelyhood of bypassing the vectorized loop because of zero trips necessary.
411-
// See `emitMinimumVectorEpilogueIterCountCheck`.
412-
static constexpr uint32_t EpilogueMinItersBypassWeights[] = {1, 127};
413410

414411
/// A helper function that returns true if the given type is irregular. The
415412
/// type is irregular if its allocated size doesn't equal the store size of an
@@ -3163,9 +3160,8 @@ BasicBlock *InnerLoopVectorizer::completeLoopSkeleton() {
31633160
// Assume that `Count % VectorTripCount` is equally distributed.
31643161
unsigned TripCount = UF * VF.getKnownMinValue();
31653162
assert(TripCount > 0 && "trip count should not be zero");
3166-
MDBuilder MDB(ScalarLatchTerm->getContext());
3167-
MDNode *BranchWeights = MDB.createBranchWeights(1, TripCount - 1);
3168-
BI.setMetadata(LLVMContext::MD_prof, BranchWeights);
3163+
const uint32_t Weights[] = {1, TripCount - 1};
3164+
setBranchWeights(BI, Weights);
31693165
}
31703166
}
31713167

@@ -8093,8 +8089,19 @@ EpilogueVectorizerEpilogueLoop::emitMinimumVectorEpilogueIterCountCheck(
80938089

80948090
BranchInst &BI =
80958091
*BranchInst::Create(Bypass, LoopVectorPreHeader, CheckMinIters);
8096-
if (hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator()))
8097-
setBranchWeights(BI, EpilogueMinItersBypassWeights);
8092+
if (hasBranchWeightMD(*OrigLoop->getLoopLatch()->getTerminator())) {
8093+
unsigned MainLoopStep = UF * VF.getKnownMinValue();
8094+
unsigned EpilogueLoopStep =
8095+
EPI.EpilogueUF * EPI.EpilogueVF.getKnownMinValue();
8096+
// We assume the remaining `Count` is equally distributed in
8097+
// [0, MainLoopStep)
8098+
// So the probability for `Count < EpilogueLoopStep` should be
8099+
// min(MainLoopStep, EpilogueLoopStep) / MainLoopStep
8100+
unsigned EstimatedSkipCount = std::min(MainLoopStep, EpilogueLoopStep);
8101+
const uint32_t Weights[] = {EstimatedSkipCount,
8102+
MainLoopStep - EstimatedSkipCount};
8103+
setBranchWeights(BI, Weights);
8104+
}
80988105
ReplaceInstWithInst(Insert->getTerminator(), &BI);
80998106

81008107
LoopBypassBlocks.push_back(Insert);

llvm/test/Transforms/LoopVectorize/branch-weights.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
; CHECK: br i1 %cmp.n, label %exit.loopexit, label %vec.epilog.iter.check, !prof [[PROF_F0_MIDDLE_BLOCKS:![0-9]+]]
2525
;
2626
; CHECK: vec.epilog.iter.check:
27-
; CHECK: br i1 %min.epilog.iters.check, label %vec.epilog.scalar.ph, label %vec.epilog.ph, !prof [[PROF_F0_UNLIKELY]]
27+
; CHECK: br i1 %min.epilog.iters.check, label %vec.epilog.scalar.ph, label %vec.epilog.ph, !prof [[PROF_F0_VEC_EPILOGUE_SKIP:![0-9]+]]
2828
;
2929
; CHECK: vec.epilog.ph:
3030
; CHECK: br label %vec.epilog.vector.body
@@ -77,5 +77,6 @@ exit:
7777
; CHECK: [[PROF_F0_UNLIKELY]] = !{!"branch_weights", i32 1, i32 127}
7878
; CEHCK: [[PROF_F0_VECTOR_BODY]] = !{!"branch_weights", i32 1, i32 307}
7979
; CHECK: [[PROF_F0_MIDDLE_BLOCKS]] = !{!"branch_weights", i32 1, i32 3}
80+
; CHECK: [[PROF_F0_VEC_EPILOGUE_SKIP]] = !{!"branch_weights", i32 4, i32 0}
8081
; CHECK: [[PROF_F0_VEC_EPILOG_VECTOR_BODY]] = !{!"branch_weights", i32 0, i32 0}
8182
; CEHCK: [[PROF_F0_LOOP]] = !{!"branch_weights", i32 2, i32 1}

0 commit comments

Comments
 (0)