Skip to content
Merged
Show file tree
Hide file tree
Changes from 71 commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
f413520
[LoopPeel] Fix branch weights' effect on block frequencies
jdenny-ornl Mar 19, 2025
f821eeb
Run update_test_checks.py on a test
jdenny-ornl Mar 26, 2025
af8ec56
Fix typo
jdenny-ornl Apr 4, 2025
a0264ad
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Apr 8, 2025
fd29a49
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Apr 9, 2025
6303177
Document new metadata
jdenny-ornl Apr 10, 2025
bbd0e95
Improve LangRef.rst entry
jdenny-ornl May 1, 2025
715cb0a
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl May 5, 2025
67fa67d
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Jun 10, 2025
37ce859
Update fixmes
jdenny-ornl Jun 16, 2025
4337dcd
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Jun 16, 2025
5193158
Update test for AArch4, which I did not build before
jdenny-ornl Jun 17, 2025
bbd2f22
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Jul 10, 2025
b23f467
Run update script on test changed by merge from main
jdenny-ornl Jul 10, 2025
13d1fbb
[PGO] Add `llvm.loop.estimated_trip_count` metadata
jdenny-ornl Jul 15, 2025
e250cfc
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Jul 15, 2025
859b84d
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Jul 15, 2025
db5920a
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Jul 21, 2025
47fbe85
Add PGOEstimateTripCounts in more cases
jdenny-ornl Jul 21, 2025
f8097fb
Add unused initialization
jdenny-ornl Jul 21, 2025
7b27203
Simplify some test changes
jdenny-ornl Jul 22, 2025
4c4669a
Extend verify pass to cover new metadata
jdenny-ornl Jul 24, 2025
0f40efd
Fix test for some builds
jdenny-ornl Jul 24, 2025
2791a1c
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Jul 24, 2025
6148922
Apply some small reviewer suggestions
jdenny-ornl Jul 24, 2025
3f6a91a
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Jul 24, 2025
e5a0a26
Update for merge from pgo-estimated-trip-count
jdenny-ornl Jul 24, 2025
3a49b43
Attempt to fix windows pre-commit CI
jdenny-ornl Jul 24, 2025
c283ebe
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Jul 24, 2025
2f7daa8
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Jul 28, 2025
ecbf6e0
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Jul 28, 2025
c627fc5
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 7, 2025
f1fa8d9
Run update script on new test from last merge
jdenny-ornl Aug 7, 2025
38ace1e
Reapply 3a18fe33f0763cd9276c99c276448412100f6270
jdenny-ornl Aug 7, 2025
92ddaa0
Convert to function pass, avoid needless pass invalidation
jdenny-ornl Aug 8, 2025
a3e0d72
Fix layering violation
jdenny-ornl Aug 8, 2025
67f22cd
Apply clang-format
jdenny-ornl Aug 8, 2025
f0ff2e2
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 9, 2025
69fe051
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 9, 2025
e7eb1fe
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 13, 2025
e4f68c3
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 13, 2025
0973ab3
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 18, 2025
680bdc2
Remove PGOEstimateTripCountsPass and no-value form of metadata
jdenny-ornl Aug 18, 2025
83531b3
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 19, 2025
59cd184
Fix case where nested loops share latch
jdenny-ornl Aug 19, 2025
47051ce
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 19, 2025
5d00250
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Aug 25, 2025
5719779
Remove redundant code
jdenny-ornl Aug 25, 2025
98cab7b
Clarify recent comments some
jdenny-ornl Aug 25, 2025
3cbe07d
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Aug 25, 2025
b3831b6
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Sep 1, 2025
59ab013
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Sep 2, 2025
12ce70e
[LoopUnroll] Skip remainder loop guard if skip unrolled loop
jdenny-ornl Sep 2, 2025
415cb8f
Improve comments
jdenny-ornl Sep 3, 2025
b8aed9b
Merge branch 'main' into pgo-estimated-trip-count
jdenny-ornl Sep 9, 2025
5c9e43e
Merge branch 'pgo-estimated-trip-count' into fix-peel-branch-weights
jdenny-ornl Sep 9, 2025
cc3283d
Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
jdenny-ornl Sep 9, 2025
75a8df6
[LoopUnroll] Fix block frequencies when no runtime
jdenny-ornl Sep 9, 2025
83ac767
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Sep 15, 2025
1f81310
Empty commit to try to restart pre-commit CI
jdenny-ornl Sep 15, 2025
2382fbd
Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
jdenny-ornl Sep 15, 2025
967f8a1
Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
jdenny-ornl Sep 15, 2025
2897e64
Improve some code comments
jdenny-ornl Sep 16, 2025
5a99593
[LoopUnroll] Fix block frequencies for epilogue
jdenny-ornl Sep 16, 2025
a95ebd1
Revert accidental change from right before pushing
jdenny-ornl Sep 16, 2025
876e055
Remove xfails
jdenny-ornl Sep 17, 2025
eb00930
Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
jdenny-ornl Sep 17, 2025
04c8ade
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Sep 22, 2025
9b80c13
Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
jdenny-ornl Sep 22, 2025
9215f47
Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
jdenny-ornl Sep 22, 2025
712b282
Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
jdenny-ornl Sep 22, 2025
d85294e
Use BranchProbability
jdenny-ornl Sep 25, 2025
72c633f
Drop includes no longer needed
jdenny-ornl Sep 25, 2025
dc1f5f1
Clean up new LoopUtils comments
jdenny-ornl Sep 26, 2025
a1a5460
Merge branch 'main' into fix-peel-branch-weights
jdenny-ornl Sep 30, 2025
df9cf8c
Merge branch 'fix-peel-branch-weights' into skip-unroll-epilog-guard
jdenny-ornl Sep 30, 2025
99c95b1
Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
jdenny-ornl Sep 30, 2025
84e5223
Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
jdenny-ornl Sep 30, 2025
4353f1f
Merge branch 'main' into skip-unroll-epilog-guard
jdenny-ornl Oct 6, 2025
f66ae02
Merge branch 'skip-unroll-epilog-guard' into fix-blockfreq-unroll-no-…
jdenny-ornl Oct 6, 2025
0eeada8
Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
jdenny-ornl Oct 6, 2025
22fdacf
Merge branch 'main' into users/jdenny-ornl/fix-blockfreq-unroll-no-ru…
jdenny-ornl Oct 7, 2025
e57014c
Merge branch 'users/jdenny-ornl/fix-blockfreq-unroll-no-runtime' into…
jdenny-ornl Oct 7, 2025
f625d45
Merge branch 'main' into fix-blockfreq-unroll-no-runtime
jdenny-ornl Oct 13, 2025
a5a4a45
Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
jdenny-ornl Oct 13, 2025
943e95e
`+= 1` -> `++`
jdenny-ornl Oct 14, 2025
1e2307e
Use setBranchWeights
jdenny-ornl Oct 14, 2025
662e60f
Merge branch 'main' into fix-blockfreq-unroll-no-runtime
jdenny-ornl Oct 22, 2025
2ca587b
Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
jdenny-ornl Oct 22, 2025
093ad2a
Merge branch 'main' into fix-blockfreq-unroll-no-runtime
jdenny-ornl Oct 29, 2025
21c4f9c
Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
jdenny-ornl Oct 29, 2025
663cf2b
Merge branch 'main' into fix-blockfreq-unroll-no-runtime
jdenny-ornl Oct 30, 2025
5ed2dec
Merge branch 'fix-blockfreq-unroll-no-runtime' into fix-blockfreq-unr…
jdenny-ornl Oct 30, 2025
b95a9e2
Merge branch 'main' into users/jdenny-ornl/fix-blockfreq-unroll-epilogue
jdenny-ornl Oct 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions llvm/include/llvm/Transforms/Utils/LoopUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,38 @@ LLVM_ABI bool setLoopEstimatedTripCount(
Loop *L, unsigned EstimatedTripCount,
std::optional<unsigned> EstimatedLoopInvocationWeight = std::nullopt);

