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
}

0 commit comments

Comments
 (0)