Skip to content

Commit 48424d3

Browse files
authored
Merge pull request #59563 from apple/pick-distributed-computed-properties
🍒[5.7][Distributed] Implement distributed computed properties
2 parents 2a3bfe9 + e2e111d commit 48424d3

27 files changed

+486
-153
lines changed

include/swift/AST/ASTMangler.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class ASTMangler : public Mangler {
197197
Type GlobalActorBound,
198198
ModuleDecl *Module);
199199

200-
std::string mangleDistributedThunk(const FuncDecl *thunk);
200+
std::string mangleDistributedThunk(const AbstractFunctionDecl *thunk);
201201

202202
/// Mangle a completion handler block implementation function, used for importing ObjC
203203
/// APIs as async.

include/swift/AST/Attr.def

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ SIMPLE_DECL_ATTR(_inheritActorContext, InheritActorContext,
661661
// 117 was 'spawn' and is now unused
662662

663663
CONTEXTUAL_SIMPLE_DECL_ATTR(distributed, DistributedActor,
664-
DeclModifier | OnClass | OnFunc | OnVar |
664+
DeclModifier | OnClass | OnFunc | OnAccessor | OnVar |
665665
ABIBreakingToAdd | ABIBreakingToRemove |
666666
APIBreakingToAdd | APIBreakingToRemove,
667667
118)
@@ -732,6 +732,12 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(_local, KnownToBeLocal,
732732
APIBreakingToAdd | APIBreakingToRemove,
733733
130)
734734

735+
SIMPLE_DECL_ATTR(_distributed_thunk, DistributedThunk,
736+
OnFunc | UserInaccessible |
737+
ABIBreakingToAdd | ABIBreakingToRemove |
738+
APIBreakingToAdd | APIBreakingToRemove,
739+
131)
740+
735741
// If you're adding a new underscored attribute here, please document it in
736742
// docs/ReferenceGuides/UnderscoredAttributes.md.
737743

include/swift/AST/Decl.h

Lines changed: 11 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;
@@ -6393,6 +6397,9 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
63936397
/// A function is concurrent if it has the @Sendable attribute.
63946398
bool isSendable() const;
63956399

6400+
/// Determine if function has 'nonisolated' attribute
6401+
bool isNonisolated() const;
6402+
63966403
/// Returns true if the function is a suitable 'async' context.
63976404
///
63986405
/// Functions that are an 'async' context can make calls to 'async' functions.
@@ -6412,6 +6419,10 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
64126419
/// Returns 'true' if the function is distributed.
64136420
bool isDistributed() const;
64146421

6422+
/// Is this a thunk function used to access a distributed method outside
6423+
/// of its actor isolation context?
6424+
bool isDistributedThunk() const;
6425+
64156426
/// For a 'distributed' target (func or computed property),
64166427
/// get the 'thunk' responsible for performing the 'remoteCall'.
64176428
///

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4633,6 +4633,9 @@ ERROR(distributed_actor_isolated_method,none,
46334633
ERROR(distributed_local_cannot_be_used,none,
46344634
"'local' cannot be used in user-defined code currently",
46354635
())
4636+
ERROR(distributed_thunk_cannot_be_used,none,
4637+
"'distributed_thunk' cannot be used in user-defined code",
4638+
())
46364639
ERROR(distributed_actor_func_param_not_codable,none,
46374640
"parameter '%0' of type %1 in %2 does not conform to serialization requirement '%3'",
46384641
(StringRef, Type, DescriptiveDeclKind, StringRef))
@@ -4827,6 +4830,9 @@ ERROR(distributed_property_cannot_be_static,none,
48274830
ERROR(distributed_property_can_only_be_computed,none,
48284831
"%0 %1 cannot be 'distributed', only computed properties can",
48294832
(DescriptiveDeclKind, DeclName))
4833+
ERROR(distributed_property_accessor_only_get_can_be_distributed,none,
4834+
"only 'get' accessors may be 'distributed'",
4835+
())
48304836
NOTE(distributed_actor_isolated_property,none,
48314837
"access to %0 %1 is only permitted within distributed actor %2",
48324838
(DescriptiveDeclKind, DeclName, DeclName))

include/swift/AST/Expr.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ class alignas(8) Expr : public ASTAllocated<Expr> {
319319
ImplicitlyAsync : 1,
320320
ImplicitlyThrows : 1,
321321
NoAsync : 1,
322-
ShouldApplyDistributedThunk : 1
322+
UsesDistributedThunk : 1
323323
);
324324

325325
SWIFT_INLINE_BITFIELD_EMPTY(CallExpr, ApplyExpr);
@@ -4445,7 +4445,7 @@ class ApplyExpr : public Expr {
44454445
Bits.ApplyExpr.ImplicitlyAsync = false;
44464446
Bits.ApplyExpr.ImplicitlyThrows = false;
44474447
Bits.ApplyExpr.NoAsync = false;
4448-
Bits.ApplyExpr.ShouldApplyDistributedThunk = false;
4448+
Bits.ApplyExpr.UsesDistributedThunk = false;
44494449
}
44504450

44514451
public:
@@ -4532,11 +4532,11 @@ class ApplyExpr : public Expr {
45324532

45334533
/// Informs IRGen to that this expression should be applied as its distributed
45344534
/// thunk, rather than invoking the function directly.
4535-
bool shouldApplyDistributedThunk() const {
4536-
return Bits.ApplyExpr.ShouldApplyDistributedThunk;
4535+
bool usesDistributedThunk() const {
4536+
return Bits.ApplyExpr.UsesDistributedThunk;
45374537
}
4538-
void setShouldApplyDistributedThunk(bool flag) {
4539-
Bits.ApplyExpr.ShouldApplyDistributedThunk = flag;
4538+
void setUsesDistributedThunk(bool flag) {
4539+
Bits.ApplyExpr.UsesDistributedThunk = flag;
45404540
}
45414541

45424542
ValueDecl *getCalledValue() 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/ASTDumper.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,8 @@ namespace {
742742

743743
void visitVarDecl(VarDecl *VD) {
744744
printCommon(VD, "var_decl");
745+
if (VD->isDistributed())
746+
PrintWithColorRAII(OS, DeclModifierColor) << " distributed";
745747
if (VD->isLet())
746748
PrintWithColorRAII(OS, DeclModifierColor) << " let";
747749
if (VD->getAttrs().hasAttribute<LazyAttr>())
@@ -872,6 +874,9 @@ namespace {
872874
if (D->isDistributed()) {
873875
PrintWithColorRAII(OS, ExprModifierColor) << " distributed";
874876
}
877+
if (D->isDistributedThunk()) {
878+
PrintWithColorRAII(OS, ExprModifierColor) << " distributed-thunk";
879+
}
875880

876881
if (auto fac = D->getForeignAsyncConvention()) {
877882
OS << " foreign_async=";
@@ -1335,6 +1340,10 @@ void ValueDecl::dumpRef(raw_ostream &os) const {
13351340
os << " known-to-be-local";
13361341
}
13371342

1343+
if (getAttrs().hasAttribute<DistributedThunkAttr>()) {
1344+
os << " distributed-thunk";
1345+
}
1346+
13381347
// Print location.
13391348
auto &srcMgr = getASTContext().SourceMgr;
13401349
if (getLoc().isValid()) {

lib/AST/ASTMangler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3484,7 +3484,7 @@ ASTMangler::mangleOpaqueTypeDescriptorRecord(const OpaqueTypeDecl *decl) {
34843484
return finalize();
34853485
}
34863486

3487-
std::string ASTMangler::mangleDistributedThunk(const FuncDecl *thunk) {
3487+
std::string ASTMangler::mangleDistributedThunk(const AbstractFunctionDecl *thunk) {
34883488
// Marker protocols cannot be checked at runtime, so there is no point
34893489
// in recording them for distributed thunks.
34903490
llvm::SaveAndRestore<bool> savedAllowMarkerProtocols(AllowMarkerProtocols,

lib/AST/Decl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7742,6 +7742,10 @@ bool AbstractFunctionDecl::isSendable() const {
77427742
return getAttrs().hasAttribute<SendableAttr>();
77437743
}
77447744

7745+
bool AbstractFunctionDecl::isNonisolated() const {
7746+
return getAttrs().hasAttribute<NonisolatedAttr>();
7747+
}
7748+
77457749
bool AbstractFunctionDecl::isBackDeployed() const {
77467750
if (getAttrs().hasAttribute<BackDeployAttr>())
77477751
return true;

lib/AST/DistributedDecl.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1330,6 +1330,10 @@ bool AbstractFunctionDecl::isDistributed() const {
13301330
return getAttrs().hasAttribute<DistributedActorAttr>();
13311331
}
13321332

1333+
bool AbstractFunctionDecl::isDistributedThunk() const {
1334+
return getAttrs().hasAttribute<DistributedThunkAttr>();
1335+
}
1336+
13331337
ConstructorDecl *
13341338
NominalTypeDecl::getDistributedRemoteCallTargetInitFunction() const {
13351339
auto mutableThis = const_cast<NominalTypeDecl *>(this);
@@ -1373,6 +1377,15 @@ AbstractFunctionDecl *ASTContext::getRemoteCallOnDistributedActorSystem(
13731377
/********************** Distributed Actor Properties **************************/
13741378
/******************************************************************************/
13751379

1380+
FuncDecl *VarDecl::getDistributedThunk() const {
1381+
if (!isDistributed())
1382+
return nullptr;
1383+
1384+
auto mutableThis = const_cast<VarDecl *>(this);
1385+
return evaluateOrDefault(getASTContext().evaluator,
1386+
GetDistributedThunkRequest{mutableThis}, nullptr);
1387+
}
1388+
13761389
FuncDecl*
13771390
AbstractFunctionDecl::getDistributedThunk() const {
13781391
if (!isDistributed())

lib/SILGen/SILGenApply.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,10 +1122,8 @@ class SILGenApply : public Lowering::ExprVisitor<SILGenApply> {
11221122
SILDeclRef constant = SILDeclRef(e->getDecl());
11231123

11241124
/// Some special handling may be necessary for thunks:
1125-
if (callSite && callSite->shouldApplyDistributedThunk()) {
1126-
if (auto distributedThunk = cast<AbstractFunctionDecl>(e->getDecl())->getDistributedThunk()) {
1127-
constant = SILDeclRef(distributedThunk).asDistributed();
1128-
}
1125+
if (callSite && callSite->usesDistributedThunk()) {
1126+
constant = SILDeclRef(e->getDecl()).asDistributed();
11291127
} else if (afd->isBackDeployed()) {
11301128
// If we're calling a back deployed function then we need to call a
11311129
// thunk instead that will handle the fallback when the original
@@ -5764,7 +5762,8 @@ RValue SILGenFunction::emitGetAccessor(SILLocation loc, SILDeclRef get,
57645762
ArgumentSource &&selfValue, bool isSuper,
57655763
bool isDirectUse,
57665764
PreparedArguments &&subscriptIndices,
5767-
SGFContext c, bool isOnSelfParameter) {
5765+
SGFContext c,
5766+
bool isOnSelfParameter) {
57685767
// Scope any further writeback just within this operation.
57695768
FormalEvaluationScope writebackScope(*this);
57705769

@@ -5838,8 +5837,8 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
58385837
ManagedValue SILGenFunction::emitAddressorAccessor(
58395838
SILLocation loc, SILDeclRef addressor, SubstitutionMap substitutions,
58405839
ArgumentSource &&selfValue, bool isSuper, bool isDirectUse,
5841-
PreparedArguments &&subscriptIndices, SILType addressType,
5842-
bool isOnSelfParameter) {
5840+
PreparedArguments &&subscriptIndices,
5841+
SILType addressType, bool isOnSelfParameter) {
58435842
// Scope any further writeback just within this operation.
58445843
FormalEvaluationScope writebackScope(*this);
58455844

@@ -5900,7 +5899,8 @@ SILGenFunction::emitCoroutineAccessor(SILLocation loc, SILDeclRef accessor,
59005899
Callee callee =
59015900
emitSpecializedAccessorFunctionRef(*this, loc, accessor,
59025901
substitutions, selfValue,
5903-
isSuper, isDirectUse, isOnSelfParameter);
5902+
isSuper, isDirectUse,
5903+
isOnSelfParameter);
59045904

59055905
// We're already in a full formal-evaluation scope.
59065906
// Make a dead writeback scope; applyCoroutine won't try to pop this.

lib/SILGen/SILGenDistributed.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ void SILGenFunction::emitDistActorIdentityInit(ConstructorDecl *ctor,
213213
initializeProperty(*this, loc, borrowedSelfArg, var, temp);
214214
}
215215

216+
// TODO(distributed): rename to DistributedActorID
216217
InitializeDistActorIdentity::InitializeDistActorIdentity(ConstructorDecl *ctor,
217218
ManagedValue actorSelf)
218219
: ctor(ctor),

lib/SILGen/SILGenFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1477,7 +1477,8 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
14771477
ManagedValue emitAddressorAccessor(
14781478
SILLocation loc, SILDeclRef addressor, SubstitutionMap substitutions,
14791479
ArgumentSource &&optionalSelfValue, bool isSuper,
1480-
bool isDirectAccessorUse, PreparedArguments &&optionalSubscripts,
1480+
bool isDirectAccessorUse,
1481+
PreparedArguments &&optionalSubscripts,
14811482
SILType addressType, bool isOnSelfParameter);
14821483

14831484
CleanupHandle emitCoroutineAccessor(SILLocation loc, SILDeclRef accessor,

0 commit comments

Comments
 (0)