Skip to content

Commit 97d1f15

Browse files
committed
[AST] Add a new accessor kind - DistrbiutedThunk
`DistributedThunk` is an accessor that is synthesized for a getter of a distributed computed property and would be called when such a property is used outside of its actor isolation context (just like a distributed method thunk).
1 parent 985a203 commit 97d1f15

27 files changed

+101
-33
lines changed

include/swift/AST/AccessorKinds.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ IMMUTABLE_ADDRESSOR(Address, unsafeAddress)
174174
/// of the type).
175175
MUTABLE_ADDRESSOR(MutableAddress, unsafeMutableAddress)
176176

177+
/// This is a distributed thunk for accessing a getter on a computed
178+
/// property of a distributed actor.
179+
///
180+
/// It returns an owned value of the storage type.
181+
SINGLETON_ACCESSOR(DistributedThunk, _distributed_thunk)
182+
177183
#ifdef LAST_ACCESSOR
178184
LAST_ACCESSOR(MutableAddress)
179185
#undef LAST_ACCESSOR

include/swift/AST/Decl.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ enum class DescriptiveDeclKind : uint8_t {
188188
MissingMember,
189189
Requirement,
190190
OpaqueResultType,
191-
OpaqueVarType
191+
OpaqueVarType,
192+
DistributedThunk,
192193
};
193194

