Skip to content

Commit 9a18a6f

Browse files
committed
Disable loop peeling during full unrolling pass.
Summary: Peeling should not occur during the full unrolling invocation early in the pipeline, but rather later with partial and runtime loop unrolling. The later loop unrolling invocation will also eventually utilize profile summary and branch frequency information, which we would like to use to control peeling. And for ThinLTO we want to delay peeling until the backend (post thin link) phase, just as we do for most types of unrolling. Ensure peeling doesn't occur during the full unrolling invocation by adding a parameter to the shared implementation function, similar to the way partial and runtime loop unrolling are disabled. Performance results for ThinLTO suggest this has a neutral to positive effect on some internal benchmarks. Reviewers: chandlerc, davidxl Subscribers: mzolotukhin, llvm-commits, mehdi_amini Differential Revision: https://reviews.llvm.org/D36258 llvm-svn: 309966
1 parent f58df39 commit 9a18a6f

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

llvm/include/llvm/Transforms/Scalar.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ Pass *createLoopInstSimplifyPass();
184184
//
185185
Pass *createLoopUnrollPass(int OptLevel = 2, int Threshold = -1, int Count = -1,
186186
int AllowPartial = -1, int Runtime = -1,
187-
int UpperBound = -1);
187+
int UpperBound = -1, int AllowPeeling = -1);
188188
// Create an unrolling pass for full unrolling that uses exact trip count only.
189189
Pass *createSimpleLoopUnrollPass(int OptLevel = 2);
190190

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ static TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences(
134134
Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI, int OptLevel,
135135
Optional<unsigned> UserThreshold, Optional<unsigned> UserCount,
136136
Optional<bool> UserAllowPartial, Optional<bool> UserRuntime,
137-
Optional<bool> UserUpperBound) {
137+
Optional<bool> UserUpperBound, Optional<bool> UserAllowPeeling) {
138138
TargetTransformInfo::UnrollingPreferences UP;
139139

140140
// Set up the defaults
@@ -201,6 +201,8 @@ static TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences(
201201
UP.Runtime = *UserRuntime;
202202
if (UserUpperBound.hasValue())
203203
UP.UpperBound = *UserUpperBound;
204+
if (UserAllowPeeling.hasValue())
205+
UP.AllowPeeling = *UserAllowPeeling;
204206

205207
return UP;
206208
}
@@ -927,15 +929,13 @@ static bool computeUnrollCount(
927929
return ExplicitUnroll;
928930
}
929931

930-
static bool tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
931-
ScalarEvolution &SE, const TargetTransformInfo &TTI,
932-
AssumptionCache &AC, OptimizationRemarkEmitter &ORE,
933-
bool PreserveLCSSA, int OptLevel,
934-
Optional<unsigned> ProvidedCount,
935-
Optional<unsigned> ProvidedThreshold,
936-
Optional<bool> ProvidedAllowPartial,
937-
Optional<bool> ProvidedRuntime,
938-
Optional<bool> ProvidedUpperBound) {
932+
static bool tryToUnrollLoop(
933+
Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
934+
const TargetTransformInfo &TTI, AssumptionCache &AC,
935+
OptimizationRemarkEmitter &ORE, bool PreserveLCSSA, int OptLevel,
936+
Optional<unsigned> ProvidedCount, Optional<unsigned> ProvidedThreshold,
937+
Optional<bool> ProvidedAllowPartial, Optional<bool> ProvidedRuntime,
938+
Optional<bool> ProvidedUpperBound, Optional<bool> ProvidedAllowPeeling) {
939939
DEBUG(dbgs() << "Loop Unroll: F[" << L->getHeader()->getParent()->getName()
940940
<< "] Loop %" << L->getHeader()->getName() << "\n");
941941
if (HasUnrollDisablePragma(L))
@@ -951,7 +951,8 @@ static bool tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
951951
bool Convergent;
952952
TargetTransformInfo::UnrollingPreferences UP = gatherUnrollingPreferences(
953953
L, SE, TTI, OptLevel, ProvidedThreshold, ProvidedCount,
954-
ProvidedAllowPartial, ProvidedRuntime, ProvidedUpperBound);
954+
ProvidedAllowPartial, ProvidedRuntime, ProvidedUpperBound,
955+
ProvidedAllowPeeling);
955956
// Exit early if unrolling is disabled.
956957
if (UP.Threshold == 0 && (!UP.Partial || UP.PartialThreshold == 0))
957958
return false;
@@ -1053,10 +1054,12 @@ class LoopUnroll : public LoopPass {
10531054
LoopUnroll(int OptLevel = 2, Optional<unsigned> Threshold = None,
10541055
Optional<unsigned> Count = None,
10551056
Optional<bool> AllowPartial = None, Optional<bool> Runtime = None,
1056-
Optional<bool> UpperBound = None)
1057+
Optional<bool> UpperBound = None,
1058+
Optional<bool> AllowPeeling = None)
10571059
: LoopPass(ID), OptLevel(OptLevel), ProvidedCount(std::move(Count)),
10581060
ProvidedThreshold(Threshold), ProvidedAllowPartial(AllowPartial),
1059-
ProvidedRuntime(Runtime), ProvidedUpperBound(UpperBound) {
1061+
ProvidedRuntime(Runtime), ProvidedUpperBound(UpperBound),
1062+
ProvidedAllowPeeling(AllowPeeling) {
10601063
initializeLoopUnrollPass(*PassRegistry::getPassRegistry());
10611064
}
10621065

@@ -1066,6 +1069,7 @@ class LoopUnroll : public LoopPass {
10661069
Optional<bool> ProvidedAllowPartial;
10671070
Optional<bool> ProvidedRuntime;
10681071
Optional<bool> ProvidedUpperBound;
1072+
Optional<bool> ProvidedAllowPeeling;
10691073

10701074
bool runOnLoop(Loop *L, LPPassManager &) override {
10711075
if (skipLoop(L))
@@ -1088,7 +1092,7 @@ class LoopUnroll : public LoopPass {
10881092
return tryToUnrollLoop(L, DT, LI, SE, TTI, AC, ORE, PreserveLCSSA, OptLevel,
10891093
ProvidedCount, ProvidedThreshold,
10901094
ProvidedAllowPartial, ProvidedRuntime,
1091-
ProvidedUpperBound);
1095+
ProvidedUpperBound, ProvidedAllowPeeling);
10921096
}
10931097

10941098
/// This transformation requires natural loop information & requires that
@@ -1112,8 +1116,8 @@ INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
11121116
INITIALIZE_PASS_END(LoopUnroll, "loop-unroll", "Unroll loops", false, false)
11131117

11141118
Pass *llvm::createLoopUnrollPass(int OptLevel, int Threshold, int Count,
1115-
int AllowPartial, int Runtime,
1116-
int UpperBound) {
1119+
int AllowPartial, int Runtime, int UpperBound,
1120+
int AllowPeeling) {
11171121
// TODO: It would make more sense for this function to take the optionals
11181122
// directly, but that's dangerous since it would silently break out of tree
11191123
// callers.
@@ -1122,11 +1126,12 @@ Pass *llvm::createLoopUnrollPass(int OptLevel, int Threshold, int Count,
11221126
Count == -1 ? None : Optional<unsigned>(Count),
11231127
AllowPartial == -1 ? None : Optional<bool>(AllowPartial),
11241128
Runtime == -1 ? None : Optional<bool>(Runtime),
1125-
UpperBound == -1 ? None : Optional<bool>(UpperBound));
1129+
UpperBound == -1 ? None : Optional<bool>(UpperBound),
1130+
AllowPeeling == -1 ? None : Optional<bool>(AllowPeeling));
11261131
}
11271132

11281133
Pass *llvm::createSimpleLoopUnrollPass(int OptLevel) {
1129-
return llvm::createLoopUnrollPass(OptLevel, -1, -1, 0, 0, 0);
1134+
return llvm::createLoopUnrollPass(OptLevel, -1, -1, 0, 0, 0, 0);
11301135
}
11311136

11321137
PreservedAnalyses LoopFullUnrollPass::run(Loop &L, LoopAnalysisManager &AM,
@@ -1156,7 +1161,8 @@ PreservedAnalyses LoopFullUnrollPass::run(Loop &L, LoopAnalysisManager &AM,
11561161
tryToUnrollLoop(&L, AR.DT, &AR.LI, AR.SE, AR.TTI, AR.AC, *ORE,
11571162
/*PreserveLCSSA*/ true, OptLevel, /*Count*/ None,
11581163
/*Threshold*/ None, /*AllowPartial*/ false,
1159-
/*Runtime*/ false, /*UpperBound*/ false);
1164+
/*Runtime*/ false, /*UpperBound*/ false,
1165+
/*AllowPeeling*/ false);
11601166
if (!Changed)
11611167
return PreservedAnalyses::all();
11621168

@@ -1278,7 +1284,8 @@ PreservedAnalyses LoopUnrollPass::run(Function &F,
12781284
bool CurChanged = tryToUnrollLoop(
12791285
&L, DT, &LI, SE, TTI, AC, ORE,
12801286
/*PreserveLCSSA*/ true, OptLevel, /*Count*/ None,
1281-
/*Threshold*/ None, AllowPartialParam, RuntimeParam, UpperBoundParam);
1287+
/*Threshold*/ None, AllowPartialParam, RuntimeParam, UpperBoundParam,
1288+
/*AllowPeeling*/ None);
12821289
Changed |= CurChanged;
12831290

12841291
// The parent must not be damaged by unrolling!

llvm/test/Transforms/LoopUnroll/peel-loop-pgo.ll

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt < %s -S -debug-only=loop-unroll -loop-unroll 2>&1 | FileCheck %s
2+
; RUN: opt < %s -S -debug-only=loop-unroll -passes='require<opt-remark-emit>,unroll' 2>&1 | FileCheck %s
23
; REQUIRES: asserts
34

45
; Make sure we use the profile information correctly to peel-off 3 iterations
@@ -9,6 +10,10 @@
910
; CHECK: Loop Unroll: F[optsize]
1011
; CHECK-NOT: PEELING
1112

13+
; Confirm that no peeling occurs when we are performing full unrolling.
14+
; RUN: opt < %s -S -debug-only=loop-unroll -passes='require<opt-remark-emit>,loop(unroll-full)' 2>&1 | FileCheck %s --check-prefix=FULLUNROLL
15+
; FULLUNROLL-NOT: PEELING
16+
1217
; CHECK-LABEL: @basic
1318
; CHECK: br i1 %{{.*}}, label %[[NEXT0:.*]], label %for.cond.for.end_crit_edge, !prof !1
1419
; CHECK: [[NEXT0]]:

0 commit comments

Comments
 (0)