@@ -5228,7 +5228,9 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
5228
5228
CanAnyFunctionType inputSubstType,
5229
5229
AbstractionPattern outputOrigType,
5230
5230
CanAnyFunctionType outputSubstType,
5231
- CanType dynamicSelfType) {
5231
+ CanType dynamicSelfType,
5232
+ llvm::function_ref<void (SILGenFunction &)> emitProlog
5233
+ = [](SILGenFunction &){}) {
5232
5234
PrettyStackTraceSILFunction stackTrace (" emitting reabstraction thunk in" ,
5233
5235
&SGF.F );
5234
5236
auto thunkType = SGF.F .getLoweredFunctionType ();
@@ -5258,6 +5260,8 @@ static void buildThunkBody(SILGenFunction &SGF, SILLocation loc,
5258
5260
}
5259
5261
}
5260
5262
5263
+ emitProlog (SGF);
5264
+
5261
5265
// Translate the argument values. Function parameters are
5262
5266
// contravariant: we want to switch the direction of transformation
5263
5267
// on them by flipping inputOrigType and outputOrigType.
@@ -7087,3 +7091,81 @@ void SILGenFunction::emitProtocolWitness(
7087
7091
// Now that we have finished emitting the function, verify it!
7088
7092
F.verify ();
7089
7093
}
7094
+
7095
+ ManagedValue SILGenFunction::emitActorIsolationErasureThunk (
7096
+ SILLocation loc, ManagedValue func,
7097
+ CanAnyFunctionType isolatedType, CanAnyFunctionType nonIsolatedType) {
7098
+ auto globalActor = isolatedType->getGlobalActor ();
7099
+
7100
+ assert (globalActor);
7101
+ assert (!nonIsolatedType->getGlobalActor ());
7102
+
7103
+ CanSILFunctionType loweredIsolatedType =
7104
+ func.getType ().castTo <SILFunctionType>();
7105
+ CanSILFunctionType loweredNonIsolatedType =
7106
+ getLoweredType (nonIsolatedType).castTo <SILFunctionType>();
7107
+
7108
+ LLVM_DEBUG (
7109
+ llvm::dbgs () << " === Generating actor isolation erasure thunk for:" ;
7110
+ loweredIsolatedType.dump (llvm::dbgs ()); llvm::dbgs () << " \n " );
7111
+
7112
+ if (loweredIsolatedType->getPatternSubstitutions ()) {
7113
+ loweredIsolatedType = loweredIsolatedType->getUnsubstitutedType (SGM.M );
7114
+ func = B.createConvertFunction (
7115
+ loc, func, SILType::getPrimitiveObjectType (loweredIsolatedType));
7116
+ }
7117
+
7118
+ auto expectedType = loweredNonIsolatedType->getUnsubstitutedType (SGM.M );
7119
+
7120
+ SubstitutionMap interfaceSubs;
7121
+ GenericEnvironment *genericEnv = nullptr ;
7122
+ CanType dynamicSelfType;
7123
+
7124
+ auto thunkType = buildThunkType (loweredIsolatedType,
7125
+ expectedType,
7126
+ isolatedType,
7127
+ nonIsolatedType,
7128
+ genericEnv,
7129
+ interfaceSubs,
7130
+ dynamicSelfType);
7131
+
7132
+ auto *thunk = SGM.getOrCreateReabstractionThunk (
7133
+ thunkType, loweredIsolatedType, expectedType, dynamicSelfType,
7134
+ globalActor->getCanonicalType ());
7135
+
7136
+ if (thunk->empty ()) {
7137
+ thunk->setGenericEnvironment (genericEnv);
7138
+ SILGenFunction thunkSGF (SGM, *thunk, FunctionDC);
7139
+
7140
+ buildThunkBody (
7141
+ thunkSGF, loc, AbstractionPattern (isolatedType), isolatedType,
7142
+ AbstractionPattern (nonIsolatedType), nonIsolatedType, dynamicSelfType,
7143
+ [&loc, &globalActor](SILGenFunction &thunkSGF) {
7144
+ auto expectedExecutor =
7145
+ thunkSGF.emitLoadGlobalActorExecutor (globalActor);
7146
+ thunkSGF.emitPreconditionCheckExpectedExecutor (loc, expectedExecutor);
7147
+ });
7148
+
7149
+ SGM.emitLazyConformancesForFunction (thunk);
7150
+ }
7151
+
7152
+ // Create it in the current function.
7153
+ ManagedValue thunkedFn = createPartialApplyOfThunk (
7154
+ *this , loc, thunk, interfaceSubs, dynamicSelfType, loweredNonIsolatedType,
7155
+ func.ensurePlusOne (*this , loc));
7156
+
7157
+ if (expectedType != loweredNonIsolatedType) {
7158
+ auto escapingExpectedType = loweredNonIsolatedType->getWithExtInfo (
7159
+ loweredNonIsolatedType->getExtInfo ().withNoEscape (false ));
7160
+ thunkedFn = B.createConvertFunction (
7161
+ loc, thunkedFn, SILType::getPrimitiveObjectType (escapingExpectedType));
7162
+ }
7163
+
7164
+ if (loweredIsolatedType->isNoEscape ()) {
7165
+ thunkedFn = B.createConvertEscapeToNoEscape (
7166
+ loc, thunkedFn,
7167
+ SILType::getPrimitiveObjectType (loweredNonIsolatedType));
7168
+ }
7169
+
7170
+ return thunkedFn;
7171
+ }
0 commit comments