Files
swift-mirror/test/SILOptimizer/ownership-utils-unit.sil
Erik Eckstein 5ef2da324c CopyToBorrowOptimization: several improvements
* support `copy_value` with "owned" source operands
* try to extend borrow scopes if necessary to do the optimization
* try to convert `store` to `store_borrow`
* add a peephole to remove "dead" `copy_value` instructions
* support sub-pass bisecting
2026-07-03 06:55:55 +02:00

298 lines
8.7 KiB
Plaintext

// RUN: %target-sil-opt -test-runner %s | %FileCheck %s
sil_stage canonical
import Builtin
class C {}
struct S {
let c: C
}
enum FakeOptional<T> {
case none
case some(T)
}
sil @getOwned : $@convention(thin) () -> (@owned C)
sil @borrow : $@convention(thin) (@guaranteed C) -> ()
sil @useUnmanaged : $@convention(thin) (@sil_unmanaged C) -> ()
sil [ossa] @test_escape_of_phi_transitive_incoming_value : $@convention(thin) () -> () {
entry:
%getOwned = function_ref @getOwned : $@convention(thin) () -> (@owned C)
%useUnmanaged = function_ref @useUnmanaged : $@convention(thin) (@sil_unmanaged C) -> ()
cond_br undef, left, right
left:
cond_br undef, left_left, left_right
left_left:
%llinstance = apply %getOwned() : $@convention(thin) () -> (@owned C)
br left_bottom(%llinstance : $C)
left_right:
%lrinstance = apply %getOwned() : $@convention(thin) () -> (@owned C)
%escape = ref_to_unmanaged %lrinstance : $C to $@sil_unmanaged C
apply %useUnmanaged(%escape) : $@convention(thin) (@sil_unmanaged C) -> ()
br left_bottom(%lrinstance : $C)
left_bottom(%linstance : @owned $C):
br exit(%linstance : $C)
right:
%rinstance = apply %getOwned() : $@convention(thin) () -> (@owned C)
br exit(%rinstance : $C)
exit(%instance : @owned $C):
// CHECK-LABEL: begin running test {{[0-9]+}} of {{[0-9]+}} on test_escape_of_phi_transitive_incoming_value: has_pointer_escape
// CHECK: %14 = argument of bb6 : $C
// CHECK: true
// CHECK-LABEL: end running test {{[0-9]+}} of {{[0-9]+}} on test_escape_of_phi_transitive_incoming_value: has_pointer_escape with
specify_test "has_pointer_escape @block.argument"
destroy_value %instance : $C
%retval = tuple ()
return %retval : $()
}
sil [ossa] @test_loop_phi : $@convention(thin) () -> () {
entry:
%instance_1 = enum $FakeOptional<C>, #FakeOptional.none!enumelt
br loop_entry(%instance_1 : $FakeOptional<C>)
loop_entry(%18 : @owned $FakeOptional<C>):
specify_test "has_pointer_escape @block.argument"
br loop_body
loop_body:
cond_br undef, loop_back, loop_exit
loop_back:
br loop_entry(%18 : $FakeOptional<C>)
loop_exit:
destroy_value %18 : $FakeOptional<C>
br exit
exit:
%retval = tuple ()
return %retval : $()
}
sil @make_c : $@convention(thin) (@guaranteed C) -> @owned C
sil @write_to_addr : $@convention(thin) (@inout C) -> ()
// CHECK-LABEL: sil [ossa] @extend_begin_borrow :
// CHECK: %1 = begin_borrow %0
// CHECK: %3 = apply
// CHECK: %4 = tuple ()
// CHECK-NEXT: end_borrow %1
// CHECK-NEXT: destroy_value %3
// CHECK: } // end sil function 'extend_begin_borrow'
sil [ossa] @extend_begin_borrow : $@convention(thin) (@owned C) -> () {
bb0(%0 : @owned $C):
specify_test "extend_borrow_scope %1 %3 true"
%1 = begin_borrow %0
%2 = function_ref @make_c : $@convention(thin) (@guaranteed C) -> @owned C
%3 = apply %2(%1) : $@convention(thin) (@guaranteed C) -> @owned C
end_borrow %1
%4 = tuple ()
destroy_value %3
destroy_value %0
return %4
}
// CHECK-LABEL: sil [ossa] @no_extend_needed :
// CHECK: destroy_value %3
// CHECK-NEXT: end_borrow %1
// CHECK: } // end sil function 'no_extend_needed'
sil [ossa] @no_extend_needed : $@convention(thin) (@owned C) -> () {
bb0(%0 : @owned $C):
specify_test "extend_borrow_scope %1 %3 true"
%1 = begin_borrow %0
%2 = function_ref @make_c : $@convention(thin) (@guaranteed C) -> @owned C
%3 = apply %2(%1) : $@convention(thin) (@guaranteed C) -> @owned C
destroy_value %3
end_borrow %1
destroy_value %0
%4 = tuple ()
return %4
}
// CHECK-LABEL: sil [ossa] @extend_begin_borrow_fail :
// CHECK: end_borrow %1
// CHECK-NEXT: destroy_value %0
// CHECK-NEXT: destroy_value %3
// CHECK: } // end sil function 'extend_begin_borrow_fail'
sil [ossa] @extend_begin_borrow_fail : $@convention(thin) (@owned C) -> () {
bb0(%0 : @owned $C):
specify_test "extend_borrow_scope %1 %3 false"
%1 = begin_borrow %0
%2 = function_ref @make_c : $@convention(thin) (@guaranteed C) -> @owned C
%3 = apply %2(%1) : $@convention(thin) (@guaranteed C) -> @owned C
end_borrow %1
destroy_value %0
destroy_value %3
%4 = tuple ()
return %4
}
// CHECK-LABEL: sil [ossa] @extend_function_arg :
// CHECK: } // end sil function 'extend_function_arg'
sil [ossa] @extend_function_arg : $@convention(thin) (@guaranteed C) -> () {
bb0(%0 : @guaranteed $C):
specify_test "extend_borrow_scope %0 %2 true"
%1 = function_ref @make_c : $@convention(thin) (@guaranteed C) -> @owned C
%2 = apply %1(%0) : $@convention(thin) (@guaranteed C) -> @owned C
destroy_value %2
%3 = tuple ()
return %3
}
// CHECK-LABEL: sil [ossa] @extend_load_borrow :
// CHECK: %1 = load_borrow %0
// CHECK: %3 = apply
// CHECK: %4 = tuple ()
// CHECK-NEXT: end_borrow %1
// CHECK-NEXT: destroy_value %3
// CHECK: } // end sil function 'extend_load_borrow'
sil [ossa] @extend_load_borrow : $@convention(thin) (@in_guaranteed C) -> () {
bb0(%0 : $*C):
specify_test "extend_borrow_scope %1 %3 true"
%1 = load_borrow %0
%2 = function_ref @make_c : $@convention(thin) (@guaranteed C) -> @owned C
%3 = apply %2(%1) : $@convention(thin) (@guaranteed C) -> @owned C
end_borrow %1
%4 = tuple ()
destroy_value %3
return %4
}
// CHECK-LABEL: sil [ossa] @extend_load_borrow_fail :
// CHECK: apply
// CHECK-NEXT: end_borrow %1
// CHECK: } // end sil function 'extend_load_borrow_fail'
sil [ossa] @extend_load_borrow_fail : $@convention(thin) (@inout C) -> () {
bb0(%0 : $*C):
specify_test "extend_borrow_scope %1 %3 false"
%1 = load_borrow %0
%2 = function_ref @make_c : $@convention(thin) (@guaranteed C) -> @owned C
%3 = apply %2(%1) : $@convention(thin) (@guaranteed C) -> @owned C
end_borrow %1
%4 = function_ref @write_to_addr : $@convention(thin) (@inout C) -> ()
apply %4(%0) : $@convention(thin) (@inout C) -> ()
destroy_value %3
%5 = tuple ()
return %5
}
// CHECK-LABEL: sil [ossa] @dont_extend_nested_borrow_scopes :
// CHECK: } // end sil function 'dont_extend_nested_borrow_scopes'
sil [ossa] @dont_extend_nested_borrow_scopes : $@convention(thin) (@guaranteed C) -> () {
bb0(%0 : @guaranteed $C):
specify_test "extend_borrow_scope %3 %5 false"
%1 = begin_borrow %0
%2 = struct $S (%1)
%3 = begin_borrow %2
%5 = apply undef(%3) : $@convention(thin) (@guaranteed S) -> @owned C
end_borrow %3
end_borrow %1
%7 = tuple ()
destroy_value %5
return %7
}
// CHECK-LABEL: sil [ossa] @extend_begin_borrow_multi_block1 :
// CHECK: bb1:
// CHECK-NEXT: debug_step
// CHECK-NEXT: end_borrow %1
// CHECK-NEXT: destroy_value %3
// CHECK: bb2:
// CHECK-NEXT: destroy_value %3
// CHECK-NEXT: end_borrow %1
// CHECK: } // end sil function 'extend_begin_borrow_multi_block1'
sil [ossa] @extend_begin_borrow_multi_block1 : $@convention(thin) (@owned C) -> () {
bb0(%0 : @owned $C):
specify_test "extend_borrow_scope %1 %3 true"
%1 = begin_borrow %0
%2 = function_ref @make_c : $@convention(thin) (@guaranteed C) -> @owned C
%3 = apply %2(%1) : $@convention(thin) (@guaranteed C) -> @owned C
cond_br undef, bb1, bb2
bb1:
end_borrow %1
debug_step
destroy_value %3
br bb3
bb2:
destroy_value %3
end_borrow %1
br bb3
bb3:
destroy_value %0
%r = tuple ()
return %r
}
// CHECK-LABEL: sil [ossa] @extend_begin_borrow_multi_block2 :
// CHECK: bb1:
// CHECK-NEXT: debug_step
// CHECK-NEXT: end_borrow %1
// CHECK-NEXT: destroy_value %3
// CHECK: bb2:
// CHECK-NEXT: end_borrow %1
// CHECK-NEXT: destroy_value %3
// CHECK: } // end sil function 'extend_begin_borrow_multi_block2'
sil [ossa] @extend_begin_borrow_multi_block2 : $@convention(thin) (@owned C) -> () {
bb0(%0 : @owned $C):
specify_test "extend_borrow_scope %1 %3 true"
%1 = begin_borrow %0
%2 = function_ref @make_c : $@convention(thin) (@guaranteed C) -> @owned C
%3 = apply %2(%1) : $@convention(thin) (@guaranteed C) -> @owned C
end_borrow %1
cond_br undef, bb1, bb2
bb1:
debug_step
destroy_value %3
br bb3
bb2:
destroy_value %3
br bb3
bb3:
destroy_value %0
%r = tuple ()
return %r
}
// CHECK-LABEL: sil [ossa] @extend_begin_borrow_multi_block_with_liverange_exit :
// CHECK: bb1:
// CHECK-NEXT: end_borrow %1
// CHECK-NEXT: fix_lifetime %2
// CHECK: bb2:
// CHECK-NEXT: end_borrow %1
// CHECK: } // end sil function 'extend_begin_borrow_multi_block_with_liverange_exit'
sil [ossa] @extend_begin_borrow_multi_block_with_liverange_exit : $@convention(thin) (@owned C) -> () {
bb0(%0 : @owned $C):
specify_test "extend_borrow_scope %1 %2 true"
%1 = begin_borrow %0
%2 = integer_literal $Builtin.Int32, 0
end_borrow %1
cond_br undef, bb1, bb2
bb1:
fix_lifetime %2
br bb3
bb2:
br bb3
bb3:
destroy_value %0
%r = tuple ()
return %r
}