Skip to content

Commit 617b25a

Browse files
authored
Merge pull request #67567 from nate-chandler/rdar112792831-2
[IRGen] alloc_stack may allocate pack metadata.
2 parents 1381d9a + 471c978 commit 617b25a

File tree

6 files changed

+54
-25
lines changed

6 files changed

+54
-25
lines changed

include/swift/AST/Types.h

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,13 @@ class RecursiveTypeProperties {
180180
/// they have a type variable originator.
181181
SolverAllocated = 0x8000,
182182

183-
/// This type contains a concrete pack.
184-
HasConcretePack = 0x10000,
183+
/// Contains a PackType.
184+
HasPack = 0x10000,
185185

186-
Last_Property = HasConcretePack
186+
/// Contains a PackArchetypeType.
187+
HasPackArchetype = 0x20000,
188+
189+
Last_Property = HasPackArchetype
187190
};
188191
enum { BitWidth = countBitsUsed(Property::Last_Property) };
189192

@@ -259,7 +262,9 @@ class RecursiveTypeProperties {
259262

260263
bool hasParameterPack() const { return Bits & HasParameterPack; }
261264

262-
bool hasConcretePack() const { return Bits & HasConcretePack; }
265+
bool hasPack() const { return Bits & HasPack; }
266+
267+
bool hasPackArchetype() const { return Bits & HasPackArchetype; }
263268

264269
/// Does a type with these properties structurally contain a
265270
/// parameterized existential type?
@@ -420,12 +425,12 @@ class alignas(1 << TypeAlignInBits) TypeBase
420425
NumProtocols : 16
421426
);
422427

