Skip to content

[5.3] FunctionSignatureOptimization: fix a miscompile in owned->guaranteed return value specialization #31661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions include/swift/SILOptimizer/Analysis/EpilogueARCAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,11 @@ class EpilogueARCContext {
// We are checking for retain. If this is a self-recursion. call
// to the function (which returns an owned value) can be treated as
// the retain instruction.
if (auto *AI = dyn_cast<ApplyInst>(II))
if (AI->getCalleeFunction() == II->getParent()->getParent())
return true;
if (auto *AI = dyn_cast<ApplyInst>(II)) {
return AI->getCalleeFunction() == II->getParent()->getParent() &&
RCFI->getRCIdentityRoot(AI) ==
RCFI->getRCIdentityRoot(getArg(AI->getParent()));
}
// Check whether this is a retain instruction and the argument it
// retains.
return isRetainInstruction(II) &&
Expand Down
2 changes: 1 addition & 1 deletion test/SILOptimizer/epilogue_arc_dumper.sil
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ bb3:
}

// CHECK-LABEL: START: epilogue_retains_in_diamond_top_and_left_split_block
// CHECK: apply
// CHECK-NOT: apply
// CHECK: FINISH: epilogue_retains_in_diamond_top_and_left_split_block
sil @epilogue_retains_in_diamond_top_and_left_split_block : $@convention(thin) (@owned foo) -> @owned foo {
bb0(%0 : $foo):
Expand Down
38 changes: 38 additions & 0 deletions test/SILOptimizer/functionsigopts.sil
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public class KlassBar : KlassFoo {
init()
}

indirect public enum IndirectEnum : Hashable {
case A
case B(IndirectEnum)
}

sil @use_Int : $@convention(thin) (Int) -> ()
sil @use_Int64 : $@convention(thin) (Int64) -> ()
sil @use_Generic : $@convention(thin) <T>(@in_guaranteed T) -> ()
Expand Down Expand Up @@ -514,6 +519,39 @@ bb0(%0 : $boo):
return %0 : $boo
}

// CHECK-LABEL: sil @no_fso_recursive_result_is_not_returned :
// CHECK-NOT: release_value
// CHECK: bb2:
// CHECK-NEXT: retain_value %0
// CHECK: return
// CHECK-LABEL: } // end sil function 'no_fso_recursive_result_is_not_returned'
sil @no_fso_recursive_result_is_not_returned : $@convention(method) (@guaranteed IndirectEnum, @guaranteed IndirectEnum) -> @owned IndirectEnum {
bb0(%0 : $IndirectEnum, %1 : $IndirectEnum):
switch_enum %1 : $IndirectEnum, case #IndirectEnum.B!enumelt: bb1, default bb2

bb1(%3 : ${ var IndirectEnum }):
%4 = project_box %3 : ${ var IndirectEnum }, 0
%5 = load %4 : $*IndirectEnum
%6 = function_ref @no_fso_recursive_result_is_not_returned : $@convention(method) (@guaranteed IndirectEnum, @guaranteed IndirectEnum) -> @owned IndirectEnum

// The result of the self-recursion is not returned (but put in a box).
// This prevents guaranteed -> owned return value specialization.
%7 = apply %6(%0, %5) : $@convention(method) (@guaranteed IndirectEnum, @guaranteed IndirectEnum) -> @owned IndirectEnum
%8 = alloc_box ${ var IndirectEnum }
%9 = project_box %8 : ${ var IndirectEnum }, 0
store %7 to %9 : $*IndirectEnum
%11 = enum $IndirectEnum, #IndirectEnum.B!enumelt, %8 : ${ var IndirectEnum }
br bb3(%11 : $IndirectEnum)

bb2:
retain_value %0 : $IndirectEnum
br bb3(%0 : $IndirectEnum)

bb3(%35 : $IndirectEnum):
return %35 : $IndirectEnum
}


// Make sure we do not move the retain_value in the throw block.
//
// CHECK-LABEL: sil [serialized] [signature_optimized_thunk] [always_inline] @owned_to_unowned_retval_with_error_result : $@convention(thin) (@owned boo) -> (@owned boo, @error Error) {
Expand Down