Skip to content

[LoopPeel] Implement initial peeling off the last loop iteration. #139551

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 5 commits into from
May 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions llvm/include/llvm/Analysis/TargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ class TargetTransformInfo {
/// If the value is true the peeling cost model can decide to peel only
/// some iterations and in this case it will set this to false.
bool PeelProfiledIterations;

/// Peel off the last PeelCount loop iterations.
bool PeelLast;
};

/// Get target-customized preferences for the generic loop peeling
Expand Down
15 changes: 11 additions & 4 deletions llvm/include/llvm/Transforms/Utils/LoopPeel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ namespace llvm {

bool canPeel(const Loop *L);

/// Returns true if the last iteration of \p L can be peeled off. It makes sure
/// the loop exit condition can be adjusted when peeling and that the loop
/// executes at least 2 iterations.
bool canPeelLastIteration(const Loop &L, ScalarEvolution &SE);

/// VMap is the value-map that maps instructions from the original loop to
/// instructions in the last peeled-off iteration.
bool peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, ScalarEvolution *SE,
DominatorTree &DT, AssumptionCache *AC, bool PreserveLCSSA,
ValueToValueMapTy &VMap);
/// instructions in the last peeled-off iteration. If \p PeelLast is true, peel
/// off the last \p PeelCount iterations from \p L (canPeelLastIteration must be
/// true for \p L), otherwise peel off the first \p PeelCount iterations.
bool peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI,
ScalarEvolution *SE, DominatorTree &DT, AssumptionCache *AC,
bool PreserveLCSSA, ValueToValueMapTy &VMap);

TargetTransformInfo::PeelingPreferences
gatherPeelingPreferences(Loop *L, ScalarEvolution &SE,
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Scalar/LoopFuse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,8 @@ struct LoopFuser {
<< " iterations of the first loop. \n");

ValueToValueMapTy VMap;
FC0.Peeled = peelLoop(FC0.L, PeelCount, &LI, &SE, DT, &AC, true, VMap);
FC0.Peeled =
peelLoop(FC0.L, PeelCount, false, &LI, &SE, DT, &AC, true, VMap);
if (FC0.Peeled) {
LLVM_DEBUG(dbgs() << "Done Peeling\n");

Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1314,7 +1314,8 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
});

ValueToValueMapTy VMap;
if (peelLoop(L, PP.PeelCount, LI, &SE, DT, &AC, PreserveLCSSA, VMap)) {
if (peelLoop(L, PP.PeelCount, PP.PeelLast, LI, &SE, DT, &AC, PreserveLCSSA,
VMap)) {
simplifyLoopAfterUnroll(L, true, LI, &SE, &DT, &AC, &TTI, nullptr);
// If the loop was peeled, we already "used up" the profile information
// we had, so we don't want to unroll or peel again.
Expand Down
Loading