194195
/// Describes which spelling was used in the source for the 'static' or 'class'
@@ -4802,6 +4803,7 @@ class AbstractStorageDecl : public ValueDecl {
48024803
/// The implementation info for the accessors.
48034804
StorageImplInfo ImplInfo;
48044805

4806+
public: // FIXME(distributed): This is temporary
48054807
/// Add a synthesized accessor.
48064808
void setSynthesizedAccessor(AccessorKind kind, AccessorDecl *getter);
48074809

@@ -7026,8 +7028,9 @@ class AccessorDecl final : public FuncDecl {
70267028
hasImplicitSelfDecl, genericParams, parent),
70277029
AccessorKeywordLoc(accessorKeywordLoc),
70287030
Storage(storage) {
7029-
assert(!async || accessorKind == AccessorKind::Get
7030-
&& "only get accessors can be async");
7031+
assert(!async || accessorKind == AccessorKind::Get ||
7032+
accessorKind == AccessorKind::DistributedThunk &&
7033+
"only get accessors can be async");
70317034
Bits.AccessorDecl.AccessorKind = unsigned(accessorKind);
70327035
}
70337036

@@ -7092,6 +7095,10 @@ class AccessorDecl final : public FuncDecl {
70927095
|| kind == AccessorKind::MutableAddress;
70937096
}
70947097

7098+
bool isDistributedThunk() const {
7099+
return getAccessorKind() == AccessorKind::DistributedThunk;
7100+
}
7101+
70957102
/// isGetterOrSetter - Determine whether this is specifically a getter or
70967103
/// a setter, as opposed to some other kind of accessor.
70977104
///

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ using namespace swift::Mangle;
5959
static StringRef getCodeForAccessorKind(AccessorKind kind) {
6060
switch (kind) {
6161
case AccessorKind::Get:
62+
case AccessorKind::DistributedThunk:
6263
return "g";
6364
case AccessorKind::Set:
6465
return "s";

lib/AST/ASTPrinter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3941,6 +3941,7 @@ void PrintAST::visitAccessorDecl(AccessorDecl *decl) {
39413941
case AccessorKind::Modify:
39423942
case AccessorKind::DidSet:
39433943
case AccessorKind::MutableAddress:
3944+
case AccessorKind::DistributedThunk:
39443945
recordDeclLoc(decl,
39453946
[&]{
39463947
Printer << getAccessorLabel(decl->getAccessorKind());

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
case AccessorKind::Get:
5353
case AccessorKind::Address:
5454
case AccessorKind::Read:
55+
case AccessorKind::DistributedThunk:
5556
return storage->getFormalAccess();
5657
case AccessorKind::Set:
5758
case AccessorKind::MutableAddress:

lib/AST/Decl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ DescriptiveDeclKind Decl::getDescriptiveKind() const {
247247

248248
case AccessorKind::Modify:
249249
return DescriptiveDeclKind::ModifyAccessor;
250+
251+
case AccessorKind::DistributedThunk:
252+
return DescriptiveDeclKind::DistributedThunk;
250253
}
251254
llvm_unreachable("bad accessor kind");
252255
}
@@ -348,6 +351,7 @@ StringRef Decl::getDescriptiveKindName(DescriptiveDeclKind K) {
348351
ENTRY(Requirement, "requirement");
349352
ENTRY(OpaqueResultType, "result");
350353
ENTRY(OpaqueVarType, "type");
354+
ENTRY(DistributedThunk, "distributed thunk");
351355
}
352356
#undef ENTRY
353357
llvm_unreachable("bad DescriptiveDeclKind");
@@ -7571,6 +7575,7 @@ DeclName AbstractFunctionDecl::getEffectiveFullName() const {
75717575
case AccessorKind::Get:
75727576
case AccessorKind::Read:
75737577
case AccessorKind::Modify:
7578+
case AccessorKind::DistributedThunk:
75747579
return subscript ? subscript->getName()
75757580
: DeclName(ctx, storage->getBaseName(),
75767581
ArrayRef<Identifier>());
@@ -8573,6 +8578,7 @@ bool AccessorDecl::isAssumedNonMutating() const {
85738578
case AccessorKind::Get:
85748579
case AccessorKind::Address:
85758580
case AccessorKind::Read:
8581+
case AccessorKind::DistributedThunk:
85768582
return true;
85778583

85788584
case AccessorKind::Set:

lib/AST/PrettyStackTrace.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ void swift::printDeclDescription(llvm::raw_ostream &out, const Decl *D,
9191
case AccessorKind::Modify:
9292
out << "modify";
9393
break;
94+
case AccessorKind::DistributedThunk:
95+
out << "distributed_thunk";
96+
break;
9497
}
9598

9699
out << " for " << ASD->getName();

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,9 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
538538
case AccessorKind::Modify:
539539
Kind = ".modify";
540540
break;
541+
case AccessorKind::DistributedThunk:
542+
Kind = ".distributedThunk";
543+
break;
541544
}
542545

543546
SmallVector<char, 64> Buf;

lib/Index/Index.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ printArtificialName(const swift::AbstractStorageDecl *ASD, AccessorKind AK, llvm
5555
case AccessorKind::WillSet:
5656
OS << "willSet:" << ASD->getName() ;
5757
return false;
58-
5958
case AccessorKind::Address:
6059
case AccessorKind::MutableAddress:
6160
case AccessorKind::Read:
6261
case AccessorKind::Modify:
62+
case AccessorKind::DistributedThunk:
6363
return true;
6464
}
6565

lib/Index/IndexSymbol.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ SymbolSubKind index::getSubKindForAccessor(AccessorKind AK) {
268268
return SymbolSubKind::SwiftAccessorMutableAddressor;
269269
case AccessorKind::Read: return SymbolSubKind::SwiftAccessorRead;
270270
case AccessorKind::Modify: return SymbolSubKind::SwiftAccessorModify;
271+
case AccessorKind::DistributedThunk: return SymbolSubKind::AccessorGetter;
271272
}
272273

273274
llvm_unreachable("Unhandled AccessorKind in switch.");

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6217,6 +6217,8 @@ static StringRef getAccessorNameForDiagnostic(AccessorKind accessorKind,
62176217
return "'willSet'";
62186218
case AccessorKind::DidSet:
62196219
return "'didSet'";
6220+
case AccessorKind::DistributedThunk:
6221+
return "distributed thunk";
62206222
}
62216223
llvm_unreachable("bad accessor kind");
62226224
}
@@ -6253,6 +6255,7 @@ static bool isAllowedInLimitedSyntax(AccessorKind kind) {
62536255
case AccessorKind::DidSet:
62546256
case AccessorKind::Read:
62556257
case AccessorKind::Modify:
6258+
case AccessorKind::DistributedThunk:
62566259
return false;
62576260
}
62586261
llvm_unreachable("bad accessor kind");

lib/SIL/IR/SILPrinter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,9 @@ void SILDeclRef::print(raw_ostream &OS) const {
317317
case AccessorKind::Modify:
318318
OS << "!modify";
319319
break;
320+
case AccessorKind::DistributedThunk:
321+
OS << "!distributedThunk";
322+
break;
320323
}
321324
break;
322325
}

lib/SILGen/SILGenLValue.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,13 +1357,10 @@ namespace {
13571357
std::move(indices), isOnSelfParameter,
13581358
actorIso)
13591359
{
1360-
if (isDistributedAccessor) {
1361-
fprintf(stderr, "[%s:%d] (%s) DIST ACCESSOR GOOD!\n", __FILE__, __LINE__, __FUNCTION__);
1362-
decl->dump();
1363-
}
1364-
assert(getAccessorDecl()->isGetterOrSetter());
1365-
}
1366-
1360+
assert(getAccessorDecl()->isGetterOrSetter() ||
1361+
getAccessorDecl()->isDistributedThunk());
1362+
}
1363+
13671364
GetterSetterComponent(const GetterSetterComponent &copied,
13681365
SILGenFunction &SGF,
13691366
SILLocation loc)
@@ -1649,7 +1646,8 @@ namespace {
16491646

16501647
RValue get(SILGenFunction &SGF, SILLocation loc,
16511648
ManagedValue base, SGFContext c) && override {
1652-
assert(getAccessorDecl()->isGetter());
1649+
assert(getAccessorDecl()->isGetter() ||
1650+
getAccessorDecl()->isDistributedThunk());
16531651

16541652
if (IsDistributedAccessor) {
16551653
fprintf(stderr, "[%s:%d] (%s) EMIT DIST GETTER\n", __FILE__, __LINE__, __FUNCTION__);
@@ -2711,7 +2709,8 @@ namespace {
27112709
assert(!isDistributed && "setters must not be 'distributed'");
27122710
LLVM_FALLTHROUGH;
27132711
}
2714-
case AccessorKind::Get: {
2712+
case AccessorKind::Get:
2713+
case AccessorKind::DistributedThunk: {
27152714
auto typeData = getLogicalStorageTypeData(
27162715
SGF.getTypeExpansionContext(), SGF.SGM, AccessKind, FormalRValueType);
27172716
return asImpl().emitUsingGetterSetter(accessor, isDirect, isDistributed, typeData);

lib/SILOptimizer/Mandatory/DefiniteInitialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,7 @@ void LifetimeChecker::handleInOutUse(const DIMemoryUse &Use) {
15781578
case AccessorKind::Get:
15791579
case AccessorKind::Read:
15801580
case AccessorKind::Address:
1581+
case AccessorKind::DistributedThunk:
15811582
return false;
15821583
case AccessorKind::Set:
15831584
case AccessorKind::Modify:

lib/Sema/CSDiagnostics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4039,6 +4039,7 @@ bool InvalidMemberRefOnExistential::diagnoseAsError() {
40394039
case AccessorKind::Modify:
40404040
case AccessorKind::Address:
40414041
case AccessorKind::MutableAddress:
4042+
case AccessorKind::DistributedThunk:
40424043
PD = SD->getIndices()->get(idx);
40434044
break;
40444045
}

lib/Sema/CodeSynthesisDistributedActor.cpp

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

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-
}
643-
644631
// --- Prepare generic parameters
645632
GenericParamList *genericParamList = nullptr;
646633
if (auto genericParams = func->getGenericParams()) {
@@ -679,13 +666,32 @@ static FuncDecl *createDistributedThunkFunction(FuncDecl *func) {
679666
}
680667
ParameterList *params = ParameterList::create(C, paramDecls); // = funcParams->clone(C);
681668

682-
auto thunk = FuncDecl::createImplicit(
683-
C, swift::StaticSpellingKind::None, thunkName, SourceLoc(),
684-
/*async=*/true, /*throws=*/true,
685-
genericParamList, params,
686-
func->getResultInterfaceType(), DC);
669+
FuncDecl *thunk = nullptr;
670+
if (auto *accessor = dyn_cast<AccessorDecl>(func)) {
671+
auto *distributedVar = cast<VarDecl>(accessor->getStorage());
672+
673+
thunk = AccessorDecl::create(
674+
C, /*declLoc=*/accessor->getLoc(), /*accessorKeywordLoc=*/SourceLoc(),
675+
AccessorKind::DistributedThunk, distributedVar,
676+
/*staticLoc=*/SourceLoc(), StaticSpellingKind::None,
677+
/*async=*/true, /*asyncLoc=*/SourceLoc(),
678+
/*throws=*/true, /*throwsLoc=*/SourceLoc(), genericParamList, params,
679+
func->getResultInterfaceType(), accessor->getDeclContext());
680+
681+
distributedVar->setSynthesizedAccessor(AccessorKind::DistributedThunk,
682+
cast<AccessorDecl>(thunk));
683+
} else {
684+
thunk = FuncDecl::createImplicit(
685+
C, swift::StaticSpellingKind::None, func->getName(), SourceLoc(),
686+
/*async=*/true, /*throws=*/true, genericParamList, params,
687+
func->getResultInterfaceType(), DC);
688+
689+
thunk->getAttrs().add(new (C) NonisolatedAttr(/*isImplicit=*/true));
690+
}
691+
692+
assert(thunk && "couldn't create a distributed thunk");
693+
687694
thunk->setSynthesized(true);
688-
thunk->getAttrs().add(new (C) NonisolatedAttr(/*isImplicit=*/true));
689695
thunk->setGenericSignature(baseSignature);
690696
thunk->copyFormalAccessFrom(func, /*sourceIsParentContext=*/false);
691697
thunk->setBodySynthesizer(deriveBodyDistributed_thunk, func);

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4066,6 +4066,7 @@ class ObjCSelectorWalker : public ASTWalker {
40664066
case AccessorKind::MutableAddress:
40674067
case AccessorKind::Read:
40684068
case AccessorKind::Modify:
4069+
case AccessorKind::DistributedThunk:
40694070
llvm_unreachable("cannot be @objc");
40704071
}
40714072
} else {

lib/Sema/TypeCheckAttr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4188,6 +4188,8 @@ static DescriptiveDeclKind getAccessorDescriptiveDeclKind(AccessorKind kind) {
41884188
return DescriptiveDeclKind::Addressor;
41894189
case AccessorKind::MutableAddress:
41904190
return DescriptiveDeclKind::MutableAddressor;
4191+
case AccessorKind::DistributedThunk:
4192+
return DescriptiveDeclKind::DistributedThunk;
41914193
}
41924194
}
41934195

lib/Sema/TypeCheckDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,9 @@ static bool doesAccessorNeedDynamicAttribute(AccessorDecl *accessor) {
371371
storage->getWriteImpl() == WriteImplKind::StoredWithObservers)
372372
return storage->isDynamic();
373373
return false;
374+
375+
case AccessorKind::DistributedThunk:
376+
return false;
374377
}
375378
llvm_unreachable("covered switch");
376379
}
@@ -1591,6 +1594,7 @@ SelfAccessKindRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
15911594
case AccessorKind::Address:
15921595
case AccessorKind::Get:
15931596
case AccessorKind::Read:
1597+
case AccessorKind::DistributedThunk:
15941598
break;
15951599

15961600
case AccessorKind::MutableAddress:
@@ -2009,6 +2013,7 @@ ResultTypeRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
20092013
switch (accessor->getAccessorKind()) {
20102014
// For getters, set the result type to the value type.
20112015
case AccessorKind::Get:
2016+
case AccessorKind::DistributedThunk:
20122017
return storage->getValueInterfaceType();
20132018

20142019
// For setters and observers, set the old/new value parameter's type

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ bool swift::isRepresentableInObjC(
697697
.limitBehavior(behavior);
698698
Reason.describe(accessor);
699699
return false;
700+
701+
case AccessorKind::DistributedThunk:
702+
return false;
700703
}
701704
llvm_unreachable("bad kind");
702705
}
@@ -1382,6 +1385,7 @@ Optional<ObjCReason> shouldMarkAsObjC(const ValueDecl *VD, bool allowImplicit) {
13821385
case AccessorKind::Modify:
13831386
case AccessorKind::Read:
13841387
case AccessorKind::WillSet:
1388+
case AccessorKind::DistributedThunk:
13851389
return false;
13861390

13871391
case AccessorKind::MutableAddress:

lib/Sema/TypeCheckDeclOverride.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,7 @@ OverriddenDeclsRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
21962196
case AccessorKind::DidSet:
21972197
case AccessorKind::Address:
21982198
case AccessorKind::MutableAddress:
2199+
case AccessorKind::DistributedThunk:
21992200
// These accessors are never part of the opaque set. Bail out early
22002201
// to avoid computing the overridden declarations of the storage.
22012202
return noResults;

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6705,6 +6705,7 @@ swift::findWitnessedObjCRequirements(const ValueDecl *witness,
67056705
case AccessorKind::MutableAddress:
67066706
case AccessorKind::Read:
67076707
case AccessorKind::Modify:
6708+
case AccessorKind::DistributedThunk:
67086709
// These accessors are never exposed to Objective-C.
67096710
return result;
67106711
case AccessorKind::DidSet:

lib/Sema/TypeCheckStorage.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,7 @@ synthesizeAccessorBody(AbstractFunctionDecl *fn, void *) {
19751975
case AccessorKind::DidSet:
19761976
case AccessorKind::Address:
19771977
case AccessorKind::MutableAddress:
1978+
case AccessorKind::DistributedThunk:
19781979
break;
19791980
}
19801981
llvm_unreachable("bad synthesized function kind");
@@ -2501,6 +2502,7 @@ IsAccessorTransparentRequest::evaluate(Evaluator &evaluator,
25012502
case AccessorKind::DidSet:
25022503
case AccessorKind::Address:
25032504
case AccessorKind::MutableAddress:
2505+
case AccessorKind::DistributedThunk:
25042506
llvm_unreachable("bad synthesized function kind");
25052507
}
25062508

0 commit comments

Comments
 (0)