Skip to content

Commit 1ca373a

Browse files
authored
Merge pull request swiftlang#62466 from slavapestov/simplify-subst-function-type-pattern-visitor
Simplify SubstFunctionTypePatternVisitor
2 parents 0fac90d + 38169af commit 1ca373a

14 files changed

+63
-150
lines changed

lib/SIL/IR/AbstractionPattern.cpp

Lines changed: 15 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,119 +1745,31 @@ class SubstFunctionTypePatternVisitor
17451745
}
17461746

17471747
auto decl = orig->getAnyNominal();
1748-
1749-
// Any type parameters we use as arguments to the nominal type must still
1750-
// satisfy the requirements on the nominal type. So we pull all of the
1751-
// generic requirements from the nominal type declaration into the
1752-
// substituted signature, then same-type-constrain those variables to the
1753-
// types we get by recursively visiting the bound generic arguments.
1748+
17541749
auto moduleDecl = decl->getParentModule();
1755-
auto origSubMap = orig->getContextSubstitutionMap(moduleDecl,
1756-
decl,
1757-
decl->getGenericEnvironment());
1758-
auto substSubMap = subst->getContextSubstitutionMap(moduleDecl, decl,
1759-
decl->getGenericEnvironment());
1760-
1761-
auto nomGenericSig = decl->getGenericSignature().getCanonicalSignature();
1762-
1763-
llvm::DenseMap<GenericTypeParamType*, GenericTypeParamType*>
1764-
substGPMapping;
1765-
1766-
// Create parallel generic params for the nominal type's generic params.
1767-
for (unsigned i = 0; i < nomGenericSig.getGenericParams().size(); ++i) {
1768-
auto nomTyParam = nomGenericSig.getGenericParams()[i];
1769-
// If the nominal type same-type constrains away this generic parameter,
1770-
// we don't need to visit it.
1771-
if (nomGenericSig->isConcreteType(nomTyParam))
1772-
continue;
1773-
1774-
unsigned substGPIndex = substGenericParams.size();
1775-
auto substGP = GenericTypeParamType::get(false, 0,
1776-
substGPIndex, TC.Context);
1777-
substGenericParams.push_back(substGP);
1778-
substReplacementTypes.push_back(Type());
1779-
substGPMapping.insert({nomGenericSig.getGenericParams()[i], substGP});
1780-
}
1750+
auto origSubMap = orig->getContextSubstitutionMap(moduleDecl, decl);
1751+
auto substSubMap = subst->getContextSubstitutionMap(moduleDecl, decl);
17811752

1782-
// Create parallel requirements too, mapping from the generic signature's
1783-
// params to our parallel params.
1784-
auto substGPMap = SubstitutionMap::get(nomGenericSig,
1785-
[&](SubstitutableType *dt) -> Type {
1786-
auto mapping = substGPMapping.find(cast<GenericTypeParamType>(dt));
1787-
assert(mapping != substGPMapping.end());
1788-
return mapping->second;
1789-
}, [&](CanType dependentType,
1790-
Type conformingReplacementType,
1791-
ProtocolDecl *conformedProtocol) -> ProtocolConformanceRef {
1792-
// We should always be substituting type parameter for type parameter
1793-
return ProtocolConformanceRef(conformedProtocol);
1794-
});
1795-
1796-
for (auto reqt : nomGenericSig.getRequirements()) {
1797-
auto firstTy = reqt.getFirstType().subst(substGPMap);
1798-
switch (auto kind = reqt.getKind()) {
1799-
case RequirementKind::SameShape:
1800-
llvm_unreachable("Same-shape requirement not supported here");
1801-
1802-
case RequirementKind::SameType:
1803-
// Skip same-type constraints that define away primary generic params,
1804-
// since we didn't duplicate those params.
1805-
if (reqt.getFirstType()->getAs<GenericTypeParamType>()
1806-
&& nomGenericSig->isConcreteType(reqt.getFirstType()))
1807-
continue;
1808-
1809-
LLVM_FALLTHROUGH;
1810-
case RequirementKind::Conformance:
1811-
case RequirementKind::Superclass: {
1812-
auto secondTy = reqt.getSecondType().subst(substGPMap);
1813-
substRequirements.push_back(Requirement(kind, firstTy, secondTy));
1814-
break;
1815-
}
1816-
case RequirementKind::Layout:
1817-
substRequirements.push_back(Requirement(kind, firstTy,
1818-
reqt.getLayoutConstraint()));
1819-
break;
1820-
}
1821-
}
1753+
auto nomGenericSig = decl->getGenericSignature();
18221754

