Skip to content

Commit 961e6ea

Browse files
authored
Merge pull request #76294 from meg-gupta/rewritenext
Rewrite UnsafeRawBufferPointer.Iterator.next
2 parents 33b88b9 + 2f37e10 commit 961e6ea

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

include/swift/SIL/Dominance.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ class DominanceInfo : public DominatorTreeBase {
9393
}
9494

9595
#ifndef NDEBUG
96-
void dump() { print(llvm::errs()); };
96+
void dump() LLVM_ATTRIBUTE_USED { print(llvm::errs()); }
9797
#endif
9898
};
9999

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,23 @@ 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 }
168+
_debugPrecondition(position < end)
171169
let result = position.load(as: UInt8.self)
170+
// Validly constructed buffer pointers also have an _end that is strictly
171+
// greater than or equal to _position.
172+
// So we do not need to do checked arithmetic here as we cannot possibly overflow.
172173
_position = position + 1
173174
return result
174175
}

test/LLVMPasses/loopinfo.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-irgen -O -o %t/loopinfo.ll %s
3+
// RUN: %swift-llvm-opt -passes='print<loops>' %t/loopinfo.ll 2>&1 | %FileCheck %s
4+
5+
// CHECK: Loop at depth 1 containing
6+
public func iterate1(urbp: UnsafeRawBufferPointer) -> Int {
7+
var s = 0
8+
for v in urbp {
9+
s += Int(v)
10+
}
11+
return s
12+
}
13+
14+
// CHECK: Loop at depth 1 containing
15+
public func iterate2(ubp: UnsafeBufferPointer<Int>) -> Int {
16+
var s = 0
17+
for v in ubp {
18+
s += v
19+
}
20+
return s
21+
}
22+
23+
24+

0 commit comments

Comments
 (0)