3535using namespace swift ;
3636using namespace irgen ;
3737
38+ static void cleanupTypeMetadataPackImpl (IRGenFunction &IGF, StackAddress pack,
39+ llvm::Value *shape);
40+ static void cleanupWitnessTablePackImpl (IRGenFunction &IGF, StackAddress pack,
41+ llvm::Value *shape);
42+
3843static CanPackArchetypeType
3944getForwardedPackArchetypeType (CanPackType packType) {
4045 if (auto expansion = packType.unwrapSingletonPackExpansion ())
@@ -454,6 +459,7 @@ irgen::emitTypeMetadataPack(IRGenFunction &IGF, CanPackType packType,
454459 assert (packType->getNumElements () == constantInt->getValue ());
455460 auto pack =
456461 StackAddress (emitFixedSizeMetadataPackRef (IGF, packType, request));
462+ IGF.recordStackPackMetadataAlloc (pack, constantInt);
457463 return {pack, constantInt};
458464 }
459465
@@ -488,6 +494,7 @@ irgen::emitTypeMetadataPack(IRGenFunction &IGF, CanPackType packType,
488494 };
489495
490496 visitPackExplosion (IGF, packType, visitFn);
497+ IGF.recordStackPackMetadataAlloc (pack, shape);
491498
492499 return {pack, shape};
493500}
@@ -599,6 +606,7 @@ irgen::emitWitnessTablePack(IRGenFunction &IGF, CanPackType packType,
599606 assert (packType->getNumElements () == constantInt->getValue ());
600607 auto pack = StackAddress (
601608 emitFixedSizeWitnessTablePack (IGF, packType, packConformance));
609+ IGF.recordStackPackWitnessTableAlloc (pack, constantInt);
602610 return {pack, constantInt};
603611 }
604612
@@ -635,12 +643,14 @@ irgen::emitWitnessTablePack(IRGenFunction &IGF, CanPackType packType,
635643 };
636644
637645 visitPackExplosion (IGF, packType, visitFn);
646+ IGF.recordStackPackWitnessTableAlloc (pack, shape);
638647
639648 return {pack, shape};
640649}
641650
642- void irgen::cleanupWitnessTablePack (IRGenFunction &IGF, StackAddress pack,
643- llvm::Value *shape) {
651+ static void cleanupWitnessTablePackImpl (IRGenFunction &IGF, StackAddress pack,
652+ llvm::Value *shape) {
653+
644654 if (pack.getExtraInfo ()) {
645655 IGF.emitDeallocateDynamicAlloca (pack);
646656 } else if (auto count = countForShape (shape)) {
@@ -649,6 +659,61 @@ void irgen::cleanupWitnessTablePack(IRGenFunction &IGF, StackAddress pack,
649659 }
650660}
651661
662+ void irgen::cleanupWitnessTablePack (IRGenFunction &IGF, StackAddress pack,
663+ llvm::Value *shape) {
664+ cleanupWitnessTablePackImpl (IGF, pack, shape);
665+ IGF.eraseStackPackWitnessTableAlloc (pack, shape);
666+ }
667+
668+ void irgen::cleanupStackAllocPacks (IRGenFunction &IGF,
669+ ArrayRef<StackPackAlloc> allocs) {
670+ for (auto alloc : llvm::reverse (allocs)) {
671+ StackAddress addr;
672+ uint8_t kind;
673+ llvm::Value *shape;
674+ std::tie (addr, shape, kind) = alloc;
675+
676+ switch ((GenericRequirement::Kind)kind) {
677+ case GenericRequirement::Kind::MetadataPack:
678+ cleanupTypeMetadataPackImpl (IGF, addr, shape);
679+ break ;
680+ case GenericRequirement::Kind::WitnessTablePack:
681+ cleanupWitnessTablePackImpl (IGF, addr, shape);
682+ break ;
683+ default :
684+ llvm_unreachable (" bad requirement in stack pack alloc" );
685+ }
686+ }
687+ }
688+
689+ void IRGenFunction::recordStackPackMetadataAlloc (StackAddress addr,
690+ llvm::Value *shape) {
691+ OutstandingStackPackAllocs.insert (
692+ {addr, shape, (uint8_t )GenericRequirement::Kind::MetadataPack});
693+ }
694+
695+ void IRGenFunction::eraseStackPackMetadataAlloc (StackAddress addr,
696+ llvm::Value *shape) {
697+ auto removed = OutstandingStackPackAllocs.remove (
698+ {addr, shape, (uint8_t )GenericRequirement::Kind::MetadataPack});
699+ assert (removed && " erased stack pack metadata addr that wasn't recorded!?" );
700+ (void )removed;
701+ }
702+
703+ void IRGenFunction::recordStackPackWitnessTableAlloc (StackAddress addr,
704+ llvm::Value *shape) {
705+ OutstandingStackPackAllocs.insert (
706+ {addr, shape, (uint8_t )GenericRequirement::Kind::WitnessTablePack});
707+ }
708+
709+ void IRGenFunction::eraseStackPackWitnessTableAlloc (StackAddress addr,
710+ llvm::Value *shape) {
711+ auto removed = OutstandingStackPackAllocs.remove (
712+ {addr, shape, (uint8_t )GenericRequirement::Kind::WitnessTablePack});
713+ assert (removed && " erased stack pack metadata addr that wasn't recorded!?" );
714+ (void )removed;
715+ }
716+
652717llvm::Value *irgen::emitWitnessTablePackRef (IRGenFunction &IGF,
653718 CanPackType packType,
654719 PackConformance *conformance) {
@@ -1045,8 +1110,8 @@ void irgen::bindOpenedElementArchetypesAtIndex(IRGenFunction &IGF,
10451110 }
10461111}
10471112
1048- void irgen::cleanupTypeMetadataPack (IRGenFunction &IGF, StackAddress pack,
1049- llvm::Value *shape) {
1113+ static void cleanupTypeMetadataPackImpl (IRGenFunction &IGF, StackAddress pack,
1114+ llvm::Value *shape) {
10501115 if (pack.getExtraInfo ()) {
10511116 IGF.emitDeallocateDynamicAlloca (pack);
10521117 } else if (auto count = countForShape (shape)) {
@@ -1055,6 +1120,12 @@ void irgen::cleanupTypeMetadataPack(IRGenFunction &IGF, StackAddress pack,
10551120 }
10561121}
10571122
1123+ void irgen::cleanupTypeMetadataPack (IRGenFunction &IGF, StackAddress pack,
1124+ llvm::Value *shape) {
1125+ cleanupTypeMetadataPackImpl (IGF, pack, shape);
1126+ IGF.eraseStackPackMetadataAlloc (pack, shape);
1127+ }
1128+
10581129Address irgen::emitStorageAddressOfPackElement (IRGenFunction &IGF, Address pack,
10591130 llvm::Value *index,
10601131 SILType elementType,
0 commit comments