Skip to content

Commit 8125a85

Browse files
authored
Merge pull request #59481 from xedin/distributed-computed-properties
[TypeChecker] Implement distributed computed properties
2 parents 728971c + fa2e64c commit 8125a85

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
@@ -664,7 +664,7 @@ SIMPLE_DECL_ATTR(_inheritActorContext, InheritActorContext,
664664
// 117 was 'spawn' and is now unused
665665

666666
CONTEXTUAL_SIMPLE_DECL_ATTR(distributed, DistributedActor,
667-
DeclModifier | OnClass | OnFunc | OnVar |
667+
DeclModifier | OnClass | OnFunc | OnAccessor | OnVar |
668668
ABIBreakingToAdd | ABIBreakingToRemove |
669669
APIBreakingToAdd | APIBreakingToRemove,
670670
118)
@@ -735,6 +735,12 @@ CONTEXTUAL_SIMPLE_DECL_ATTR(_local, KnownToBeLocal,
735735
APIBreakingToAdd | APIBreakingToRemove,
736736
130)
737737

738+
SIMPLE_DECL_ATTR(_distributed_thunk, DistributedThunk,
739+
OnFunc | UserInaccessible |
740+
ABIBreakingToAdd | ABIBreakingToRemove |
741+
APIBreakingToAdd | APIBreakingToRemove,
742+
131)
743+
738744
// If you're adding a new underscored attribute here, please document it in
739745
// docs/ReferenceGuides/UnderscoredAttributes.md.
740746

include/swift/AST/Decl.h

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

6417+
/// Determine if function has 'nonisolated' attribute
6418+
bool isNonisolated() const;
6419+
64136420
/// Returns true if the function is a suitable 'async' context.
64146421
///
64156422
/// Functions that are an 'async' context can make calls to 'async' functions.
@@ -6429,6 +6436,10 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
64296436
/// Returns 'true' if the function is distributed.
64306437
bool isDistributed() const;
64316438

6439+
/// Is this a thunk function used to access a distributed method outside
6440+
/// of its actor isolation context?
6441+
bool isDistributedThunk() const;
6442+
64326443
/// For a 'distributed' target (func or computed property),
64336444
/// get the 'thunk' responsible for performing the 'remoteCall'.
64346445
///

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4604,6 +4604,9 @@ ERROR(distributed_actor_isolated_method,none,
46044604
ERROR(distributed_local_cannot_be_used,none,
46054605
"'local' cannot be used in user-defined code currently",
46064606
())
4607+
ERROR(distributed_thunk_cannot_be_used,none,
4608+
"'distributed_thunk' cannot be used in user-defined code",
4609+
())
46074610
ERROR(distributed_actor_func_param_not_codable,none,
46084611
"parameter '%0' of type %1 in %2 does not conform to serialization requirement '%3'",
46094612
(StringRef, Type, DescriptiveDeclKind, StringRef))
@@ -4801,6 +4804,9 @@ ERROR(distributed_property_cannot_be_static,none,
48014804
ERROR(distributed_property_can_only_be_computed,none,
48024805
"%0 %1 cannot be 'distributed', only computed properties can",
48034806
(DescriptiveDeclKind, DeclName))
4807+
ERROR(distributed_property_accessor_only_get_can_be_distributed,none,
4808+
"only 'get' accessors may be 'distributed'",
4809+
())
48044810
NOTE(distributed_actor_isolated_property,none,
48054811
"access to %0 %1 is only permitted within distributed actor %2",
48064812
(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
@@ -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/ASTDumper.cpp

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

746746
void visitVarDecl(VarDecl *VD) {
747747
printCommon(VD, "var_decl");
748+
if (VD->isDistributed())
749+
PrintWithColorRAII(OS, DeclModifierColor) << " distributed";
748750
if (VD->isLet())
749751
PrintWithColorRAII(OS, DeclModifierColor) << " let";
750752
if (VD->getAttrs().hasAttribute<LazyAttr>())
@@ -875,6 +877,9 @@ namespace {
875877
if (D->isDistributed()) {
876878
PrintWithColorRAII(OS, ExprModifierColor) << " distributed";
877879
}
880+
if (D->isDistributedThunk()) {
881+
PrintWithColorRAII(OS, ExprModifierColor) << " distributed-thunk";
882+
}
878883

879884
if (auto fac = D->getForeignAsyncConvention()) {
880885
OS << " foreign_async=";
@@ -1348,6 +1353,10 @@ void ValueDecl::dumpRef(raw_ostream &os) const {
13481353
os << " known-to-be-local";
13491354
}
13501355

1356+
if (getAttrs().hasAttribute<DistributedThunkAttr>()) {
1357+
os << " distributed-thunk";
1358+
}
1359+
13511360
// Print location.
13521361
auto &srcMgr = getASTContext().SourceMgr;
13531362
if (getLoc().isValid()) {

lib/AST/ASTMangler.cpp

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

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

lib/AST/Decl.cpp

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

7784+
bool AbstractFunctionDecl::isNonisolated() const {
7785+
return getAttrs().hasAttribute<NonisolatedAttr>();
7786+
}
7787+
77847788
bool AbstractFunctionDecl::isBackDeployed() const {
77857789
if (getAttrs().hasAttribute<BackDeployAttr>())
77867790
return true;

lib/AST/DistributedDecl.cpp

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

1293+
bool AbstractFunctionDecl::isDistributedThunk() const {
1294+
return getAttrs().hasAttribute<DistributedThunkAttr>();
1295+
}
1296+
12931297
ConstructorDecl *
12941298
NominalTypeDecl::getDistributedRemoteCallTargetInitFunction() const {
12951299
auto mutableThis = const_cast<NominalTypeDecl *>(this);
@@ -1333,6 +1337,15 @@ AbstractFunctionDecl *ASTContext::getRemoteCallOnDistributedActorSystem(
13331337
/********************** Distributed Actor Properties **************************/
13341338
/******************************************************************************/
13351339

1340+
FuncDecl *VarDecl::getDistributedThunk() const {
1341+
if (!isDistributed())
1342+
return nullptr;
1343+
1344+
auto mutableThis = const_cast<VarDecl *>(this);
1345+
return evaluateOrDefault(getASTContext().evaluator,
1346+
GetDistributedThunkRequest{mutableThis}, nullptr);
1347+
}
1348+
13361349
FuncDecl*
13371350
AbstractFunctionDecl::getDistributedThunk() const {
13381351
if (!isDistributed())

lib/SILGen/SILGenApply.cpp

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

11271127
/// Some special handling may be necessary for thunks:
1128-
if (callSite && callSite->shouldApplyDistributedThunk()) {
1129-
if (auto distributedThunk = cast<AbstractFunctionDecl>(e->getDecl())->getDistributedThunk()) {
1130-
constant = SILDeclRef(distributedThunk).asDistributed();
1131-
}
1128+
if (callSite && callSite->usesDistributedThunk()) {
1129+
constant = SILDeclRef(e->getDecl()).asDistributed();
11321130
} else if (afd->isBackDeployed()) {
11331131
// If we're calling a back deployed function then we need to call a
11341132
// thunk instead that will handle the fallback when the original
@@ -5773,7 +5771,8 @@ RValue SILGenFunction::emitGetAccessor(SILLocation loc, SILDeclRef get,
57735771
ArgumentSource &&selfValue, bool isSuper,
57745772
bool isDirectUse,
57755773
PreparedArguments &&subscriptIndices,
5776-
SGFContext c, bool isOnSelfParameter) {
5774+
SGFContext c,
5775+
bool isOnSelfParameter) {
57775776
// Scope any further writeback just within this operation.
57785777
FormalEvaluationScope writebackScope(*this);
57795778

@@ -5847,8 +5846,8 @@ void SILGenFunction::emitSetAccessor(SILLocation loc, SILDeclRef set,
58475846
ManagedValue SILGenFunction::emitAddressorAccessor(
58485847
SILLocation loc, SILDeclRef addressor, SubstitutionMap substitutions,
58495848
ArgumentSource &&selfValue, bool isSuper, bool isDirectUse,
5850-
PreparedArguments &&subscriptIndices, SILType addressType,
5851-
bool isOnSelfParameter) {
5849+
PreparedArguments &&subscriptIndices,
5850+
SILType addressType, bool isOnSelfParameter) {
58525851
// Scope any further writeback just within this operation.
58535852
FormalEvaluationScope writebackScope(*this);
58545853

@@ -5909,7 +5908,8 @@ SILGenFunction::emitCoroutineAccessor(SILLocation loc, SILDeclRef accessor,
59095908
Callee callee =
59105909
emitSpecializedAccessorFunctionRef(*this, loc, accessor,
59115910
substitutions, selfValue,
5912-
isSuper, isDirectUse, isOnSelfParameter);
5911+
isSuper, isDirectUse,
5912+
isOnSelfParameter);
59135913

59145914
// We're already in a full formal-evaluation scope.
59155915
// 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
@@ -212,6 +212,7 @@ void SILGenFunction::emitDistActorIdentityInit(ConstructorDecl *ctor,
212212
initializeProperty(*this, loc, borrowedSelfArg, var, temp);
213213
}
214214

215+
// TODO(distributed): rename to DistributedActorID
215216
InitializeDistActorIdentity::InitializeDistActorIdentity(ConstructorDecl *ctor,
216217
ManagedValue actorSelf)
217218
: 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)