Skip to content

Commit aa6ef50

Browse files
committed
Swap to an unprotected load to get better codegen
1 parent acb7f8e commit aa6ef50

18 files changed

+75
-71
lines changed

stdlib/public/Reflection/Sources/Reflection/GenericArguments.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ extension GenericArguments: RandomAccessCollection {
5151

5252
let start = argumentPointer.unsafelyUnwrapped
5353
let address = start + position * MemoryLayout<Type>.size
54-
return address.loadUnaligned(as: Type.self)
54+
return address.unprotectedLoad(as: Type.self)
5555
}
5656
}
5757

stdlib/public/Reflection/Sources/Reflection/KeyPath.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ extension Case {
9696
var value = pair.buffer
9797

9898
if isIndirect {
99-
let owner = value.loadUnaligned(as: HeapObject.self)
99+
let owner = value.unprotectedLoad(as: HeapObject.self)
100100
value = swift_projectBox(owner)
101101
}
102102

stdlib/public/Reflection/Sources/_Runtime/ContextDescriptor/ClassDescriptor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension ClassDescriptor {
8080
// 'TargetStoredClassMetadataBounds', by the time we access this it will
8181
// have already been initialized way before for us. Thus, it is safe to
8282
// access this value non-atomically.
83-
return storedBounds.load(as: Int.self)
83+
return storedBounds.unprotectedLoad(as: Int.self)
8484
}
8585

8686
@inlinable

stdlib/public/Reflection/Sources/_Runtime/ContextDescriptor/GenericSignature.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ extension GenericSignature.RequirementDescriptor {
119119
@available(SwiftStdlib 5.9, *)
120120
@inlinable
121121
public var layoutKind: GenericSignature.LayoutKind {
122-
address(for: \.requirement).loadUnaligned(
122+
address(for: \.requirement).unprotectedLoad(
123123
as: GenericSignature.LayoutKind.self
124124
)
125125
}
@@ -140,7 +140,7 @@ extension GenericSignature.RequirementDescriptor {
140140
) -> Any.Type? {
141141
_getTypeByMangledNameInContext(
142142
UnsafePointer(parameter.ptr._rawValue),
143-
UInt(parameter.length),
143+
UInt(truncatingIfNeeded: parameter.length),
144144
genericContext: context.ptr,
145145
genericArguments: argPtr
146146
)
@@ -269,7 +269,7 @@ extension GenericSignature.RequirementDescriptor {
269269
func getGenericSignature(at address: UnsafeRawPointer) -> GenericSignature {
270270
var address = address
271271

272-
let header = address.loadUnaligned(as: GenericSignature.Header.self)
272+
let header = address.unprotectedLoad(as: GenericSignature.Header.self)
273273
address += MemoryLayout<GenericSignature.Header>.size
274274

275275
let parameters = BufferView<GenericSignature.ParameterDescriptor>(

stdlib/public/Reflection/Sources/_Runtime/ExistentialContainer.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,14 @@ extension AnyExistentialContainer {
6767
}
6868
}
6969

70-
let alignMask = UInt(metadata.vwt.flags.alignmentMask)
70+
let alignMask = UInt(truncatingIfNeeded:metadata.vwt.flags.alignmentMask)
7171
let heapObjSize = UInt(MemoryLayout<Int>.size * 2)
7272
let byteOffset = (heapObjSize + alignMask) & ~alignMask
7373

7474
return try withUnsafeMutablePointer(to: &self) {
75-
let raw = UnsafeMutableRawPointer($0)
76-
let heap = raw.loadUnaligned(as: UnsafeMutableRawPointer.self)
75+
let heap = $0.raw.unprotectedLoad(as: UnsafeMutableRawPointer.self)
7776

78-
return try body(heap + Int(byteOffset))
77+
return try body(heap + Int(truncatingIfNeeded: byteOffset))
7978
}
8079
}
8180
}

stdlib/public/Reflection/Sources/_Runtime/Metadata/TupleMetadata.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ extension TupleMetadata.Elements.Element {
8585
// string
8686
address -= MemoryLayout<Int>.size
8787

88-
guard let cString = address.loadUnaligned(
88+
guard let cString = address.unprotectedLoad(
8989
as: UnsafePointer<CChar>?.self
9090
) else {
9191
return ""

stdlib/public/Reflection/Sources/_Runtime/Metadata/TypeMetadata.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension TypeMetadata {
2929
@inlinable
3030
public var descriptor: TypeDescriptor {
3131
var address: UnsafeRawPointer
32-
32+
3333
switch metadata.kind {
3434
case .struct:
3535
address = ptr + MemoryLayout<StructMetadata.Layout>.offset(of: \.descriptor)!
@@ -39,12 +39,12 @@ extension TypeMetadata {
3939
default:
4040
address = ptr + MemoryLayout<ClassMetadata.Layout>.offset(of: \.descriptor)!
4141
}
42-
43-
address = address.loadUnaligned(as: UnsafeRawPointer.self)
44-
42+
43+
address = address.unprotectedLoad(as: UnsafeRawPointer.self)
44+
4545
return TypeDescriptor(address)
4646
}
47-
47+
4848
@inlinable
4949
public var genericArguments: UnsafeRawPointer {
5050
switch metadata.kind {
@@ -68,17 +68,17 @@ extension TypeMetadata {
6868
public var metadata: Metadata {
6969
Metadata(ptr)
7070
}
71-
71+
7272
@inlinable
7373
public var `class`: ClassMetadata {
7474
ClassMetadata(ptr)
7575
}
76-
76+
7777
@inlinable
7878
public var `enum`: EnumMetadata {
7979
EnumMetadata(ptr)
8080
}
81-
81+
8282
@inlinable
8383
public var `struct`: StructMetadata {
8484
StructMetadata(ptr)
@@ -93,10 +93,10 @@ extension TypeMetadata {
9393
if let ss = typeRef.standardSubstitution {
9494
return ss
9595
}
96-
96+
9797
return _resolve(typeRef)
9898
}
99-
99+
100100
@usableFromInline
101101
func _resolve(_ typeRef: MangledTypeReference) -> Any.Type? {
102102
typeCache.getOrInsert(typeRef, from: self)

stdlib/public/Reflection/Sources/_Runtime/Utils/BufferView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extension BufferView: RandomAccessCollection {
5454

5555
@inlinable
5656
public subscript(position: Int) -> Element {
57-
start.loadUnaligned(
57+
start.unprotectedLoad(
5858
fromByteOffset: position * MemoryLayout<Element>.size,
5959
as: Element.self
6060
)

stdlib/public/Reflection/Sources/_Runtime/Utils/Layouts.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extension PublicLayout {
2525
@inline(__always)
2626
@inlinable
2727
public var layout: Layout {
28-
ptr.loadUnaligned(as: Layout.self)
28+
ptr.unprotectedLoad(as: Layout.self)
2929
}
3030

3131
@inline(__always)
@@ -74,7 +74,7 @@ protocol PrivateLayout {
7474
@available(SwiftStdlib 5.9, *)
7575
extension PrivateLayout {
7676
var layout: Layout {
77-
ptr.loadUnaligned(as: Layout.self)
77+
ptr.unprotectedLoad(as: Layout.self)
7878
}
7979

8080
var trailing: UnsafeRawPointer {

stdlib/public/Reflection/Sources/_Runtime/Utils/MangledTypeReference.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,19 @@ extension MangledTypeReference {
3434
@available(SwiftStdlib 5.9, *)
3535
@inlinable
3636
var standardSubstitution: Any.Type? {
37-
let byte1 = ptr.loadUnaligned(fromByteOffset: 1, as: UInt8.self)
37+
let byte1 = ptr.unprotectedLoad(fromByteOffset: 1, as: UInt8.self)
3838

3939
guard byte1 != 0 else {
4040
return nil
4141
}
4242

43-
let byte2 = ptr.loadUnaligned(fromByteOffset: 2, as: UInt8.self)
43+
let byte2 = ptr.unprotectedLoad(fromByteOffset: 2, as: UInt8.self)
4444

4545
guard byte2 == 0 else {
4646
return nil
4747
}
4848

49-
let byte0 = ptr.loadUnaligned(as: UInt8.self)
49+
let byte0 = ptr.unprotectedLoad(as: UInt8.self)
5050

5151
switch (byte0, byte1) {
5252
// Stdlib types

0 commit comments

Comments
 (0)