Skip to content

Commit 4fb0399

Browse files
authored
Merge pull request swiftlang#78982 from meg-gupta/fixmdi2
Fix AddressOwnership for unidentified access
2 parents dd33243 + 27fc324 commit 4fb0399

File tree

4 files changed

+57
-12
lines changed

4 files changed

+57
-12
lines changed

include/swift/SIL/OwnershipUtils.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,9 @@ struct AddressOwnership {
10491049

10501050
AddressOwnership(AccessBase base) : base(base) {}
10511051

1052-
operator bool() const { return bool(base); }
1052+
operator bool() const {
1053+
return bool(base) && base.getKind() != AccessRepresentation::Unidentified;
1054+
}
10531055

10541056
bool operator==(const AddressOwnership &other) const {
10551057
return base.hasIdenticalAccessInfo(other.base);

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ GuaranteedOwnershipExtension::Status
164164
GuaranteedOwnershipExtension::checkAddressOwnership(SILValue parentAddress,
165165
SILValue childAddress) {
166166
AddressOwnership addressOwnership(parentAddress);
167+
if (!addressOwnership) {
168+
return Invalid;
169+
}
167170
if (!addressOwnership.hasLocalOwnershipLifetime()) {
168171
// Indirect Arg, Stack, Global, Unidentified, Yield
169172
// (these have no reference lifetime to extend).
@@ -1409,6 +1412,12 @@ OwnershipRAUWHelper::OwnershipRAUWHelper(OwnershipFixupContext &inputCtx,
14091412
// NOTE: We also need to handle this here since a pointer_to_address is not a
14101413
// valid base value for an access path since it doesn't refer to any storage.
14111414
AddressOwnership addressOwnership(newValue);
1415+
1416+
if (!addressOwnership) {
1417+
invalidate();
1418+
return;
1419+
}
1420+
14121421
if (!addressOwnership.hasLocalOwnershipLifetime())
14131422
return;
14141423

test/SILOptimizer/cse_ossa.sil

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -565,13 +565,8 @@ sil [ossa] @sil_string_different_encodings : $@convention(thin) () -> Builtin.Wo
565565
}
566566

567567
// CHECK-LABEL: sil [ossa] @raw_idx_cse :
568-
// CHECK: integer_literal
569-
// CHECK-NEXT: index_raw_pointer
570-
// CHECK-NEXT: pointer_to_address
571-
// CHECK-NEXT: load
572-
// CHECK-NEXT: load
573-
// CHECK-NEXT: apply
574-
// CHECK-NEXT: tuple
568+
// CHECK: pointer_to_address
569+
// CHECK: pointer_to_address
575570
// CHECK-LABEL: } // end sil function 'raw_idx_cse'
576571
sil [ossa] @raw_idx_cse: $@convention(thin) (Builtin.RawPointer) -> () {
577572
bb0(%0 : $Builtin.RawPointer):
@@ -1091,10 +1086,8 @@ bb0(%0 : @guaranteed $Proto & Ping):
10911086
sil [ossa] [global_init] @$s4test10testGlobalSivau : $@convention(thin) () -> Builtin.RawPointer
10921087

10931088
// CHECK-LABEL: sil [ossa] @cse_global_init :
1094-
// CHECK: [[P:%[0-9]+]] = apply
1095-
// CHECK: [[A:%[0-9]+]] = pointer_to_address [[P]]
1096-
// CHECK: begin_access [modify] [dynamic] [no_nested_conflict] [[A]]
1097-
// CHECK: begin_access [read] [dynamic] [no_nested_conflict] [[A]]
1089+
// CHECK: pointer_to_address
1090+
// CHECK: pointer_to_address
10981091
// CHECK: // end sil function 'cse_global_init'
10991092
sil [ossa] @cse_global_init : $@convention(thin) () -> Int64 {
11001093
bb0:

test/SILOptimizer/cse_ossa_nontrivial.sil

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,3 +1033,44 @@ bb0(%0 : @owned $_NativeDictionary<Int, Klass>):
10331033
destroy_value %49
10341034
unreachable
10351035
}
1036+
1037+
struct Pointer<Element> {
1038+
@_hasStorage let pointer: UnsafePointer<Klass> { get }
1039+
}
1040+
1041+
struct Wrapper {
1042+
let k: Klass
1043+
var ptr: Pointer<Klass>
1044+
}
1045+
1046+
sil @use_wrapper : $@convention(thin) (@in_guaranteed Wrapper) -> ()
1047+
1048+
// CHECK-LABEL: sil [ossa] @testMarkDepNonEscAddressValue : $@convention(thin) (@in Wrapper) -> () {
1049+
// CHECK: mark_dependence
1050+
// CHECK: mark_dependence
1051+
// CHECK-LABEL: } // end sil function 'testMarkDepNonEscAddressValue'
1052+
sil [ossa] @testMarkDepNonEscAddressValue : $@convention(thin) (@in Wrapper) -> () {
1053+
bb0(%0 : $*Wrapper):
1054+
%f = function_ref @use_wrapper : $@convention(thin) (@in_guaranteed Wrapper) -> ()
1055+
%1 = load [copy] %0
1056+
%2 = begin_borrow %1
1057+
%3 = struct_extract %2, #Wrapper.ptr
1058+
%4 = struct_extract %3, #Pointer.pointer
1059+
%5 = struct_extract %4, #UnsafePointer._rawValue
1060+
%6 = pointer_to_address %5 to [strict] $*Wrapper
1061+
%7 = mark_dependence [nonescaping] %6 on %2
1062+
apply %f(%7) : $@convention(thin) (@in_guaranteed Wrapper) -> ()
1063+
end_borrow %2
1064+
%9 = begin_borrow %1
1065+
%10 = struct_extract %9, #Wrapper.ptr
1066+
%11 = struct_extract %10, #Pointer.pointer
1067+
%12 = struct_extract %11, #UnsafePointer._rawValue
1068+
%13 = pointer_to_address %12 to [strict] $*Wrapper
1069+
%14 = mark_dependence [nonescaping] %13 on %9
1070+
apply %f(%14) : $@convention(thin) (@in_guaranteed Wrapper) -> ()
1071+
end_borrow %9
1072+
destroy_value %1
1073+
destroy_addr %0
1074+
%18 = tuple ()
1075+
return %18
1076+
}

0 commit comments

Comments
 (0)