Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.1] Fix swift::areUsesWithinValueLifetime for guaranteed values #79091

Open
wants to merge 1 commit into
base: release/6.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/SILOptimizer/Utils/OwnershipOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,15 @@ bool swift::areUsesWithinValueLifetime(SILValue value, ArrayRef<Operand *> uses,
return false;
}
if (value->getOwnershipKind() == OwnershipKind::Guaranteed) {
// For guaranteed values, we have to find the borrow introducing guaranteed
// reference roots and then ensure uses are within all of their lifetimes.
// For simplicity, we only look through single forwarding operations to find
// a borrow introducer here.
value = findOwnershipReferenceAggregate(value);
BorrowedValue borrowedValue(value);
if (!borrowedValue) {
return false;
}
if (!borrowedValue.isLocalScope()) {
return true;
}
Expand Down
25 changes: 25 additions & 0 deletions test/SILOptimizer/outliner_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import Foundation

struct DataWrapper {
let data: Data
let otherData: Data
}

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

// Not optimized
// CHECK-LABEL: sil [Osize] [ossa] @test_struct_guaranteed :
// CHECK: objc_method
// CHECK-LABEL: } // end sil function 'test_struct_guaranteed'
sil [Osize] [ossa] @test_struct_guaranteed : $@convention(thin) (@owned MyObject) -> @owned Optional<NSData> {
bb0(%0 : @owned $MyObject):
%1 = metatype $@objc_metatype MyObject.Type
%2 = function_ref @getData : $@convention(thin) () -> @owned Data
%3 = apply %2() : $@convention(thin) () -> @owned Data
%4 = begin_borrow %3
%5 = struct $DataWrapper (%4, %4)
%6 = struct_extract %5, #DataWrapper.data
%7 = function_ref @$s10Foundation4DataV19_bridgeToObjectiveCSo6NSDataCyF : $@convention(method) (@guaranteed Data) -> @owned NSData
%8 = apply %7(%6) : $@convention(method) (@guaranteed Data) -> @owned NSData
%9 = enum $Optional<NSData>, #Optional.some!enumelt, %8
end_borrow %4
%13 = objc_method %1, #MyObject.take!foreign : (MyObject.Type) -> (Data?) -> Data?, $@convention(objc_method) (Optional<NSData>, @objc_metatype MyObject.Type) -> @autoreleased Optional<NSData>
%14 = apply %13(%9, %1) : $@convention(objc_method) (Optional<NSData>, @objc_metatype MyObject.Type) -> @autoreleased Optional<NSData>
destroy_value %0
destroy_value %3
destroy_value %9
return %14
}

sil [Osize] [ossa] @test_dont_crash : $@convention(thin) (@owned MyObject) -> () {
bb0(%0: @owned $MyObject):
%35 = metatype $@objc_metatype MyObject.Type
Expand Down