Skip to content

Emit hop_to_executor instructors for foreign async calls. #38767

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
Aug 6, 2021
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
14 changes: 10 additions & 4 deletions lib/SILGen/SILGenApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4460,7 +4460,7 @@ RValue SILGenFunction::emitApply(
}

ExecutorBreadcrumb breadcrumb;

// The presence of `implicitActorHopTarget` indicates that the callee is a
// synchronous function isolated to an actor other than our own.
// Such functions require the caller to hop to the callee's executor
Expand All @@ -4486,7 +4486,8 @@ RValue SILGenFunction::emitApply(
}

breadcrumb = emitHopToTargetExecutor(loc, executor);
} else if (ExpectedExecutor && substFnType->isAsync()) {
} else if (ExpectedExecutor &&
(substFnType->isAsync() || calleeTypeInfo.foreign.async)) {
// Otherwise, if we're in an actor method ourselves, and we're calling into
// any sort of async function, we'll want to make sure to hop back to our
// own executor afterward, since the callee could have made arbitrary hops
Expand All @@ -4503,8 +4504,10 @@ RValue SILGenFunction::emitApply(
rawDirectResult = rawDirectResults[0];
}

// hop back to the current executor
breadcrumb.emit(*this, loc);
if (!calleeTypeInfo.foreign.async) {
// hop back to the current executor
breadcrumb.emit(*this, loc);
}

// For objc async calls, lifetime extend the args until the result plan which
// generates `await_async_continuation`.
Expand Down Expand Up @@ -4616,6 +4619,9 @@ RValue SILGenFunction::emitApply(
B.emitFixLifetime(loc, value);
B.emitDestroyOperation(loc, value);
}

// hop back to the current executor
breadcrumb.emit(*this, loc);
}

return result;
Expand Down
27 changes: 27 additions & 0 deletions test/SILGen/objc_async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,30 @@ func testGeneric2<T: AnyObject, U>(x: GenericObject<T>, y: U) async throws {
// CHECK: [[RESULT_1_BUF:%.*]] = tuple_element_addr [[RESULT_BUF]] {{.*}}, 1
// CHECK: store %2 to [trivial] [[RESULT_1_BUF]]

// CHECK-LABEL: sil {{.*}}@${{.*}}22testSlowServerFromMain
@MainActor
func testSlowServerFromMain(slowServer: SlowServer) async throws {
// CHECK: hop_to_executor %6 : $MainActor
// CHECK: [[RESUME_BUF:%.*]] = alloc_stack $Int
// CHECK: [[STRINGINIT:%.*]] = function_ref @$sSS10FoundationE19_bridgeToObjectiveCSo8NSStringCyF :
// CHECK: [[ARG:%.*]] = apply [[STRINGINIT]]
// CHECK: [[METHOD:%.*]] = objc_method {{.*}} $@convention(objc_method) (NSString, @convention(block) (Int) -> (), SlowServer) -> ()
// CHECK: [[CONT:%.*]] = get_async_continuation_addr Int, [[RESUME_BUF]]
// CHECK: [[WRAPPED:%.*]] = struct $UnsafeContinuation<Int, Never> ([[CONT]] : $Builtin.RawUnsafeContinuation)
// CHECK: [[BLOCK_STORAGE:%.*]] = alloc_stack $@block_storage UnsafeContinuation<Int, Never>
// CHECK: [[CONT_SLOT:%.*]] = project_block_storage [[BLOCK_STORAGE]]
// CHECK: store [[WRAPPED]] to [trivial] [[CONT_SLOT]]
// CHECK: [[BLOCK_IMPL:%.*]] = function_ref @[[INT_COMPLETION_BLOCK:.*]] : $@convention(c) (@inout_aliasable @block_storage UnsafeContinuation<Int, Never>, Int) -> ()
// CHECK: [[BLOCK:%.*]] = init_block_storage_header [[BLOCK_STORAGE]] {{.*}}, invoke [[BLOCK_IMPL]]
// CHECK: apply [[METHOD]]([[ARG]], [[BLOCK]], %0)
// CHECK: [[COPY:%.*]] = copy_value [[ARG]]
// CHECK: destroy_value [[ARG]]
// CHECK: await_async_continuation [[CONT]] {{.*}}, resume [[RESUME:bb[0-9]+]]
// CHECK: [[RESUME]]:
// CHECK: [[RESULT:%.*]] = load [trivial] [[RESUME_BUF]]
// CHECK: fix_lifetime [[COPY]]
// CHECK: destroy_value [[COPY]]
// CHECK: hop_to_executor %6 : $MainActor
// CHECK: dealloc_stack [[RESUME_BUF]]
let _: Int = await slowServer.doSomethingSlow("mail")
}