Skip to content

Commit 2c462ba

Browse files
authored
Merge pull request swiftlang#80733 from meg-gupta/updatectb
Handle a special case of borrowed from instruction in CopyToBorrowOptimization
2 parents b278783 + fe780b6 commit 2c462ba

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/CopyToBorrowOptimization.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,13 @@ private extension Value {
360360
}
361361

362362
var lookThroughForwardingInstructions: Value {
363+
if let bfi = definingInstruction as? BorrowedFromInst,
364+
!bfi.borrowedPhi.isReborrow,
365+
bfi.enclosingValues.count == 1
366+
{
367+
// Return the single forwarded enclosingValue
368+
return bfi.enclosingValues[0]
369+
}
363370
if let fi = definingInstruction as? ForwardingInstruction,
364371
let forwardedOp = fi.singleForwardedOperand
365372
{

test/SILOptimizer/copy-to-borrow-optimization.sil

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %target-sil-opt -copy-to-borrow-optimization %s | %FileCheck %s
2+
// REQUIRES: macosx
23

34
sil_stage canonical
45

@@ -2220,3 +2221,42 @@ sil [ossa] @keep_yield2ed_copy : $@convention(thin) () -> () {
22202221
%retval = tuple ()
22212222
return %retval : $()
22222223
}
2224+
2225+
// CHECK-LABEL: sil [ossa] @borrowed_from_forward1 : {{.*}} {
2226+
// CHECK-NOT: copy_value
2227+
// CHECK-LABEL: } // end sil function 'borrowed_from_forward1'
2228+
sil [ossa] @borrowed_from_forward1 : $@convention(thin) (@guaranteed C) -> () {
2229+
bb0(%0 : @guaranteed $C):
2230+
br bb1(%0)
2231+
2232+
bb1(%1 : @guaranteed $C):
2233+
%2 = borrowed %1 from (%0)
2234+
%copy = copy_value %2
2235+
%useC = function_ref @useC : $@convention(thin) (@guaranteed C) -> ()
2236+
apply %useC(%copy) : $@convention(thin) (@guaranteed C) -> ()
2237+
destroy_value %copy
2238+
%retval = tuple ()
2239+
return %retval : $()
2240+
}
2241+
2242+
// CHECK-LABEL: sil [ossa] @borrowed_from_forward2 : {{.*}} {
2243+
// CHECK-NOT: copy_value
2244+
// CHECK-LABEL: } // end sil function 'borrowed_from_forward2'
2245+
sil [ossa] @borrowed_from_forward2 : $@convention(thin) (@guaranteed Array<Int>) -> () {
2246+
bb0(%0 : @guaranteed $Array<Int>):
2247+
%1 = struct_extract %0, #Array._buffer
2248+
%2 = struct_extract %1, #_ArrayBuffer._storage
2249+
%3 = struct_extract %2, #_BridgeStorage.rawValue
2250+
%4 = unchecked_ref_cast %3 to $__ContiguousArrayStorageBase
2251+
br bb1(%4)
2252+
2253+
bb1(%6 : @guaranteed $__ContiguousArrayStorageBase):
2254+
%7 = borrowed %6 from (%0)
2255+
%8 = copy_value %7
2256+
%9 = begin_borrow %8
2257+
%10 = ref_element_addr [immutable] %9, #__ContiguousArrayStorageBase.countAndCapacity
2258+
end_borrow %9
2259+
destroy_value %8
2260+
%13 = tuple ()
2261+
return %13
2262+
}

0 commit comments

Comments
 (0)