Skip to content

Commit 35f10fa

Browse files
authored
Merge pull request #79222 from meg-gupta/boundstestsupdate
[NFC] Update span unit tests for bounds check optimizations
2 parents 73c875d + 2ac449a commit 35f10fa

File tree

2 files changed

+88
-202
lines changed

2 files changed

+88
-202
lines changed

test/SILOptimizer/Inputs/SpanExtras.swift

Lines changed: 45 additions & 174 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
@@ -516,45 +528,7 @@ extension MutableSpan {
516528
}
517529

518530
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)
519-
extension MutableSpan where Element: ~Copyable {
520531

521-
@_alwaysEmitIntoClient
522-
public mutating func moveUpdate(
523-
fromContentsOf source: consuming OutputSpan<Element>
524-
) -> Index {
525-
guard !source.isEmpty else { return 0 }
526-
precondition(
527-
source.count <= self.count,
528-
"destination span cannot contain every element from source."
529-
)
530-
let buffer = source.relinquishBorrowedMemory()
531-
// we must now deinitialize the returned UMBP
532-
_start().moveInitializeMemory(
533-
as: Element.self, from: buffer.baseAddress!, count: buffer.count
534-
)
535-
return buffer.count
536-
}
537-
538-
public mutating func moveUpdate(
539-
fromContentsOf source: UnsafeMutableBufferPointer<Element>
540-
) -> Index {
541-
let source = OutputSpan(_initializing: source, initialized: source.count)
542-
return self.moveUpdate(fromContentsOf: source)
543-
}
544-
}
545-
546-
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)
547-
extension MutableSpan {
548-
549-
public mutating func moveUpdate(
550-
fromContentsOf source: Slice<UnsafeMutableBufferPointer<Element>>
551-
) -> Index {
552-
self.moveUpdate(fromContentsOf: .init(rebasing: source))
553-
}
554-
}
555-
556-
557-
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)
558532
extension MutableSpan where Element: BitwiseCopyable {
559533

560534
@_alwaysEmitIntoClient
@@ -686,115 +660,12 @@ public struct OutputSpan<Element: ~Copyable>: ~Copyable, ~Escapable {
686660
@available(*, unavailable)
687661
extension OutputSpan: Sendable {}
688662

689-
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)
690-
extension OutputSpan where Element: ~Copyable {
691-
692-
@usableFromInline @inline(__always)
693-
@lifetime(borrow buffer)
694-
init(
695-
_unchecked buffer: UnsafeMutableBufferPointer<Element>,
696-
initialized: Int
697-
) {
698-
_pointer = .init(buffer.baseAddress)
699-
capacity = buffer.count
700-
_initialized = initialized
701-
}
702-
703-
@_alwaysEmitIntoClient
704-
@lifetime(borrow buffer)
705-
public init(
706-
_initializing buffer: UnsafeMutableBufferPointer<Element>,
707-
initialized: Int = 0
708-
) {
709-
precondition(
710-
((Int(bitPattern: buffer.baseAddress) &
711-
(MemoryLayout<Element>.alignment&-1)) == 0),
712-
"baseAddress must be properly aligned to access Element"
713-
)
714-
self.init(_unchecked: buffer, initialized: initialized)
715-
}
716-
717-
@_alwaysEmitIntoClient
718-
@lifetime(borrow pointer)
719-
public init(
720-
_initializing pointer: UnsafeMutablePointer<Element>,
721-
capacity: Int,
722-
initialized: Int = 0
723-
) {
724-
precondition(capacity >= 0, "Capacity must be 0 or greater")
725-
let buffer = UnsafeMutableBufferPointer(start: pointer, count: capacity)
726-
let os = OutputSpan(_initializing: buffer, initialized: initialized)
727-
self = _overrideLifetime(of: os, to: pointer)
728-
}
729-
}
730-
731-
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)
732-
extension OutputSpan {
733-
734-
@_alwaysEmitIntoClient
735-
@lifetime(borrow buffer)
736-
public init(
737-
_initializing buffer: borrowing Slice<UnsafeMutableBufferPointer<Element>>,
738-
initialized: Int = 0
739-
) {
740-
let rebased = UnsafeMutableBufferPointer(rebasing: buffer)
741-
let os = OutputSpan(_initializing: rebased, initialized: 0)
742-
self = _overrideLifetime(of: os, to: buffer)
743-
}
744-
}
745-
746-
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)
747-
extension OutputSpan where Element: BitwiseCopyable {
748-
749-
@_alwaysEmitIntoClient
750-
@lifetime(borrow bytes)
751-
public init(
752-
_initializing bytes: UnsafeMutableRawBufferPointer,
753-
initialized: Int = 0
754-
) {
755-
precondition(
756-
((Int(bitPattern: bytes.baseAddress) &
757-
(MemoryLayout<Element>.alignment&-1)) == 0),
758-
"baseAddress must be properly aligned to access Element"
759-
)
760-
let (byteCount, stride) = (bytes.count, MemoryLayout<Element>.stride)
761-
let (count, remainder) = byteCount.quotientAndRemainder(dividingBy: stride)
762-
precondition(remainder == 0, "Span must contain a whole number of elements")
763-
let pointer = bytes.baseAddress
764-
let os = OutputSpan(
765-
_unchecked: pointer, capacity: count, initialized: initialized
766-
)
767-
self = _overrideLifetime(of: os, to: bytes)
768-
}
769-
770-
@_alwaysEmitIntoClient
771-
@lifetime(borrow pointer)
772-
public init(
773-
_initializing pointer: UnsafeMutableRawPointer,
774-
capacity: Int,
775-
initialized: Int = 0
776-
) {
777-
precondition(capacity >= 0, "Capacity must be 0 or greater")
778-
let buffer = UnsafeMutableRawBufferPointer(start: pointer, count: capacity)
779-
let os = OutputSpan(_initializing: buffer, initialized: initialized)
780-
self = _overrideLifetime(of: os, to: pointer)
781-
}
782-
783-
@_alwaysEmitIntoClient
784-
@lifetime(borrow buffer)
785-
public init(
786-
_initializing buffer: borrowing Slice<UnsafeMutableRawBufferPointer>,
787-
initialized: Int = 0
788-
) {
789-
let rebased = UnsafeMutableRawBufferPointer(rebasing: buffer)
790-
let os = OutputSpan(_initializing: rebased, initialized: initialized)
791-
self = _overrideLifetime(of: os, to: buffer)
792-
}
793-
}
794-
795663
@available(macOS 9999, iOS 9999, tvOS 9999, watchOS 9999, visionOS 9999, *)
796664
extension OutputSpan where Element: ~Copyable {
797665

666+
@available(macOS 9999, *)
667+
@available(macOS 9999, *)
668+
@available(macOS 9999, *)
798669
@_alwaysEmitIntoClient
799670
public mutating func append(_ value: consuming Element) {
800671
precondition(_initialized < capacity, "Output buffer overflow")
@@ -1000,7 +871,7 @@ extension OutputSpan where Element: ~Copyable {
1000871
let pointer = _pointer?.assumingMemoryBound(to: Element.self)
1001872
let buffer = UnsafeBufferPointer(start: pointer, count: _initialized)
1002873
let span = Span(_unsafeElements: buffer)
1003-
return _overrideLifetime(of: span, to: self)
874+
return _overrideLifetime(span, borrowing: self)
1004875
}
1005876
}
1006877

@@ -1011,7 +882,7 @@ extension OutputSpan where Element: ~Copyable {
1011882
let pointer = _pointer?.assumingMemoryBound(to: Element.self)
1012883
let buffer = UnsafeMutableBufferPointer(start: pointer, count: _initialized)
1013884
let span = MutableSpan(_unsafeElements: buffer)
1014-
return _overrideLifetime(of: span, mutating: &self)
885+
return _overrideLifetime(span, mutating: &self)
1015886
}
1016887
}
1017888
}
@@ -1050,7 +921,7 @@ extension Span {
1050921
// get {
1051922
// let nilBasedBuffer = UnsafeBufferPointer<Element>(start: nil, count: 0)
1052923
// let span = Span(_unsafeElements: nilBasedBuffer)
1053-
// return _overrideLifetime(of: span, to: immortalThing)
924+
// return _overrideLifetime(span, to: immortalThing)
1054925
// }
1055926
// }
1056927
//
@@ -1059,7 +930,7 @@ extension Span {
1059930
// public init() {
1060931
// let nilBasedBuffer = UnsafeBufferPointer<Element>(start: nil, count: 0)
1061932
// let span = Span(_unsafeElements: nilBasedBuffer)
1062-
// self = _overrideLifetime(of: span, to: immortalThing)
933+
// self = _overrideLifetime(span, to: immortalThing)
1063934
// }
1064935
}
1065936

0 commit comments

Comments
 (0)