Skip to content

Random distributed cleanup #69514

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
Closed
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
6 changes: 0 additions & 6 deletions include/swift/AST/DistributedDecl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ llvm::SmallPtrSet<ProtocolDecl *, 2>
getDistributedSerializationRequirementProtocols(
NominalTypeDecl *decl, ProtocolDecl* protocol);

/// Desugar and flatten the `SerializationRequirement` type into a set of
/// specific protocol declarations.
llvm::SmallPtrSet<ProtocolDecl *, 2>
flattenDistributedSerializationTypeToRequiredProtocols(
TypeBase *serializationRequirement);

/// Check if the `allRequirements` represent *exactly* the
/// `Encodable & Decodable` (also known as `Codable`) requirement.
///
Expand Down
114 changes: 25 additions & 89 deletions lib/AST/DistributedDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,52 +346,19 @@ swift::getDistributedSerializationRequirements(
if (existentialRequirementTy->isAny())
return true; // we're done here, any means there are no requirements

if (!existentialRequirementTy->isExistentialType()) {
// SerializationRequirement must be an existential type
return false;
}

ExistentialType *serialReqType = existentialRequirementTy
->castTo<ExistentialType>();
auto *serialReqType = existentialRequirementTy->getAs<ExistentialType>();
if (!serialReqType || serialReqType->hasError()) {
return false;
}

auto desugaredTy = serialReqType->getConstraintType()->getDesugaredType();
auto flattenedRequirements =
flattenDistributedSerializationTypeToRequiredProtocols(
desugaredTy);
for (auto p : flattenedRequirements) {
auto layout = serialReqType->getExistentialLayout();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, nice thank you

for (auto p : layout.getProtocols()) {
requirementProtos.insert(p);
}

return true;
}

llvm::SmallPtrSet<ProtocolDecl *, 2>
swift::flattenDistributedSerializationTypeToRequiredProtocols(
TypeBase *serializationRequirement) {
llvm::SmallPtrSet<ProtocolDecl *, 2> serializationReqs;
if (auto composition =
serializationRequirement->getAs<ProtocolCompositionType>()) {
for (auto member : composition->getMembers()) {
if (auto comp = member->getAs<ProtocolCompositionType>()) {
for (auto protocol :
flattenDistributedSerializationTypeToRequiredProtocols(comp)) {
serializationReqs.insert(protocol);
}
} else if (auto *protocol = member->getAs<ProtocolType>()) {
serializationReqs.insert(protocol->getDecl());
}
}
} else {
auto protocol = serializationRequirement->castTo<ProtocolType>()->getDecl();
serializationReqs.insert(protocol);
}

return serializationReqs;
}

bool swift::checkDistributedSerializationRequirementIsExactlyCodable(
ASTContext &C,
const llvm::SmallPtrSetImpl<ProtocolDecl *> &allRequirements) {
Expand Down Expand Up @@ -571,25 +538,19 @@ bool AbstractFunctionDecl::isDistributedActorSystemRemoteCall(bool isVoidReturn)

// --- Check requirement: conforms_to: Act DistributedActor
auto actorReq = requirements[0];
auto distActorTy = C.getProtocol(KnownProtocolKind::DistributedActor)
->getInterfaceType()
->getMetatypeInstanceType();
if (actorReq.getKind() != RequirementKind::Conformance) {
return false;
}
if (!actorReq.getSecondType()->isEqual(distActorTy)) {
if (!actorReq.getProtocolDecl()->isSpecificProtocol(KnownProtocolKind::DistributedActor)) {
return false;
}

// --- Check requirement: conforms_to: Err Error
auto errorReq = requirements[1];
auto errorTy = C.getProtocol(KnownProtocolKind::Error)
->getInterfaceType()
->getMetatypeInstanceType();
if (errorReq.getKind() != RequirementKind::Conformance) {
return false;
}
if (!errorReq.getSecondType()->isEqual(errorTy)) {
if (!errorReq.getProtocolDecl()->isSpecificProtocol(KnownProtocolKind::Error)) {
return false;
}

Expand All @@ -604,10 +565,9 @@ bool AbstractFunctionDecl::isDistributedActorSystemRemoteCall(bool isVoidReturn)
assert(ResParam && "Non void function, yet no Res generic parameter found");
if (auto func = dyn_cast<FuncDecl>(this)) {
auto resultType = func->mapTypeIntoContext(func->getResultInterfaceType())
->getMetatypeInstanceType()
->getDesugaredType();
->getMetatypeInstanceType();
auto resultParamType = func->mapTypeIntoContext(
ResParam->getInterfaceType()->getMetatypeInstanceType());
ResParam->getDeclaredInterfaceType());
// The result of the function must be the `Res` generic argument.
if (!resultType->isEqual(resultParamType)) {
return false;
Expand Down Expand Up @@ -803,12 +763,10 @@ AbstractFunctionDecl::isDistributedTargetInvocationEncoderRecordArgument() const

// the <Value> of the RemoteCallArgument<Value>
auto remoteCallArgValueGenericTy =
mapTypeIntoContext(argGenericParams[0]->getInterfaceType())
->getDesugaredType()
->getMetatypeInstanceType();
mapTypeIntoContext(argGenericParams[0]->getDeclaredInterfaceType());
// expected (the <Value> from the recordArgument<Value>)
auto expectedGenericParamTy = mapTypeIntoContext(
ArgumentParam->getInterfaceType()->getMetatypeInstanceType());
ArgumentParam->getDeclaredInterfaceType());

if (!remoteCallArgValueGenericTy->isEqual(expectedGenericParamTy)) {
return false;
Expand Down Expand Up @@ -938,11 +896,10 @@ AbstractFunctionDecl::isDistributedTargetInvocationEncoderRecordReturnType() con
// ...

auto resultType = func->mapTypeIntoContext(argumentParam->getInterfaceType())
->getMetatypeInstanceType()
->getDesugaredType();
->getMetatypeInstanceType();

auto resultParamType = func->mapTypeIntoContext(
ArgumentParam->getInterfaceType()->getMetatypeInstanceType());
ArgumentParam->getDeclaredInterfaceType());

// The result of the function must be the `Res` generic argument.
if (!resultType->isEqual(resultParamType)) {
Expand Down Expand Up @@ -1052,13 +1009,10 @@ AbstractFunctionDecl::isDistributedTargetInvocationEncoderRecordErrorType() cons

// --- Check requirement: conforms_to: Err Error
auto errorReq = requirements[0];
auto errorTy = C.getProtocol(KnownProtocolKind::Error)
->getInterfaceType()
->getMetatypeInstanceType();
if (errorReq.getKind() != RequirementKind::Conformance) {
return false;
}
if (!errorReq.getSecondType()->isEqual(errorTy)) {
if (!errorReq.getProtocolDecl()->isSpecificProtocol(KnownProtocolKind::Error)) {
return false;
}

Expand Down Expand Up @@ -1145,10 +1099,9 @@ AbstractFunctionDecl::isDistributedTargetInvocationDecoderDecodeNextArgument() c
// --- Check: Argument: SerializationRequirement
GenericTypeParamDecl *ArgumentParam = genericParams->getParams()[0];
auto resultType = func->mapTypeIntoContext(func->getResultInterfaceType())
->getMetatypeInstanceType()
->getDesugaredType();
->getMetatypeInstanceType();
auto resultParamType = func->mapTypeIntoContext(
ArgumentParam->getInterfaceType()->getMetatypeInstanceType());
ArgumentParam->getDeclaredInterfaceType());
// The result of the function must be the `Res` generic argument.
if (!resultType->isEqual(resultParamType)) {
return false;
Expand Down Expand Up @@ -1243,11 +1196,10 @@ AbstractFunctionDecl::isDistributedTargetInvocationResultHandlerOnReturn() const
// === Check generic parameters in detail
// --- Check: Argument: SerializationRequirement
GenericTypeParamDecl *ArgumentParam = genericParams->getParams()[0];
auto argumentType = func->mapTypeIntoContext(valueParam->getInterfaceType())
->getMetatypeInstanceType()
->getDesugaredType();
auto argumentType = func->mapTypeIntoContext(
valueParam->getInterfaceType()->getMetatypeInstanceType());
auto resultParamType = func->mapTypeIntoContext(
ArgumentParam->getInterfaceType()->getMetatypeInstanceType());
ArgumentParam->getDeclaredInterfaceType());
// The result of the function must be the `Res` generic argument.
if (!argumentType->isEqual(resultParamType)) {
return false;
Expand Down Expand Up @@ -1275,35 +1227,19 @@ swift::extractDistributedSerializationRequirements(
auto DA = C.getDistributedActorDecl();
auto daSerializationReqAssocType =
DA->getAssociatedType(C.Id_SerializationRequirement);
auto daSystemSerializationReqTy = daSerializationReqAssocType->getInterfaceType();

for (auto req : allRequirements) {
if (req.getSecondType()->isAny()) {
continue;
}
if (!req.getFirstType()->hasDependentMember())
// FIXME: Seems unprincipled
if (req.getKind() != RequirementKind::SameType &&
req.getKind() != RequirementKind::Conformance)
continue;

if (auto dependentMemberType =
req.getFirstType()->castTo<DependentMemberType>()) {
auto dependentTy =
dependentMemberType->getAssocType()->getInterfaceType();

if (dependentTy->isEqual(daSystemSerializationReqTy)) {
auto requirementProto = req.getSecondType();
if (auto proto = dyn_cast_or_null<ProtocolDecl>(
requirementProto->getAnyNominal())) {
serializationReqs.insert(proto);
} else {
auto serialReqType = requirementProto->castTo<ExistentialType>()
->getConstraintType()
->getDesugaredType();
auto flattenedRequirements =
flattenDistributedSerializationTypeToRequiredProtocols(
serialReqType);
for (auto p : flattenedRequirements) {
serializationReqs.insert(p);
}
req.getFirstType()->getAs<DependentMemberType>()) {
if (dependentMemberType->getAssocType() == daSerializationReqAssocType) {
auto layout = req.getSecondType()->getExistentialLayout();
for (auto p : layout.getProtocols()) {
serializationReqs.insert(p);
}
}
}
Expand Down
31 changes: 14 additions & 17 deletions lib/Sema/TypeCheckDistributed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ bool CheckDistributedFunctionRequest::evaluate(
} else if (isa<ProtocolDecl>(DC)) {
if (auto seqReqTy =
getConcreteReplacementForMemberSerializationRequirement(func)) {
auto seqReqTyDes = seqReqTy->castTo<ExistentialType>()->getConstraintType()->getDesugaredType();
for (auto req : flattenDistributedSerializationTypeToRequiredProtocols(seqReqTyDes)) {
auto layout = seqReqTy->getExistentialLayout();
for (auto req : layout.getProtocols()) {
serializationRequirements.insert(req);
}
}
Expand Down Expand Up @@ -814,11 +814,13 @@ swift::getDistributedSerializationRequirementProtocols(
return {};
}

auto serialReqType =
ty->castTo<ExistentialType>()->getConstraintType()->getDesugaredType();

// TODO(distributed): check what happens with Any
return flattenDistributedSerializationTypeToRequiredProtocols(serialReqType);
auto layout = ty->getExistentialLayout();
llvm::SmallPtrSet<ProtocolDecl *, 2> result;
for (auto p : layout.getProtocols()) {
result.insert(p);
}
return result;
}

ConstructorDecl*
Expand Down Expand Up @@ -942,29 +944,24 @@ GetDistributedActorArgumentDecodingMethodRequest::evaluate(Evaluator &evaluator,
continue;

auto paramTy = genericParamList->getParams()[0]
->getInterfaceType()
->getMetatypeInstanceType();
->getDeclaredInterfaceType();

// `decodeNextArgument` should return its generic parameter value
if (!FD->getResultInterfaceType()->isEqual(paramTy))
continue;

// Let's find out how many serialization requirements does this method cover
// e.g. `Codable` is two requirements - `Encodable` and `Decodable`.
unsigned numSerializationReqsCovered = llvm::count_if(
FD->getGenericRequirements(), [&](const Requirement &requirement) {
if (!(requirement.getFirstType()->isEqual(paramTy) &&
requirement.getKind() == RequirementKind::Conformance))
return 0;

return serializationReqs.count(requirement.getProtocolDecl()) ? 1 : 0;
});
bool okay = llvm::all_of(serializationReqs,
[&](ProtocolDecl *p) -> bool {
return FD->getGenericSignature()->requiresProtocol(paramTy, p);
});

// If the current method covers all of the serialization requirements,
// it's a match. Note that it might also have other requirements, but
// we let that go as long as there are no two candidates that differ
// only in generic requirements.
if (numSerializationReqsCovered == serializationReqs.size())
if (okay)
candidates.push_back(FD);
}

Expand Down