Skip to content

Commit e545d7f

Browse files
committed
Lift getCanonicalTypeInContext up to GenericSignature
1 parent e1be618 commit e545d7f

File tree

14 files changed

+69
-85
lines changed

14 files changed

+69
-85
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,10 @@ class GenericSignature {
209209
SmallVector<Requirement, 4>
210210
requirementsNotSatisfiedBy(GenericSignature otherSig) const;
211211

212+
/// Return the canonical version of the given type under this generic
213+
/// signature.
214+
CanType getCanonicalTypeInContext(Type type) const;
215+
212216
/// Check invariants.
213217
void verify() const;
214218
};
@@ -381,10 +385,6 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
381385
bool isRequirementSatisfied(
382386
Requirement requirement, bool allowMissing = false) const;
383387

384-
/// Return the canonical version of the given type under this generic
385-
/// signature.
386-
CanType getCanonicalTypeInContext(Type type) const;
387-
388388
bool isCanonicalTypeInContext(Type type) const;
389389
bool isCanonicalTypeInContext(Type type,
390390
GenericSignatureBuilder &builder) const;
@@ -470,6 +470,10 @@ class alignas(1 << TypeAlignInBits) GenericSignatureImpl final
470470
/// equivalent to or a subset of the generic parameters in this signature.
471471
SmallVector<Requirement, 4>
472472
requirementsNotSatisfiedBy(GenericSignature otherSig) const;
473+
474+
/// Return the canonical version of the given type under this generic
475+
/// signature.
476+
CanType getCanonicalTypeInContext(Type type) const;
473477
};
474478

475479
void simple_display(raw_ostream &out, GenericSignature sig);

include/swift/SIL/AbstractionPattern.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,7 +530,7 @@ class AbstractionPattern {
530530
OrigType = origType;
531531
GenericSig = CanGenericSignature();
532532
if (OrigType->hasTypeParameter()) {
533-
assert(OrigType == signature->getCanonicalTypeInContext(origType));
533+
assert(OrigType == signature.getCanonicalTypeInContext(origType));
534534
GenericSig = signature;
535535
}
536536
}

