@@ -987,7 +987,8 @@ ManagedValue Transform::transformTuple(ManagedValue inputTuple,
987987void SILGenFunction::collectThunkParams (
988988 SILLocation loc, SmallVectorImpl<ManagedValue> ¶ms,
989989 SmallVectorImpl<ManagedValue> *indirectResults,
990- SmallVectorImpl<ManagedValue> *indirectErrors) {
990+ SmallVectorImpl<ManagedValue> *indirectErrors,
991+ ManagedValue *implicitIsolation) {
991992 // Add the indirect results.
992993 for (auto resultTy : F.getConventions ().getIndirectSILResultTypes (
993994 getTypeExpansionContext ())) {
@@ -1029,8 +1030,12 @@ void SILGenFunction::collectThunkParams(
10291030 // If our thunk has an implicit param and we are being asked to forward it,
10301031 // to the callee, skip it. We are going to handle it especially later.
10311032 if (param.hasOption (SILParameterInfo::ImplicitLeading) &&
1032- param.hasOption (SILParameterInfo::Isolated))
1033+ param.hasOption (SILParameterInfo::Isolated)) {
1034+ if (implicitIsolation)
1035+ *implicitIsolation = functionArgument;
10331036 continue ;
1037+ }
1038+
10341039 params.push_back (functionArgument);
10351040 }
10361041}
@@ -5463,9 +5468,18 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
54635468 options |= ThunkGenFlag::CalleeHasImplicitIsolatedParam;
54645469 }
54655470
5471+ // Collect the thunk parameters. We don't need to collect the indirect
5472+ // error parameter because it'll be stored as IndirectErrorResult, which
5473+ // gets used implicitly by emitApplyWithRethrow.
54665474 SmallVector<ManagedValue, 8 > params;
54675475 SmallVector<ManagedValue, 4 > indirectResultParams;
5468- SGF.collectThunkParams (loc, params, &indirectResultParams);
5476+ ManagedValue implicitIsolationParam;
5477+ SGF.collectThunkParams (loc, params, &indirectResultParams,
5478+ /* indirect error params*/ nullptr ,
5479+ &implicitIsolationParam);
5480+
5481+ assert (bool (options & ThunkGenFlag::ThunkHasImplicitIsolatedParam)
5482+ == implicitIsolationParam.isValid ());
54695483
54705484 // Ignore the self parameter at the SIL level. IRGen will use it to
54715485 // recover type metadata.
@@ -5511,9 +5525,11 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
55115525 case FunctionTypeIsolation::Kind::NonIsolated:
55125526 break ;
55135527
5528+ // For a function for caller isolation, we'll have to figure out what the
5529+ // output function's formal isolation is. This is quite doable, but we don't
5530+ // have to do it yet.
55145531 case FunctionTypeIsolation::Kind::NonIsolatedCaller:
5515- hopToIsolatedParameter = true ;
5516- break ;
5532+ llvm_unreachable (" synchronous function has caller isolation?" );
55175533
55185534 // For a function with parameter isolation, we'll have to dig the
55195535 // argument out after translation but before making the call.
@@ -5595,12 +5611,15 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
55955611 /* mandatory*/ false );
55965612 }
55975613
5598- // If we are thunking a nonisolated caller to nonisolated or global actor, we
5599- // need to load the actor .
5614+ // If the input function has caller isolation, we need to fill in that
5615+ // argument with the formal isolation of the output function .
56005616 if (options.contains (ThunkGenFlag::CalleeHasImplicitIsolatedParam)) {
56015617 auto outputIsolation = outputSubstType->getIsolation ();
56025618 switch (outputIsolation.getKind ()) {
56035619 case FunctionTypeIsolation::Kind::NonIsolated:
5620+ case FunctionTypeIsolation::Kind::Erased:
5621+ // Converting a caller-isolated function to @isolated(any) makes
5622+ // it @concurrent. In either case, emit a nil actor reference.
56045623 argValues.push_back (SGF.emitNonIsolatedIsolation (loc).getValue ());
56055624 break ;
56065625 case FunctionTypeIsolation::Kind::GlobalActor: {
@@ -5610,11 +5629,17 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
56105629 SGF.emitGlobalActorIsolation (loc, globalActor).getValue ());
56115630 break ;
56125631 }
5632+ case FunctionTypeIsolation::Kind::NonIsolatedCaller: {
5633+ argValues.push_back (implicitIsolationParam.getValue ());
5634+ break ;
5635+ }
56135636 case FunctionTypeIsolation::Kind::Parameter:
5614- case FunctionTypeIsolation::Kind::Erased:
5615- case FunctionTypeIsolation::Kind::NonIsolatedCaller:
5637+ // This would require a conversion from a type with caller
5638+ // isolation to a type with parameter isolation, which is not
5639+ // currently allowed and probably won't ever be. Anyway, to
5640+ // implement it, we'd need to borrow the isolated parameter
5641+ // and wrap it up as an `Optional<any Actor>`.
56165642 llvm_unreachable (" Should never see this" );
5617- break ;
56185643 }
56195644 }
56205645
@@ -5965,7 +5990,9 @@ static void buildWithoutActuallyEscapingThunkBody(SILGenFunction &SGF,
59655990 SmallVector<ManagedValue, 8 > params;
59665991 SmallVector<ManagedValue, 8 > indirectResults;
59675992 SmallVector<ManagedValue, 1 > indirectErrorResults;
5968- SGF.collectThunkParams (loc, params, &indirectResults, &indirectErrorResults);
5993+ ManagedValue implicitIsolationParam;
5994+ SGF.collectThunkParams (loc, params, &indirectResults, &indirectErrorResults,
5995+ &implicitIsolationParam);
59695996
59705997 // Ignore the self parameter at the SIL level. IRGen will use it to
59715998 // recover type metadata.
@@ -5986,6 +6013,11 @@ static void buildWithoutActuallyEscapingThunkBody(SILGenFunction &SGF,
59866013 for (auto indirectError : indirectErrorResults)
59876014 argValues.push_back (indirectError.getLValueAddress ());
59886015
6016+ // Forward the implicit isolation parameter.
6017+ if (implicitIsolationParam.isValid ()) {
6018+ argValues.push_back (implicitIsolationParam.getValue ());
6019+ }
6020+
59896021 // Add the rest of the arguments.
59906022 forwardFunctionArguments (SGF, loc, fnType, params, argValues);
59916023
@@ -6175,6 +6207,7 @@ ManagedValue SILGenFunction::getThunkedAutoDiffLinearMap(
61756207 return getThunkedResult ();
61766208 thunk->setGenericEnvironment (genericEnv);
61776209
6210+ // FIXME: handle implicit isolation parameter here
61786211 SILGenFunction thunkSGF (SGM, *thunk, FunctionDC);
61796212 SmallVector<ManagedValue, 4 > params;
61806213 SmallVector<ManagedValue, 4 > thunkIndirectResults;
@@ -6523,6 +6556,7 @@ SILFunction *SILGenModule::getOrCreateCustomDerivativeThunk(
65236556 thunk->setGenericEnvironment (thunkGenericEnv);
65246557
65256558 SILGenFunction thunkSGF (*this , *thunk, customDerivativeFn->getDeclContext ());
6559+ // FIXME: handle implicit isolation parameter here
65266560 SmallVector<ManagedValue, 4 > params;
65276561 SmallVector<ManagedValue, 4 > indirectResults;
65286562 SmallVector<ManagedValue, 1 > indirectErrorResults;
0 commit comments