Skip to content

Commit c5f3618

Browse files
committed
Update _overrideLifetime and use -O
1 parent 716cc5c commit c5f3618

File tree

2 files changed

+48
-36
lines changed

2 files changed

+48
-36
lines changed

test/SILOptimizer/Inputs/SpanExtras.swift

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,53 @@
11
import Builtin
22

3+
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
4+
/// a value identical to `dependent` with a lifetime dependency on the caller's
5+
/// borrow scope of the `source` argument.
6+
@unsafe
37
@_unsafeNonescapableResult
4-
@inlinable @inline(__always)
8+
@_alwaysEmitIntoClient
9+
@_transparent
510
@lifetime(borrow source)
6-
public func _overrideLifetime<
7-
T: ~Copyable & ~Escapable,
8-
U: ~Copyable & ~Escapable
11+
internal func _overrideLifetime<
12+
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
913
>(
10-
of dependent: consuming T,
11-
to source: borrowing U
14+
_ dependent: consuming T, borrowing source: borrowing U
1215
) -> T {
16+
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
17+
// should be expressed by a builtin that is hidden within the function body.
1318
dependent
1419
}
1520

21+
/// Unsafely discard any lifetime dependency on the `dependent` argument. Return
22+
/// a value identical to `dependent` that inherits all lifetime dependencies from
23+
/// the `source` argument.
24+
@unsafe
1625
@_unsafeNonescapableResult
17-
@inlinable @inline(__always)
26+
@_alwaysEmitIntoClient
27+
@_transparent
1828
@lifetime(source)
19-
public func _overrideLifetime<
20-
T: ~Copyable & ~Escapable,
21-
U: ~Copyable & ~Escapable
29+
internal func _overrideLifetime<
30+
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
2231
>(
23-
of dependent: consuming T,
24-
copyingFrom source: consuming U
32+
_ dependent: consuming T, copying source: borrowing U
2533
) -> T {
34+
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
35+
// should be expressed by a builtin that is hidden within the function body.
2636
dependent
2737
}
2838

39+
@unsafe
2940
@_unsafeNonescapableResult
30-
@inlinable @inline(__always)
41+
@_alwaysEmitIntoClient
42+
@_transparent
3143
@lifetime(source)
32-
public func _overrideLifetime<
33-
T: ~Copyable & ~Escapable,
34-
U: ~Copyable & ~Escapable
44+
internal func _overrideLifetime<
45+
T: ~Copyable & ~Escapable, U: ~Copyable & ~Escapable
3546
>(
36-
of dependent: consuming T,
37-
mutating source: inout U
47+
_ dependent: consuming T, mutating source: inout U
3848
) -> T {
49+
// TODO: Remove @_unsafeNonescapableResult. Instead, the unsafe dependence
50+
// should be expressed by a builtin that is hidden within the function body.
3951
dependent
4052
}
4153

@@ -91,7 +103,7 @@ extension MutableSpan where Element: ~Copyable {
91103
"baseAddress must be properly aligned to access Element"
92104
)
93105
let ms = MutableSpan<Element>(_unchecked: buffer)
94-
self = _overrideLifetime(of: ms, to: buffer)
106+
self = _overrideLifetime(ms, borrowing: buffer)
95107
}
96108

97109
@_alwaysEmitIntoClient
@@ -103,7 +115,7 @@ extension MutableSpan where Element: ~Copyable {
103115
precondition(count >= 0, "Count must not be negative")
104116
let buffer = UnsafeMutableBufferPointer(start: start, count: count)
105117
let ms = MutableSpan(_unsafeElements: buffer)
106-
self = _overrideLifetime(of: ms, to: start)
118+
self = _overrideLifetime(ms, borrowing: start)
107119
}
108120
}
109121

