Skip to content

Commit b56a787

Browse files
committed
SILGen: emit mark_dependence for unsafeAddress
Fixes a correctness issue with unsafe addressors: `unsafeAddress` and `unsafeMutableAddress`. Previously, the resulting `Unsafe[Mutable]Pointer` did not depend on `self`, meaning that the compiler is allowed to destroy `self` before any uses of the pointer. This happens to be valid for `UnsafePointer.pointee` because, in that case, `self` does not have a lifetime anyway; the correctness burden was on the programmer to use `withExtendedLifetime` around all uses of `self`. Now, unsafe addressors can be used for arbitrary `Self` types. This also enables lifetime dependence diagnostics when the addressor points to a `~Escapable` type. Addressors can now be used as an implementation of borrowed properties.
1 parent f2ad9f3 commit b56a787

File tree

6 files changed

+50
-23
lines changed

6 files changed

+50
-23
lines changed

lib/SILGen/SILGenApply.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7304,22 +7304,32 @@ ManagedValue SILGenFunction::emitAddressorAccessor(
73047304
emission.apply().getAll(results);
73057305

73067306
assert(results.size() == 1);
7307-
auto pointer = results[0].getUnmanagedValue();
7307+
auto result = results[0].getUnmanagedValue();
73087308

73097309
// Drill down to the raw pointer using intrinsic knowledge of those types.
73107310
auto pointerType =
7311-
pointer->getType().castTo<BoundGenericStructType>()->getDecl();
7311+
result->getType().castTo<BoundGenericStructType>()->getDecl();
73127312
auto props = pointerType->getStoredProperties();
73137313
assert(props.size() == 1);
73147314
VarDecl *rawPointerField = props[0];
7315-
pointer = B.createStructExtract(loc, pointer, rawPointerField,
7316-
SILType::getRawPointerType(getASTContext()));
7315+
auto rawPointer =
7316+
B.createStructExtract(loc, result, rawPointerField,
7317+
SILType::getRawPointerType(getASTContext()));
73177318

73187319
// Convert to the appropriate address type and return.
7319-
SILValue address = B.createPointerToAddress(loc, pointer, addressType,
7320+
SILValue address = B.createPointerToAddress(loc, rawPointer, addressType,
73207321
/*isStrict*/ true,
73217322
/*isInvariant*/ false);
7322-
7323+
// Create a dependency on self: the pointer is only valid as long as self is
7324+
// alive.
7325+
auto apply = cast<ApplyInst>(result);
7326+
// global addressors don't have a source value. Presumably, the addressor
7327+
// is the only way to get at them.
7328+
if (apply->hasSelfArgument()) {
7329+
auto selfSILValue = apply->getSelfArgument();
7330+
address = B.createMarkDependence(loc, address, selfSILValue,
7331+
MarkDependenceKind::Unresolved);
7332+
}
73237333
return ManagedValue::forLValue(address);
73247334
}
73257335

test/SILGen/addressors.swift

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ func test0() {
5959
// CHECK: [[T1:%.*]] = apply [[T0]]({{%.*}}, [[AVAL]])
6060
// CHECK: [[T2:%.*]] = struct_extract [[T1]] : $UnsafePointer<Int32>, #UnsafePointer._rawValue
6161
// CHECK: [[T3:%.*]] = pointer_to_address [[T2]] : $Builtin.RawPointer to [strict] $*Int32
62-
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[T3]] : $*Int32
62+
// CHECK: [[MD:%.*]] = mark_dependence [[T3]] : $*Int32 on [[AVAL]] : $A
63+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[MD]] : $*Int32
6364
// CHECK: [[Z:%.*]] = load [[ACCESS]] : $*Int32
6465
let z = a[10]
6566

