-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathMoveOnlyChecker.cpp
293 lines (255 loc) · 10.1 KB
/
MoveOnlyChecker.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
//===--- MoveOnlyChecker.cpp ----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2022 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-move-only-checker"
#include "swift/AST/AccessScope.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticsSIL.h"
#include "swift/AST/SemanticAttrs.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/FrozenMultiMap.h"
#include "swift/Basic/SmallBitVector.h"
#include "swift/SIL/ApplySite.h"
#include "swift/SIL/BasicBlockBits.h"
#include "swift/SIL/BasicBlockData.h"
#include "swift/SIL/BasicBlockDatastructures.h"
#include "swift/SIL/BasicBlockUtils.h"
#include "swift/SIL/Consumption.h"
#include "swift/SIL/DebugUtils.h"
#include "swift/SIL/FieldSensitivePrunedLiveness.h"
#include "swift/SIL/InstructionUtils.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/SIL/OSSALifetimeCompletion.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/PrunedLiveness.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILArgumentConvention.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILUndef.h"
#include "swift/SIL/SILValue.h"
#include "swift/SILOptimizer/Analysis/ClosureScope.h"
#include "swift/SILOptimizer/Analysis/DeadEndBlocksAnalysis.h"
#include "swift/SILOptimizer/Analysis/DominanceAnalysis.h"
#include "swift/SILOptimizer/Analysis/NonLocalAccessBlockAnalysis.h"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SILOptimizer/Utils/CanonicalizeOSSALifetime.h"
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PointerIntPair.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "MoveOnlyAddressCheckerUtils.h"
#include "MoveOnlyDiagnostics.h"
#include "MoveOnlyObjectCheckerUtils.h"
#include "MoveOnlyUtils.h"
using namespace swift;
using namespace swift::siloptimizer;
//===----------------------------------------------------------------------===//
// MARK: Top Level Object Entrypoint
//===----------------------------------------------------------------------===//
namespace {
struct MoveOnlyChecker {
DiagnosticEmitter diagnosticEmitter;
SILFunction *fn;
DominanceInfo *domTree;
PostOrderAnalysis *poa;
DeadEndBlocksAnalysis *deba;
bool madeChange = false;
borrowtodestructure::IntervalMapAllocator allocator;
MoveOnlyChecker(SILFunction *fn, DominanceInfo *domTree,
PostOrderAnalysis *poa, DeadEndBlocksAnalysis *deba)
: diagnosticEmitter(fn), fn(fn), domTree(domTree), poa(poa), deba(deba) {}
void checkObjects();
void completeObjectLifetimes(ArrayRef<MarkUnresolvedNonCopyableValueInst *>);
void checkAddresses();
};
} // namespace
void MoveOnlyChecker::checkObjects() {
llvm::SmallSetVector<MarkUnresolvedNonCopyableValueInst *, 32>
moveIntroducersToProcess;
unsigned diagCount = diagnosticEmitter.getDiagnosticCount();
madeChange |= searchForCandidateObjectMarkUnresolvedNonCopyableValueInsts(
fn, moveIntroducersToProcess, diagnosticEmitter);
LLVM_DEBUG(
llvm::dbgs()
<< "Emitting diagnostic when checking for mark must check inst: "
<< (diagCount != diagnosticEmitter.getDiagnosticCount() ? "yes" : "no")
<< '\n');
if (moveIntroducersToProcess.empty()) {
LLVM_DEBUG(llvm::dbgs()
<< "No move introducers found?! Returning early?!\n");
return;
}
completeObjectLifetimes(moveIntroducersToProcess.getArrayRef());
MoveOnlyObjectChecker checker{diagnosticEmitter, domTree, deba, poa,
allocator};
madeChange |= checker.check(moveIntroducersToProcess);
}
void MoveOnlyChecker::completeObjectLifetimes(
ArrayRef<MarkUnresolvedNonCopyableValueInst *> insts) {
// TODO: Delete once OSSALifetimeCompletion is run as part of SILGenCleanup.
OSSALifetimeCompletion completion(fn, domTree, *deba->get(fn));
// Collect all values derived from each mark_unresolved_non_copyable_value
// instruction via ownership instructions and phis.
ValueWorklist transitiveValues(fn);
for (auto *inst : insts) {
transitiveValues.push(inst);
}
while (auto value = transitiveValues.pop()) {
for (auto *use : value->getUses()) {
auto *user = use->getUser();
switch (user->getKind()) {
case SILInstructionKind::BeginBorrowInst:
case SILInstructionKind::CopyValueInst:
case SILInstructionKind::MoveValueInst:
transitiveValues.pushIfNotVisited(cast<SingleValueInstruction>(user));
break;
case SILInstructionKind::BranchInst: {
PhiOperand po(use);
transitiveValues.pushIfNotVisited(po.getValue());
break;
}
default: {
auto forward = ForwardingOperation(user);
if (!forward)
continue;
forward.visitForwardedValues([&transitiveValues](auto forwarded) {
transitiveValues.pushIfNotVisited(forwarded);
return true;
});
break;
}
}
}
}
// Complete the lifetime of each collected value. This is a subset of the
// work that SILGenCleanup will do.
for (auto *block : poa->get(fn)->getPostOrder()) {
for (SILInstruction &inst : reverse(*block)) {
for (auto result : inst.getResults()) {
if (!transitiveValues.isVisited(result))
continue;
if (completion.completeOSSALifetime(
result, OSSALifetimeCompletion::Boundary::Availability) ==
LifetimeCompletion::WasCompleted) {
madeChange = true;
}
}
}
for (SILArgument *arg : block->getArguments()) {
assert(!arg->isReborrow() && "reborrows not legal at this SIL stage");
if (!transitiveValues.isVisited(arg))
continue;
if (completion.completeOSSALifetime(
arg, OSSALifetimeCompletion::Boundary::Availability) ==
LifetimeCompletion::WasCompleted) {
madeChange = true;
}
}
}
}
void MoveOnlyChecker::checkAddresses() {
unsigned diagCount = diagnosticEmitter.getDiagnosticCount();
llvm::SmallSetVector<MarkUnresolvedNonCopyableValueInst *, 32>
moveIntroducersToProcess;
searchForCandidateAddressMarkUnresolvedNonCopyableValueInsts(
fn, poa, moveIntroducersToProcess, diagnosticEmitter);
LLVM_DEBUG(
llvm::dbgs()
<< "Emitting diagnostic when checking for mark must check inst: "
<< (diagCount != diagnosticEmitter.getDiagnosticCount() ? "yes" : "no")
<< '\n');
if (moveIntroducersToProcess.empty()) {
LLVM_DEBUG(llvm::dbgs()
<< "No move introducers found?! Returning early?!\n");
return;
}
MoveOnlyAddressChecker checker{
fn, diagnosticEmitter, allocator, domTree, poa, deba};
madeChange |= checker.completeLifetimes();
madeChange |= checker.check(moveIntroducersToProcess);
}
//===----------------------------------------------------------------------===//
// MARK: Top Level Entrypoint
//===----------------------------------------------------------------------===//
namespace {
static bool canonicalizeLoadBorrows(SILFunction *F) {
bool changed = false;
for (auto &block : *F) {
for (auto &inst : block) {
if (auto *lbi = dyn_cast<LoadBorrowInst>(&inst)) {
if (lbi->isUnchecked()) {
changed = true;
lbi->setUnchecked(false);
}
}
}
}
return changed;
}
class MoveOnlyCheckerPass : public SILFunctionTransform {
void run() override {
auto *fn = getFunction();
// Don't rerun diagnostics on deserialized functions.
if (getFunction()->wasDeserializedCanonical())
return;
assert(fn->getModule().getStage() == SILStage::Raw &&
"Should only run on Raw SIL");
// If an earlier pass told use to not emit diagnostics for this function,
// clean up any copies, invalidate the analysis, and return early.
if (fn->hasSemanticsAttr(semantics::NO_MOVEONLY_DIAGNOSTICS)) {
bool didChange = canonicalizeLoadBorrows(fn);
didChange |= cleanupNonCopyableCopiesAfterEmittingDiagnostic(getFunction());
if (didChange) {
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
}
return;
}
LLVM_DEBUG(llvm::dbgs()
<< "===> MoveOnly Checker. Visiting: " << fn->getName() << '\n');
MoveOnlyChecker checker(fn, getAnalysis<DominanceAnalysis>()->get(fn),
getAnalysis<PostOrderAnalysis>(),
getAnalysis<DeadEndBlocksAnalysis>());
checker.checkObjects();
checker.checkAddresses();
// If we did not emit any diagnostics, emit an error on any copies that
// remain. If we emitted a diagnostic, we just want to rewrite all of the
// non-copyable copies into explicit variants below and let the user
// recompile.
if (!checker.diagnosticEmitter.emittedDiagnostic()) {
emitCheckerMissedCopyOfNonCopyableTypeErrors(fn,
checker.diagnosticEmitter);
}
// Remaining borrows
// should be correctly immutable. We can canonicalize any remaining
// `load_borrow [unchecked]` instructions.
checker.madeChange |= canonicalizeLoadBorrows(fn);
checker.madeChange |=
cleanupNonCopyableCopiesAfterEmittingDiagnostic(fn);
if (checker.madeChange)
invalidateAnalysis(SILAnalysis::InvalidationKind::Instructions);
}
};
} // namespace
SILTransform *swift::createMoveOnlyChecker() {
return new MoveOnlyCheckerPass();
}