Skip to content

Commit 47e7447

Browse files
committed
[ownership] Rename BorrowScopeIntroducingValue -> BorrowedValue and BorrowScopeOperand -> BorrowingOperand.
Andy and I for some time have been discussing the right name for these two "ownership concepts". What we realized is that the "ing" on BorrowScopeIntroducingValue is very unfortunate since this value is the result of a new borrow scope being introduced. So the name should be really: BorrowScopeIntroducedValue. Given that is sort of unsatisfying, we settled on the name BorrowedValue. Once we found the name BorrowedValue, we naturally realized that BorrowScopeOperand -> BorrowingOperand followed. This is because the operand is the operand of the instruction that is creating the new borrow scope. So in a sense the Operand is the "Use" that causes the original value to become borrowed. So a BorrowingOperand is where the action is and is "active".
1 parent 270f734 commit 47e7447

File tree

6 files changed

+100
-106
lines changed

6 files changed

+100
-106
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 40 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -100,36 +100,37 @@ struct BorrowScopeOperandKind {
100100
llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
101101
BorrowScopeOperandKind kind);
102102

103-
struct BorrowScopeIntroducingValue;
103+
struct BorrowedValue;
104104

105105
/// An operand whose user instruction introduces a new borrow scope for the
106-
/// operand's value. The value of the operand must be considered as implicitly
107-
/// borrowed until the user's corresponding end scope instruction.
106+
/// operand's value. By executing the given user, the operand's value becomes
107+
/// borrowed and thus the incoming value must implicitly be borrowed until the
108+
/// user's corresponding end scope instruction.
108109
///
109110
/// NOTE: We do not require that the guaranteed scope be represented by a
110111
/// guaranteed value in the same function: see begin_apply. In such cases, we
111112
/// require instead an end_* instruction to mark the end of the scope's region.
112-
struct BorrowScopeOperand {
113+
struct BorrowingOperand {
113114
BorrowScopeOperandKind kind;
114115
Operand *op;
115116

116-
BorrowScopeOperand(Operand *op)
117+
BorrowingOperand(Operand *op)
117118
: kind(*BorrowScopeOperandKind::get(op->getUser()->getKind())), op(op) {}
118-
BorrowScopeOperand(const BorrowScopeOperand &other)
119+
BorrowingOperand(const BorrowingOperand &other)
119120
: kind(other.kind), op(other.op) {}
120-
BorrowScopeOperand &operator=(const BorrowScopeOperand &other) {
121+
BorrowingOperand &operator=(const BorrowingOperand &other) {
121122
kind = other.kind;
122123
op = other.op;
123124
return *this;
124125
}
125126

126127
/// If value is a borrow introducer return it after doing some checks.
127-
static Optional<BorrowScopeOperand> get(Operand *op) {
128+
static Optional<BorrowingOperand> get(Operand *op) {
128129
auto *user = op->getUser();
129130
auto kind = BorrowScopeOperandKind::get(user->getKind());
130131
if (!kind)
131132
return None;
132-
return BorrowScopeOperand(*kind, op);
133+
return BorrowingOperand(*kind, op);
133134
}
134135

135136
void visitEndScopeInstructions(function_ref<void(Operand *)> func) const;
@@ -182,8 +183,8 @@ struct BorrowScopeOperand {
182183
/// Visit all of the "results" of the user of this operand that are borrow
183184
/// scope introducers for the specific scope that this borrow scope operand
184185
/// summarizes.
185-
void visitBorrowIntroducingUserResults(
186-
function_ref<void(BorrowScopeIntroducingValue)> visitor);
186+
void
187+
visitBorrowIntroducingUserResults(function_ref<void(BorrowedValue)> visitor);
187188

188189
/// Passes to visitor all of the consuming uses of this use's using
189190
/// instruction.
@@ -200,14 +201,14 @@ struct BorrowScopeOperand {
200201
private:
201202
/// Internal constructor for failable static constructor. Please do not expand
202203
/// its usage since it assumes the code passed in is well formed.
203-
BorrowScopeOperand(BorrowScopeOperandKind kind, Operand *op)
204+
BorrowingOperand(BorrowScopeOperandKind kind, Operand *op)
204205
: kind(kind), op(op) {}
205206
};
206207

207208
llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
208-
const BorrowScopeOperand &operand);
209+
const BorrowingOperand &operand);
209210

210-
struct BorrowScopeIntroducingValueKind {
211+
struct BorrowedValueKind {
211212
/// Enum we use for exhaustive pattern matching over borrow scope introducers.
212213
enum Kind {
213214
LoadBorrow,
@@ -216,35 +217,34 @@ struct BorrowScopeIntroducingValueKind {
216217
Phi,
217218
};
218219

219-
static Optional<BorrowScopeIntroducingValueKind> get(SILValue value) {
220+
static Optional<BorrowedValueKind> get(SILValue value) {
220221
if (value.getOwnershipKind() != ValueOwnershipKind::Guaranteed)
221222
return None;
222223
switch (value->getKind()) {
223224
default:
224225
return None;
225226
case ValueKind::LoadBorrowInst:
226-
return BorrowScopeIntroducingValueKind(LoadBorrow);
227+
return BorrowedValueKind(LoadBorrow);
227228
case ValueKind::BeginBorrowInst:
228-
return BorrowScopeIntroducingValueKind(BeginBorrow);
229+
return BorrowedValueKind(BeginBorrow);
229230
case ValueKind::SILFunctionArgument:
230-
return BorrowScopeIntroducingValueKind(SILFunctionArgument);
231+
return BorrowedValueKind(SILFunctionArgument);
231232
case ValueKind::SILPhiArgument: {
232233
if (llvm::any_of(value->getParentBlock()->getPredecessorBlocks(),
233234
[](SILBasicBlock *block) {
234235
return !isa<BranchInst>(block->getTerminator());
235236
})) {
236237
return None;
237238
}
238-
return BorrowScopeIntroducingValueKind(Phi);
239+
return BorrowedValueKind(Phi);
239240
}
240241
}
241242
}
242243

243244
Kind value;
244245

245-
BorrowScopeIntroducingValueKind(Kind newValue) : value(newValue) {}
246-
BorrowScopeIntroducingValueKind(const BorrowScopeIntroducingValueKind &other)
247-
: value(other.value) {}
246+
BorrowedValueKind(Kind newValue) : value(newValue) {}
247+
BorrowedValueKind(const BorrowedValueKind &other) : value(other.value) {}
248248
operator Kind() const { return value; }
249249

250250
/// Is this a borrow scope that begins and ends within the same function and
@@ -255,11 +255,11 @@ struct BorrowScopeIntroducingValueKind {
255255
/// of the scope.
256256
bool isLocalScope() const {
257257
switch (value) {
258-
case BorrowScopeIntroducingValueKind::BeginBorrow:
259-
case BorrowScopeIntroducingValueKind::LoadBorrow:
260-
case BorrowScopeIntroducingValueKind::Phi:
258+
case BorrowedValueKind::BeginBorrow:
259+
case BorrowedValueKind::LoadBorrow:
260+
case BorrowedValueKind::Phi:
261261
return true;
262-
case BorrowScopeIntroducingValueKind::SILFunctionArgument:
262+
case BorrowedValueKind::SILFunctionArgument:
263263
return false;
264264
}
265265
llvm_unreachable("Covered switch isnt covered?!");
@@ -269,13 +269,12 @@ struct BorrowScopeIntroducingValueKind {
269269
SWIFT_DEBUG_DUMP { print(llvm::dbgs()); }
270270
};
271271

272-
llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
273-
BorrowScopeIntroducingValueKind kind);
272+
llvm::raw_ostream &operator<<(llvm::raw_ostream &os, BorrowedValueKind kind);
274273

275274
struct InteriorPointerOperand;
276275

277-
/// A higher level construct for working with values that represent the
278-
/// introduction of a new borrow scope.
276+
/// A higher level construct for working with values that act as a "borrow
277+
/// introducer" for a new borrow scope.
279278
///
280279
/// DISCUSSION: A "borrow introducer" is a SILValue that represents the
281280
/// beginning of a borrow scope that the ownership verifier validates. The idea
@@ -290,19 +289,19 @@ struct InteriorPointerOperand;
290289
/// guaranteed results are borrow introducers. In practice this means that
291290
/// borrow introducers can not have guaranteed results that are not creating a
292291
/// new borrow scope. No such instructions exist today.
293-
struct BorrowScopeIntroducingValue {
294-
BorrowScopeIntroducingValueKind kind;
292+
struct BorrowedValue {
293+
BorrowedValueKind kind;
295294
SILValue value;
296295

297296
/// If value is a borrow introducer return it after doing some checks.
298297
///
299298
/// This is the only way to construct a BorrowScopeIntroducingValue. We make
300299
/// the primary constructor private for this reason.
301-
static Optional<BorrowScopeIntroducingValue> get(SILValue value) {
302-
auto kind = BorrowScopeIntroducingValueKind::get(value);
300+
static Optional<BorrowedValue> get(SILValue value) {
301+
auto kind = BorrowedValueKind::get(value);
303302
if (!kind)
304303
return None;
305-
return BorrowScopeIntroducingValue(*kind, value);
304+
return BorrowedValue(*kind, value);
306305
}
307306

308307
/// If this value is introducing a local scope, gather all local end scope
@@ -359,13 +358,12 @@ struct BorrowScopeIntroducingValue {
359358
private:
360359
/// Internal constructor for failable static constructor. Please do not expand
361360
/// its usage since it assumes the code passed in is well formed.
362-
BorrowScopeIntroducingValue(BorrowScopeIntroducingValueKind kind,
363-
SILValue value)
361+
BorrowedValue(BorrowedValueKind kind, SILValue value)
364362
: kind(kind), value(value) {}
365363
};
366364

367365
llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
368-
const BorrowScopeIntroducingValue &value);
366+
const BorrowedValue &value);
369367

370368
/// Look up the def-use graph starting at use \p inputOperand, recording any
371369
/// "borrow" introducing values that we find into \p out. If at any point, we
@@ -376,16 +374,15 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
376374
/// NOTE: This may return multiple borrow introducing values in cases where
377375
/// there are phi-like nodes in the IR like any true phi block arguments or
378376
/// aggregate literal instructions (struct, tuple, enum, etc.).
379-
bool getAllBorrowIntroducingValues(
380-
SILValue value, SmallVectorImpl<BorrowScopeIntroducingValue> &out);
377+
bool getAllBorrowIntroducingValues(SILValue value,
378+
SmallVectorImpl<BorrowedValue> &out);
381379

382380
/// Look up through the def-use chain of \p inputValue, looking for an initial
383381
/// "borrow" introducing value. If at any point, we find two introducers or we
384382
/// find a point in the chain we do not understand, we bail and return false. If
385383
/// we are able to understand all of the def-use graph and only find a single
386384
/// introducer, then we return a .some(BorrowScopeIntroducingValue).
387-
Optional<BorrowScopeIntroducingValue>
388-
getSingleBorrowIntroducingValue(SILValue inputValue);
385+
Optional<BorrowedValue> getSingleBorrowIntroducingValue(SILValue inputValue);
389386

390387
struct InteriorPointerOperandKind {
391388
enum Kind : uint8_t {
@@ -441,7 +438,7 @@ struct InteriorPointerOperand {
441438
/// projection. Returns true if we were able to find all borrow introducing
442439
/// values.
443440
bool visitBaseValueScopeEndingUses(function_ref<void(Operand *)> func) const {
444-
SmallVector<BorrowScopeIntroducingValue, 4> introducers;
441+
SmallVector<BorrowedValue, 4> introducers;
445442
if (!getAllBorrowIntroducingValues(operand->get(), introducers))
446443
return false;
447444
for (const auto &introducer : introducers) {

0 commit comments

Comments
 (0)