Skip to content

🍒[5.7-04182022][Distributed] Guard ID synthesis from happening multiple times #42579

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
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
3 changes: 3 additions & 0 deletions include/swift/AST/DistributedDecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class DeclContext;
class FuncDecl;
class NominalTypeDecl;

/// Obtain a distributed actor's well-known property by name.
VarDecl* lookupDistributedActorProperty(NominalTypeDecl *decl, DeclName name);

/// Determine the concrete type of 'ActorSystem' as seen from the member.
/// E.g. when in a protocol, and trying to determine what the actor system was
/// constrained to.
Expand Down
39 changes: 39 additions & 0 deletions lib/AST/DistributedDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,45 @@

using namespace swift;

/******************************************************************************/
/********************** Distributed Actor Properties **************************/
/******************************************************************************/

VarDecl* swift::lookupDistributedActorProperty(NominalTypeDecl *decl, DeclName name) {
assert(decl && "decl was null");
auto &C = decl->getASTContext();

auto clazz = dyn_cast<ClassDecl>(decl);
if (!clazz)
return nullptr;

auto refs = decl->lookupDirect(name);
if (refs.size() != 1)
return nullptr;

auto var = dyn_cast<VarDecl>(refs.front());
if (!var)
return nullptr;

Type expectedType = Type();
if (name == C.Id_id) {
expectedType = getDistributedActorIDType(decl);
} else if (name == C.Id_actorSystem) {
expectedType = getDistributedActorSystemType(decl);
} else {
llvm_unreachable("Unexpected distributed actor property lookup!");
}
if (!expectedType)
return nullptr;

if (!var->getInterfaceType()->isEqual(expectedType))
return nullptr;

assert(var->isSynthesized() && "Expected compiler synthesized property");
return var;
}


/******************************************************************************/
/************** Distributed Actor System Associated Types *********************/
/******************************************************************************/
Expand Down
29 changes: 7 additions & 22 deletions lib/SILGen/SILGenDistributed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,6 @@ using namespace Lowering;

// MARK: utility functions

/// Obtain a nominal type's member by name, as a VarDecl.
/// \returns nullptr if the name lookup doesn't resolve to exactly one member,
/// or the subsequent cast to VarDecl failed.
static VarDecl* lookupProperty(NominalTypeDecl *decl, DeclName name) {
assert(decl && "decl was null");
if (auto clazz = dyn_cast<ClassDecl>(decl)) {
auto refs = decl->lookupDirect(name);
if (refs.size() != 1)
return nullptr;
return dyn_cast<VarDecl>(refs.front());
}

return nullptr;
}

