Skip to content

Commit 4ebc087

Browse files
authored
Merge pull request #85481 from meg-gupta/mutablespanbitwisecopyable
Update MutableSpan and OutputSpan
2 parents a1ce6e6 + c93163c commit 4ebc087

File tree

5 files changed

+108
-1086
lines changed

5 files changed

+108
-1086
lines changed

stdlib/public/core/Span/MutableSpan.swift

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ extension MutableSpan where Element: ~Copyable {
236236
extension MutableSpan where Element: ~Copyable {
237237

238238
@_alwaysEmitIntoClient
239+
@_semantics("fixed_storage.get_count")
239240
public var count: Int { _assumeNonNegative(_count) }
240241

241242
@_alwaysEmitIntoClient
@@ -279,7 +280,13 @@ extension MutableSpan where Element: BitwiseCopyable {
279280
@available(SwiftCompatibilitySpan 5.0, *)
280281
@_originallyDefinedIn(module: "Swift;CompatibilitySpan", SwiftCompatibilitySpan 6.2)
281282
extension MutableSpan where Element: ~Copyable {
282-
283+
// SILOptimizer looks for fixed_storage.check_index semantics for bounds check optimizations.
284+
@_semantics("fixed_storage.check_index")
285+
@inline(__always)
286+
@_alwaysEmitIntoClient
287+
internal func _checkIndex(_ position: Index) {
288+
_precondition(indices.contains(position), "index out of bounds")
289+
}
283290
/// Accesses the element at the specified position in the `MutableSpan`.
284291
///
285292
/// - Parameter position: The offset of the element to access. `position`
@@ -289,12 +296,12 @@ extension MutableSpan where Element: ~Copyable {
289296
@_alwaysEmitIntoClient
290297
public subscript(_ position: Index) -> Element {
291298
unsafeAddress {
292-
_precondition(indices.contains(position), "index out of bounds")
299+
_checkIndex(position)
293300
return unsafe UnsafePointer(_unsafeAddressOfElement(unchecked: position))
294301
}
295302
@lifetime(self: copy self)
296303
unsafeMutableAddress {
297-
_precondition(indices.contains(position), "index out of bounds")
304+
_checkIndex(position)
298305
return unsafe _unsafeAddressOfElement(unchecked: position)
299306
}
300307
}

stdlib/public/core/Span/OutputSpan.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ extension OutputSpan where Element: ~Copyable {
8282
extension OutputSpan where Element: ~Copyable {
8383
/// The number of initialized elements in this span.
8484
@_alwaysEmitIntoClient
85-
public var count: Int { _count }
85+
@_semantics("fixed_storage.get_count")
86+
public var count: Int { _assumeNonNegative(_count) }
8687

8788
/// The number of additional elements that can be added to this span.
8889
@_alwaysEmitIntoClient
@@ -190,7 +191,15 @@ extension OutputSpan where Element: ~Copyable {
190191
/// The range of initialized positions for this `OutputSpan`.
191192
@_alwaysEmitIntoClient
192193
public var indices: Range<Index> {
193-
unsafe Range(_uncheckedBounds: (0, _count))
194+
unsafe Range(_uncheckedBounds: (0, count))
195+
}
196+
197+
// SILOptimizer looks for fixed_storage.check_index semantics for bounds check optimizations.
198+
@_semantics("fixed_storage.check_index")
199+
@inline(__always)
200+
@_alwaysEmitIntoClient
201+
internal func _checkIndex(_ index: Index) {
202+
_precondition(indices.contains(index), "index out of bounds")
194203
}
195204

196205
/// Accesses the element at the specified position.
@@ -201,12 +210,12 @@ extension OutputSpan where Element: ~Copyable {
201210
@_alwaysEmitIntoClient
202211
public subscript(_ index: Index) -> Element {
203212
unsafeAddress {
204-
_precondition(indices.contains(index), "index out of bounds")
213+
_checkIndex(index)
205214
return unsafe UnsafePointer(_unsafeAddressOfElement(unchecked: index))
206215
}
207216
@lifetime(self: copy self)
208217
unsafeMutableAddress {
209-
_precondition(indices.contains(index), "index out of bounds")
218+
_checkIndex(index)
210219
return unsafe _unsafeAddressOfElement(unchecked: index)
211220
}
212221
}

stdlib/public/core/Span/Span.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ extension Span where Element: ~Copyable {
414414
@available(SwiftCompatibilitySpan 5.0, *)
415415
@_originallyDefinedIn(module: "Swift;CompatibilitySpan", SwiftCompatibilitySpan 6.2)
416416
extension Span where Element: ~Copyable {
417+
// SILOptimizer looks for fixed_storage.check_index semantics for bounds check optimizations.
417418
@_semantics("fixed_storage.check_index")
418419
@inline(__always)
419420
@_alwaysEmitIntoClient

0 commit comments

Comments
 (0)