-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathClosureScope.cpp
402 lines (336 loc) · 12.2 KB
/
ClosureScope.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
//===--- ClosureScope.cpp - Closure Scope Analysis ------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
///
/// Implementation of ClosureScopeAnalysis.
///
/// The "scope" of a closure is the function body that refers to the closure.
///
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "closure-scope"
#include "swift/SILOptimizer/Analysis/ClosureScope.h"
#include "swift/SIL/ApplySite.h"
#include "swift/SIL/SILModule.h"
#include "llvm/ADT/iterator.h"
namespace swift {
// A function is a node in this graph if it either refers to a closure or is
// itself a closure.
//
// The graph is represented with two vectors. One for forward edges from "scope
// functions" to closures. Another with backward edges from closures to
// "scope functions".
class ClosureGraph {
struct Edge {
SILFunction *from;
SILFunction *to;
bool operator<(const Edge &other) const {
unsigned from1 = from->getIndex();
unsigned from2 = other.from->getIndex();
if (from1 != from2)
return from1 < from2;
return to->getIndex() < other.to->getIndex();
}
};
using EdgePos = std::vector<Edge>::iterator;
using EdgeRange = llvm::iterator_range<EdgePos>;
struct NodeIterator
: llvm::iterator_adaptor_base<
NodeIterator, EdgePos, std::random_access_iterator_tag,
SILFunction *, ptrdiff_t, SILFunction *, SILFunction *> {
NodeIterator() = default;
explicit NodeIterator(EdgePos pos) : iterator_adaptor_base(pos) {}
EdgePos edgeIter() const { return I; }
SILFunction *operator*() const { return I->from; }
};
// Compare the node identified by the from index of \p edge with the node
// identified by \p functionIndex.
struct NodeCompare {
bool operator()(SILFunction *node1, SILFunction *node2) {
return node1->getIndex() < node2->getIndex();
}
};
// A set of edges in the relational mapping from a function to the set of
// nonescaping closures that are referenced within its body.
//
// from: parent function
// to: nonescaping child closure
std::vector<Edge> functionToClosures;
// Map every nonescaping closure to the parent function that refers to it.
//
// from: nonescaping child closure
// to: parent function
//
// A closure almost always has a single parent. A local function, however, may
// be referenced recursively, even within a different closure. Notice that
// capturedInt is captured from the outer-most function, but is passed down
//
// func localFunc(b: Apply) {
// capturedInt += 1
// let closure = { (c: Apply) in
// c.apply(localFunc)
// }
// b.apply(closure)
// }
// a.apply(localFunc)
std::vector<Edge> closureToFunctions;
public:
ClosureGraph() = default;
/// Visit the parent scopes of \p closure if it has any. If \p visitor returns
/// false, exit early and return false. Otherwise return true.
bool visitClosureScopes(SILFunction *closure,
std::function<bool(SILFunction *scopeFunc)> visitor);
/// Visit the closures directly referenced by \p scopeFunc.
bool visitClosures(SILFunction *scopeFunc,
std::function<bool(SILFunction *closure)> visitor);
/// Called when a \p function is removed from this module.
void erase(SILFunction *function);
/// Record all closure scopes in this module.
void compute(SILModule *M);
protected:
ClosureGraph(const ClosureGraph &) = delete;
ClosureGraph &operator=(const ClosureGraph &) = delete;
EdgeRange getEdgeRange(SILFunction *node, std::vector<Edge> &edges) {
auto it = std::lower_bound(NodeIterator(edges.begin()),
NodeIterator(edges.end()),
node, NodeCompare());
auto next = it.edgeIter();
for (auto end = edges.end(); next != end; ++next) {
if (next->from != node)
break;
}
return EdgeRange(it.edgeIter(), next);
}
EdgeRange getFunctionToClosureEdges(SILFunction *scopeFunc) {
return getEdgeRange(scopeFunc, functionToClosures);
}
EdgeRange getClosureToFunctionEdges(SILFunction *closure) {
return getEdgeRange(closure, closureToFunctions);
}
void recordScope(ApplySite apply);
void finalize();
SWIFT_ASSERT_ONLY_DECL(void dump());
};
bool ClosureGraph::visitClosureScopes(
SILFunction *closure, std::function<bool(SILFunction *scopeFunc)> visitor) {
for (ClosureGraph::Edge &edge : getClosureToFunctionEdges(closure)) {
if (!visitor(edge.to))
return false;
}
return true;
}
bool ClosureGraph::visitClosures(
SILFunction *scopeFunc, std::function<bool(SILFunction *closure)> visitor) {
for (ClosureGraph::Edge &edge : getFunctionToClosureEdges(scopeFunc)) {
if (!visitor(edge.to))
return false;
}
return true;
}
void ClosureGraph::erase(SILFunction *function) {
struct RefersToFunction {
SILFunction *function;
bool operator()(const Edge &edge) {
return edge.from == function || edge.to == function;
}
};
llvm::erase_if(functionToClosures, RefersToFunction{function});
llvm::erase_if(closureToFunctions, RefersToFunction{function});
}
// Handle both partial_apply and directly applied closures of the form:
// %f = function_ref @... : $(...inout_aliasable...) -> ...
// apply %f(...)
void ClosureGraph::recordScope(ApplySite apply) {
// Only track scopes of non-escaping closures.
auto closureTy = apply.getCallee()->getType().castTo<SILFunctionType>();
// FIXME: isCalleeDynamicallyReplaceable should not be true but can today
// because local functions can be marked dynamic.
if (!isNonEscapingClosure(closureTy)
|| apply.isCalleeDynamicallyReplaceable()) {
return;
}
auto closureFunc = apply.getCalleeFunction();
assert(closureFunc && "non-escaping closure needs a direct partial_apply.");
auto scopeFunc = apply.getFunction();
// Passes may assume that a deserialized function can only refer to
// deserialized closures. For example, AccessEnforcementSelection skips
// deserialized functions but assumes all a closure's parent scope have been
// processed.
assert(scopeFunc->wasDeserializedCanonical() ==
closureFunc->wasDeserializedCanonical() &&
"A closure cannot be serialized in a different module than its "
"parent context");
functionToClosures.push_back({scopeFunc, closureFunc});
closureToFunctions.push_back({closureFunc, scopeFunc});
}
void ClosureGraph::finalize() {
llvm::stable_sort(functionToClosures);
llvm::stable_sort(closureToFunctions);
LLVM_DEBUG(dump());
}
#ifndef NDEBUG
static void dumpFunctionName(SILFunction *function) {
auto opts = Demangle::DemangleOptions::SimplifiedUIDemangleOptions();
opts.ShowAsyncResumePartial = true;
llvm::dbgs() << Demangle::demangleSymbolAsString(function->getName(), opts)
<< " '" << function->getName() << "'\n";
}
void ClosureGraph::dump() {
llvm::dbgs() << "\n";
SILFunction *currentFunc = nullptr;
for (auto &edge : functionToClosures) {
auto *scopeFunc = edge.from;
if (currentFunc != scopeFunc) {
currentFunc = scopeFunc;
llvm::dbgs() << "SCOPE: ";
dumpFunctionName(scopeFunc);
}
llvm::dbgs() << " CLOSURE: ";
dumpFunctionName(edge.to);
}
currentFunc = nullptr;
for (auto &edge : closureToFunctions) {
auto *closure = edge.from;
if (currentFunc != closure) {
currentFunc = closure;
llvm::dbgs() << "CLOSURE: ";
dumpFunctionName(closure);
}
llvm::dbgs() << " SCOPE: ";
dumpFunctionName(edge.to);
}
}
#endif
void ClosureGraph::compute(SILModule *M) {
for (auto &F : *M) {
for (auto &BB : F) {
for (auto &I : BB) {
if (auto apply = ApplySite::isa(&I)) {
recordScope(apply);
}
}
}
}
finalize();
}
ClosureScopeAnalysis::ClosureScopeAnalysis(SILModule *M)
: SILAnalysis(SILAnalysisKind::ClosureScope), M(M), scopeGraph(nullptr) {}
ClosureScopeAnalysis::~ClosureScopeAnalysis() = default;
bool ClosureScopeAnalysis::visitClosureScopes(
SILFunction *closure, std::function<bool(SILFunction *scopeFunc)> visitor) {
return getOrComputeGraph()->visitClosureScopes(closure, visitor);
}
bool ClosureScopeAnalysis::visitClosures(
SILFunction *scopeFunc, std::function<bool(SILFunction *closure)> visitor) {
return getOrComputeGraph()->visitClosures(scopeFunc, visitor);
}
void ClosureScopeAnalysis::invalidate() {
scopeGraph.reset();
}
void ClosureScopeAnalysis::notifyWillDeleteFunction(SILFunction *F) {
if (scopeGraph)
scopeGraph->erase(F);
}
ClosureGraph *ClosureScopeAnalysis::getOrComputeGraph() {
if (!scopeGraph) {
scopeGraph = std::make_unique<ClosureGraph>();
scopeGraph->compute(M);
}
return scopeGraph.get();
}
SILAnalysis *createClosureScopeAnalysis(SILModule *M) {
return new ClosureScopeAnalysis(M);
}
class ClosureFunctionOrder::ClosureDFS {
ClosureFunctionOrder &functionOrder;
llvm::SmallBitVector visited;
llvm::SmallBitVector finished;
SmallVector<SILFunction *, 4> postorderClosures;
public:
ClosureDFS(ClosureFunctionOrder &functionOrder)
: functionOrder(functionOrder),
visited(functionOrder.csa->getModule()->getNumFunctionIndices()),
finished(functionOrder.csa->getModule()->getNumFunctionIndices())
{}
bool isVisited(SILFunction *function) const {
return visited.test(function->getIndex());
}
void performDFS(SILFunction *root) {
postorderClosures.clear();
recursiveDFS(root);
// Closures are discovered in postorder, bottom-up.
// Reverse-append them onto the top-down function list.
llvm::append_range(functionOrder.topDownFunctions,
llvm::reverse(postorderClosures));
}
protected:
void recursiveDFS(SILFunction *function) {
unsigned index = function->getIndex();
if (visited.test(index)) {
if (!finished.test(index)) {
// Cycle in the closure graph.
functionOrder.closureCycleHeads.insert(function);
}
return;
}
visited.set(index);
functionOrder.csa->visitClosures(function, [this](SILFunction *closure) {
recursiveDFS(closure);
return true;
});
finished.set(index);
postorderClosures.push_back(function);
};
};
void ClosureFunctionOrder::compute() {
auto *module = csa->getModule();
assert(topDownFunctions.empty() && closureCycleHeads.empty() &&
"attempting to recompute");
topDownFunctions.reserve(module->getNumFunctionIndices());
ClosureDFS dfs(*this);
SmallVector<SILFunction *, 4> closureWorklist;
unsigned numFunctions = 0;
for (auto &function : module->getFunctionList()) {
++numFunctions;
if (dfs.isVisited(&function))
continue;
if (csa->isReachableClosure(&function)) {
// This is a closure, reachable from some other function. Reaching this
// point is rare, because closures typically follow their parent in the
// function list.
//
// A closure at the head of a cycle might only be reachable from other
// closures. So use a worklist to visit them once more as DFS roots after
// finishing DFS from all acyclic functions.
closureWorklist.push_back(&function);
continue;
}
dfs.performDFS(&function);
}
// Revisit closures in forward order. DFS will immediately return except in
// the unlikely event that this is an orphaned closure.
for (auto *closure : closureWorklist) {
dfs.performDFS(closure);
}
LLVM_DEBUG(dump());
assert(numFunctions == topDownFunctions.size() && "DFS missed a function");
}
#ifndef NDEBUG
void ClosureFunctionOrder::dump() {
llvm::dbgs() << "\nRPO function order:\n";
for (auto *function : getTopDownFunctions()) {
llvm::dbgs() << "[" << function->getIndex() << "] ";
if (isHeadOfClosureCycle(function))
llvm::dbgs() << "CYCLE HEAD: ";
dumpFunctionName(function);
}
}
#endif
} // namespace swift