Skip to content

Commit c45583a

Browse files
authored
Merge pull request #40112 from jckarter/subst-type-lowering-by-abstraction-pattern
SIL: More robust substituted function type lowering.
2 parents eda0080 + 5c404ac commit c45583a

25 files changed

+667
-405
lines changed

include/swift/AST/Types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ class alignas(1 << TypeAlignInBits) TypeBase
953953
CanType subst,
954954
ArchetypeType *upperBound,
955955
ArrayRef<ProtocolConformanceRef> substConformances)> substFn);
956-
956+
957957
/// Determines whether this type is similar to \p other as defined by
958958
/// \p matchOptions.
959959
bool matches(Type other, TypeMatchOptions matchOptions);

include/swift/SIL/AbstractionPattern.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1406,6 +1406,33 @@ class AbstractionPattern {
14061406
/// is the corresponding value passed?
14071407
CallingConventionKind getParameterConvention(TypeConverter &TC) const;
14081408

1409+
/// Generate the abstraction pattern for lowering the substituted SIL
1410+
/// function type for a function type matching this abstraction pattern.
1411+
///
1412+
/// This abstraction pattern must be a function abstraction pattern, matching
1413+
/// \c substType .
1414+
///
1415+
/// Where the abstraction pattern involves substitutable types, in order
1416+
/// to minimize function conversions, we extract those positions out into
1417+
/// fresh generic arguments, with the minimum set of constraints necessary
1418+
/// to maintain the calling convention (such as passed-directly or
1419+
/// passed-indirectly) as well as satisfy requirements of where the generic
1420+
/// argument structurally appears in the type.
1421+
/// The goal is for similar-shaped generic function types to remain
1422+
/// canonically equivalent, like `(T, U) -> ()`, `(T, T) -> ()`,
1423+
/// `(U, T) -> ()` or `(T, T.A) -> ()` when given substitutions that produce
1424+
/// the same function types.
1425+
///
1426+
/// Returns a new AbstractionPattern to use for type lowering, as well as
1427+
/// the SubstitutionMap used to map `substType` into the new abstraction
1428+
/// pattern's generic environment, and the coroutine yield type mapped into
1429+
/// the generic environment of the new abstraction pattern.
1430+
std::tuple<AbstractionPattern, SubstitutionMap, AbstractionPattern>
1431+
getSubstFunctionTypePattern(CanAnyFunctionType substType,
1432+
TypeConverter &TC,
1433+
AbstractionPattern coroutineYieldOrigType,
1434+
CanType coroutineYieldSubstType) const;
1435+
14091436
void dump() const LLVM_ATTRIBUTE_USED;
14101437
void print(raw_ostream &OS) const;
14111438

lib/IRGen/GenDecl.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,8 @@ void IRGenerator::emitDynamicReplacements() {
17381738
llvm::SmallSet<OpaqueTypeArchetypeType *, 8> origUniqueOpaqueTypes;
17391739
for (auto *newFunc : DynamicReplacements) {
17401740
auto newResultTy = newFunc->getLoweredFunctionType()
1741-
->getAllResultsInterfaceType()
1741+
->getAllResultsSubstType(newFunc->getModule(),
1742+
TypeExpansionContext::minimal())
17421743
.getASTType();
17431744
if (!newResultTy->hasOpaqueArchetype())
17441745
continue;
@@ -1750,8 +1751,9 @@ void IRGenerator::emitDynamicReplacements() {
17501751
auto *origFunc = newFunc->getDynamicallyReplacedFunction();
17511752
assert(origFunc);
17521753
auto origResultTy = origFunc->getLoweredFunctionType()
1753-
->getAllResultsInterfaceType()
1754-
.getASTType();
1754+
->getAllResultsSubstType(origFunc->getModule(),
1755+
TypeExpansionContext::minimal())
1756+
.getASTType();
17551757
assert(origResultTy->hasOpaqueArchetype());
17561758
origResultTy.visit([&](CanType ty) {
17571759
if (auto opaque = ty->getAs<OpaqueTypeArchetypeType>())

lib/IRGen/LoadableByAddress.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1401,7 +1401,8 @@ SILArgument *LoadableStorageAllocation::replaceArgType(SILBuilder &argBuilder,
14011401
void LoadableStorageAllocation::insertIndirectReturnArgs() {
14021402
GenericEnvironment *genEnv = getSubstGenericEnvironment(pass.F);
14031403
auto loweredTy = pass.F->getLoweredFunctionType();
1404-
SILType resultStorageType = loweredTy->getAllResultsInterfaceType();
1404+
SILType resultStorageType = loweredTy->getAllResultsSubstType(pass.F->getModule(),
1405+
pass.F->getTypeExpansionContext());
14051406
auto canType = resultStorageType.getASTType();
14061407
if (canType->hasTypeParameter()) {
14071408
assert(genEnv && "Expected a GenericEnv");

0 commit comments

Comments
 (0)