Skip to content

Commit d918b31

Browse files
committed
TempRValueOptimization: don't optimize copies to mark_dependence base values.
We want to keep the original lifetime of the base. If we would eliminate the base alloc_stack, we risk to insert a destroy_addr too early.
1 parent 024f4bb commit d918b31

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ collectLoads(Operand *addressUse, CopyAddrInst *originalCopy,
181181
}
182182
case SILInstructionKind::MarkDependenceInst: {
183183
auto mdi = cast<MarkDependenceInst>(user);
184-
// If the user is the base operand of the MarkDependenceInst we can return
185-
// true, because this would be the end of this dataflow chain
186184
if (mdi->getBase() == address) {
187-
return true;
185+
// We want to keep the original lifetime of the base. If we would eliminate
186+
// the base alloc_stack, we risk to insert a destroy_addr too early.
187+
return false;
188188
}
189189
// If the user is the value operand of the MarkDependenceInst we have to
190190
// transitively explore its uses until we reach a load or return false

test/SILOptimizer/temp_rvalue_opt.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ sil @unknown : $@convention(thin) () -> ()
3434
sil @load_string : $@convention(thin) (@in_guaranteed String) -> String
3535
sil @guaranteed_user : $@convention(thin) (@guaranteed Klass) -> ()
3636
sil @consume : $@convention(thin) (@owned C) -> ()
37+
sil @createKlass : $@convention(thin) () -> @owned Klass
38+
sil @initC : $@convention(thin) () -> @out C
3739

3840
sil @inguaranteed_user_without_result : $@convention(thin) (@in_guaranteed Klass) -> () {
3941
bb0(%0 : $*Klass):
@@ -802,3 +804,26 @@ bb0(%0 : $C):
802804
dealloc_stack %2
803805
return %5
804806
}
807+
808+
// CHECK-LABEL: sil [ossa] @dont_shrink_lifetime_of_mark_dependence_base
809+
// CHECK: copy_addr
810+
// CHECK: apply
811+
// CHECK: } // end sil function 'dont_shrink_lifetime_of_mark_dependence_base'
812+
sil [ossa] @dont_shrink_lifetime_of_mark_dependence_base : $@convention(thin) () -> () {
813+
bb0:
814+
%0 = alloc_stack $C
815+
%1 = function_ref @initC : $@convention(thin) () -> @out C
816+
%2 = apply %1(%0) : $@convention(thin) () -> @out C
817+
%3 = alloc_stack [var_decl] $C, let, name "a"
818+
copy_addr [take] %0 to [init] %3
819+
%5 = function_ref @createKlass : $@convention(thin) () -> @owned Klass
820+
%6 = apply %5() : $@convention(thin) () -> @owned Klass
821+
%7 = mark_dependence [nonescaping] %6 on %3
822+
destroy_value %7
823+
destroy_addr %3
824+
dealloc_stack %3
825+
dealloc_stack %0
826+
%12 = tuple ()
827+
return %12
828+
}
829+

0 commit comments

Comments
 (0)