Skip to content

Commit 684739e

Browse files
Merge pull request #70248 from nate-chandler/rdar96956089
[stdlib] BitwiseCopyable loadUnaligned overloads.
2 parents d005743 + b7f5171 commit 684739e

File tree

5 files changed

+63
-26
lines changed

5 files changed

+63
-26
lines changed

lib/IRGen/GenArchetype.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -433,16 +433,14 @@ const TypeInfo *TypeConverter::convertArchetypeType(ArchetypeType *archetype) {
433433
: IsNotABIAccessible;
434434
}
435435

436-
auto &ASTContext = IGM.getSwiftModule()->getASTContext();
437-
if (ASTContext.LangOpts.hasFeature(Feature::BitwiseCopyable)) {
438-
// TODO: Should this conformance imply isAddressOnlyTrivial is true?
439-
auto *proto = ASTContext.getProtocol(KnownProtocolKind::BitwiseCopyable);
440-
// It's possible for the protocol not to exist if the stdlib is built
441-
// no_asserts.
442-
if (proto && IGM.getSwiftModule()->lookupConformance(archetype, proto)) {
443-
return BitwiseCopyableArchetypeTypeInfo::create(storageType,
444-
abiAccessible);
445-
}
436+
// TODO: Should this conformance imply isAddressOnlyTrivial is true?
437+
auto *bitwiseCopyableProtocol =
438+
IGM.getSwiftModule()->getASTContext().getProtocol(
439+
KnownProtocolKind::BitwiseCopyable);
440+
// The protocol won't be present in swiftinterfaces from older SDKs.
441+
if (bitwiseCopyableProtocol && IGM.getSwiftModule()->lookupConformance(
442+
archetype, bitwiseCopyableProtocol)) {
443+
return BitwiseCopyableArchetypeTypeInfo::create(storageType, abiAccessible);
446444
}
447445

448446
return OpaqueArchetypeTypeInfo::create(storageType, abiAccessible);

stdlib/public/core/UnsafeBufferPointerSlice.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,17 @@ extension Slice where Base == UnsafeMutableRawBufferPointer {
378378
/// with `type`.
379379
/// - Returns: A new instance of type `T`, copied from the buffer pointer's
380380
/// memory.
381+
#if $BitwiseCopyable
382+
@inlinable
383+
@_alwaysEmitIntoClient
384+
public func loadUnaligned<T : _BitwiseCopyable>(
385+
fromByteOffset offset: Int = 0,
386+
as type: T.Type
387+
) -> T {
388+
let buffer = Base(rebasing: self)
389+
return buffer.loadUnaligned(fromByteOffset: offset, as: T.self)
390+
}
391+
#endif
381392
@inlinable
382393
@_alwaysEmitIntoClient
383394
public func loadUnaligned<T>(
@@ -606,6 +617,17 @@ extension Slice where Base == UnsafeRawBufferPointer {
606617
/// with `type`.
607618
/// - Returns: A new instance of type `T`, copied from the buffer pointer's
608619
/// memory.
620+
#if $BitwiseCopyable
621+
@inlinable
622+
@_alwaysEmitIntoClient
623+
public func loadUnaligned<T : _BitwiseCopyable>(
624+
fromByteOffset offset: Int = 0,
625+
as type: T.Type
626+
) -> T {
627+
let buffer = Base(rebasing: self)
628+
return buffer.loadUnaligned(fromByteOffset: offset, as: T.self)
629+
}
630+
#endif
609631
@inlinable
610632
@_alwaysEmitIntoClient
611633
public func loadUnaligned<T>(

stdlib/public/core/UnsafeRawBufferPointer.swift.gyb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,18 @@ extension Unsafe${Mutable}RawBufferPointer {
430430
/// with `type`.
431431
/// - Returns: A new instance of type `T`, copied from the buffer pointer's
432432
/// memory.
433+
#if $BitwiseCopyable
434+
@_alwaysEmitIntoClient
435+
public func loadUnaligned<T : _BitwiseCopyable>(
436+
fromByteOffset offset: Int = 0,
437+
as type: T.Type
438+
) -> T {
439+
_debugPrecondition(offset >= 0, "${Self}.load with negative offset")
440+
_debugPrecondition(offset + MemoryLayout<T>.size <= self.count,
441+
"${Self}.load out of bounds")
442+
return baseAddress!.loadUnaligned(fromByteOffset: offset, as: T.self)
443+
}
444+
#endif
433445
@_alwaysEmitIntoClient
434446
public func loadUnaligned<T>(
435447
fromByteOffset offset: Int = 0,

stdlib/public/core/UnsafeRawPointer.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,16 @@ public struct UnsafeRawPointer: _Pointer {
456456
/// - Returns: A new instance of type `T`, read from the raw bytes at
457457
/// `offset`. The returned instance isn't associated
458458
/// with the value in the range of memory referenced by this pointer.
459+
#if $BitwiseCopyable
460+
@inlinable
461+
@_alwaysEmitIntoClient
462+
public func loadUnaligned<T : _BitwiseCopyable>(
463+
fromByteOffset offset: Int = 0,
464+
as type: T.Type
465+
) -> T {
466+
return Builtin.loadRaw((self + offset)._rawValue)
467+
}
468+
#endif
459469
@inlinable
460470
@_alwaysEmitIntoClient
461471
public func loadUnaligned<T>(
@@ -473,8 +483,6 @@ public struct UnsafeRawPointer: _Pointer {
473483
)
474484
return temporary.pointee
475485
}
476-
//FIXME: reimplement with `loadRaw` when supported in SIL (rdar://96956089)
477-
// e.g. Builtin.loadRaw((self + offset)._rawValue)
478486
}
479487
}
480488

@@ -1238,6 +1246,16 @@ public struct UnsafeMutableRawPointer: _Pointer {
12381246
/// - Returns: A new instance of type `T`, read from the raw bytes at
12391247
/// `offset`. The returned instance isn't associated
12401248
/// with the value in the range of memory referenced by this pointer.
1249+
#if $BitwiseCopyable
1250+
@inlinable
1251+
@_alwaysEmitIntoClient
1252+
public func loadUnaligned<T : _BitwiseCopyable>(
1253+
fromByteOffset offset: Int = 0,
1254+
as type: T.Type
1255+
) -> T {
1256+
return Builtin.loadRaw((self + offset)._rawValue)
1257+
}
1258+
#endif
12411259
@inlinable
12421260
@_alwaysEmitIntoClient
12431261
public func loadUnaligned<T>(
@@ -1255,8 +1273,6 @@ public struct UnsafeMutableRawPointer: _Pointer {
12551273
)
12561274
return temporary.pointee
12571275
}
1258-
//FIXME: reimplement with `loadRaw` when supported in SIL (rdar://96956089)
1259-
// e.g. Builtin.loadRaw((self + offset)._rawValue)
12601276
}
12611277

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

test/IRGen/bitwise-copyable-loadRaw.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,14 @@ func doit() {
1010
let bytes: [UInt8] = Array(repeating: 0, count: 64)
1111
bytes.withUnsafeBufferPointer { bytes in
1212
let rawBytes = UnsafeRawPointer(bytes.baseAddress!) + 1
13-
let vector = rawBytes.myLoadUnaligned(as: SIMD16<UInt8>.self)
13+
let vector = rawBytes.loadUnaligned(as: SIMD16<UInt8>.self)
1414
//CHECK: SIMD16<UInt8>(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
1515
blackhole(vector)
1616
}
1717
}
1818

1919
import Builtin
2020

21-
extension UnsafeRawPointer {
22-
@inlinable
23-
@_alwaysEmitIntoClient
24-
public func myLoadUnaligned<T : _BitwiseCopyable>(
25-
fromByteOffset offset: Int = 0,
26-
as type: T.Type
27-
) -> T {
28-
return Builtin.loadRaw((self + offset)._rawValue)
29-
}
30-
}
31-
3221
doit()
3322

3423
@_silgen_name("blackhole")

0 commit comments

Comments
 (0)