Skip to content

Commit 21f11ab

Browse files
authored
Merge pull request #24581 from atrick/cleanup-end-borrow
[NFC] Add LoadBorrow::getEndBorrows() and minor cleanup.
2 parents ed90f33 + 207a4f7 commit 21f11ab

File tree

3 files changed

+27
-40
lines changed

3 files changed

+27
-40
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,6 @@ class LinearLifetimeError {
126126
}
127127
};
128128

129-
struct UseToEndBorrow {
130-
Optional<EndBorrowInst *> operator()(Operand *use) const {
131-
if (auto *ebi = dyn_cast<EndBorrowInst>(use->getUser())) {
132-
return ebi;
133-
}
134-
return None;
135-
}
136-
};
137-
138-
using EndBorrowRange =
139-
OptionalTransformRange<ValueBase::use_range, UseToEndBorrow,
140-
ValueBase::use_iterator>;
141-
142-
/// Given a value \p v that is a "borrow" introducer, return its associated
143-
/// end_borrow users.
144-
inline auto makeEndBorrowRange(SILValue v) -> EndBorrowRange {
145-
assert((isa<BeginBorrowInst>(v) || isa<LoadBorrowInst>(v)) &&
146-
"Unhandled borrow introducer");
147-
return EndBorrowRange(v->getUses(), UseToEndBorrow());
148-
}
149-
150129
/// Returns true if:
151130
///
152131
/// 1. No consuming uses are reachable from any other consuming use, from any

include/swift/SIL/SILInstruction.h

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3231,19 +3231,40 @@ class StoreInst
32313231
}
32323232
};
32333233

3234+
class EndBorrowInst;
3235+
3236+
struct UseToEndBorrow {
3237+
Optional<EndBorrowInst *> operator()(Operand *use) const {
3238+
if (auto endBorrow = dyn_cast<EndBorrowInst>(use->getUser())) {
3239+
return endBorrow;
3240+
} else {
3241+
return None;
3242+
}
3243+
}
3244+
};
3245+
32343246
/// Represents a load of a borrowed value. Must be paired with an end_borrow
32353247
/// instruction in its use-def list.
32363248
class LoadBorrowInst :
32373249
public UnaryInstructionBase<SILInstructionKind::LoadBorrowInst,
32383250
SingleValueInstruction> {
32393251
friend class SILBuilder;
32403252

3253+
public:
32413254
LoadBorrowInst(SILDebugLocation DebugLoc, SILValue LValue)
32423255
: UnaryInstructionBase(DebugLoc, LValue,
32433256
LValue->getType().getObjectType()) {}
3257+
3258+
using EndBorrowRange =
3259+
OptionalTransformRange<use_range, UseToEndBorrow, use_iterator>;
3260+
3261+
/// Return a range over all EndBorrow instructions for this BeginBorrow.
3262+
EndBorrowRange getEndBorrows() const;
32443263
};
32453264

3246-
class EndBorrowInst;
3265+
inline auto LoadBorrowInst::getEndBorrows() const -> EndBorrowRange {
3266+
return EndBorrowRange(getUses(), UseToEndBorrow());
3267+
}
32473268

32483269
/// Represents the begin scope of a borrowed value. Must be paired with an
32493270
/// end_borrow instruction in its use-def list.
@@ -3252,9 +3273,6 @@ class BeginBorrowInst
32523273
SingleValueInstruction> {
32533274
friend class SILBuilder;
32543275

3255-
/// Predicate used to filter EndBorrowRange.
3256-
struct UseToEndBorrow;
3257-
32583276
BeginBorrowInst(SILDebugLocation DebugLoc, SILValue LValue)
32593277
: UnaryInstructionBase(DebugLoc, LValue,
32603278
LValue->getType().getObjectType()) {}
@@ -3275,6 +3293,10 @@ class BeginBorrowInst
32753293
Operand *getSingleNonEndingUse() const;
32763294
};
32773295

3296+
inline auto BeginBorrowInst::getEndBorrows() const -> EndBorrowRange {
3297+
return EndBorrowRange(getUses(), UseToEndBorrow());
3298+
}
3299+
32783300
/// Represents a store of a borrowed value into an address. Returns the borrowed
32793301
/// address. Must be paired with an end_borrow in its use-def list.
32803302
class StoreBorrowInst
@@ -3364,20 +3386,6 @@ class EndBorrowInst
33643386
}
33653387
};
33663388

3367-
struct BeginBorrowInst::UseToEndBorrow {
3368-
Optional<EndBorrowInst *> operator()(Operand *use) const {
3369-
if (auto borrow = dyn_cast<EndBorrowInst>(use->getUser())) {
3370-
return borrow;
3371-
} else {
3372-
return None;
3373-
}
3374-
}
3375-
};
3376-
3377-
inline auto BeginBorrowInst::getEndBorrows() const -> EndBorrowRange {
3378-
return EndBorrowRange(getUses(), UseToEndBorrow());
3379-
}
3380-
33813389
/// Different kinds of access.
33823390
enum class SILAccessKind : uint8_t {
33833391
/// An access which takes uninitialized memory and initializes it.

lib/SIL/SILOwnershipVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ bool SILValueOwnershipChecker::gatherUsers(
331331
for (unsigned i : indices(nonLifetimeEndingUsers)) {
332332
if (auto *bbi = dyn_cast<BeginBorrowInst>(
333333
nonLifetimeEndingUsers[i].getInst())) {
334-
copy(makeEndBorrowRange(bbi),
334+
copy(bbi->getEndBorrows(),
335335
std::back_inserter(implicitRegularUsers));
336336
}
337337
}

0 commit comments

Comments
 (0)