-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathLoopUnroll.cpp
464 lines (391 loc) · 15.1 KB
/
LoopUnroll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//===--- LoopUnroll.cpp - Loop unrolling ----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-loopunroll"
#include "llvm/ADT/DepthFirstIterator.h"
#include "swift/SIL/PatternMatch.h"
#include "swift/SIL/SILCloner.h"
#include "swift/SILOptimizer/Analysis/LoopAnalysis.h"
#include "swift/SILOptimizer/PassManager/Passes.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/SILInliner.h"
#include "swift/SILOptimizer/Utils/SILSSAUpdater.h"
using namespace swift;
using namespace swift::PatternMatch;
using llvm::DenseMap;
using llvm::MapVector;
static const uint64_t SILLoopUnrollThreshold = 250;
namespace {
/// Clone the basic blocks in a loop.
class LoopCloner : public SILCloner<LoopCloner> {
SILLoop *Loop;
friend class SILVisitor<LoopCloner>;
friend class SILCloner<LoopCloner>;
public:
LoopCloner(SILLoop *Loop)
: SILCloner<LoopCloner>(*Loop->getHeader()->getParent()), Loop(Loop) {}
/// Clone the basic blocks in the loop.
void cloneLoop();
/// Get a map from basic blocks or the original loop to the cloned loop.
MapVector<SILBasicBlock *, SILBasicBlock *> &getBBMap() { return BBMap; }
DenseMap<SILValue, SILValue> &getValueMap() { return ValueMap; }
DenseMap<SILInstruction *, SILInstruction *> &getInstMap() {
return InstructionMap;
}
protected:
SILValue remapValue(SILValue V) {
if (auto *BB = V->getParentBB()) {
if (!Loop->contains(BB))
return V;
}
return SILCloner<LoopCloner>::remapValue(V);
}
void postProcess(SILInstruction *Orig, SILInstruction *Cloned) {
SILCloner<LoopCloner>::postProcess(Orig, Cloned);
}
};
} // End anonymous namespace.
void LoopCloner::cloneLoop() {
auto *Header = Loop->getHeader();
auto *CurFun = Loop->getHeader()->getParent();
auto &Mod = CurFun->getModule();
SmallVector<SILBasicBlock *, 16> ExitBlocks;
Loop->getExitBlocks(ExitBlocks);
for (auto *ExitBB : ExitBlocks)
BBMap[ExitBB] = ExitBB;
auto *ClonedHeader = new (Mod) SILBasicBlock(CurFun);
BBMap[Header] = ClonedHeader;
// Clone the arguments.
for (auto *Arg : Header->getBBArgs()) {
SILValue MappedArg =
new (Mod) SILArgument(ClonedHeader, getOpType(Arg->getType()));
ValueMap.insert(std::make_pair(Arg, MappedArg));
}
// Clone the instructions in this basic block and recursively clone
// successor blocks.
getBuilder().setInsertionPoint(ClonedHeader);
visitSILBasicBlock(Header);
// Fix-up terminators.
for (auto BBPair : BBMap)
if (BBPair.first != BBPair.second) {
getBuilder().setInsertionPoint(BBPair.second);
visit(BBPair.first->getTerminator());
}
}
/// Determine the number of iterations the loop is at most executed. The loop
/// might contain early exits so this is the maximum if no early exits are
/// taken.
static Optional<uint64_t> getMaxLoopTripCount(SILLoop *Loop,
SILBasicBlock *Preheader,
SILBasicBlock *Header,
SILBasicBlock *Latch) {
// Skip a split backedge.
SILBasicBlock *OrigLatch = Latch;
if (!Loop->isLoopExiting(Latch) && !(Latch = Latch->getSinglePredecessor()))
return None;
if (!Loop->isLoopExiting(Latch))
return None;
// Get the loop exit condition.
auto *CondBr = dyn_cast<CondBranchInst>(Latch->getTerminator());
if (!CondBr)
return None;
// Match an add 1 recurrence.
SILArgument *RecArg;
IntegerLiteralInst *End;
SILValue RecNext;
if (!match(CondBr->getCondition(),
m_BuiltinInst(BuiltinValueKind::ICMP_EQ, m_SILValue(RecNext),
m_IntegerLiteralInst(End))))
return None;
if (!match(RecNext,
m_TupleExtractInst(m_ApplyInst(BuiltinValueKind::SAddOver,
m_SILArgument(RecArg), m_One()),
0)))
return None;
if (RecArg->getParent() != Header)
return None;
auto *Start = dyn_cast_or_null<IntegerLiteralInst>(
RecArg->getIncomingValue(Preheader));
if (!Start)
return None;
if (RecNext != RecArg->getIncomingValue(OrigLatch))
return None;
auto StartVal = Start->getValue();
auto EndVal = End->getValue();
if (StartVal.sgt(EndVal))
return None;
auto Dist = EndVal - StartVal;
if (Dist.getBitWidth() > 64)
return None;
if (Dist == 0)
return None;
return Dist.getZExtValue();
}
/// Check whether we can duplicate the instructions in the loop and use a
/// heuristic that looks at the trip count and the cost of the instructions in
/// the loop to determine whether we should unroll this loop.
static bool canAndShouldUnrollLoop(SILLoop *Loop, uint64_t TripCount) {
assert(Loop->getSubLoops().empty() && "Expect innermost loops");
if (TripCount > 32)
return false;
// We can unroll a loop if we can duplicate the instructions it holds.
uint64_t Cost = 0;
for (auto *BB : Loop->getBlocks()) {
for (auto &Inst : *BB) {
if (!Loop->canDuplicate(&Inst))
return false;
if (instructionInlineCost(Inst) != InlineCost::Free)
++Cost;
if (Cost * TripCount > SILLoopUnrollThreshold)
return false;
}
}
return true;
}
/// Redirect the terminator of the current loop iteration's latch to the next
/// iterations header or if this is the last iteration remove the backedge to
/// the header.
static void redirectTerminator(SILBasicBlock *Latch, unsigned CurLoopIter,
unsigned LastLoopIter, SILBasicBlock *OrigHeader,
SILBasicBlock *NextIterationsHeader) {
auto *CurrentTerminator = Latch->getTerminator();
// We can either have a split backedge as our latch terminator.
// HeaderBlock:
// ...
// cond_br %cond, ExitBlock, BackedgeBlock
//
// BackedgeBlock:
// br HeaderBlock:
//
// Or a conditional branch back to the header.
// HeaderBlock:
// ...
// cond_br %cond, ExitBlock, HeaderBlock
//
// Redirect the HeaderBlock target to the unrolled successor. In the
// unrolled block of the last iteration unconditionally jump to the
// ExitBlock instead.
// Handle the split backedge case.
if (auto *Br = dyn_cast<BranchInst>(CurrentTerminator)) {
// On the last iteration change the conditional exit to an unconditional
// one.
if (CurLoopIter == LastLoopIter) {
auto *CondBr =
cast<CondBranchInst>(Latch->getSinglePredecessor()->getTerminator());
if (CondBr->getTrueBB() != Latch)
SILBuilder(CondBr).createBranch(CondBr->getLoc(), CondBr->getTrueBB(),
CondBr->getTrueArgs());
else
SILBuilder(CondBr).createBranch(CondBr->getLoc(), CondBr->getFalseBB(),
CondBr->getFalseArgs());
CondBr->eraseFromParent();
return;
}
// Otherwise, branch to the next iteration's header.
SILBuilder(Br).createBranch(Br->getLoc(), NextIterationsHeader,
Br->getArgs());
Br->eraseFromParent();
return;
}
// Otherwise, we have a conditional branch to the header.
auto *CondBr = cast<CondBranchInst>(CurrentTerminator);
// On the last iteration change the conditional exit to an unconditional
// one.
if (CurLoopIter == LastLoopIter) {
if (CondBr->getTrueBB() != OrigHeader)
SILBuilder(CondBr).createBranch(CondBr->getLoc(), CondBr->getTrueBB(),
CondBr->getTrueArgs());
else
SILBuilder(CondBr).createBranch(CondBr->getLoc(), CondBr->getFalseBB(),
CondBr->getFalseArgs());
CondBr->eraseFromParent();
return;
}
// Otherwise, branch to the next iteration's header.
if (CondBr->getTrueBB() == OrigHeader) {
SILBuilder(CondBr).createCondBranch(
CondBr->getLoc(), CondBr->getCondition(), NextIterationsHeader,
CondBr->getTrueArgs(), CondBr->getFalseBB(), CondBr->getFalseArgs());
} else {
SILBuilder(CondBr).createCondBranch(
CondBr->getLoc(), CondBr->getCondition(), CondBr->getTrueBB(),
CondBr->getTrueArgs(), NextIterationsHeader, CondBr->getFalseArgs());
}
CondBr->eraseFromParent();
}
/// Collect all the loop live out values in the map that maps original live out
/// value to live out value in the cloned loop.
static void collectLoopLiveOutValues(
DenseMap<SILValue, SmallVector<SILValue, 8>> &LoopLiveOutValues,
SILLoop *Loop, DenseMap<SILValue, SILValue> &ClonedValues,
DenseMap<SILInstruction *, SILInstruction *> &ClonedInstructions) {
for (auto *Block : Loop->getBlocks()) {
// Look at block arguments.
for (auto *Arg : Block->getBBArgs()) {
for (auto *Op : Arg->getUses()) {
// Is this use outside the loop?
if (!Loop->contains(Op->getUser())) {
auto ArgumentValue = SILValue(Arg);
assert(ClonedValues.count(ArgumentValue) && "Unmapped Argument!");
if (!LoopLiveOutValues.count(ArgumentValue))
LoopLiveOutValues[ArgumentValue].push_back(
ClonedValues[ArgumentValue]);
}
}
}
// And the instructions.
for (auto &Inst : *Block) {
for (auto *Op : Inst.getUses()) {
// Is this use outside the loop.
if (!Loop->contains(Op->getUser())) {
auto UsedValue = Op->get();
assert(UsedValue == &Inst && "Instructions must match");
assert(ClonedInstructions.count(&Inst) && "Unmapped instruction!");
if (!LoopLiveOutValues.count(UsedValue))
LoopLiveOutValues[UsedValue].push_back(ClonedInstructions[&Inst]);
}
}
}
}
}
static void
updateSSA(SILLoop *Loop,
DenseMap<SILValue, SmallVector<SILValue, 8>> &LoopLiveOutValues) {
SILSSAUpdater SSAUp;
for (auto &MapEntry : LoopLiveOutValues) {
// Collect out of loop uses of this value.
auto OrigValue = MapEntry.first;
SmallVector<UseWrapper, 16> UseList;
for (auto Use : OrigValue->getUses())
if (!Loop->contains(Use->getUser()->getParent()))
UseList.push_back(UseWrapper(Use));
// Update SSA of use with the available values.
SSAUp.Initialize(OrigValue->getType());
SSAUp.AddAvailableValue(OrigValue->getParentBB(), OrigValue);
for (auto NewValue : MapEntry.second)
SSAUp.AddAvailableValue(NewValue->getParentBB(), NewValue);
for (auto U : UseList) {
Operand *Use = U;
SSAUp.RewriteUse(*Use);
}
}
}
/// Try to fully unroll the loop if we can determine the trip count and the trip
/// count lis below a threshold.
static bool tryToUnrollLoop(SILLoop *Loop) {
assert(Loop->getSubLoops().empty() && "Expecting innermost loops");
auto *Preheader = Loop->getLoopPreheader();
if (!Preheader)
return false;
auto *Latch = Loop->getLoopLatch();
if (!Latch)
return false;
auto *Header = Loop->getHeader();
Optional<uint64_t> MaxTripCount =
getMaxLoopTripCount(Loop, Preheader, Header, Latch);
if (!MaxTripCount)
return false;
if (!canAndShouldUnrollLoop(Loop, MaxTripCount.getValue()))
return false;
// TODO: We need to split edges from non-condbr exits for the SSA updater. For
// now just don't handle loops containing such exits.
SmallVector<SILBasicBlock *, 16> ExitingBlocks;
Loop->getExitingBlocks(ExitingBlocks);
for (auto &Exit : ExitingBlocks)
if (!isa<CondBranchInst>(Exit->getTerminator()))
return false;
DEBUG(llvm::dbgs() << "Unrolling loop in " << Header->getParent()->getName()
<< " " << *Loop << "\n");
SmallVector<SILBasicBlock *, 16> Headers;
Headers.push_back(Header);
SmallVector<SILBasicBlock *, 16> Latches;
Latches.push_back(Latch);
DenseMap<SILValue, SmallVector<SILValue, 8>> LoopLiveOutValues;
// Copy the body MaxTripCount-1 times.
for (uint64_t Cnt = 1; Cnt < *MaxTripCount; ++Cnt) {
// Clone the blocks in the loop.
LoopCloner Cloner(Loop);
Cloner.cloneLoop();
Headers.push_back(Cloner.getBBMap()[Header]);
Latches.push_back(Cloner.getBBMap()[Latch]);
// Collect values defined in the loop but used outside. On the first
// iteration we populate the map from original loop to cloned loop. On
// subsequent iterations we only need to update this map with the values
// from the new iteration's clone.
if (Cnt == 1)
collectLoopLiveOutValues(LoopLiveOutValues, Loop, Cloner.getValueMap(),
Cloner.getInstMap());
else {
for (auto &MapEntry : LoopLiveOutValues) {
// If this is an argument look up the value in the value map.
SILValue MappedValue;
if (isa<SILArgument>(MapEntry.first))
MappedValue = Cloner.getValueMap()[MapEntry.first];
// Otherwise, consult the instruction map.
else
MappedValue = Cloner
.getInstMap()[cast<SILInstruction>(MapEntry.first)];
MapEntry.second.push_back(MappedValue);
assert(MapEntry.second.size() == Cnt);
}
}
}
// Thread the loop clones by redirecting the loop latches to the successor
// iteration's header.
for (unsigned Iteration = 0, End = Latches.size(); Iteration != End;
++Iteration) {
auto *CurrentLatch = Latches[Iteration];
auto LastIteration = End - 1;
auto *OriginalHeader = Headers[0];
auto *NextIterationsHeader =
Iteration == LastIteration ? nullptr : Headers[Iteration + 1];
redirectTerminator(CurrentLatch, Iteration, LastIteration, OriginalHeader,
NextIterationsHeader);
}
// Fixup SSA form for loop values used outside the loop.
updateSSA(Loop, LoopLiveOutValues);
return true;
}
// =============================================================================
// Driver
// =============================================================================
namespace {
class LoopUnrolling : public SILFunctionTransform {
StringRef getName() override { return "SIL Loop Unrolling"; }
void run() override {
bool Changed = false;
auto *Fun = getFunction();
SILLoopInfo *LoopInfo = PM->getAnalysis<SILLoopAnalysis>()->get(Fun);
// Collect innermost loops.
SmallVector<SILLoop *, 16> InnermostLoops;
for (auto *Loop : *LoopInfo) {
SmallVector<SILLoop *, 8> Worklist;
Worklist.push_back(Loop);
for (unsigned i = 0; i < Worklist.size(); ++i) {
auto *L = Worklist[i];
for (auto *SubLoop : *L)
Worklist.push_back(SubLoop);
if (L->getSubLoops().empty())
InnermostLoops.push_back(L);
}
}
// Try to unroll innermost loops.
for (auto *Loop : InnermostLoops)
Changed |= tryToUnrollLoop(Loop);
if (Changed) {
invalidateAnalysis(SILAnalysis::InvalidationKind::FunctionBody);
}
}
};
} // end anonymous namespace.
SILTransform *swift::createLoopUnroll() {
return new LoopUnrolling();
}