Skip to content

Commit 03d47a5

Browse files
xedinktoso
authored andcommitted
[Distributed] Synthesize thunks for distributed computed properties
1 parent 9b73d7c commit 03d47a5

File tree

7 files changed

+80
-12
lines changed

7 files changed

+80
-12
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5345,6 +5345,10 @@ class VarDecl : public AbstractStorageDecl {
53455345
/// Does this have a 'distributed' modifier?
53465346
bool isDistributed() const;
53475347

5348+
/// Return a distributed thunk if this computed property is marked as
5349+
/// 'distributed' and and nullptr otherwise.
5350+
FuncDecl *getDistributedThunk() const;
5351+
53485352
/// Is this var known to be a "local" distributed actor,
53495353
/// if so the implicit throwing ans some isolation checks can be skipped.
53505354
bool isKnownToBeLocal() const;

include/swift/AST/TypeCheckRequests.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,17 +1279,20 @@ class GetDistributedRemoteCallArgumentInitFunctionRequest :
12791279
///
12801280
/// The thunk is responsible for invoking 'remoteCall' when invoked on a remote
12811281
/// 'distributed actor'.
1282-
class GetDistributedThunkRequest :
1283-
public SimpleRequest<GetDistributedThunkRequest,
1284-
FuncDecl *(AbstractFunctionDecl *),
1285-
RequestFlags::Cached> {
1282+
class GetDistributedThunkRequest
1283+
: public SimpleRequest<
1284+
GetDistributedThunkRequest,
1285+
FuncDecl *(llvm::PointerUnion<VarDecl *, AbstractFunctionDecl *>),
1286+
RequestFlags::Cached> {
1287+
using Originator = llvm::PointerUnion<VarDecl *, AbstractFunctionDecl *>;
1288+
12861289
public:
12871290
using SimpleRequest::SimpleRequest;
12881291

12891292
private:
12901293
friend SimpleRequest;
12911294

1292-
FuncDecl *evaluate(Evaluator &evaluator, AbstractFunctionDecl *distributedFunc) const;
1295+
FuncDecl *evaluate(Evaluator &evaluator, Originator originator) const;
12931296

12941297
public:
12951298
// Caching

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ SWIFT_REQUEST(TypeChecker, GetDistributedTargetInvocationResultHandlerOnReturnFu
137137
AbstractFunctionDecl *(NominalTypeDecl *),
138138
Cached, NoLocationInfo)
139139
SWIFT_REQUEST(TypeChecker, GetDistributedThunkRequest,
140-
FuncDecl *(AbstractFunctionDecl *),
140+
FuncDecl *(llvm::PointerUnion<VarDecl *, AbstractFunctionDecl *>),
141141
Cached, NoLocationInfo)
142142
SWIFT_REQUEST(TypeChecker, GetDistributedActorIDPropertyRequest,
143143
VarDecl *(NominalTypeDecl *),

lib/AST/DistributedDecl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,15 @@ AbstractFunctionDecl *ASTContext::getRemoteCallOnDistributedActorSystem(
14041404
/********************** Distributed Actor Properties **************************/
14051405
/******************************************************************************/
14061406

1407+
FuncDecl *VarDecl::getDistributedThunk() const {
1408+
if (!isDistributed())
1409+
return nullptr;
1410+
1411+
auto mutableThis = const_cast<VarDecl *>(this);
1412+
return evaluateOrDefault(getASTContext().evaluator,
1413+
GetDistributedThunkRequest{mutableThis}, nullptr);
1414+
}
1415+
14071416
FuncDecl*
14081417
AbstractFunctionDecl::getDistributedThunk() const {
14091418
if (!isDistributed())

lib/SILGen/SILGenType.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,6 +1161,12 @@ class SILGenType : public TypeMemberVisitor<SILGenType> {
11611161
SGM.emitPropertyWrapperBackingInitializer(vd);
11621162
}
11631163

1164+
if (auto *thunk = vd->getDistributedThunk()) {
1165+
auto thunkRef = SILDeclRef(thunk).asDistributed();
1166+
SGM.emitFunctionDefinition(thunkRef,
1167+
SGM.getFunction(thunkRef, ForDefinition));
1168+
}
1169+
11641170
visitAbstractStorageDecl(vd);
11651171
}
11661172

@@ -1287,6 +1293,13 @@ class SILGenExtension : public TypeMemberVisitor<SILGenExtension> {
12871293
return;
12881294
}
12891295
}
1296+
1297+
if (auto *thunk = vd->getDistributedThunk()) {
1298+
auto thunkRef = SILDeclRef(thunk).asDistributed();
1299+
SGM.emitFunctionDefinition(thunkRef,
1300+
SGM.getFunction(thunkRef, ForDefinition));
1301+
}
1302+
12901303
visitAbstractStorageDecl(vd);
12911304
}
12921305

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,18 @@ static FuncDecl *createDistributedThunkFunction(FuncDecl *func) {
644644
assert(getConcreteReplacementForProtocolActorSystemType(func) &&
645645
"Thunk synthesis must have concrete actor system type available");
646646

647-
DeclName thunkName = func->getName();
647+
DeclName thunkName;
648+
649+
// Since accessors don't have names, let's generate one based on
650+
// the computed property.
651+
if (auto *accessor = dyn_cast<AccessorDecl>(func)) {
652+
auto *var = accessor->getStorage();
653+
thunkName = DeclName(C, var->getBaseName(),
654+
/*argumentNames=*/ArrayRef<Identifier>());
655+
} else {
656+
// Let's use the name of a 'distributed func'
657+
thunkName = func->getName();
658+
}
648659

649660
// --- Prepare generic parameters
650661
GenericParamList *genericParamList = nullptr;
@@ -774,10 +785,21 @@ addDistributedActorCodableConformance(
774785
/*********************** SYNTHESIS ENTRY POINTS *******************************/
775786
/******************************************************************************/
776787

777-
FuncDecl *GetDistributedThunkRequest::evaluate(
778-
Evaluator &evaluator, AbstractFunctionDecl *distributedTarget) const {
779-
if (!distributedTarget->isDistributed())
780-
return nullptr;
788+
FuncDecl *GetDistributedThunkRequest::evaluate(Evaluator &evaluator,
789+
Originator originator) const {
790+
AbstractFunctionDecl *distributedTarget = nullptr;
791+
if (auto *var = originator.dyn_cast<VarDecl *>()) {
792+
if (!var->isDistributed())
793+
return nullptr;
794+
795+
distributedTarget = var->getAccessor(AccessorKind::Get);
796+
} else {
797+
distributedTarget = originator.get<AbstractFunctionDecl *>();
798+
if (!distributedTarget->isDistributed())
799+
return nullptr;
800+
}
801+
802+
assert(distributedTarget);
781803

782804
auto &C = distributedTarget->getASTContext();
783805

lib/Sema/TypeCheckDistributed.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,12 @@ bool swift::checkDistributedFunction(AbstractFunctionDecl *func) {
482482

483483
bool CheckDistributedFunctionRequest::evaluate(
484484
Evaluator &evaluator, AbstractFunctionDecl *func) const {
485-
assert(func->isDistributed());
485+
if (auto *accessor = dyn_cast<AccessorDecl>(func)) {
486+
auto *var = cast<VarDecl>(accessor->getStorage());
487+
assert(var->isDistributed() && accessor->isGetter());
488+
} else {
489+
assert(func->isDistributed());
490+
}
486491

487492
auto &C = func->getASTContext();
488493
auto DC = func->getDeclContext();
@@ -670,6 +675,18 @@ void TypeChecker::checkDistributedActor(SourceFile *SF, NominalTypeDecl *nominal
670675

671676

672677
for (auto member : nominal->getMembers()) {
678+
// A distributed computed property needs to have a thunk for
679+
// its getter accessor.
680+
if (auto *var = dyn_cast<VarDecl>(member)) {
681+
if (!var->isDistributed())
682+
continue;
683+
684+
if (auto thunk = var->getDistributedThunk())
685+
SF->DelayedFunctions.push_back(thunk);
686+
687+
continue;
688+
}
689+
673690
// --- Ensure all thunks
674691
if (auto func = dyn_cast<AbstractFunctionDecl>(member)) {
675692
if (!func->isDistributed())

0 commit comments

Comments
 (0)