Skip to content

Commit c55856e

Browse files
committed
[pred-memopt] Simplify logic around lifetime extending a value that is consumed in a loop.
I realized after 84232cf started testing that I can simplify this code as such, but I decided to finish testing. This is a NFC change.
1 parent 829ab27 commit c55856e

File tree

1 file changed

+10
-47
lines changed

1 file changed

+10
-47
lines changed

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 10 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,46 +1199,25 @@ void AvailableValueAggregator::addHandOffCopyDestroysForPhis(
11991199
// no further work to do.
12001200
auto *loadOperand = &load->getAllOperands()[0];
12011201
LinearLifetimeChecker checker(visitedBlocks, deadEndBlocks);
1202-
bool didInsertDestroy = false;
12031202
bool consumedInLoop = checker.completeConsumingUseSet(
12041203
phi, loadOperand, leakingBlocks, [&](SILBasicBlock::iterator iter) {
12051204
SILBuilderWithScope builder(iter);
12061205
builder.emitDestroyValueOperation(loc, phi);
1207-
didInsertDestroy = true;
12081206
});
12091207

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.
12321208
// Ok, we found some leaking blocks and potentially that our load is
12331209
// "consumed" inside a different loop in the loop nest from cvi. If we are
12341210
// consumed in the loop, then our visit should have inserted all of the
12351211
// 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.
12371216
if (consumedInLoop) {
12381217
continue;
12391218
}
12401219

1241-
// Otherwise, we had a branching case,
1220+
// Otherwise, we need to insert one last destroy after the load for our phi.
12421221
auto next = std::next(load->getIterator());
12431222
SILBuilderWithScope builder(next);
12441223
builder.emitDestroyValueOperation(next->getLoc(), phi);
@@ -1304,41 +1283,25 @@ void AvailableValueAggregator::addMissingDestroysForCopiedValues(
13041283
// no further work to do.
13051284
auto *loadOperand = &load->getAllOperands()[0];
13061285
LinearLifetimeChecker checker(visitedBlocks, deadEndBlocks);
1307-
bool didInsertDestroy = false;
13081286
bool consumedInLoop = checker.completeConsumingUseSet(
13091287
cvi, loadOperand, leakingBlocks, [&](SILBasicBlock::iterator iter) {
13101288
SILBuilderWithScope builder(iter);
13111289
builder.emitDestroyValueOperation(loc, cvi);
1312-
didInsertDestroy = true;
13131290
});
13141291

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-
13321292
// Ok, we found some leaking blocks and potentially that our load is
13331293
// "consumed" inside a different loop in the loop nest from cvi. If we are
13341294
// consumed in the loop, then our visit should have inserted all of the
13351295
// 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.
13371300
if (consumedInLoop) {
13381301
continue;
13391302
}
13401303

1341-
// Otherwise, we had a branching case,
1304+
// Otherwise, we need to insert one last destroy after the load for our phi.
13421305
auto next = std::next(load->getIterator());
13431306
SILBuilderWithScope builder(next);
13441307
builder.emitDestroyValueOperation(next->getLoc(), cvi);

0 commit comments

Comments
 (0)