Skip to content

🍒[5.7][Distributed] SILGen must consistently use prop get requests #42600

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 1 commit into from
Apr 23, 2022
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
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 = classDecl->getDistributedActorSystemProperty();
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 = classDecl->getDistributedActorIDProperty();
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 = classDecl->getDistributedActorSystemProperty();
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),
classDecl->getDistributedActorIDProperty(),
idArg);

initializeProperty(*this, loc, remote,
lookupProperty(classDecl, C.Id_actorSystem),
classDecl->getDistributedActorSystemProperty(),
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(), actorDecl->getDistributedActorIDProperty());

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

// Perform the call.
emitDistributedActorSystemWitnessCall(
Expand Down
36 changes: 36 additions & 0 deletions lib/Sema/CodeSynthesisDistributedActor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,42 @@ using namespace swift;
/************************ PROPERTY SYNTHESIS **********************************/
/******************************************************************************/

static VarDecl*
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;
}


// Note: This would be nice to implement in DerivedConformanceDistributedActor,
// but we can't since those are lazily triggered and an implementation exists
// for the 'id' property because 'Identifiable.id' has an extension that impls
Expand Down