Skip to content

Commit 0ba17c5

Browse files
authored
Merge pull request #79046 from eeckstein/fix-lower-hop-to-actor
LowerHopToActor: insert borrow scope if an optional actor value is owned
2 parents e3de9d5 + 5fb41a9 commit 0ba17c5

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

lib/SILOptimizer/Mandatory/LowerHopToActor.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,16 @@ SILValue LowerHopToActor::emitGetExecutor(SILBuilderWithScope &B,
268268
return B.createStructExtract(loc, witnessCall, executorProps[0]);
269269
};
270270

271+
bool needEndBorrow = false;
271272
SILValue unmarkedExecutor;
272273
if (auto wrappedActor = actorType->getOptionalObjectType()) {
273274
assert(makeOptional);
274275

276+
if (B.hasOwnership() && actor->getOwnershipKind() == OwnershipKind::Owned) {
277+
actor = B.createBeginBorrow(loc, actor);
278+
needEndBorrow = true;
279+
}
280+
275281
// Unwrap the optional and call 'unownedExecutor'.
276282
auto *someDecl = B.getASTContext().getOptionalSomeDecl();
277283
auto *curBB = B.getInsertionPoint()->getParent();
@@ -323,6 +329,9 @@ SILValue LowerHopToActor::emitGetExecutor(SILBuilderWithScope &B,
323329
// force the actor to stay alive.
324330
SILValue executor = B.createMarkDependence(loc, unmarkedExecutor, actor,
325331
MarkDependenceKind::Escaping);
332+
if (needEndBorrow) {
333+
B.createEndBorrow(loc, actor);
334+
}
326335

327336
return executor;
328337
}

test/SILOptimizer/lower_hop_to_actor.sil

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,20 @@ bb0(%0 : @guaranteed $Optional<any Actor>):
290290
%r = tuple ()
291291
return %r : $()
292292
}
293+
294+
// CHECK-LABEL: sil [ossa] @optional_owned_actor :
295+
// CHECK: %1 = copy_value %0
296+
// CHECK-NEXT: %2 = begin_borrow %1
297+
// CHECK-NEXT: switch_enum %2
298+
// CHECK: mark_dependence
299+
// CHECK-NEXT: end_borrow %2
300+
// CHECKL: } // end sil function '@optional_owned_actor'
301+
sil [ossa] @optional_owned_actor : $@convention(thin) @async (@guaranteed Optional<any Actor>) -> () {
302+
bb0(%0 : @guaranteed $Optional<any Actor>):
303+
%1 = copy_value %0
304+
hop_to_executor %1
305+
destroy_value %1
306+
%r = tuple ()
307+
return %r
308+
}
309+

0 commit comments

Comments
 (0)