Skip to content

[Distributed] cleanup some warnings in SILGenDistributed #38901

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
Aug 17, 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
120 changes: 37 additions & 83 deletions lib/SILGen/SILGenDestructor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,110 +197,64 @@ void SILGenFunction::emitIVarDestroyer(SILDeclRef ivarDestroyer) {
emitEpilog(loc);
}

void SILGenFunction::destroyClassMember(SILLocation cleanupLoc,
ManagedValue selfValue, VarDecl *D) {
const TypeLowering &ti = getTypeLowering(D->getType());
if (!ti.isTrivial()) {
SILValue addr =
B.createRefElementAddr(cleanupLoc, selfValue.getValue(), D,
ti.getLoweredType().getAddressType());
addr = B.createBeginAccess(
cleanupLoc, addr, SILAccessKind::Deinit, SILAccessEnforcement::Static,
false /*noNestedConflict*/, false /*fromBuiltin*/);
B.createDestroyAddr(cleanupLoc, addr);
B.createEndAccess(cleanupLoc, addr, false /*is aborting*/);
}
}

void SILGenFunction::emitClassMemberDestruction(ManagedValue selfValue,
ClassDecl *cd,
CleanupLocation cleanupLoc) {
assert(selfValue.getOwnershipKind() == OwnershipKind::Guaranteed);
ASTContext &ctx = getASTContext();

auto loc = SILLocation(cd);
loc.markAutoGenerated();

/// If this ClassDecl is a distributed actor, we must synthesise another code
/// path for deallocating a 'remote' actor. In that case, these basic blocks
/// are used to return to the "normal" (i.e. 'local' instance) destruction.
///
/// For other cases, the basic blocks are not necessary and the destructor
/// can just emit all the normal destruction code right into the current block.
// If set, used as the basic block for the destroying of all members.
SILBasicBlock* normalMemberDestroyBB = nullptr;
SILBasicBlock* returnBB = nullptr;

auto destroyVar = [&](VarDecl *vd) {
const TypeLowering &ti = getTypeLowering(vd->getType());
if (!ti.isTrivial()) {
SILValue addr =
B.createRefElementAddr(cleanupLoc, selfValue.getValue(), vd,
ti.getLoweredType().getAddressType());
addr = B.createBeginAccess(
cleanupLoc, addr, SILAccessKind::Deinit, SILAccessEnforcement::Static,
false /*noNestedConflict*/, false /*fromBuiltin*/);
B.createDestroyAddr(cleanupLoc, addr);
B.createEndAccess(cleanupLoc, addr, false /*is aborting*/);
}
};
// If set, used as the basic block after members have been destroyed,
// and we're ready to perform final cleanups before returning.
SILBasicBlock* finishBB = nullptr;

/// A distributed actor may be 'remote' in which case there is no need to
/// destroy "all" members, because they never had storage to begin with.
if (cd->isDistributedActor()) {
auto selfTy = cd->getDeclaredInterfaceType();

Scope scope(Cleanups, CleanupLocation(loc));

auto isLocalBB = createBasicBlock();
finishBB = createBasicBlock();
normalMemberDestroyBB = createBasicBlock();
auto remoteMemberDestroyBB = createBasicBlock();
returnBB = createBasicBlock();

// TODO(distributed): de-duplicate with the thunk logic in SILGenDistributed
// if __isRemoteActor(self) {
// ...
// } else {
// ...
// }
{
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(selfTy), selfValue, {});
auto result = emitApplyOfLibraryIntrinsic(
loc, isRemoteFn, SubstitutionMap(), {selfAnyObject}, SGFContext());

SILValue isRemoteResult =
std::move(result).forwardAsSingleValue(*this, loc);
SILValue isRemoteResultUnwrapped =
emitUnwrapIntegerResult(loc, isRemoteResult);

B.createCondBranch(loc, isRemoteResultUnwrapped, remoteMemberDestroyBB, isLocalBB);
}

// // if __isRemoteActor(self)
// {
// // destroy only self.id and self.actorTransport
// }
{
B.emitBlock(remoteMemberDestroyBB);

for (VarDecl *vd : cd->getStoredProperties()) {
if (!vd->getAttrs().hasAttribute<DistributedActorIndependentAttr>())
continue;

destroyVar(vd);
}

B.createBranch(loc, returnBB);
}

// // else (local distributed actor)
// {
// <continue normal deinit>
// }
{
B.emitBlock(isLocalBB);

B.createBranch(loc, normalMemberDestroyBB);
}
emitDistributedActorClassMemberDestruction(cleanupLoc, selfValue, cd,
normalMemberDestroyBB,
finishBB);
}

/// Destroy all members.
{
if (normalMemberDestroyBB)
B.emitBlock(normalMemberDestroyBB);

for (VarDecl *vd : cd->getStoredProperties())
destroyVar(vd);
destroyClassMember(cleanupLoc, selfValue, vd);

if (returnBB)
B.createBranch(loc, returnBB);
if (finishBB)
B.createBranch(cleanupLoc, finishBB);
}

{
if (returnBB)
B.emitBlock(returnBB);

if (finishBB)
B.emitBlock(finishBB);

if (cd->isRootDefaultActor()) {
// TODO(distributed): we may need to call the distributed destroy here instead?
Expand Down
Loading