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

@@ -711,6 +711,7 @@ pub const Vm = struct {
const sp: i64 = @bitCast(operand);
break :blk @bitCast(if (want_max) @max(so, sp) else @min(so, sp));
},
.xchg => operand, // swap: new value IS the operand
};
try self.writeField(table, frame.get(a.ptr.index()), vty, new_val);
return .{ .value = old };
@@ -733,14 +734,21 @@ pub const Vm = struct {
// Build the `?T` result in VM memory.
const opt_ty = ins.ty; // ?T
const addr = self.machine.allocBytes(table.typeSizeBytes(opt_ty), table.typeAlignBytes(opt_ty));
// writeWord(addr, SIZE, val): write the 1-byte has_value flag
// EXPLICITLY (size=1) — never rely on alloc zero-init for the
// success/null case (a size=0 write is a no-op, correct only by
// accident; REJECTED-PATTERNS "coincidentally correct").
const has_value_off = addr + table.typeSizeBytes(elem_ty);
if (success) {
try self.machine.writeWord(addr + table.typeSizeBytes(elem_ty), 0, 1); // has_value = 0 (null)
try self.machine.writeWord(has_value_off, 1, 0); // has_value = 0 (null)
} else {
try self.writeField(table, addr, elem_ty, actual); // payload = actual
try self.machine.writeWord(addr + table.typeSizeBytes(elem_ty), 1, 1); // has_value = 1
try self.machine.writeWord(has_value_off, 1, 1); // has_value = 1
}
return .{ .value = addr };
},
// A fence is a no-op at comptime (single-thread → nothing to order).
.atomic_fence => return .{ .value = 0 },
.struct_init => |agg| {
const table = try self.requireTable();
const sty = ins.ty;