Skip to content

Commit a99e935

Browse files
committed
[Distributed] implemented storing transport in resolve
1 parent f1a0665 commit a99e935

File tree

6 files changed

+211
-61
lines changed

6 files changed

+211
-61
lines changed

lib/SILGen/SILGenDestructor.cpp

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ void SILGenFunction::emitDestroyingDestructor(DestructorDecl *dd) {
8181
/// and can remove it from its (weak) lookup tables.
8282
if (cd->isDistributedActor()) {
8383
SILBasicBlock *continueBB = createBasicBlock();
84-
injectDistributedActorDestructorLifecycleCall(dd, selfValue, continueBB);
84+
emitDistributedActor_resignAddress(dd, selfValue, continueBB);
8585
B.emitBlock(continueBB);
8686
}
8787

@@ -225,6 +225,127 @@ void SILGenFunction::emitClassMemberDestruction(ManagedValue selfValue,
225225
}
226226
}
227227

228+
//void SILGenFunction::emitClassMemberDestruction(ManagedValue selfValue,
229+
// ClassDecl *cd,
230+
// CleanupLocation cleanupLoc) {
231+
// assert(selfValue.getOwnershipKind() == OwnershipKind::Guaranteed);
232+
// ASTContext &ctx = getASTContext();
233+
// auto normalBB = createBasicBlock();
234+
//
235+
// // TODO(distributed): a remote actor does not have properties,
236+
// // so do not destroy them if the actor is remote
237+
// if (cd->isDistributedActor()) {
238+
// // FIXME(distributed): if remote, do the logic below, otherwise do the normal logic
239+
// auto selfType = cd->getDeclaredInterfaceType();
240+
//
241+
// auto loc = SILLocation(cd);
242+
// loc.markAutoGenerated();
243+
// Scope scope(Cleanups, CleanupLocation(loc));
244+
//
245+
// auto isRemoteBB = createBasicBlock();
246+
// auto isLocalBB = createBasicBlock();
247+
//
248+
// // TODO(distributed): de-duplicate with the thunk logic in SILGenDistributed
249+
// // if __isRemoteActor(self) {
250+
// // ...
251+
// // } else {
252+
// // ...
253+
// // }
254+
// {
255+
// FuncDecl *isRemoteFn = ctx.getIsRemoteDistributedActor();
256+
// assert(isRemoteFn && "Could not find 'is remote' function, is the "
257+
// "'_Distributed' module available?");
258+
//
259+
// ManagedValue selfAnyObject =
260+
// B.createInitExistentialRef(loc, getLoweredType(ctx.getAnyObjectType()),
261+
// CanType(selfType), selfValue, {});
262+
// auto result = emitApplyOfLibraryIntrinsic(
263+
// loc, isRemoteFn, SubstitutionMap(), {selfAnyObject}, SGFContext());
264+
//
265+
// SILValue isRemoteResult =
266+
// std::move(result).forwardAsSingleValue(*this, loc);
267+
// SILValue isRemoteResultUnwrapped =
268+
// emitUnwrapIntegerResult(loc, isRemoteResult);
269+
//
270+
// B.createCondBranch(loc, isRemoteResultUnwrapped, isRemoteBB, isLocalBB);
271+
// }
272+
//
273+
// // // if __isRemoteActor(self)
274+
// // {
275+
// // // nothing
276+
// // }
277+
// {
278+
// B.emitBlock(isRemoteBB);
279+
//
280+
// // TODO: deduplicate in this func, make another bb
281+
// auto builtinName = getASTContext().getIdentifier(
282+
// getBuiltinName(BuiltinValueKind::DestroyDefaultActor));
283+
// auto resultTy = SGM.Types.getEmptyTupleType();
284+
//
285+
// B.createBuiltin(cleanupLoc, builtinName, resultTy, /*subs*/{},
286+
// { selfValue.getValue() });
287+
// }
288+
//
289+
// // // else (local distributed actor)
290+
// // {
291+
// // <continue normal deinit>
292+
// // }
293+
// {
294+
// B.emitBlock(isLocalBB);
295+
//
296+
// B.createBranch(loc, normalBB, {selfValue});
297+
// }
298+
// }
299+
//
300+
// {
301+
// if (cd->isDistributedActor())
302+
// B.emitBlock(normalBB);
303+
//
304+
// for (VarDecl *vd : cd->getStoredProperties()) {
305+
// const TypeLowering &ti = getTypeLowering(vd->getType());
306+
// if (!ti.isTrivial()) {
307+
// SILValue addr =
308+
// B.createRefElementAddr(cleanupLoc, selfValue.getValue(), vd,
309+
// ti.getLoweredType().getAddressType());
310+
// addr = B.createBeginAccess(
311+
// cleanupLoc, addr, SILAccessKind::Deinit, SILAccessEnforcement::Static,
312+
// false /*noNestedConflict*/, false /*fromBuiltin*/);
313+
// B.createDestroyAddr(cleanupLoc, addr);
314+
// B.createEndAccess(cleanupLoc, addr, false /*is aborting*/);
315+
// }
316+
// }
317+
//
318+
// if (cd->isRootDefaultActor()) {
319+
// auto builtinName = getASTContext().getIdentifier(
320+
// getBuiltinName(BuiltinValueKind::DestroyDefaultActor));
321+
// auto resultTy = SGM.Types.getEmptyTupleType();
322+
//
323+
// B.createBuiltin(cleanupLoc, builtinName, resultTy, /*subs*/{},
324+
// { selfValue.getValue() });
325+
// }
326+
// }
327+
//
328+
//
329+
//
330+
//// // Additional destroy logic for actors
331+
//// {
332+
//// llvm::Optional<Identifier> builtinName;
333+
//// if (cd->isDistributedActor()) {
334+
//// builtinName = getASTContext().getIdentifier(
335+
//// getBuiltinName(BuiltinValueKind::DestroyDistributedActor));
336+
//// } else if (cd->isRootDefaultActor()) {
337+
//// builtinName = getASTContext().getIdentifier(
338+
//// getBuiltinName(BuiltinValueKind::DestroyDefaultActor));
339+
//// }
340+
////
341+
//// if (builtinName.hasValue()) {
342+
//// auto resultTy = SGM.Types.getEmptyTupleType();
343+
//// B.createBuiltin(cleanupLoc, *builtinName, resultTy, /*subs*/ {},
344+
//// {selfValue.getValue()});
345+
//// }
346+
//// }
347+
//}
348+
228349

