Skip to content

Commit d1e1638

Browse files
committed
[Distributed] SILGenDDistributed initializers
1 parent b654a91 commit d1e1638

12 files changed

+264
-258
lines changed

lib/SILGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ add_swift_host_library(swiftSILGen STATIC
1818
SILGenConvert.cpp
1919
SILGenDecl.cpp
2020
SILGenDestructor.cpp
21+
SILGenDistributed.cpp
2122
SILGenDynamicCast.cpp
2223
SILGenEpilog.cpp
2324
SILGenExpr.cpp

lib/SILGen/SILGenConstructor.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -669,20 +669,20 @@ static void emitDefaultActorInitialization(
669669
{ self.borrow(SGF, loc).getValue() });
670670
}
671671

672-
static void emitDistributedRemoteActorInitialization(
673-
SILGenFunction &SGF, SILLocation loc,
674-
ManagedValue self,
675-
bool addressArg, bool transportArg // FIXME: make those real arguments
676-
) {
677-
auto &ctx = SGF.getASTContext();
678-
auto builtinName = ctx.getIdentifier(
679-
getBuiltinName(BuiltinValueKind::InitializeDistributedRemoteActor));
680-
auto resultTy = SGF.SGM.Types.getEmptyTupleType();
681-
682-
FullExpr scope(SGF.Cleanups, CleanupLocation(loc));
683-
SGF.B.createBuiltin(loc, builtinName, resultTy, /*subs*/{},
684-
{ self.borrow(SGF, loc).getValue() });
685-
}
672+
//static void emitDistributedRemoteActorInitialization(
673+
// SILGenFunction &SGF, SILLocation loc,
674+
// ManagedValue self,
675+
// bool addressArg, bool transportArg // FIXME: make those real arguments
676+
// ) {
677+
// auto &ctx = SGF.getASTContext();
678+
// auto builtinName = ctx.getIdentifier(
679+
// getBuiltinName(BuiltinValueKind::InitializeDistributedRemoteActor));
680+
// auto resultTy = SGF.SGM.Types.getEmptyTupleType();
681+
//
682+
// FullExpr scope(SGF.Cleanups, CleanupLocation(loc));
683+
// SGF.B.createBuiltin(loc, builtinName, resultTy, /*subs*/{},
684+
// { self.borrow(SGF, loc).getValue() });
685+
//}
686686

