Skip to content

Commit b7cfa6c

Browse files
author
Sidharth Baveja
committed
[Loop Peeling] Separate the Loop Peeling Utilities from the Loop Unrolling Utilities
Summary: This patch separates the Loop Peeling Utilities from Loop Unrolling. The reason for this change is that Loop Peeling is no longer only being used by loop unrolling; Patch D82927 introduces loop peeling with fusion, such that loops can be modified to have to same trip count, making them legal to be peeled. Reviewed By: Meinersbur Differential Revision: https://reviews.llvm.org/D83056
1 parent bf812c1 commit b7cfa6c

File tree

9 files changed

+109
-82
lines changed

9 files changed

+109
-82
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- llvm/Transforms/Utils/LoopPeel.h ----- Peeling utilities -*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines some loop peeling utilities. It does not define any
10+
// actual pass or policy.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_TRANSFORMS_UTILS_LOOPPEEL_H
15+
#define LLVM_TRANSFORMS_UTILS_LOOPPEEL_H
16+
17+
#include "llvm/Analysis/TargetTransformInfo.h"
18+
19+
namespace llvm {
20+
21+
bool canPeel(Loop *L);
22+
23+
bool peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, ScalarEvolution *SE,
24+
DominatorTree *DT, AssumptionCache *AC, bool PreserveLCSSA);
25+
26+
TargetTransformInfo::PeelingPreferences
27+
gatherPeelingPreferences(Loop *L, ScalarEvolution &SE,
28+
const TargetTransformInfo &TTI,
29+
Optional<bool> UserAllowPeeling,
30+
Optional<bool> UserAllowProfileBasedPeeling,
31+
bool UnrollingSpecficValues = false);
32+
33+
void computePeelCount(Loop *L, unsigned LoopSize,
34+
TargetTransformInfo::PeelingPreferences &PP,
35+
unsigned &TripCount, ScalarEvolution &SE,
36+
unsigned Threshold = UINT_MAX);
37+
38+
} // end namespace llvm
39+
40+
#endif // LLVM_TRANSFORMS_UTILS_LOOPPEEL_H

llvm/include/llvm/Transforms/Utils/UnrollLoop.h

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,6 @@ bool UnrollRuntimeLoopRemainder(
9292
const TargetTransformInfo *TTI, bool PreserveLCSSA,
9393
Loop **ResultLoop = nullptr);
9494

95-
void computePeelCount(Loop *L, unsigned LoopSize,
96-
TargetTransformInfo::UnrollingPreferences &UP,
97-
TargetTransformInfo::PeelingPreferences &PP,
98-
unsigned &TripCount, ScalarEvolution &SE);
99-
100-
bool canPeel(Loop *L);
101-
102-
bool peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI, ScalarEvolution *SE,
103-
DominatorTree *DT, AssumptionCache *AC, bool PreserveLCSSA);
104-
10595
LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
10696
unsigned TripMultiple, bool UnrollRemainder,
10797
LoopInfo *LI, ScalarEvolution *SE,
@@ -121,7 +111,6 @@ bool computeUnrollCount(Loop *L, const TargetTransformInfo &TTI,
121111
unsigned &TripMultiple, unsigned LoopSize,
122112
TargetTransformInfo::UnrollingPreferences &UP,
123113
TargetTransformInfo::PeelingPreferences &PP,
124-
125114
bool &UseUpperBound);
126115

