Skip to content

Commit 40f0d58

Browse files
committed
Merge pull request #2582 from swiftix/master
2 parents 0e657fb + 48f7941 commit 40f0d58

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,12 @@ SILInstruction *SILCombiner::visitAllocStackInst(AllocStackInst *AS) {
310310
// If the only users of the alloc_stack are alloc, destroy and
311311
// init_existential_addr then we can promote the allocation of the init
312312
// existential.
313-
if (IEI && !OEI) {
313+
// Be careful with open archetypes, because they cannot be moved before
314+
// their definitions.
315+
if (IEI && !OEI &&
316+
!IEI->getLoweredConcreteType()
317+
.getSwiftRValueType()
318+
->isOpenedExistential()) {
314319
auto *ConcAlloc = Builder.createAllocStack(
315320
AS->getLoc(), IEI->getLoweredConcreteType(), AS->getVarInfo());
316321
IEI->replaceAllUsesWith(ConcAlloc);

test/SILOptimizer/sil_combine.sil

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2887,3 +2887,51 @@ bb1(%4 : $Builtin.Int32):
28872887
bb2(%5 : $MyErrorType):
28882888
throw %5 : $MyErrorType
28892889
}
2890+
2891+
protocol PM {
2892+
var sum: Int { get nonmutating set }
2893+
func done()
2894+
}
2895+
2896+
extension PM {
2897+
func plus() -> Self
2898+
func minus()
2899+
}
2900+
2901+
2902+
public final class VV {
2903+
@sil_stored final var m: PM { get set }
2904+
init()
2905+
deinit
2906+
}
2907+
2908+
sil @plus : $@convention(method) <Self where Self : PM> (@in_guaranteed Self) -> @out Self
2909+
2910+
sil @minus : $@convention(method) <Self where Self : PM> (@in_guaranteed Self) -> ()
2911+
2912+
// CHECK-LABEL: sil @silcombine_dont_change_allocstack_for_opened_archetypes
2913+
// CHECK-NOT: alloc_stack{{.*}}opened
2914+
// CHECK: open_existential_addr {{%[0-9]+}} : $*PM to $*@opened("090C3DB0-1C76-11E6-81C4-B8E856428C60") PM
2915+
// CHECK: return
2916+
sil @silcombine_dont_change_allocstack_for_opened_archetypes : $@convention(thin) (@owned VV) -> () {
2917+
bb0(%0 : $VV):
2918+
%8 = alloc_stack $PM, let, name "x"
2919+
%9 = ref_element_addr %0 : $VV, #VV.m
2920+
%10 = alloc_stack $PM
2921+
copy_addr %9 to [initialization] %10 : $*PM
2922+
%12 = open_existential_addr %10 : $*PM to $*@opened("090C3DB0-1C76-11E6-81C4-B8E856428C60") PM
2923+
%13 = init_existential_addr %8 : $*PM, $@opened("090C3DB0-1C76-11E6-81C4-B8E856428C60") PM
2924+
%14 = function_ref @plus : $@convention(method) <τ_0_0 where τ_0_0 : PM> (@in_guaranteed τ_0_0) -> @out τ_0_0
2925+
%15 = apply %14<@opened("090C3DB0-1C76-11E6-81C4-B8E856428C60") PM>(%13, %12) : $@convention(method) <τ_0_0 where τ_0_0 : PM> (@in_guaranteed τ_0_0) -> @out τ_0_0
2926+
destroy_addr %12 : $*@opened("090C3DB0-1C76-11E6-81C4-B8E856428C60") PM
2927+
deinit_existential_addr %10 : $*PM
2928+
dealloc_stack %10 : $*PM
2929+
%20 = function_ref @minus : $@convention(method) <τ_0_0 where τ_0_0 : PM> (@in_guaranteed τ_0_0) -> ()
2930+
%21 = apply %20<@opened("090C3DB0-1C76-11E6-81C4-B8E856428C60") PM>(%13) : $@convention(method) <τ_0_0 where τ_0_0 : PM> (@in_guaranteed τ_0_0) -> ()
2931+
destroy_addr %8 : $*PM
2932+
dealloc_stack %8 : $*PM
2933+
strong_release %0 : $VV
2934+
%26 = tuple ()
2935+
return %26 : $()
2936+
}
2937+

0 commit comments

Comments
 (0)