Skip to content

Commit 1af18c3

Browse files
Merge pull request #67634 from nate-chandler/rdar113121424
[Mem2Reg] Don't canonicalize undef.
2 parents fb2f9bf + 4f16bb1 commit 1af18c3

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

Diff for: lib/SILOptimizer/Transforms/SILMem2Reg.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -2153,6 +2153,8 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
21532153
void MemoryToRegisters::collectStoredValues(AllocStackInst *asi,
21542154
StackList<SILValue> &owned,
21552155
StackList<SILValue> &guaranteed) {
2156+
if (!f.hasOwnership())
2157+
return;
21562158
for (auto *use : asi->getUses()) {
21572159
auto *user = use->getUser();
21582160
if (auto *si = dyn_cast<StoreInst>(user)) {
@@ -2166,6 +2168,8 @@ void MemoryToRegisters::collectStoredValues(AllocStackInst *asi,
21662168
void MemoryToRegisters::canonicalizeValueLifetimes(
21672169
StackList<SILValue> &owned, StackList<SILValue> &guaranteed,
21682170
BasicBlockSetVector &livePhiBlocks) {
2171+
if (!f.hasOwnership())
2172+
return;
21692173
if (Mem2RegDisableLifetimeCanonicalization)
21702174
return;
21712175

@@ -2191,6 +2195,8 @@ void MemoryToRegisters::canonicalizeValueLifetimes(
21912195
/*pruneDebug=*/true, /*maximizeLifetime=*/!f.shouldOptimize(), &f,
21922196
accessBlockAnalysis, domInfo, calleeAnalysis, deleter);
21932197
for (auto value : owned) {
2198+
if (isa<SILUndef>(value))
2199+
continue;
21942200
auto root = CanonicalizeOSSALifetime::getCanonicalCopiedDef(value);
21952201
if (auto *copy = dyn_cast<CopyValueInst>(root)) {
21962202
if (SILValue borrowDef = CanonicalizeBorrowScope::getCanonicalBorrowedDef(
@@ -2203,6 +2209,8 @@ void MemoryToRegisters::canonicalizeValueLifetimes(
22032209
}
22042210
CanonicalizeBorrowScope borrowCanonicalizer(&f, deleter);
22052211
for (auto value : guaranteed) {
2212+
if (isa<SILUndef>(value))
2213+
continue;
22062214
auto borrowee = CanonicalizeBorrowScope::getCanonicalBorrowedDef(value);
22072215
if (!borrowee)
22082216
continue;

Diff for: test/SILOptimizer/mem2reg.sil

+13
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,16 @@ sil @load_from_uninitialized_empty : $@convention(thin) () -> (Pair<Pair<EmptySt
518518
dealloc_stack %addr : $*(Pair<Pair<EmptyStruct, EmptyStruct>, EmptyStruct>, (EmptyStruct, EmptyStruct), EmptyStruct)
519519
return %value : $(Pair<Pair<EmptyStruct, EmptyStruct>, EmptyStruct>, (EmptyStruct, EmptyStruct), EmptyStruct)
520520
}
521+
522+
523+
// CHECK-LABEL: sil @dont_canonicalize_undef : {{.*}} {
524+
// CHECK: [[RETVAL:%[^,]+]] = tuple
525+
// CHECK: return [[RETVAL]]
526+
// CHECK-LABEL: } // end sil function 'dont_canonicalize_undef'
527+
sil @dont_canonicalize_undef : $@convention(thin) () -> () {
528+
%addr = alloc_stack $()
529+
store undef to %addr : $*()
530+
dealloc_stack %addr : $*()
531+
%retval = tuple ()
532+
return %retval : $()
533+
}

Diff for: test/SILOptimizer/mem2reg_ossa.sil

+12
Original file line numberDiff line numberDiff line change
@@ -730,3 +730,15 @@ exit:
730730
%retval = tuple ()
731731
return %retval : $()
732732
}
733+
734+
// CHECK-LABEL: sil [ossa] @dont_canonicalize_undef : {{.*}} {
735+
// CHECK: [[RETVAL:%[^,]+]] = tuple
736+
// CHECK: return [[RETVAL]]
737+
// CHECK-LABEL: } // end sil function 'dont_canonicalize_undef'
738+
sil [ossa] @dont_canonicalize_undef : $@convention(thin) () -> () {
739+
%addr = alloc_stack $()
740+
store undef to [trivial] %addr : $*()
741+
dealloc_stack %addr : $*()
742+
%retval = tuple ()
743+
return %retval : $()
744+
}

0 commit comments

Comments
 (0)