Skip to content

Commit 1f7432d

Browse files
committed
[Mem2Reg] Don't skip write-only enum locations.
The lifetimes of the values stored into these locations may need to be completed: if a trivial case of a non-trivial enum is written to the location, its lifetime must be completed. rdar://158139991
1 parent 63a3c65 commit 1f7432d

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2214,7 +2214,8 @@ bool MemoryToRegisters::promoteAllocation(AllocStackInst *alloc,
22142214
}
22152215

22162216
// Remove write-only AllocStacks.
2217-
if (isWriteOnlyAllocation(alloc) && !lexicalLifetimeEnsured(alloc)) {
2217+
if (isWriteOnlyAllocation(alloc) && !alloc->getType().isOrHasEnum() &&
2218+
!lexicalLifetimeEnsured(alloc)) {
22182219
LLVM_DEBUG(llvm::dbgs() << "*** Deleting store-only AllocStack: "<< *alloc);
22192220
deleter.forceDeleteWithUsers(alloc);
22202221
return true;

test/SILOptimizer/mem2reg_ossa_nontrivial.sil

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1591,3 +1591,84 @@ bb3:
15911591
return %r
15921592
}
15931593

1594+
// CHECK-LABEL: sil [ossa] @end_lifetime_at_trivial_case_dealloc_stack : {{.*}} {
1595+
// CHECK-NOT: alloc_stack
1596+
// CHECK: [[IN:%[^,]+]] = load [copy]
1597+
// CHECK: [[IN_BORROW:%[^,]+]] = begin_borrow [[IN]]
1598+
// CHECK: switch_enum [[IN_BORROW]]
1599+
// CHECK-SAME: case #Result.success!enumelt: [[INT:bb[0-9]+]]
1600+
// CHECK: [[INT]]({{%[^,]+}} : $Int):
1601+
// CHECK: end_borrow [[IN_BORROW]]
1602+
// CHECK: end_lifetime [[IN]]
1603+
// CHECK-LABEL: } // end sil function 'end_lifetime_at_trivial_case_dealloc_stack'
1604+
sil [ossa] @end_lifetime_at_trivial_case_dealloc_stack : $@convention(thin) (@in_guaranteed Result<Int, X>) -> () {
1605+
entry(%in_addr : $*Result<Int, X>):
1606+
%main_addr = alloc_stack $Result<Int, X>
1607+
%in = load [copy] %in_addr
1608+
store %in to [init] %main_addr
1609+
%in_borrow = load_borrow %main_addr
1610+
switch_enum %in_borrow,
1611+
case #Result.success!enumelt: int,
1612+
case #Result.failure!enumelt: full
1613+
1614+
int(%6 : $Int):
1615+
end_borrow %in_borrow
1616+
%temp = alloc_stack $Result<Int, X>
1617+
%in_copy = load [copy] %main_addr
1618+
store %in_copy to [init] %temp
1619+
br int2
1620+
1621+
int2:
1622+
dealloc_stack %temp
1623+
br exit
1624+
1625+
full(%13 : @guaranteed $X):
1626+
end_borrow %in_borrow
1627+
br exit
1628+
1629+
exit:
1630+
destroy_addr %main_addr
1631+
dealloc_stack %main_addr
1632+
%18 = tuple ()
1633+
return %18
1634+
}
1635+
1636+
// CHECK-LABEL: sil [ossa] @end_lifetime_at_trivial_case_dealloc_stack_2 : {{.*}} {
1637+
// CHECK: bb0([[IN_ADDR:%[^,]+]] :
1638+
// CHECK: [[IN_COPY:%[^,]+]] = load [copy] [[IN_ADDR]]
1639+
// CHECK: [[IN_BORROW:%[^,]+]] = begin_borrow [[IN_COPY]]
1640+
// CHECK: switch_enum [[IN_BORROW]]
1641+
// CHECK: case #Result.success!enumelt: [[INT:bb[0-9]+]]
1642+
// CHECK: [[INT]]
1643+
// CHECK: end_borrow [[IN_BORROW]]
1644+
// CHECK: end_lifetime [[IN_COPY]]
1645+
// CHECK-LABEL: } // end sil function 'end_lifetime_at_trivial_case_dealloc_stack_2'
1646+
sil [ossa] @end_lifetime_at_trivial_case_dealloc_stack_2 : $@convention(thin) (@in_guaranteed Result<Int, X>) -> () {
1647+
entry(%in_addr : $*Result<Int, X>):
1648+
%main_addr = alloc_stack $Result<Int, X>
1649+
%temp = alloc_stack $Result<Int, X>
1650+
%in = load [copy] %in_addr
1651+
store %in to [init] %main_addr
1652+
%in_borrow = load_borrow %main_addr
1653+
switch_enum %in_borrow,
1654+
case #Result.success!enumelt: int,
1655+
case #Result.failure!enumelt: x
1656+
1657+
int(%int : $Int):
1658+
end_borrow %in_borrow
1659+
%in_copy = load [copy] %main_addr
1660+
store %in_copy to [init] %temp
1661+
br int2
1662+
1663+
int2:
1664+
br exit
1665+
1666+
x(%13 : @guaranteed $X):
1667+
end_borrow %in_borrow
1668+
br exit
1669+
1670+
exit:
1671+
destroy_addr %main_addr
1672+
%retval = tuple ()
1673+
return %retval
1674+
}

0 commit comments

Comments
 (0)