@@ -1199,46 +1199,25 @@ void AvailableValueAggregator::addHandOffCopyDestroysForPhis(
1199
1199
// no further work to do.
1200
1200
auto *loadOperand = &load->getAllOperands ()[0 ];
1201
1201
LinearLifetimeChecker checker (visitedBlocks, deadEndBlocks);
1202
- bool didInsertDestroy = false ;
1203
1202
bool consumedInLoop = checker.completeConsumingUseSet (
1204
1203
phi, loadOperand, leakingBlocks, [&](SILBasicBlock::iterator iter) {
1205
1204
SILBuilderWithScope builder (iter);
1206
1205
builder.emitDestroyValueOperation (loc, phi);
1207
- didInsertDestroy = true ;
1208
1206
});
1209
1207
1210
- // If we did not insert any destroy_values, we either:
1211
- //
1212
- // 1. Are consuming the value in a loop, but the loop is an infinite loop
1213
- // so we shouldn't insert a destroy.
1214
- //
1215
- // 2. Are not consuming the value in the loop and our phi is strongly
1216
- // control equivalent with the load_borrow, so we insert a destroy
1217
- // /after/ the load_borrow.
1218
- if (!didInsertDestroy) {
1219
- if (!consumedInLoop) {
1220
- auto next = std::next (load->getIterator ());
1221
- SILBuilderWithScope builder (next);
1222
- builder.emitDestroyValueOperation (next->getLoc (), phi);
1223
- }
1224
- continue ;
1225
- }
1226
-
1227
- // Ok, we found some leaking blocks and potentially that our load is
1228
- // "consumed" inside a different loop in the loop nest from cvi. If we are
1229
- // consumed in the loop, then our visit should have inserted all of the
1230
- // necessary destroys for us by inserting the destroys on the loop
1231
- // boundaries. So, exit now.
1232
1208
// Ok, we found some leaking blocks and potentially that our load is
1233
1209
// "consumed" inside a different loop in the loop nest from cvi. If we are
1234
1210
// consumed in the loop, then our visit should have inserted all of the
1235
1211
// necessary destroys for us by inserting the destroys on the loop
1236
- // boundaries. So, exit now.
1212
+ // boundaries. So, continue.
1213
+ //
1214
+ // NOTE: This includes cases where due to an infinite loop, we did not
1215
+ // insert /any/ destroys since the loop has no boundary in a certain sense.
1237
1216
if (consumedInLoop) {
1238
1217
continue ;
1239
1218
}
1240
1219
1241
- // Otherwise, we had a branching case,
1220
+ // Otherwise, we need to insert one last destroy after the load for our phi.
1242
1221
auto next = std::next (load->getIterator ());
1243
1222
SILBuilderWithScope builder (next);
1244
1223
builder.emitDestroyValueOperation (next->getLoc (), phi);
@@ -1304,41 +1283,25 @@ void AvailableValueAggregator::addMissingDestroysForCopiedValues(
1304
1283
// no further work to do.
1305
1284
auto *loadOperand = &load->getAllOperands ()[0 ];
1306
1285
LinearLifetimeChecker checker (visitedBlocks, deadEndBlocks);
1307
- bool didInsertDestroy = false ;
1308
1286
bool consumedInLoop = checker.completeConsumingUseSet (
1309
1287
cvi, loadOperand, leakingBlocks, [&](SILBasicBlock::iterator iter) {
1310
1288
SILBuilderWithScope builder (iter);
1311
1289
builder.emitDestroyValueOperation (loc, cvi);
1312
- didInsertDestroy = true ;
1313
1290
});
1314
1291
1315
- // If we did not insert any destroy_values, we either:
1316
- //
1317
- // 1. Are consuming the value in a loop, but the loop is an infinite loop
1318
- // so we shouldn't insert a destroy.
1319
- //
1320
- // 2. Are not consuming the value in the loop and our phi is strongly
1321
- // control equivalent with the load_borrow, so we insert a destroy
1322
- // /after/ the load_borrow.
1323
- if (!didInsertDestroy) {
1324
- if (!consumedInLoop) {
1325
- auto next = std::next (load->getIterator ());
1326
- SILBuilderWithScope builder (next);
1327
- builder.emitDestroyValueOperation (next->getLoc (), cvi);
1328
- }
1329
- continue ;
1330
- }
1331
-
1332
1292
// Ok, we found some leaking blocks and potentially that our load is
1333
1293
// "consumed" inside a different loop in the loop nest from cvi. If we are
1334
1294
// consumed in the loop, then our visit should have inserted all of the
1335
1295
// necessary destroys for us by inserting the destroys on the loop
1336
- // boundaries. So, exit now.
1296
+ // boundaries. So, continue.
1297
+ //
1298
+ // NOTE: This includes cases where due to an infinite loop, we did not
1299
+ // insert /any/ destroys since the loop has no boundary in a certain sense.
1337
1300
if (consumedInLoop) {
1338
1301
continue ;
1339
1302
}
1340
1303
1341
- // Otherwise, we had a branching case,
1304
+ // Otherwise, we need to insert one last destroy after the load for our phi.
1342
1305
auto next = std::next (load->getIterator ());
1343
1306
SILBuilderWithScope builder (next);
1344
1307
builder.emitDestroyValueOperation (next->getLoc (), cvi);
0 commit comments