Skip to content

[Distributed] Complete remote actor resolve() implementation in SIL #38823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
d1e1638
[Distributed] SILGenDDistributed initializers
ktoso Aug 2, 2021
4f2c2f0
[Distributed] move emitDistributedThunk to SILGenDDistributed
ktoso Aug 2, 2021
e1dcd77
[Distributed] Implement assigning address in SIL
ktoso Aug 2, 2021
1f33a0c
[Distributed] move where we invoke dist synthesis; detect transport p…
ktoso Aug 6, 2021
2ef8421
[Distributed] move dist. init synthesis to default synthesis
ktoso Aug 7, 2021
d5b7bb3
[Distributed] diagnose missing module
ktoso Aug 10, 2021
c9bfccb
[distributed] a number of ctor infra cleanups
kavon Aug 3, 2021
89629fe
Generalize BodyKind::MemberwiseInitializer
kavon Aug 4, 2021
c49f7d0
[distributed] synthesize factory resolve func
kavon Aug 3, 2021
d4c7194
[distributed] working on runtime allocation function
kavon Aug 4, 2021
a965abd
[Distributed] AnyActor must be Sendable
ktoso Aug 10, 2021
7e0a3eb
[Distributed] Implementing calling transport in resolve and assigning…
ktoso Aug 10, 2021
ac6bee4
[Distributed] SIL invocation of initialize remote done
ktoso Aug 11, 2021
0c3720b
[Distributed] introduce GenDistributed.h/cpp for code organization
ktoso Aug 11, 2021
aaf8137
[Distributed] work in progress SIL actorReady call
ktoso Aug 11, 2021
f1a0665
[Distributed] move SILGen for destructor resignIdentity to
ktoso Aug 11, 2021
a99e935
[Distributed] implemented storing transport in resolve
ktoso Aug 11, 2021
9dcd6e4
[Distributed] implement more deinit tests
ktoso Aug 11, 2021
879dc37
[Distributed] Add SIL test for deinit calling resignIdentity
ktoso Aug 12, 2021
829a5d6
[Distributed] Review followup and cleanups
ktoso Aug 12, 2021
ad346ae
[Distributed] re-disable distributed/runtime tests to land initial work
ktoso Aug 12, 2021
3c76419
[Distributed] Remove AST default init synthesisi replaced by Implicit…
ktoso Aug 12, 2021
b4e8bbb
[Distributed] Remove deinit AST synthesis, now handled in SILGenDestr…
ktoso Aug 12, 2021
0ec4251
[Distributed] remove distributed aware emitClassMemberDestruction for…
ktoso Aug 12, 2021
bf37ba6
[Distributed] Don't duplicate hasUserDefinedInit entry, less guards
ktoso Aug 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Builtins.def
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ BUILTIN_MISC_OPERATION(InitializeDefaultActor, "initializeDefaultActor", "", Spe
/// Destroy the default-actor instance in a default actor object.
BUILTIN_MISC_OPERATION(DestroyDefaultActor, "destroyDefaultActor", "", Special)

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

Expand Down
63 changes: 44 additions & 19 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,10 +403,13 @@ class alignas(1 << DeclAlignInBits) Decl {
SWIFT_INLINE_BITFIELD(SubscriptDecl, VarDecl, 2,
StaticSpelling : 2
);
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+8+1+1+1+1+1+1,
SWIFT_INLINE_BITFIELD(AbstractFunctionDecl, ValueDecl, 3+2+8+1+1+1+1+1+1,
/// \see AbstractFunctionDecl::BodyKind
BodyKind : 3,

/// \see AbstractFunctionDecl::SILSynthesizeKind
SILSynthesizeKind : 2,

/// Import as member status.
IAMStatus : 8,

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

bool hasDistributedActorLocalInitializer() const;

/// Whether this type is a global actor, which can be used as an
/// attribute to decorate declarations for inclusion in the actor-isolated
/// state denoted by this type.
Expand Down Expand Up @@ -5769,6 +5770,15 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
friend class NeedsNewVTableEntryRequest;

public:
/// records the kind of SILGen-synthesized body this decl represents
enum class SILSynthesizeKind {
None,
MemberwiseInitializer,
DistributedActorFactory

// This enum currently needs to fit in a 2-bit bitfield.
};

enum class BodyKind {
/// The function did not have a body in the source code file.
None,
Expand All @@ -5788,8 +5798,8 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
/// Function body is present and type-checked.
TypeChecked,

/// This is a memberwise initializer that will be synthesized by SILGen.
MemberwiseInitializer,
// Function body will be synthesized by SILGen.
SILSynthesize,

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

void setSILSynthesizeKind(SILSynthesizeKind K) {
Bits.AbstractFunctionDecl.SILSynthesizeKind = unsigned(K);
}

SILSynthesizeKind getSILSynthesizeKind() const {
return SILSynthesizeKind(Bits.AbstractFunctionDecl.SILSynthesizeKind);
}

public:
void setHasSingleExpressionBody(bool Has = true) {
Bits.AbstractFunctionDecl.HasSingleExpressionBody = Has;
Expand Down Expand Up @@ -6065,7 +6083,17 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
void setIsMemberwiseInitializer() {
assert(getBodyKind() == BodyKind::None);
assert(isa<ConstructorDecl>(this));
setBodyKind(BodyKind::MemberwiseInitializer);
setBodyKind(BodyKind::SILSynthesize);
setSILSynthesizeKind(SILSynthesizeKind::MemberwiseInitializer);
}

/// Mark that the body should be filled in to be a factory method for creating
/// a distributed actor.
void setDistributedActorFactory() {
assert(getBodyKind() == BodyKind::None);
assert(isa<FuncDecl>(this));
setBodyKind(BodyKind::SILSynthesize);
setSILSynthesizeKind(SILSynthesizeKind::DistributedActorFactory);
}

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

bool isMemberwiseInitializer() const {
return getBodyKind() == BodyKind::MemberwiseInitializer;
return getBodyKind() == BodyKind::SILSynthesize
&& getSILSynthesizeKind() == SILSynthesizeKind::MemberwiseInitializer;
}

/// Determines whether this function represents a distributed actor
/// initialization factory. Such functions do not have a body that is
/// representable in the AST, so it must be synthesized during SILGen.
bool isDistributedActorFactory() const {
return getBodyKind() == BodyKind::SILSynthesize
&& getSILSynthesizeKind() == SILSynthesizeKind::DistributedActorFactory;
}

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

/// Checks if the initializer is a distributed actor's 'local' initializer:
/// ```
/// init(transport: ActorTransport)
/// ```
bool isDistributedActorLocalInit() const;

/// Checks if the initializer is a distributed actor's 'resolve' initializer:
/// ```
/// init(resolve address: ActorAddress, using transport: ActorTransport)
/// ```
bool isDistributedActorResolveInit() const;

static bool classof(const Decl *D) {
return D->getKind() == DeclKind::Constructor;
}
Expand Down
33 changes: 12 additions & 21 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4558,27 +4558,6 @@ ERROR(actor_instance_property_wrapper,none,
ERROR(distributed_actor_func_defined_outside_of_distributed_actor,none,
"distributed function %0 is declared outside of an distributed actor",
(DeclName))
ERROR(distributed_actor_local_init_explicitly_defined,none,
"'distributed actor' local-initializer 'init(transport:)' "
"cannot be implemented explicitly.",
())
ERROR(distributed_actor_init_resolve_must_not_be_user_defined,none,
"'distributed actor' resolve-initializer 'init(resolve:using:)' "
"cannot be implemented explicitly.",
())
ERROR(distributed_actor_init_user_defined_must_be_convenience,none,
"'distributed actor' initializer %0 must be 'convenience' initializer. "
"Distributed actors have an implicitly synthesized designated "
"'init(transport:)' local-initializer, which other initializers must delegate to",
(DeclName))
ERROR(distributed_actor_init_must_delegate_to_local_init,none,
"'distributed actor' initializer %0 must (directly or indirectly) delegate "
"to 'init(transport:)'",
(DeclName))
ERROR(distributed_actor_init_must_not_delegate_to_resolve_init,none,
"'distributed actor' initializer %0 cannot delegate to resolve-initializer "
"'init(resolve:using:)', as it may result resolving a storageless proxy instance",
(DeclName))
ERROR(distributed_actor_local_var,none,
"'distributed' can not be applied to local variables",
())
Expand All @@ -4602,6 +4581,18 @@ ERROR(distributed_actor_func_static,none,
ERROR(distributed_actor_func_not_in_distributed_actor,none,
"'distributed' function can only be declared within 'distributed actor'",
())
ERROR(distributed_actor_designated_ctor_must_have_one_transport_param,none,
"designated distributed actor initializer %0 must accept exactly one "
"ActorTransport parameter, found %1",
(DeclName, int))
ERROR(distributed_actor_designated_ctor_missing_transport_param,none,
"designated distributed actor initializer %0 is missing required "
"ActorTransport parameter",
(DeclName))
ERROR(distributed_actor_user_defined_special_property,none,
"property %0 cannot be defined explicitly, as it conflicts with "
"distributed actor synthesized stored property",
(DeclName))
ERROR(distributed_actor_independent_property_must_be_let,none,
"_distributedActorIndependent can be applied to properties, however they must be 'let'",
())
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ IDENTIFIER(assignIdentity)
IDENTIFIER(resignIdentity)
IDENTIFIER(resolve)
IDENTIFIER(id)
IDENTIFIER(identity)
IDENTIFIER(identifier)
IDENTIFIER(_distributedActorRemoteInitialize)
IDENTIFIER(_distributedActorDestroy)
Expand Down
1 change: 1 addition & 0 deletions include/swift/AST/KnownProtocols.def
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ PROTOCOL(Differentiable)

// Distributed Actors
PROTOCOL(DistributedActor)
PROTOCOL(ActorIdentity)
PROTOCOL(ActorTransport)

PROTOCOL(AsyncSequence)
Expand Down
37 changes: 19 additions & 18 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -1955,24 +1955,6 @@ class HasDefaultInitRequest
bool isCached() const { return true; }
};

/// Checks whether this type has a distributed actor "local" initializer.
class HasDistributedActorLocalInitRequest
: public SimpleRequest<HasDistributedActorLocalInitRequest, bool(NominalTypeDecl *),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
bool evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;

public:
// Caching.
bool isCached() const { return true; }
};

/// Synthesizes a default initializer for a given type.
class SynthesizeDefaultInitRequest
: public SimpleRequest<SynthesizeDefaultInitRequest,
Expand Down Expand Up @@ -2065,6 +2047,7 @@ enum class ImplicitMemberAction : uint8_t {
ResolveDecodable,
ResolveDistributedActor,
ResolveDistributedActorIdentity,
ResolveDistributedActorTransport,
};

class ResolveImplicitMemberRequest
Expand Down Expand Up @@ -2249,6 +2232,24 @@ class HasCircularRawValueRequest
bool isCached() const { return true; }
};

/// Checks if the _Distributed module is available.
class DistributedModuleIsAvailableRequest
: public SimpleRequest<DistributedModuleIsAvailableRequest, bool(Decl *),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
bool evaluate(Evaluator &evaluator, Decl *decl) const;

public:
// Cached.
bool isCached() const { return true; }
};

/// Computes an initializer context for a parameter with a default argument.
class DefaultArgumentInitContextRequest
: public SimpleRequest<DefaultArgumentInitContextRequest,
Expand Down
4 changes: 2 additions & 2 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ SWIFT_REQUEST(TypeChecker, IsDefaultActorRequest,
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, IsDistributedActorRequest, bool(NominalTypeDecl *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, HasDistributedActorLocalInitRequest,
bool(NominalTypeDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, IsDistributedFuncRequest, bool(FuncDecl *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, GlobalActorInstanceRequest,
Expand Down Expand Up @@ -140,6 +138,8 @@ SWIFT_REQUEST(TypeChecker, InferredGenericSignatureRequest,
SmallVector<Requirement, 2>,
SmallVector<TypeLoc, 2>, bool),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, DistributedModuleIsAvailableRequest,
bool(ModuleDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, InheritedTypeRequest,
Type(llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>,
unsigned, TypeResolutionStage),
Expand Down
13 changes: 2 additions & 11 deletions include/swift/Runtime/Concurrency.h
Original file line number Diff line number Diff line change
Expand Up @@ -716,18 +716,9 @@ SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
void swift_defaultActor_deallocateResilient(HeapObject *actor);

/// Initialize the runtime storage for a distributed remote actor.
// TODO: this may end up being removed as we move to the "proxy creation" below
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
void swift_distributedActor_remote_initialize(DefaultActor *actor);

/// Create a proxy object that will serve as remote distributed actor instance.
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
OpaqueValue* swift_distributedActor_remote_create(
/* +1 */OpaqueValue *identity,
/* +1 */OpaqueValue *transport
// metadata for identity
// metadata for transport
);
OpaqueValue*
swift_distributedActor_remote_initialize(const Metadata *actorType);

/// Destroy the runtime storage for a default actor.
SWIFT_EXPORT_FROM(swift_Concurrency) SWIFT_CC(swift)
Expand Down
18 changes: 5 additions & 13 deletions include/swift/Runtime/RuntimeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -1687,22 +1687,14 @@ FUNCTION(DefaultActorDeallocateResilient,
ARGS(RefCountedPtrTy),
ATTRS(NoUnwind))

// void swift_distributedActor_remote_initialize(DefaultActor *actor);
// OpaqueValue* swift_distributedActor_remote_initialize(
// const Metadata *actorType
// );
FUNCTION(DistributedActorInitializeRemote,
swift_distributedActor_remote_initialize, SwiftCC,
ConcurrencyAvailability,
RETURNS(VoidTy),
ARGS(RefCountedPtrTy), // TODO also address and transport?
ATTRS(NoUnwind))

// OpaqueValue* swift_distributedActor_remote_create(
// OpaqueValue *identity,
// OpaqueValue *transport);
FUNCTION(DistributedActorRemoteCreate,
swift_distributedActor_remote_create, SwiftCC,
ConcurrencyAvailability,
RETURNS(OpaquePtrTy), // TODO: is this the right type here?
ARGS(OpaquePtrTy, OpaquePtrTy),
RETURNS(OpaquePtrTy),
ARGS(TypeMetadataPtrTy),
ATTRS(NoUnwind))

// void swift_distributedActor_destroy(DefaultActor *actor); // TODO: ProxyActor *proxy?
Expand Down
2 changes: 2 additions & 0 deletions lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,8 @@ ProtocolDecl *ASTContext::getProtocol(KnownProtocolKind kind) const {
M = getLoadedModule(Id_Concurrency);
break;
case KnownProtocolKind::DistributedActor:
case KnownProtocolKind::ActorTransport:
case KnownProtocolKind::ActorIdentity:
M = getLoadedModule(Id_Distributed);
break;
default:
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class Verifier : public ASTWalker {
case AbstractFunctionDecl::BodyKind::None:
case AbstractFunctionDecl::BodyKind::TypeChecked:
case AbstractFunctionDecl::BodyKind::Skipped:
case AbstractFunctionDecl::BodyKind::MemberwiseInitializer:
case AbstractFunctionDecl::BodyKind::SILSynthesize:
case AbstractFunctionDecl::BodyKind::Deserialized:
return true;

Expand Down
16 changes: 13 additions & 3 deletions lib/AST/Builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1462,10 +1462,18 @@ static ValueDecl *getDefaultActorInitDestroy(ASTContext &ctx,
_void);
}

static ValueDecl *getDistributedActorInitDestroy(ASTContext &ctx,
static ValueDecl *getDistributedActorInitializeRemote(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_generics(_unrestricted), // TODO(distributed): restrict to DistributedActor
_parameters(_metatype(_typeparam(0))),
_rawPointer);
}

static ValueDecl *getDistributedActorDestroy(ASTContext &ctx,
Identifier id) {
return getBuiltinFunction(ctx, id, _thin,
_parameters(_nativeObject), // TODO: no idea if to pass more here?
_parameters(_nativeObject),
_void);
}

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

case BuiltinValueKind::InitializeDistributedRemoteActor:
return getDistributedActorInitializeRemote(Context, Id);

case BuiltinValueKind::DestroyDistributedActor:
return getDistributedActorInitDestroy(Context, Id);
return getDistributedActorDestroy(Context, Id);

case BuiltinValueKind::StartAsyncLet:
case BuiltinValueKind::StartAsyncLetWithLocalBuffer:
Expand Down
Loading