Skip to content

[Distributed] synthesize _remote and implement via dynamic #37738

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 3 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5265,7 +5265,10 @@ class ParamDecl : public VarDecl {

/// Create a new ParamDecl identical to the first except without the interface type.
static ParamDecl *cloneWithoutType(const ASTContext &Ctx, ParamDecl *PD);


/// Create a an identical copy of this ParamDecl.
static ParamDecl *clone(const ASTContext &Ctx, ParamDecl *PD);

/// Retrieve the argument (API) name for this function parameter.
Identifier getArgumentName() const {
return ArgumentNameAndDestructured.getPointer();
Expand Down
8 changes: 3 additions & 5 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4444,11 +4444,9 @@ ERROR(distributed_actor_func_param_not_codable,none,
ERROR(distributed_actor_func_result_not_codable,none,
"distributed function result type %0 does not conform to 'Codable'",
(Type))
ERROR(distributed_actor_func_missing_remote_func,none,
"distributed function is missing its remote static func implementation (%0). "
"You may want to use a distributed actor SwiftPM plugin to generate those, "
"or implement it manually.",
(Identifier))
ERROR(distributed_actor_remote_func_implemented_manually,none,
"Distributed function's %0 remote counterpart %1 cannot not be implemented manually.",
(Identifier, Identifier))
ERROR(distributed_actor_remote_func_is_not_static,none,
"remote function %0 must be static.",
(DeclName))
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/KnownSDKDecls.def
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
# define KNOWN_SDK_FUNC_DECL(Module, Name, Id)
#endif

KNOWN_SDK_FUNC_DECL(Distributed, MissingDistributedActorTransport, "_missingDistributedActorTransport")
KNOWN_SDK_FUNC_DECL(Distributed, IsRemoteDistributedActor, "__isRemoteActor")

#undef KNOWN_SDK_FUNC_DECL

8 changes: 8 additions & 0 deletions include/swift/AST/ParameterList.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ class alignas(ParamDecl *) ParameterList final :
return create(decl->getASTContext(), decl);
}

static ParameterList *clone(const ASTContext &Ctx, ParameterList *PL) {
SmallVector<ParamDecl*, 4> params;
params.reserve(PL->size());
for (auto p : *PL)
params.push_back(ParamDecl::clone(Ctx, p));
return ParameterList::create(Ctx, params);
}

SourceLoc getLParenLoc() const { return LParenLoc; }
SourceLoc getRParenLoc() const { return RParenLoc; }

Expand Down
6 changes: 6 additions & 0 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2309,6 +2309,12 @@ class Verifier : public ASTWalker {
}

void verifyChecked(ValueDecl *VD) {
if (VD->getName().isSimpleName() &&
!VD->getName().isSpecial() &&
VD->getName().getBaseIdentifier().str() == "echo") {
VD->dump();
}

if (VD->getInterfaceType()->hasError()) {
Out << "checked decl cannot have error type\n";
VD->dump(Out);
Expand Down
6 changes: 6 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6313,6 +6313,12 @@ ParamDecl *ParamDecl::cloneWithoutType(const ASTContext &Ctx, ParamDecl *PD) {
return Clone;
}

ParamDecl *ParamDecl::clone(const ASTContext &Ctx, ParamDecl *PD) {
auto *Clone = ParamDecl::cloneWithoutType(Ctx, PD);
Clone->setInterfaceType(PD->getInterfaceType());
return Clone;
}

/// Retrieve the type of 'self' for the given context.
Type DeclContext::getSelfTypeInContext() const {
assert(isTypeContext());
Expand Down
6 changes: 3 additions & 3 deletions lib/SIL/IR/SILDeclRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ bool swift::requiresForeignEntryPoint(ValueDecl *vd) {
return false;
}

SILDeclRef::SILDeclRef(ValueDecl *vd, SILDeclRef::Kind kind, bool isForeign,
bool isDistributed,
SILDeclRef::SILDeclRef(ValueDecl *vd, SILDeclRef::Kind kind,
bool isForeign, bool isDistributed,
AutoDiffDerivativeFunctionIdentifier *derivativeId)
: loc(vd), kind(kind), isForeign(isForeign), isDistributed(isDistributed),
defaultArgIndex(0), pointer(derivativeId) {}
Expand Down Expand Up @@ -171,7 +171,7 @@ SILDeclRef::SILDeclRef(SILDeclRef::Loc baseLoc, bool asForeign, bool asDistribut

SILDeclRef::SILDeclRef(SILDeclRef::Loc baseLoc,
GenericSignature prespecializedSig)
: SILDeclRef(baseLoc, false) {
: SILDeclRef(baseLoc, false, false) {
pointer = prespecializedSig.getPointer();
}

Expand Down
32 changes: 10 additions & 22 deletions lib/SILGen/SILGenBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1960,7 +1960,7 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
//
// func X_distributedThunk(...) async throws -> T {
// if __isRemoteActor(self) {
// return try await Self.__remote_X(...)
// return try await self._remote_X(...)
// } else {
// return try await self.X(...)
// }
Expand Down Expand Up @@ -2011,19 +2011,9 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
// ...
// }
{
auto desc = UnqualifiedLookupDescriptor(
DeclNameRef(ctx.Id___isRemoteActor),
fd->getDeclContext(), fd->getLoc(), UnqualifiedLookupOptions());
auto lookup =
evaluateOrDefault(ctx.evaluator, UnqualifiedLookupRequest{desc}, {});
FuncDecl *isRemoteFn = nullptr;
for (const auto &result : lookup) {
// FIXME: Validate this further, because we're assuming the exact type.
if (auto func = dyn_cast<FuncDecl>(result.getValueDecl())) {
isRemoteFn = func;
break;
}
}
FuncDecl* isRemoteFn = ctx.getIsRemoteDistributedActor();
assert(isRemoteFn &&
"Could not find 'is remote' function, is the '_Distributed' module available?");

ManagedValue selfAnyObject = B.createInitExistentialRef(loc, getLoweredType(ctx.getAnyObjectType()),
CanType(selfType),
Expand All @@ -2037,39 +2027,37 @@ void SILGenFunction::emitDistributedThunk(SILDeclRef thunk) {
B.createCondBranch(loc, isRemoteResultUnwrapped, isRemoteBB, isLocalBB);
}


// if __isRemoteActor(self) {
// return try await Self._remote_X(...)
// return try await self._remote_X(...)
// }
{
B.emitBlock(isRemoteBB);

auto *selfTyDecl = FunctionDC->getParent()->getSelfNominalTypeDecl();
// FIXME: should this be an llvm_unreachable instead?
assert(selfTyDecl && "distributed function declared outside of actor");

auto selfMetatype = getLoweredType(selfTyDecl->getInterfaceType());
SILValue metatypeValue = B.createMetatype(loc, selfMetatype);
// auto selfMetatype = getLoweredType(selfTyDecl->getInterfaceType());
// SILValue metatypeValue = B.createMetatype(loc, selfMetatype);

auto remoteFnDecl = selfTyDecl->lookupDirectRemoteFunc(fd);
assert(remoteFnDecl && "Could not find _remote_<dist_func_name> function");
auto remoteFnRef = SILDeclRef(remoteFnDecl);

SILGenFunctionBuilder builder(SGM);
auto remoteFnSIL = builder.getOrCreateFunction(loc, remoteFnRef, ForDefinition);

SILValue remoteFn = B.createFunctionRefFor(loc, remoteFnSIL);

auto subs = F.getForwardingSubstitutionMap();

SmallVector<SILValue, 8> remoteParams(params);
remoteParams.emplace_back(metatypeValue);
// remoteParams.emplace_back(metatypeValue);

// B.createTryApply(loc, remoteFn, subs, params, remoteReturnBB, remoteErrorBB);
B.createTryApply(loc, remoteFn, subs, remoteParams, remoteReturnBB, remoteErrorBB);
}

// else {
// return try await self.X(...)
// return (try)? (await)? self.X(...)
// }
{
B.emitBlock(isLocalBB);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CodeSynthesis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "swift/Sema/ConstraintSystem.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "CodeSynthesisDistributedActor.cpp"
#include "CodeSynthesisDistributedActor.cpp" // FIXME: remove this!!!
using namespace swift;

const bool IsImplicit = true;
Expand Down
Loading