Skip to content

Add support for distributed functions in extensions of distributed actors #39785

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

Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
1 change: 1 addition & 0 deletions lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,7 @@ static void emitOrDelayFunction(SILGenModule &SGM,
auto linkage = constant.getLinkage(ForDefinition);
bool mayDelay = !forceEmission &&
(constant.isImplicit() &&
!constant.isDynamicallyReplaceable() &&
!isPossiblyUsedExternally(linkage, SGM.M.isWholeModule()));

// Avoid emitting a delayable definition if it hasn't already been referenced.
Expand Down
14 changes: 13 additions & 1 deletion lib/SILGen/SILGenDistributed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -951,8 +951,20 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
auto nativeFnSILTy = SILType::getPrimitiveObjectType(nativeMethodTy);
auto nativeSilFnType = nativeFnSILTy.castTo<SILFunctionType>();

SILValue nativeFn = emitClassMethodRef(
bool isClassMethod = false;
if (auto classDecl = dyn_cast<ClassDecl>(fd->getDeclContext())) {
if (!classDecl->isFinal() && !fd->isFinal() &&
!fd->hasForcedStaticDispatch())
isClassMethod = true;
}

SILValue nativeFn;
if (isClassMethod) {
nativeFn = emitClassMethodRef(
loc, params[params.size() - 1], native, nativeMethodTy);
} else {
nativeFn = emitGlobalFunctionRef(loc, native);
}
auto subs = F.getForwardingSubstitutionMap();

if (nativeSilFnType->hasErrorResult()) {
Expand Down
32 changes: 24 additions & 8 deletions lib/Sema/CodeSynthesisDistributedActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,24 +137,23 @@ static Identifier makeRemoteFuncIdentifier(FuncDecl* distributedFunc) {
///
/// and is intended to be replaced by a transport library by providing an
/// appropriate @_dynamicReplacement function.
AbstractFunctionDecl *TypeChecker::addImplicitDistributedActorRemoteFunction(
ClassDecl *decl, AbstractFunctionDecl *AFD) {
if (!decl->isDistributedActor())
static AbstractFunctionDecl *addImplicitDistributedActorRemoteFunction(
DeclContext *parentDC, AbstractFunctionDecl *AFD) {
auto nominal = parentDC->getSelfNominalTypeDecl();
if (!nominal || !nominal->isDistributedActor())
return nullptr;

auto func = dyn_cast<FuncDecl>(AFD);
if (!func || !func->isDistributed())
return nullptr;

// ==== if the remote func already exists, return it
if (auto existing = decl->lookupDirectRemoteFunc(func))
if (auto existing = nominal->lookupDirectRemoteFunc(func))
return existing;

// ==== Synthesize and add 'remote' func to the actor decl

auto &C = decl->getASTContext();
auto parentDC = decl;

auto &C = func->getASTContext();
auto remoteFuncIdent = makeRemoteFuncIdentifier(func);

auto params = ParameterList::clone(C, func->getParameters());
Expand Down Expand Up @@ -189,7 +188,24 @@ AbstractFunctionDecl *TypeChecker::addImplicitDistributedActorRemoteFunction(
// same access control as the original function is fine
remoteFuncDecl->copyFormalAccessFrom(func, /*sourceIsParentContext=*/false);

decl->addMember(remoteFuncDecl);
cast<IterableDeclContext>(parentDC->getAsDecl())->addMember(remoteFuncDecl);

return remoteFuncDecl;
}

AbstractFunctionDecl *GetDistributedRemoteFuncRequest::evaluate(
Evaluator &evaluator, AbstractFunctionDecl *func) const {

if (!func->isDistributed())
return nullptr;

auto &C = func->getASTContext();
DeclContext *DC = func->getDeclContext();

// not via `ensureDistributedModuleLoaded` to avoid generating a warning,
// we won't be emitting the offending decl after all.
if (!C.getLoadedModule(C.Id_Distributed))
return nullptr;

return addImplicitDistributedActorRemoteFunction(DC, func);
}
7 changes: 4 additions & 3 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2820,10 +2820,11 @@ static ArrayRef<Decl *> evaluateMembersRequest(
(void) var->getPropertyWrapperAuxiliaryVariables();
(void) var->getPropertyWrapperInitializerInfo();
}
}

if (auto *func = dyn_cast<FuncDecl>(member)) {
(void) func->getDistributedActorRemoteFuncDecl();
}
// For a distributed function, add the remote function.
if (auto *func = dyn_cast<FuncDecl>(member)) {
(void) func->getDistributedActorRemoteFuncDecl();
}
}

Expand Down
36 changes: 1 addition & 35 deletions lib/Sema/TypeCheckDistributed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,36 +71,6 @@ bool IsDistributedActorRequest::evaluate(
return classDecl->isExplicitDistributedActor();
}

AbstractFunctionDecl *GetDistributedRemoteFuncRequest::evaluate(
Evaluator &evaluator, AbstractFunctionDecl *func) const {

if (!func->isDistributed())
return nullptr;

auto &C = func->getASTContext();
DeclContext *DC = func->getDeclContext();

// not via `ensureDistributedModuleLoaded` to avoid generating a warning,
// we won't be emitting the offending decl after all.
if (!C.getLoadedModule(C.Id_Distributed))
return nullptr;

// Locate the actor decl that the member must be synthesized to.
// TODO(distributed): should this just be added to the extension instead when we're in one?
ClassDecl *decl = dyn_cast<ClassDecl>(DC);
if (!decl) {
if (auto ED = dyn_cast<ExtensionDecl>(DC)) {
decl = dyn_cast<ClassDecl>(ED->getExtendedNominal());
}
}

/// A distributed func cannot be added to a non-distributed actor;
/// If the 'decl' was not a distributed actor we must have declared and
/// requested it from a illegal context, let's just ignore the synthesis.
assert(decl && "Can't find actor detect to add implicit _remote function to");
return TypeChecker::addImplicitDistributedActorRemoteFunction(decl, func);
}

// ==== ------------------------------------------------------------------------

/// Check whether the function is a proper distributed function
Expand Down Expand Up @@ -150,7 +120,7 @@ bool swift::checkDistributedFunction(FuncDecl *func, bool diagnose) {
}

// === Check _remote functions
ClassDecl *actorDecl = dyn_cast<ClassDecl>(func->getParent());
auto actorDecl = func->getParent()->getSelfNominalTypeDecl();
assert(actorDecl && actorDecl->isDistributedActor());

// _remote function for a distributed function must not be implemented by end-users,
Expand Down Expand Up @@ -252,10 +222,6 @@ void TypeChecker::checkDistributedActor(ClassDecl *decl) {
// --- Check all constructors
if (auto ctor = dyn_cast<ConstructorDecl>(member))
checkDistributedActorConstructor(decl, ctor);

// --- synthesize _remote functions for distributed functions
if (auto func = dyn_cast<FuncDecl>(member))
(void)addImplicitDistributedActorRemoteFunction(decl, func);
}

// ==== Properties
Expand Down
8 changes: 0 additions & 8 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -499,14 +499,6 @@ bool checkContextualRequirements(GenericTypeDecl *decl,
/// struct, class or actor.
void addImplicitConstructors(NominalTypeDecl *typeDecl);

/// Synthesize and add a '_remote' counterpart of the passed in `func` to `decl`.
///
/// \param decl the actor type to add the '_remote' definition to
/// \param func the 'distributed func' that the '_remote' func should mirror
/// \return the synthesized function
AbstractFunctionDecl *addImplicitDistributedActorRemoteFunction(
ClassDecl* decl, AbstractFunctionDecl *func);

/// Fold the given sequence expression into an (unchecked) expression
/// tree.
Expr *foldSequence(SequenceExpr *expr, DeclContext *dc);
Expand Down
19 changes: 19 additions & 0 deletions test/SILGen/distributed_thunk.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// RUN: %target-swift-emit-silgen %s -enable-experimental-distributed -disable-availability-checking | %FileCheck %s --dump-input=fail
// REQUIRES: concurrency
// REQUIRES: distributed

import _Distributed

distributed actor DA { }

extension DA {
// CHECK-LABEL: sil hidden [thunk] [ossa] @$s17distributed_thunk2DAC1fyyFTd : $@convention(method) @async (@guaranteed DA) -> @error Error
// CHECK: function_ref @swift_distributed_actor_is_remote

// Call the actor function
// CHECK: function_ref @$s17distributed_thunk2DAC1fyyF : $@convention(method) (@guaranteed DA) -> ()

// ... or the remote thunk
// CHECK: dynamic_function_ref @$s17distributed_thunk2DAC9_remote_fyyYaKF : $@convention(method) @async (@guaranteed DA) -> @error Error
distributed func f() { }
}