Skip to content

Commit cc26650

Browse files
gottesmmxedin
authored andcommitted
[concurrency] Teach NativeToForeign thunk how to handle implicit isolated parameters of a native function.
(cherry picked from commit 61ff4a9)
1 parent 60899ff commit cc26650

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

lib/SILGen/SILGenBridging.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,11 +1373,16 @@ emitObjCThunkArguments(SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
13731373
auto inputs = objcFnTy->getParameters();
13741374
auto nativeInputs = swiftFnTy->getParameters();
13751375
auto fnConv = SGF.silConv.getFunctionConventions(swiftFnTy);
1376-
assert(nativeInputs.size() == bridgedFormalTypes.size());
1377-
assert(nativeInputs.size() == nativeFormalTypes.size());
1376+
bool nativeInputsHasImplicitIsolatedParam = false;
1377+
if (auto param = swiftFnTy->maybeGetIsolatedParameter())
1378+
nativeInputsHasImplicitIsolatedParam = param->hasOption(SILParameterInfo::ImplicitLeading);
1379+
assert(nativeInputs.size() - unsigned(nativeInputsHasImplicitIsolatedParam) == bridgedFormalTypes.size());
1380+
assert(nativeInputs.size() - unsigned(nativeInputsHasImplicitIsolatedParam) == nativeFormalTypes.size());
13781381
assert(inputs.size() ==
13791382
nativeInputs.size() + unsigned(foreignError.has_value())
1380-
+ unsigned(foreignAsync.has_value()));
1383+
+ unsigned(foreignAsync.has_value()) -
1384+
unsigned(nativeInputsHasImplicitIsolatedParam));
1385+
13811386
for (unsigned i = 0, e = inputs.size(); i < e; ++i) {
13821387
SILType argTy = SGF.getSILType(inputs[i], objcFnTy);
13831388
SILValue arg = SGF.F.begin()->createFunctionArgument(argTy);
@@ -1423,13 +1428,13 @@ emitObjCThunkArguments(SILGenFunction &SGF, SILLocation loc, SILDeclRef thunk,
14231428
+ unsigned(foreignAsync.has_value())
14241429
== objcFnTy->getParameters().size() &&
14251430
"objc inputs don't match number of arguments?!");
1426-
assert(bridgedArgs.size() == swiftFnTy->getParameters().size() &&
1431+
assert(bridgedArgs.size() == swiftFnTy->getParameters().size() - bool(nativeInputsHasImplicitIsolatedParam) &&
14271432
"swift inputs don't match number of arguments?!");
14281433
assert((foreignErrorSlot || !foreignError) &&
14291434
"didn't find foreign error slot");
14301435

14311436
// Bridge the input types.
1432-
assert(bridgedArgs.size() == nativeInputs.size());
1437+
assert(bridgedArgs.size() == nativeInputs.size() - bool(nativeInputsHasImplicitIsolatedParam));
14331438
for (unsigned i = 0, size = bridgedArgs.size(); i < size; ++i) {
14341439
// Consider the bridged values to be "call results" since they're coming
14351440
// from potentially nil-unsound ObjC callers.
@@ -1656,6 +1661,31 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
16561661
// we will deallocate it too early.
16571662
Scope argScope(Cleanups, CleanupLocation(loc));
16581663

1664+
// See if our native function has an implicitly isolated parameter. In such a
1665+
// case, we need to pass in the isolation.
1666+
if (auto isolatedParameter = substTy->maybeGetIsolatedParameter();
1667+
isolatedParameter && isolatedParameter->hasOption(SILParameterInfo::ImplicitLeading)) {
1668+
assert(F.isAsync() && "Can only be async");
1669+
assert(isolation && "No isolation?!");
1670+
switch (isolation->getKind()) {
1671+
case ActorIsolation::Unspecified:
1672+
case ActorIsolation::Nonisolated:
1673+
case ActorIsolation::NonisolatedUnsafe:
1674+
case ActorIsolation::CallerIsolationInheriting:
1675+
args.push_back(emitNonIsolatedIsolation(loc).getValue());
1676+
break;
1677+
case ActorIsolation::ActorInstance:
1678+
llvm::report_fatal_error("Should never see this");
1679+
break;
1680+
case ActorIsolation::GlobalActor:
1681+
args.push_back(emitLoadGlobalActorExecutor(isolation->getGlobalActor()));
1682+
break;
1683+
case ActorIsolation::Erased:
1684+
llvm::report_fatal_error("Should never see this");
1685+
break;
1686+
}
1687+
}
1688+
16591689
// Bridge the arguments.
16601690
SILValue foreignErrorSlot;
16611691
SILValue foreignAsyncSlot;

0 commit comments

Comments
 (0)