@@ -1129,8 +1129,9 @@ createStackAllocation(SILValue value) {
11291129
11301130 // For opened existential types, allocate stack space at the type
11311131 // definition. Allocating as early as possible provides more opportunity for
1132- // creating use projections into value.
1133- SILInstruction *firstOpeningInst = nullptr ;
1132+ // creating use projections into value. But allocation must be no earlier then
1133+ // the latest type definition.
1134+ SILInstruction *latestOpeningInst = nullptr ;
11341135 allocTy.getASTType ().visit ([&](CanType type) {
11351136 auto archetype = dyn_cast<ArchetypeType>(type);
11361137 if (!archetype)
@@ -1142,23 +1143,23 @@ createStackAllocation(SILValue value) {
11421143
11431144 auto *openingInst = openingVal->getDefiningInstruction ();
11441145 assert (openingVal && " all opened archetypes should be resolved" );
1145- if (firstOpeningInst
1146- && pass.domInfo ->dominates (firstOpeningInst, openingInst )) {
1146+ if (latestOpeningInst
1147+ && pass.domInfo ->dominates (openingInst, latestOpeningInst )) {
11471148 return ;
11481149 }
1149- firstOpeningInst = openingInst;
1150+ latestOpeningInst = openingInst;
11501151 }
11511152 });
1152- auto allocPt = firstOpeningInst ? std::next (firstOpeningInst ->getIterator ())
1153- : pass.function ->begin ()->begin ();
1153+ auto allocPt = latestOpeningInst ? std::next (latestOpeningInst ->getIterator ())
1154+ : pass.function ->begin ()->begin ();
11541155 auto allocBuilder = pass.getBuilder (allocPt);
11551156 AllocStackInst *alloc = allocBuilder.createAllocStack (pass.genLoc (), allocTy);
11561157
11571158 auto dealloc = [&](SILBasicBlock::iterator insertPt) {
11581159 auto deallocBuilder = pass.getBuilder (insertPt);
11591160 deallocBuilder.createDeallocStack (pass.genLoc (), alloc);
11601161 };
1161- if (firstOpeningInst ) {
1162+ if (latestOpeningInst ) {
11621163 // Deallocate at the dominance frontier to ensure that allocation encloses
11631164 // not only the uses of the current value, but also of any values reusing
11641165 // this storage as a use projection.
0 commit comments