Handle a special case of borrowed from instruction in CopyToBorrowOptimization

This commit is contained in:
Meghana Gupta
2025-04-10 12:38:04 -07:00
parent 21480b8021
commit fe780b670f
2 changed files with 47 additions and 0 deletions

View File

@@ -360,6 +360,13 @@ private extension Value {
}
var lookThroughForwardingInstructions: Value {
if let bfi = definingInstruction as? BorrowedFromInst,
!bfi.borrowedPhi.isReborrow,
bfi.enclosingValues.count == 1
{
// Return the single forwarded enclosingValue
return bfi.enclosingValues[0]
}
if let fi = definingInstruction as? ForwardingInstruction,
let forwardedOp = fi.singleForwardedOperand
{

View File

@@ -1,4 +1,5 @@
// RUN: %target-sil-opt -copy-to-borrow-optimization %s | %FileCheck %s
// REQUIRES: macosx
sil_stage canonical
@@ -2220,3 +2221,42 @@ sil [ossa] @keep_yield2ed_copy : $@convention(thin) () -> () {
%retval = tuple ()
return %retval : $()
}
// CHECK-LABEL: sil [ossa] @borrowed_from_forward1 : {{.*}} {
// CHECK-NOT: copy_value
// CHECK-LABEL: } // end sil function 'borrowed_from_forward1'
sil [ossa] @borrowed_from_forward1 : $@convention(thin) (@guaranteed C) -> () {
bb0(%0 : @guaranteed $C):
br bb1(%0)
bb1(%1 : @guaranteed $C):
%2 = borrowed %1 from (%0)
%copy = copy_value %2
%useC = function_ref @useC : $@convention(thin) (@guaranteed C) -> ()
apply %useC(%copy) : $@convention(thin) (@guaranteed C) -> ()
destroy_value %copy
%retval = tuple ()
return %retval : $()
}
// CHECK-LABEL: sil [ossa] @borrowed_from_forward2 : {{.*}} {
// CHECK-NOT: copy_value
// CHECK-LABEL: } // end sil function 'borrowed_from_forward2'
sil [ossa] @borrowed_from_forward2 : $@convention(thin) (@guaranteed Array<Int>) -> () {
bb0(%0 : @guaranteed $Array<Int>):
%1 = struct_extract %0, #Array._buffer
%2 = struct_extract %1, #_ArrayBuffer._storage
%3 = struct_extract %2, #_BridgeStorage.rawValue
%4 = unchecked_ref_cast %3 to $__ContiguousArrayStorageBase
br bb1(%4)
bb1(%6 : @guaranteed $__ContiguousArrayStorageBase):
%7 = borrowed %6 from (%0)
%8 = copy_value %7
%9 = begin_borrow %8
%10 = ref_element_addr [immutable] %9, #__ContiguousArrayStorageBase.countAndCapacity
end_borrow %9
destroy_value %8
%13 = tuple ()
return %13
}