Skip to content

Commit 4f2c2f0

Browse files
committed
[Distributed] move emitDistributedThunk to SILGenDDistributed
1 parent d1e1638 commit 4f2c2f0

File tree

2 files changed

+173
-173
lines changed

2 files changed

+173
-173
lines changed

lib/SILGen/SILGenBridging.cpp

Lines changed: 0 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,179 +1990,6 @@ void SILGenFunction::emitNativeToForeignThunk(SILDeclRef thunk) {
19901990
B.createReturn(loc, result);
19911991
}
19921992

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-
21661993
static SILValue
21671994
getThunkedForeignFunctionRef(SILGenFunction &SGF,
21681995
SILLocation loc,

lib/SILGen/SILGenDistributed.cpp

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,177 @@ void SILGenFunction::initializeDistributedActorImplicitStorageInit(
104104
assert(transportMember && "Missing DistributedActor.actorTransport member");
105105
assert(idMember && "Missing DistributedActor.id member");
106106

107+
}
108+
109+
void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
110+
// Check if actor is local or remote and call respective function
111+
//
112+
// func X_distributedThunk(...) async throws -> T {
113+
// if __isRemoteActor(self) {
114+
// return try await self._remote_X(...)
115+
// } else {
116+
// return try await self.X(...)
117+
// }
118+
// }
119+
//
120+
121+
assert(thunk.isDistributed);
122+
SILDeclRef native = thunk.asDistributed(false);
123+
auto fd = cast<AbstractFunctionDecl>(thunk.getDecl());
124+
125+
ASTContext &ctx = getASTContext();
126+
127+
// Use the same generic environment as the native entry point.
128+
F.setGenericEnvironment(SGM.Types.getConstantGenericEnvironment(native));
129+
130+
auto loc = thunk.getAsRegularLocation();
131+
loc.markAutoGenerated();
132+
Scope scope(Cleanups, CleanupLocation(loc));
133+
134+
auto isRemoteBB = createBasicBlock();
135+
auto isLocalBB = createBasicBlock();
136+
auto localErrorBB = createBasicBlock();
137+
auto remoteErrorBB = createBasicBlock();
138+
auto localReturnBB = createBasicBlock();
139+
auto remoteReturnBB = createBasicBlock();
140+
auto errorBB = createBasicBlock();
141+
auto returnBB = createBasicBlock();
142+
143+
auto methodTy = SGM.Types.getConstantOverrideType(getTypeExpansionContext(),
144+
thunk);
145+
auto derivativeFnSILTy = SILType::getPrimitiveObjectType(methodTy);
146+
auto silFnType = derivativeFnSILTy.castTo<SILFunctionType>();
147+
SILFunctionConventions fnConv(silFnType, SGM.M);
148+
auto resultType = fnConv.getSILResultType(getTypeExpansionContext());
149+
150+
auto *selfDecl = fd->getImplicitSelfDecl();
151+
152+
SmallVector<SILValue, 8> params;
153+
154+
bindParametersForForwarding(fd->getParameters(), params);
155+
bindParameterForForwarding(selfDecl, params);
156+
auto selfValue = ManagedValue::forUnmanaged(params[params.size() - 1]);
157+
auto selfType = selfDecl->getType();
158+
159+
// if __isRemoteActor(self) {
160+
// ...
161+
// } else {
162+
// ...
163+
// }
164+
{
165+
FuncDecl* isRemoteFn = ctx.getIsRemoteDistributedActor();
166+
assert(isRemoteFn &&
167+
"Could not find 'is remote' function, is the '_Distributed' module available?");
168+
169+
ManagedValue selfAnyObject = B.createInitExistentialRef(loc, getLoweredType(ctx.getAnyObjectType()),
170+
CanType(selfType),
171+
selfValue, {});
172+
auto result = emitApplyOfLibraryIntrinsic(loc, isRemoteFn, SubstitutionMap(),
173+
{selfAnyObject}, SGFContext());
174+
175+
SILValue isRemoteResult = std::move(result).forwardAsSingleValue(*this, loc);
176+
SILValue isRemoteResultUnwrapped = emitUnwrapIntegerResult(loc, isRemoteResult);
177+
178+
B.createCondBranch(loc, isRemoteResultUnwrapped, isRemoteBB, isLocalBB);
179+
}
180+
181+
// // if __isRemoteActor(self)
182+
// {
183+
// return try await self._remote_X(...)
184+
// }
185+
{
186+
B.emitBlock(isRemoteBB);
187+
188+
auto *selfTyDecl = FunctionDC->getParent()->getSelfNominalTypeDecl();
189+
assert(selfTyDecl && "distributed function declared outside of actor");
190+
191+
auto remoteFnDecl = selfTyDecl->lookupDirectRemoteFunc(fd);
192+
assert(remoteFnDecl && "Could not find _remote_<dist_func_name> function");
193+
auto remoteFnRef = SILDeclRef(remoteFnDecl);
194+
195+
SILGenFunctionBuilder builder(SGM);
196+
auto remoteFnSIL = builder.getOrCreateFunction(loc, remoteFnRef, ForDefinition);
197+
SILValue remoteFn = B.createFunctionRefFor(loc, remoteFnSIL);
198+
199+
auto subs = F.getForwardingSubstitutionMap();
200+
201+
SmallVector<SILValue, 8> remoteParams(params);
202+
203+
B.createTryApply(loc, remoteFn, subs, remoteParams, remoteReturnBB, remoteErrorBB);
204+
}
205+
206+
// // else
207+
// {
208+
// return (try)? (await)? self.X(...)
209+
// }
210+
{
211+
B.emitBlock(isLocalBB);
212+
213+
auto nativeMethodTy = SGM.Types.getConstantOverrideType(getTypeExpansionContext(),
214+
native);
215+
auto nativeFnSILTy = SILType::getPrimitiveObjectType(nativeMethodTy);
216+
auto nativeSilFnType = nativeFnSILTy.castTo<SILFunctionType>();
217+
218+
SILValue nativeFn = emitClassMethodRef(
219+
loc, params[params.size() - 1], native, nativeMethodTy);
220+
auto subs = F.getForwardingSubstitutionMap();
221+
222+
if (nativeSilFnType->hasErrorResult()) {
223+
B.createTryApply(loc, nativeFn, subs, params, localReturnBB, localErrorBB);
224+
} else {
225+
auto result = B.createApply(loc, nativeFn, subs, params);
226+
B.createBranch(loc, returnBB, {result});
227+
}
228+
}
229+
230+
{
231+
B.emitBlock(remoteErrorBB);
232+
SILValue error = remoteErrorBB->createPhiArgument(
233+
fnConv.getSILErrorType(getTypeExpansionContext()),
234+
OwnershipKind::Owned);
235+
236+
B.createBranch(loc, errorBB, {error});
237+
}
238+
239+
{
240+
B.emitBlock(localErrorBB);
241+
SILValue error = localErrorBB->createPhiArgument(
242+
fnConv.getSILErrorType(getTypeExpansionContext()),
243+
OwnershipKind::Owned);
244+
245+
B.createBranch(loc, errorBB, {error});
246+
}
247+
248+
{
249+
B.emitBlock(remoteReturnBB);
250+
SILValue result = remoteReturnBB->createPhiArgument(
251+
resultType, OwnershipKind::Owned);
252+
B.createBranch(loc, returnBB, {result});
253+
}
254+
255+
{
256+
B.emitBlock(localReturnBB);
257+
SILValue result = localReturnBB->createPhiArgument(
258+
resultType, OwnershipKind::Owned);
259+
B.createBranch(loc, returnBB, {result});
260+
}
261+
262+
// Emit return logic
263+
{
264+
B.emitBlock(returnBB);
265+
SILValue resArg = returnBB->createPhiArgument(
266+
resultType, OwnershipKind::Owned);
267+
B.createReturn(loc, resArg);
268+
}
269+
270+
// Emit the rethrow logic.
271+
{
272+
B.emitBlock(errorBB);
273+
SILValue error = errorBB->createPhiArgument(
274+
fnConv.getSILErrorType(getTypeExpansionContext()),
275+
OwnershipKind::Owned);
276+
277+
Cleanups.emitCleanupsForReturn(CleanupLocation(loc), IsForUnwind);
278+
B.createThrow(loc, error);
279+
}
107280
}

0 commit comments

Comments
 (0)