-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathScopedAddressUtils.cpp
255 lines (227 loc) · 8.15 KB
/
ScopedAddressUtils.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
//===--- ScopedAddressUtils.cpp -------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "swift/SIL/ScopedAddressUtils.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/PrunedLiveness.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBuilder.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SILOptimizer/Utils/InstructionDeleter.h"
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
using namespace swift;
void ScopedAddressValueKind::print(llvm::raw_ostream &os) const {
switch (value) {
case ScopedAddressValueKind::Invalid:
llvm_unreachable("Using invalid case?!");
case ScopedAddressValueKind::StoreBorrow:
os << "StoreBorrow";
return;
case ScopedAddressValueKind::BeginAccess:
os << "BeginAccess";
return;
}
llvm_unreachable("Covered switch isn't covered?!");
}
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
ScopedAddressValueKind kind) {
kind.print(os);
return os;
}
bool ScopedAddressValue::isScopeEndingUse(Operand *op) const {
switch (kind) {
case ScopedAddressValueKind::Invalid:
llvm_unreachable("Using invalid case?!");
case ScopedAddressValueKind::StoreBorrow: {
if (auto *endBorrow = dyn_cast<EndBorrowInst>(op->getUser())) {
return endBorrow->getOperand() == value;
}
return false;
}
case ScopedAddressValueKind::BeginAccess: {
if (auto *endAccess = dyn_cast<EndAccessInst>(op->getUser())) {
return endAccess->getOperand() == value;
}
return false;
}
}
}
bool ScopedAddressValue::visitScopeEndingUses(
function_ref<bool(Operand *)> visitor) const {
switch (kind) {
case ScopedAddressValueKind::Invalid:
llvm_unreachable("Using invalid case?!");
case ScopedAddressValueKind::StoreBorrow: {
for (auto *use : value->getUses()) {
if (isa<EndBorrowInst>(use->getUser())) {
if (!visitor(use))
return false;
}
}
return true;
}
case ScopedAddressValueKind::BeginAccess: {
for (auto *use : value->getUses()) {
if (isa<EndAccessInst>(use->getUser())) {
if (!visitor(use))
return false;
}
}
return true;
}
}
}
// Note: This is used to fixup an incomplete address scope, so cannot assume the
// scope's lifetime is already complete. Therefore, it needs to transitively
// process all address uses.
//
// FIXME: this should use the standard recursive lifetime completion
// utility. Otherwise dealing with nested incomplete lifetimes becomes expensive
// and complex. e.g.
//
// %storeBorrow = store_borrow %_ to %adr
// %loadBorrow = load_borrow %storeBorrow
// apply %f(%loadBorrow) : $@convention(thin) (...) -> Never
// unreachable
//
AddressUseKind ScopedAddressValue::
computeLiveness(SSAPrunedLiveness &liveness) const {
SmallVector<Operand *, 4> uses;
// Collect all uses that need to be enclosed by the scope.
auto addressKind = findTransitiveUsesForAddress(value, &uses);
liveness.initializeDef(value);
for (auto *use : uses) {
// Update all collected uses as non-lifetime ending.
liveness.updateForUse(use->getUser(), /* lifetimeEnding */ false);
}
visitScopeEndingUses([&](Operand *endOp) {
liveness.updateForUse(endOp->getUser(), /* isLifetimeEnding */ true);
return true;
});
return addressKind;
}
void ScopedAddressValue::createScopeEnd(SILBasicBlock::iterator insertPt,
SILLocation loc) const {
switch (kind) {
case ScopedAddressValueKind::StoreBorrow: {
SILBuilderWithScope(insertPt).createEndBorrow(loc, value);
return;
}
case ScopedAddressValueKind::BeginAccess: {
SILBuilderWithScope(insertPt).createEndAccess(loc, value, false);
return;
}
case ScopedAddressValueKind::Invalid:
llvm_unreachable("Using invalid case?!");
}
}
void ScopedAddressValue::endScopeAtLivenessBoundary(
SSAPrunedLiveness *liveness) const {
// If no users exist, create scope ending instruction immediately after the
// scoped address value.
if (liveness->empty()) {
createScopeEnd(value->getNextInstruction()->getIterator(),
RegularLocation::getAutoGeneratedLocation());
return;
}
PrunedLivenessBoundary scopedAddressBoundary;
liveness->computeBoundary(scopedAddressBoundary);
// Go over the boundary and create scope ending instructions.
scopedAddressBoundary.visitInsertionPoints(
[&](SILBasicBlock::iterator insertPt) {
createScopeEnd(insertPt, RegularLocation::getAutoGeneratedLocation());
});
}
bool swift::hasOtherStoreBorrowsInLifetime(StoreBorrowInst *storeBorrow,
SSAPrunedLiveness *liveness,
DeadEndBlocks *deadEndBlocks) {
SmallVector<StoreBorrowInst *, 4> otherStoreBorrows;
// Collect all other store_borrows to the destination of \p storeBorrow
for (auto *destUse : storeBorrow->getDest()->getUses()) {
if (auto *user = dyn_cast<StoreBorrowInst>(destUse->getUser())) {
if (user == storeBorrow) {
continue;
}
otherStoreBorrows.push_back(user);
}
}
for (auto *otherStoreBorrow : otherStoreBorrows) {
// Return true, if otherStoreBorrow was in \p storeBorrow's scope
if (liveness->isWithinBoundary(otherStoreBorrow)) {
return true;
}
}
return false;
}
bool swift::extendStoreBorrow(StoreBorrowInst *sbi,
SmallVectorImpl<Operand *> &newUses,
DeadEndBlocks *deadEndBlocks,
InstModCallbacks callbacks) {
ScopedAddressValue scopedAddress(sbi);
SmallVector<SILBasicBlock *, 4> discoveredBlocks;
SSAPrunedLiveness storeBorrowLiveness(&discoveredBlocks);
AddressUseKind useKind = scopedAddress.computeLiveness(storeBorrowLiveness);
// If all new uses are within store_borrow boundary, no need for extension.
if (storeBorrowLiveness.areUsesWithinBoundary(newUses, deadEndBlocks)) {
return true;
}
if (useKind != AddressUseKind::NonEscaping) {
return false;
}
// store_borrow extension is possible only when there are no other
// store_borrows to the same destination within the store_borrow's lifetime
// built from newUsers.
if (hasOtherStoreBorrowsInLifetime(sbi, &storeBorrowLiveness,
deadEndBlocks)) {
return false;
}
InstModCallbacks tempCallbacks = callbacks;
InstructionDeleter deleter(std::move(tempCallbacks));
GuaranteedOwnershipExtension borrowExtension(deleter, *deadEndBlocks,
sbi->getFunction());
auto status = borrowExtension.checkBorrowExtension(
BorrowedValue(sbi->getSrc()), newUses);
if (status == GuaranteedOwnershipExtension::Invalid) {
return false;
}
borrowExtension.transform(status);
SmallVector<Operand *, 4> endBorrowUses;
// Collect old scope-ending instructions.
scopedAddress.visitScopeEndingUses([&](Operand *op) {
endBorrowUses.push_back(op);
return true;
});
for (auto *use : newUses) {
// Update newUsers as non-lifetime ending.
storeBorrowLiveness.updateForUse(use->getUser(),
/* lifetimeEnding */ false);
}
// Add new scope-ending instructions.
scopedAddress.endScopeAtLivenessBoundary(&storeBorrowLiveness);
// Remove old scope-ending instructions.
for (auto *endBorrowUse : endBorrowUses) {
callbacks.deleteInst(endBorrowUse->getUser());
}
return true;
}
void ScopedAddressValue::print(llvm::raw_ostream &os) const {
os << "ScopedAddressIntroducingValue:\n"
"Kind: "
<< kind
<< "\n"
"Value: "
<< value;
}
llvm::raw_ostream &swift::operator<<(llvm::raw_ostream &os,
const ScopedAddressValue &value) {
value.print(os);
return os;
}