Thanks for using Compiler Explorer
Sponsors
Jakt
C++
Ada
Algol68
Analysis
Android Java
Android Kotlin
Assembly
C
C3
Carbon
C with Coccinelle
C++ with Coccinelle
C++ (Circle)
CIRCT
Clean
Clojure
CMake
CMakeScript
COBOL
C++ for OpenCL
MLIR
Cppx
Cppx-Blue
Cppx-Gold
Cpp2-cppfront
Crystal
C#
CUDA C++
D
Dart
Elixir
Erlang
Fortran
F#
GLSL
Go
Haskell
HLSL
Helion
Hook
Hylo
IL
ispc
Java
Julia
Kotlin
LLVM IR
LLVM MIR
Modula-2
Mojo
Nim
Numba
Nix
Objective-C
Objective-C++
OCaml
Odin
OpenCL C
Pascal
Pony
PTX
Python
Racket
Raku
Ruby
Rust
Sail
Snowball
Scala
Slang
Solidity
Spice
SPIR-V
Swift
LLVM TableGen
Toit
Triton
TypeScript Native
V
Vala
Visual Basic
Vyper
WASM
Yul (Solidity IR)
Zig
Javascript
GIMPLE
Ygen
sway
swift source #1
Output
Compile to binary object
Link to binary
Execute the code
Intel asm syntax
Demangle identifiers
Verbose demangling
Filters
Unused labels
Library functions
Directives
Comments
Horizontal whitespace
Debug intrinsics
Compiler
aarch64 swiftc 6.0.3
aarch64 swiftc 6.1
aarch64 swiftc 6.2
x86-64 swiftc 3.1.1
x86-64 swiftc 4.0.2
x86-64 swiftc 4.0.3
x86-64 swiftc 4.1
x86-64 swiftc 4.1.1
x86-64 swiftc 4.1.2
x86-64 swiftc 4.2
x86-64 swiftc 5.0
x86-64 swiftc 5.1
x86-64 swiftc 5.10
x86-64 swiftc 5.2
x86-64 swiftc 5.3
x86-64 swiftc 5.4
x86-64 swiftc 5.5
x86-64 swiftc 5.6
x86-64 swiftc 5.7
x86-64 swiftc 5.8
x86-64 swiftc 5.9
x86-64 swiftc 6.0.3
x86-64 swiftc 6.1
x86-64 swiftc 6.2
x86-64 swiftc devsnapshot
x86-64 swiftc nightly
Options
Source code
enum Demo { typealias intType = UInt64 static func demo() { typealias IntType = Demo.intType typealias MyAddress = Address<IntType> let release = Proposal.AtomicUpdateOrdering.releasing let loadAcquire = Proposal.AtomicLoadOrdering.acquiring let address = MyAddress() // simple use, just to load value let loadAddress = loadAcquire.load(address) let value = loadAddress.load() _ = value // Configure producer/consumer team with modify shape let addProduce = release.modify(address, .add, and: .getAfter) let getConsume = loadAcquire.load(address) // Use team var updatedValue = addProduce.modify(23) var updateRead = getConsume.load() (_, _) = (updatedValue, updateRead) // Use team - one-shot long form updatedValue = release.modify(address, .add, and: .getAfter).modify(23) updateRead = loadAcquire.load(address).load() // Use team - alias e.g., via local function func addAndGetToRelease(_ address: MyAddress, _ value: IntType) -> IntType { release.modify(address, .add, and: .getAfter).modify(value) } func load(_ address: MyAddress) -> IntType { loadAcquire.load(address).load() ?? 0 // ?? only d/t demo } updatedValue = addAndGetToRelease(address, 23) updateRead = load(address) } } // MARK: Address struct Address<intType> { //: RawRepresentable { typealias RawValue = intType } // MARK: Proposal order extension to build operations from addresses extension Proposal.AtomicLoadOrdering { func load<T>(_ value: Address<T>) -> Shapes.Load<T> { Shapes.Load(address: value, order: self) } } extension Proposal.AtomicStoreOrdering { func store<T>(_ value: Address<T>) -> Shapes.Store<T> { Shapes.Store(address: value, order: self) } } extension Proposal.AtomicUpdateOrdering { func compareAndSet<T>(_ value: Address<T>) -> Shapes.CompareAndSet<T> { Shapes.CompareAndSet(address: value, order: self) } func modify<T>( _ value: Address<T>, _ op: Shapes.ModifyOp, and get: Shapes.Peek ) -> Shapes.Modify<T> { Shapes.Modify( address: value, order: self, op: op, peek: get) } } enum Shapes { /// AtomicLoad has type (Builtin.RawPointer) -> T. /// BUILTIN_MISC_OPERATION(AtomicLoad, "atomicload", "", Special) struct Load<T> { let address: Address<T> let order: Proposal.AtomicLoadOrdering func load() -> T? { // T1: optional only for Impl nil } } /// AtomicStore has type (Builtin.RawPointer, T) -> (). /// BUILTIN_MISC_OPERATION(AtomicStore, "atomicstore", "", Special) struct Store<T> { let address: Address<T> let order: Proposal.AtomicStoreOrdering func store(_ newValue: T) { // T2: Impl } } /// CmpXChg has type (Builtin.RawPointer, T, T) -> (T, Bool). /// BUILTIN_MISC_OPERATION(CmpXChg, "cmpxchg", "", Special) struct CompareAndSet<T> { let address: Address<T> let order: Proposal.AtomicUpdateOrdering func cas(expect: T, newValue: T) -> (found: T, changed: Bool) { (expect, false) // T3: Impl } } /// AtomicRMW has type (Builtin.RawPointer, T) -> T. /// BUILTIN_MISC_OPERATION(AtomicRMW, "atomicrmw", "", IntegerOrRawPointer) struct Modify<T> { let address: Address<T> let order: Proposal.AtomicUpdateOrdering let op: ModifyOp let peek: Peek func modify(_ newValue: T) -> T { newValue // T4: Impl } } // 10 (CAS has a different shape) enum ModifyOp { // case xchg // cas above case add, min, max case sub, umin, umax // U only case and, nand, or, xor // U only? } enum Peek { case getBefore, getAfter } } // MARK: Orders from Proposal enum Proposal { typealias intType = UInt64 public struct AtomicLoadOrdering { public static var relaxed = Self(i: Id.relaxed) public static var acquiring = Self(i: Id.acquiring) public static var sequentiallyConsistent = Self(i: Id.ordered) private let i: Int } /// Specifies the memory ordering semantics of an atomic store operation. public struct AtomicStoreOrdering { public static var relaxed = Self(i: Id.relaxed) public static var releasing = Self(i: Id.releasing) public static var sequentiallyConsistent = Self(i: Id.ordered) private let i: Int } /// atomic read-modify-write orders. public struct AtomicUpdateOrdering { public static var relaxed = Self(i: Id.relaxed) public static var releasing = Self(i: Id.releasing) public static var acquiring = Self(i: Id.acquiring) public static var acquiringAndReleasing = Self(i: Id.acquiringAndReleasing) public static var sequentiallyConsistent = Self(i: Id.ordered) private let i: Int } private struct Id { static let relaxed = 0 static let releasing = 1 static let acquiring = 2 static let acquiringAndReleasing = 3 static let sequentiallyConsistent = 7 static let ordered = Id.sequentiallyConsistent } }
Become a Patron
Sponsor on GitHub
Donate via PayPal
Compiler Explorer Shop
Source on GitHub
Mailing list
Installed libraries
Wiki
Report an issue
How it works
Contact the author
CE on Mastodon
CE on Bluesky
Statistics
Changelog
Version tree