/// Based on branch weight metadata, return either:
/// - \c std::nullopt if the implementation is unable to handle the loop form
/// of \p L (e.g., \p L must have a latch block that controls the loop exit).
/// - Else, the estimated probability that, at the end of any iteration, the
/// latch of \p L will start another iteration. The result \c P is such that
/// `0 <= P <= 1`, and `1 - P` is the probability of exiting the loop.
std::optional<double> getLoopProbability(Loop *L);

/// Set branch weight metadata for the latch of \p L to indicate that, at the
/// end of any iteration, its estimated probability of starting another
/// iteration is \p P. Return false if the implementation is unable to handle
/// the loop form of \p L (e.g., \p L must have a latch block that controls the
/// loop exit). Otherwise, return true.
bool setLoopProbability(Loop *L, double P);

/// Based on branch weight metadata, return either:
/// - \c std::nullopt if the implementation cannot extract the probability
/// (e.g., \p B must have exactly two target labels, so it must be a
/// conditional branch).
/// - The probability \c P that control flows from \p B to its first target
/// label such that `1 - P` is the probability of control flowing to its
/// second target label, or vice-versa if \p ForFirstTarget is false.
std::optional<double> getBranchProbability(BranchInst *B, bool ForFirstTarget);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this in terms of double and not BranchProbability?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose BranchProbability works. Thanks. Done.


