Skip to content

RequirementMachine: Protocol reduction order refactoring #39864

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
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
1 change: 0 additions & 1 deletion lib/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ add_swift_host_library(swiftAST STATIC
RequirementMachine/GenericSignatureQueries.cpp
RequirementMachine/PropertyMap.cpp
RequirementMachine/PropertyUnification.cpp
RequirementMachine/ProtocolGraph.cpp
RequirementMachine/RequirementMachine.cpp
RequirementMachine/RequirementMachineRequests.cpp
RequirementMachine/RewriteContext.cpp
Expand Down
36 changes: 10 additions & 26 deletions lib/AST/RequirementMachine/GenericSignatureQueries.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,20 @@ RequirementMachine::getLocalRequirements(
System.simplify(term);
verify(term);

auto &protos = System.getProtocols();

GenericSignature::LocalRequirements result;
result.anchor = Context.getTypeForTerm(term, genericParams, protos);
result.anchor = Context.getTypeForTerm(term, genericParams);

auto *props = Map.lookUpProperties(term);
if (!props)
return result;

if (props->isConcreteType()) {
result.concreteType = props->getConcreteType({}, term, protos, Context);
result.concreteType = props->getConcreteType({}, term, Context);
return result;
}

if (props->hasSuperclassBound()) {
result.superclass = props->getSuperclassBound({}, term, protos, Context);
result.superclass = props->getSuperclassBound({}, term, Context);
}

for (const auto *proto : props->getConformsToExcludingSuperclassConformances())
Expand Down Expand Up @@ -154,8 +152,7 @@ getSuperclassBound(Type depType,
if (!props->hasSuperclassBound())
return Type();

auto &protos = System.getProtocols();
return props->getSuperclassBound(genericParams, term, protos, Context);
return props->getSuperclassBound(genericParams, term, Context);
}

bool RequirementMachine::isConcreteType(Type depType) const {
Expand Down Expand Up @@ -186,8 +183,7 @@ getConcreteType(Type depType,
if (!props->isConcreteType())
return Type();

auto &protos = System.getProtocols();
return props->getConcreteType(genericParams, term, protos, Context);
return props->getConcreteType(genericParams, term, Context);
}

bool RequirementMachine::areSameTypeParameterInContext(Type depType1,
Expand Down Expand Up @@ -217,9 +213,6 @@ RequirementMachine::getLongestValidPrefix(const MutableTerm &term) const {
case Symbol::Kind::Protocol:
assert(prefix.empty() &&
"Protocol symbol can only appear at the start of a type term");
if (!System.getProtocols().isKnownProtocol(symbol.getProtocol()))
return prefix;

break;

case Symbol::Kind::GenericParam:
Expand All @@ -235,9 +228,6 @@ RequirementMachine::getLongestValidPrefix(const MutableTerm &term) const {
auto conformsTo = props->getConformsTo();

for (const auto *proto : symbol.getProtocols()) {
if (!System.getProtocols().isKnownProtocol(proto))
return prefix;

// T.[P:A] is valid iff T conforms to P.
if (std::find(conformsTo.begin(), conformsTo.end(), proto)
== conformsTo.end())
Expand Down Expand Up @@ -268,8 +258,6 @@ RequirementMachine::getLongestValidPrefix(const MutableTerm &term) const {
/// not considered canonical, since they can be replaced with their
/// concrete type).
bool RequirementMachine::isCanonicalTypeInContext(Type type) const {
auto &protos = System.getProtocols();

// Look for non-canonical type parameters.
return !type.findIf([&](Type component) -> bool {
if (!component->isTypeParameter())
Expand All @@ -288,7 +276,7 @@ bool RequirementMachine::isCanonicalTypeInContext(Type type) const {
if (props->isConcreteType())
return true;

auto anchor = Context.getTypeForTerm(term, {}, protos);
auto anchor = Context.getTypeForTerm(term, {});
return CanType(anchor) != CanType(component);
});
}
Expand All @@ -304,7 +292,6 @@ bool RequirementMachine::isCanonicalTypeInContext(Type type) const {
Type RequirementMachine::getCanonicalTypeInContext(
Type type,
TypeArrayView<GenericTypeParamType> genericParams) const {
const auto &protos = System.getProtocols();

return type.transformRec([&](Type t) -> Optional<Type> {
if (!t->isTypeParameter())
Expand Down Expand Up @@ -353,8 +340,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
if (props) {
if (props->isConcreteType()) {
auto concreteType = props->getConcreteType(genericParams,
prefix, protos,
Context);
prefix, Context);
if (!concreteType->hasTypeParameter())
return concreteType;

Expand All @@ -369,8 +355,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
if (props->hasSuperclassBound() &&
prefix.size() != term.size()) {
auto superclass = props->getSuperclassBound(genericParams,
prefix, protos,
Context);
prefix, Context);
if (!superclass->hasTypeParameter())
return superclass;

Expand All @@ -379,7 +364,7 @@ Type RequirementMachine::getCanonicalTypeInContext(
}
}

return Context.getTypeForTerm(prefix, genericParams, protos);
return Context.getTypeForTerm(prefix, genericParams);
}();

// If T is already valid, the longest valid prefix U of T is T itself, and
Expand All @@ -404,8 +389,7 @@ Type RequirementMachine::getCanonicalTypeInContext(

// Compute the type of the unresolved suffix term V, rooted in the
// generic parameter τ_0_0.
auto origType = Context.getRelativeTypeForTerm(
term, prefix, System.getProtocols());
auto origType = Context.getRelativeTypeForTerm(term, prefix);

// Substitute τ_0_0 in the above relative type with the concrete type
// for U.
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/RequirementMachine/HomotopyReduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ findRuleToDelete(bool firstPass,
const auto &otherRule = getRule(*found);

// Prefer to delete "less canonical" rules.
if (rule.compare(otherRule, Protos) > 0)
if (rule.compare(otherRule, Context) > 0)
found = ruleID;
}

Expand Down
8 changes: 2 additions & 6 deletions lib/AST/RequirementMachine/PropertyMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,11 @@ PropertyBag::getPrefixAfterStrippingKey(const MutableTerm &lookupTerm) const {
Type PropertyBag::getSuperclassBound(
TypeArrayView<GenericTypeParamType> genericParams,
const MutableTerm &lookupTerm,
const ProtocolGraph &protos,
RewriteContext &ctx) const {
MutableTerm prefix = getPrefixAfterStrippingKey(lookupTerm);
return ctx.getTypeFromSubstitutionSchema(Superclass->getSuperclass(),
Superclass->getSubstitutions(),
genericParams, prefix,
protos);
genericParams, prefix);
}

/// Get the concrete type of the term represented by this property bag.
Expand All @@ -173,13 +171,11 @@ Type PropertyBag::getSuperclassBound(
Type PropertyBag::getConcreteType(
TypeArrayView<GenericTypeParamType> genericParams,
const MutableTerm &lookupTerm,
const ProtocolGraph &protos,
RewriteContext &ctx) const {
MutableTerm prefix = getPrefixAfterStrippingKey(lookupTerm);
return ctx.getTypeFromSubstitutionSchema(ConcreteType->getConcreteType(),
ConcreteType->getSubstitutions(),
genericParams, prefix,
protos);
genericParams, prefix);
}

void PropertyBag::copyPropertiesFrom(const PropertyBag *next,
Expand Down
7 changes: 1 addition & 6 deletions lib/AST/RequirementMachine/PropertyMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
#include <vector>

#include "Debug.h"
#include "ProtocolGraph.h"
#include "RewriteContext.h"
#include "RewriteSystem.h"

Expand Down Expand Up @@ -102,7 +101,6 @@ class PropertyBag {
Type getSuperclassBound(
TypeArrayView<GenericTypeParamType> genericParams,
const MutableTerm &lookupTerm,
const ProtocolGraph &protos,
RewriteContext &ctx) const;

bool isConcreteType() const {
Expand All @@ -116,7 +114,6 @@ class PropertyBag {
Type getConcreteType(
TypeArrayView<GenericTypeParamType> genericParams,
const MutableTerm &lookupTerm,
const ProtocolGraph &protos,
RewriteContext &ctx) const;

LayoutConstraint getLayoutConstraint() const {
Expand Down Expand Up @@ -146,7 +143,6 @@ class PropertyMap {
using ConcreteTypeInDomain = std::pair<CanType, ArrayRef<const ProtocolDecl *>>;
llvm::DenseMap<ConcreteTypeInDomain, Term> ConcreteTypeInDomainMap;

const ProtocolGraph &Protos;
DebugOptions Debug;

PropertyBag *getOrCreateProperties(Term key);
Expand All @@ -159,8 +155,7 @@ class PropertyMap {
public:
explicit PropertyMap(RewriteSystem &system)
: Context(system.getRewriteContext()),
System(system),
Protos(system.getProtocols()) {
System(system) {
Debug = Context.getDebugOptions();
}

Expand Down
4 changes: 2 additions & 2 deletions lib/AST/RequirementMachine/PropertyUnification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ void PropertyMap::concretizeNestedTypesFromConcreteParent(
// PropertyBag::getConformsToExcludingSuperclassConformances().
conformances.push_back(concrete);

auto assocTypes = Protos.getProtocolInfo(proto).AssociatedTypes;
auto assocTypes = proto->getAssociatedTypeMembers();
if (assocTypes.empty())
continue;

Expand Down Expand Up @@ -543,7 +543,7 @@ void PropertyMap::concretizeNestedTypesFromConcreteParent(
auto term = Context.getRelativeTermForType(t->getCanonicalType(),
substitutions);
System.simplify(term);
return Context.getTypeForTerm(term, { }, Protos);
return Context.getTypeForTerm(term, { });
}));
};

Expand Down
Loading