Skip to content

Commit 0403ab1

Browse files
committed
[Mem2Reg] Don't canonicalize erased vals.
To eliminate copies which become newly spurious, Mem2Reg canonicalizes the lifetimes of values that are stored and of newly introduced phis after rewriting. It's possible, however, for the values that are stored to be deleted during canonicalization if a value and its copy are both stored to the address. Such values must not be canonicalized. So check whether values have been erased before canonicalizing them. rdar://113762355
1 parent 3113e83 commit 0403ab1

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2195,7 +2195,7 @@ void MemoryToRegisters::canonicalizeValueLifetimes(
21952195
/*pruneDebug=*/true, /*maximizeLifetime=*/!f.shouldOptimize(), &f,
21962196
accessBlockAnalysis, domInfo, calleeAnalysis, deleter);
21972197
for (auto value : owned) {
2198-
if (isa<SILUndef>(value))
2198+
if (isa<SILUndef>(value) || value->isMarkedAsDeleted())
21992199
continue;
22002200
auto root = CanonicalizeOSSALifetime::getCanonicalCopiedDef(value);
22012201
if (auto *copy = dyn_cast<CopyValueInst>(root)) {
@@ -2209,7 +2209,7 @@ void MemoryToRegisters::canonicalizeValueLifetimes(
22092209
}
22102210
CanonicalizeBorrowScope borrowCanonicalizer(&f, deleter);
22112211
for (auto value : guaranteed) {
2212-
if (isa<SILUndef>(value))
2212+
if (isa<SILUndef>(value) || value->isMarkedAsDeleted())
22132213
continue;
22142214
auto borrowee = CanonicalizeBorrowScope::getCanonicalBorrowedDef(value);
22152215
if (!borrowee)

test/SILOptimizer/mem2reg_ossa.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,3 +742,22 @@ sil [ossa] @dont_canonicalize_undef : $@convention(thin) () -> () {
742742
%retval = tuple ()
743743
return %retval : $()
744744
}
745+
746+
// CHECK-LABEL: sil [ossa] @dont_canonicalize_erased_copy : {{.*}} {
747+
// CHECK: bb0(%0 :
748+
// CHECK: destroy_value %0
749+
// CHECK-LABEL: } // end sil function 'dont_canonicalize_erased_copy'
750+
sil [ossa] @dont_canonicalize_erased_copy : $@convention(thin) (@owned C) -> () {
751+
entry(%instance : @owned $C):
752+
%stack = alloc_stack $C
753+
%copy = copy_value %instance : $C
754+
store %copy to [init] %stack : $*C
755+
%loaded_copy = load [take] %stack : $*C
756+
destroy_value %loaded_copy : $C
757+
store %instance to [init] %stack : $*C
758+
%loaded_instance = load [take] %stack : $*C
759+
destroy_value %loaded_instance : $C
760+
dealloc_stack %stack : $*C
761+
%retval = tuple ()
762+
return %retval : $()
763+
}

0 commit comments

Comments
 (0)