Skip to content

[5.9] Fix the type-lowering verifier to handle pack expansions #64965

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
10 changes: 6 additions & 4 deletions include/swift/SIL/TypeLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -1260,14 +1260,16 @@ class TypeConverter {
/// Check the result of
/// getTypeLowering(AbstractionPattern,Type,TypeExpansionContext).
void verifyLowering(const TypeLowering &, AbstractionPattern origType,
Type origSubstType, TypeExpansionContext forExpansion);
CanType origSubstType,
TypeExpansionContext forExpansion);
bool
visitAggregateLeaves(Lowering::AbstractionPattern origType, Type substType,
visitAggregateLeaves(Lowering::AbstractionPattern origType,
CanType substType,
TypeExpansionContext context,
std::function<bool(Type, Lowering::AbstractionPattern,
std::function<bool(CanType, Lowering::AbstractionPattern,
ValueDecl *, Optional<unsigned>)>
isLeafAggregate,
std::function<bool(Type, Lowering::AbstractionPattern,
std::function<bool(CanType, Lowering::AbstractionPattern,
ValueDecl *, Optional<unsigned>)>
visit);
#endif
Expand Down
60 changes: 34 additions & 26 deletions lib/SIL/IR/TypeLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2712,74 +2712,81 @@ TypeConverter::getTypeLowering(AbstractionPattern origType,

#ifndef NDEBUG
bool TypeConverter::visitAggregateLeaves(
Lowering::AbstractionPattern origType, Type substType,
Lowering::AbstractionPattern origType, CanType substType,
TypeExpansionContext context,
std::function<bool(Type, Lowering::AbstractionPattern, ValueDecl *,
std::function<bool(CanType, Lowering::AbstractionPattern, ValueDecl *,
Optional<unsigned>)>
isLeafAggregate,
std::function<bool(Type, Lowering::AbstractionPattern, ValueDecl *,
std::function<bool(CanType, Lowering::AbstractionPattern, ValueDecl *,
Optional<unsigned>)>
visit) {
llvm::SmallSet<std::tuple<TypeBase *, ValueDecl *, unsigned>, 16> visited;
llvm::SmallSet<std::tuple<CanType, ValueDecl *, unsigned>, 16> visited;
llvm::SmallVector<
std::tuple<TypeBase *, AbstractionPattern, ValueDecl *, unsigned>, 16>
std::tuple<CanType, AbstractionPattern, ValueDecl *, unsigned>, 16>
worklist;
auto insertIntoWorklist = [&visited,
&worklist](Type substTy, AbstractionPattern origTy,
&worklist](CanType substTy,
AbstractionPattern origTy,
ValueDecl *field,
Optional<unsigned> maybeIndex) -> bool {
unsigned index = maybeIndex.value_or(UINT_MAX);
if (!visited.insert({substTy.getPointer(), field, index}).second)
if (!visited.insert({substTy, field, index}).second)
return false;
worklist.push_back({substTy.getPointer(), origTy, field, index});
worklist.push_back({substTy, origTy, field, index});
return true;
};
auto popFromWorklist = [&worklist]()
-> std::tuple<Type, AbstractionPattern, ValueDecl *, Optional<unsigned>> {
TypeBase *ty;
-> std::tuple<CanType, AbstractionPattern, ValueDecl *, Optional<unsigned>> {
CanType ty;
AbstractionPattern origTy = AbstractionPattern::getOpaque();
ValueDecl *field;
unsigned index;
std::tie(ty, origTy, field, index) = worklist.pop_back_val();
Optional<unsigned> maybeIndex;
if (index != UINT_MAX)
maybeIndex = {index};
return {ty->getCanonicalType(), origTy, field, index};
return {ty, origTy, field, index};
};
auto isAggregate = [](Type ty) {
return ty->is<SILPackType>() || ty->is<TupleType>() || ty->getEnumOrBoundGenericEnum() ||
ty->getStructOrBoundGenericStruct();
auto isAggregate = [](CanType ty) {
return isa<SILPackType>(ty) ||
isa<TupleType>(ty) ||
isa<PackExpansionType>(ty) ||
ty.getEnumOrBoundGenericEnum() ||
ty.getStructOrBoundGenericStruct();
};
insertIntoWorklist(substType, origType, nullptr, llvm::None);
while (!worklist.empty()) {
Type ty;
CanType ty;
AbstractionPattern origTy = AbstractionPattern::getOpaque();
ValueDecl *field;
Optional<unsigned> index;
std::tie(ty, origTy, field, index) = popFromWorklist();
if (isAggregate(ty) && !isLeafAggregate(ty, origTy, field, index)) {
if (auto packTy = ty->getAs<SILPackType>()) {
if (auto packTy = dyn_cast<SILPackType>(ty)) {
for (auto packIndex : indices(packTy->getElementTypes())) {
auto origElementTy = origTy.getPackElementType(packIndex);
auto substElementTy =
packTy->getElementType(packIndex)->getCanonicalType();
auto substElementTy = packTy.getElementType(packIndex);
substElementTy =
computeLoweredRValueType(context, origElementTy, substElementTy);
insertIntoWorklist(substElementTy, origElementTy, nullptr,
packIndex);
}
} else if (auto tupleTy = ty->getAs<TupleType>()) {
} else if (auto tupleTy = dyn_cast<TupleType>(ty)) {
unsigned tupleIndex = 0;
origTy.forEachExpandedTupleElement(
CanTupleType(tupleTy),
tupleTy,
[&](auto origElementTy, auto substElementTy, auto element) {
substElementTy =
substOpaqueTypesWithUnderlyingTypes(substElementTy, context);
insertIntoWorklist(substElementTy, origElementTy, nullptr,
tupleIndex);
++tupleIndex;
});
} else if (auto *decl = ty->getStructOrBoundGenericStruct()) {
} else if (auto expansion = dyn_cast<PackExpansionType>(ty)) {
insertIntoWorklist(expansion.getPatternType(),
origTy.getPackExpansionPatternType(),
field, index);
} else if (auto *decl = ty.getStructOrBoundGenericStruct()) {
for (auto *structField : decl->getStoredProperties()) {
auto subMap = ty->getContextSubstitutionMap(&M, decl);
auto substFieldTy =
Expand All @@ -2794,7 +2801,7 @@ bool TypeConverter::visitAggregateLeaves(
insertIntoWorklist(substFieldTy, origFieldType, structField,
llvm::None);
}
} else if (auto *decl = ty->getEnumOrBoundGenericEnum()) {
} else if (auto *decl = ty.getEnumOrBoundGenericEnum()) {
auto subMap = ty->getContextSubstitutionMap(&M, decl);
for (auto *element : decl->getAllElements()) {
if (!element->hasAssociatedValues())
Expand Down Expand Up @@ -2827,16 +2834,17 @@ bool TypeConverter::visitAggregateLeaves(
}

void TypeConverter::verifyLowering(const TypeLowering &lowering,
AbstractionPattern origType, Type substType,
AbstractionPattern origType,
CanType substType,
TypeExpansionContext forExpansion) {
// Non-trivial lowerings should always be lexical unless all non-trivial
// fields are eager move.
if (!lowering.isTrivial() && !lowering.isLexical()) {
if (lowering.getRecursiveProperties().isInfinite())
return;
auto getLifetimeAnnotation = [](Type ty) -> LifetimeAnnotation {
auto getLifetimeAnnotation = [](CanType ty) -> LifetimeAnnotation {
NominalTypeDecl *nominal;
if (!(nominal = ty->getAnyNominal()))
if (!(nominal = ty.getAnyNominal()))
return LifetimeAnnotation::None;
return nominal->getLifetimeAnnotation();
};
Expand Down Expand Up @@ -2865,7 +2873,7 @@ void TypeConverter::verifyLowering(const TypeLowering &lowering,

// If the leaf is the whole type, verify that it is annotated
// @_eagerMove.
if (ty->getCanonicalType() == substType->getCanonicalType())
if (ty == substType)
return getLifetimeAnnotation(ty) == LifetimeAnnotation::EagerMove;

auto &tyLowering = getTypeLowering(origTy, ty, forExpansion);
Expand Down
3 changes: 3 additions & 0 deletions test/SILGen/variadic-generic-tuples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,6 @@ func testFancyTuple_concrete() {
func testFancyTuple_pack<each T>(values: repeat each T) {
FancyTuple<Int, String, repeat each T, Bool>(x: (1, "hi", repeat each values, false)).makeTuple()
}

// rdar://107664237
func f<each T>() -> (repeat Array<each T>) {}