@@ -68,7 +69,8 @@ func test0() {
6869
// CHECK: [[T1:%.*]] = apply [[T0]]({{%.*}}, [[WRITE]])
6970
// CHECK: [[T2:%.*]] = struct_extract [[T1]] : $UnsafeMutablePointer<Int32>, #UnsafeMutablePointer._rawValue
7071
// CHECK: [[T3:%.*]] = pointer_to_address [[T2]] : $Builtin.RawPointer to [strict] $*Int32
71-
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[T3]] : $*Int32
72+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[T3]] : $*Int32 on [[WRITE]] : $*A
73+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]] : $*Int32
7274
// CHECK: load
7375
// CHECK: sadd_with_overflow_Int{{32|64}}
7476
// CHECK: store {{%.*}} to [[ACCESS]]
@@ -79,7 +81,8 @@ func test0() {
7981
// CHECK: [[T1:%.*]] = apply [[T0]]({{%.*}}, [[WRITE]])
8082
// CHECK: [[T2:%.*]] = struct_extract [[T1]] : $UnsafeMutablePointer<Int32>, #UnsafeMutablePointer._rawValue
8183
// CHECK: [[T3:%.*]] = pointer_to_address [[T2]] : $Builtin.RawPointer to [strict] $*Int32
82-
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[T3]] : $*Int32
84+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[T3]] : $*Int32 on [[WRITE]] : $*A
85+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]] : $*Int32
8386
// CHECK: store {{%.*}} to [[ACCESS]]
8487
a[3] = 6
8588
}
@@ -93,7 +96,8 @@ func test1() -> Int32 {
9396
// CHECK: [[PTR:%.*]] = apply [[ACCESSOR]]({{%.*}}, [[A]]) : $@convention(method) (Int32, A) -> UnsafePointer<Int32>
9497
// CHECK: [[T0:%.*]] = struct_extract [[PTR]] : $UnsafePointer<Int32>, #UnsafePointer._rawValue
9598
// CHECK: [[T1:%.*]] = pointer_to_address [[T0]] : $Builtin.RawPointer to [strict] $*Int32
96-
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[T1]] : $*Int32
99+
// CHECK: [[MD:%.*]] = mark_dependence [[T1]] : $*Int32 on [[A]] : $A
100+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[MD]] : $*Int32
97101
// CHECK: [[T2:%.*]] = load [[ACCESS]] : $*Int32
98102
// CHECK: return [[T2]] : $Int32
99103
return A()[0]
@@ -147,7 +151,8 @@ struct B : Subscriptable {
147151
// CHECK: [[PTR:%.*]] = apply [[T0]]([[INDEX]], [[WRITE]])
148152
// CHECK: [[T0:%.*]] = struct_extract [[PTR]] : $UnsafeMutablePointer<Int32>,
149153
// CHECK: [[ADDR:%.*]] = pointer_to_address [[T0]] : $Builtin.RawPointer to [strict] $*Int32
150-
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[ADDR]] : $*Int32
154+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADDR]] : $*Int32 on [[WRITE]] : $*B
155+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]] : $*Int32
151156
// Accept either of struct_extract+load or load+struct_element_addr.
152157
// CHECK: load
153158
// CHECK: [[T1:%.*]] = builtin "or_Int32"
@@ -176,7 +181,8 @@ func test_carray(_ array: inout CArray<(Int32) -> Int32>) -> Int32 {
176181
// CHECK: [[T1:%.*]] = apply [[T0]]<(Int32) -> Int32>({{%.*}}, [[WRITE]])
177182
// CHECK: [[T2:%.*]] = struct_extract [[T1]] : $UnsafeMutablePointer<(Int32) -> Int32>, #UnsafeMutablePointer._rawValue
178183
// CHECK: [[T3:%.*]] = pointer_to_address [[T2]] : $Builtin.RawPointer to [strict] $*@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Int32, Int32>
179-
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[T3]]
184+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[T3]] : $*@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Int32, Int32> on [[WRITE]] : $*CArray<(Int32) -> Int32>
185+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]]
180186
// CHECK: store {{%.*}} to [[ACCESS]] :
181187
array[0] = id_int
182188

