@@ -986,7 +986,8 @@ ManagedValue Transform::transformTuple(ManagedValue inputTuple,
986986void SILGenFunction::collectThunkParams (
987987 SILLocation loc, SmallVectorImpl<ManagedValue> ¶ms,
988988 SmallVectorImpl<ManagedValue> *indirectResults,
989- SmallVectorImpl<ManagedValue> *indirectErrors) {
989+ SmallVectorImpl<ManagedValue> *indirectErrors,
990+ ManagedValue *implicitIsolation) {
990991 // Add the indirect results.
991992 for (auto resultTy : F.getConventions ().getIndirectSILResultTypes (
992993 getTypeExpansionContext ())) {
@@ -1028,8 +1029,12 @@ void SILGenFunction::collectThunkParams(
10281029 // If our thunk has an implicit param and we are being asked to forward it,
10291030 // to the callee, skip it. We are going to handle it especially later.
10301031 if (param.hasOption (SILParameterInfo::ImplicitLeading) &&
1031- param.hasOption (SILParameterInfo::Isolated))
1032+ param.hasOption (SILParameterInfo::Isolated)) {
1033+ if (implicitIsolation)
1034+ *implicitIsolation = functionArgument;
10321035 continue ;
1036+ }
1037+
10331038 params.push_back (functionArgument);
10341039 }
10351040}
@@ -5462,9 +5467,18 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
54625467 options |= ThunkGenFlag::CalleeHasImplicitIsolatedParam;
54635468 }
54645469
5470+ // Collect the thunk parameters. We don't need to collect the indirect
5471+ // error parameter because it'll be stored as IndirectErrorResult, which
5472+ // gets used implicitly by emitApplyWithRethrow.
54655473 SmallVector<ManagedValue, 8 > params;
54665474 SmallVector<ManagedValue, 4 > indirectResultParams;
5467- SGF.collectThunkParams (loc, params, &indirectResultParams);
5475+ ManagedValue implicitIsolationParam;
5476+ SGF.collectThunkParams (loc, params, &indirectResultParams,
5477+ /* indirect error params*/ nullptr ,
5478+ &implicitIsolationParam);
5479+
5480+ assert (bool (options & ThunkGenFlag::ThunkHasImplicitIsolatedParam)
5481+ == implicitIsolationParam.isValid ());
54685482
54695483 // Ignore the self parameter at the SIL level. IRGen will use it to
54705484 // recover type metadata.
@@ -5510,9 +5524,11 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
55105524 case FunctionTypeIsolation::Kind::NonIsolated:
55115525 break ;
55125526
5527+ // For a function for caller isolation, we'll have to figure out what the
5528+ // output function's formal isolation is. This is quite doable, but we don't
5529+ // have to do it yet.
55135530 case FunctionTypeIsolation::Kind::NonIsolatedCaller:
5514- hopToIsolatedParameter = true ;
5515- break ;
5531+ llvm_unreachable (" synchronous function has caller isolation?" );
55165532
55175533 // For a function with parameter isolation, we'll have to dig the
55185534 // argument out after translation but before making the call.
@@ -5594,12 +5610,15 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
55945610 /* mandatory*/ false );
55955611 }
55965612
5597- // If we are thunking a nonisolated caller to nonisolated or global actor, we
5598- // need to load the actor .
5613+ // If the input function has caller isolation, we need to fill in that
5614+ // argument with the formal isolation of the output function .
55995615 if (options.contains (ThunkGenFlag::CalleeHasImplicitIsolatedParam)) {
56005616 auto outputIsolation = outputSubstType->getIsolation ();
56015617 switch (outputIsolation.getKind ()) {
56025618 case FunctionTypeIsolation::Kind::NonIsolated:
5619+ case FunctionTypeIsolation::Kind::Erased:
5620+ // Converting a caller-isolated function to @isolated(any) makes
5621+ // it @concurrent. In either case, emit a nil actor reference.
56035622 argValues.push_back (SGF.emitNonIsolatedIsolation (loc).getValue ());
56045623 break ;
56055624 case FunctionTypeIsolation::Kind::GlobalActor: {
@@ -5609,11 +5628,17 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
56095628 SGF.emitGlobalActorIsolation (loc, globalActor).getValue ());
56105629 break ;
56115630 }
5631+ case FunctionTypeIsolation::Kind::NonIsolatedCaller: {
5632+ argValues.push_back (implicitIsolationParam.getValue ());
5633+ break ;
5634+ }
56125635 case FunctionTypeIsolation::Kind::Parameter:
5613- case FunctionTypeIsolation::Kind::Erased:
5614- case FunctionTypeIsolation::Kind::NonIsolatedCaller:
5636+ // This would require a conversion from a type with caller
5637+ // isolation to a type with parameter isolation, which is not
5638+ // currently allowed and probably won't ever be. Anyway, to
5639+ // implement it, we'd need to borrow the isolated parameter
5640+ // and wrap it up as an `Optional<any Actor>`.
56155641 llvm_unreachable (" Should never see this" );
5616- break ;
56175642 }
56185643 }
56195644
@@ -5964,7 +5989,9 @@ static void buildWithoutActuallyEscapingThunkBody(SILGenFunction &SGF,
59645989 SmallVector<ManagedValue, 8 > params;
59655990 SmallVector<ManagedValue, 8 > indirectResults;
59665991 SmallVector<ManagedValue, 1 > indirectErrorResults;
5967- SGF.collectThunkParams (loc, params, &indirectResults, &indirectErrorResults);
5992+ ManagedValue implicitIsolationParam;
5993+ SGF.collectThunkParams (loc, params, &indirectResults, &indirectErrorResults,
5994+ &implicitIsolationParam);
59685995
59695996 // Ignore the self parameter at the SIL level. IRGen will use it to
59705997 // recover type metadata.
@@ -5985,6 +6012,11 @@ static void buildWithoutActuallyEscapingThunkBody(SILGenFunction &SGF,
59856012 for (auto indirectError : indirectErrorResults)
59866013 argValues.push_back (indirectError.getLValueAddress ());
59876014
6015+ // Forward the implicit isolation parameter.
6016+ if (implicitIsolationParam.isValid ()) {
6017+ argValues.push_back (implicitIsolationParam.getValue ());
6018+ }
6019+
59886020 // Add the rest of the arguments.
59896021 forwardFunctionArguments (SGF, loc, fnType, params, argValues);
59906022
@@ -6174,6 +6206,7 @@ ManagedValue SILGenFunction::getThunkedAutoDiffLinearMap(
61746206 return getThunkedResult ();
61756207 thunk->setGenericEnvironment (genericEnv);
61766208
6209+ // FIXME: handle implicit isolation parameter here
61776210 SILGenFunction thunkSGF (SGM, *thunk, FunctionDC);
61786211 SmallVector<ManagedValue, 4 > params;
61796212 SmallVector<ManagedValue, 4 > thunkIndirectResults;
@@ -6516,6 +6549,7 @@ SILFunction *SILGenModule::getOrCreateCustomDerivativeThunk(
65166549 thunk->setGenericEnvironment (thunkGenericEnv);
65176550
65186551 SILGenFunction thunkSGF (*this , *thunk, customDerivativeFn->getDeclContext ());
6552+ // FIXME: handle implicit isolation parameter here
65196553 SmallVector<ManagedValue, 4 > params;
65206554 SmallVector<ManagedValue, 4 > indirectResults;
65216555 SmallVector<ManagedValue, 1 > indirectErrorResults;
0 commit comments