Skip to content

Commit 3ef7b85

Browse files
authored
Merge pull request swiftlang#79091 from meg-gupta/fixapiscp
[6.1] Fix swift::areUsesWithinValueLifetime for guaranteed values
2 parents 1fbbebe + 6574608 commit 3ef7b85

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,15 @@ bool swift::areUsesWithinValueLifetime(SILValue value, ArrayRef<Operand *> uses,
468468
return false;
469469
}
470470
if (value->getOwnershipKind() == OwnershipKind::Guaranteed) {
471+
// For guaranteed values, we have to find the borrow introducing guaranteed
472+
// reference roots and then ensure uses are within all of their lifetimes.
473+
// For simplicity, we only look through single forwarding operations to find
474+
// a borrow introducer here.
471475
value = findOwnershipReferenceAggregate(value);
472476
BorrowedValue borrowedValue(value);
477+
if (!borrowedValue) {
478+
return false;
479+
}
473480
if (!borrowedValue.isLocalScope()) {
474481
return true;
475482
}

test/SILOptimizer/outliner_ossa.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import Foundation
1616

1717
struct DataWrapper {
1818
let data: Data
19+
let otherData: Data
1920
}
2021

2122
sil @getData : $@convention(thin) () -> @owned Data
@@ -200,6 +201,30 @@ bb0(%0: @owned $MyObject):
200201
return %51 : $Optional<NSData>
201202
}
202203

204+
// Not optimized
205+
// CHECK-LABEL: sil [Osize] [ossa] @test_struct_guaranteed :
206+
// CHECK: objc_method
207+
// CHECK-LABEL: } // end sil function 'test_struct_guaranteed'
208+
sil [Osize] [ossa] @test_struct_guaranteed : $@convention(thin) (@owned MyObject) -> @owned Optional<NSData> {
209+
bb0(%0 : @owned $MyObject):
210+
%1 = metatype $@objc_metatype MyObject.Type
211+
%2 = function_ref @getData : $@convention(thin) () -> @owned Data
212+
%3 = apply %2() : $@convention(thin) () -> @owned Data
213+
%4 = begin_borrow %3 : $Data
214+
%5 = struct $DataWrapper (%4 : $Data, %4 : $Data)
215+
%6 = struct_extract %5 : $DataWrapper, #DataWrapper.data
216+
%7 = function_ref @$s10Foundation4DataV19_bridgeToObjectiveCSo6NSDataCyF : $@convention(method) (@guaranteed Data) -> @owned NSData
217+
%8 = apply %7(%6) : $@convention(method) (@guaranteed Data) -> @owned NSData
218+
%9 = enum $Optional<NSData>, #Optional.some!enumelt, %8 : $NSData
219+
end_borrow %4 : $Data
220+
%11 = objc_method %1 : $@objc_metatype MyObject.Type, #MyObject.take!foreign : (MyObject.Type) -> (Data?) -> Data?, $@convention(objc_method) (Optional<NSData>, @objc_metatype MyObject.Type) -> @autoreleased Optional<NSData>
221+
%12 = apply %11(%9, %1) : $@convention(objc_method) (Optional<NSData>, @objc_metatype MyObject.Type) -> @autoreleased Optional<NSData>
222+
destroy_value %0 : $MyObject
223+
destroy_value %3 : $Data
224+
destroy_value %9 : $Optional<NSData>
225+
return %12 : $Optional<NSData>
226+
}
227+
203228
sil [Osize] [ossa] @test_dont_crash : $@convention(thin) (@owned MyObject) -> () {
204229
bb0(%0: @owned $MyObject):
205230
%35 = metatype $@objc_metatype MyObject.Type

0 commit comments

Comments
 (0)