Skip to content

Commit ac47357

Browse files
authored
[Distributed] Complete handling of protocol calls (#72549)
1 parent d426b6f commit ac47357

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+576
-234
lines changed

include/swift/AST/AccessorKinds.def

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@
121121
/// move-only types are introduced.
122122
OBJC_ACCESSOR(Get, get)
123123

124+
/// This is a "distributed thunk" getter, that is used for a "distributed var"
125+
/// 'get { ... }' accessor.
126+
///
127+
/// A distributed thunk is mangled as it's Storage's name.
128+
/// This is never invoked directly by user code, but whenever making calls on
129+
/// a distributed actor and the target is not known to be local, we invoke the
130+
/// thunk. This is equivalent to a `distributed_thunk' FuncDecl of a
131+
/// 'distributed func'.
132+
///
133+
/// For the most part, its behavior is exactly the same as a normal getter.
134+
OBJC_ACCESSOR(DistributedGet, _distributed_get)
135+
124136
/// This is a setter: a function that is called when a value is assigned
125137
/// to the storage. It takes a value of the storage type as its
126138
/// primary parameter, in addition to any index values that may be

include/swift/AST/Decl.h

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2788,6 +2788,12 @@ class ValueDecl : public Decl {
27882788
/// can be distributed.
27892789
bool isDistributed() const;
27902790

2791+
/// Is this a '_distributed_get' accessor?
2792+
///
2793+
/// These are special accessors used by distributed thunks, implementing
2794+
/// `distributed var get { }` accessors.
2795+
bool isDistributedGetAccessor() const;
2796+
27912797
bool hasName() const { return bool(Name); }
27922798
bool isOperator() const { return Name.isOperator(); }
27932799

@@ -8050,7 +8056,7 @@ class AccessorDecl final : public FuncDecl {
80508056
throws, throwsLoc, thrownTy, hasImplicitSelfDecl,
80518057
/*genericParams*/ nullptr, parent),
80528058
AccessorKeywordLoc(accessorKeywordLoc), Storage(storage) {
8053-
assert(!async || accessorKind == AccessorKind::Get
8059+
assert(!async || (accessorKind == AccessorKind::Get || accessorKind == AccessorKind::DistributedGet)
80548060
&& "only get accessors can be async");
80558061
Bits.AccessorDecl.AccessorKind = unsigned(accessorKind);
80568062
}
@@ -8082,6 +8088,14 @@ class AccessorDecl final : public FuncDecl {
80828088
TypeLoc thrownType, ParameterList *parameterList, Type fnRetType,
80838089
DeclContext *parent, ClangNode clangNode = ClangNode());
80848090

8091+
static AccessorDecl *createImplicit(ASTContext &Context,
8092+
AccessorKind accessorKind,
8093+
AbstractStorageDecl *storage,
8094+
bool async,
8095+
bool throws, TypeLoc thrownType,
8096+
Type fnRetType,
8097+
DeclContext *parent);
8098+
80858099
/// Create a parsed accessor.
80868100
///
80878101
/// \param paramList A parameter list for e.g \c set(newValue), or \c nullptr
@@ -8110,6 +8124,7 @@ class AccessorDecl final : public FuncDecl {
81108124
}
81118125

81128126
bool isGetter() const { return getAccessorKind() == AccessorKind::Get; }
8127+
bool isDistributedGetter() const { return getAccessorKind() == AccessorKind::DistributedGet; }
81138128
bool isSetter() const { return getAccessorKind() == AccessorKind::Set; }
81148129
bool isAnyAddressor() const {
81158130
auto kind = getAccessorKind();

include/swift/SIL/SILWitnessVisitor.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
129129
asDerived().addMethod(SILDeclRef(accessor, SILDeclRef::Kind::Func));
130130
addAutoDiffDerivativeMethodsIfRequired(accessor,
131131
SILDeclRef::Kind::Func);
132+
addDistributedWitnessMethodsIfRequired(accessor,
133+
SILDeclRef::Kind::Func);
132134
}
133135
});
134136
}
@@ -201,10 +203,13 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
201203

202204
void addDistributedWitnessMethodsIfRequired(AbstractFunctionDecl *AFD,
203205
SILDeclRef::Kind kind) {
204-
if (!AFD || !AFD->isDistributed())
206+
if (!AFD)
205207
return;
206208

207209
auto thunk = AFD->getDistributedThunk();
210+
if (!thunk)
211+
return;
212+
208213
SILDeclRef declRef(thunk, kind);
209214
asDerived().addMethod(declRef.asDistributed());
210215
}

