File: Primitives.native.swift.gyb

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (290 lines) | stat: -rw-r--r-- 9,872 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

%{
  from gyb_utils import *
}%
${autogenerated_warning()}

// FIXME: The conditionals below have been carefully constructed to
// avoid confusing Swift 5.7; they can be sanitized once we drop support
// for that version.
#if compiler(>=5.9)
#if !ATOMICS_NATIVE_BUILTINS
#error("swift-atomics requires native builtins on Swift 5.9")
#endif
import Builtin

@_alwaysEmitIntoClient
@_transparent
internal func _atomicMemoryFence(
  ordering: AtomicUpdateOrdering
) {
  switch ordering {
    case .relaxed:
      break
% for (enumOrder, apiOrder, docOrder, llvmOrder, _) in updateOrderings:
%   if enumOrder != "relaxed":
    case .${enumOrder}:
      Builtin.fence_${llvmOrder}()
%   end
% end
    default:
      fatalError("Unsupported ordering")
  }
}
% for (swiftType, builtinType, alignment) in nativePrimitives:

%   if builtinType == "Int128":
#if _pointerBitWidth(_64)
%   end
@usableFromInline
@frozen
@_alignment(${alignment})
internal struct ${swiftType} {
  @usableFromInline
  internal var _value: Builtin.${builtinType}

  @_alwaysEmitIntoClient @_transparent
  internal init(_ value: Builtin.${builtinType}) {
    self._value = value
  }
}

extension UnsafeMutablePointer where Pointee == ${swiftType} {
  /// Atomically loads a word starting at this address with the specified
  /// memory ordering.
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent // Debug performance
  internal func _atomicLoad(ordering: AtomicLoadOrdering) -> ${swiftType} {
    switch ordering {
% for (enumOrder, apiOrder, _, llvmOrder) in loadOrderings:
    case .${enumOrder}:
      return ${swiftType}(Builtin.atomicload_${llvmOrder}_${builtinType}(_rawValue))
% end
    default:
      fatalError("Unsupported ordering")
    }
  }

  /// Atomically stores the specified value starting at the memory referenced by
  /// this pointer, with the specified memory ordering.
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent // Debug performance
  internal func _atomicStore(
    _ desired: ${swiftType},
    ordering: AtomicStoreOrdering
  ) {
    switch ordering {
% for (enumOrder, apiOrder, _, llvmOrder) in storeOrderings:
    case .${enumOrder}:
      Builtin.atomicstore_${llvmOrder}_${builtinType}(_rawValue, desired._value)
% end
    default:
      fatalError("Unsupported ordering")
    }
  }

  /// Atomically stores the specified value starting at the memory referenced by
  /// this pointer, with the specified memory ordering.
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent // Debug performance
  internal func _atomicExchange(
    _ desired: ${swiftType},
    ordering: AtomicUpdateOrdering
  ) -> ${swiftType} {
    switch ordering {
% for (enumOrder, apiOrder, _, llvmOrder, failureOrder) in updateOrderings:
    case .${enumOrder}:
      let oldValue = Builtin.atomicrmw_xchg_${llvmOrder}_${builtinType}(
        _rawValue, desired._value)
      return ${swiftType}(oldValue)
% end
    default:
      fatalError("Unsupported ordering")
    }
  }

  /// Perform an atomic compare and exchange operation with the specified memory
  /// ordering.
  ///
  /// This operation is equivalent to the following pseudocode:
  ///
  /// ```
  /// atomic(self, ordering) { currentValue in
  ///   let original = currentValue
  ///   guard original == expected else { return (false, original) }
  ///   currentValue = desired
  ///   return (true, original)
  /// }
  /// ```
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent // Debug performance
  internal func _atomicCompareExchange(
    expected: ${swiftType},
    desired: ${swiftType},
    ordering: AtomicUpdateOrdering
  ) -> (exchanged: Bool, original: ${swiftType}) {
    switch ordering {
% for (enumOrder, apiOrder, _, llvmOrder, failureOrder) in updateOrderings:
    case .${enumOrder}:
      let (oldValue, won) = Builtin.cmpxchg_${llvmOrder}_${failureOrder}_${builtinType}(
        _rawValue, expected._value, desired._value)
      return (Bool(won), ${swiftType}(oldValue))
% end
    default:
      fatalError("Unsupported ordering")
    }
  }