@@ -186,7 +192,9 @@ func test_carray(_ array: inout CArray<(Int32) -> Int32>) -> Int32 {
186192
// CHECK: [[T2:%.*]] = apply [[T1]]<(Int32) -> Int32>({{%.*}}, [[T0]])
187193
// CHECK: [[T3:%.*]] = struct_extract [[T2]] : $UnsafePointer<(Int32) -> Int32>, #UnsafePointer._rawValue
188194
// CHECK: [[T4:%.*]] = pointer_to_address [[T3]] : $Builtin.RawPointer to [strict] $*@callee_guaranteed @substituted <τ_0_0, τ_0_1> (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Int32, Int32>
189-
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[T4]]
195+
// CHECK: [[MD:%.*]] = mark_dependence [[T4]] : $*@callee_guaranteed @substituted <τ_0_0, τ_0_1>
196+
// (@in_guaranteed τ_0_0) -> @out τ_0_1 for <Int32, Int32> on [[T0]] : $CArray<(Int32) -> Int32>
197+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[MD]]
190198
// CHECK: [[T5:%.*]] = load [[ACCESS]]
191199
return array[1](5)
192200
}
@@ -209,7 +217,8 @@ struct D : Subscriptable {
209217
// SILGEN: [[PTR:%.*]] = apply [[T0]]([[I]], [[ACCESS]])
210218
// SILGEN: [[T0:%.*]] = struct_extract [[PTR]] : $UnsafeMutablePointer<Int32>,
211219
// SILGEN: [[ADDR:%.*]] = pointer_to_address [[T0]] : $Builtin.RawPointer to [strict] $*Int32
212-
// SILGEN: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[ADDR]] : $*Int32
220+
// SILGEN: [[MD:%.*]] = mark_dependence [unresolved] [[ADDR]] : $*Int32 on %6 : $*D
221+
// SILGEN: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]] : $*Int32
213222
// SILGEN: assign [[VALUE]] to [[ACCESS]] : $*Int32
214223

215224
// SILGEN-LABEL: sil hidden [transparent] [ossa] @$s10addressors1DVys5Int32VAEciM
@@ -219,7 +228,8 @@ struct D : Subscriptable {
219228
// SILGEN: [[PTR:%.*]] = apply [[T0]]([[I]], [[SELF_ACCESS]])
220229
// SILGEN: [[ADDR_TMP:%.*]] = struct_extract [[PTR]] : $UnsafeMutablePointer<Int32>,
221230
// SILGEN: [[ADDR:%.*]] = pointer_to_address [[ADDR_TMP]]
222-
// SILGEN: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[ADDR]]
231+
// SILGEN: [[MD:%.*]] = mark_dependence [unresolved] [[ADDR]] : $*Int32 on [[SELF_ACCESS]] : $*D
232+
// SILGEN: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]]
223233
// SILGEN: yield [[ACCESS]]
224234
// SILGEN: end_access [[ACCESS]]
225235

@@ -236,7 +246,8 @@ func test_d(_ array: inout D) -> Int32 {
236246
// CHECK: [[T1:%.*]] = apply [[T0]]({{%.*}}, [[WRITE]])
237247
// CHECK: [[T2:%.*]] = struct_extract [[T1]] : $UnsafeMutablePointer<Int32>,
238248
// CHECK: [[ADDR:%.*]] = pointer_to_address [[T2]] : $Builtin.RawPointer to [strict] $*Int32
239-
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[ADDR]] : $*Int32
249+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADDR]] : $*Int32 on [[WRITE]] : $*D
250+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]] : $*Int32
240251
// CHECK: store [[V]] to [[ACCESS]] : $*Int32
241252
array[0] = make_int()
242253

