Skip to content

Commit 398cb58

Browse files
authored
Merge pull request #28083 from eeckstein/fix-silcombine
SILCombine: fix a wrong dealloc_stack ordering when propagating concrete types of apply arguments.
2 parents 4008261 + 40c1e8f commit 398cb58

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1311,7 +1311,7 @@ SILInstruction *SILCombiner::createApplyWithConcreteType(
13111311
// deallocate the temporary copy.
13121312
SILBuilder cleanupBuilder(cleanupPos, NewApply.getDebugScope(), BuilderCtx);
13131313
auto cleanupLoc = RegularLocation::getAutoGeneratedLocation();
1314-
for (ConcreteArgumentCopy &argCopy : concreteArgCopies) {
1314+
for (ConcreteArgumentCopy &argCopy : llvm::reverse(concreteArgCopies)) {
13151315
cleanupBuilder.createDestroyAddr(cleanupLoc, argCopy.origArg);
13161316
cleanupBuilder.createDeallocStack(cleanupLoc,
13171317
argCopy.tempArgCopy->getDest());

test/SILOptimizer/sil_combine_concrete_existential.sil

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,48 @@ bb0(%0 : $C):
570570
%9 = tuple ()
571571
return %9 : $()
572572
}
573+
574+
protocol P1 { }
575+
protocol P2 { }
576+
protocol P3 { }
577+
578+
sil @callee : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 : P1, τ_0_1 : P2, τ_0_2 : P3> (@in τ_0_0, @in τ_0_2, @in τ_0_1) -> ()
579+
580+
// Check that the deallocations are created in the right order and the SILVerifier does not complain.
581+
582+
// CHECK-LABEL: sil @test_multiple_propagated_args
583+
// CHECK-NOT: open_existential_addr
584+
// CHECK-LABEL: } // end sil function 'test_multiple_propagated_args'
585+
sil @test_multiple_propagated_args : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 : P1, τ_0_1 : P2, τ_0_2 : P3> (@in τ_0_0, @in τ_0_2, @in τ_0_1) -> () {
586+
bb0(%0 : $*τ_0_0, %1 : $*τ_0_2, %2 : $*τ_0_1):
587+
%5 = alloc_stack $P1
588+
%6 = init_existential_addr %5 : $*P1, $τ_0_0
589+
copy_addr [take] %0 to [initialization] %6 : $*τ_0_0
590+
%8 = alloc_stack $P3
591+
%9 = init_existential_addr %8 : $*P3, $τ_0_2
592+
copy_addr [take] %1 to [initialization] %9 : $*τ_0_2
593+
%11 = alloc_stack $P2
594+
%12 = init_existential_addr %11 : $*P2, $τ_0_1
595+
copy_addr [take] %2 to [initialization] %12 : $*τ_0_1
596+
%15 = function_ref @callee : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 : P1, τ_0_1 : P2, τ_0_2 : P3> (@in τ_0_0, @in τ_0_2, @in τ_0_1) -> ()
597+
%16 = open_existential_addr mutable_access %5 : $*P1 to $*@opened("76D54B80-FF66-11E9-B604-8C8590A6A134") P1
598+
%17 = alloc_stack $@opened("76D54B80-FF66-11E9-B604-8C8590A6A134") P1
599+
copy_addr %16 to [initialization] %17 : $*@opened("76D54B80-FF66-11E9-B604-8C8590A6A134") P1
600+
%19 = open_existential_addr mutable_access %8 : $*P3 to $*@opened("76D54C34-FF66-11E9-B604-8C8590A6A134") P3
601+
%20 = alloc_stack $@opened("76D54C34-FF66-11E9-B604-8C8590A6A134") P3
602+
copy_addr %19 to [initialization] %20 : $*@opened("76D54C34-FF66-11E9-B604-8C8590A6A134") P3
603+
%22 = open_existential_addr mutable_access %11 : $*P2 to $*@opened("76D54C84-FF66-11E9-B604-8C8590A6A134") P2
604+
%23 = alloc_stack $@opened("76D54C84-FF66-11E9-B604-8C8590A6A134") P2
605+
copy_addr %22 to [initialization] %23 : $*@opened("76D54C84-FF66-11E9-B604-8C8590A6A134") P2
606+
%25 = apply %15<@opened("76D54B80-FF66-11E9-B604-8C8590A6A134") P1, @opened("76D54C84-FF66-11E9-B604-8C8590A6A134") P2, @opened("76D54C34-FF66-11E9-B604-8C8590A6A134") P3>(%17, %20, %23) : $@convention(thin) <τ_0_0, τ_0_1, τ_0_2 where τ_0_0 : P1, τ_0_1 : P2, τ_0_2 : P3> (@in τ_0_0, @in τ_0_2, @in τ_0_1) -> ()
607+
destroy_addr %11 : $*P2
608+
dealloc_stack %23 : $*@opened("76D54C84-FF66-11E9-B604-8C8590A6A134") P2
609+
destroy_addr %8 : $*P3
610+
dealloc_stack %20 : $*@opened("76D54C34-FF66-11E9-B604-8C8590A6A134") P3
611+
destroy_addr %5 : $*P1
612+
dealloc_stack %17 : $*@opened("76D54B80-FF66-11E9-B604-8C8590A6A134") P1
613+
dealloc_stack %11 : $*P2
614+
dealloc_stack %8 : $*P3
615+
dealloc_stack %5 : $*P1
616+
return %25 : $()
617+
}

0 commit comments

Comments
 (0)