  /// Perform an atomic compare and exchange operation with the specified
  /// success/failure memory orderings.
  ///
  /// This operation is equivalent to the following pseudocode:
  ///
  /// ```
  /// atomic(self, ordering, failureOrdering) { currentValue in
  ///   let original = currentValue
  ///   guard original == expected else { return (false, original) }
  ///   currentValue = desired
  ///   return (true, original)
  /// }
  /// ```
  ///
  /// The `ordering` argument specifies the memory ordering to use when the
  /// operation manages to update the current value, while `failureOrdering`
  /// will be used when the operation leaves the value intact.
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent // Debug performance
  internal func _atomicCompareExchange(
    expected: ${swiftType},
    desired: ${swiftType},
    successOrdering: AtomicUpdateOrdering,
    failureOrdering: AtomicLoadOrdering
  ) -> (exchanged: Bool, original: ${swiftType}) {
    // FIXME: LLVM doesn't support arbitrary ordering combinations
    // yet, so upgrade the success ordering when necessary so that it
    // is at least as "strong" as the failure case.
    switch (successOrdering, failureOrdering) {
% for (swiftSuccess, apiSuccess, _, llvmOrder, _) in updateOrderings:
%   for (swiftFailure, apiFailure, _, llvmFailOrder) in loadOrderings:
    case (.${swiftSuccess}, .${swiftFailure}):
      let (oldValue, won) = Builtin.cmpxchg_${actualOrders(llvmOrder, llvmFailOrder)}_${builtinType}(
        _rawValue, expected._value, desired._value)
      return (Bool(won), ${swiftType}(oldValue))
%   end
% end
    default:
      preconditionFailure("Unsupported orderings")
    }
  }

  /// Perform an atomic compare and exchange operation with the specified
  /// success/failure memory orderings.
  ///
  /// This operation is equivalent to the following pseudocode:
  ///
  /// ```
  /// atomic(self, ordering, failureOrdering) { currentValue in
  ///   let original = currentValue
  ///   guard original == expected else { return (false, original) }
  ///   currentValue = desired
  ///   return (true, original)
  /// }
  /// ```
  ///
  /// The `ordering` argument specifies the memory ordering to use when the
  /// operation manages to update the current value, while `failureOrdering`
  /// will be used when the operation leaves the value intact.
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent // Debug performance
  internal func _atomicWeakCompareExchange(
    expected: ${swiftType},
    desired: ${swiftType},
    successOrdering: AtomicUpdateOrdering,
    failureOrdering: AtomicLoadOrdering
  ) -> (exchanged: Bool, original: ${swiftType}) {
    // FIXME: LLVM doesn't support arbitrary ordering combinations
    // yet, so upgrade the success ordering when necessary so that it
    // is at least as "strong" as the failure case.
    switch (successOrdering, failureOrdering) {
% for (swiftSuccess, apiSuccess, _, llvmOrder, _) in updateOrderings:
%   for (swiftFailure, apiFailure, _, llvmFailOrder) in loadOrderings:
    case (.${swiftSuccess}, .${swiftFailure}):
      let (oldValue, won) = Builtin.cmpxchg_${actualOrders(llvmOrder, llvmFailOrder)}_weak_${builtinType}(
        _rawValue, expected._value, desired._value)
      return (Bool(won), ${swiftType}(oldValue))
%   end
% end
    default:
      preconditionFailure("Unsupported orderings")
    }
  }
% if builtinType != "Int128":
%   for (swiftName, builtinName, operator, label, doc) in integerOperations:

  /// Perform an atomic ${doc} operation and return the new value,
  /// with the specified memory ordering.
  ///
  % if "Wrapping" in swiftName:
  /// - Note: This operation silently wraps around on overflow, like the
  /// `${operator}` operator does on `UInt` values.
  ///
  % end
  /// - Returns: The original value before the operation.
  @_semantics("atomics.requires_constant_orderings")
  @_alwaysEmitIntoClient
  @_transparent // Debug performance
  internal func _atomicLoadThen${swiftName}(
    ${label} operand: ${swiftType},
    ordering: AtomicUpdateOrdering
  ) -> ${swiftType} {
    switch ordering {
% for (enumOrder, apiOrder, _, llvmOrder, failureOrder) in updateOrderings:
    case .${enumOrder}:
      let value = Builtin.atomicrmw_${builtinName}_${llvmOrder}_${builtinType}(
        _rawValue, operand._value)
      return ${swiftType}(value)
% end
    default:
      preconditionFailure("Unsupported ordering")
    }
  }
%   end
% end
}
% if builtinType == "Int128":
#endif
% end
% end

#if _pointerBitWidth(_64)
@usableFromInline internal typealias _AtomicIntStorage = _AtomicInt64Storage
@usableFromInline internal typealias _AtomicDoubleWordStorage = _AtomicInt128Storage
#elseif _pointerBitWidth(_32)
@usableFromInline internal typealias _AtomicIntStorage = _AtomicInt32Storage
@usableFromInline internal typealias _AtomicDoubleWordStorage = _AtomicInt64Storage
#else
#error("Unexpected pointer bit width")
#endif

#else // compiler(>=5.9)
#if ATOMICS_NATIVE_BUILTINS
#error("swift-atomics requires C shims on Swift versions below 5.9")
#endif
#endif // compiler(>=5.9)