Skip to content

Commit 385a85c

Browse files
committed
Add assumeAlignment to UnsafeRawPointer.load()
To preserve current behavior. I expect this builtin to be removed in the default case after proposing the change in Swift Evolution.
1 parent 370a4ea commit 385a85c

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

stdlib/public/core/UnsafeRawPointer.swift

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,13 @@ public struct UnsafeRawPointer: _Pointer, Sendable {
355355
& (UInt(MemoryLayout<T>.alignment) - 1)),
356356
"load from misaligned raw pointer")
357357

358-
return Builtin.loadRaw((self + offset)._rawValue)
358+
let rawPointer = (self + offset)._rawValue
359+
360+
// TODO: to support misaligned raw loads, simply remove this assumption.
361+
let alignedPointer =
362+
Builtin.assumeAlignment(rawPointer,
363+
MemoryLayout<T>.alignment._builtinWordValue)
364+
return Builtin.loadRaw(alignedPointer)
359365
}
360366

361367
}
@@ -901,7 +907,13 @@ public struct UnsafeMutableRawPointer: _Pointer, Sendable {
901907
& (UInt(MemoryLayout<T>.alignment) - 1)),
902908
"load from misaligned raw pointer")
903909

904-
return Builtin.loadRaw((self + offset)._rawValue)
910+
let rawPointer = (self + offset)._rawValue
911+
912+
// TODO: to support misaligned raw loads, simply remove this assumption.
913+
let alignedPointer =
914+
Builtin.assumeAlignment(rawPointer,
915+
MemoryLayout<T>.alignment._builtinWordValue)
916+
return Builtin.loadRaw(alignedPointer)
905917
}
906918

907919
/// Stores the given value's bytes into raw memory at the specified offset.

0 commit comments

Comments
 (0)