Skip to content

Commit 53553fa

Browse files
committed
Fix swift::areUsesWithinValueLifetime for guaranteed values
To find if all the uses are within a guaranteed value, we should find all borrow introducers. swift::findOwnershipReferenceAggregate looks only through forwarding operations with single operands. For simplicity, continue using swift::findOwnershipReferenceAggregate, but return false when it does not find a borrow introducer.
1 parent f695875 commit 53553fa

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
@@ -471,8 +471,15 @@ bool swift::areUsesWithinValueLifetime(SILValue value, ArrayRef<Operand *> uses,
471471
return false;
472472
}
473473
if (value->getOwnershipKind() == OwnershipKind::Guaranteed) {
474+
// For guaranteed values, we have to find the borrow introducing guaranteed
475+
// reference roots and then ensure uses are within all of their lifetimes.
476+
// For simplicity, we only look through single forwarding operations to find
477+
// a borrow introducer here.
474478
value = findOwnershipReferenceAggregate(value);
475479
BorrowedValue borrowedValue(value);
480+
if (!borrowedValue) {
481+
return false;
482+
}
476483
if (!borrowedValue.isLocalScope()) {
477484
return true;
478485
}

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
214+
%5 = struct $DataWrapper (%4, %4)
215+
%6 = struct_extract %5, #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
219+
end_borrow %4
220+
%13 = objc_method %1, #MyObject.take!foreign : (MyObject.Type) -> (Data?) -> Data?, $@convention(objc_method) (Optional<NSData>, @objc_metatype MyObject.Type) -> @autoreleased Optional<NSData>
221+
%14 = apply %13(%9, %1) : $@convention(objc_method) (Optional<NSData>, @objc_metatype MyObject.Type) -> @autoreleased Optional<NSData>
222+
destroy_value %0
223+
destroy_value %3
224+
destroy_value %9
225+
return %14
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)