-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathOwnershipLiveness.cpp
363 lines (306 loc) · 12.2 KB
/
OwnershipLiveness.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
//===--- OwnershipLiveness.cpp --------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 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/OwnershipLiveness.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Debug.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/LLVM.h"
#include "swift/SIL/Dominance.h"
#include "swift/SIL/PrunedLiveness.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/Test.h"
#include "llvm/ADT/SmallVector.h"
namespace swift {
void OSSALiveness::print(llvm::raw_ostream &OS) const { liveness.print(OS); }
void OSSALiveness::dump() const { print(llvm::dbgs()); }
struct LinearLivenessVisitor :
public OwnershipUseVisitor<LinearLivenessVisitor> {
LinearLiveness &linearLiveness;
LinearLivenessVisitor(LinearLiveness &linearLiveness):
linearLiveness(linearLiveness){}
bool handleUsePoint(Operand *use, UseLifetimeConstraint useConstraint) {
if (!linearLiveness.includeExtensions &&
isa<ExtendLifetimeInst>(use->getUser())) {
return true;
}
linearLiveness.liveness.updateForUse(
use->getUser(), useConstraint == UseLifetimeConstraint::LifetimeEnding);
return true;
}
bool handlePointerEscape(Operand *use) {
llvm_unreachable("a pointer escape cannot end a linear lifetime");
}
// handleOwnedPhi and handleOuterReborrow ends the linear lifetime.
// By default, they are treated like a normal lifetime-ending use.
bool handleGuaranteedForwardingPhi(Operand *use) {
llvm_unreachable("guaranteed forwarding phi cannot end a linear lifetime");
}
bool handleInnerBorrow(BorrowingOperand borrowingOperand) {
llvm_unreachable("an inner borrow cannot end a linear lifetime");
}
bool handleInnerAdjacentReborrow(SILArgument *reborrow) {
llvm_unreachable("inner adjacent reborrows are not visited");
}
bool handleInnerReborrow(BorrowingOperand borrowingOperand) {
llvm_unreachable("an inner borrow cannot end a linear lifetime");
}
bool handleScopedAddress(ScopedAddressValue scopedAddress) {
llvm_unreachable("an scoped address cannot end a linear lifetime");
}
};
LinearLiveness::LinearLiveness(SILValue def,
IncludeExtensions_t includeExtensions)
: OSSALiveness(def), includeExtensions(includeExtensions) {
switch (def->getOwnershipKind()) {
case OwnershipKind::Owned:
break;
case OwnershipKind::Guaranteed: {
BorrowedValue borrowedValue(def);
assert(borrowedValue && borrowedValue.isLocalScope());
(void)borrowedValue;
break;
}
case OwnershipKind::None:
assert(def->isFromVarDecl());
break;
case OwnershipKind::Unowned:
case OwnershipKind::Any:
llvm_unreachable("bad ownership for LinearLiveness");
}
}
void LinearLiveness::compute() {
liveness.initializeDef(ownershipDef);
LinearLivenessVisitor(*this).visitLifetimeEndingUses(ownershipDef);
}
/// Override OwnershipUseVisitor to callback to handleInnerScopeCallback for
/// nested borrow scopes. This supports lifetime completion from the inside-out.
///
/// By default, handleOwnedPhi and handleOuterReborrow are already treated like
/// a normal lifetime-ending use.
///
/// By default, handleGuaranteedForwardingPhi is already treated like a normal
/// non-lifetime-ending use.
struct InteriorLivenessVisitor :
public OwnershipUseVisitor<InteriorLivenessVisitor> {
InteriorLiveness &interiorLiveness;
// If domInfo is nullptr, then InteriorLiveness never assumes dominance. As a
// result it may report extra unenclosedPhis. In that case, any attempt to
// create a new phi would result in an immediately redundant phi.
const DominanceInfo *domInfo = nullptr;
/// handleInnerScopeCallback may add uses to the inner scope, but it may not
/// modify the use-list containing \p borrowingOperand. This callback can be
/// used to ensure that the inner scope is complete before visiting its scope
/// ending operands.
///
/// An inner scope encapsulates any pointer escapes so visiting its interior
/// uses is not necessary when visiting the outer scope's interior uses.
InteriorLiveness::InnerScopeHandlerRef handleInnerScopeCallback;
// State local to an invocation of
// OwnershipUseVisitor::visitOwnershipUses().
NodeSet visited;
InteriorLivenessVisitor(
InteriorLiveness &interiorLiveness,
const DominanceInfo *domInfo,
InteriorLiveness::InnerScopeHandlerRef handleInnerScope)
: interiorLiveness(interiorLiveness),
domInfo(domInfo),
handleInnerScopeCallback(handleInnerScope),
visited(interiorLiveness.ownershipDef->getFunction()) {}
bool handleUsePoint(Operand *use, UseLifetimeConstraint useConstraint) {
interiorLiveness.liveness.updateForUse(
use->getUser(), useConstraint == UseLifetimeConstraint::LifetimeEnding);
return true;
}
bool handlePointerEscape(Operand *use) {
interiorLiveness.addressUseKind = AddressUseKind::PointerEscape;
interiorLiveness.escapingUse = use;
if (!handleUsePoint(use, UseLifetimeConstraint::NonLifetimeEnding))
return false;
return true;
}
/// After this returns true, handleUsePoint will be called on the scope
/// ending operands.
///
/// Handles begin_borrow, load_borrow, store_borrow, begin_apply.
bool handleInnerBorrow(BorrowingOperand borrowingOperand) {
if (handleInnerScopeCallback) {
if (auto value = borrowingOperand.getScopeIntroducingUserResult()) {
handleInnerScopeCallback(value);
}
}
return true;
}
/// After this returns true, handleUsePoint will be called on the scope
/// ending operands.
///
/// Handles store_borrow, begin_access.
bool handleScopedAddress(ScopedAddressValue scopedAddress) {
if (handleInnerScopeCallback) {
handleInnerScopeCallback(scopedAddress.value);
}
return true;
}
};
void InteriorLiveness::compute(const DominanceInfo *domInfo, InnerScopeHandlerRef handleInnerScope) {
liveness.initializeDef(ownershipDef);
addressUseKind = AddressUseKind::NonEscaping;
InteriorLivenessVisitor(*this, domInfo, handleInnerScope)
.visitInteriorUses(ownershipDef);
}
void InteriorLiveness::print(llvm::raw_ostream &OS) const {
OSSALiveness::print(OS);
switch (getAddressUseKind()) {
case AddressUseKind::NonEscaping:
OS << "Complete liveness\n";
break;
case AddressUseKind::PointerEscape:
OS << "Incomplete liveness: Escaping address\n";
break;
case AddressUseKind::Dependent:
OS << "Incomplete liveness: Dependent value\n";
break;
case AddressUseKind::Unknown:
OS << "Incomplete liveness: Unknown address use\n";
break;
}
}
void InteriorLiveness::dump() const { print(llvm::dbgs()); }
// =============================================================================
// ExtendedLinearLiveness
// =============================================================================
struct ExtendedLinearLivenessVisitor
: public OwnershipUseVisitor<ExtendedLinearLivenessVisitor> {
ExtendedLinearLiveness &extendedLiveness;
// State local to an invocation of
// OwnershipUseVisitor::visitOwnershipUses().
InstructionSet visited;
ExtendedLinearLivenessVisitor(ExtendedLinearLiveness &extendedLiveness)
: extendedLiveness(extendedLiveness),
visited(extendedLiveness.ownershipDef->getFunction()) {}
bool handleUsePoint(Operand *use, UseLifetimeConstraint useConstraint) {
extendedLiveness.liveness.updateForUse(
use->getUser(), useConstraint == UseLifetimeConstraint::LifetimeEnding);
return true;
}
bool handlePointerEscape(Operand *use) {
llvm_unreachable("a pointer escape cannot end a linear lifetime");
}
bool handleOwnedPhi(Operand *phiOper) {
extendedLiveness.liveness.initializeDef(PhiOperand(phiOper).getValue());
return true;
}
bool handleOuterReborrow(Operand *phiOper) {
extendedLiveness.liveness.initializeDef(PhiOperand(phiOper).getValue());
return true;
}
bool handleGuaranteedForwardingPhi(Operand *use) {
llvm_unreachable("guaranteed forwarding phi cannot end a linear lifetime");
}
bool handleInnerBorrow(BorrowingOperand borrowingOperand) {
llvm_unreachable("an inner borrow cannot end a linear lifetime");
}
bool handleInnerAdjacentReborrow(SILArgument *reborrow) {
llvm_unreachable("inner adjacent reborrows are not visited");
}
bool handleInnerReborrow(BorrowingOperand borrowingOperand) {
llvm_unreachable("an inner borrow cannot end a linear lifetime");
}
bool handleScopedAddress(ScopedAddressValue scopedAddress) {
llvm_unreachable("an scoped address cannot end a linear lifetime");
}
};
ExtendedLinearLiveness::ExtendedLinearLiveness(SILValue def)
: ownershipDef(def), liveness(def->getFunction(), &discoveredBlocks) {
if (def->getOwnershipKind() != OwnershipKind::Owned) {
BorrowedValue borrowedValue(def);
assert(borrowedValue && borrowedValue.isLocalScope());
(void)borrowedValue;
}
}
void ExtendedLinearLiveness::compute() {
liveness.initializeDef(ownershipDef);
for (auto defIter = liveness.defBegin(); defIter != liveness.defEnd();
++defIter) {
auto *def = cast<ValueBase>(*defIter);
ExtendedLinearLivenessVisitor(*this).visitLifetimeEndingUses(def);
}
}
void ExtendedLinearLiveness::print(llvm::raw_ostream &OS) const {
liveness.print(OS);
}
void ExtendedLinearLiveness::dump() const { print(llvm::dbgs()); }
} // namespace swift
namespace swift::test {
// Arguments:
// - SILValue: value
// Dumps:
// - function
// - the computed pruned liveness
// - the liveness boundary
static FunctionTest LinearLivenessTest("linear_liveness", [](auto &function,
auto &arguments,
auto &test) {
SILValue value = arguments.takeValue();
function.print(llvm::outs());
llvm::outs() << "Linear liveness: " << value;
LinearLiveness liveness(value);
liveness.compute();
liveness.print(llvm::outs());
PrunedLivenessBoundary boundary;
liveness.getLiveness().computeBoundary(boundary);
boundary.print(llvm::outs());
});
// Arguments:
// - SILValue: value
// Dumps:
// - function
// - the computed pruned liveness
// - the liveness boundary
static FunctionTest
InteriorLivenessTest("interior_liveness",
[](auto &function, auto &arguments, auto &test) {
SILValue value = arguments.takeValue();
function.print(llvm::outs());
llvm::outs() << "Interior liveness: " << value;
auto *domTree = test.getDominanceInfo();
InteriorLiveness liveness(value);
auto handleInnerScope = [](SILValue innerBorrow) {
llvm::outs() << "Inner scope: " << innerBorrow;
};
liveness.compute(domTree, handleInnerScope);
liveness.print(llvm::outs());
PrunedLivenessBoundary boundary;
liveness.getLiveness().computeBoundary(boundary);
boundary.print(llvm::outs());
});
// Arguments:
// - SILValue: value
// Dumps:
// - function
// - the computed pruned liveness
// - the liveness boundary
static FunctionTest ExtendedLinearLivenessTest(
"extended-liveness", [](auto &function, auto &arguments, auto &test) {
SILValue value = arguments.takeValue();
function.print(llvm::outs());
llvm::outs() << "Extended liveness: " << value;
ExtendedLinearLiveness liveness(value);
liveness.compute();
liveness.print(llvm::outs());
PrunedLivenessBoundary boundary;
liveness.getLiveness().computeBoundary(boundary);
boundary.print(llvm::outs());
});
} // end namespace swift::test