@@ -1129,8 +1129,9 @@ createStackAllocation(SILValue value) {
1129
1129
1130
1130
// For opened existential types, allocate stack space at the type
1131
1131
// 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 ;
1134
1135
allocTy.getASTType ().visit ([&](CanType type) {
1135
1136
auto archetype = dyn_cast<ArchetypeType>(type);
1136
1137
if (!archetype)
@@ -1142,23 +1143,23 @@ createStackAllocation(SILValue value) {
1142
1143
1143
1144
auto *openingInst = openingVal->getDefiningInstruction ();
1144
1145
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 )) {
1147
1148
return ;
1148
1149
}
1149
- firstOpeningInst = openingInst;
1150
+ latestOpeningInst = openingInst;
1150
1151
}
1151
1152
});
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 ();
1154
1155
auto allocBuilder = pass.getBuilder (allocPt);
1155
1156
AllocStackInst *alloc = allocBuilder.createAllocStack (pass.genLoc (), allocTy);
1156
1157
1157
1158
auto dealloc = [&](SILBasicBlock::iterator insertPt) {
1158
1159
auto deallocBuilder = pass.getBuilder (insertPt);
1159
1160
deallocBuilder.createDeallocStack (pass.genLoc (), alloc);
1160
1161
};
1161
- if (firstOpeningInst ) {
1162
+ if (latestOpeningInst ) {
1162
1163
// Deallocate at the dominance frontier to ensure that allocation encloses
1163
1164
// not only the uses of the current value, but also of any values reusing
1164
1165
// this storage as a use projection.
0 commit comments