/// Set branch weight metadata for \p B to indicate that \p P and `1 - P` are
/// the probabilities of control flowing to its first and second target labels,
/// respectively, or vice-versa if \p ForFirstTarget is false. Return false if
/// the implementation cannot set the probability (e.g., \p B must have exactly
/// two target labels, so it must be a conditional branch). Otherwise, return
/// true.
bool setBranchProbability(BranchInst *B, double P, bool ForFirstTarget);

/// Check inner loop (L) backedge count is known to be invariant on all
/// iterations of its outer loop. If the loop has no parent, this is trivially
/// true.
Expand Down
4 changes: 3 additions & 1 deletion llvm/include/llvm/Transforms/Utils/UnrollLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ LLVM_ABI bool UnrollRuntimeLoopRemainder(
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
const TargetTransformInfo *TTI, bool PreserveLCSSA,
unsigned SCEVExpansionBudget, bool RuntimeUnrollMultiExit,
Loop **ResultLoop = nullptr);
Loop **ResultLoop = nullptr,
std::optional<unsigned> OriginalTripCount = std::nullopt,
std::optional<double> OriginalLoopProb = std::nullopt);

LLVM_ABI LoopUnrollResult UnrollAndJamLoop(
Loop *L, unsigned Count, unsigned TripCount, unsigned TripMultiple,
Expand Down
135 changes: 42 additions & 93 deletions llvm/lib/Transforms/Utils/LoopPeel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,84 +883,6 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
}
}

struct WeightInfo {
// Weights for current iteration.
SmallVector<uint32_t> Weights;
// Weights to subtract after each iteration.
const SmallVector<uint32_t> SubWeights;
};

/// Update the branch weights of an exiting block of a peeled-off loop
/// iteration.
/// Let F is a weight of the edge to continue (fallthrough) into the loop.
/// Let E is a weight of the edge to an exit.
/// F/(F+E) is a probability to go to loop and E/(F+E) is a probability to
/// go to exit.
/// Then, Estimated ExitCount = F / E.
/// For I-th (counting from 0) peeled off iteration we set the weights for
/// the peeled exit as (EC - I, 1). It gives us reasonable distribution,
/// The probability to go to exit 1/(EC-I) increases. At the same time
/// the estimated exit count in the remainder loop reduces by I.
/// To avoid dealing with division rounding we can just multiple both part
/// of weights to E and use weight as (F - I * E, E).
static void updateBranchWeights(Instruction *Term, WeightInfo &Info) {
setBranchWeights(*Term, Info.Weights, /*IsExpected=*/false);
for (auto [Idx, SubWeight] : enumerate(Info.SubWeights))
if (SubWeight != 0)
// Don't set the probability of taking the edge from latch to loop header
// to less than 1:1 ratio (meaning Weight should not be lower than
// SubWeight), as this could significantly reduce the loop's hotness,
// which would be incorrect in the case of underestimating the trip count.
Info.Weights[Idx] =
Info.Weights[Idx] > SubWeight
? std::max(Info.Weights[Idx] - SubWeight, SubWeight)
: SubWeight;
}

