Skip to content

Commit f177be0

Browse files
authored
Merge pull request #68325 from meg-gupta/fixnonestoremem2reg
Fix handling of none value stores to non-trivial enums in Mem2Reg
2 parents a7ff6e9 + c89dcd8 commit f177be0

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,6 @@ class LiveValues {
203203
return LiveValues::forOwned({stored, move});
204204
}
205205

206-
static LiveValues forValues(SILValue stored, SILValue lexical) {
207-
if (stored->getOwnershipKind() == OwnershipKind::Guaranteed) {
208-
return LiveValues::forGuaranteed({stored, lexical});
209-
}
210-
return LiveValues::forOwned({stored, lexical});
211-
}
212-
213206
static LiveValues toReplace(AllocStackInst *asi, SILValue replacement) {
214207
if (replacement->getOwnershipKind() == OwnershipKind::Guaranteed) {
215208
return LiveValues::forGuaranteed(Guaranteed::toReplace(asi, replacement));
@@ -1290,7 +1283,9 @@ StackAllocationPromoter::getLiveOutValues(BasicBlockSetVector &phiBlocks,
12901283
auto *inst = it->second;
12911284
auto stored = inst->getOperand(CopyLikeInstruction::Src);
12921285
auto lexical = getLexicalValueForStore(inst, asi);
1293-
return LiveValues::forValues(stored, lexical);
1286+
return isa<StoreBorrowInst>(inst)
1287+
? LiveValues::forGuaranteed(stored, lexical)
1288+
: LiveValues::forOwned(stored, lexical);
12941289
}
12951290

12961291
// If there is a Phi definition in this block:

test/SILOptimizer/mem2reg_ossa_nontrivial.sil

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,3 +1227,69 @@ entry(%instance : @owned $AnyObject):
12271227
%82 = tuple ()
12281228
return %82 : $()
12291229
}
1230+
1231+
// CHECK-LABEL: sil [ossa] @test_enum_store_borrow_none :
1232+
// CHECK: alloc_stack
1233+
// CHECK-NOT: alloc_stack
1234+
// CHECK: dealloc_stack
1235+
// CHECK-LABEL: } // end sil function 'test_enum_store_borrow_none'
1236+
sil [ossa] @test_enum_store_borrow_none : $@convention(thin) () -> () {
1237+
bb0:
1238+
%1 = alloc_stack [lexical] $FakeOptional<Klass>
1239+
%none = enum $FakeOptional<Klass>, #FakeOptional.none!enumelt
1240+
%2 = store_borrow %none to %1 : $*FakeOptional<Klass>
1241+
%3 = alloc_stack $FakeOptional<Klass>
1242+
%4 = load [copy] %2 : $*FakeOptional<Klass>
1243+
store %4 to [init] %3 : $*FakeOptional<Klass>
1244+
switch_enum_addr %3 : $*FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
1245+
1246+
bb1:
1247+
%7 = unchecked_take_enum_data_addr %3 : $*FakeOptional<Klass>, #FakeOptional.some!enumelt
1248+
%8 = load [take] %7 : $*Klass
1249+
destroy_value %8 : $Klass
1250+
dealloc_stack %3 : $*FakeOptional<Klass>
1251+
br bb3
1252+
1253+
bb2:
1254+
dealloc_stack %3 : $*FakeOptional<Klass>
1255+
br bb3
1256+
1257+
bb3:
1258+
end_borrow %2 : $*FakeOptional<Klass>
1259+
dealloc_stack %1 : $*FakeOptional<Klass>
1260+
%t = tuple ()
1261+
return %t : $()
1262+
}
1263+
1264+
// CHECK-LABEL: sil [ossa] @test_enum_store_none :
1265+
// CHECK: alloc_stack
1266+
// CHECK-NOT: alloc_stack
1267+
// CHECK: dealloc_stack
1268+
// CHECK-LABEL: } // end sil function 'test_enum_store_none'
1269+
sil [ossa] @test_enum_store_none : $@convention(thin) () -> () {
1270+
bb0:
1271+
%1 = alloc_stack [lexical] $FakeOptional<Klass>
1272+
%none = enum $FakeOptional<Klass>, #FakeOptional.none!enumelt
1273+
store %none to [init] %1 : $*FakeOptional<Klass>
1274+
%3 = alloc_stack $FakeOptional<Klass>
1275+
%4 = load [copy] %1 : $*FakeOptional<Klass>
1276+
store %4 to [init] %3 : $*FakeOptional<Klass>
1277+
switch_enum_addr %3 : $*FakeOptional<Klass>, case #FakeOptional.some!enumelt: bb1, case #FakeOptional.none!enumelt: bb2
1278+
1279+
bb1:
1280+
%7 = unchecked_take_enum_data_addr %3 : $*FakeOptional<Klass>, #FakeOptional.some!enumelt
1281+
%8 = load [take] %7 : $*Klass
1282+
destroy_value %8 : $Klass
1283+
dealloc_stack %3 : $*FakeOptional<Klass>
1284+
br bb3
1285+
1286+
bb2:
1287+
dealloc_stack %3 : $*FakeOptional<Klass>
1288+
br bb3
1289+
1290+
bb3:
1291+
destroy_addr %1 : $*FakeOptional<Klass>
1292+
dealloc_stack %1 : $*FakeOptional<Klass>
1293+
%t = tuple ()
1294+
return %t : $()
1295+
}

0 commit comments

Comments
 (0)