@@ -245,7 +256,8 @@ func test_d(_ array: inout D) -> Int32 {
245256
// CHECK: [[T1:%.*]] = apply [[T0]]({{%.*}}, [[WRITE]])
246257
// CHECK: [[T2:%.*]] = struct_extract [[T1]] : $UnsafeMutablePointer<Int32>,
247258
// CHECK: [[ADDR:%.*]] = pointer_to_address [[T2]] : $Builtin.RawPointer to [strict] $*Int32
248-
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[ADDR]] : $*Int32
259+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] [[ADDR]] : $*Int32 on [[WRITE]] : $*D
260+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]] : $*Int32
249261
// CHECK: [[FN:%.*]] = function_ref @$s10addressors14take_int_inoutyys5Int32VzF
250262
// CHECK: apply [[FN]]([[ACCESS]])
251263
take_int_inout(&array[1])
@@ -271,7 +283,8 @@ struct E {
271283
// CHECK: [[T1:%.*]] = apply [[T0]]([[E]])
272284
// CHECK: [[T2:%.*]] = struct_extract [[T1]]
273285
// CHECK: [[T3:%.*]] = pointer_to_address [[T2]]
274-
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[T3]] : $*Int32
286+
// CHECK: [[MD:%.*]] = mark_dependence [[T3]] : $*Int32 on %0 : $E
287+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]] : $*Int32
275288
// CHECK: store {{%.*}} to [[ACCESS]] : $*Int32
276289
func test_e(_ e: E) {
277290
e.value = 0

test/SILGen/borrow_from_load_expr.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public struct FA<T> {
1616
// CHECK: [[POINTER:%[^,]+]] = apply [[ADDRESSOR]]
1717
// CHECK: [[RAW_POINTER:%[^,]+]] = struct_extract [[POINTER]]
1818
// CHECK: [[ADDR:%[^,]+]] = pointer_to_address [[RAW_POINTER]]
19-
// CHECK: [[ACCESS:%[^,]+]] = begin_access [read] [unsafe] [[ADDR]]
19+
// CHECK: [[MD:%.*]] = mark_dependence [unresolved] [[ADDR]] : $*T
20+
// CHECK: [[ACCESS:%[^,]+]] = begin_access [read] [unsafe] [[MD]]
2021
// Verify that no spurious temporary is emitted.
2122
// CHECK-NOT: alloc_stack
2223
// CHECK: yield [[ACCESS]] : $*T, resume [[SUCCESS:bb[0-9]+]], unwind [[FAILURE:bb[0-9]+]]

test/SILGen/moveonly_subscript_addressor.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ func load(b: UnsafeMutableBufferPointer<Foo>) -> Int {
1212
// Ensure the borrowing invocation of `load` happens within the access to
1313
// the pointed-at memory.
1414
// CHECK: [[PTR:%.*]] = pointer_to_address
15-
// CHECK: [[BEGIN:%.*]] = begin_access [read] [unsafe] [[PTR]]
15+
// CHECK: [[MD:%.*]] = mark_dependence [unresolved] [[PTR]] : $*Foo on %0 : $UnsafeMutableBufferPointer<Foo>
16+
// CHECK: [[BEGIN:%.*]] = begin_access [read] [unsafe] [[MD]]
1617
// CHECK: [[FN:%.*]] = function_ref @load
1718
// CHECK: apply [[FN]]
1819
// CHECK: end_access [[BEGIN]]

test/SILOptimizer/access_marker_verify.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,8 @@ func testAddressor(p: UnsafePointer<Int>) -> Int {
606606
// CHECK: apply
607607
// CHECK: struct_extract
608608
// CHECK: [[ADR:%.*]] = pointer_to_address
609-
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[ADR]] : $*Int
609+
// CHECK: [[MD:%.*]] = mark_dependence [unresolved] [[ADR]] : $*Int on %0 : $UnsafePointer<Int>
610+
// CHECK: [[ACCESS:%.*]] = begin_access [read] [unsafe] [[MD]] : $*Int
610611
// CHECK: load [trivial] [[ACCESS]] : $*Int
611612
// CHECK: return
612613
// CHECK-LABEL: } // end sil function '$s20access_marker_verify13testAddressor1pSiSPySiG_tF'
@@ -1021,7 +1022,8 @@ func testPointerInit(x: Int, y: UnsafeMutablePointer<Int>) {
10211022
// CHECK: [[POINTEE:%.*]] = apply %{{.*}}<Int>(%1) : $@convention(method) <τ_0_0 where τ_0_0 : ~Copyable> (UnsafeMutablePointer<τ_0_0>) -> UnsafeMutablePointer<τ_0_0>
10221023
// CHECK: [[RAWPTR:%.*]] = struct_extract [[POINTEE]] : $UnsafeMutablePointer<Int>, #UnsafeMutablePointer._rawValue
10231024
// CHECK: [[ADR:%.*]] = pointer_to_address [[RAWPTR]] : $Builtin.RawPointer to [strict] $*Int
1024-
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[ADR]] : $*Int
1025+
// CHECK: [[MD:%.*]] = mark_dependence [unresolved] [[ADR]] : $*Int on %1 : $UnsafeMutablePointer<Int> // user: %9
1026+
// CHECK: [[ACCESS:%.*]] = begin_access [modify] [unsafe] [[MD]] : $*Int
10251027
// CHECK: assign %0 to [[ACCESS]] : $*Int
10261028
// CHECK-LABEL: } // end sil function '$s20access_marker_verify15testPointerInit1x1yySi_SpySiGtF'
10271029

test/SILOptimizer/moveonly_unsafeAddress.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ bb0(%0 : $*SNC):
4343
%6 = apply %5(%4) : $@convention(method) (@guaranteed SNC) -> UnsafePointer<NC>
4444
%7 = struct_extract %6 : $UnsafePointer<NC>, #UnsafePointer._rawValue
4545
%8 = pointer_to_address %7 : $Builtin.RawPointer to [strict] $*NC
46-
%9 = mark_dependence [unresolved] %8 : $*NC on %3 : $*SNC
46+
%9 = mark_dependence [nonescaping] %8 : $*NC on %3 : $*SNC
4747
%10 = begin_access [read] [unsafe] %9 : $*NC
4848
%11 = mark_unresolved_non_copyable_value [no_consume_or_assign] %10 : $*NC // expected-error {{'unknown' is borrowed and cannot be consumed}}
4949
%12 = load [copy] %11 : $*NC

0 commit comments

Comments
 (0)