Skip to content

Commit 06686a9

Browse files
authored
Merge pull request swiftlang#38823 from ktoso/wip-inject-lifecycle-ctors-distributed-3
2 parents b25c65e + bf37ba6 commit 06686a9

File tree

61 files changed

+1871
-1430
lines changed

Some content is hidden

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

61 files changed

+1871
-1430
lines changed

include/swift/AST/Builtins.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ BUILTIN_MISC_OPERATION(InitializeDefaultActor, "initializeDefaultActor", "", Spe
727727
/// Destroy the default-actor instance in a default actor object.
728728
BUILTIN_MISC_OPERATION(DestroyDefaultActor, "destroyDefaultActor", "", Special)
729729

730-
/// Initialize a "proxy" for a distributed remote actor.
730+
/// Allocate a "proxy" for a distributed remote actor. TODO(distributed) change the name of this to create throughout.
731731
BUILTIN_MISC_OPERATION(InitializeDistributedRemoteActor,
732732
"initializeDistributedRemoteActor", "", Special)
733733

include/swift/AST/Decl.h

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,13 @@ class alignas(1 << DeclAlignInBits) Decl {
403403
SWIFT_INLINE_BITFIELD(SubscriptDecl, VarDecl, 2,
404404
StaticSpelling : 2
405405
);
406-
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1,
406+
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+2+8+1+1+1+1+1+1,
407407
/// \see AbstractFunctionDecl::BodyKind
408408
BodyKind : 3,
409409

410+
/// \see AbstractFunctionDecl::SILSynthesizeKind
411+
SILSynthesizeKind : 2,
412+
410413
/// Import as member status.
411414
IAMStatus : 8,
412415

@@ -3367,8 +3370,6 @@ class NominalTypeDecl : public GenericTypeDecl, public IterableDeclContext {
33673370
/// for types that are not global actors.
33683371
VarDecl *getGlobalActorInstance() const;
33693372

3370-
bool hasDistributedActorLocalInitializer() const;
3371-
33723373
/// Whether this type is a global actor, which can be used as an
33733374
/// attribute to decorate declarations for inclusion in the actor-isolated
33743375
/// state denoted by this type.
@@ -5769,6 +5770,15 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
57695770
friend class NeedsNewVTableEntryRequest;
57705771

57715772
public:
5773+
/// records the kind of SILGen-synthesized body this decl represents
5774+
enum class SILSynthesizeKind {
5775+
None,
5776+
MemberwiseInitializer,
5777+
DistributedActorFactory
5778+
5779+
// This enum currently needs to fit in a 2-bit bitfield.
5780+
};
5781+
57725782
enum class BodyKind {
57735783
/// The function did not have a body in the source code file.
57745784
None,
@@ -5788,8 +5798,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
57885798
/// Function body is present and type-checked.
57895799
TypeChecked,
57905800

5791-
/// This is a memberwise initializer that will be synthesized by SILGen.
5792-
MemberwiseInitializer,
5801+
// Function body will be synthesized by SILGen.
5802+
SILSynthesize,
57935803

57945804
/// Function body text was deserialized from a .swiftmodule.
57955805
Deserialized
@@ -5889,6 +5899,14 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
58895899
Bits.AbstractFunctionDecl.BodyKind = unsigned(K);
58905900
}
58915901

5902+
void setSILSynthesizeKind(SILSynthesizeKind K) {
5903+
Bits.AbstractFunctionDecl.SILSynthesizeKind = unsigned(K);
5904+
}
5905+
5906+
SILSynthesizeKind getSILSynthesizeKind() const {
5907+
return SILSynthesizeKind(Bits.AbstractFunctionDecl.SILSynthesizeKind);
5908+
}
5909+
58925910
public:
58935911
void setHasSingleExpressionBody(bool Has = true) {
58945912
Bits.AbstractFunctionDecl.HasSingleExpressionBody = Has;
@@ -6065,7 +6083,17 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
60656083
void setIsMemberwiseInitializer() {
60666084
assert(getBodyKind() == BodyKind::None);
60676085
assert(isa<ConstructorDecl>(this));
6068-
setBodyKind(BodyKind::MemberwiseInitializer);
6086+
setBodyKind(BodyKind::SILSynthesize);
6087+
setSILSynthesizeKind(SILSynthesizeKind::MemberwiseInitializer);
6088+
}
6089+
6090+
/// Mark that the body should be filled in to be a factory method for creating
6091+
/// a distributed actor.
6092+
void setDistributedActorFactory() {
6093+
assert(getBodyKind() == BodyKind::None);
6094+
assert(isa<FuncDecl>(this));
6095+
setBodyKind(BodyKind::SILSynthesize);
6096+
setSILSynthesizeKind(SILSynthesizeKind::DistributedActorFactory);
60696097
}
60706098

60716099
/// Gets the body of this function, stripping the unused portions of #if
@@ -6089,7 +6117,16 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
60896117
}
60906118

60916119
bool isMemberwiseInitializer() const {
6092-
return getBodyKind() == BodyKind::MemberwiseInitializer;
6120+
return getBodyKind() == BodyKind::SILSynthesize
6121+
&& getSILSynthesizeKind() == SILSynthesizeKind::MemberwiseInitializer;
6122+
}
6123+
6124+
/// Determines whether this function represents a distributed actor
6125+
/// initialization factory. Such functions do not have a body that is
6126+
/// representable in the AST, so it must be synthesized during SILGen.
6127+
bool isDistributedActorFactory() const {
6128+
return getBodyKind() == BodyKind::SILSynthesize
6129+
&& getSILSynthesizeKind() == SILSynthesizeKind::DistributedActorFactory;
60936130
}
60946131

60956132
/// For a method of a class, checks whether it will require a new entry in the
@@ -6999,18 +7036,6 @@ class ConstructorDecl : public AbstractFunctionDecl {
69997036
/// \endcode
70007037
bool isObjCZeroParameterWithLongSelector() const;
70017038

7002-
/// Checks if the initializer is a distributed actor's 'local' initializer:
7003-
/// ```
7004-
/// init(transport: ActorTransport)
7005-
/// ```
7006-
bool isDistributedActorLocalInit() const;
7007-
7008-
/// Checks if the initializer is a distributed actor's 'resolve' initializer:
7009-
/// ```
7010-
/// init(resolve address: ActorAddress, using transport: ActorTransport)
7011-
/// ```
7012-
bool isDistributedActorResolveInit() const;
7013-
70147039
static bool classof(const Decl *D) {
70157040
return D->getKind() == DeclKind::Constructor;
70167041
}

include/swift/AST/DiagnosticsSema.def

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4558,27 +4558,6 @@ ERROR(actor_instance_property_wrapper,none,
45584558
ERROR(distributed_actor_func_defined_outside_of_distributed_actor,none,
45594559
"distributed function %0 is declared outside of an distributed actor",
45604560
(DeclName))
4561-
ERROR(distributed_actor_local_init_explicitly_defined,none,
4562-
"'distributed actor' local-initializer 'init(transport:)' "
4563-
"cannot be implemented explicitly.",
4564-
())
4565-
ERROR(distributed_actor_init_resolve_must_not_be_user_defined,none,
4566-
"'distributed actor' resolve-initializer 'init(resolve:using:)' "
4567-
"cannot be implemented explicitly.",
4568-
())
4569-
ERROR(distributed_actor_init_user_defined_must_be_convenience,none,
4570-
"'distributed actor' initializer %0 must be 'convenience' initializer. "
4571-
"Distributed actors have an implicitly synthesized designated "
4572-
"'init(transport:)' local-initializer, which other initializers must delegate to",
4573-
(DeclName))
4574-
ERROR(distributed_actor_init_must_delegate_to_local_init,none,
4575-
"'distributed actor' initializer %0 must (directly or indirectly) delegate "
4576-
"to 'init(transport:)'",
4577-
(DeclName))
4578-
ERROR(distributed_actor_init_must_not_delegate_to_resolve_init,none,
4579-
"'distributed actor' initializer %0 cannot delegate to resolve-initializer "
4580-
"'init(resolve:using:)', as it may result resolving a storageless proxy instance",
4581-
(DeclName))
45824561
ERROR(distributed_actor_local_var,none,
45834562
"'distributed' can not be applied to local variables",
45844563
())
@@ -4602,6 +4581,18 @@ ERROR(distributed_actor_func_static,none,
46024581
ERROR(distributed_actor_func_not_in_distributed_actor,none,
46034582
"'distributed' function can only be declared within 'distributed actor'",
46044583
())
4584+
ERROR(distributed_actor_designated_ctor_must_have_one_transport_param,none,
4585+
"designated distributed actor initializer %0 must accept exactly one "
4586+
"ActorTransport parameter, found %1",
4587+
(DeclName, int))
4588+
ERROR(distributed_actor_designated_ctor_missing_transport_param,none,
4589+
"designated distributed actor initializer %0 is missing required "
4590+
"ActorTransport parameter",
4591+
(DeclName))
4592+
ERROR(distributed_actor_user_defined_special_property,none,
4593+
"property %0 cannot be defined explicitly, as it conflicts with "
4594+
"distributed actor synthesized stored property",
4595+
(DeclName))
46054596
ERROR(distributed_actor_independent_property_must_be_let,none,
46064597
"_distributedActorIndependent can be applied to properties, however they must be 'let'",
46074598
())

include/swift/AST/KnownIdentifiers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ IDENTIFIER(assignIdentity)
258258
IDENTIFIER(resignIdentity)
259259
IDENTIFIER(resolve)
260260
IDENTIFIER(id)
261+
IDENTIFIER(identity)
261262
IDENTIFIER(identifier)
262263
IDENTIFIER(_distributedActorRemoteInitialize)
263264
IDENTIFIER(_distributedActorDestroy)

include/swift/AST/KnownProtocols.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ PROTOCOL(Differentiable)
9696

9797
// Distributed Actors
9898
PROTOCOL(DistributedActor)
99+
PROTOCOL(ActorIdentity)
99100
PROTOCOL(ActorTransport)
100101

101102
PROTOCOL(AsyncSequence)

include/swift/AST/TypeCheckRequests.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,24 +1955,6 @@ class HasDefaultInitRequest
19551955
bool isCached() const { return true; }
19561956
};
19571957

1958-
/// Checks whether this type has a distributed actor "local" initializer.
1959-
class HasDistributedActorLocalInitRequest
1960-
: public SimpleRequest<HasDistributedActorLocalInitRequest, bool(NominalTypeDecl *),
1961-
RequestFlags::Cached> {
1962-
public:
1963-
using SimpleRequest::SimpleRequest;
1964-
1965-
private:
1966-
friend SimpleRequest;
1967-
1968-
// Evaluation.
1969-
bool evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;
1970-
1971-
public:
1972-
// Caching.
1973-
bool isCached() const { return true; }
1974-
};
1975-
19761958
/// Synthesizes a default initializer for a given type.
19771959
class SynthesizeDefaultInitRequest
19781960
: public SimpleRequest<SynthesizeDefaultInitRequest,
@@ -2065,6 +2047,7 @@ enum class ImplicitMemberAction : uint8_t {
20652047
ResolveDecodable,
20662048
ResolveDistributedActor,
20672049
ResolveDistributedActorIdentity,
2050+
ResolveDistributedActorTransport,
20682051
};
20692052

20702053
class ResolveImplicitMemberRequest
@@ -2249,6 +2232,24 @@ class HasCircularRawValueRequest
22492232
bool isCached() const { return true; }
22502233
};
22512234

2235+
/// Checks if the _Distributed module is available.
2236+
class DistributedModuleIsAvailableRequest
2237+
: public SimpleRequest<DistributedModuleIsAvailableRequest, bool(Decl *),
2238+
RequestFlags::Cached> {
2239+
public:
2240+
using SimpleRequest::SimpleRequest;
2241+
2242+
private:
2243+
friend SimpleRequest;
2244+
2245+
// Evaluation.
2246+
bool evaluate(Evaluator &evaluator, Decl *decl) const;
2247+
2248+
public:
2249+
// Cached.
2250+
bool isCached() const { return true; }
2251+
};
2252+
22522253
/// Computes an initializer context for a parameter with a default argument.
22532254
class DefaultArgumentInitContextRequest
22542255
: public SimpleRequest<DefaultArgumentInitContextRequest,

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ SWIFT_REQUEST(TypeChecker, IsDefaultActorRequest,
100100
Cached, NoLocationInfo)
101101
SWIFT_REQUEST(TypeChecker, IsDistributedActorRequest, bool(NominalTypeDecl *),
102102
Cached, NoLocationInfo)
103-
SWIFT_REQUEST(TypeChecker, HasDistributedActorLocalInitRequest,
104-
bool(NominalTypeDecl *), Cached, NoLocationInfo)
105103
SWIFT_REQUEST(TypeChecker, IsDistributedFuncRequest, bool(FuncDecl *),
106104
Cached, NoLocationInfo)
107105
SWIFT_REQUEST(TypeChecker, GlobalActorInstanceRequest,
@@ -140,6 +138,8 @@ SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
140138
SmallVector<Requirement, 2>,
141139
SmallVector<TypeLoc, 2>, bool),
142140
Cached, NoLocationInfo)
141+
SWIFT_REQUEST(TypeChecker, DistributedModuleIsAvailableRequest,
142+
bool(ModuleDecl *), Cached, NoLocationInfo)
143143
SWIFT_REQUEST(TypeChecker, InheritedTypeRequest,
144144
Type(llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>,
145145
unsigned, TypeResolutionStage),

include/swift/Runtime/Concurrency.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -716,18 +716,9 @@ SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
716716
void swift_defaultActor_deallocateResilient(HeapObject *actor);
717717

718718
/// Initialize the runtime storage for a distributed remote actor.
719-
// TODO: this may end up being removed as we move to the "proxy creation" below
720719
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
721-
void swift_distributedActor_remote_initialize(DefaultActor *actor);
722-
723-
/// Create a proxy object that will serve as remote distributed actor instance.
724-
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
725-
OpaqueValue* swift_distributedActor_remote_create(
726-
/* +1 */OpaqueValue *identity,
727-
/* +1 */OpaqueValue *transport
728-
// metadata for identity
729-
// metadata for transport
730-
);
720+
OpaqueValue*
721+
swift_distributedActor_remote_initialize(const Metadata *actorType);
731722

732723
/// Destroy the runtime storage for a default actor.
733724
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1687,22 +1687,14 @@ FUNCTION(DefaultActorDeallocateResilient,
16871687
ARGS(RefCountedPtrTy),
16881688
ATTRS(NoUnwind))
16891689

1690-
// void swift_distributedActor_remote_initialize(DefaultActor *actor);
1690+
// OpaqueValue* swift_distributedActor_remote_initialize(
1691+
// const Metadata *actorType
1692+
// );
16911693
FUNCTION(DistributedActorInitializeRemote,
16921694
swift_distributedActor_remote_initialize, SwiftCC,
16931695
ConcurrencyAvailability,
1694-
RETURNS(VoidTy),
1695-
ARGS(RefCountedPtrTy), // TODO also address and transport?
1696-
ATTRS(NoUnwind))
1697-
1698-
// OpaqueValue* swift_distributedActor_remote_create(
1699-
// OpaqueValue *identity,
1700-
// OpaqueValue *transport);
1701-
FUNCTION(DistributedActorRemoteCreate,
1702-
swift_distributedActor_remote_create, SwiftCC,
1703-
ConcurrencyAvailability,
1704-
RETURNS(OpaquePtrTy), // TODO: is this the right type here?
1705-
ARGS(OpaquePtrTy, OpaquePtrTy),
1696+
RETURNS(OpaquePtrTy),
1697+
ARGS(TypeMetadataPtrTy),
17061698
ATTRS(NoUnwind))
17071699

17081700
// void swift_distributedActor_destroy(DefaultActor *actor); // TODO: ProxyActor *proxy?

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,8 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
10401040
M = getLoadedModule(Id_Concurrency);
10411041
break;
10421042
case KnownProtocolKind::DistributedActor:
1043+
case KnownProtocolKind::ActorTransport:
1044+
case KnownProtocolKind::ActorIdentity:
10431045
M = getLoadedModule(Id_Distributed);
10441046
break;
10451047
default:

lib/AST/ASTVerifier.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ class Verifier : public ASTWalker {
475475
case AbstractFunctionDecl::BodyKind::None:
476476
case AbstractFunctionDecl::BodyKind::TypeChecked:
477477
case AbstractFunctionDecl::BodyKind::Skipped:
478-
case AbstractFunctionDecl::BodyKind::MemberwiseInitializer:
478+
case AbstractFunctionDecl::BodyKind::SILSynthesize:
479479
case AbstractFunctionDecl::BodyKind::Deserialized:
480480
return true;
481481

lib/AST/Builtins.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,10 +1462,18 @@ static ValueDecl *getDefaultActorInitDestroy(ASTContext &ctx,
14621462
_void);
14631463
}
14641464

1465-
static ValueDecl *getDistributedActorInitDestroy(ASTContext &ctx,
1465+
static ValueDecl *getDistributedActorInitializeRemote(ASTContext &ctx,
1466+
Identifier id) {
1467+
return getBuiltinFunction(ctx, id, _thin,
1468+
_generics(_unrestricted), // TODO(distributed): restrict to DistributedActor
1469+
_parameters(_metatype(_typeparam(0))),
1470+
_rawPointer);
1471+
}
1472+
1473+
static ValueDecl *getDistributedActorDestroy(ASTContext &ctx,
14661474
Identifier id) {
14671475
return getBuiltinFunction(ctx, id, _thin,
1468-
_parameters(_nativeObject), // TODO: no idea if to pass more here?
1476+
_parameters(_nativeObject),
14691477
_void);
14701478
}
14711479

@@ -2808,8 +2816,10 @@ ValueDecl *swift::getBuiltinValueDecl(ASTContext &Context, Identifier Id) {
28082816
return getDefaultActorInitDestroy(Context, Id);
28092817

28102818
case BuiltinValueKind::InitializeDistributedRemoteActor:
2819+
return getDistributedActorInitializeRemote(Context, Id);
2820+
28112821
case BuiltinValueKind::DestroyDistributedActor:
2812-
return getDistributedActorInitDestroy(Context, Id);
2822+
return getDistributedActorDestroy(Context, Id);
28132823

28142824
case BuiltinValueKind::StartAsyncLet:
28152825
case BuiltinValueKind::StartAsyncLetWithLocalBuffer:

0 commit comments

Comments
 (0)