@@ -1990,179 +1990,6 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
1990
1990
B.createReturn (loc, result);
1991
1991
}
1992
1992
1993
- void SILGenFunction::emitDistributedThunk (SILDeclRef thunk) {
1994
- // Check if actor is local or remote and call respective function
1995
- //
1996
- // func X_distributedThunk(...) async throws -> T {
1997
- // if __isRemoteActor(self) {
1998
- // return try await self._remote_X(...)
1999
- // } else {
2000
- // return try await self.X(...)
2001
- // }
2002
- // }
2003
- //
2004
-
2005
- assert (thunk.isDistributed );
2006
- SILDeclRef native = thunk.asDistributed (false );
2007
- auto fd = cast<AbstractFunctionDecl>(thunk.getDecl ());
2008
-
2009
- ASTContext &ctx = getASTContext ();
2010
-
2011
- // Use the same generic environment as the native entry point.
2012
- F.setGenericEnvironment (SGM.Types .getConstantGenericEnvironment (native));
2013
-
2014
- auto loc = thunk.getAsRegularLocation ();
2015
- loc.markAutoGenerated ();
2016
- Scope scope (Cleanups, CleanupLocation (loc));
2017
-
2018
- auto isRemoteBB = createBasicBlock ();
2019
- auto isLocalBB = createBasicBlock ();
2020
- auto localErrorBB = createBasicBlock ();
2021
- auto remoteErrorBB = createBasicBlock ();
2022
- auto localReturnBB = createBasicBlock ();
2023
- auto remoteReturnBB = createBasicBlock ();
2024
- auto errorBB = createBasicBlock ();
2025
- auto returnBB = createBasicBlock ();
2026
-
2027
- auto methodTy = SGM.Types .getConstantOverrideType (getTypeExpansionContext (),
2028
- thunk);
2029
- auto derivativeFnSILTy = SILType::getPrimitiveObjectType (methodTy);
2030
- auto silFnType = derivativeFnSILTy.castTo <SILFunctionType>();
2031
- SILFunctionConventions fnConv (silFnType, SGM.M );
2032
- auto resultType = fnConv.getSILResultType (getTypeExpansionContext ());
2033
-
2034
- auto *selfDecl = fd->getImplicitSelfDecl ();
2035
-
2036
- SmallVector<SILValue, 8 > params;
2037
-
2038
- bindParametersForForwarding (fd->getParameters (), params);
2039
- bindParameterForForwarding (selfDecl, params);
2040
- auto selfValue = ManagedValue::forUnmanaged (params[params.size () - 1 ]);
2041
- auto selfType = selfDecl->getType ();
2042
-
2043
- // if __isRemoteActor(self) {
2044
- // ...
2045
- // } else {
2046
- // ...
2047
- // }
2048
- {
2049
- FuncDecl* isRemoteFn = ctx.getIsRemoteDistributedActor ();
2050
- assert (isRemoteFn &&
2051
- " Could not find 'is remote' function, is the '_Distributed' module available?" );
2052
-
2053
- ManagedValue selfAnyObject = B.createInitExistentialRef (loc, getLoweredType (ctx.getAnyObjectType ()),
2054
- CanType (selfType),
2055
- selfValue, {});
2056
- auto result = emitApplyOfLibraryIntrinsic (loc, isRemoteFn, SubstitutionMap (),
2057
- {selfAnyObject}, SGFContext ());
2058
-
2059
- SILValue isRemoteResult = std::move (result).forwardAsSingleValue (*this , loc);
2060
- SILValue isRemoteResultUnwrapped = emitUnwrapIntegerResult (loc, isRemoteResult);
2061
-
2062
- B.createCondBranch (loc, isRemoteResultUnwrapped, isRemoteBB, isLocalBB);
2063
- }
2064
-
2065
- // // if __isRemoteActor(self)
2066
- // {
2067
- // return try await self._remote_X(...)
2068
- // }
2069
- {
2070
- B.emitBlock (isRemoteBB);
2071
-
2072
- auto *selfTyDecl = FunctionDC->getParent ()->getSelfNominalTypeDecl ();
2073
- assert (selfTyDecl && " distributed function declared outside of actor" );
2074
-
2075
- auto remoteFnDecl = selfTyDecl->lookupDirectRemoteFunc (fd);
2076
- assert (remoteFnDecl && " Could not find _remote_<dist_func_name> function" );
2077
- auto remoteFnRef = SILDeclRef (remoteFnDecl);
2078
-
2079
- SILGenFunctionBuilder builder (SGM);
2080
- auto remoteFnSIL = builder.getOrCreateFunction (loc, remoteFnRef, ForDefinition);
2081
- SILValue remoteFn = B.createFunctionRefFor (loc, remoteFnSIL);
2082
-
2083
- auto subs = F.getForwardingSubstitutionMap ();
2084
-
2085
- SmallVector<SILValue, 8 > remoteParams (params);
2086
-
2087
- B.createTryApply (loc, remoteFn, subs, remoteParams, remoteReturnBB, remoteErrorBB);
2088
- }
2089
-
2090
- // // else
2091
- // {
2092
- // return (try)? (await)? self.X(...)
2093
- // }
2094
- {
2095
- B.emitBlock (isLocalBB);
2096
-
2097
- auto nativeMethodTy = SGM.Types .getConstantOverrideType (getTypeExpansionContext (),
2098
- native);
2099
- auto nativeFnSILTy = SILType::getPrimitiveObjectType (nativeMethodTy);
2100
- auto nativeSilFnType = nativeFnSILTy.castTo <SILFunctionType>();
2101
-
2102
- SILValue nativeFn = emitClassMethodRef (
2103
- loc, params[params.size () - 1 ], native, nativeMethodTy);
2104
- auto subs = F.getForwardingSubstitutionMap ();
2105
-
2106
- if (nativeSilFnType->hasErrorResult ()) {
2107
- B.createTryApply (loc, nativeFn, subs, params, localReturnBB, localErrorBB);
2108
- } else {
2109
- auto result = B.createApply (loc, nativeFn, subs, params);
2110
- B.createBranch (loc, returnBB, {result});
2111
- }
2112
- }
2113
-
2114
- {
2115
- B.emitBlock (remoteErrorBB);
2116
- SILValue error = remoteErrorBB->createPhiArgument (
2117
- fnConv.getSILErrorType (getTypeExpansionContext ()),
2118
- OwnershipKind::Owned);
2119
-
2120
- B.createBranch (loc, errorBB, {error});
2121
- }
2122
-
2123
- {
2124
- B.emitBlock (localErrorBB);
2125
- SILValue error = localErrorBB->createPhiArgument (
2126
- fnConv.getSILErrorType (getTypeExpansionContext ()),
2127
- OwnershipKind::Owned);
2128
-
2129
- B.createBranch (loc, errorBB, {error});
2130
- }
2131
-
2132
- {
2133
- B.emitBlock (remoteReturnBB);
2134
- SILValue result = remoteReturnBB->createPhiArgument (
2135
- resultType, OwnershipKind::Owned);
2136
- B.createBranch (loc, returnBB, {result});
2137
- }
2138
-
2139
- {
2140
- B.emitBlock (localReturnBB);
2141
- SILValue result = localReturnBB->createPhiArgument (
2142
- resultType, OwnershipKind::Owned);
2143
- B.createBranch (loc, returnBB, {result});
2144
- }
2145
-
2146
- // Emit return logic
2147
- {
2148
- B.emitBlock (returnBB);
2149
- SILValue resArg = returnBB->createPhiArgument (
2150
- resultType, OwnershipKind::Owned);
2151
- B.createReturn (loc, resArg);
2152
- }
2153
-
2154
- // Emit the rethrow logic.
2155
- {
2156
- B.emitBlock (errorBB);
2157
- SILValue error = errorBB->createPhiArgument (
2158
- fnConv.getSILErrorType (getTypeExpansionContext ()),
2159
- OwnershipKind::Owned);
2160
-
2161
- Cleanups.emitCleanupsForReturn (CleanupLocation (loc), IsForUnwind);
2162
- B.createThrow (loc, error);
2163
- }
2164
- }
2165
-
2166
1993
static SILValue
2167
1994
getThunkedForeignFunctionRef (SILGenFunction &SGF,
2168
1995
SILLocation loc,
0 commit comments