/// Initialize the weights for all exiting blocks.
static void initBranchWeights(DenseMap<Instruction *, WeightInfo> &WeightInfos,
Loop *L) {
SmallVector<BasicBlock *> ExitingBlocks;
L->getExitingBlocks(ExitingBlocks);
for (BasicBlock *ExitingBlock : ExitingBlocks) {
Instruction *Term = ExitingBlock->getTerminator();
SmallVector<uint32_t> Weights;
if (!extractBranchWeights(*Term, Weights))
continue;

// See the comment on updateBranchWeights() for an explanation of what we
// do here.
uint32_t FallThroughWeights = 0;
uint32_t ExitWeights = 0;
for (auto [Succ, Weight] : zip(successors(Term), Weights)) {
if (L->contains(Succ))
FallThroughWeights += Weight;
else
ExitWeights += Weight;
}

// Don't try to update weights for degenerate case.
if (FallThroughWeights == 0)
continue;

SmallVector<uint32_t> SubWeights;
for (auto [Succ, Weight] : zip(successors(Term), Weights)) {
if (!L->contains(Succ)) {
// Exit weights stay the same.
SubWeights.push_back(0);
continue;
}

// Subtract exit weights on each iteration, distributed across all
// fallthrough edges.
double W = (double)Weight / (double)FallThroughWeights;
SubWeights.push_back((uint32_t)(ExitWeights * W));
}

WeightInfos.insert({Term, {std::move(Weights), std::move(SubWeights)}});
}
}

/// Clones the body of the loop L, putting it between \p InsertTop and \p
/// InsertBot.
/// \param IterNumber The serial number of the iteration currently being
Expand Down Expand Up @@ -1332,11 +1254,6 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI,
Instruction *LatchTerm =
cast<Instruction>(cast<BasicBlock>(Latch)->getTerminator());

// If we have branch weight information, we'll want to update it for the
// newly created branches.
DenseMap<Instruction *, WeightInfo> Weights;
initBranchWeights(Weights, L);

// Identify what noalias metadata is inside the loop: if it is inside the
// loop, the associated metadata must be cloned for each iteration.
SmallVector<MDNode *, 6> LoopLocalNoAliasDeclScopes;
Expand Down Expand Up @@ -1382,11 +1299,6 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI,
assert(DT.verify(DominatorTree::VerificationLevel::Fast));
#endif

for (auto &[Term, Info] : Weights) {
auto *TermCopy = cast<Instruction>(VMap[Term]);
updateBranchWeights(TermCopy, Info);
}

// Remove Loop metadata from the latch branch instruction
// because it is not the Loop's latch branch anymore.
auto *LatchTermCopy = cast<Instruction>(VMap[LatchTerm]);
Expand Down Expand Up @@ -1426,15 +1338,52 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI,
}
}

for (const auto &[Term, Info] : Weights) {
setBranchWeights(*Term, Info.Weights, /*IsExpected=*/false);
}

// Update Metadata for count of peeled off iterations.
unsigned AlreadyPeeled = 0;
if (auto Peeled = getOptionalIntLoopAttribute(L, PeeledCountMetaData))
AlreadyPeeled = *Peeled;
addStringMetadataToLoop(L, PeeledCountMetaData, AlreadyPeeled + PeelCount);
unsigned TotalPeeled = AlreadyPeeled + PeelCount;
addStringMetadataToLoop(L, PeeledCountMetaData, TotalPeeled);