1823-
// Now recur into the type arguments, and same-type-constrain the
1824-
// substituted type arguments to the parallel generic parameter, giving us
1825-
// the intersection of constraints on the type binding and the nominal type's
1826-
// requirements.
1827-
llvm::DenseMap<GenericTypeParamType*, Type>
1828-
newGPMapping;
1829-
1755+
TypeSubstitutionMap replacementTypes;
18301756
for (auto gp : nomGenericSig.getGenericParams()) {
1831-
if (nomGenericSig->isConcreteType(gp))
1832-
continue;
1833-
18341757
auto origParamTy = Type(gp).subst(origSubMap)
18351758
->getCanonicalType();
18361759
auto substParamTy = Type(gp).subst(substSubMap)
18371760
->getCanonicalType();
1838-
1839-
auto newParamTy = visit(substParamTy,
1840-
AbstractionPattern(origSig, origParamTy));
1841-
newGPMapping.insert({gp, newParamTy});
1842-
auto substGPTy = Type(gp).subst(substGPMap)->castTo<GenericTypeParamType>();
1843-
substRequirements.push_back(Requirement(RequirementKind::SameType,
1844-
newParamTy,
1845-
substGPTy));
1846-
assert(!substReplacementTypes[substGPTy->getIndex()]);
1847-
substReplacementTypes[substGPTy->getIndex()] = substParamTy;
1761+
1762+
replacementTypes[gp->getCanonicalType()->castTo<SubstitutableType>()]
1763+
= visit(substParamTy, AbstractionPattern(origSig, origParamTy));
18481764
}
1849-
1765+
18501766
auto newSubMap = SubstitutionMap::get(nomGenericSig,
1851-
[&](SubstitutableType *dt) -> Type {
1852-
auto mapping = newGPMapping.find(cast<GenericTypeParamType>(dt));
1853-
assert(mapping != newGPMapping.end());
1854-
return mapping->second;
1855-
}, [&](CanType dependentType,
1856-
Type conformingReplacementType,
1857-
ProtocolDecl *conformedProtocol) -> ProtocolConformanceRef {
1858-
// We should always be substituting type parameter for type parameter
1859-
return ProtocolConformanceRef(conformedProtocol);
1860-
});
1767+
QueryTypeSubstitutionMap{replacementTypes},
1768+
LookUpConformanceInModule(moduleDecl));
1769+
1770+
for (auto reqt : nomGenericSig.getRequirements()) {
1771+
substRequirements.push_back(reqt.subst(newSubMap));
1772+
}
18611773

18621774
return decl->getDeclaredInterfaceType().subst(newSubMap)->getCanonicalType();
18631775
}

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 725; // macro declarations
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 726; // SIL function lowering change
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///

test/SILGen/coroutine_subst_function_types.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class ConcreteWithInt: Generic<Int> {
6363
set {}
6464
}
6565

66-
// CHECK-LABEL: sil private [thunk] [ossa] @$s3mod15ConcreteWithIntC12complexTupleSiSg_SDySSSiGtvMAA7GenericCADxSg_SDySSxGtvMTV : $@yield_once @convention(method) @substituted <τ_0_0, τ_0_1, τ_0_2, τ_0_3, τ_0_4 where τ_0_0 == τ_0_1, τ_0_2 == String, τ_0_3 == τ_0_4> (@guaranteed ConcreteWithInt) -> @yields @inout (Optional<τ_0_0>, Dictionary<String, τ_0_3>) for <Int, Int, String, Int, Int>
66+
// CHECK-LABEL: sil private [thunk] [ossa] @$s3mod15ConcreteWithIntC12complexTupleSiSg_SDySSSiGtvMAA7GenericCADxSg_SDySSxGtvMTV : $@yield_once @convention(method) @substituted <τ_0_0, τ_0_1> (@guaranteed ConcreteWithInt) -> @yields @inout (Optional<τ_0_0>, Dictionary<String, τ_0_1>) for <Int, Int>
6767
override var complexTuple: (Int?, [String: Int]) {
6868
get { super.complexTuple }
6969
set {}
@@ -110,7 +110,7 @@ extension ConcreteWithInt : ProtoWithAssoc {
110110

111111
// var complexTuple
112112
// CHECK-LABEL: sil shared [ossa] @$s3mod15ConcreteWithIntC12complexTupleSiSg_SDySSSiGtvr : $@yield_once @convention(method) (@guaranteed ConcreteWithInt) -> (@yields Optional<Int>, @yields @guaranteed Dictionary<String, Int>)
113-
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s3mod15ConcreteWithIntCAA05ProtoC5AssocA2aDP12complexTuple0F0QzSg_SDySSAHGtvMTW : $@yield_once @convention(witness_method: ProtoWithAssoc) @substituted <τ_0_0, τ_0_1, τ_0_2, τ_0_3, τ_0_4, τ_0_5 where τ_0_1 == τ_0_2, τ_0_3 == String, τ_0_4 == τ_0_5> (@inout τ_0_0) -> @yields @inout (Optional<τ_0_1>, Dictionary<String, τ_0_4>) for <ConcreteWithInt, Int, Int, String, Int, Int>
113+
// CHECK-LABEL: sil private [transparent] [thunk] [ossa] @$s3mod15ConcreteWithIntCAA05ProtoC5AssocA2aDP12complexTuple0F0QzSg_SDySSAHGtvMTW : $@yield_once @convention(witness_method: ProtoWithAssoc) @substituted <τ_0_0, τ_0_1, τ_0_2> (@inout τ_0_0) -> @yields @inout (Optional<τ_0_1>, Dictionary<String, τ_0_2>) for <ConcreteWithInt, Int, Int>
114114
}
115115

116116
// CHECK-LABEL: sil_vtable ConcreteWithInt {

0 commit comments

Comments
 (0)