@@ -117,7 +129,7 @@ extension MutableSpan {
117129
) {
118130
let rb = UnsafeMutableBufferPointer(rebasing: elements)
119131
let ms = MutableSpan(_unsafeElements: rb)
120-
self = _overrideLifetime(of: ms, to: rb)
132+
self = _overrideLifetime(ms, borrowing: rb)
121133
}
122134
}
123135

@@ -142,7 +154,7 @@ extension MutableSpan where Element: BitwiseCopyable {
142154
count: count
143155
)
144156
let ms = MutableSpan(_unsafeElements: elements)
145-
self = _overrideLifetime(of: ms, to: buffer)
157+
self = _overrideLifetime(ms, borrowing: buffer)
146158
}
147159

148160
@_alwaysEmitIntoClient
@@ -154,7 +166,7 @@ extension MutableSpan where Element: BitwiseCopyable {
154166
precondition(byteCount >= 0, "Count must not be negative")
155167
let bytes = UnsafeMutableRawBufferPointer(start: pointer, count: byteCount)
156168
let ms = MutableSpan(_unsafeBytes: bytes)
157-
self = _overrideLifetime(of: ms, to: pointer)
169+
self = _overrideLifetime(ms, borrowing: pointer)
158170
}
159171

160172
@_alwaysEmitIntoClient
@@ -164,7 +176,7 @@ extension MutableSpan where Element: BitwiseCopyable {
164176
) {
165177
let bytes = UnsafeMutableRawBufferPointer(rebasing: buffer)
166178
let ms = MutableSpan(_unsafeBytes: bytes)
167-
self = _overrideLifetime(of: ms, to: buffer)
179+
self = _overrideLifetime(ms, borrowing: buffer)
168180
}
169181
}
170182

@@ -176,7 +188,7 @@ extension Span where Element: ~Copyable {
176188
let pointer = mutableSpan._pointer?.assumingMemoryBound(to: Element.self)
177189
let buffer = UnsafeBufferPointer(start: pointer, count: mutableSpan.count)
178190
let span = Span(_unsafeElements: buffer)
179-
self = _overrideLifetime(of: span, to: mutableSpan)
191+
self = _overrideLifetime(span, borrowing: mutableSpan)
180192
}
181193
}
182194

@@ -210,7 +222,7 @@ extension RawSpan {
210222
let byteCount = mutableSpan.count &* MemoryLayout<Element>.stride
211223
let buffer = UnsafeRawBufferPointer(start: pointer, count: byteCount)
212224
let rawSpan = RawSpan(_unsafeBytes: buffer)
213-
self = _overrideLifetime(of: rawSpan, to: mutableSpan)
225+
self = _overrideLifetime(rawSpan, borrowing: mutableSpan)
214226
}
215227
}
216228
@@ -724,7 +736,7 @@ extension OutputSpan where Element: ~Copyable {
724736
precondition(capacity >= 0, "Capacity must be 0 or greater")
725737
let buffer = UnsafeMutableBufferPointer(start: pointer, count: capacity)
726738
let os = OutputSpan(_initializing: buffer, initialized: initialized)
727-
self = _overrideLifetime(of: os, to: pointer)
739+
self = _overrideLifetime(os, borrowing: pointer)
728740
}
729741
}
730742

@@ -739,7 +751,7 @@ extension OutputSpan {
739751
) {
740752
let rebased = UnsafeMutableBufferPointer(rebasing: buffer)
741753
let os = OutputSpan(_initializing: rebased, initialized: 0)
742-
self = _overrideLifetime(of: os, to: buffer)
754+
self = _overrideLifetime(os, borrowing: buffer)
743755
}
744756
}
745757

@@ -764,7 +776,7 @@ extension OutputSpan where Element: BitwiseCopyable {
764776
let os = OutputSpan(
765777
_unchecked: pointer, capacity: count, initialized: initialized
766778
)
767-
self = _overrideLifetime(of: os, to: bytes)
779+
self = _overrideLifetime(os, borrowing: bytes)
768780
}
769781

