Skip to content

Commit 1e7562a

Browse files
committed
[GSB] Simplify the anchor cache to always rely on term rewriting.
Use term rewriting exclusively in the computation of the canonical dependent type (anchor) for an equivalence class, eliminating any dependence on the specific potential archetypes and eliminating the hacks around generic type parameters.
1 parent f2bef2d commit 1e7562a

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

include/swift/AST/GenericSignatureBuilder.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,8 @@ class GenericSignatureBuilder {
273273
/// The cached anchor itself.
274274
Type anchor;
275275

276-
/// The number of members of the equivalence class when the archetype
277-
/// anchor was cached.
278-
unsigned numMembers;
276+
/// The generation at which the anchor was last computed.
277+
unsigned lastGeneration;
279278
} archetypeAnchorCache;
280279

281280
/// Describes a cached nested type.

lib/AST/GenericSignatureBuilder.cpp

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,12 +2118,10 @@ TypeDecl *EquivalenceClass::lookupNestedType(
21182118
Type EquivalenceClass::getAnchor(
21192119
GenericSignatureBuilder &builder,
21202120
TypeArrayView<GenericTypeParamType> genericParams) {
2121-
// Check whether the cache is valid.
2122-
if (archetypeAnchorCache.anchor &&
2123-
archetypeAnchorCache.numMembers == members.size()) {
2124-
++NumArchetypeAnchorCacheHits;
2121+
// Substitute into the anchor with the given generic parameters.
2122+
auto substAnchor = [&] {
2123+
if (genericParams.empty()) return archetypeAnchorCache.anchor;
21252124

2126-
// Reparent the anchor using genericParams.
21272125
return archetypeAnchorCache.anchor.subst(
21282126
[&](SubstitutableType *dependentType) {
21292127
if (auto gp = dyn_cast<GenericTypeParamType>(dependentType)) {
@@ -2135,30 +2133,28 @@ Type EquivalenceClass::getAnchor(
21352133
return Type(dependentType);
21362134
},
21372135
MakeAbstractConformanceForGenericType());
2138-
}
21392136

2140-
// Map the members of this equivalence class to the best associated type
2141-
// within that equivalence class.
2142-
llvm::SmallDenseMap<EquivalenceClass *, AssociatedTypeDecl *> nestedTypes;
2143-
2144-
Type bestGenericParam;
2145-
for (auto member : members) {
2146-
// If the member is a generic parameter, keep the best generic parameter.
2147-
if (member->isGenericParam()) {
2148-
Type genericParamType = member->getDependentType(genericParams);
2149-
if (!bestGenericParam ||
2150-
compareDependentTypes(genericParamType, bestGenericParam) < 0)
2151-
bestGenericParam = genericParamType;
2152-
continue;
2153-
}
2137+
};
21542138

2155-
// If we saw a generic parameter, ignore any nested types.
2156-
if (bestGenericParam) continue;
2139+
// Check whether the cache is valid.
2140+
if (archetypeAnchorCache.anchor &&
2141+
archetypeAnchorCache.lastGeneration == builder.Impl->Generation) {
2142+
++NumArchetypeAnchorCacheHits;
2143+
return substAnchor();
21572144
}
21582145

2159-
// If we found a generic parameter, return that.
2160-
if (bestGenericParam)
2161-
return bestGenericParam;
2146+
// Check whether we already have an anchor, in which case we
2147+
// can simplify it further.
2148+
if (archetypeAnchorCache.anchor) {
2149+
// Record the cache miss.
2150+
++NumArchetypeAnchorCacheMisses;
2151+
2152+
// Update the anchor by simplifying it further.
2153+
archetypeAnchorCache.anchor =
2154+
builder.getCanonicalTypeParameter(archetypeAnchorCache.anchor);
2155+
archetypeAnchorCache.lastGeneration = builder.Impl->Generation;
2156+
return substAnchor();
2157+
}
21622158

21632159
// Form the anchor.
21642160
for (auto member : members) {
@@ -2169,8 +2165,8 @@ Type EquivalenceClass::getAnchor(
21692165
// Record the cache miss and update the cache.
21702166
++NumArchetypeAnchorCacheMisses;
21712167
archetypeAnchorCache.anchor = anchorType;
2172-
archetypeAnchorCache.numMembers = members.size();
2173-
return anchorType;
2168+
archetypeAnchorCache.lastGeneration = builder.Impl->Generation;
2169+
return substAnchor();
21742170
}
21752171

21762172
llvm_unreachable("Unable to compute anchor");

0 commit comments

Comments
 (0)