Skip to content

Commit 0303bed

Browse files
committed
[Distributed] Synthesize thunks for distributed computed properties
1 parent a91f63e commit 0303bed

File tree

6 files changed

+67
-12
lines changed

6 files changed

+67
-12
lines changed

include/swift/AST/Decl.h

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

5358+
/// Return a distributed thunk if this computed property is marked as
5359+
/// 'distributed' and and nullptr otherwise.
5360+
FuncDecl *getDistributedThunk() const;
5361+
53585362
/// Is this var known to be a "local" distributed actor,
53595363
/// if so the implicit throwing ans some isolation checks can be skipped.
53605364
bool isKnownToBeLocal() const;

include/swift/AST/TypeCheckRequests.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,17 +1237,20 @@ class GetDistributedRemoteCallArgumentInitFunctionRequest :
12371237
///
12381238
/// The thunk is responsible for invoking 'remoteCall' when invoked on a remote
12391239
/// 'distributed actor'.
1240-
class GetDistributedThunkRequest :
1241-
public SimpleRequest<GetDistributedThunkRequest,
1242-
FuncDecl *(AbstractFunctionDecl *),
1243-
RequestFlags::Cached> {
1240+
class GetDistributedThunkRequest
1241+
: public SimpleRequest<
1242+
GetDistributedThunkRequest,
1243+
FuncDecl *(llvm::PointerUnion<VarDecl *, AbstractFunctionDecl *>),
1244+
RequestFlags::Cached> {
1245+
using Originator = llvm::PointerUnion<VarDecl *, AbstractFunctionDecl *>;
1246+
12441247
public:
12451248
using SimpleRequest::SimpleRequest;
12461249

12471250
private:
12481251
friend SimpleRequest;
12491252

1250-
FuncDecl *evaluate(Evaluator &evaluator, AbstractFunctionDecl *distributedFunc) const;
1253+
FuncDecl *evaluate(Evaluator &evaluator, Originator originator) const;
12511254

12521255
public:
12531256
// Caching

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ SWIFT_REQUEST(TypeChecker, GetDistributedTargetInvocationResultHandlerOnReturnFu
127127
AbstractFunctionDecl *(NominalTypeDecl *),
128128
Cached, NoLocationInfo)
129129
SWIFT_REQUEST(TypeChecker, GetDistributedThunkRequest,
130-
FuncDecl *(AbstractFunctionDecl *),
130+
FuncDecl *(llvm::PointerUnion<VarDecl *, AbstractFunctionDecl *>),
131131
Cached, NoLocationInfo)
132132
SWIFT_REQUEST(TypeChecker, GetDistributedActorIDPropertyRequest,
133133
VarDecl *(NominalTypeDecl *),

lib/AST/DistributedDecl.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,15 @@ AbstractFunctionDecl *ASTContext::getRemoteCallOnDistributedActorSystem(
13331333
/********************** Distributed Actor Properties **************************/
13341334
/******************************************************************************/
13351335

1336+
FuncDecl *VarDecl::getDistributedThunk() const {
1337+
if (!isDistributed())
1338+
return nullptr;
1339+
1340+
auto mutableThis = const_cast<VarDecl *>(this);
1341+
return evaluateOrDefault(getASTContext().evaluator,
1342+
GetDistributedThunkRequest{mutableThis}, nullptr);
1343+
}
1344+
13361345
FuncDecl*
13371346
AbstractFunctionDecl::getDistributedThunk() const {
13381347
if (!isDistributed())

lib/Sema/CodeSynthesisDistributedActor.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,18 @@ static FuncDecl *createDistributedThunkFunction(FuncDecl *func) {
628628
assert(systemTy &&
629629
"Thunk synthesis must have concrete actor system type available");
630630

631-
DeclName thunkName = func->getName();
631+
DeclName thunkName;
632+
633+
// Since accessors don't have names, let's generate one based on
634+
// the computed property.
635+
if (auto *accessor = dyn_cast<AccessorDecl>(func)) {
636+
auto *var = accessor->getStorage();
637+
thunkName = DeclName(C, var->getBaseName(),
638+
/*argumentNames=*/ArrayRef<Identifier>());
639+
} else {
640+
// Let's use the name of a 'distributed func'
641+
thunkName = func->getName();
642+
}
632643

633644
// --- Prepare generic parameters
634645
GenericParamList *genericParamList = nullptr;
@@ -754,10 +765,21 @@ addDistributedActorCodableConformance(
754765
/*********************** SYNTHESIS ENTRY POINTS *******************************/
755766
/******************************************************************************/
756767

757-
FuncDecl *GetDistributedThunkRequest::evaluate(
758-
Evaluator &evaluator, AbstractFunctionDecl *distributedTarget) const {
759-
if (!distributedTarget->isDistributed())
760-
return nullptr;
768+
FuncDecl *GetDistributedThunkRequest::evaluate(Evaluator &evaluator,
769+
Originator originator) const {
770+
AbstractFunctionDecl *distributedTarget = nullptr;
771+
if (auto *var = originator.dyn_cast<VarDecl *>()) {
772+
if (!var->isDistributed())
773+
return nullptr;
774+
775+
distributedTarget = var->getAccessor(AccessorKind::Get);
776+
} else {
777+
distributedTarget = originator.get<AbstractFunctionDecl *>();
778+
if (!distributedTarget->isDistributed())
779+
return nullptr;
780+
}
781+
782+
assert(distributedTarget);
761783

762784
auto &C = distributedTarget->getASTContext();
763785
auto DC = distributedTarget->getDeclContext();

lib/Sema/TypeCheckDistributed.cpp

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

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

488493
auto &C = func->getASTContext();
489494
auto DC = func->getDeclContext();
@@ -654,6 +659,18 @@ void TypeChecker::checkDistributedActor(SourceFile *SF, NominalTypeDecl *nominal
654659
(void)nominal->getDefaultInitializer();
655660

656661
for (auto member : nominal->getMembers()) {
662+
// A distributed computed property needs to have a thunk for
663+
// its getter accessor.
664+
if (auto *var = dyn_cast<VarDecl>(member)) {
665+
if (!var->isDistributed())
666+
continue;
667+
668+
if (auto thunk = var->getDistributedThunk())
669+
SF->DelayedFunctions.push_back(thunk);
670+
671+
continue;
672+
}
673+
657674
// --- Ensure all thunks
658675
if (auto func = dyn_cast<AbstractFunctionDecl>(member)) {
659676
if (!func->isDistributed())

0 commit comments

Comments
 (0)