// Update metadata for the estimated trip count. The original branch weight
// metadata is already correct for both the remaining loop and the peeled loop
// iterations, so don't adjust it.
//
// For example, consider what happens when peeling 2 iterations from a loop
// with an estimated trip count of 10 and inserting them before the remaining
// loop. Each of the peeled iterations and each iteration in the remaining
// loop still has the same probability of exiting the *entire original* loop
// as it did when in the original loop, and thus it should still have the same
// branch weights. The peeled iterations' non-zero probabilities of exiting
// already appropriately reduce the probability of reaching the remaining
// iterations just as they did in the original loop. Trying to also adjust
// the remaining loop's branch weights to reflect its new trip count of 8 will
// erroneously further reduce its block frequencies. However, in case an
// analysis later needs to determine the trip count of the remaining loop
// while examining it in isolation without considering the probability of
// actually reaching it, we store the new trip count as separate metadata.
if (auto EstimatedTripCount = getLoopEstimatedTripCount(L)) {
// FIXME: The previous updateBranchWeights implementation had this
// comment:
//
// Don't set the probability of taking the edge from latch to loop header
// to less than 1:1 ratio (meaning Weight should not be lower than
// SubWeight), as this could significantly reduce the loop's hotness,
// which would be incorrect in the case of underestimating the trip count.
//
// See e8d5db206c2f commit log for further discussion. That seems to
// suggest that we should avoid ever setting a trip count of < 2 here
// (equal chance of continuing and exiting means the loop will likely
// continue once and then exit once). Or is keeping the original branch
// weights already a sufficient improvement for whatever analysis cares
// about this case?
unsigned EstimatedTripCountNew = *EstimatedTripCount;
if (EstimatedTripCountNew < TotalPeeled) // FIXME: TotalPeeled + 2?
EstimatedTripCountNew = 0; // FIXME: = 2?
else
EstimatedTripCountNew -= TotalPeeled;
setLoopEstimatedTripCount(L, EstimatedTripCountNew);
}

if (Loop *ParentLoop = L->getParentLoop())
L = ParentLoop;
Expand Down
60 changes: 48 additions & 12 deletions llvm/lib/Transforms/Utils/LoopUnroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "llvm/Transforms/Utils/UnrollLoop.h"
#include "llvm/Transforms/Utils/ValueMapper.h"
#include <assert.h>
#include <cmath>
#include <numeric>
#include <type_traits>
#include <vector>
Expand Down Expand Up @@ -499,9 +500,9 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,

const unsigned MaxTripCount = SE->getSmallConstantMaxTripCount(L);
const bool MaxOrZero = SE->isBackedgeTakenCountMaxOrZero(L);
unsigned EstimatedLoopInvocationWeight = 0;
std::optional<unsigned> OriginalTripCount =
llvm::getLoopEstimatedTripCount(L, &EstimatedLoopInvocationWeight);
llvm::getLoopEstimatedTripCount(L);
std::optional<double> OriginalLoopProb = llvm::getLoopProbability(L);

// Effectively "DCE" unrolled iterations that are beyond the max tripcount
// and will never be executed.
Expand Down Expand Up @@ -592,11 +593,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 {
Expand Down Expand Up @@ -1131,11 +1132,46 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
LI->erase(L);
// We shouldn't try to use `L` anymore.
L = nullptr;
} else if (OriginalTripCount) {
// Update the trip count. Note that the remainder has already logic
// computing it in `UnrollRuntimeLoopRemainder`.
setLoopEstimatedTripCount(L, *OriginalTripCount / ULO.Count,
EstimatedLoopInvocationWeight);
} 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: Handle prologue loops.
// - Otherwise, if unrolled loop iteration latches become unconditional,
// branch weights are adjusted above. FIXME: Actually handle such
// unconditional latches.
// - Otherwise, the original loop's branch weights are correct for the
// unrolled loop, so do not adjust them.
// - In all cases, the unrolled loop's estimated trip count is set below.
//
// As an example of the last case, consider what happens if the unroll count
// is 4 for a loop with an estimated trip count of 10 when we do not create
// a remainder loop and all iterations' latches remain conditional. Each
// unrolled iteration's latch still has the same probability of exiting the
// loop as it did when in the original loop, and thus it should still have
// the same branch weights. Each unrolled iteration's non-zero probability
// of exiting already appropriately reduces the probability of reaching the
// remaining iterations just as it did in the original loop. Trying to also
// adjust the branch weights of the final unrolled iteration's latch (i.e.,
// the backedge for the unrolled loop as a whole) to reflect its new trip
// count of 3 will erroneously further reduce its block frequencies.
// However, in case an analysis later needs to estimate the trip count of
// 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.
if (OriginalLoopProb && 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, pow(*OriginalLoopProb, ULO.Count));
}
if (OriginalTripCount) {
unsigned NewTripCount = *OriginalTripCount / ULO.Count;
if (!ULO.Runtime && *OriginalTripCount % ULO.Count)
NewTripCount += 1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: ++NewTripCount?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Done. I was thinking in Python, I guess.

setLoopEstimatedTripCount(L, NewTripCount);
}
}

// LoopInfo should not be valid, confirm that.
Expand Down
Loading
Loading