Skip to content

Commit 11912d2

Browse files
committed
Rewrite UnsafeRawBufferPointer.Iterator.next to avoid non natural loop in SIL
The current implementation creates a non-natural loop and none of the SIL and LLVM loop passes will work for such loops. We have to find a way to fix this in SIL. Until then, rewrite so we get a natural loop in SIL.
1 parent 423aa00 commit 11912d2

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,22 @@ extension UnsafeRawBufferPointer.Iterator: IteratorProtocol, Sequence {
153153
/// exists; otherwise, `nil`.
154154
@inlinable
155155
public mutating func next() -> UInt8? {
156-
if _position == _end { return nil }
157-
156+
guard let position = _position else {
157+
return nil
158+
}
158159
// We can do an unchecked unwrap here by borrowing invariants from the pointer.
159-
// For a validly constructed buffer pointer, the only way _position can be nil is
160-
// if _end is also nil. We checked that case above. Thus, we can safely do an
161-
// unchecked unwrap here.
162-
//
163-
// Additionally, validly constructed buffer pointers also have an _end that is
164-
// strictly greater than or equal to _position, and so we do not need to do checked
165-
// arithmetic here as we cannot possibly overflow.
166-
//
160+
// For a validly constructed buffer pointer, the only way _end can be nil is
161+
// if _position is also nil. We checked that case above.
162+
// Thus, we can safely do an unchecked unwrap here.
167163
// We check these invariants in debug builds to defend against invalidly constructed
168164
// pointers.
169-
_debugPrecondition(_position! < _end!)
170-
let position = _position._unsafelyUnwrappedUnchecked
165+
_debugPrecondition(_end != nil)
166+
let end = _end._unsafelyUnwrappedUnchecked
167+
if position == end { return nil }
171168
let result = position.load(as: UInt8.self)
169+
// Validly constructed buffer pointers also have an _end that is strictly
170+
// greater than or equal to _position.
171+
// So we do not need to do checked arithmetic here as we cannot possibly overflow.
172172
_position = position + 1
173173
return result
174174
}

0 commit comments

Comments
 (0)