Skip to content

Propagate function subtype conversion information down to closure emission #71833

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
42 changes: 23 additions & 19 deletions include/swift/SIL/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,6 +695,21 @@ enum class CaptureKind {
Immutable
};

/// Interesting information about the lowering of a function type.
struct FunctionTypeInfo {
/// The abstraction pattern that the type has been lowered under.
AbstractionPattern OrigType;

/// The formal type that the function is being used as. When this
/// type is used to specify a type context (e.g. as the contextual
/// type info of a closure; see `TypeConverter::getClosureTypeInfo`),
/// this may be a subtype of the closure's formal type.
CanAnyFunctionType FormalType;

/// The expected lowered type.
CanSILFunctionType ExpectedLoweredType;
};

/// TypeConverter - helper class for creating and managing TypeLowerings.
class TypeConverter {
friend class TypeLowering;
Expand Down Expand Up @@ -825,8 +840,7 @@ class TypeConverter {
/// Second element is a ResilienceExpansion.
llvm::DenseMap<std::pair<SILType, unsigned>, unsigned> TypeFields;

llvm::DenseMap<AbstractClosureExpr *, std::optional<AbstractionPattern>>
ClosureAbstractionPatterns;
llvm::DenseMap<AbstractClosureExpr *, FunctionTypeInfo> ClosureInfos;
llvm::DenseMap<SILDeclRef, TypeExpansionContext>
CaptureTypeExpansionContexts;

Expand Down Expand Up @@ -1226,27 +1240,17 @@ class TypeConverter {
SILType enumType,
EnumElementDecl *elt);

/// Get the preferred abstraction pattern, if any, by which to lower a
/// declaration.
///
/// This can be set using \c setAbstractionPattern , but only before
/// the abstraction pattern is queried using this function. Once the
/// abstraction pattern has been asked for, it may not be changed.
std::optional<AbstractionPattern>
getConstantAbstractionPattern(SILDeclRef constant);
TypeExpansionContext getCaptureTypeExpansionContext(SILDeclRef constant);

/// Set the preferred abstraction pattern for a closure.
///
/// The abstraction pattern can only be set before any calls to
/// \c getConstantAbstractionPattern on the same closure. It may not be
/// changed once it has been read.
void setAbstractionPattern(AbstractClosureExpr *closure,
AbstractionPattern pattern);

void setCaptureTypeExpansionContext(SILDeclRef constant,
SILModule &M);

const FunctionTypeInfo *getClosureTypeInfo(SILDeclRef constant);
const FunctionTypeInfo &getClosureTypeInfo(AbstractClosureExpr *closure);

void withClosureTypeInfo(AbstractClosureExpr *closure,
const FunctionTypeInfo &closureInfo,
llvm::function_ref<void()> operation);

void setLoweredAddresses();

private:
Expand Down
10 changes: 6 additions & 4 deletions lib/SIL/IR/SILFunctionType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3980,11 +3980,11 @@ static CanSILFunctionType getUncachedSILFunctionTypeForConstant(
auto proto = constant.getDecl()->getDeclContext()->getSelfProtocolDecl();
witnessMethodConformance = ProtocolConformanceRef(proto);
}

// Does this constant have a preferred abstraction pattern set?
AbstractionPattern origType = [&]{
if (auto abstraction = TC.getConstantAbstractionPattern(constant)) {
return *abstraction;
if (auto closureInfo = TC.getClosureTypeInfo(constant)) {
return closureInfo->OrigType;
} else {
return AbstractionPattern(origLoweredInterfaceType);
}
Expand Down Expand Up @@ -4036,6 +4036,9 @@ static CanSILFunctionType getUncachedSILFunctionTypeForConstant(
CanSILFunctionType TypeConverter::getUncachedSILFunctionTypeForConstant(
TypeExpansionContext context, SILDeclRef constant,
CanAnyFunctionType origInterfaceType) {
// This entrypoint is only used for computing a type for dynamic dispatch.
assert(!constant.getAbstractClosureExpr());

auto bridgedTypes = getLoweredFormalTypes(constant, origInterfaceType);
return ::getUncachedSILFunctionTypeForConstant(*this, context, constant,
bridgedTypes);
Expand Down Expand Up @@ -4168,7 +4171,6 @@ getLoweredResultIndices(const SILFunctionType *functionType,
numResults, resultIndices);
}


const SILConstantInfo &
TypeConverter::getConstantInfo(TypeExpansionContext expansion,
SILDeclRef constant) {
Expand Down
50 changes: 32 additions & 18 deletions lib/SIL/IR/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4784,15 +4784,40 @@ CanSILBoxType TypeConverter::getBoxTypeForEnumElement(
return boxTy;
}

std::optional<AbstractionPattern>
TypeConverter::getConstantAbstractionPattern(SILDeclRef constant) {
const FunctionTypeInfo *TypeConverter::getClosureTypeInfo(SILDeclRef constant) {
if (auto closure = constant.getAbstractClosureExpr()) {
// Using operator[] here creates an entry in the map if one doesn't exist
// yet, marking the fact that the lack of abstraction pattern has been
// established and cannot be overridden by `setAbstractionPattern` later.
return ClosureAbstractionPatterns[closure];
return &getClosureTypeInfo(closure);
}
return std::nullopt;
return nullptr;
}

const FunctionTypeInfo &
TypeConverter::getClosureTypeInfo(AbstractClosureExpr *closure) {
auto it = ClosureInfos.find(closure);
assert(it != ClosureInfos.end() &&
"looking for closure info for closure without any set");
return it->second;
}

void TypeConverter::withClosureTypeInfo(AbstractClosureExpr *closure,
const FunctionTypeInfo &info,
llvm::function_ref<void()> operation) {
auto insertResult = ClosureInfos.insert({closure, info});
(void) insertResult;
#ifndef NDEBUG
if (!insertResult.second) {
auto &existing = insertResult.first->second;
assert(existing.FormalType == info.FormalType);
assert(existing.ExpectedLoweredType == info.ExpectedLoweredType);
}
#endif

operation();

// TODO: figure out a way to clear this out so that emitting a closure
// doesn't require permanent memory use. Right now we have too much
// code relying on not emitting this in a scoped pattern.
//ClosureInfos.erase(closure);
}

TypeExpansionContext
Expand All @@ -4808,17 +4833,6 @@ TypeConverter::getCaptureTypeExpansionContext(SILDeclRef constant) {
return minimal;
}

void TypeConverter::setAbstractionPattern(AbstractClosureExpr *closure,
AbstractionPattern pattern) {
auto existing = ClosureAbstractionPatterns.find(closure);
if (existing != ClosureAbstractionPatterns.end()) {
assert(*existing->second == pattern
&& "closure shouldn't be emitted at different abstraction level contexts");
} else {
ClosureAbstractionPatterns[closure] = pattern;
}
}

void TypeConverter::setCaptureTypeExpansionContext(SILDeclRef constant,
SILModule &M) {
if (!hasLoweredLocalCaptures(constant)) {
Expand Down
Loading