127116
void simplifyLoopAfterUnroll(Loop *L, bool SimplifyIVs, LoopInfo *LI,
@@ -138,12 +127,6 @@ TargetTransformInfo::UnrollingPreferences gatherUnrollingPreferences(
138127
Optional<bool> UserAllowPartial, Optional<bool> UserRuntime,
139128
Optional<bool> UserUpperBound, Optional<unsigned> UserFullUnrollMaxCount);
140129

141-
TargetTransformInfo::PeelingPreferences
142-
gatherPeelingPreferences(Loop *L, ScalarEvolution &SE,
143-
const TargetTransformInfo &TTI,
144-
Optional<bool> UserAllowPeeling,
145-
Optional<bool> UserAllowProfileBasedPeeling);
146-
147130
unsigned ApproximateLoopSize(const Loop *L, unsigned &NumCalls,
148131
bool &NotDuplicatable, bool &Convergent,
149132
const TargetTransformInfo &TTI,

llvm/lib/Target/Hexagon/HexagonTargetTransformInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/IR/User.h"
2222
#include "llvm/Support/Casting.h"
2323
#include "llvm/Support/CommandLine.h"
24+
#include "llvm/Transforms/Utils/LoopPeel.h"
2425
#include "llvm/Transforms/Utils/UnrollLoop.h"
2526

2627
using namespace llvm;

llvm/lib/Transforms/Scalar/LoopFuse.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
#include "llvm/Transforms/Utils.h"
6767
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
6868
#include "llvm/Transforms/Utils/CodeMoverUtils.h"
69-
#include "llvm/Transforms/Utils/UnrollLoop.h"
69+
#include "llvm/Transforms/Utils/LoopPeel.h"
7070

7171
using namespace llvm;
7272

llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "llvm/Support/Debug.h"
4242
#include "llvm/Support/raw_ostream.h"
4343
#include "llvm/Transforms/Scalar.h"
44+
#include "llvm/Transforms/Utils/LoopPeel.h"
4445
#include "llvm/Transforms/Utils/LoopSimplify.h"
4546
#include "llvm/Transforms/Utils/LoopUtils.h"
4647
#include "llvm/Transforms/Utils/UnrollLoop.h"

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "llvm/Transforms/Scalar.h"
5757
#include "llvm/Transforms/Scalar/LoopPassManager.h"
5858
#include "llvm/Transforms/Utils.h"
59+
#include "llvm/Transforms/Utils/LoopPeel.h"
5960
#include "llvm/Transforms/Utils/LoopSimplify.h"
6061
#include "llvm/Transforms/Utils/LoopUtils.h"
6162
#include "llvm/Transforms/Utils/SizeOpts.h"
@@ -115,10 +116,6 @@ static cl::opt<unsigned> UnrollFullMaxCount(
115116
cl::desc(
116117
"Set the max unroll count for full unrolling, for testing purposes"));
117118

118-
static cl::opt<unsigned> UnrollPeelCount(
119-
"unroll-peel-count", cl::Hidden,
120-
cl::desc("Set the unroll peeling count, for testing purposes"));
121-
122119
static cl::opt<bool>
123120
UnrollAllowPartial("unroll-allow-partial", cl::Hidden,
124121
cl::desc("Allows loops to be partially unrolled until "
@@ -149,15 +146,6 @@ static cl::opt<unsigned> FlatLoopTripCountThreshold(
149146
"threshold, the loop is considered as flat and will be less "
150147
"aggressively unrolled."));
151148

152-
static cl::opt<bool>
153-
UnrollAllowPeeling("unroll-allow-peeling", cl::init(true), cl::Hidden,
154-
cl::desc("Allows loops to be peeled when the dynamic "
155-
"trip count is known to be low."));
156-
157-
static cl::opt<bool> UnrollAllowLoopNestsPeeling(
158-
"unroll-allow-loop-nests-peeling", cl::init(false), cl::Hidden,
159-
cl::desc("Allows loop nests to be peeled."));
160-
161149
static cl::opt<bool> UnrollUnrollRemainder(
162150
"unroll-remainder", cl::Hidden,
163151
cl::desc("Allow the loop remainder to be unrolled."));
@@ -275,39 +263,6 @@ TargetTransformInfo::UnrollingPreferences llvm::gatherUnrollingPreferences(
275263
return UP;
276264
}
277265

278-
TargetTransformInfo::PeelingPreferences
279-
llvm::gatherPeelingPreferences(Loop *L, ScalarEvolution &SE,
280-
const TargetTransformInfo &TTI,
281-
Optional<bool> UserAllowPeeling,
282-
Optional<bool> UserAllowProfileBasedPeeling) {
283-
TargetTransformInfo::PeelingPreferences PP;
284-
285-
// Default values
286-
PP.PeelCount = 0;
287-
PP.AllowPeeling = true;
288-
PP.AllowLoopNestsPeeling = false;
289-
PP.PeelProfiledIterations = true;
290-
291-
// Get Target Specifc Values
292-
TTI.getPeelingPreferences(L, SE, PP);
293-
294-
// User Specified Values using cl::opt
295-
if (UnrollPeelCount.getNumOccurrences() > 0)
296-
PP.PeelCount = UnrollPeelCount;
297-
if (UnrollAllowPeeling.getNumOccurrences() > 0)
298-
PP.AllowPeeling = UnrollAllowPeeling;
299-
if (UnrollAllowLoopNestsPeeling.getNumOccurrences() > 0)
300-
PP.AllowLoopNestsPeeling = UnrollAllowLoopNestsPeeling;
301-
302-
// User Specifed values provided by argument
303-
if (UserAllowPeeling.hasValue())
304-
PP.AllowPeeling = *UserAllowPeeling;
305-
if (UserAllowProfileBasedPeeling.hasValue())
306-
PP.PeelProfiledIterations = *UserAllowProfileBasedPeeling;
307-
308-
return PP;
309-
}
310-
311266
namespace {
312267

313268
/// A struct to densely store the state of an instruction after unrolling at
@@ -881,7 +836,7 @@ bool llvm::computeUnrollCount(
881836
}
882837

883838
// 4th priority is loop peeling.
884-
computePeelCount(L, LoopSize, UP, PP, TripCount, SE);
839+
computePeelCount(L, LoopSize, PP, TripCount, SE, UP.Threshold);
885840
if (PP.PeelCount) {
886841
UP.Runtime = false;
887842
UP.Count = 1;
@@ -1087,7 +1042,7 @@ static LoopUnrollResult tryToUnrollLoop(
10871042
ProvidedAllowPartial, ProvidedRuntime, ProvidedUpperBound,
10881043
ProvidedFullUnrollMaxCount);
10891044
TargetTransformInfo::PeelingPreferences PP = gatherPeelingPreferences(
1090-
L, SE, TTI, ProvidedAllowPeeling, ProvidedAllowProfileBasedPeeling);
1045+
L, SE, TTI, ProvidedAllowPeeling, ProvidedAllowProfileBasedPeeling, true);
10911046

10921047
// Exit early if unrolling is disabled. For OptForSize, we pick the loop size
10931048
// as threshold later on.

llvm/lib/Transforms/Utils/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ add_llvm_component_library(LLVMTransformUtils
3535
LCSSA.cpp
3636
LibCallsShrinkWrap.cpp
3737
Local.cpp
38+
LoopPeel.cpp
3839
LoopRotationUtils.cpp
3940
LoopSimplify.cpp
4041
LoopUnroll.cpp
4142
LoopUnrollAndJam.cpp
42-
LoopUnrollPeel.cpp
4343
LoopUnrollRuntime.cpp
4444
LoopUtils.cpp
4545
LoopVersioning.cpp

llvm/lib/Transforms/Utils/LoopUnrollPeel.cpp renamed to llvm/lib/Transforms/Utils/LoopPeel.cpp

Lines changed: 61 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
//===- UnrollLoopPeel.cpp - Loop peeling utilities ------------------------===//
1+
//===- LoopPeel.cpp -------------------------------------------------------===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// This file implements some loop unrolling utilities for peeling loops
10-
// with dynamically inferred (from PGO) trip counts. See LoopUnroll.cpp for
11-
// unrolling loops with compile-time constant trip counts.
12-
//
9+
// Loop Peeling Utilities.
1310
//===----------------------------------------------------------------------===//
1411

12+
#include "llvm/Transforms/Utils/LoopPeel.h"
1513
#include "llvm/ADT/DenseMap.h"
1614
#include "llvm/ADT/Optional.h"
1715
#include "llvm/ADT/SmallVector.h"
@@ -49,10 +47,24 @@
4947
using namespace llvm;
5048
using namespace llvm::PatternMatch;
5149

52-
#define DEBUG_TYPE "loop-unroll"
50+
#define DEBUG_TYPE "loop-peel"
5351

5452
STATISTIC(NumPeeled, "Number of loops peeled");
5553

54+
static cl::opt<unsigned> UnrollPeelCount(
55+
"unroll-peel-count", cl::Hidden,
56+
cl::desc("Set the unroll peeling count, for testing purposes"));
57+
58+
static cl::opt<bool>
59+
UnrollAllowPeeling("unroll-allow-peeling", cl::init(true), cl::Hidden,
60+
cl::desc("Allows loops to be peeled when the dynamic "
61+
"trip count is known to be low."));
62+
63+
static cl::opt<bool>
64+
UnrollAllowLoopNestsPeeling("unroll-allow-loop-nests-peeling",
65+
cl::init(false), cl::Hidden,
66+
cl::desc("Allows loop nests to be peeled."));
67+
5668
static cl::opt<unsigned> UnrollPeelMaxCount(
5769
"unroll-peel-max-count", cl::init(7), cl::Hidden,
5870
cl::desc("Max average trip count which will cause loop peeling."));
@@ -278,9 +290,9 @@ static unsigned countToEliminateCompares(Loop &L, unsigned MaxPeelCount,
278290

279291
// Return the number of iterations we want to peel off.
280292
void llvm::computePeelCount(Loop *L, unsigned LoopSize,
281-
TargetTransformInfo::UnrollingPreferences &UP,
282293
TargetTransformInfo::PeelingPreferences &PP,
283-
unsigned &TripCount, ScalarEvolution &SE) {
294+
unsigned &TripCount, ScalarEvolution &SE,
295+
unsigned Threshold) {
284296
assert(LoopSize > 0 && "Zero loop size is not allowed!");
285297
// Save the PP.PeelCount value set by the target in
286298
// TTI.getPeelingPreferences or by the flag -unroll-peel-count.
@@ -322,7 +334,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
322334
// maximum number of iterations among these values, thus turning all those
323335
// Phis into invariants.
324336
// First, check that we can peel at least one iteration.
325-
if (2 * LoopSize <= UP.Threshold && UnrollPeelMaxCount > 0) {
337+
if (2 * LoopSize <= Threshold && UnrollPeelMaxCount > 0) {
326338
// Store the pre-calculated values here.
327339
SmallDenseMap<PHINode *, unsigned> IterationsToInvariance;
328340
// Now go through all Phis to calculate their the number of iterations they
@@ -342,7 +354,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
342354

343355
// Pay respect to limitations implied by loop size and the max peel count.
344356
unsigned MaxPeelCount = UnrollPeelMaxCount;
345-
MaxPeelCount = std::min(MaxPeelCount, UP.Threshold / LoopSize - 1);
357+
MaxPeelCount = std::min(MaxPeelCount, Threshold / LoopSize - 1);
346358

347359
DesiredPeelCount = std::max(DesiredPeelCount,
348360
countToEliminateCompares(*L, MaxPeelCount, SE));
@@ -385,7 +397,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
385397

386398
if (*PeelCount) {
387399
if ((*PeelCount + AlreadyPeeled <= UnrollPeelMaxCount) &&
388-
(LoopSize * (*PeelCount + 1) <= UP.Threshold)) {
400+
(LoopSize * (*PeelCount + 1) <= Threshold)) {
389401
LLVM_DEBUG(dbgs() << "Peeling first " << *PeelCount
390402
<< " iterations.\n");
391403
PP.PeelCount = *PeelCount;
@@ -396,7 +408,7 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
396408
LLVM_DEBUG(dbgs() << "Max peel count: " << UnrollPeelMaxCount << "\n");
397409
LLVM_DEBUG(dbgs() << "Peel cost: " << LoopSize * (*PeelCount + 1)
398410
<< "\n");
399-
LLVM_DEBUG(dbgs() << "Max peel cost: " << UP.Threshold << "\n");
411+
LLVM_DEBUG(dbgs() << "Max peel cost: " << Threshold << "\n");
400412
}
401413
}
402414
}
@@ -491,7 +503,7 @@ static void fixupBranchWeights(BasicBlock *Header, BranchInst *LatchBR,
491503
/// instructions in the last peeled-off iteration.
492504
static void cloneLoopBlocks(
493505
Loop *L, unsigned IterNumber, BasicBlock *InsertTop, BasicBlock *InsertBot,
494-
SmallVectorImpl<std::pair<BasicBlock *, BasicBlock *> > &ExitEdges,
506+
SmallVectorImpl<std::pair<BasicBlock *, BasicBlock *>> &ExitEdges,
495507
SmallVectorImpl<BasicBlock *> &NewBlocks, LoopBlocksDFS &LoopBlocks,
496508
ValueToValueMapTy &VMap, ValueToValueMapTy &LVMap, DominatorTree *DT,
497509
LoopInfo *LI) {
@@ -599,6 +611,40 @@ static void cloneLoopBlocks(
599611
LVMap[KV.first] = KV.second;
600612
}
601613

614+
TargetTransformInfo::PeelingPreferences llvm::gatherPeelingPreferences(
615+
Loop *L, ScalarEvolution &SE, const TargetTransformInfo &TTI,
616+
Optional<bool> UserAllowPeeling,
617+
Optional<bool> UserAllowProfileBasedPeeling, bool UnrollingSpecficValues) {
618+
TargetTransformInfo::PeelingPreferences PP;
619+
620+
// Set the default values.
621+
PP.PeelCount = 0;
622+
PP.AllowPeeling = true;
623+
PP.AllowLoopNestsPeeling = false;
624+
PP.PeelProfiledIterations = true;
625+
626+
// Get the target specifc values.
627+
TTI.getPeelingPreferences(L, SE, PP);
628+
629+
// User specified values using cl::opt.
630+
if (UnrollingSpecficValues) {
631+
if (UnrollPeelCount.getNumOccurrences() > 0)
632+
PP.PeelCount = UnrollPeelCount;
633+
if (UnrollAllowPeeling.getNumOccurrences() > 0)
634+
PP.AllowPeeling = UnrollAllowPeeling;
635+
if (UnrollAllowLoopNestsPeeling.getNumOccurrences() > 0)
636+
PP.AllowLoopNestsPeeling = UnrollAllowLoopNestsPeeling;
637+
}
638+
639+
// User specifed values provided by argument.
640+
if (UserAllowPeeling.hasValue())
641+
PP.AllowPeeling = *UserAllowPeeling;
642+
if (UserAllowProfileBasedPeeling.hasValue())
643+
PP.PeelProfiledIterations = *UserAllowProfileBasedPeeling;
644+
645+
return PP;
646+
}
647+
602648
/// Peel off the first \p PeelCount iterations of loop \p L.
603649
///
604650
/// Note that this does not peel them off as a single straight-line block.
@@ -609,8 +655,8 @@ static void cloneLoopBlocks(
609655
/// for the bulk of dynamic execution, can be further simplified by scalar
610656
/// optimizations.
611657
bool llvm::peelLoop(Loop *L, unsigned PeelCount, LoopInfo *LI,
612-
ScalarEvolution *SE, DominatorTree *DT,
613-
AssumptionCache *AC, bool PreserveLCSSA) {
658+
ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
659+
bool PreserveLCSSA) {
614660
assert(PeelCount > 0 && "Attempt to peel out zero iterations?");
615661
assert(canPeel(L) && "Attempt to peel a loop which is not peelable?");
616662

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
6060
#include "llvm/Transforms/Utils/Cloning.h"
6161
#include "llvm/Transforms/Utils/Local.h"
62+
#include "llvm/Transforms/Utils/LoopPeel.h"
6263
#include "llvm/Transforms/Utils/LoopSimplify.h"
6364
#include "llvm/Transforms/Utils/LoopUtils.h"
6465
#include "llvm/Transforms/Utils/SimplifyIndVar.h"

0 commit comments

Comments
 (0)