Skip to content

LowerHopToActor: insert borrow scope if an optional actor value is owned #79046

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, 2025
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
9 changes: 9 additions & 0 deletions lib/SILOptimizer/Mandatory/LowerHopToActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,10 +268,16 @@ SILValue LowerHopToActor::emitGetExecutor(SILBuilderWithScope &B,
return B.createStructExtract(loc, witnessCall, executorProps[0]);
};

bool needEndBorrow = false;
SILValue unmarkedExecutor;
if (auto wrappedActor = actorType->getOptionalObjectType()) {
assert(makeOptional);

if (B.hasOwnership() && actor->getOwnershipKind() == OwnershipKind::Owned) {
actor = B.createBeginBorrow(loc, actor);
needEndBorrow = true;
}

// Unwrap the optional and call 'unownedExecutor'.
auto *someDecl = B.getASTContext().getOptionalSomeDecl();
auto *curBB = B.getInsertionPoint()->getParent();
Expand Down Expand Up @@ -323,6 +329,9 @@ SILValue LowerHopToActor::emitGetExecutor(SILBuilderWithScope &B,
// force the actor to stay alive.
SILValue executor = B.createMarkDependence(loc, unmarkedExecutor, actor,
MarkDependenceKind::Escaping);
if (needEndBorrow) {
B.createEndBorrow(loc, actor);
}

return executor;
}
Expand Down
17 changes: 17 additions & 0 deletions test/SILOptimizer/lower_hop_to_actor.sil
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,20 @@ bb0(%0 : @guaranteed $Optional<any Actor>):
%r = tuple ()
return %r : $()
}

// CHECK-LABEL: sil [ossa] @optional_owned_actor :
// CHECK: %1 = copy_value %0
// CHECK-NEXT: %2 = begin_borrow %1
// CHECK-NEXT: switch_enum %2
// CHECK: mark_dependence
// CHECK-NEXT: end_borrow %2
// CHECKL: } // end sil function '@optional_owned_actor'
sil [ossa] @optional_owned_actor : $@convention(thin) @async (@guaranteed Optional<any Actor>) -> () {
bb0(%0 : @guaranteed $Optional<any Actor>):
%1 = copy_value %0
hop_to_executor %1
destroy_value %1
%r = tuple ()
return %r
}