Skip to content

SILGen: Separate borrow from consume phase for destructive pattern matches. #71231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/SILGen/Cleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,14 @@ void CleanupManager::endScope(CleanupsDepth depth, CleanupLocation loc) {
emitCleanups(depth, loc, NotForUnwind, /*popCleanups*/ true);
}

/// Leave a scope, emitting all the cleanups that are currently active but leaving them on the stack so they
/// can be reenabled on other pattern match branches.
void CleanupManager::endNoncopyablePatternMatchBorrow(CleanupsDepth depth,
CleanupLocation loc,
bool popCleanups) {
emitCleanups(depth, loc, NotForUnwind, popCleanups);
}

bool CleanupManager::hasAnyActiveCleanups(CleanupsDepth from,
CleanupsDepth to) {
return ::hasAnyActiveCleanups(stack.find(from), stack.find(to));
Expand Down
5 changes: 4 additions & 1 deletion lib/SILGen/Cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ typedef DiverseStackImpl<Cleanup>::stable_iterator CleanupHandle;
class LLVM_LIBRARY_VISIBILITY CleanupManager {
friend class Scope;
friend class CleanupCloner;

SILGenFunction &SGF;

/// Stack - Currently active cleanups in this scope tree.
Expand Down Expand Up @@ -289,6 +289,9 @@ class LLVM_LIBRARY_VISIBILITY CleanupManager {
/// Verify that the given cleanup handle is valid.
void checkIterator(CleanupHandle handle) const;

void endNoncopyablePatternMatchBorrow(CleanupsDepth depth, CleanupLocation l,
bool finalEndBorrow = false);

private:
// Look up the flags and optionally the writeback address associated with the
// cleanup at \p depth. If
Expand Down
15 changes: 15 additions & 0 deletions lib/SILGen/ManagedValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ class ConsumableManagedValue {

/// Return a managed value that's appropriate for borrowing this
/// value and promising not to consume it.
///
/// TODO: Should be superseded by `asBorrowedOperand2` once existing code is
/// updated to tolerate address-only values being borrowed.
ConsumableManagedValue asBorrowedOperand(SILGenFunction &SGF,
SILLocation loc) const {
if (getType().isAddress())
Expand All @@ -532,6 +535,18 @@ class ConsumableManagedValue {
CastConsumptionKind::BorrowAlways};
}

ConsumableManagedValue asBorrowedOperand2(SILGenFunction &SGF,
SILLocation loc) const {
if (getType().isAddress())
return {asUnmanagedOwnedValue(), CastConsumptionKind::BorrowAlways};

if (Value.getOwnershipKind() == OwnershipKind::Guaranteed)
return {Value, CastConsumptionKind::BorrowAlways};

return {asUnmanagedOwnedValue().borrow(SGF, loc),
CastConsumptionKind::BorrowAlways};
}

/// Return a managed value that's appropriate for copying this value and
/// always consuming it.
ConsumableManagedValue copy(SILGenFunction &SGF, SILLocation loc) const {
Expand Down
Loading