/// Emit a reference to a specific stored property of the actor.
static SILValue emitActorPropertyReference(
SILGenFunction &SGF, SILLocation loc, SILValue actorSelf,
Expand Down Expand Up @@ -186,7 +171,7 @@ static void emitActorSystemInit(SILGenFunction &SGF,
// By construction, automatically generated distributed actor ctors have
// exactly one ActorSystem-conforming argument to the constructor,
// so we grab the first one from the params.
VarDecl *var = lookupProperty(classDecl, C.Id_actorSystem);
VarDecl *var = lookupDistributedActorProperty(classDecl, C.Id_actorSystem);
assert(var);

initializeProperty(SGF, loc, actorSelf.getValue(), var, systemValue);
Expand Down Expand Up @@ -219,7 +204,7 @@ void SILGenFunction::emitDistActorIdentityInit(ConstructorDecl *ctor,

// --- create a temporary storage for the result of the call
// it will be deallocated automatically as we exit this scope
VarDecl *var = lookupProperty(classDecl, C.Id_id);
VarDecl *var = lookupDistributedActorProperty(classDecl, C.Id_id);
auto resultTy = getLoweredType(F.mapTypeIntoContext(var->getInterfaceType()));
auto temp = emitTemporaryAllocation(loc, resultTy);

Expand Down Expand Up @@ -332,7 +317,7 @@ void SILGenFunction::emitDistributedActorReady(
ManagedValue actorSystem;
SGFContext sgfCxt;
{
VarDecl *property = lookupProperty(classDecl, C.Id_actorSystem);
VarDecl *property = lookupDistributedActorProperty(classDecl, C.Id_actorSystem);
Type formalType = F.mapTypeIntoContext(property->getInterfaceType());
SILType loweredType = getLoweredType(formalType).getAddressType();
SILValue actorSystemRef = emitActorPropertyReference(
Expand Down Expand Up @@ -466,11 +451,11 @@ void SILGenFunction::emitDistributedActorFactory(FuncDecl *fd) { // TODO(distrib
auto classDecl = dc->getSelfClassDecl();

initializeProperty(*this, loc, remote,
lookupProperty(classDecl, C.Id_id),
lookupDistributedActorProperty(classDecl, C.Id_id),
idArg);

initializeProperty(*this, loc, remote,
lookupProperty(classDecl, C.Id_actorSystem),
lookupDistributedActorProperty(classDecl, C.Id_actorSystem),
actorSystemArg);

// ==== Branch to return the fully initialized remote instance
Expand Down Expand Up @@ -513,12 +498,12 @@ void SILGenFunction::emitDistributedActorSystemResignIDCall(

// ==== locate: self.id
auto idRef = emitActorPropertyReference(
*this, loc, actorSelf.getValue(), lookupProperty(actorDecl, ctx.Id_id));
*this, loc, actorSelf.getValue(), lookupDistributedActorProperty(actorDecl, ctx.Id_id));

// ==== locate: self.actorSystem
auto systemRef = emitActorPropertyReference(
*this, loc, actorSelf.getValue(),
lookupProperty(actorDecl, ctx.Id_actorSystem));
lookupDistributedActorProperty(actorDecl, ctx.Id_actorSystem));

// Perform the call.
emitDistributedActorSystemWitnessCall(
Expand Down
7 changes: 7 additions & 0 deletions lib/Sema/CodeSynthesisDistributedActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,13 @@ VarDecl *GetDistributedActorIDPropertyRequest::evaluate(
if (!classDecl)
return nullptr;

// We may enter this request multiple times, e.g. in multi-file projects,
// so in order to avoid synthesizing a property many times, first perform
// a lookup and return if it already exists.
if (auto existingProp = lookupDistributedActorProperty(classDecl, C.Id_id)) {
return existingProp;
}

return addImplicitDistributedActorIDProperty(classDecl);
}

Expand Down
23 changes: 23 additions & 0 deletions test/Distributed/Inputs/EchoActor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of Swift project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Distributed

distributed actor Echo /* in the mirror */{
typealias ActorSystem = LocalTestingDistributedActorSystem

distributed func echo(_ input: String) -> String {
return "echo: \(input)"
}
}
35 changes: 35 additions & 0 deletions test/Distributed/Runtime/distributed_actor_in_other_module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/../Inputs/FakeDistributedActorSystems.swift
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/EchoActorModule.swiftmodule -module-name EchoActorModule -disable-availability-checking %S/../Inputs/EchoActor.swift
// RUN: %target-build-swift -module-name main -Xfrontend -enable-experimental-distributed -Xfrontend -disable-availability-checking -j2 -parse-as-library -I %t %s %S/../Inputs/FakeDistributedActorSystems.swift %S/../Inputs/EchoActor.swift -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s --color

// REQUIRES: executable_test
// REQUIRES: concurrency
// REQUIRES: distributed


// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime

// FIXME(distributed): Distributed actors currently have some issues on windows, isRemote always returns false. rdar://82593574
// UNSUPPORTED: windows

import Distributed
import EchoActorModule
import FakeDistributedActorSystems

func test() async {
let system = LocalTestingDistributedActorSystem()

let echo = Echo(actorSystem: system)
let reply = try! await echo.echo("in the mirror")
// CHECK: reply: echo: in the mirror
print("reply: \(reply)")
}

@main struct Main {
static func main() async {
await test()
}
}