770782
@_alwaysEmitIntoClient
@@ -777,7 +789,7 @@ extension OutputSpan where Element: BitwiseCopyable {
777789
precondition(capacity >= 0, "Capacity must be 0 or greater")
778790
let buffer = UnsafeMutableRawBufferPointer(start: pointer, count: capacity)
779791
let os = OutputSpan(_initializing: buffer, initialized: initialized)
780-
self = _overrideLifetime(of: os, to: pointer)
792+
self = _overrideLifetime(os, borrowing: pointer)
781793
}
782794
783795
@_alwaysEmitIntoClient
@@ -788,7 +800,7 @@ extension OutputSpan where Element: BitwiseCopyable {
788800
) {
789801
let rebased = UnsafeMutableRawBufferPointer(rebasing: buffer)
790802
let os = OutputSpan(_initializing: rebased, initialized: initialized)
791-
self = _overrideLifetime(of: os, to: buffer)
803+
self = _overrideLifetime(os, borrowing: buffer)
792804
}
793805
}
794806
@@ -1000,7 +1012,7 @@ extension OutputSpan where Element: ~Copyable {
10001012
let pointer = _pointer?.assumingMemoryBound(to: Element.self)
10011013
let buffer = UnsafeBufferPointer(start: pointer, count: _initialized)
10021014
let span = Span(_unsafeElements: buffer)
1003-
return _overrideLifetime(of: span, to: self)
1015+
return _overrideLifetime(span, borrowing: self)
10041016
}
10051017
}
10061018

@@ -1011,7 +1023,7 @@ extension OutputSpan where Element: ~Copyable {
10111023
let pointer = _pointer?.assumingMemoryBound(to: Element.self)
10121024
let buffer = UnsafeMutableBufferPointer(start: pointer, count: _initialized)
10131025
let span = MutableSpan(_unsafeElements: buffer)
1014-
return _overrideLifetime(of: span, mutating: &self)
1026+
return _overrideLifetime(span, mutating: &self)
10151027
}
10161028
}
10171029
}
@@ -1050,7 +1062,7 @@ extension Span {
10501062
// get {
10511063
// let nilBasedBuffer = UnsafeBufferPointer<Element>(start: nil, count: 0)
10521064
// let span = Span(_unsafeElements: nilBasedBuffer)
1053-
// return _overrideLifetime(of: span, to: immortalThing)
1065+
// return _overrideLifetime(span, to: immortalThing)
10541066
// }
10551067
// }
10561068
//
@@ -1059,7 +1071,7 @@ extension Span {
10591071
// public init() {
10601072
// let nilBasedBuffer = UnsafeBufferPointer<Element>(start: nil, count: 0)
10611073
// let span = Span(_unsafeElements: nilBasedBuffer)
1062-
// self = _overrideLifetime(of: span, to: immortalThing)
1074+
// self = _overrideLifetime(span, to: immortalThing)
10631075
// }
10641076
}
10651077

test/SILOptimizer/mutable_span_bounds_check_tests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %empty-directory(%t)
2-
// RUN: %target-swift-frontend -emit-module-path %t/SpanExtras.swiftmodule %S/Inputs/SpanExtras.swift -enable-builtin-module -enable-experimental-feature LifetimeDependence -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature Span
2+
// RUN: %target-swift-frontend -emit-module-path %t/SpanExtras.swiftmodule %S/Inputs/SpanExtras.swift -enable-builtin-module -enable-experimental-feature LifetimeDependence -enable-experimental-feature AllowUnsafeAttribute -enable-experimental-feature Span -O
33
// RUN: %target-swift-frontend -I %t -O -emit-sil %s -enable-experimental-feature Span -disable-availability-checking | %FileCheck %s --check-prefix=CHECK-SIL
44
// RUN: %target-swift-frontend -I %t -O -emit-ir %s -enable-experimental-feature Span -disable-availability-checking
55

0 commit comments

Comments
 (0)