Skip to content

Commit e723550

Browse files
authored
Merge pull request #34588 from eeckstein/async-bug-fix
2 parents 5e34851 + 9fc1171 commit e723550

File tree

5 files changed

+26
-29
lines changed

5 files changed

+26
-29
lines changed

lib/SIL/IR/TypeLowering.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,7 @@ getFunctionInterfaceTypeWithCaptures(TypeConverter &TC,
25112511
auto innerExtInfo =
25122512
AnyFunctionType::ExtInfoBuilder(FunctionType::Representation::Thin,
25132513
funcType->isThrowing())
2514+
.withAsync(funcType->isAsync())
25142515
.build();
25152516

25162517
return CanAnyFunctionType::get(

lib/SIL/Verifier/SILVerifier.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,8 +1595,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
15951595
verifySILFunctionType(resultInfo);
15961596
require(resultInfo->getExtInfo().hasContext(),
15971597
"result of closure cannot have a thin function type");
1598-
require(!resultInfo->isAsync() || PAI->getFunction()->isAsync(),
1599-
"cannot call an async function from a non async function");
16001598

16011599
checkApplyTypeDependentArguments(PAI);
16021600

@@ -1745,8 +1743,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
17451743
"result of function_ref");
17461744
require(!fnType->getExtInfo().hasContext(),
17471745
"function_ref should have a context-free function result");
1748-
require(!fnType->isAsync() || FRI->getFunction()->isAsync(),
1749-
"cannot call an async function from a non-async function");
17501746

17511747
// Note: in SingleFunction mode, we relax some of these checks because
17521748
// we may not have linked everything yet.
@@ -2921,8 +2917,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
29212917
void checkWitnessMethodInst(WitnessMethodInst *AMI) {
29222918
auto methodType = requireObjectType(SILFunctionType, AMI,
29232919
"result of witness_method");
2924-
require(!methodType->isAsync() || AMI->getFunction()->isAsync(),
2925-
"cannot call an async function from a non-async function");
29262920

29272921
auto *protocol
29282922
= dyn_cast<ProtocolDecl>(AMI->getMember().getDecl()->getDeclContext());
@@ -3078,8 +3072,6 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
30783072
"result of class_method");
30793073
require(!methodType->getExtInfo().hasContext(),
30803074
"result method must be of a context-free function type");
3081-
require(!methodType->isAsync() || CMI->getFunction()->isAsync(),
3082-
"cannot call an async function from a non-async function");
30833075

30843076
SILType operandType = CMI->getOperand()->getType();
30853077
require(operandType.isClassOrClassMetatype(),

lib/SILGen/SILGenProlog.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ void SILGenFunction::emitProlog(CaptureInfo captureInfo,
451451
}
452452
}
453453

454+
if (!F.isAsync())
455+
return;
456+
454457
if (auto *funcDecl =
455458
dyn_cast_or_null<AbstractFunctionDecl>(FunctionDC->getAsDecl())) {
456459
auto actorIsolation = getActorIsolation(funcDecl);

test/IRGen/async/async_calls_verifier.sil

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ bb0:
3636
}
3737

3838
// CHECK-LABEL: Begin Error in function nonasync_apply_caller
39-
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !fnType->isAsync() || FRI->getFunction()->isAsync()
39+
// CHECK: SIL verification failed: cannot call an async function from a non async function: !calleeConv.funcTy->isAsync() || AI->getFunction()->isAsync()
4040
// CHECK: Verifying instruction:
41-
// CHECK: -> // function_ref asynccallee1
41+
// CHECK: // function_ref asynccallee1
4242
// CHECK: %0 = function_ref @asynccallee1 : $@convention(thin) @async () -> () // user: %1
43-
// CHECK: %1 = apply %0() : $@convention(thin) @async () -> ()
43+
// CHECK: -> %1 = apply %0() : $@convention(thin) @async () -> ()
4444
// CHECK-LABEL: End Error in function nonasync_apply_caller
4545
sil [ossa] @nonasync_apply_caller : $@convention(thin) () -> () {
4646
bb0:
@@ -62,11 +62,11 @@ bb2(%result_error : $Error):
6262
}
6363

6464
// CHECK-LABEL: Begin Error in function nonasync_try_apply_caller
65-
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !fnType->isAsync() || FRI->getFunction()->isAsync()
65+
// CHECK: SIL verification failed: cannot call an async function from a non async function: !calleeConv.funcTy->isAsync() || AI->getFunction()->isAsync()
6666
// CHECK: Verifying instruction:
67-
// CHECK: -> // function_ref asynccallee2
67+
// CHECK: // function_ref asynccallee2
6868
// CHECK: %0 = function_ref @asynccallee2 : $@convention(thin) @async () -> ((), @error Error) // user: %1
69-
// CHECK: try_apply %0() : $@convention(thin) @async () -> ((), @error Error), normal bb1, error bb2 // id: %1
69+
// CHECK: -> try_apply %0() : $@convention(thin) @async () -> ((), @error Error), normal bb1, error bb2 // id: %1
7070
// CHECK-LABEL: End Error in function nonasync_try_apply_caller
7171
sil [ossa] @nonasync_try_apply_caller : $@convention(thin) () -> () {
7272
bb0:
@@ -86,13 +86,7 @@ bb0:
8686
return %2 : $@async @callee_owned () -> ()
8787
}
8888

89-
// CHECK-LABEL: Begin Error in function nonasync_partial_apply_caller
90-
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !fnType->isAsync() || FRI->getFunction()->isAsync()
91-
// CHECK: Verifying instruction:
92-
// CHECK: -> // function_ref asynccallee1
93-
// CHECK: %0 = function_ref @asynccallee1 : $@convention(thin) @async () -> () // user: %1
94-
// CHECK: %1 = partial_apply %0() : $@convention(thin) @async () -> () // user: %2
95-
// CHECK-LABEL: End Error in function nonasync_partial_apply_caller
89+
// CHECK-NOT: Function: 'nonasync_partial_apply_caller'
9690
sil [ossa] @nonasync_partial_apply_caller : $@convention(thin) () -> @owned @async @callee_owned () -> () {
9791
bb0:
9892
%1 = function_ref @asynccallee1 : $@async @convention(thin) () -> ()
@@ -113,10 +107,10 @@ bb0(%t : $*T):
113107
}
114108

115109
// CHECK-LABEL: Begin Error in function nonasync_witness_method_caller
116-
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !methodType->isAsync() || AMI->getFunction()->isAsync()
110+
// CHECK: SIL verification failed: cannot call an async function from a non async function: !calleeConv.funcTy->isAsync() || AI->getFunction()->isAsync()
117111
// CHECK: Verifying instruction:
118-
// CHECK: -> %1 = witness_method $T, #P.printMe : <Self where Self : P> (Self) -> () async -> Int64 : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64 // user: %2
119-
// CHECK: %2 = apply %1<T>(%0) : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64 // user: %3
112+
// CHECK: %1 = witness_method $T, #P.printMe : <Self where Self : P> (Self) -> () async -> Int64 : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64 // user: %2
113+
// CHECK: -> %2 = apply %1<T>(%0) : $@convention(witness_method: P) @async <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int64 // user: %3
120114
// CHECK-LABEL: End Error in function nonasync_witness_method_caller
121115
sil hidden @nonasync_witness_method_caller : $@convention(thin) <T where T : P> (@in_guaranteed T) -> Int64 {
122116
bb0(%t : $*T):
@@ -141,11 +135,11 @@ bb0(%int : $Int64, %instance : $S):
141135
}
142136

143137
// CHECK-LABEL: Begin Error in function nonasync_class_method_caller
144-
// CHECK: SIL verification failed: cannot call an async function from a non-async function: !methodType->isAsync() || CMI->getFunction()->isAsync()
138+
// CHECK: SIL verification failed: cannot call an async function from a non async function: !calleeConv.funcTy->isAsync() || AI->getFunction()->isAsync()
145139
// CHECK: Verifying instruction:
146140
// CHECK: %1 = argument of bb0 : $S // users: %3, %2
147-
// CHECK: -> %2 = class_method %1 : $S, #S.method : (S) -> (Int64) async -> (), $@convention(method) @async (Int64, @guaranteed S) -> () // user: %3
148-
// CHECK: %3 = apply %2(%0, %1) : $@convention(method) @async (Int64, @guaranteed S) -> ()
141+
// CHECK: %2 = class_method %1 : $S, #S.method : (S) -> (Int64) async -> (), $@convention(method) @async (Int64, @guaranteed S) -> () // user: %3
142+
// CHECK: -> %3 = apply %2(%0, %1) : $@convention(method) @async (Int64, @guaranteed S) -> ()
149143
// CHECK-LABEL: End Error in function nonasync_class_method_caller
150144
sil hidden @nonasync_class_method_caller : $@convention(thin) (Int64, S) -> () {
151145
bb0(%int : $Int64, %instance : $S):

test/SILGen/hop_to_executor.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ actor class MyActor {
4747
await callee(p)
4848
}
4949

50+
// CHECK-LABEL: sil hidden [ossa] @$s4test7MyActorC13dontInsertHTESiyF : $@convention(method) (@guaranteed MyActor) -> Int {
51+
// CHECK-NOT: hop_to_executor
52+
// CHECK: } // end sil function '$s4test7MyActorC13dontInsertHTESiyF'
53+
func dontInsertHTE() -> Int {
54+
return p
55+
}
56+
5057
init() {
5158
p = 27
5259
}
@@ -57,7 +64,7 @@ struct GlobalActor {
5764
static var shared: MyActor = MyActor()
5865
}
5966

60-
// CHECK-LABEL: sil hidden [ossa] @$s4test0A11GlobalActoryyYF : $@convention(thin) () -> () {
67+
// CHECK-LABEL: sil hidden [ossa] @$s4test0A11GlobalActoryyYF : $@convention(thin) @async () -> () {
6168
// CHECK: [[F:%[0-9]+]] = function_ref @$s4test11GlobalActorV6sharedAA02MyC0Cvau : $@convention(thin) () -> Builtin.RawPointer
6269
// CHECK: [[P:%[0-9]+]] = apply [[F]]() : $@convention(thin) () -> Builtin.RawPointer
6370
// CHECK: [[A:%[0-9]+]] = pointer_to_address %2 : $Builtin.RawPointer to [strict] $*MyActor
@@ -75,7 +82,7 @@ struct GenericGlobalActorWithGetter<T> {
7582
static var shared: MyActor { return MyActor() }
7683
}
7784

78-
// CHECK-LABEL: sil hidden [ossa] @$s4test0A28GenericGlobalActorWithGetteryyYF : $@convention(thin) () -> () {
85+
// CHECK-LABEL: sil hidden [ossa] @$s4test0A28GenericGlobalActorWithGetteryyYF : $@convention(thin) @async () -> () {
7986
// CHECK: [[MT:%[0-9]+]] = metatype $@thin GenericGlobalActorWithGetter<Int>.Type
8087
// CHECK: [[F:%[0-9]+]] = function_ref @$s4test28GenericGlobalActorWithGetterV6sharedAA02MyD0CvgZ : $@convention(method) <τ_0_0> (@thin GenericGlobalActorWithGetter<τ_0_0>.Type) -> @owned MyActor
8188
// CHECK: [[A:%[0-9]+]] = apply [[F]]<Int>([[MT]]) : $@convention(method) <τ_0_0> (@thin GenericGlobalActorWithGetter<τ_0_0>.Type) -> @owned MyActor

0 commit comments

Comments
 (0)