diff options
| author | Joel E. Denny <jdenny.ornl@gmail.com> | 2025-10-31 11:01:42 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-31 11:01:42 -0400 |
| commit | cc8ff73fbab875e33071b23ff6e4b512d5adf64e (patch) | |
| tree | 8d87b9c6fca0ec9e0f0094dc21f9afda240ceaa0 /llvm/lib/Transforms/Utils/LoopUnroll.cpp | |
| parent | 37e7ef0998b0b79fefd811a807d24d9d71033239 (diff) | |
| download | llvm-cc8ff73fbab875e33071b23ff6e4b512d5adf64e.zip llvm-cc8ff73fbab875e33071b23ff6e4b512d5adf64e.tar.gz llvm-cc8ff73fbab875e33071b23ff6e4b512d5adf64e.tar.bz2 | |
[LoopUnroll] Fix block frequencies for epilogue (#159163)
As another step in issue #135812, this patch fixes block frequencies for
partial loop unrolling with an epilogue remainder loop. It does not
fully handle the case when the epilogue loop itself is unrolled. That
will be handled in the next patch.
For the guard and latch of each of the unrolled loop and epilogue loop,
this patch sets branch weights derived directly from the original loop
latch branch weights. The total frequency of the original loop body,
summed across all its occurrences in the unrolled loop and epilogue
loop, is the same as in the original loop. This patch also sets
`llvm.loop.estimated_trip_count` for the epilogue loop instead of
relying on the epilogue's latch branch weights to imply it.
This patch fixes branch weights in tests that PR #157754 adversely
affected.
Diffstat (limited to 'llvm/lib/Transforms/Utils/LoopUnroll.cpp')
| -rw-r--r-- | llvm/lib/Transforms/Utils/LoopUnroll.cpp | 30 |
1 files changed, 19 insertions, 11 deletions
diff --git a/llvm/lib/Transforms/Utils/LoopUnroll.cpp b/llvm/lib/Transforms/Utils/LoopUnroll.cpp index 2368644..94dfd3a 100644 --- a/llvm/lib/Transforms/Utils/LoopUnroll.cpp +++ b/llvm/lib/Transforms/Utils/LoopUnroll.cpp @@ -501,6 +501,7 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, const bool MaxOrZero = SE->isBackedgeTakenCountMaxOrZero(L); std::optional<unsigned> OriginalTripCount = llvm::getLoopEstimatedTripCount(L); + BranchProbability OriginalLoopProb = llvm::getLoopProbability(L); // Effectively "DCE" unrolled iterations that are beyond the max tripcount // and will never be executed. @@ -591,11 +592,11 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, : isEpilogProfitable(L); if (ULO.Runtime && - !UnrollRuntimeLoopRemainder(L, ULO.Count, ULO.AllowExpensiveTripCount, - EpilogProfitability, ULO.UnrollRemainder, - ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI, - PreserveLCSSA, ULO.SCEVExpansionBudget, - ULO.RuntimeUnrollMultiExit, RemainderLoop)) { + !UnrollRuntimeLoopRemainder( + L, ULO.Count, ULO.AllowExpensiveTripCount, EpilogProfitability, + ULO.UnrollRemainder, ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI, + PreserveLCSSA, ULO.SCEVExpansionBudget, ULO.RuntimeUnrollMultiExit, + RemainderLoop, OriginalTripCount, OriginalLoopProb)) { if (ULO.Force) ULO.Runtime = false; else { @@ -1129,13 +1130,13 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, LI->erase(L); // We shouldn't try to use `L` anymore. L = nullptr; - } else if (OriginalTripCount) { + } else { // Update metadata for the loop's branch weights and estimated trip count: // - If ULO.Runtime, UnrollRuntimeLoopRemainder sets the guard branch // weights, latch branch weights, and estimated trip count of the // remainder loop it creates. It also sets the branch weights for the // unrolled loop guard it creates. The branch weights for the unrolled - // loop latch are adjusted below. FIXME: Actually handle ULO.Runtime. + // loop latch are adjusted below. FIXME: Handle prologue loops. // - Otherwise, if unrolled loop iteration latches become unconditional, // branch weights are adjusted above. FIXME: Actually handle such // unconditional latches. @@ -1158,10 +1159,17 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI, // the unrolled loop as a whole without considering the branch weights for // each unrolled iteration's latch within it, we store the new trip count as // separate metadata. - unsigned NewTripCount = *OriginalTripCount / ULO.Count; - if (!ULO.Runtime && *OriginalTripCount % ULO.Count) - NewTripCount += 1; - setLoopEstimatedTripCount(L, NewTripCount); + if (!OriginalLoopProb.isUnknown() && ULO.Runtime && EpilogProfitability) { + // Where p is always the probability of executing at least 1 more + // iteration, the probability for at least n more iterations is p^n. + setLoopProbability(L, OriginalLoopProb.pow(ULO.Count)); + } + if (OriginalTripCount) { + unsigned NewTripCount = *OriginalTripCount / ULO.Count; + if (!ULO.Runtime && *OriginalTripCount % ULO.Count) + ++NewTripCount; + setLoopEstimatedTripCount(L, NewTripCount); + } } // LoopInfo should not be valid, confirm that. |