include/swift/SILOptimizer/Analysis/DifferentiableActivityAnalysis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ class DifferentiableActivityInfo {
135135
bool hasTangentSpace(SILValue value) {
136136
auto type = value->getType().getASTType();
137137
// Remap archetypes in the derivative generic signature, if it exists.
138-
if (derivativeGenericSignature && type->hasArchetype()) {
139-
type = derivativeGenericSignature->getCanonicalTypeInContext(
138+
if (type->hasArchetype()) {
139+
type = derivativeGenericSignature.getCanonicalTypeInContext(
140140
type->mapTypeOutOfContext());
141141
}
142142
// Look up conformance in the current module.

lib/AST/GenericSignature.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,15 @@ bool GenericSignatureImpl::isCanonicalTypeInContext(
10811081
});
10821082
}
10831083

1084+
CanType GenericSignature::getCanonicalTypeInContext(Type type) const {
1085+
// The null generic signature has no requirements so cannot influence the
1086+
// structure of the can type computed here.
1087+
if (isNull()) {
1088+
return type->getCanonicalType();
1089+
}
1090+
return getPointer()->getCanonicalTypeInContext(type);
1091+
}
1092+
10841093
CanType GenericSignatureImpl::getCanonicalTypeInContext(Type type) const {
10851094
type = type->getCanonicalType();
10861095

lib/AST/SubstitutionMap.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ Type SubstitutionMap::lookupSubstitution(CanSubstitutableType type) const {
295295

296296
// The generic parameter may not be canonical. Retrieve the canonical
297297
// type, which will be dependent.
298-
CanType canonicalType = genericSig->getCanonicalTypeInContext(genericParam);
298+
CanType canonicalType = genericSig.getCanonicalTypeInContext(genericParam);
299299

300300
// If nothing changed, we don't have a replacement.
301301
if (canonicalType == type) return Type();

lib/AST/Type.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,10 +1450,7 @@ CanType TypeBase::computeCanonicalType() {
14501450
}
14511451

14521452
CanType TypeBase::getCanonicalType(GenericSignature sig) {
1453-
if (!sig)
1454-
return getCanonicalType();
1455-
1456-
return sig->getCanonicalTypeInContext(this);
1453+
return sig.getCanonicalTypeInContext(this);
14571454
}
14581455

14591456
TypeBase *TypeBase::reconstituteSugar(bool Recursive) {
@@ -1904,7 +1901,7 @@ class IsBindableVisitor
19041901

19051902
// Collect requirements from the conformance not satisfied by the
19061903
// original declaration.
1907-
for (auto reqt : conformanceSig->requirementsNotSatisfiedBy(genericSig)) {
1904+
for (auto reqt : conformanceSig.requirementsNotSatisfiedBy(genericSig)) {
19081905
LLVM_DEBUG(llvm::dbgs() << "\n- adds requirement\n";
19091906
reqt.dump(llvm::dbgs()));
19101907

lib/SIL/IR/AbstractionPattern.cpp

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@ TypeConverter::getAbstractionPattern(AbstractStorageDecl *decl,
4747

4848
AbstractionPattern
4949
TypeConverter::getAbstractionPattern(SubscriptDecl *decl, bool isNonObjC) {
50-
auto type = decl->getElementInterfaceType()->getCanonicalType();
51-
CanGenericSignature genericSig;
52-
if (auto sig = decl->getGenericSignatureOfContext()) {
53-
genericSig = sig.getCanonicalSignature();
54-
type = sig->getCanonicalTypeInContext(type);
55-
}
56-
return AbstractionPattern(genericSig, type);
50+
auto sig = decl->getGenericSignatureOfContext().getCanonicalSignature();
51+
auto type = sig.getCanonicalTypeInContext(decl->getElementInterfaceType());
52+
return AbstractionPattern(sig, type);
5753
}
5854

5955
static const clang::Type *getClangType(const clang::Decl *decl) {
@@ -78,30 +74,27 @@ static Bridgeability getClangDeclBridgeability(const clang::Decl *decl) {
7874

7975
AbstractionPattern
8076
TypeConverter::getAbstractionPattern(VarDecl *var, bool isNonObjC) {
81-
CanType swiftType = var->getInterfaceType()
82-
->getCanonicalType();
83-
84-
CanGenericSignature genericSig;
85-
if (auto sig = var->getDeclContext()->getGenericSignatureOfContext()) {
86-
genericSig = sig.getCanonicalSignature();
87-
swiftType = genericSig->getCanonicalTypeInContext(swiftType);
88-
}
77+
auto sig = var->getDeclContext()
78+
->getGenericSignatureOfContext()
79+
.getCanonicalSignature();
80+
auto swiftType = sig.getCanonicalTypeInContext(var->getInterfaceType());
8981

9082
if (isNonObjC)
91-
return AbstractionPattern(genericSig, swiftType);
83+
return AbstractionPattern(sig, swiftType);
9284

9385
if (auto clangDecl = var->getClangDecl()) {
9486
auto clangType = getClangType(clangDecl);
9587
auto contextType = var->getDeclContext()->mapTypeIntoContext(swiftType);
96-
swiftType = getLoweredBridgedType(
97-
AbstractionPattern(genericSig, swiftType, clangType),
98-
contextType, getClangDeclBridgeability(clangDecl),
99-
SILFunctionTypeRepresentation::CFunctionPointer,
100-
TypeConverter::ForMemory)->getCanonicalType();
101-
return AbstractionPattern(genericSig, swiftType, clangType);
88+
swiftType =
89+
getLoweredBridgedType(AbstractionPattern(sig, swiftType, clangType),
90+
contextType, getClangDeclBridgeability(clangDecl),
91+
SILFunctionTypeRepresentation::CFunctionPointer,
92+
TypeConverter::ForMemory)
93+
->getCanonicalType();
94+
return AbstractionPattern(sig, swiftType, clangType);
10295
}
10396

104-
return AbstractionPattern(genericSig, swiftType);
97+
return AbstractionPattern(sig, swiftType);
10598
}
10699

107100
AbstractionPattern TypeConverter::getAbstractionPattern(EnumElementDecl *decl) {
@@ -113,15 +106,12 @@ AbstractionPattern TypeConverter::getAbstractionPattern(EnumElementDecl *decl) {
113106
"Optional.Some does not have a unique abstraction pattern because "
114107
"optionals are re-abstracted");
115108

116-
CanType type = decl->getArgumentInterfaceType()->getCanonicalType();
117-
118-
CanGenericSignature genericSig;
119-
if (auto sig = decl->getParentEnum()->getGenericSignatureOfContext()) {
120-
genericSig = sig.getCanonicalSignature();
121-
type = genericSig->getCanonicalTypeInContext(type);
122-
}
109+
auto sig = decl->getParentEnum()
110+
->getGenericSignatureOfContext()
111+
.getCanonicalSignature();
112+
auto type = sig.getCanonicalTypeInContext(decl->getArgumentInterfaceType());
123113

124-
return AbstractionPattern(genericSig, type);
114+
return AbstractionPattern(sig, type);
125115
}
126116

127117
AbstractionPattern::EncodedForeignInfo

lib/SIL/IR/TypeLowering.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,11 +3402,11 @@ TypeConverter::getInterfaceBoxTypeForCapture(ValueDecl *captured,
34023402

34033403
// Instantiate the layout with identity substitutions.
34043404
auto subMap = SubstitutionMap::get(
3405-
signature,
3406-
[&](SubstitutableType *type) -> Type {
3407-
return signature->getCanonicalTypeInContext(type);
3408-
},
3409-
MakeAbstractConformanceForGenericType());
3405+
signature,
3406+
[&](SubstitutableType *type) -> Type {
3407+
return signature.getCanonicalTypeInContext(type);
3408+
},
3409+
MakeAbstractConformanceForGenericType());
34103410

34113411
auto boxTy = SILBoxType::get(C, layout, subMap);
34123412
#ifndef NDEBUG

lib/SILOptimizer/Differentiation/JVPCloner.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ class JVPCloner::Implementation final
352352
/// Find the tangent space of a given canonical type.
353353
Optional<TangentSpace> getTangentSpace(CanType type) {
354354
// Use witness generic signature to remap types.
355-
if (auto witnessGenSig = witness->getDerivativeGenericSignature())
356-
type = witnessGenSig->getCanonicalTypeInContext(type);
355+
type = witness->getDerivativeGenericSignature().getCanonicalTypeInContext(
356+
type);
357357
return type->getAutoDiffTangentSpace(
358358
LookUpConformanceInModule(getModule().getSwiftModule()));
359359
}

lib/SILOptimizer/Differentiation/PullbackCloner.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ class PullbackCloner::Implementation final
211211

212212
Optional<TangentSpace> getTangentSpace(CanType type) {
213213
// Use witness generic signature to remap types.
214-
if (auto witnessGenSig = getWitness()->getDerivativeGenericSignature())
215-
type = witnessGenSig->getCanonicalTypeInContext(type);
214+
type =
215+
getWitness()->getDerivativeGenericSignature().getCanonicalTypeInContext(
216+
type);
216217
return type->getAutoDiffTangentSpace(
217218
LookUpConformanceInModule(getModule().getSwiftModule()));
218219
}

lib/SILOptimizer/Utils/Generics.cpp

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -802,23 +802,14 @@ ReabstractionInfo::createSubstitutedType(SILFunction *OrigF,
802802
auto CanSpecializedGenericSig = SpecializedGenericSig.getCanonicalSignature();
803803

804804
// First substitute concrete types into the existing function type.
805-
CanSILFunctionType FnTy;
806-
{
807-
FnTy = OrigF->getLoweredFunctionType()
808-
->substGenericArgs(M, SubstMap, getResilienceExpansion())
809-
->getUnsubstitutedType(M);
810-
// FIXME: Some of the added new requirements may not have been taken into
811-
// account by the substGenericArgs. So, canonicalize in the context of the
812-
// specialized signature.
813-
if (CanSpecializedGenericSig)
814-
FnTy = cast<SILFunctionType>(
815-
CanSpecializedGenericSig->getCanonicalTypeInContext(FnTy));
816-
else {
817-
FnTy = cast<SILFunctionType>(FnTy->getCanonicalType());
818-
assert(!FnTy->hasTypeParameter() && "Type parameters outside generic context?");
819-
}
820-
}
805+
CanSILFunctionType FnTy =
806+
cast<SILFunctionType>(CanSpecializedGenericSig.getCanonicalTypeInContext(
807+
OrigF->getLoweredFunctionType()
808+
->substGenericArgs(M, SubstMap, getResilienceExpansion())
809+
->getUnsubstitutedType(M)));
821810
assert(FnTy);
811+
assert((CanSpecializedGenericSig || !FnTy->hasTypeParameter()) &&
812+
"Type parameters outside generic context?");
822813

823814
// Use the new specialized generic signature.
824815
auto NewFnTy = SILFunctionType::get(
@@ -1439,8 +1430,7 @@ void FunctionSignaturePartialSpecializer::
14391430
createGenericParamsForCalleeGenericParams() {
14401431
for (auto GP : CalleeGenericSig.getGenericParams()) {
14411432
auto CanTy = GP->getCanonicalType();
1442-
auto CanTyInContext =
1443-
CalleeGenericSig->getCanonicalTypeInContext(CanTy);
1433+
auto CanTyInContext = CalleeGenericSig.getCanonicalTypeInContext(CanTy);
14441434
auto Replacement = CanTyInContext.subst(CalleeInterfaceToCallerArchetypeMap);
14451435
LLVM_DEBUG(llvm::dbgs() << "\n\nChecking callee generic parameter:\n";
14461436
CanTy->dump(llvm::dbgs()));

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4024,15 +4024,6 @@ static bool checkFunctionSignature(
40244024
return false;
40254025
}
40264026

4027-
// Map type into the required function type's generic signature, if it exists.
4028-
// This is significant when the required generic signature has same-type
4029-
// requirements while the candidate generic signature does not.
4030-
auto mapType = [&](Type type) {
4031-
if (!requiredGenSig)
4032-
return type->getCanonicalType();
4033-
return requiredGenSig->getCanonicalTypeInContext(type);
4034-
};
4035-
40364027
// Check that parameter types match, disregarding labels.
40374028
if (required->getNumParams() != candidateFnTy->getNumParams())
40384029
return false;
@@ -4041,14 +4032,16 @@ static bool checkFunctionSignature(
40414032
[&](AnyFunctionType::Param x, AnyFunctionType::Param y) {
40424033
auto xInstanceTy = x.getOldType()->getMetatypeInstanceType();
40434034
auto yInstanceTy = y.getOldType()->getMetatypeInstanceType();
4044-
return xInstanceTy->isEqual(mapType(yInstanceTy));
4035+
return xInstanceTy->isEqual(
4036+
requiredGenSig.getCanonicalTypeInContext(yInstanceTy));
40454037
}))
40464038
return false;
40474039

40484040
// If required result type is not a function type, check that result types
40494041
// match exactly.
40504042
auto requiredResultFnTy = dyn_cast<AnyFunctionType>(required.getResult());
4051-
auto candidateResultTy = mapType(candidateFnTy.getResult());
4043+
auto candidateResultTy =
4044+
requiredGenSig.getCanonicalTypeInContext(candidateFnTy.getResult());
40524045
if (!requiredResultFnTy) {
40534046
auto requiredResultTupleTy = dyn_cast<TupleType>(required.getResult());
40544047
auto candidateResultTupleTy = dyn_cast<TupleType>(candidateResultTy);

lib/Sema/TypeCheckProtocolInference.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ Type AssociatedTypeInference::computeFixedTypeWitness(
827827
continue;
828828

829829
auto structuralTy = DependentMemberType::get(selfTy, assocType->getName());
830-
const auto ty = sig->getCanonicalTypeInContext(structuralTy);
830+
const auto ty = sig.getCanonicalTypeInContext(structuralTy);
831831

832832
// A dependent member type with an identical base and name indicates that
833833
// the protocol does not same-type constrain it in any way; move on to

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,8 +338,8 @@ bool TypeResolution::areSameType(Type type1, Type type2) const {
338338
type2->isTypeParameter()) {
339339
return genericSig->areSameTypeParameterInContext(type1, type2);
340340
}
341-
return genericSig->getCanonicalTypeInContext(type1)
342-
== genericSig->getCanonicalTypeInContext(type2);
341+
return genericSig.getCanonicalTypeInContext(type1) ==
342+
genericSig.getCanonicalTypeInContext(type2);
343343
}
344344

345345
// Otherwise, perform a structural check.

0 commit comments

Comments
 (0)