687687
void SILGenFunction::emitConstructorPrologActorHop(
688688
SILLocation loc,
@@ -776,17 +776,12 @@ void SILGenFunction::emitClassConstructorInitializer(ConstructorDecl *ctor) {
776776
if (selfClassDecl->isRootDefaultActor() && !isDelegating) {
777777
SILLocation PrologueLoc(selfDecl);
778778
PrologueLoc.markAsPrologue();
779+
emitDefaultActorInitialization(*this, PrologueLoc, selfArg);
780+
}
779781

780-
if (selfClassDecl->isDistributedActor() &&
781-
ctor->isDistributedActorResolveInit()) {
782-
auto addressArg = false; // TODO: get the address argument
783-
auto transportArg = false; // TODO: get the transport argument
784-
emitDistributedRemoteActorInitialization(*this, PrologueLoc,
785-
selfArg, addressArg, transportArg);
786-
} else {
787-
// is normal (default) actor
788-
emitDefaultActorInitialization(*this, PrologueLoc, selfArg);
789-
}
782+
// Distributed actor initializers implicitly initialize their transport and id
783+
if (selfClassDecl->isDistributedActor() && !isDelegating) {
784+
initializeDistributedActorImplicitStorageInit(ctor, selfArg);
790785
}
791786

792787
if (!ctor->hasStubImplementation()) {

lib/SILGen/SILGenDistributed.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//===--- SILGenConstructor.cpp - SILGen for constructors ------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "ArgumentSource.h"
14+
#include "Conversion.h"
15+
#include "ExecutorBreadcrumb.h"
16+
#include "Initialization.h"
17+
#include "LValue.h"
18+
#include "RValue.h"
19+
#include "SILGenFunction.h"
20+
#include "SILGenFunctionBuilder.h"
21+
#include "Scope.h"
22+
#include "swift/AST/ASTMangler.h"
23+
#include "swift/AST/ForeignErrorConvention.h"
24+
#include "swift/AST/GenericEnvironment.h"
25+
#include "swift/AST/ParameterList.h"
26+
#include "swift/AST/PropertyWrappers.h"
27+
#include "swift/Basic/Defer.h"
28+
#include "swift/SIL/SILArgument.h"
29+
#include "swift/SIL/SILUndef.h"
30+
#include "swift/SIL/TypeLowering.h"
31+
32+
using namespace swift;
33+
using namespace Lowering;
34+
35+
static InitializationPtr
36+
emitDistributedActorTransportInit(SILGenFunction &SGF, VarDecl *selfDecl,
37+
Pattern *transportPattern, VarDecl *transportVar) {
38+
auto self = SGF.emitSelfForMemberInit(SGF, transportPattern, selfDecl);
39+
InitializationPtr initialization =
40+
emitPatternBindingInitialization(transportPattern, SOMEDEST);
41+
42+
//
43+
FullExpr Scope(SGF.Cleanups, CleanupLocation(transportVar->getInitializer()));
44+
emitExprInto(transportVar->getInitializer());
45+
}
46+
47+
48+
void SILGenFunction::initializeDistributedActorImplicitStorageInit(
49+
ConstructorDecl *ctor, ManagedValue selfArg) {
50+
VarDecl *selfDecl = ctor->getImplicitSelfDecl();
51+
auto *dc = ctor->getDeclContext();
52+
auto classDecl = dc->getSelfClassDecl();
53+
auto &C = classDecl->getASTContext();
54+
55+
SILLocation prologueLoc = RegularLocation(ctor);
56+
prologueLoc.markAsPrologue(); // TODO: no idea if this is necessary or makes sense
57+
58+
fprintf(stderr, "[%s:%d] (%s) EMIT initializeDistributedActorImplicitStorageInit\n", __FILE__, __LINE__, __FUNCTION__);
59+
ctor->dump();
60+
fprintf(stderr, "[%s:%d] (%s) CLASS---------------------------------------------\n", __FILE__, __LINE__, __FUNCTION__);
61+
classDecl->dump();
62+
fprintf(stderr, "[%s:%d] (%s) --------------------------------------------------\n", __FILE__, __LINE__, __FUNCTION__);
63+
64+
// find the transport parameter
65+
SILValue transportArgValue = F.getArgument(0);
66+
ManagedValue transportArgManaged = ManagedValue::forUnmanaged(transportArgValue);
67+
68+
auto transportTy = C.getActorTransportType(); // getProtocol(KnownProtocolKind::ActorTransport);
69+
auto identityProtoTy = C.getActorIdentityType(); //getProtocol(KnownProtocolKind::ActorIdentity);
70+
auto anyIdentityTy = C.getAnyActorIdentityType();
71+
72+
VarDecl *transportMember;
73+
VarDecl *idMember;
74+
75+
for (auto member : classDecl->getMembers()) {
76+
PatternBindingDecl *pbd = dyn_cast<PatternBindingDecl>(member);
77+
if (!pbd) continue;
78+
if (pbd->isStatic()) continue;
79+
80+
fprintf(stderr, "[%s:%d] (%s) MEMBER\n", __FILE__, __LINE__, __FUNCTION__);
81+
member->dump();
82+
83+
Pattern *pattern = pbd->getPattern(0);
84+
VarDecl *var = pbd->getSingleVar();
85+
if (!var) continue;
86+
87+
if (var->getName() == C.Id_actorTransport &&
88+
var->getInterfaceType()->isEqual(transportTy)) {
89+
transportMember = var;
90+
fprintf(stderr, "[%s:%d] (%s) FOUND TRANSPORT MEMBER\n", __FILE__, __LINE__, __FUNCTION__);
91+
auto transportInit = emitDistributedActorTransportInit(*this, selfDecl, pattern, var);
92+
} else if (var->getName() == C.Id_id &&
93+
(var->getInterfaceType()->isEqual(identityProtoTy) ||
94+
var->getInterfaceType()->isEqual(anyIdentityTy))) { // TODO(distributed): stick one way to store, but today we can't yet store the existential
95+
idMember = var;
96+
fprintf(stderr, "[%s:%d] (%s) FOUND ID MEMBER\n", __FILE__, __LINE__, __FUNCTION__);
97+
auto transportInit = emitDistributedActorTransportInit(*this, selfDecl, pattern, var);
98+
99+
}
100+
if (transportMember && idMember)
101+
break; // we found all properties we care about, break out of the loop early
102+
}
103+
104+
assert(transportMember && "Missing DistributedActor.actorTransport member");
105+
assert(idMember && "Missing DistributedActor.id member");
106+
107+
}

lib/SILGen/SILGenFunction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,10 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
625625
/// destructor, then implicitly releases the elements of the class.
626626
void emitDestroyingDestructor(DestructorDecl *dd);
627627

628+
/// Initialize the distributed actors transport and id.
629+
void initializeDistributedActorImplicitStorageInit(
630+
ConstructorDecl *ctor, ManagedValue selfArg);
631+
628632
/// Inject distributed actor and transport interaction code into the destructor.
629633
void injectDistributedActorDestructorLifecycleCall(
630634
DestructorDecl *dd, SILValue selfValue, SILBasicBlock *continueBB);

0 commit comments

Comments
 (0)