Skip to content

SIL: More robust substituted function type lowering. #40112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,7 @@ class alignas(1 << TypeAlignInBits) TypeBase
CanType subst,
ArchetypeType *upperBound,
ArrayRef<ProtocolConformanceRef> substConformances)> substFn);

/// Determines whether this type is similar to \p other as defined by
/// \p matchOptions.
bool matches(Type other, TypeMatchOptions matchOptions);
Expand Down
27 changes: 27 additions & 0 deletions include/swift/SIL/AbstractionPattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -1406,6 +1406,33 @@ class AbstractionPattern {
/// is the corresponding value passed?
CallingConventionKind getParameterConvention(TypeConverter &TC) const;

/// Generate the abstraction pattern for lowering the substituted SIL
/// function type for a function type matching this abstraction pattern.
///
/// This abstraction pattern must be a function abstraction pattern, matching
/// \c substType .
///
/// Where the abstraction pattern involves substitutable types, in order
/// to minimize function conversions, we extract those positions out into
/// fresh generic arguments, with the minimum set of constraints necessary
/// to maintain the calling convention (such as passed-directly or
/// passed-indirectly) as well as satisfy requirements of where the generic
/// argument structurally appears in the type.
/// The goal is for similar-shaped generic function types to remain
/// canonically equivalent, like `(T, U) -> ()`, `(T, T) -> ()`,
/// `(U, T) -> ()` or `(T, T.A) -> ()` when given substitutions that produce
/// the same function types.
///
/// Returns a new AbstractionPattern to use for type lowering, as well as
/// the SubstitutionMap used to map `substType` into the new abstraction
/// pattern's generic environment, and the coroutine yield type mapped into
/// the generic environment of the new abstraction pattern.
std::tuple<AbstractionPattern, SubstitutionMap, AbstractionPattern>
getSubstFunctionTypePattern(CanAnyFunctionType substType,
TypeConverter &TC,
AbstractionPattern coroutineYieldOrigType,
CanType coroutineYieldSubstType) const;

void dump() const LLVM_ATTRIBUTE_USED;
void print(raw_ostream &OS) const;

Expand Down
8 changes: 5 additions & 3 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1738,7 +1738,8 @@ void IRGenerator::emitDynamicReplacements() {
llvm::SmallSet<OpaqueTypeArchetypeType *, 8> origUniqueOpaqueTypes;
for (auto *newFunc : DynamicReplacements) {
auto newResultTy = newFunc->getLoweredFunctionType()
->getAllResultsInterfaceType()
->getAllResultsSubstType(newFunc->getModule(),
TypeExpansionContext::minimal())
.getASTType();
if (!newResultTy->hasOpaqueArchetype())
continue;
Expand All @@ -1750,8 +1751,9 @@ void IRGenerator::emitDynamicReplacements() {
auto *origFunc = newFunc->getDynamicallyReplacedFunction();
assert(origFunc);
auto origResultTy = origFunc->getLoweredFunctionType()
->getAllResultsInterfaceType()
.getASTType();
->getAllResultsSubstType(origFunc->getModule(),
TypeExpansionContext::minimal())
.getASTType();
assert(origResultTy->hasOpaqueArchetype());
origResultTy.visit([&](CanType ty) {
if (auto opaque = ty->getAs<OpaqueTypeArchetypeType>())
Expand Down
3 changes: 2 additions & 1 deletion lib/IRGen/LoadableByAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,8 @@ SILArgument *LoadableStorageAllocation::replaceArgType(SILBuilder &argBuilder,
void LoadableStorageAllocation::insertIndirectReturnArgs() {
GenericEnvironment *genEnv = getSubstGenericEnvironment(pass.F);
auto loweredTy = pass.F->getLoweredFunctionType();
SILType resultStorageType = loweredTy->getAllResultsInterfaceType();
SILType resultStorageType = loweredTy->getAllResultsSubstType(pass.F->getModule(),
pass.F->getTypeExpansionContext());
auto canType = resultStorageType.getASTType();
if (canType->hasTypeParameter()) {
assert(genEnv && "Expected a GenericEnv");
Expand Down
Loading