-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[LoopVectorize] Further improve cost model for early exit loops #126235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3a1f06c
[LoopVectorize] Further improve cost model for early exit loops
david-arm 33272b0
Address review comments
david-arm 0e082a2
Address review comments
david-arm b22497f
Fix test after rebase
david-arm 4afbfd0
Address review comments
david-arm 08c1f5b
Address review comment
david-arm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10171,19 +10171,56 @@ static void checkMixedPrecision(Loop *L, OptimizationRemarkEmitter *ORE) { | |
} | ||
} | ||
|
||
static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks, | ||
VectorizationFactor &VF, Loop *L, | ||
PredicatedScalarEvolution &PSE, | ||
ScalarEpilogueLowering SEL, | ||
std::optional<unsigned> VScale) { | ||
InstructionCost CheckCost = Checks.getCost(); | ||
if (!CheckCost.isValid()) | ||
/// For loops with uncountable early exits, find the cost of doing work when | ||
/// exiting the loop early, such as calculating the final exit values of | ||
/// variables used outside the loop. | ||
/// TODO: This is currently overly pessimistic because the loop may not take | ||
/// the early exit, but better to keep this conservative for now. In future, | ||
/// it might be possible to relax this by using branch probabilities. | ||
static InstructionCost calculateEarlyExitCost(VPCostContext &CostCtx, | ||
VPlan &Plan, ElementCount VF) { | ||
InstructionCost Cost = 0; | ||
for (auto *ExitVPBB : Plan.getExitBlocks()) { | ||
for (auto *PredVPBB : ExitVPBB->getPredecessors()) { | ||
// If the predecessor is not the middle.block, then it must be the | ||
// vector.early.exit block, which may contain work to calculate the exit | ||
// values of variables used outside the loop. | ||
if (PredVPBB != Plan.getMiddleBlock()) { | ||
LLVM_DEBUG(dbgs() << "Calculating cost of work in exit block " | ||
<< PredVPBB->getName() << ":\n"); | ||
Cost += PredVPBB->cost(VF, CostCtx); | ||
} | ||
} | ||
} | ||
return Cost; | ||
} | ||
|
||
/// This function determines whether or not it's still profitable to vectorize | ||
/// the loop given the extra work we have to do outside of the loop: | ||
/// 1. Perform the runtime checks before entering the loop to ensure it's safe | ||
/// to vectorize. | ||
/// 2. In the case of loops with uncountable early exits, we may have to do | ||
/// extra work when exiting the loop early, such as calculating the final | ||
/// exit values of variables used outside the loop. | ||
static bool isOutsideLoopWorkProfitable(GeneratedRTChecks &Checks, | ||
VectorizationFactor &VF, Loop *L, | ||
PredicatedScalarEvolution &PSE, | ||
VPCostContext &CostCtx, VPlan &Plan, | ||
ScalarEpilogueLowering SEL, | ||
std::optional<unsigned> VScale) { | ||
InstructionCost TotalCost = Checks.getCost(); | ||
if (!TotalCost.isValid()) | ||
return false; | ||
|
||
// Add on the cost of any work required in the vector early exit block, if | ||
// one exists. | ||
TotalCost += calculateEarlyExitCost(CostCtx, Plan, VF.Width); | ||
|
||
// When interleaving only scalar and vector cost will be equal, which in turn | ||
// would lead to a divide by 0. Fall back to hard threshold. | ||
if (VF.Width.isScalar()) { | ||
if (CheckCost > VectorizeMemoryCheckThreshold) { | ||
// TODO: Should we rename VectorizeMemoryCheckThreshold? | ||
if (TotalCost > VectorizeMemoryCheckThreshold) { | ||
LLVM_DEBUG( | ||
dbgs() | ||
<< "LV: Interleaving only is not profitable due to runtime checks\n"); | ||
|
@@ -10209,7 +10246,9 @@ static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks, | |
// The total cost of the vector loop is | ||
// RtC + VecC * (TC / VF) + EpiC | ||
// where | ||
// * RtC is the cost of the generated runtime checks | ||
// * RtC is the cost of the generated runtime checks plus the cost of | ||
// performing any additional work in the vector.early.exit block for loops | ||
// with uncountable early exits. | ||
// * VecC is the cost of a single vector iteration. | ||
// * TC is the actual trip count of the loop | ||
// * VF is the vectorization factor | ||
|
@@ -10227,7 +10266,7 @@ static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks, | |
// the computations are performed on doubles, not integers and the result | ||
// is rounded up, hence we get an upper estimate of the TC. | ||
unsigned IntVF = getEstimatedRuntimeVF(VF.Width, VScale); | ||
uint64_t RtC = *CheckCost.getValue(); | ||
uint64_t RtC = *TotalCost.getValue(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reference to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
uint64_t Div = ScalarC * IntVF - *VF.Cost.getValue(); | ||
uint64_t MinTC1 = Div == 0 ? 0 : divideCeil(RtC * IntVF, Div); | ||
|
||
|
@@ -10555,8 +10594,8 @@ bool LoopVectorizePass::processLoop(Loop *L) { | |
// iteration count is low. However, setting the epilogue policy to | ||
// `CM_ScalarEpilogueNotAllowedLowTripLoop` prevents vectorizing loops | ||
// with runtime checks. It's more effective to let | ||
// `areRuntimeChecksProfitable` determine if vectorization is beneficial | ||
// for the loop. | ||
// `isOutsideLoopWorkProfitable` determine if vectorization is | ||
// beneficial for the loop. | ||
if (SEL != CM_ScalarEpilogueNotNeededUsePredicate) | ||
SEL = CM_ScalarEpilogueNotAllowedLowTripLoop; | ||
} else { | ||
|
@@ -10654,9 +10693,12 @@ bool LoopVectorizePass::processLoop(Loop *L) { | |
// Check if it is profitable to vectorize with runtime checks. | ||
bool ForceVectorization = | ||
Hints.getForce() == LoopVectorizeHints::FK_Enabled; | ||
VPCostContext CostCtx(CM.TTI, *CM.TLI, CM.Legal->getWidestInductionType(), | ||
CM, CM.CostKind); | ||
if (!ForceVectorization && | ||
!areRuntimeChecksProfitable(Checks, VF, L, PSE, SEL, | ||
CM.getVScaleForTuning())) { | ||
!isOutsideLoopWorkProfitable(Checks, VF, L, PSE, CostCtx, | ||
LVP.getPlanFor(VF.Width), SEL, | ||
CM.getVScaleForTuning())) { | ||
ORE->emit([&]() { | ||
return OptimizationRemarkAnalysisAliasing( | ||
DEBUG_TYPE, "CantReorderMemOps", L->getStartLoc(), | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
llvm/test/Transforms/LoopVectorize/AArch64/early_exit_costs.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 4 | ||
; REQUIRES: asserts | ||
; RUN: opt -S < %s -p loop-vectorize -enable-early-exit-vectorization -disable-output \ | ||
; RUN: -debug-only=loop-vectorize 2>&1 | FileCheck %s --check-prefixes=CHECK | ||
|
||
target triple = "aarch64-unknown-linux-gnu" | ||
|
||
declare void @init_mem(ptr, i64); | ||
|
||
define i64 @same_exit_block_pre_inc_use1_sve() #1 { | ||
; CHECK-LABEL: LV: Checking a loop in 'same_exit_block_pre_inc_use1_sve' | ||
; CHECK: LV: Selecting VF: vscale x 16 | ||
; CHECK: Calculating cost of work in exit block vector.early.exit | ||
; CHECK-NEXT: Cost of 6 for VF vscale x 16: EMIT vp<{{.*}}> = extract-first-active | ||
; CHECK-NEXT: Cost of 6 for VF vscale x 16: EMIT vp<{{.*}}> = extract-first-active | ||
; CHECK: LV: Minimum required TC for runtime checks to be profitable:32 | ||
entry: | ||
%p1 = alloca [1024 x i8] | ||
%p2 = alloca [1024 x i8] | ||
call void @init_mem(ptr %p1, i64 1024) | ||
call void @init_mem(ptr %p2, i64 1024) | ||
br label %loop | ||
|
||
loop: | ||
%index = phi i64 [ %index.next, %loop.inc ], [ 3, %entry ] | ||
%index2 = phi i64 [ %index2.next, %loop.inc ], [ 15, %entry ] | ||
%arrayidx = getelementptr inbounds i8, ptr %p1, i64 %index | ||
%ld1 = load i8, ptr %arrayidx, align 1 | ||
%arrayidx1 = getelementptr inbounds i8, ptr %p2, i64 %index | ||
%ld2 = load i8, ptr %arrayidx1, align 1 | ||
%cmp3 = icmp eq i8 %ld1, %ld2 | ||
br i1 %cmp3, label %loop.inc, label %loop.end | ||
|
||
loop.inc: | ||
%index.next = add i64 %index, 1 | ||
%index2.next = add i64 %index2, 2 | ||
%exitcond = icmp ne i64 %index.next, 67 | ||
br i1 %exitcond, label %loop, label %loop.end | ||
|
||
loop.end: | ||
%val1 = phi i64 [ %index, %loop ], [ 67, %loop.inc ] | ||
%val2 = phi i64 [ %index2, %loop ], [ 98, %loop.inc ] | ||
%retval = add i64 %val1, %val2 | ||
ret i64 %retval | ||
} | ||
|
||
define i64 @same_exit_block_pre_inc_use1_nosve() { | ||
; CHECK-LABEL: LV: Checking a loop in 'same_exit_block_pre_inc_use1_nosve' | ||
; CHECK: LV: Selecting VF: 16 | ||
; CHECK: Calculating cost of work in exit block vector.early.exit | ||
; CHECK-NEXT: Cost of 50 for VF 16: EMIT vp<{{.*}}> = extract-first-active | ||
; CHECK-NEXT: Cost of 50 for VF 16: EMIT vp<{{.*}}> = extract-first-active | ||
; CHECK: LV: Minimum required TC for runtime checks to be profitable:176 | ||
; CHECK-NEXT: LV: Vectorization is not beneficial: expected trip count < minimum profitable VF (64 < 176) | ||
; CHECK-NEXT: LV: Too many memory checks needed. | ||
entry: | ||
%p1 = alloca [1024 x i8] | ||
%p2 = alloca [1024 x i8] | ||
call void @init_mem(ptr %p1, i64 1024) | ||
call void @init_mem(ptr %p2, i64 1024) | ||
br label %loop | ||
|
||
loop: | ||
%index = phi i64 [ %index.next, %loop.inc ], [ 3, %entry ] | ||
%index2 = phi i64 [ %index2.next, %loop.inc ], [ 15, %entry ] | ||
%arrayidx = getelementptr inbounds i8, ptr %p1, i64 %index | ||
%ld1 = load i8, ptr %arrayidx, align 1 | ||
%arrayidx1 = getelementptr inbounds i8, ptr %p2, i64 %index | ||
%ld2 = load i8, ptr %arrayidx1, align 1 | ||
%cmp3 = icmp eq i8 %ld1, %ld2 | ||
br i1 %cmp3, label %loop.inc, label %loop.end | ||
|
||
loop.inc: | ||
%index.next = add i64 %index, 1 | ||
%index2.next = add i64 %index2, 2 | ||
%exitcond = icmp ne i64 %index.next, 67 | ||
br i1 %exitcond, label %loop, label %loop.end | ||
|
||
loop.end: | ||
%val1 = phi i64 [ %index, %loop ], [ 67, %loop.inc ] | ||
%val2 = phi i64 [ %index2, %loop ], [ 98, %loop.inc ] | ||
%retval = add i64 %val1, %val2 | ||
ret i64 %retval | ||
} | ||
|
||
attributes #1 = { "target-features"="+sve" vscale_range(1,16) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you document this, now that this does more than checking runtime checks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done