Skip to content

Commit 2831f2f

Browse files
committed
Add findPointerEscape check on store source, before shortening its lifetime by inserting destroy at the store
1 parent c3c78fa commit 2831f2f

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

lib/SILOptimizer/Transforms/DeadObjectElimination.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/SIL/BasicBlockUtils.h"
3030
#include "swift/SIL/DebugUtils.h"
3131
#include "swift/SIL/InstructionUtils.h"
32+
#include "swift/SIL/OwnershipUtils.h"
3233
#include "swift/SIL/Projection.h"
3334
#include "swift/SIL/SILArgument.h"
3435
#include "swift/SIL/SILDeclRef.h"
@@ -858,6 +859,19 @@ bool DeadObjectElimination::processAllocRef(AllocRefInstBase *ARI) {
858859
}
859860

860861
if (ARI->getFunction()->hasOwnership()) {
862+
// In ossa, we are going to delete the dead element store and insert a
863+
// destroy_value of the store's source. This is shortening the store's
864+
// source lifetime. Check if there was a pointer escape of the store's
865+
// source, if so bail out.
866+
for (auto *user : UsersToRemove) {
867+
auto *store = dyn_cast<StoreInst>(user);
868+
if (!store ||
869+
store->getOwnershipQualifier() == StoreOwnershipQualifier::Trivial)
870+
continue;
871+
if (findPointerEscape(store->getSrc())) {
872+
return false;
873+
}
874+
}
861875
for (auto *user : UsersToRemove) {
862876
auto *store = dyn_cast<StoreInst>(user);
863877
if (!store ||
@@ -893,6 +907,19 @@ bool DeadObjectElimination::processAllocStack(AllocStackInst *ASI) {
893907
}
894908

895909
if (ASI->getFunction()->hasOwnership()) {
910+
for (auto *user : UsersToRemove) {
911+
auto *store = dyn_cast<StoreInst>(user);
912+
if (!store ||
913+
store->getOwnershipQualifier() == StoreOwnershipQualifier::Trivial)
914+
continue;
915+
// In ossa, we are going to delete the dead store and insert a
916+
// destroy_value of the store's source. This is shortening the store's
917+
// source lifetime. Check if there was a pointer escape of the store's
918+
// source, if so bail out.
919+
if (findPointerEscape(store->getSrc())) {
920+
return false;
921+
}
922+
}
896923
for (auto *user : UsersToRemove) {
897924
auto *store = dyn_cast<StoreInst>(user);
898925
if (!store ||
@@ -929,6 +956,17 @@ bool DeadObjectElimination::processKeyPath(KeyPathInst *KPI) {
929956
}
930957

931958
if (KPI->getFunction()->hasOwnership()) {
959+
for (const Operand &Op : KPI->getPatternOperands()) {
960+
if (Op.get()->getType().isTrivial(*KPI->getFunction()))
961+
continue;
962+
// In ossa, we are going to delete the dead keypath which was consuming
963+
// the pattern operand and insert a destroy_value of the pattern operand
964+
// value. This is shortening the pattern operand value's lifetime. Check
965+
// if there was a pointer escape, if so bail out.
966+
if (findPointerEscape(Op.get())) {
967+
return false;
968+
}
969+
}
932970
for (const Operand &Op : KPI->getPatternOperands()) {
933971
if (Op.get()->getType().isTrivial(*KPI->getFunction()))
934972
continue;

0 commit comments

Comments
 (0)