Skip to content

Commit d787293

Browse files
committed
IRGen: Fix another out-of-order task_dealloc with parameter pack metadata
1 parent 75f7134 commit d787293

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

lib/IRGen/GenPack.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,8 @@ irgen::emitPackArchetypeMetadataRef(IRGenFunction &IGF,
195195

196196
static Address emitFixedSizeMetadataPackRef(IRGenFunction &IGF,
197197
CanPackType packType,
198-
DynamicMetadataRequest request) {
198+
DynamicMetadataRequest request,
199+
llvm::ConstantInt *constantInt) {
199200
assert(!packType->containsPackExpansionType());
200201

201202
unsigned elementCount = packType->getNumElements();
@@ -206,6 +207,10 @@ static Address emitFixedSizeMetadataPackRef(IRGenFunction &IGF,
206207
IGF.Builder.CreateLifetimeStart(pack,
207208
IGF.IGM.getPointerSize() * elementCount);
208209

210+
// Record this before making recursive calls to emitTypeMetadataRef(),
211+
// to ensure we maintain correct stack discipline.
212+
IGF.recordStackPackMetadataAlloc(pack, constantInt);
213+
209214
for (unsigned i : indices(packType->getElementTypes())) {
210215
Address slot = IGF.Builder.CreateStructGEP(
211216
pack, i, IGF.IGM.getPointerSize());
@@ -461,8 +466,8 @@ irgen::emitTypeMetadataPack(IRGenFunction &IGF, CanPackType packType,
461466
if (auto *constantInt = dyn_cast<llvm::ConstantInt>(shape)) {
462467
assert(packType->getNumElements() == constantInt->getValue());
463468
auto pack =
464-
StackAddress(emitFixedSizeMetadataPackRef(IGF, packType, request));
465-
IGF.recordStackPackMetadataAlloc(pack, constantInt);
469+
StackAddress(emitFixedSizeMetadataPackRef(IGF, packType, request,
470+
constantInt));
466471
return {pack, constantInt};
467472
}
468473

@@ -471,6 +476,10 @@ irgen::emitTypeMetadataPack(IRGenFunction &IGF, CanPackType packType,
471476
IGF.IGM.getPointerAlignment(),
472477
/*allowTaskAlloc=*/true);
473478

479+
// Record this before making recursive calls to emitTypeMetadataRef(),
480+
// to ensure we maintain correct stack discipline.
481+
IGF.recordStackPackMetadataAlloc(pack, shape);
482+
474483
auto visitFn =
475484
[&](CanType eltTy, unsigned staticIndex,
476485
llvm::Value *dynamicIndex,
@@ -497,7 +506,6 @@ irgen::emitTypeMetadataPack(IRGenFunction &IGF, CanPackType packType,
497506
};
498507

499508
visitPackExplosion(IGF, packType, visitFn);
500-
IGF.recordStackPackMetadataAlloc(pack, shape);
501509

502510
return {pack, shape};
503511
}
@@ -537,7 +545,8 @@ irgen::emitTypeMetadataPackRef(IRGenFunction &IGF, CanPackType packType,
537545

538546
static Address emitFixedSizeWitnessTablePack(IRGenFunction &IGF,
539547
CanPackType packType,
540-
PackConformance *packConformance) {
548+
PackConformance *packConformance,
549+
llvm::ConstantInt *constantInt) {
541550
assert(!packType->containsPackExpansionType());
542551

543552
unsigned elementCount = packType->getNumElements();
@@ -548,6 +557,10 @@ static Address emitFixedSizeWitnessTablePack(IRGenFunction &IGF,
548557
IGF.Builder.CreateLifetimeStart(pack,
549558
IGF.IGM.getPointerSize() * elementCount);
550559

560+
// Record this before making recursive calls to emitTypeMetadataRef(),
561+
// to ensure we maintain correct stack discipline.
562+
IGF.recordStackPackWitnessTableAlloc(pack, constantInt);
563+
551564
for (unsigned i : indices(packType->getElementTypes())) {
552565
Address slot =
553566
IGF.Builder.CreateStructGEP(pack, i, IGF.IGM.getPointerSize());
@@ -607,8 +620,7 @@ irgen::emitWitnessTablePack(IRGenFunction &IGF, CanPackType packType,
607620
if (auto *constantInt = dyn_cast<llvm::ConstantInt>(shape)) {
608621
assert(packType->getNumElements() == constantInt->getValue());
609622
auto pack = StackAddress(
610-
emitFixedSizeWitnessTablePack(IGF, packType, packConformance));
611-
IGF.recordStackPackWitnessTableAlloc(pack, constantInt);
623+
emitFixedSizeWitnessTablePack(IGF, packType, packConformance, constantInt));
612624
return {pack, constantInt};
613625
}
614626

@@ -617,6 +629,10 @@ irgen::emitWitnessTablePack(IRGenFunction &IGF, CanPackType packType,
617629
IGF.IGM.getPointerAlignment(),
618630
/*allowTaskAlloc=*/true);
619631

632+
// Record this before making recursive calls to emitTypeMetadataRef(),
633+
// to ensure we maintain correct stack discipline.
634+
IGF.recordStackPackWitnessTableAlloc(pack, shape);
635+
620636
auto index = 0;
621637
auto visitFn = [&](CanType eltTy, unsigned staticIndex,
622638
llvm::Value *dynamicIndex, llvm::Value *dynamicLength) {
@@ -645,7 +661,6 @@ irgen::emitWitnessTablePack(IRGenFunction &IGF, CanPackType packType,
645661
};
646662

647663
visitPackExplosion(IGF, packType, visitFn);
648-
IGF.recordStackPackWitnessTableAlloc(pack, shape);
649664

650665
return {pack, shape};
651666
}
@@ -1437,6 +1452,11 @@ irgen::emitInducedTupleTypeMetadataPack(
14371452
auto pack = IGF.emitDynamicAlloca(IGF.IGM.TypeMetadataPtrTy, shape,
14381453
IGF.IGM.getPointerAlignment(),
14391454
/*allowTaskAlloc=*/true);
1455+
1456+
// Record this before making recursive calls to emitTypeMetadataRef(),
1457+
// to ensure we maintain correct stack discipline.
1458+
IGF.recordStackPackMetadataAlloc(pack, shape);
1459+
14401460
auto elementForIndex =
14411461
[&](llvm::Value *index) -> llvm::Value * {
14421462
return irgen::emitTupleTypeMetadataElementType(IGF, tupleMetadata, index);
@@ -1446,8 +1466,6 @@ irgen::emitInducedTupleTypeMetadataPack(
14461466
emitPackExpansionPack(IGF, pack.getAddress(), index, shape,
14471467
elementForIndex);
14481468

1449-
IGF.recordStackPackMetadataAlloc(pack, shape);
1450-
14511469
return {pack, shape};
14521470
}
14531471

0 commit comments

Comments
 (0)