229350
void SILGenFunction::emitObjCDestructor(SILDeclRef dtor) {
230351
auto dd = cast<DestructorDecl>(dtor.getDecl());

lib/SILGen/SILGenDistributed.cpp

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,40 @@ getActorTransportArgument(ASTContext& C, SILFunction& F, ConstructorDecl *ctor)
9797
/// \verbatim
9898
/// self.actorTransport = <<constructor parameter:ActorTransport>>
9999
/// \endverbatim
100+
static void emitDistributedActorStore_transport(
101+
ASTContext& C, SILGenFunction &SGF,
102+
SILValue actorSelf, AbstractFunctionDecl *func,
103+
SILArgument *transportArg) {
104+
auto &B = SGF.B;
105+
auto &F = SGF.F;
106+
auto &SGM = SGF.SGM;
107+
SILGenFunctionBuilder builder(SGM);
108+
109+
auto *dc = func->getDeclContext();
110+
auto classDecl = dc->getSelfClassDecl();
111+
112+
auto loc = SILLocation(func);
113+
loc.markAutoGenerated();
114+
115+
// ==== Prepare the property reference: self.id
116+
auto vars = classDecl->lookupDirect(C.Id_actorTransport);
117+
assert(vars.size() == 1);
118+
auto *var = dyn_cast<VarDecl>(vars.front());
119+
120+
// ----
121+
auto *selfTyDecl = func->getParent()->getSelfNominalTypeDecl();
122+
auto fieldAddr = B.createRefElementAddr(
123+
loc, actorSelf, var,
124+
SGF.getLoweredType(var->getInterfaceType()));
125+
126+
// ==== Store the transport
127+
B.createCopyAddr(loc,
128+
/*src*/transportArg,
129+
/*dest*/fieldAddr,
130+
IsNotTake, IsInitialization); // TODO(distributed): should it be IsTake?
131+
}
132+
133+
// TODO(distributed): remove this store impl and reuse Store_transport
100134
static void
101135
emitDistributedActor_init_transportStore(
102136
SILGenFunction &SGF,
@@ -134,50 +168,44 @@ emitDistributedActor_init_transportStore(
134168
/// \verbatim
135169
/// self.id = <<parameter:identity>>
136170
/// \endverbatim
137-
static void
138-
emitDistributedActorIdentityStore(
171+
static void emitDistributedActorStore_id(
139172
ASTContext& C, SILGenFunction &SGF,
140-
SILValue actorSelf, FuncDecl *func, SILArgument *identityArg) {
173+
SILValue actorSelf, AbstractFunctionDecl *func,
174+
SILArgument *identityArg) {
141175
auto &B = SGF.B;
142176
auto &F = SGF.F;
143177
auto &SGM = SGF.SGM;
144178
SILGenFunctionBuilder builder(SGM);
145179

146180
auto *dc = func->getDeclContext();
147181
auto classDecl = dc->getSelfClassDecl();
148-
assert(classDecl->isDistributedActor());
149182

150183
auto loc = SILLocation(func);
151184
loc.markAutoGenerated();
152185

153186
// ==== Prepare the property reference: self.id
154-
auto idVars = classDecl->lookupDirect(C.Id_id);
155-
assert(idVars.size() == 1);
156-
auto *idVar = dyn_cast<VarDecl>(idVars.front());
187+
auto vars = classDecl->lookupDirect(C.Id_id);
188+
assert(vars.size() == 1);
189+
auto *var = dyn_cast<VarDecl>(vars.front());
157190

158191
// ==== Prepare assignment
159-
// SILValue identityArgValue = identityArg->getValue();
160-
fprintf(stderr, "[%s:%d] (%s) THE ACTOR SELF\n", __FILE__, __LINE__, __FUNCTION__);
161-
actorSelf->dump();
162-
163-
SILValue identityArgValue = identityArg;
164-
auto idFieldAddr = B.createRefElementAddr(
165-
loc, actorSelf, idVar,
166-
SGF.getLoweredType(idVar->getInterfaceType()));
192+
auto fieldAddr = B.createRefElementAddr(
193+
loc, actorSelf, var,
194+
SGF.getLoweredType(var->getInterfaceType()));
167195

168196
// ==== Store the transport
169197
B.createCopyAddr(loc,
170-
/*src*/identityArgValue,
171-
/*dest*/idFieldAddr,
172-
IsNotTake, IsInitialization); // TODO(distributed): should it be take?
198+
/*src*/identityArg,
199+
/*dest*/fieldAddr,
200+
IsNotTake, IsInitialization); // TODO(distributed): should it be IsTake?
173201
}
174202

175203
/// Synthesize the distributed actor's identity (`id`) initialization:
176204
///
177205
/// \verbatim
178206
/// self.id = transport.assignIdentity(Self.self)
179207
/// \endverbatim
180-
static void emitDistributedActorIdentity_init_assignIdentity(
208+
static void emitDistributedActorStore_init_assignIdentity(
181209
SILGenFunction &SGF,
182210
ManagedValue borrowedSelfArg, VarDecl *selfVarDecl,
183211
ConstructorDecl *ctor,
@@ -273,7 +301,7 @@ static void emitDistributedActorIdentity_init_assignIdentity(
273301
{ temp, selfMetatypeValue, transportArchetypeValue});
274302

275303
// ==== Assign the identity to stored property
276-
304+
// TODO(distributed): reuse emitDistributedActorStore_id here, pass the SILValue
277305
// --- Prepare address of self.id
278306
auto idFieldAddr = B.createRefElementAddr(
279307
loc, borrowedSelfArg.getValue(), var,
@@ -325,13 +353,14 @@ void SILGenFunction::initializeDistributedActorImplicitStorageInit(
325353
if (var->getName() == C.Id_actorTransport &&
326354
var->getInterfaceType()->isEqual(transportTy)) {
327355
transportMember = var;
356+
// TODO(distributed): reuse emitDistributedActorStore_transport
328357
emitDistributedActor_init_transportStore(
329358
*this, borrowedSelfArg, selfVarDecl, ctor, pattern, var);
330359
} else if (var->getName() == C.Id_id &&
331360
(var->getInterfaceType()->isEqual(identityProtoTy) ||
332361
var->getInterfaceType()->isEqual(anyIdentityTy))) { // TODO(distributed): stick one way to store, but today we can't yet store the existential
333362
idMember = var;
334-
emitDistributedActorIdentity_init_assignIdentity(
363+
emitDistributedActorStore_init_assignIdentity(
335364
*this, borrowedSelfArg, selfVarDecl, ctor, pattern, var);
336365
}
337366
if (transportMember && idMember) {
@@ -458,7 +487,7 @@ void SILGenFunction::emitDistributedActorFactory(FuncDecl *fd) {
458487
assert(identityArg->getType().getASTType()->isEqual(C.getAnyActorIdentityType()));
459488

460489
// --- Parameter: transport
461-
SILArgument *transportArg = F.getArgument(1); // existential
490+
SILArgument *transportArg = F.getArgument(1);
462491
assert(
463492
transportArg->getType().getASTType()->isEqual(C.getActorTransportType()));
464493

@@ -530,13 +559,12 @@ void SILGenFunction::emitDistributedActorFactory(FuncDecl *fd) {
530559

531560
// ==== Initialize distributed actor properties
532561
// --- Store the identity: self.id = identity
533-
emitDistributedActorIdentityStore(
562+
emitDistributedActorStore_id(
534563
C, *this, /*actorSelf*/remote, fd, identityArg);
535564

536-
// --- Store the transport: self.transport = transport
537-
// FIXME(distributed): IMPLEMENT:
538-
// emitDistributedActorTransportStore(
539-
// *this, borrowedSelfArg, selfVarDecl, fd, transportArg);
565+
// --- Store the transport: self.actorTransport = transport
566+
emitDistributedActorStore_transport(
567+
C, *this, /*actorSelf*/remote, fd, transportArg);
540568

541569
// ==== Return the fully initialized remote instance
542570
B.createReturn(loc, remote);
@@ -576,8 +604,7 @@ void SILGenFunction::emitDistributedActorFactory(FuncDecl *fd) {
576604
/******************* DISTRIBUTED DEINIT: resignAddress ************************/
577605
/******************************************************************************/
578606

579-
580-
void SILGenFunction::injectDistributedActorDestructorLifecycleCall(
607+
void SILGenFunction::emitDistributedActor_resignAddress(
581608
DestructorDecl *dd, SILValue selfValue, SILBasicBlock *continueBB) {
582609
ASTContext &ctx = getASTContext();
583610

lib/SILGen/SILGenFunction.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2010,7 +2010,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
20102010
ConstructorDecl *ctor, ManagedValue selfArg);
20112011

20122012
/// Inject distributed actor and transport interaction code into the destructor.
2013-
void injectDistributedActorDestructorLifecycleCall(
2013+
void emitDistributedActor_resignAddress(
20142014
DestructorDecl *dd, SILValue selfValue, SILBasicBlock *continueBB);
20152015

20162016

0 commit comments

Comments
 (0)