lib/APIDigester/ModuleAnalyzerNodes.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,6 +1745,7 @@ SDKContext::shouldIgnore(Decl *D, const Decl* Parent) const {
17451745
if (!checkingABI()) {
17461746
switch (ACC->getAccessorKind()) {
17471747
case AccessorKind::Get:
1748+
case AccessorKind::DistributedGet:
17481749
case AccessorKind::Set:
17491750
break;
17501751
default:

lib/AST/ASTMangler.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ static StringRef getCodeForAccessorKind(AccessorKind kind) {
8282
switch (kind) {
8383
case AccessorKind::Get:
8484
return "g";
85+
case AccessorKind::DistributedGet:
86+
// TODO(distributed): probably does not matter since we mangle distributed
87+
// thunk getters as the name of the Storage?
88+
return "g";
8589
case AccessorKind::Set:
8690
return "s";
8791
case AccessorKind::WillSet:
@@ -4283,7 +4287,17 @@ void ASTMangler::appendDistributedThunk(
42834287
appendContextOf(thunk, base);
42844288
}
42854289

4286-
appendIdentifier(thunk->getBaseName().getIdentifier().str());
4290+
if (auto accessor = dyn_cast<AccessorDecl>(thunk)) {
4291+
assert(accessor->getAccessorKind() == AccessorKind::DistributedGet &&
4292+
"Only accessors marked as _distributed_get are expected to be "
4293+
"mangled as thunks");
4294+
// A distributed getter is mangled as the name of its storage (i.e. "the
4295+
// var")
4296+
appendIdentifier(accessor->getStorage()->getBaseIdentifier().str());
4297+
} else {
4298+
appendIdentifier(thunk->getBaseIdentifier().str());
4299+
}
4300+
42874301
appendDeclType(thunk, base, FunctionMangling);
42884302
appendOperator("F");
42894303
appendSymbolKind(SymbolKind::DistributedThunk);

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4011,6 +4011,7 @@ void PrintAST::visitAccessorDecl(AccessorDecl *decl) {
40114011

40124012
switch (auto kind = decl->getAccessorKind()) {
40134013
case AccessorKind::Get:
4014+
case AccessorKind::DistributedGet:
40144015
case AccessorKind::Address:
40154016
case AccessorKind::Read:
40164017
case AccessorKind::Modify:

lib/AST/AccessRequests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ AccessLevelRequest::evaluate(Evaluator &evaluator, ValueDecl *D) const {
5252
AbstractStorageDecl *storage = accessor->getStorage();
5353
switch (accessor->getAccessorKind()) {
5454
case AccessorKind::Get:
55+
case AccessorKind::DistributedGet:
5556
case AccessorKind::Address:
5657
case AccessorKind::Read:
5758
return storage->getFormalAccess();

lib/AST/Decl.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ DescriptiveDeclKind Decl::getDescriptiveKind() const {
232232

233233
switch (accessor->getAccessorKind()) {
234234
case AccessorKind::Get:
235+
case AccessorKind::DistributedGet:
235236
return DescriptiveDeclKind::Getter;
236237

237238
case AccessorKind::Set:
@@ -993,8 +994,10 @@ std::optional<Type> AbstractFunctionDecl::getEffectiveThrownErrorType() const {
993994
// has a cyclic reference if we try to get its interface type here. Find a
994995
// better way to express this.
995996
if (auto accessor = dyn_cast<AccessorDecl>(this)) {
996-
if (accessor->getAccessorKind() != AccessorKind::Get)
997+
if (accessor->getAccessorKind() != AccessorKind::Get &&
998+
accessor->getAccessorKind() != AccessorKind::DistributedGet) {
997999
return std::nullopt;
1000+
}
9981001
}
9991002

10001003
Type interfaceType = getInterfaceType();
@@ -2787,6 +2790,7 @@ bool AbstractStorageDecl::requiresOpaqueAccessors() const {
27872790
bool AbstractStorageDecl::requiresOpaqueAccessor(AccessorKind kind) const {
27882791
switch (kind) {
27892792
case AccessorKind::Get:
2793+
case AccessorKind::DistributedGet:
27902794
return requiresOpaqueGetter();
27912795
case AccessorKind::Set:
27922796
return requiresOpaqueSetter();
@@ -6805,6 +6809,8 @@ StringRef swift::getAccessorNameForDiagnostic(AccessorKind accessorKind,
68056809
switch (accessorKind) {
68066810
case AccessorKind::Get:
68076811
return article ? "a getter" : "getter";
6812+
case AccessorKind::DistributedGet:
6813+
return article ? "a distributed getter" : "distributed getter";
68086814
case AccessorKind::Set:
68096815
return article ? "a setter" : "setter";
68106816
case AccessorKind::Address:
@@ -8958,6 +8964,7 @@ DeclName AbstractFunctionDecl::getEffectiveFullName() const {
89588964
case AccessorKind::Address:
89598965
case AccessorKind::MutableAddress:
89608966
case AccessorKind::Get:
8967+
case AccessorKind::DistributedGet:
89618968
case AccessorKind::Read:
89628969
case AccessorKind::Modify:
89638970
return subscript ? subscript->getName()
@@ -10148,6 +10155,25 @@ AccessorDecl *AccessorDecl::create(ASTContext &ctx, SourceLoc declLoc,
1014810155
return D;
1014910156
}
1015010157

10158+
AccessorDecl *AccessorDecl::createImplicit(ASTContext &ctx,
10159+
AccessorKind accessorKind,
10160+
AbstractStorageDecl *storage,
10161+
bool async, bool throws,
10162+
TypeLoc thrownType,
10163+
Type fnRetType,
10164+
DeclContext *parent) {
10165+
AccessorDecl *D = AccessorDecl::createImpl(
10166+
ctx, /*declLoc=*/SourceLoc(),
10167+
/*accessorKeywordLoc=*/SourceLoc(), accessorKind,
10168+
storage, async, /*asyncLoc=*/SourceLoc(),
10169+
/*throws=*/true, /*throwsLoc=*/SourceLoc(),
10170+
thrownType, parent,
10171+
/*clangNode=*/ClangNode());
10172+
D->setImplicit();
10173+
D->setResultInterfaceType(fnRetType);
10174+
return D;
10175+
}
10176+
1015110177
AccessorDecl *AccessorDecl::createParsed(
1015210178
ASTContext &ctx, AccessorKind accessorKind, AbstractStorageDecl *storage,
1015310179
SourceLoc declLoc, SourceLoc accessorKeywordLoc, ParameterList *paramList,
@@ -10226,6 +10252,7 @@ StringRef AccessorDecl::implicitParameterNameFor(AccessorKind kind) {
1022610252
case AccessorKind::DidSet:
1022710253
return "oldValue";
1022810254
case AccessorKind::Get:
10255+
case AccessorKind::DistributedGet:
1022910256
case AccessorKind::Read:
1023010257
case AccessorKind::Modify:
1023110258
case AccessorKind::Address:
@@ -10237,6 +10264,7 @@ StringRef AccessorDecl::implicitParameterNameFor(AccessorKind kind) {
1023710264
bool AccessorDecl::isAssumedNonMutating() const {
1023810265
switch (getAccessorKind()) {
1023910266
case AccessorKind::Get:
10267+
case AccessorKind::DistributedGet:
1024010268
case AccessorKind::Address:
1024110269
case AccessorKind::Read:
1024210270
return true;
@@ -10270,6 +10298,9 @@ void AccessorDecl::printUserFacingName(raw_ostream &out) const {
1027010298
case AccessorKind::Get:
1027110299
out << "getter:";
1027210300
break;
10301+
case AccessorKind::DistributedGet:
10302+
out << "_distributed_getter:";
10303+
break;
1027310304
case AccessorKind::Set:
1027410305
out << "setter:";
1027510306
break;

lib/AST/DistributedDecl.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1289,6 +1289,16 @@ bool ValueDecl::isDistributed() const {
12891289
return getAttrs().hasAttribute<DistributedActorAttr>();
12901290
}
12911291

1292+
bool ValueDecl::isDistributedGetAccessor() const {
1293+
if (auto accessor = dyn_cast<AccessorDecl>(this)) {
1294+
if (accessor->getAccessorKind() == AccessorKind::DistributedGet) {
1295+
return true;
1296+
}
1297+
}
1298+
1299+
return false;
1300+
}
1301+
12921302
ConstructorDecl *
12931303
NominalTypeDecl::getDistributedRemoteCallTargetInitFunction() const {
12941304
auto mutableThis = const_cast<NominalTypeDecl *>(this);
@@ -1349,10 +1359,22 @@ AbstractFunctionDecl::getDistributedThunk() const {
13491359
if (isDistributedThunk())
13501360
return const_cast<FuncDecl *>(dyn_cast<FuncDecl>(this));
13511361

1362+
auto mutableThis = const_cast<AbstractFunctionDecl *>(this);
1363+
1364+
// For an accessor, get the Storage (VarDecl) and get the thunk off it.
1365+
//
1366+
// Since only 'get' computed distributed properties are allowed, we know
1367+
// this will be the equivalent 'get' thunk for this AccessorDecl.
1368+
//
1369+
// The AccessorDecl is not marked distributed, but the VarDecl will be.
1370+
if (auto accessor = dyn_cast<AccessorDecl>(mutableThis)) {
1371+
auto Storage = accessor->getStorage();
1372+
return Storage->getDistributedThunk();
1373+
}
1374+
13521375
if (!isDistributed())
13531376
return nullptr;
13541377

1355-
auto mutableThis = const_cast<AbstractFunctionDecl *>(this);
13561378
return evaluateOrDefault(
13571379
getASTContext().evaluator,
13581380
GetDistributedThunkRequest{mutableThis},

lib/AST/PrettyStackTrace.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
6767
case AccessorKind::Get:
6868
out << "getter";
6969
break;
70+
case AccessorKind::DistributedGet:
71+
out << "_distributed_getter";
72+
break;
7073
case AccessorKind::Set:
7174
out << "setter";
7275
break;

lib/IRGen/GenClass.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,6 +1865,7 @@ namespace {
18651865
#define OBJC_ACCESSOR(ID, KEYWORD)
18661866
#define ACCESSOR(ID) \
18671867
case AccessorKind::ID:
1868+
case AccessorKind::DistributedGet:
18681869
#include "swift/AST/AccessorKinds.def"
18691870
llvm_unreachable("shouldn't be trying to build this accessor");
18701871
}

lib/IRGen/GenMeta.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ static Flags getMethodDescriptorFlags(ValueDecl *fn) {
266266
#define OPAQUE_ACCESSOR(ID, KEYWORD)
267267
#define ACCESSOR(ID) \
268268
case AccessorKind::ID:
269+
case AccessorKind::DistributedGet:
269270
#include "swift/AST/AccessorKinds.def"
270271
llvm_unreachable("these accessors never appear in protocols or v-tables");
271272
}
@@ -919,7 +920,7 @@ namespace {
919920
{
920921
auto *requirement = cast<AbstractFunctionDecl>(func.getDecl());
921922
if (requirement->isDistributedThunk()) {
922-
// when thunk, because in protocol we want accessof for the thunk
923+
// when thunk, because in protocol we want access of for the thunk
923924
IGM.emitDistributedTargetAccessor(requirement);
924925
}
925926
}
@@ -992,6 +993,13 @@ namespace {
992993
}
993994
}
994995

996+
if (entry.isFunction() &&
997+
entry.getFunction().getDecl()->isDistributedGetAccessor()) {
998+
// We avoid emitting _distributed_get accessors, as they cannot be
999+
// referred to anyway
1000+
continue;
1001+
}
1002+
9951003
if (entry.isAssociatedType()) {
9961004
auto assocType = entry.getAssociatedType();
9971005
// Define the associated type descriptor to point to the current

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
600600
case AccessorKind::Get:
601601
Kind = ".get";
602602
break;
603+
case AccessorKind::DistributedGet:
604+
Kind = "._distributed_get";
605+
break;
603606
case AccessorKind::Set:
604607
Kind = ".set";
605608
break;

lib/Index/Index.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ printArtificialName(const swift::AbstractStorageDecl *ASD, AccessorKind AK, llvm
4747
case AccessorKind::Get:
4848
OS << "getter:" << ASD->getName();
4949
return false;
50+
case AccessorKind::DistributedGet:
51+
OS << "_distributed_getter:" << ASD->getName();
52+
return false;
5053
case AccessorKind::Set:
5154
OS << "setter:" << ASD->getName();
5255
return false;

lib/Index/IndexSymbol.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ SymbolInfo index::getSymbolInfoForDecl(const Decl *D) {
267267
SymbolSubKind index::getSubKindForAccessor(AccessorKind AK) {
268268
switch (AK) {
269269
case AccessorKind::Get: return SymbolSubKind::AccessorGetter;
270+
case AccessorKind::DistributedGet: return SymbolSubKind::AccessorGetter;
270271
case AccessorKind::Set: return SymbolSubKind::AccessorSetter;
271272
case AccessorKind::WillSet: return SymbolSubKind::SwiftAccessorWillSet;
272273
case AccessorKind::DidSet: return SymbolSubKind::SwiftAccessorDidSet;

0 commit comments

Comments
 (0)