From 3af67e30db2fc8b650299d9d0797eeb8dcdfe1e8 Mon Sep 17 00:00:00 2001 From: John McCall Date: Wed, 5 Apr 2023 16:22:51 -0400 Subject: [PATCH 1/2] [NFC] Convert this code to use canonical types --- include/swift/SIL/TypeLowering.h | 10 +++--- lib/SIL/IR/TypeLowering.cpp | 55 +++++++++++++++++--------------- 2 files changed, 35 insertions(+), 30 deletions(-) diff --git a/include/swift/SIL/TypeLowering.h b/include/swift/SIL/TypeLowering.h index c279683c7c063..0848ac1ffd124 100644 --- a/include/swift/SIL/TypeLowering.h +++ b/include/swift/SIL/TypeLowering.h @@ -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)> isLeafAggregate, - std::function)> visit); #endif diff --git a/lib/SIL/IR/TypeLowering.cpp b/lib/SIL/IR/TypeLowering.cpp index db35876500a6d..759b1ce0b9bbd 100644 --- a/lib/SIL/IR/TypeLowering.cpp +++ b/lib/SIL/IR/TypeLowering.cpp @@ -2712,31 +2712,32 @@ TypeConverter::getTypeLowering(AbstractionPattern origType, #ifndef NDEBUG bool TypeConverter::visitAggregateLeaves( - Lowering::AbstractionPattern origType, Type substType, + Lowering::AbstractionPattern origType, CanType substType, TypeExpansionContext context, - std::function)> isLeafAggregate, - std::function)> visit) { - llvm::SmallSet, 16> visited; + llvm::SmallSet, 16> visited; llvm::SmallVector< - std::tuple, 16> + std::tuple, 16> worklist; auto insertIntoWorklist = [&visited, - &worklist](Type substTy, AbstractionPattern origTy, + &worklist](CanType substTy, + AbstractionPattern origTy, ValueDecl *field, Optional 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> { - TypeBase *ty; + -> std::tuple> { + CanType ty; AbstractionPattern origTy = AbstractionPattern::getOpaque(); ValueDecl *field; unsigned index; @@ -2744,34 +2745,35 @@ bool TypeConverter::visitAggregateLeaves( Optional 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() || ty->is() || ty->getEnumOrBoundGenericEnum() || - ty->getStructOrBoundGenericStruct(); + auto isAggregate = [](CanType ty) { + return isa(ty) || + isa(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 index; std::tie(ty, origTy, field, index) = popFromWorklist(); if (isAggregate(ty) && !isLeafAggregate(ty, origTy, field, index)) { - if (auto packTy = ty->getAs()) { + if (auto packTy = dyn_cast(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()) { + } else if (auto tupleTy = dyn_cast(ty)) { unsigned tupleIndex = 0; origTy.forEachExpandedTupleElement( - CanTupleType(tupleTy), + tupleTy, [&](auto origElementTy, auto substElementTy, auto element) { substElementTy = substOpaqueTypesWithUnderlyingTypes(substElementTy, context); @@ -2779,7 +2781,7 @@ bool TypeConverter::visitAggregateLeaves( tupleIndex); ++tupleIndex; }); - } else if (auto *decl = ty->getStructOrBoundGenericStruct()) { + } else if (auto *decl = ty.getStructOrBoundGenericStruct()) { for (auto *structField : decl->getStoredProperties()) { auto subMap = ty->getContextSubstitutionMap(&M, decl); auto substFieldTy = @@ -2794,7 +2796,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()) @@ -2827,16 +2829,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(); }; @@ -2865,7 +2868,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); From ab9e9cbe81fc35b5fd4bb7ea3e28cadd55e022ba Mon Sep 17 00:00:00 2001 From: John McCall Date: Wed, 5 Apr 2023 16:52:21 -0400 Subject: [PATCH 2/2] Walk into pack expansions in this verifier. Fixes rdar://107664237 --- lib/SIL/IR/TypeLowering.cpp | 5 +++++ test/SILGen/variadic-generic-tuples.swift | 3 +++ 2 files changed, 8 insertions(+) diff --git a/lib/SIL/IR/TypeLowering.cpp b/lib/SIL/IR/TypeLowering.cpp index 759b1ce0b9bbd..426ed4ed4dc83 100644 --- a/lib/SIL/IR/TypeLowering.cpp +++ b/lib/SIL/IR/TypeLowering.cpp @@ -2750,6 +2750,7 @@ bool TypeConverter::visitAggregateLeaves( auto isAggregate = [](CanType ty) { return isa(ty) || isa(ty) || + isa(ty) || ty.getEnumOrBoundGenericEnum() || ty.getStructOrBoundGenericStruct(); }; @@ -2781,6 +2782,10 @@ bool TypeConverter::visitAggregateLeaves( tupleIndex); ++tupleIndex; }); + } else if (auto expansion = dyn_cast(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); diff --git a/test/SILGen/variadic-generic-tuples.swift b/test/SILGen/variadic-generic-tuples.swift index d8f1d332a3aa5..646153d1ed334 100644 --- a/test/SILGen/variadic-generic-tuples.swift +++ b/test/SILGen/variadic-generic-tuples.swift @@ -331,3 +331,6 @@ func testFancyTuple_concrete() { func testFancyTuple_pack(values: repeat each T) { FancyTuple(x: (1, "hi", repeat each values, false)).makeTuple() } + +// rdar://107664237 +func f() -> (repeat Array) {}