-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathOwnershipModelEliminator.cpp
353 lines (306 loc) · 12.1 KB
/
OwnershipModelEliminator.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
//===--- OwnershipModelEliminator.cpp - Eliminate SILOwnership Instr. -----===//
//
// 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
//
//===----------------------------------------------------------------------===//
///
/// \file
///
/// This file contains a small pass that lowers SIL ownership instructions to
/// their constituent operations. This will enable us to separate
/// implementation
/// of Semantic ARC in SIL and SILGen from ensuring that all of the optimizer
/// passes respect Semantic ARC. This is done by running this pass right after
/// SILGen and as the pass pipeline is updated, moving this pass further and
/// further back in the pipeline.
///
//===----------------------------------------------------------------------===//
#define DEBUG_TYPE "sil-ownership-model-eliminator"
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILFunction.h"
#include "swift/SIL/SILVisitor.h"
using namespace swift;
//===----------------------------------------------------------------------===//
// Implementation
//===----------------------------------------------------------------------===//
namespace {
struct OwnershipModelEliminatorVisitor
: SILInstructionVisitor<OwnershipModelEliminatorVisitor, bool> {
SILBuilder &B;
OwnershipModelEliminatorVisitor(SILBuilder &B) : B(B) {}
void beforeVisit(ValueBase *V) {
auto *I = cast<SILInstruction>(V);
B.setInsertionPoint(I);
B.setCurrentDebugScope(I->getDebugScope());
}
bool visitValueBase(ValueBase *V) { return false; }
bool visitLoadInst(LoadInst *LI);
bool visitStoreInst(StoreInst *SI);
bool visitStoreBorrowInst(StoreBorrowInst *SI);
bool visitCopyValueInst(CopyValueInst *CVI);
bool visitCopyUnownedValueInst(CopyUnownedValueInst *CVI);
bool visitDestroyValueInst(DestroyValueInst *DVI);
bool visitLoadBorrowInst(LoadBorrowInst *LBI);
bool visitBeginBorrowInst(BeginBorrowInst *BBI) {
BBI->replaceAllUsesWith(BBI->getOperand());
BBI->eraseFromParent();
return true;
}
bool visitEndBorrowInst(EndBorrowInst *EBI) {
EBI->eraseFromParent();
return true;
}
bool visitEndLifetimeInst(EndLifetimeInst *ELI) {
ELI->eraseFromParent();
return true;
}
bool visitUncheckedOwnershipConversionInst(
UncheckedOwnershipConversionInst *UOCI) {
UOCI->replaceAllUsesWith(UOCI->getOperand());
UOCI->eraseFromParent();
return true;
}
bool visitUnmanagedRetainValueInst(UnmanagedRetainValueInst *URVI);
bool visitUnmanagedReleaseValueInst(UnmanagedReleaseValueInst *URVI);
bool visitUnmanagedAutoreleaseValueInst(UnmanagedAutoreleaseValueInst *UAVI);
bool visitCheckedCastBranchInst(CheckedCastBranchInst *CBI);
bool visitSwitchEnumInst(SwitchEnumInst *SWI);
bool visitProjectBoxInst(ProjectBoxInst *PBI);
};
} // end anonymous namespace
bool OwnershipModelEliminatorVisitor::visitLoadInst(LoadInst *LI) {
auto Qualifier = LI->getOwnershipQualifier();
// If the qualifier is unqualified, there is nothing further to do
// here. Just return.
if (Qualifier == LoadOwnershipQualifier::Unqualified)
return false;
SILValue Result = B.emitLoadValueOperation(LI->getLoc(), LI->getOperand(),
LI->getOwnershipQualifier());
// Then remove the qualified load and use the unqualified load as the def of
// all of LI's uses.
LI->replaceAllUsesWith(Result);
LI->eraseFromParent();
return true;
}
bool OwnershipModelEliminatorVisitor::visitStoreInst(StoreInst *SI) {
auto Qualifier = SI->getOwnershipQualifier();
// If the qualifier is unqualified, there is nothing further to do
// here. Just return.
if (Qualifier == StoreOwnershipQualifier::Unqualified)
return false;
B.emitStoreValueOperation(SI->getLoc(), SI->getSrc(), SI->getDest(),
SI->getOwnershipQualifier());
// Then remove the qualified store.
SI->eraseFromParent();
return true;
}
bool OwnershipModelEliminatorVisitor::visitStoreBorrowInst(
StoreBorrowInst *SI) {
B.emitStoreValueOperation(SI->getLoc(), SI->getSrc(), SI->getDest(),
StoreOwnershipQualifier::Init);
// Then remove the qualified store.
SI->eraseFromParent();
return true;
}
bool
OwnershipModelEliminatorVisitor::visitLoadBorrowInst(LoadBorrowInst *LBI) {
// Break down the load borrow into an unqualified load.
auto *UnqualifiedLoad = B.createLoad(LBI->getLoc(), LBI->getOperand(),
LoadOwnershipQualifier::Unqualified);
// Then remove the qualified load and use the unqualified load as the def of
// all of LI's uses.
LBI->replaceAllUsesWith(UnqualifiedLoad);
LBI->eraseFromParent();
return true;
}
bool OwnershipModelEliminatorVisitor::visitCopyValueInst(CopyValueInst *CVI) {
// A copy_value of an address-only type cannot be replaced.
if (CVI->getType().isAddressOnly(B.getModule()))
return false;
// Now that we have set the unqualified ownership flag, destroy value
// operation will delegate to the appropriate strong_release, etc.
B.emitCopyValueOperation(CVI->getLoc(), CVI->getOperand());
CVI->replaceAllUsesWith(CVI->getOperand());
CVI->eraseFromParent();
return true;
}
bool OwnershipModelEliminatorVisitor::visitCopyUnownedValueInst(
CopyUnownedValueInst *CVI) {
B.createStrongRetainUnowned(CVI->getLoc(), CVI->getOperand(),
B.getDefaultAtomicity());
// Users of copy_value_unowned expect an owned value. So we need to convert
// our unowned value to a ref.
auto *UTRI =
B.createUnownedToRef(CVI->getLoc(), CVI->getOperand(), CVI->getType());
CVI->replaceAllUsesWith(UTRI);
CVI->eraseFromParent();
return true;
}
bool OwnershipModelEliminatorVisitor::visitUnmanagedRetainValueInst(
UnmanagedRetainValueInst *URVI) {
// Now that we have set the unqualified ownership flag, destroy value
// operation will delegate to the appropriate strong_release, etc.
B.emitCopyValueOperation(URVI->getLoc(), URVI->getOperand());
URVI->replaceAllUsesWith(URVI->getOperand());
URVI->eraseFromParent();
return true;
}
bool OwnershipModelEliminatorVisitor::visitUnmanagedReleaseValueInst(
UnmanagedReleaseValueInst *URVI) {
// Now that we have set the unqualified ownership flag, destroy value
// operation will delegate to the appropriate strong_release, etc.
B.emitDestroyValueOperation(URVI->getLoc(), URVI->getOperand());
URVI->eraseFromParent();
return true;
}
bool OwnershipModelEliminatorVisitor::visitUnmanagedAutoreleaseValueInst(
UnmanagedAutoreleaseValueInst *UAVI) {
// Now that we have set the unqualified ownership flag, destroy value
// operation will delegate to the appropriate strong_release, etc.
B.createAutoreleaseValue(UAVI->getLoc(), UAVI->getOperand(),
UAVI->getAtomicity());
UAVI->eraseFromParent();
return true;
}
bool OwnershipModelEliminatorVisitor::visitDestroyValueInst(DestroyValueInst *DVI) {
// A destroy_value of an address-only type cannot be replaced.
if (DVI->getOperand()->getType().isAddressOnly(B.getModule()))
return false;
// Now that we have set the unqualified ownership flag, destroy value
// operation will delegate to the appropriate strong_release, etc.
B.emitDestroyValueOperation(DVI->getLoc(), DVI->getOperand());
DVI->eraseFromParent();
return true;
}
bool OwnershipModelEliminatorVisitor::visitCheckedCastBranchInst(
CheckedCastBranchInst *CBI) {
// In ownership qualified SIL, checked_cast_br must pass its argument to the
// fail case so we can clean it up. In non-ownership qualified SIL, we expect
// no argument from the checked_cast_br in the default case. The way that we
// handle this transformation is that:
//
// 1. We replace all uses of the argument to the false block with a use of the
// checked cast branch's operand.
// 2. We delete the argument from the false block.
SILBasicBlock *FailureBlock = CBI->getFailureBB();
if (FailureBlock->getNumArguments() == 0)
return false;
FailureBlock->getArgument(0)->replaceAllUsesWith(CBI->getOperand());
FailureBlock->eraseArgument(0);
return true;
}
bool OwnershipModelEliminatorVisitor::visitSwitchEnumInst(
SwitchEnumInst *SWEI) {
// In ownership qualified SIL, switch_enum must pass its argument to the fail
// case so we can clean it up. In non-ownership qualified SIL, we expect no
// argument from the switch_enum in the default case. The way that we handle
// this transformation is that:
//
// 1. We replace all uses of the argument to the false block with a use of the
// checked cast branch's operand.
// 2. We delete the argument from the false block.
if (!SWEI->hasDefault())
return false;
SILBasicBlock *DefaultBlock = SWEI->getDefaultBB();
if (DefaultBlock->getNumArguments() == 0)
return false;
DefaultBlock->getArgument(0)->replaceAllUsesWith(SWEI->getOperand());
DefaultBlock->eraseArgument(0);
return true;
}
// Since we are threading through copies, we may have situations like:
//
// let x = alloc_box $Foo
// let y = project_box x
// let z = mark_uninitialized y
// ... use z ...
//
// let y2 = project_box x
//
// let x2 = copy_value x
// let y3 = project_box y
//
// We need to move project_box like y2 and y3 to go through z so that DI can
// reason about them.
//
// Once DI is updated for ownership, this can go away.
bool OwnershipModelEliminatorVisitor::visitProjectBoxInst(ProjectBoxInst *PBI) {
// First if our operand is already a mark_uninitialized, then we do not need
// to do anything.
auto *Use = PBI->getSingleUse();
if (Use && isa<MarkUninitializedInst>(Use->getUser())) {
return false;
}
// Otherwise, lets try to find the alloc_box.
SILValue BoxValue = PBI->getOperand();
while (auto *CVI = dyn_cast<CopyValueInst>(BoxValue)) {
BoxValue = CVI->getOperand();
}
// We were unable to find the alloc_box. This must be an indirect enum box
// pattern.
auto *ABI = dyn_cast<AllocBoxInst>(BoxValue);
if (!ABI)
return false;
// See if we can find (mark_uninitialized (project_box))
SILValue MUI;
for (auto *Use : ABI->getUses()) {
auto *BoxProjection = dyn_cast<ProjectBoxInst>(Use->getUser());
if (!BoxProjection)
continue;
auto *Op = BoxProjection->getSingleUse();
if (!Op || !isa<MarkUninitializedInst>(Op->getUser()))
continue;
MUI = SILValue(Op->getUser());
break;
}
// If we did not find a mark uninitialized inst, then this is not the pattern
// that we are looking for.
if (!MUI)
return false;
// Ok, we found it. Replace all uses of this project box with the
// mark_uninitialized and then erase it.
PBI->replaceAllUsesWith(MUI);
PBI->eraseFromParent();
return true;
}
//===----------------------------------------------------------------------===//
// Top Level Entry Point
//===----------------------------------------------------------------------===//
namespace {
struct OwnershipModelEliminator : SILModuleTransform {
void run() override {
for (auto &F : *getModule()) {
// Set F to have unqualified ownership.
F.setUnqualifiedOwnership();
bool MadeChange = false;
SILBuilder B(F);
OwnershipModelEliminatorVisitor Visitor(B);
for (auto &BB : F) {
for (auto II = BB.begin(), IE = BB.end(); II != IE;) {
// Since we are going to be potentially removing instructions, we need
// to make sure to increment our iterator before we perform any
// visits.
SILInstruction *I = &*II;
++II;
MadeChange |= Visitor.visit(I);
}
}
if (MadeChange) {
auto InvalidKind =
SILAnalysis::InvalidationKind::BranchesAndInstructions;
invalidateAnalysis(&F, InvalidKind);
}
}
}
};
} // end anonymous namespace
SILTransform *swift::createOwnershipModelEliminator() {
return new OwnershipModelEliminator();
}