-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathAccessStorageAnalysis.cpp
398 lines (351 loc) · 14.8 KB
/
AccessStorageAnalysis.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
//===--- AccessStorageAnalysis.cpp - Accessed Storage Analysis ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 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
//
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-sea"
#include "swift/SILOptimizer/Analysis/AccessStorageAnalysis.h"
#include "swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h"
#include "swift/SILOptimizer/Analysis/FunctionOrder.h"
#include "swift/SILOptimizer/PassManager/PassManager.h"
using namespace swift;
// -----------------------------------------------------------------------------
// MARK: Accessing the results.
// -----------------------------------------------------------------------------
bool AccessStorageResult::hasNoNestedConflict(
const AccessStorage &otherStorage) const {
assert(otherStorage.isUniquelyIdentified());
assert(!hasUnidentifiedAccess());
return getStorageAccessInfo(otherStorage).hasNoNestedConflict();
}
bool AccessStorageResult::mayConflictWith(
SILAccessKind otherAccessKind, const AccessStorage &otherStorage) const {
if (hasUnidentifiedAccess()
&& accessKindMayConflict(otherAccessKind,
unidentifiedAccess.value())) {
return true;
}
for (auto &storageAccess : storageAccessSet) {
assert(storageAccess && "FunctionAccessStorage mapped invalid storage.");
if (!accessKindMayConflict(otherAccessKind, storageAccess.getAccessKind()))
continue;
if (!otherStorage.isDistinctFrom(storageAccess))
return true;
}
return false;
}
StorageAccessInfo AccessStorageResult::getStorageAccessInfo(
const AccessStorage &otherStorage) const {
// Construct a fake StorageAccessInfo to do a hash lookup for the real
// StorageAccessInfo. The DenseSet key is limited to the AccessStorage base
// class members.
StorageAccessInfo storageKey(otherStorage, SILAccessKind::Read, false);
auto iter = storageAccessSet.find(storageKey);
assert(iter != storageAccessSet.end());
return *iter;
}
// -----------------------------------------------------------------------------
// MARK: Constructing the results.
// -----------------------------------------------------------------------------
static bool updateAccessKind(SILAccessKind &LHS, SILAccessKind RHS) {
bool changed = false;
// Assume we don't track Init/Deinit.
if (LHS == SILAccessKind::Read && RHS == SILAccessKind::Modify) {
LHS = RHS;
changed = true;
}
return changed;
}
static bool updateOptionalAccessKind(Optional<SILAccessKind> &LHS,
Optional<SILAccessKind> RHS) {
if (RHS == None)
return false;
if (LHS == None) {
LHS = RHS;
return true;
}
return updateAccessKind(LHS.value(), RHS.value());
}
bool StorageAccessInfo::mergeFrom(const StorageAccessInfo &RHS) {
bool changed = false;
SILAccessKind accessKind = getAccessKind();
assert(accessKind == SILAccessKind::Read
|| accessKind == SILAccessKind::Modify && "uninitialized info");
if (updateAccessKind(accessKind, RHS.getAccessKind())) {
setAccessKind(accessKind);
changed = true;
}
if (hasNoNestedConflict() && !RHS.hasNoNestedConflict()) {
setNoNestedConflict(false);
changed = true;
}
return changed;
}
bool AccessStorageResult::updateUnidentifiedAccess(SILAccessKind accessKind) {
if (unidentifiedAccess == None) {
unidentifiedAccess = accessKind;
return true;
}
return updateAccessKind(unidentifiedAccess.value(), accessKind);
}
// Merge the given AccessStorageResult in `other` into this
// AccessStorageResult. Use the given `transformStorage` to map `other`
// AccessStorage into this context. If `other` is from a callee, argument
// substitution will be performed if possible. However, there's no guarantee
// that the merged access values will belong to this region.
//
// Return true if these results changed, requiring further propagation through
// the call graph.
bool AccessStorageResult::mergeAccesses(
const AccessStorageResult &other,
std::function<StorageAccessInfo(const StorageAccessInfo &)>
transformStorage) {
// The cost of BottomUpIPAnalysis can be quadratic for large recursive call
// graphs. That cost is multiplied by the size of storageAccessSet. Slowdowns
// can occur ~1000 elements. 200 is large enough to cover "normal" code,
// while ensuring compile time isn't affected.
if (storageAccessSet.size() > 200) {
setWorstEffects();
return true;
}
// To save compile time, if this storage already has worst-case effects, avoid
// growing its storageAccessSet.
if (hasWorstEffects())
return false;
// When `this` == `other` (for self-recursion), insertion in DenseMap
// invalidates the iterator. We still need to propagate and merge in that case
// because arguments can be recursively dependent. The alternative would be
// treating all self-recursion conservatively.
const AccessStorageResult *otherRegionAccesses = &other;
AccessStorageResult regionAccessCopy;
if (this == &other) {
regionAccessCopy = other;
otherRegionAccesses = ®ionAccessCopy;
}
bool changed = false;
// Nondeterministically iterate for the sole purpose of inserting into another
// unordered set.
for (auto &rawStorageInfo : otherRegionAccesses->storageAccessSet) {
const StorageAccessInfo &otherStorageInfo =
transformStorage(rawStorageInfo);
// If transformStorage() returns invalid storage object for local storage,
// that should not be merged with the caller.
if (!otherStorageInfo)
continue;
if (otherStorageInfo.getKind() == AccessStorage::Unidentified) {
changed |= updateUnidentifiedAccess(otherStorageInfo.getAccessKind());
continue;
}
// Attempt to add identified AccessStorage to this map.
auto result = insertStorageAccess(otherStorageInfo);
if (result.second) {
// A new AccessStorage key was added to this map.
changed = true;
continue;
}
// Merge StorageAccessInfo into already-mapped AccessStorage.
changed |= result.first->mergeFrom(otherStorageInfo);
}
if (other.unidentifiedAccess != None)
changed |= updateUnidentifiedAccess(other.unidentifiedAccess.value());
return changed;
}
bool AccessStorageResult::mergeFrom(const AccessStorageResult &other) {
// Merge accesses from other. Both `this` and `other` are either from the same
// function or are both callees of the same call site, so their parameters
// indices coincide. transformStorage is the identity function.
return mergeAccesses(other, [](const StorageAccessInfo &s) { return s; });
}
/// Returns the argument of the full apply or partial apply corresponding to the
/// callee's parameter index, or returns an invalid SILValue if the applied
/// closure cannot be found. This walks up the apply chain starting at the given
/// `fullApply` to find the applied argument.
static SILValue getCallerArg(FullApplySite fullApply, unsigned paramIndex) {
if (paramIndex < fullApply.getNumArguments())
return fullApply.getArgument(paramIndex);
SILValue callee = fullApply.getCalleeOrigin();
auto *PAI = dyn_cast<PartialApplyInst>(callee);
if (!PAI)
return SILValue();
unsigned appliedIndex =
paramIndex - ApplySite(PAI).getCalleeArgIndexOfFirstAppliedArg();
if (appliedIndex < PAI->getNumArguments())
return PAI->getArgument(appliedIndex);
// This must be a chain of partial_applies. We don't expect this in practice,
// so handle it conservatively.
return SILValue();
}
/// Transform AccessStorage from a callee into the caller context. If this is
/// uniquely identified local storage, then return an invalid storage object.
///
/// For correctness, AccessEnforcementOpts relies on all Argument access to
/// either be mapped into the caller's context or marked as an unidentified
/// access at the call site.
///
/// Note: This does *not* map the storage index into the caller function's index
/// range. (When the storage value doesn't need to be remapped, it returns the
/// original storage value.) It's simpler to set the storage index later when it
/// is actually added to the function's storageAccessSet.
static StorageAccessInfo
transformCalleeStorage(const StorageAccessInfo &storage,
FullApplySite fullApply) {
if (storage.isLocal()) {
// Do not merge local storage.
return StorageAccessInfo(AccessStorage(), storage);
}
// Remap reference storage. The caller's argument becomes the new object. The
// old storage info is inherited.
if (storage.isReference()) {
auto object = storage.getObject();
if (auto *arg = dyn_cast<SILFunctionArgument>(object)) {
if (SILValue argVal = getCallerArg(fullApply, arg->getIndex())) {
// Remap this storage info. The argument source value is now the new
// object. The old storage info is inherited.
auto callerStorage = storage.transformReference(argVal);
return StorageAccessInfo(callerStorage, storage);
}
}
// Continue using the callee value as the storage object.
return storage;
}
switch (storage.getKind()) {
case AccessStorage::Box:
case AccessStorage::Class:
case AccessStorage::Tail:
case AccessStorage::Stack:
llvm_unreachable("Handled immediately above");
case AccessStorage::Nested:
llvm_unreachable("Nested storage should not be used here");
case AccessStorage::Global:
// Global accesses is universal.
return storage;
case AccessStorage::Yield:
// Continue to hold on to yields from the callee because we don't have
// any better placeholder in the callee.
return storage;
case AccessStorage::Unidentified:
// For unidentified storage, continue to reference the value in the callee
// because we don't have any better placeholder for a callee-defined object.
return storage;
case AccessStorage::Argument: {
// Transitively search for the storage base in the caller.
if (SILValue argVal = getCallerArg(fullApply, storage.getParamIndex())) {
// Remap the argument source value and inherit the old storage info.
if (auto calleeStorage = AccessStorage::compute(argVal))
return StorageAccessInfo(calleeStorage, storage);
}
// If the argument can't be transformed, demote it to an unidentified
// access.
//
// This is an untested bailout. It is only reachable if the call graph
// contains an edge that getCallerArg is unable to analyze OR if
// AccessStorage::compute returns an invalid SILValue, which won't
// pass SIL verification.
//
// TODO: To handle invalid argVal, consider allowing Unidentified access for
// invalid values. This may be useful for partially invalidating results.
return StorageAccessInfo(
AccessStorage(storage.getValue(), AccessStorage::Unidentified),
storage);
}
}
llvm_unreachable("unhandled kind");
}
bool AccessStorageResult::mergeFromApply(
const AccessStorageResult &calleeAccess, FullApplySite fullApply) {
// Merge accesses from calleeAccess. Transform any Argument type
// AccessStorage into the caller context to be added to `this` storage map.
return mergeAccesses(calleeAccess, [&fullApply](const StorageAccessInfo &s) {
return transformCalleeStorage(s, fullApply);
});
}
template <typename B>
void AccessStorageResult::visitBeginAccess(B *beginAccess) {
if (beginAccess->getEnforcement() != SILAccessEnforcement::Dynamic)
return;
auto storage = AccessStorage::compute(beginAccess->getSource());
if (storage.getKind() == AccessStorage::Unidentified) {
// This also catches invalid storage.
updateOptionalAccessKind(unidentifiedAccess, beginAccess->getAccessKind());
return;
}
StorageAccessInfo storageAccess(storage, beginAccess);
auto result = insertStorageAccess(storageAccess);
if (!result.second)
result.first->mergeFrom(storageAccess);
}
void AccessStorageResult::analyzeInstruction(SILInstruction *I) {
assert(!FullApplySite::isa(I) && "caller should merge");
if (auto *BAI = dyn_cast<BeginAccessInst>(I))
visitBeginAccess(BAI);
else if (auto *BUAI = dyn_cast<BeginUnpairedAccessInst>(I))
visitBeginAccess(BUAI);
}
void StorageAccessInfo::print(raw_ostream &os) const {
os << " [" << getSILAccessKindName(getAccessKind()) << "] ";
if (hasNoNestedConflict())
os << "[no_nested_conflict] ";
AccessStorage::print(os);
}
void StorageAccessInfo::dump() const { print(llvm::dbgs()); }
void AccessStorageResult::print(raw_ostream &os) const {
for (auto &storageAccess : storageAccessSet)
storageAccess.print(os);
if (unidentifiedAccess != None) {
os << " unidentified accesses: "
<< getSILAccessKindName(unidentifiedAccess.value()) << "\n";
}
}
void AccessStorageResult::dump() const { print(llvm::dbgs()); }
// -----------------------------------------------------------------------------
// MARK: FunctionAccessStorage, implementation of
// GenericFunctionEffectAnalysis.
// -----------------------------------------------------------------------------
bool FunctionAccessStorage::summarizeFunction(SILFunction *F) {
assert(accessResult.isEmpty() && "expected uninitialized results.");
if (F->isDefinition())
return false;
// If the function definition is unavailable, set unidentifiedAccess to a
// conservative value, since analyzeInstruction will never be called.
//
// If FunctionSideEffects can be summarized, use that information.
FunctionSideEffects functionSideEffects;
if (!functionSideEffects.summarizeFunction(F)) {
setWorstEffects();
// May as well consider this a successful summary since there are no
// instructions to visit anyway.
return true;
}
bool mayRead = functionSideEffects.getGlobalEffects().mayRead();
bool mayWrite = functionSideEffects.getGlobalEffects().mayWrite();
for (auto ¶mEffects : functionSideEffects.getParameterEffects()) {
mayRead |= paramEffects.mayRead();
mayWrite |= paramEffects.mayWrite();
}
if (mayWrite)
accessResult.setUnidentifiedAccess(SILAccessKind::Modify);
else if (mayRead)
accessResult.setUnidentifiedAccess(SILAccessKind::Read);
// If function side effects is "readnone" then this result will have an empty
// storageAccessSet and unidentifiedAccess == None.
return true;
}
bool FunctionAccessStorage::summarizeCall(FullApplySite fullApply) {
assert(accessResult.isEmpty() && "expected uninitialized results.");
if (SILFunction *callee = fullApply.getReferencedFunctionOrNull()) {
if (callee->getName() == "_swift_stdlib_malloc_size" ||
callee->getName() == "_swift_stdlib_has_malloc_size") {
return true;
}
}
return false;
}
SILAnalysis *swift::createAccessStorageAnalysis(SILModule *) {
return new AccessStorageAnalysis();
}