423-
SWIFT_INLINE_BITFIELD_FULL(TypeVariableType, TypeBase, 7+30,
428+
SWIFT_INLINE_BITFIELD_FULL(TypeVariableType, TypeBase, 7+29,
424429
/// Type variable options.
425430
Options : 7,
426431
: NumPadBits,
427432
/// The unique number assigned to this type variable.
428-
ID : 30
433+
ID : 29
429434
);
430435

431436
SWIFT_INLINE_BITFIELD(SILFunctionType, TypeBase, NumSILExtInfoBits+1+4+1+2+1+1,
@@ -689,16 +694,26 @@ class alignas(1 << TypeAlignInBits) TypeBase
689694
return getRecursiveProperties().hasLocalArchetype();
690695
}
691696

697+
/// Whether the type contains a generic parameter declared as a parameter
698+
/// pack.
692699
bool hasParameterPack() const {
693700
return getRecursiveProperties().hasParameterPack();
694701
}
695702

696-
bool hasConcretePack() const {
697-
return getRecursiveProperties().hasConcretePack();
703+
/// Whether the type contains a PackType.
704+
bool hasPack() const {
705+
return getRecursiveProperties().hasPack();
698706
}
699707

700-
/// Whether the type has some flavor of pack.
701-
bool hasPack() const { return hasParameterPack() || hasConcretePack(); }
708+
/// Whether the type contains a PackArchetypeType.
709+
bool hasPackArchetype() const {
710+
return getRecursiveProperties().hasPackArchetype();
711+
}
712+
713+
/// Whether the type has any flavor of pack.
714+
bool hasAnyPack() const {
715+
return hasParameterPack() || hasPack() || hasPackArchetype();
716+
}
702717

703718
/// Determine whether the type involves a parameterized existential type.
704719
bool hasParameterizedExistential() const {

include/swift/SIL/SILType.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,15 @@ class SILType {
377377
/// pack.
378378
bool hasParameterPack() const { return getASTType()->hasParameterPack(); }
379379

380-
/// Whether the type contains a concrete pack.
381-
bool hasConcretePack() const { return getASTType()->hasConcretePack(); }
382-
383-
/// Whether the type contains some flavor of pack.
380+
/// Whether the type contains a PackType.
384381
bool hasPack() const { return getASTType()->hasPack(); }
385382

383+
/// Whether the type contains a PackArchetypeType.
384+
bool hasPackArchetype() const { return getASTType()->hasPackArchetype(); }
385+
386+
/// Whether the type contains any flavor of pack.
387+
bool hasAnyPack() const { return getASTType()->hasAnyPack(); }
388+
386389
/// True if the type is an empty tuple or an empty struct or a tuple or
387390
/// struct containing only empty types.
388391
bool isEmpty(const SILFunction &F) const;

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3415,7 +3415,7 @@ CanPackType CanPackType::get(const ASTContext &C,
34153415
}
34163416

34173417
PackType *PackType::get(const ASTContext &C, ArrayRef<Type> elements) {
3418-
RecursiveTypeProperties properties = RecursiveTypeProperties::HasConcretePack;
3418+
RecursiveTypeProperties properties = RecursiveTypeProperties::HasPack;
34193419
bool isCanonical = true;
34203420
for (Type eltTy : elements) {
34213421
assert(!eltTy->is<PackType>() &&
@@ -3455,7 +3455,7 @@ void PackType::Profile(llvm::FoldingSetNodeID &ID, ArrayRef<Type> Elements) {
34553455

34563456
CanSILPackType SILPackType::get(const ASTContext &C, ExtInfo info,
34573457
ArrayRef<CanType> elements) {
3458-
RecursiveTypeProperties properties = RecursiveTypeProperties::HasConcretePack;
3458+
RecursiveTypeProperties properties;
34593459
for (CanType eltTy : elements) {
34603460
assert(!isa<SILPackType>(eltTy) &&
34613461
"Cannot have pack directly inside another pack");
@@ -4047,7 +4047,7 @@ isAnyFunctionTypeCanonical(ArrayRef<AnyFunctionType::Param> params,
40474047
static RecursiveTypeProperties
40484048
getGenericFunctionRecursiveProperties(ArrayRef<AnyFunctionType::Param> params,
40494049
Type result) {
4050-
static_assert(RecursiveTypeProperties::BitWidth == 17,
4050+
static_assert(RecursiveTypeProperties::BitWidth == 18,
40514051
"revisit this if you add new recursive type properties");
40524052
RecursiveTypeProperties properties;
40534053

@@ -4689,7 +4689,7 @@ CanSILFunctionType SILFunctionType::get(
46894689
void *mem = ctx.Allocate(bytes, alignof(SILFunctionType));
46904690

46914691
RecursiveTypeProperties properties;
4692-
static_assert(RecursiveTypeProperties::BitWidth == 17,
4692+
static_assert(RecursiveTypeProperties::BitWidth == 18,
46934693
"revisit this if you add new recursive type properties");
46944694
for (auto &param : params)
46954695
properties |= param.getInterfaceType()->getRecursiveProperties();

lib/AST/Type.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3703,8 +3703,9 @@ PackArchetypeType::PackArchetypeType(
37033703
ArrayRef<ProtocolDecl *> ConformsTo, Type Superclass,
37043704
LayoutConstraint Layout, PackShape Shape)
37053705
: ArchetypeType(TypeKind::PackArchetype, Ctx,
3706-
RecursiveTypeProperties::HasArchetype, InterfaceType,
3707-
ConformsTo, Superclass, Layout, GenericEnv) {
3706+
RecursiveTypeProperties::HasArchetype |
3707+
RecursiveTypeProperties::HasPackArchetype,
3708+
InterfaceType, ConformsTo, Superclass, Layout, GenericEnv) {
37083709
assert(InterfaceType->isParameterPack());
37093710
*getTrailingObjects<PackShape>() = Shape;
37103711
}

lib/SIL/IR/SILInstruction.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,11 +1287,11 @@ bool SILInstruction::mayRequirePackMetadata() const {
12871287
case SILInstructionKind::TryApplyInst: {
12881288
// Check the function type for packs.
12891289
auto apply = ApplySite::isa(const_cast<SILInstruction *>(this));
1290-
if (apply.getCallee()->getType().hasPack())
1290+
if (apply.getCallee()->getType().hasAnyPack())
12911291
return true;
12921292
// Check the substituted types for packs.
12931293
for (auto ty : apply.getSubstitutionMap().getReplacementTypes()) {
1294-
if (ty->hasPack())
1294+
if (ty->hasAnyPack())
12951295
return true;
12961296
}
12971297
return false;
@@ -1302,16 +1302,20 @@ bool SILInstruction::mayRequirePackMetadata() const {
13021302
case SILInstructionKind::DestroyValueInst:
13031303
// Unary instructions.
13041304
{
1305-
return getOperand(0)->getType().hasPack();
1305+
return getOperand(0)->getType().hasAnyPack();
1306+
}
1307+
case SILInstructionKind::AllocStackInst: {
1308+
auto *asi = cast<AllocStackInst>(this);
1309+
return asi->getType().hasAnyPack();
13061310
}
13071311
case SILInstructionKind::MetatypeInst: {
13081312
auto *mi = cast<MetatypeInst>(this);
1309-
return mi->getType().hasPack();
1313+
return mi->getType().hasAnyPack();
13101314
}
13111315
case SILInstructionKind::WitnessMethodInst: {
13121316
auto *wmi = cast<WitnessMethodInst>(this);
13131317
auto ty = wmi->getLookupType();
1314-
return ty->hasPack();
1318+
return ty->hasAnyPack();
13151319
}
13161320
default:
13171321
return false;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// RUN: %target-swift-frontend -emit-ir %s
2+
3+
// Verify that we don't hit the `Instruction missing on-stack pack metadata cleanups!` assertion.
4+
5+
// For alloc_stacks of tuples featuring a pack.
6+
public func tupleExpansionWithMemberType<each T: Sequence>(seqs: (repeat each T), elts: (repeat (each T).Element)) {}

0 commit comments

Comments
 (0)