atomics A.3a: swap + fence ops + recognizer, emit bails (lock)

swap (atomicrmw xchg) and a standalone fence wired end-to-end except LLVM
emission (both bail loudly; A.3b makes them real).
- RmwKind += xchg; atomic_swap intrinsic + swap method reuse the atomic_rmw op.
- new atomic_fence op (+ AtomicFence) — ordering-only, void; fence($o)/atomic_fence
  intrinsic; recognizer rejects .relaxed (LLVM has no monotonic fence).
- comptime_vm: xchg = store operand/return old; fence = no-op (single-thread).
- examples 1703 (swap) + 1704 (fence) locked to bails; 1187 (relaxed-fence reject).
- 1186 converted to a direct-intrinsic call → stable user-file diagnostic span
  (the lib-forward-site span shifted when atomic.sx grew — fragile-snapshot fix).

Also fixes a latent A.2 comptime-CAS bug found while here: the success/null
has_value write was 'writeWord(addr, SIZE=0, val=1)' — a 0-byte no-op, correct
ONLY because allocBytes zero-inits (REJECTED-PATTERNS 'coincidentally correct').
Now writes the flag explicitly (size=1, val=0). Suite green (721/0).
This commit is contained in:
agra
2026-06-20 13:47:08 +03:00
parent 79895be401
commit fca4304f83
21 changed files with 129 additions and 9 deletions

View File

@@ -1,11 +1,12 @@
// Atomic compare-exchange dual-ordering validation: the FAILURE ordering may not
// be stronger than the SUCCESS ordering (LLVM rule). Here failure=.seq_cst (rank
// 3) is stronger than success=.relaxed (rank 0) → loud diagnostic, not invalid IR.
// Stream A (atomics) A.2.
// Calls the intrinsic directly so the diagnostic span is stable (user file, not
// the lib forward site). Stream A (atomics) A.2.
#import "modules/std.sx";
#import "modules/std/atomic.sx";
main :: () {
a := Atomic(i64).init(0);
_ := a.compare_exchange(0, 1, .relaxed, .seq_cst);
n : i64 = 0;
_ := atomic_cmpxchg(i64, @n, 0, 1, .relaxed, .seq_cst);
}

View File

@@ -0,0 +1,8 @@
// A fence with .relaxed ordering is rejected (LLVM has no monotonic/unordered
// fence). Stream A (atomics) guard.
#import "modules/std.sx";
#import "modules/std/atomic.sx";
main :: () {
atomic_fence(.relaxed);
}

View File

@@ -0,0 +1,11 @@
// Atomic($T).swap — atomic exchange (LLVM atomicrmw xchg): store the new value,
// return the OLD one. Stream A (atomics) A.3. Single-thread.
#import "modules/std.sx";
#import "modules/std/atomic.sx";
main :: () {
a := Atomic(i64).init(7);
old := a.swap(42, .acq_rel);
print("swap old: {}\n", old); // 7
print("swap now: {}\n", a.load(.acquire)); // 42
}

View File

@@ -0,0 +1,15 @@
// Standalone memory fence — fence(.ordering) → LLVM fence. Stream A (atomics) A.3.
// (.relaxed is rejected; see 1187.) Single-thread: a fence is observable only as
// "compiled + ran without error" here.
#import "modules/std.sx";
#import "modules/std/atomic.sx";
main :: () {
a := Atomic(i64).init(1);
a.store(2, .relaxed);
fence(.release);
a.store(3, .relaxed);
fence(.acquire);
fence(.seq_cst);
print("after fences: {}\n", a.load(.relaxed)); // 3
}

View File

@@ -1,5 +1,5 @@
error: atomic compare-exchange failure ordering ('.seq_cst') cannot be stronger than the success ordering ('.relaxed')
--> /Users/agra/projects/sx/library/modules/std/atomic.sx:79:188
--> examples/1186-diagnostics-atomic-cas-ordering.sx:11:50
|
79 | compare_exchange :: (self: *Atomic(T), expected: T, desired: T, $success: Ordering, $failure: Ordering) -> ?T { return atomic_cmpxchg(T, @self.value, expected, desired, success, failure); }
| ^^^^^^^
11 | _ := atomic_cmpxchg(i64, @n, 0, 1, .relaxed, .seq_cst);
| ^^^^^^^^

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,5 @@
error: fence ordering cannot be .relaxed (use .acquire / .release / .acq_rel / .seq_cst)
--> examples/1187-diagnostics-atomic-fence-relaxed.sx:7:18
|
7 | atomic_fence(.relaxed);
| ^^^^^^^^

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1 @@
error: atomic swap (xchg) LLVM emission not yet implemented (Stream A, A.3b)

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,3 @@
error: atomic fence LLVM emission not yet implemented (Stream A, A.3b)
error: atomic fence LLVM emission not yet implemented (Stream A, A.3b)
error: atomic fence LLVM emission not yet implemented (Stream A, A.3b)

View File

@@ -0,0 +1 @@