Skip to content

Commit

Permalink
Add basic support for atomic ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
lucteo committed Dec 3, 2024
1 parent 1eab2a4 commit df7d987
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Coverage.md
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@
- [ ] LLVMGetWeak
- [ ] LLVMSetWeak
- [ ] LLVMGetOrdering
- [ ] LLVMSetOrdering
- [x] LLVMSetOrdering
- [ ] LLVMGetAtomicRMWBinOp
- [ ] LLVMSetAtomicRMWBinOp
- [x] LLVMBuildTrunc
Expand Down
88 changes: 88 additions & 0 deletions Sources/SwiftyLLVM/AtomicOrdering.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
internal import llvmc

/// The ordering for an atomic operation.
///
/// See https://en.cppreference.com/w/cpp/atomic/memory_order
public enum AtomicOrdering {

/// A load or a store operation that is not atomic.
///
/// Matches C++ memory model for non-atomic shared variables.
case notAtomic

/// Lowest level of atomicity, guarantees somewhat sane results, lock free.
///
/// Matches Java memory model for shared variables.
case unordered

/// Guarantees that if you take all the operations affecting a specific address, a consistent
/// ordering exists.
///
/// Matches the C++ memory_order_relaxed memory order.
case monotonic

/// A load that is an *acquire operation*, or a barrier necessary to acquire a lock to access
/// other memory with normal loads and stores.
///
/// Matches the C++ memory_order_acquire memory order.
case acquire

/// A store that is a *release operation*, or a barrier necessary to release a lock.
///
/// Matches the C++ memory_order_release memory order.
case release

/// A read-modify-write operation with this memory order is both an *acquire operation* and a
/// *release operation*, or a barrier that is both an Acquire and a Release barrier.
///
/// Matches the C++ memory_order_acq_rel memory order.
case acquireRelease

/// Same as `acquireRelease`, but also provides a single total order of all modifications.
///
/// Matches the C++ memory_order_seq_cst memory order.
case sequentiallyConsistent

/// Creates an instance from its LLVM representation.
internal init(llvm: LLVMAtomicOrdering) {
switch llvm {
case LLVMAtomicOrderingNotAtomic:
self = .notAtomic
case LLVMAtomicOrderingUnordered:
self = .unordered
case LLVMAtomicOrderingMonotonic:
self = .monotonic
case LLVMAtomicOrderingAcquire:
self = .acquire
case LLVMAtomicOrderingRelease:
self = .release
case LLVMAtomicOrderingAcquireRelease:
self = .acquireRelease
case LLVMAtomicOrderingSequentiallyConsistent:
self = .sequentiallyConsistent
default:
fatalError("unsupported atomic ordering")
}
}

Check warning on line 66 in Sources/SwiftyLLVM/AtomicOrdering.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SwiftyLLVM/AtomicOrdering.swift#L47-L66

Added lines #L47 - L66 were not covered by tests

/// The LLVM representation of this instance.
internal var llvm: LLVMAtomicOrdering {
switch self {
case .notAtomic:
return LLVMAtomicOrderingNotAtomic
case .unordered:
return LLVMAtomicOrderingUnordered
case .monotonic:
return LLVMAtomicOrderingMonotonic
case .acquire:
return LLVMAtomicOrderingAcquire
case .release:
return LLVMAtomicOrderingRelease
case .acquireRelease:
return LLVMAtomicOrderingAcquireRelease
case .sequentiallyConsistent:
return LLVMAtomicOrderingSequentiallyConsistent
}
}

Check warning on line 86 in Sources/SwiftyLLVM/AtomicOrdering.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SwiftyLLVM/AtomicOrdering.swift#L69-L86

Added lines #L69 - L86 were not covered by tests

}
4 changes: 4 additions & 0 deletions Sources/SwiftyLLVM/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,10 @@ public struct Module {
.init(LLVMBuildStore(p.llvm, value.llvm.raw, location.llvm.raw))
}

public mutating func setOrdering(_ ordering: AtomicOrdering, for i: Instruction) {
LLVMSetOrdering(i.llvm.raw, ordering.llvm)
}

Check warning on line 627 in Sources/SwiftyLLVM/Module.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SwiftyLLVM/Module.swift#L625-L627

Added lines #L625 - L627 were not covered by tests

// MARK: Terminators

@discardableResult
Expand Down

0 comments on commit df7d987

Please sign in to comment.