Phase 5: atomics through every layer (RFC 0015)
AIR: linear atomic<T> (AtomicScalar), atomic.new/take/load/store/RMW/ cmpxchg (two results)/fence with explicit orderings; feature atomics published. Verifier: ordering tables (E4201), ordinary access (E4202), effects [atomic] / [atomic, atomic.weak], linearity, take while lent (E4101, also for mutex.take). Interpreter: timestamped message machine (src/interp/weak.zig); views travel through spawn/join, channels, mutexes, close and cancel; seq_cst reads the latest message and programs without weak reads make no extra draw (existing records unchanged). aji explore: exhaustive exploration that branches only before visible operations. Litmus: 20/20 tests give exactly the oracle's sets; differential test (1500 random programs) found no unsound outcome. .aji layer 10, Quick Reference atomics, source coverage 67/67, audit report atomic_operations. AIR bundle +5.9 %, QUICK.md +0.5k.
This commit is contained in:
1 parent
ce9ef03bc0
commit
4242132b2a
70 files changed
+2630
-80
No files matched your search
+97
-5
@@ -85,9 +85,14 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
|
||||
.void, .never => pos == .ret,
|
||||
.local => false,
|
||||
.ref, .mut, .slice, .slice_mut => pos != .member and pos != .borrowed,
|
||||
.resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc => pos != .member,
|
||||
.resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .atomic => pos != .member,
|
||||
else => true,
|
||||
};
|
||||
// An atomic cell holds an AtomicScalar (spec 04 §23).
|
||||
if (t == .atomic and !isAtomicScalar(t.atomic.*)) try c.report("E1001", span, "atomic<T> needs bool or an integer of at most 64 bits", &.{
|
||||
.{ .name = "in", .value = .{ .str = owner } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t.atomic.*) } },
|
||||
});
|
||||
if (!ok) try c.report("E1001", span, "type not allowed in this position", &.{
|
||||
.{ .name = "in", .value = .{ .str = owner } },
|
||||
.{ .name = "position", .value = .{ .str = @tagName(pos) } },
|
||||
@@ -339,6 +344,8 @@ pub fn isSend(m: air.Module, t: Type) bool {
|
||||
.int, .bool, .builtin, .cap, .resource, .region, .sender, .receiver => true,
|
||||
// Graph contents are IslandSafe plain data (spec 03 §22–§24).
|
||||
.island, .frozen, .arc, .iref => true,
|
||||
// Every access to a cell is an atomic operation (spec 04 §23).
|
||||
.atomic => true,
|
||||
.own, .own_array, .mutex => |inner| isSend(m, inner.*),
|
||||
.ref, .slice => |inner| isShare(m, inner.*),
|
||||
.mut, .slice_mut => |inner| isSend(m, inner.*),
|
||||
@@ -352,7 +359,7 @@ pub fn isSend(m: air.Module, t: Type) bool {
|
||||
pub fn isShare(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap => true,
|
||||
.frozen, .arc, .iref => true,
|
||||
.frozen, .arc, .iref, .atomic => true,
|
||||
// Its owner may mutate it: freeze to share (spec 04 §35).
|
||||
.island => false,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .shield, .guard, .sender, .receiver, .local, .void, .never => false,
|
||||
@@ -364,6 +371,43 @@ pub fn isShare(m: air.Module, t: Type) bool {
|
||||
};
|
||||
}
|
||||
|
||||
/// A borrow whose pointee is an atomic cell: reached only atomically.
|
||||
fn atomicPointee(t: Type) bool {
|
||||
return (t == .ref or t == .mut) and t.inner().? == .atomic;
|
||||
}
|
||||
|
||||
/// `E4202`: an ordinary load or store of an atomic cell (spec 04 §28).
|
||||
fn ordinaryAtomicAccess(fc: *FnCtx, span: air.Span, v: air.ValueId, use: []const u8) !void {
|
||||
try fc.c.report("E4202", span, "an atomic cell is read and written only by atomic operations", &.{
|
||||
.{ .name = "function", .value = .{ .str = fc.f.name } },
|
||||
.{ .name = "operand", .value = .{ .str = try fc.c.valueName(fc.f, v) } },
|
||||
.{ .name = "found", .value = .{ .str = try fc.c.typeName(fc.typeOf(v)) } },
|
||||
.{ .name = "use", .value = .{ .str = use } },
|
||||
});
|
||||
}
|
||||
|
||||
/// `E4201`: an ordering outside the operation's row (spec 04 §24).
|
||||
fn checkOrdering(fc: *FnCtx, span: air.Span, operation: []const u8, ord: air.Ordering, allowed: []const air.Ordering) !void {
|
||||
if (std.mem.indexOfScalar(air.Ordering, allowed, ord) != null) return;
|
||||
var text: std.Io.Writer.Allocating = .init(fc.c.arena);
|
||||
for (allowed, 0..) |o, i| text.writer.print("{s}{s}", .{ if (i == 0) "" else ", ", @tagName(o) }) catch return error.OutOfMemory;
|
||||
try fc.c.report("E4201", span, "memory ordering not valid for this atomic operation", &.{
|
||||
.{ .name = "function", .value = .{ .str = fc.f.name } },
|
||||
.{ .name = "operation", .value = .{ .str = operation } },
|
||||
.{ .name = "ordering", .value = .{ .str = @tagName(ord) } },
|
||||
.{ .name = "allowed", .value = .{ .str = text.written() } },
|
||||
});
|
||||
}
|
||||
|
||||
/// AtomicScalar (spec 04 §23): `bool` and integers of at most 64 bits.
|
||||
pub fn isAtomicScalar(t: Type) bool {
|
||||
return switch (t) {
|
||||
.bool => true,
|
||||
.int => |it| it.bits() <= 64,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// `IslandSafe` (spec 02 §6): plain data and `iref` handles, at any depth.
|
||||
/// `ArcSafe` is the same set in format 0.
|
||||
pub fn isIslandSafe(m: air.Module, t: Type) bool {
|
||||
@@ -527,6 +571,11 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.slice_split => |x| fc.ty[x.slice] orelse return null,
|
||||
.island_root, .frozen_root, .arc_root, .island_freeze, .arc_from_frozen, .arc_clone => |v| fc.ty[v] orelse return null,
|
||||
inline .island_borrow, .frozen_borrow, .arc_borrow => |x| fc.ty[x.owner] orelse return null,
|
||||
.atomic_new => |v| return .{ .atomic = try boxed(a, fc.ty[v] orelse return null) },
|
||||
.atomic_take => |v| fc.ty[v] orelse return null,
|
||||
.atomic_load => |x| fc.ty[x.cell] orelse return null,
|
||||
.atomic_rmw => |x| fc.ty[x.cell] orelse return null,
|
||||
.atomic_cmpxchg => |x| fc.ty[x.cell] orelse return null,
|
||||
else => return null,
|
||||
};
|
||||
const invalid: Type = .void;
|
||||
@@ -534,7 +583,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.local_load => if (t == .local) t.local.* else invalid,
|
||||
.borrow_ref => switch (t) {
|
||||
.local, .ref, .mut => .{ .ref = try boxed(a, t.inner().?) },
|
||||
.resource, .region, .task, .scope, .sender, .receiver, .mutex, .island, .frozen, .arc => .{ .ref = try boxed(a, t) },
|
||||
.resource, .region, .task, .scope, .sender, .receiver, .mutex, .island, .frozen, .arc, .atomic => .{ .ref = try boxed(a, t) },
|
||||
.own => |inner| .{ .ref = inner },
|
||||
.own_array => |inner| .{ .slice = inner },
|
||||
.slice, .slice_mut => .{ .slice = try boxed(a, t.inner().?) },
|
||||
@@ -598,6 +647,9 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
if (owner == .island and t == .mut and t.mut.* == .island) break :blk Type{ .mut = target };
|
||||
break :blk invalid;
|
||||
},
|
||||
.atomic_take => if (t == .atomic) t.atomic.* else invalid,
|
||||
// Every atomic operation takes `ref<atomic<T>>` (spec 04 §23).
|
||||
.atomic_load, .atomic_rmw, .atomic_cmpxchg => if (t == .ref and t.ref.* == .atomic) t.ref.atomic.* else invalid,
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
@@ -638,7 +690,7 @@ fn checkFunction(c: *Context, f: air.Function) ![]const Type {
|
||||
if (fc.ty[r] != null) continue;
|
||||
if (try derivedType(&fc, inst.op)) |t| {
|
||||
fc.ty[r] = t;
|
||||
if (inst.result2) |r2| fc.ty[r2] = t;
|
||||
if (inst.result2) |r2| fc.ty[r2] = if (inst.op == .atomic_cmpxchg) .bool else t;
|
||||
progress = true;
|
||||
}
|
||||
};
|
||||
@@ -690,9 +742,13 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
.borrow_mut => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "local<T>, a resource, mut<T> or slice_mut<T>"),
|
||||
.borrow_field => |x| if (resultOf(fc, inst) == .void) try fc.kindError(span, x.borrow, "ref or mut of the named struct"),
|
||||
.borrow_end => |v| if (!fc.typeOf(v).isBorrow()) try fc.kindError(span, v, "a borrow"),
|
||||
.load => |v| if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<T> or mut<T>"),
|
||||
.load => |v| {
|
||||
if (atomicPointee(fc.typeOf(v))) return ordinaryAtomicAccess(fc, span, v, "atomic.load");
|
||||
if (resultOf(fc, inst) == .void) try fc.kindError(span, v, "ref<T> or mut<T>");
|
||||
},
|
||||
.store => |x| {
|
||||
const t = fc.typeOf(x.borrow);
|
||||
if (atomicPointee(t)) return ordinaryAtomicAccess(fc, span, x.borrow, "atomic.store");
|
||||
if (t != .mut) return fc.kindError(span, x.borrow, "mut<T>");
|
||||
try fc.expectValue(span, x.value, t.mut.*);
|
||||
},
|
||||
@@ -912,6 +968,42 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
if (a != .iref) return fc.kindError(span, x.a, "iref<T>");
|
||||
try fc.expectValue(span, x.b, a);
|
||||
},
|
||||
// Atomics (spec 04 §23–§25, §42).
|
||||
.atomic_new => |v| if (!isAtomicScalar(fc.typeOf(v))) try fc.kindError(span, v, "bool or an integer of at most 64 bits"),
|
||||
.atomic_take => |v| if (fc.typeOf(v) != .atomic) try fc.kindError(span, v, "atomic<T>"),
|
||||
.atomic_load => |x| {
|
||||
if (resultOf(fc, inst) == .void) return fc.kindError(span, x.cell, "ref<atomic<T>>");
|
||||
try checkOrdering(fc, span, "atomic.load", x.ord, &.{ .relaxed, .acquire, .seq_cst });
|
||||
},
|
||||
.atomic_store => |x| {
|
||||
const t = fc.typeOf(x.cell);
|
||||
if (t != .ref or t.ref.* != .atomic) return fc.kindError(span, x.cell, "ref<atomic<T>>");
|
||||
try fc.expectValue(span, x.value, t.ref.atomic.*);
|
||||
try checkOrdering(fc, span, "atomic.store", x.ord, &.{ .relaxed, .release, .seq_cst });
|
||||
},
|
||||
.atomic_rmw => |x| {
|
||||
const r = resultOf(fc, inst);
|
||||
if (r == .void) return fc.kindError(span, x.cell, "ref<atomic<T>>");
|
||||
if (r == .bool and (x.op == .add_wrap or x.op == .sub_wrap)) return fc.kindError(span, x.cell, "ref<atomic<T>> with an integer T");
|
||||
try fc.expectValue(span, x.value, r);
|
||||
try checkOrdering(fc, span, try std.fmt.allocPrint(fc.c.arena, "atomic.{s}", .{x.op.text()}), x.ord, &.{ .relaxed, .acquire, .release, .acq_rel, .seq_cst });
|
||||
},
|
||||
.atomic_cmpxchg => |x| {
|
||||
const r = resultOf(fc, inst);
|
||||
if (r == .void) return fc.kindError(span, x.cell, "ref<atomic<T>>");
|
||||
try fc.expectValue(span, x.expected, r);
|
||||
try fc.expectValue(span, x.desired, r);
|
||||
try checkOrdering(fc, span, "atomic.cmpxchg", x.success, &.{ .relaxed, .acquire, .release, .acq_rel, .seq_cst });
|
||||
// A failed compare-exchange is a read, never stronger than the
|
||||
// success ordering (spec 04 §24.1).
|
||||
const failure: []const air.Ordering = switch (x.success) {
|
||||
.relaxed, .release => &.{.relaxed},
|
||||
.acquire, .acq_rel => &.{ .relaxed, .acquire },
|
||||
.seq_cst => &.{ .relaxed, .acquire, .seq_cst },
|
||||
};
|
||||
try checkOrdering(fc, span, "atomic.cmpxchg failure", x.failure, failure);
|
||||
},
|
||||
.atomic_fence => |o| try checkOrdering(fc, span, "atomic.fence", o, &.{ .acquire, .release, .acq_rel, .seq_cst }),
|
||||
.cap_narrow => |x| {
|
||||
const t = fc.typeOf(x.cap);
|
||||
if (t != .cap) return fc.kindError(span, x.cap, "cap<K>");
|
||||
|
||||
Reference in new issue
Block a user