Skip to content

Commit 9fdf850

Browse files
authored
Merge pull request #21640 from gottesmm/pr-02a5ad432c647ea45ba14d73742ac44ca22abc0f
2 parents 12ea649 + f4b1ae0 commit 9fdf850

File tree

1 file changed

+39
-24
lines changed

1 file changed

+39
-24
lines changed

lib/SILOptimizer/Mandatory/PredictableMemOpt.cpp

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,35 +1273,50 @@ bool AllocOptimize::tryToRemoveDeadAllocation() {
12731273
}
12741274
}
12751275

1276-
// If the memory object has non-trivial type, then removing the deallocation
1277-
// will drop any releases. Check that there is nothing preventing removal.
1276+
// If our memory is trivially typed, we can just remove it without needing to
1277+
// consider if the stored value needs to be destroyed. So at this point,
1278+
// delete the memory!
1279+
if (MemoryType.isTrivial(Module)) {
1280+
LLVM_DEBUG(llvm::dbgs() << "*** Removing autogenerated trivial allocation: "
1281+
<< *TheMemory);
1282+
1283+
// If it is safe to remove, do it. Recursively remove all instructions
1284+
// hanging off the allocation instruction, then return success. Let the
1285+
// caller remove the allocation itself to avoid iterator invalidation.
1286+
eraseUsesOfInstruction(TheMemory);
1287+
1288+
return true;
1289+
}
1290+
1291+
// Otherwise removing the deallocation will drop any releases. Check that
1292+
// there is nothing preventing removal.
12781293
llvm::SmallVector<unsigned, 8> DestroyAddrIndices;
12791294
llvm::SmallVector<AvailableValue, 32> AvailableValueList;
12801295
llvm::SmallVector<unsigned, 8> AvailableValueStartOffsets;
12811296

1282-
if (!MemoryType.isTrivial(Module)) {
1283-
for (auto P : llvm::enumerate(Releases)) {
1284-
auto *R = P.value();
1285-
if (R == nullptr)
1286-
continue;
1287-
1288-
// We stash all of the destroy_addr that we see.
1289-
if (auto *DAI = dyn_cast<DestroyAddrInst>(R)) {
1290-
AvailableValueStartOffsets.push_back(AvailableValueList.size());
1291-
// Make sure we can actually promote this destroy addr. If we can not,
1292-
// then we must bail. In order to not gather available values twice, we
1293-
// gather the available values here that we will use to promote the
1294-
// values.
1295-
if (!canPromoteDestroyAddr(DAI, AvailableValueList))
1296-
return false;
1297-
DestroyAddrIndices.push_back(P.index());
1298-
continue;
1299-
}
1297+
for (auto P : llvm::enumerate(Releases)) {
1298+
auto *R = P.value();
1299+
if (R == nullptr)
1300+
continue;
13001301

1301-
LLVM_DEBUG(llvm::dbgs() << "*** Failed to remove autogenerated alloc: "
1302-
"kept alive by release: " << *R);
1303-
return false;
1302+
// We stash all of the destroy_addr that we see.
1303+
if (auto *DAI = dyn_cast<DestroyAddrInst>(R)) {
1304+
AvailableValueStartOffsets.push_back(AvailableValueList.size());
1305+
// Make sure we can actually promote this destroy addr. If we can not,
1306+
// then we must bail. In order to not gather available values twice, we
1307+
// gather the available values here that we will use to promote the
1308+
// values.
1309+
if (!canPromoteDestroyAddr(DAI, AvailableValueList))
1310+
return false;
1311+
DestroyAddrIndices.push_back(P.index());
1312+
continue;
13041313
}
1314+
1315+
LLVM_DEBUG(llvm::dbgs()
1316+
<< "*** Failed to remove autogenerated non-trivial alloc: "
1317+
"kept alive by release: "
1318+
<< *R);
1319+
return false;
13051320
}
13061321

13071322
// If we reached this point, we can promote all of our destroy_addr.
@@ -1324,7 +1339,7 @@ bool AllocOptimize::tryToRemoveDeadAllocation() {
13241339
Releases[DestroyAddrIndex] = nullptr;
13251340
}
13261341

1327-
LLVM_DEBUG(llvm::dbgs() << "*** Removing autogenerated alloc_stack: "
1342+
LLVM_DEBUG(llvm::dbgs() << "*** Removing autogenerated non-trivial alloc: "
13281343
<< *TheMemory);
13291344

13301345
// If it is safe to remove, do it. Recursively remove all instructions

0 commit comments

Comments
 (0)