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
+139
-15
@@ -73,13 +73,15 @@ pub const Ty = union(enum) {
|
||||
frozen: *const Ty,
|
||||
arc: *const Ty,
|
||||
iref: *const Ty,
|
||||
/// `atomic<T>` (spec 04 §23; layer 10): a linear cell.
|
||||
atomic: *const Ty,
|
||||
|
||||
pub const Arr = struct { elem: Ty, len: u64 };
|
||||
|
||||
/// Linear: consumed exactly once, by `move` (spec 15 §5).
|
||||
fn isLinear(t: Ty) bool {
|
||||
return switch (t) {
|
||||
.own, .own_array, .region, .resource, .task, .sender, .receiver, .scope, .mutex, .guard, .shield, .island, .frozen, .arc => true,
|
||||
.own, .own_array, .region, .resource, .task, .sender, .receiver, .scope, .mutex, .guard, .shield, .island, .frozen, .arc, .atomic => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
@@ -90,7 +92,7 @@ pub const Ty = union(enum) {
|
||||
.int => |x| std.mem.eql(u8, x, b.int),
|
||||
.named => |i| i == b.named,
|
||||
.array => |x| x.len == b.array.len and x.elem.eql(b.array.elem),
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref => |x| x.eql(inner(b).?),
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.eql(inner(b).?),
|
||||
.cap => |k| std.mem.eql(u8, k, b.cap),
|
||||
.builtin => |e| e == b.builtin,
|
||||
.resource => |r| r == b.resource,
|
||||
@@ -100,7 +102,7 @@ pub const Ty = union(enum) {
|
||||
|
||||
fn inner(t: Ty) ?Ty {
|
||||
return switch (t) {
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref => |x| x.*,
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.*,
|
||||
.array => |a| a.elem,
|
||||
else => null,
|
||||
};
|
||||
@@ -117,6 +119,11 @@ pub const Ty = union(enum) {
|
||||
|
||||
const int_types = [_][]const u8{ "u8", "u16", "u32", "u64", "u128", "usize", "i8", "i16", "i32", "i64", "i128", "isize" };
|
||||
|
||||
/// AtomicScalar (spec 04 §23): `bool` and integers of at most 64 bits.
|
||||
fn atomicScalar(t: Ty) bool {
|
||||
return t == .bool or (t == .int and intBits(t.int) <= 64);
|
||||
}
|
||||
|
||||
fn isIntName(n: []const u8) bool {
|
||||
for (int_types) |t| if (std.mem.eql(u8, t, n)) return true;
|
||||
return false;
|
||||
@@ -327,7 +334,7 @@ const Lowerer = struct {
|
||||
.never => "never",
|
||||
.named => |i| l.typeNames[i],
|
||||
.array => |a| std.fmt.allocPrint(l.arena, "array<{s}, {d}>", .{ l.tyText(a.elem), a.len }) catch "array",
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref => |x| std.fmt.allocPrint(l.arena, "{s}<{s}>", .{ @tagName(t), l.tyText(x.*) }) catch "borrow",
|
||||
.ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| std.fmt.allocPrint(l.arena, "{s}<{s}>", .{ @tagName(t), l.tyText(x.*) }) catch "borrow",
|
||||
.scope => "scope",
|
||||
.shield => "shield",
|
||||
.region => "region",
|
||||
@@ -449,6 +456,9 @@ const Lowerer = struct {
|
||||
.{ "cancellation", &.{ "task.shield", "task.check_cancel", "task.join.cancel" } },
|
||||
.{ "islands", &.{ "island.", "island<", "frozen.", "frozen<", "iref.", "iref<" } },
|
||||
.{ "arc", &.{ "arc.", "arc<" } },
|
||||
// Instructions only: the effect names `atomic` and `atomic.weak`
|
||||
// in a function header are not uses of the feature.
|
||||
.{ "atomics", &.{ "= atomic.", "atomic.store.", "atomic.fence.", "atomic<" } },
|
||||
};
|
||||
for (rules) |r| {
|
||||
const used = for (texts.items) |t| {
|
||||
@@ -519,7 +529,7 @@ const Lowerer = struct {
|
||||
},
|
||||
.array => |a| return l.arrayOf(try l.memberType(a.elem.*), a.len),
|
||||
.wrapped => |w| {
|
||||
const Wr = enum { ref, mut, slice, slice_mut, own, own_array, task, sender, receiver, mutex, island, frozen, arc, iref };
|
||||
const Wr = enum { ref, mut, slice, slice_mut, own, own_array, task, sender, receiver, mutex, island, frozen, arc, iref, atomic };
|
||||
const which = std.meta.stringToEnum(Wr, w.wrapper) orelse return l.notYet(t.span, "this type (later layers)");
|
||||
const in = try l.typeOf(w.inner.*);
|
||||
return switch (which) {
|
||||
@@ -537,6 +547,7 @@ const Lowerer = struct {
|
||||
.frozen => l.wrap(.frozen, in),
|
||||
.arc => l.wrap(.arc, in),
|
||||
.iref => l.wrap(.iref, in),
|
||||
.atomic => if (atomicScalar(in)) l.wrap(.atomic, in) else l.fail("E7005", t.span, "atomic<T> needs bool or an integer of at most 64 bits", &.{.{ .name = "found", .value = l.tyText(in) }}),
|
||||
};
|
||||
},
|
||||
.cap => |k| {
|
||||
@@ -856,7 +867,10 @@ const Lowerer = struct {
|
||||
fn stmt(l: *Lowerer, s: ast.Stmt) Error!void {
|
||||
switch (s.kind) {
|
||||
.let => |x| {
|
||||
if (x.names.len == 2) return l.splitLet(s, x.names, x.e);
|
||||
if (x.names.len == 2) {
|
||||
if (pathOf(x.e.kind.call.callee.*)) |p| if (std.mem.eql(u8, p, "atomic.compare_exchange")) return l.compareExchangeLet(s, x.names, x.e);
|
||||
return l.splitLet(s, x.names, x.e);
|
||||
}
|
||||
if (x.e.kind == .borrow) return l.fail("E7006", x.e.span, "bind a borrow with `ref name = place;` or `mut name = place;`", &.{});
|
||||
if (x.e.kind == .bytes) {
|
||||
const st = try l.materializeBytes(x.e.kind.bytes, x.e.span);
|
||||
@@ -1239,6 +1253,8 @@ const Lowerer = struct {
|
||||
const r = try l.expr(x.*, null);
|
||||
const in = r.ty.inner() orelse return l.fail("E7005", e.span, "`*` reads through a borrow", &.{.{ .name = "found", .value = l.tyText(r.ty) }});
|
||||
if (r.ty == .slice or r.ty == .slice_mut or in == .array) return l.fail("E7006", e.span, "read slice and array elements with `s[i]`, not `*`", &.{});
|
||||
// The verifier's code (spec 15 §1 rule 2, spec 04 §28).
|
||||
if (in == .atomic) return l.fail("E4202", e.span, "an atomic cell is read only by atomic operations: use `atomic.load(r, ordering)`", &.{});
|
||||
return l.def("v", in, try l.fmt("load %{s}", .{r.name}), e.span);
|
||||
},
|
||||
.index => |x| {
|
||||
@@ -1421,6 +1437,7 @@ const Lowerer = struct {
|
||||
return .{ .name = "", .ty = .void };
|
||||
}
|
||||
if (try l.concurrencyCall(e, p, args)) |v| return v;
|
||||
if (std.mem.startsWith(u8, p, "atomic.")) return l.atomicCall(e, p, args);
|
||||
if (authority.intrinsicIndex(p)) |ix| {
|
||||
const x = authority.intrinsics[ix];
|
||||
var temps: std.ArrayList([]const u8) = .empty;
|
||||
@@ -1725,6 +1742,14 @@ const Lowerer = struct {
|
||||
const eq = std.mem.eql;
|
||||
if ((eq(u8, p, "own.take") or eq(u8, p, "task.join")) and args.len == 1) return if (l.peekType(args[0])) |t| t.inner() else null;
|
||||
if (eq(u8, p, "task.cancelled")) return .bool;
|
||||
if (std.mem.startsWith(u8, p, "atomic.") and args.len >= 1) {
|
||||
if (eq(u8, p, "atomic.new")) return if (l.peekType(args[0])) |t| (l.wrap(.atomic, t) catch null) else null;
|
||||
if (eq(u8, p, "atomic.take")) return if (l.peekType(args[0])) |t| t.inner() else null;
|
||||
if (eq(u8, p, "atomic.store") or eq(u8, p, "atomic.fence") or eq(u8, p, "atomic.compare_exchange")) return null;
|
||||
// A `ref a` argument, or a `ref<atomic<T>>` binding: the cell's T.
|
||||
const cell: ?Ty = if (args[0].kind == .borrow) l.peekType(args[0].kind.borrow.place.*) else if (l.peekType(args[0])) |t| t.inner() else null;
|
||||
return if (cell) |c| (if (c == .atomic) c.inner() else null) else null;
|
||||
}
|
||||
if ((eq(u8, p, "island.root") or eq(u8, p, "frozen.root") or eq(u8, p, "arc.root")) and args.len == 1)
|
||||
return if (l.peekType(args[0])) |t| (if (t.inner()) |o| (if (o.inner()) |root| l.wrap(.iref, root) catch null else null) else null) else null;
|
||||
if (eq(u8, p, "island.freeze") and args.len == 1) return if (l.peekType(args[0])) |t| (if (t == .island) l.wrap(.frozen, t.inner().?) catch null else null) else null;
|
||||
@@ -1900,6 +1925,8 @@ const Lowerer = struct {
|
||||
.region => if (mutable) try l.wrap(.mut, .region) else try l.wrap(.ref, .region),
|
||||
.resource, .mutex, .sender, .receiver, .task, .island => if (mutable) try l.wrap(.mut, b.ty) else try l.wrap(.ref, b.ty),
|
||||
.frozen, .arc => if (mutable) return l.fail("E7005", place.span, "frozen graphs and arcs are immutable: borrow them with `ref`", &.{.{ .name = "name", .value = n }}) else try l.wrap(.ref, b.ty),
|
||||
// Atomic operations go through a shared borrow (spec 04 §23).
|
||||
.atomic => if (mutable) return l.fail("E7005", place.span, "an atomic cell is borrowed with `ref`: every atomic operation goes through a shared borrow", &.{.{ .name = "name", .value = n }}) else try l.wrap(.ref, b.ty),
|
||||
else => null,
|
||||
};
|
||||
const t = bt orelse return l.fail("E7006", place.span, "this value is not addressable here; borrow a variable, a field or an element", &.{.{ .name = "name", .value = n }});
|
||||
@@ -2164,6 +2191,101 @@ const Lowerer = struct {
|
||||
|
||||
// ------------------------------------------------------------ tasks and channels (layer 7)
|
||||
|
||||
// ------------------------------------------------------------ atomics (spec 15 §12, spec 04 §42)
|
||||
|
||||
/// An ordering, written as a bare word (spec 15 §12).
|
||||
fn ordering(l: *Lowerer, a: ast.Expr) Error![]const u8 {
|
||||
const words = [_][]const u8{ "relaxed", "acquire", "release", "acq_rel", "seq_cst" };
|
||||
if (a.kind == .name) for (words) |w| if (std.mem.eql(u8, a.kind.name, w)) return w;
|
||||
return l.fail("E7006", a.span, "an ordering is one of the words relaxed, acquire, release, acq_rel, seq_cst, written in place", &.{});
|
||||
}
|
||||
|
||||
/// The cell operand of an atomic operation: a `ref<atomic<T>>` binding or
|
||||
/// parameter, or `ref a` of an atomic, borrowed for the operation.
|
||||
fn cellOperand(l: *Lowerer, a: ast.Expr, temps: *std.ArrayList([]const u8)) Error!Value {
|
||||
if (a.kind == .borrow) {
|
||||
const br = a.kind.borrow;
|
||||
if (br.mutable) return l.fail("E7005", a.span, "atomic operations take `ref` of the cell, never `mut`", &.{});
|
||||
const b = try l.bindingOf(br.place.*, "an atomic cell");
|
||||
if (b.ty != .atomic) return l.fail("E7005", a.span, "an atomic operation takes `ref` of an `atomic<T>`", &.{.{ .name = "found", .value = l.tyText(b.ty) }});
|
||||
const r = try l.def(br.place.kind.name, try l.wrap(.ref, b.ty), try l.fmt("borrow.ref %{s}", .{b.value}), a.span);
|
||||
try temps.append(l.arena, r.name);
|
||||
return r;
|
||||
}
|
||||
const b = try l.bindingOf(a, "an atomic cell");
|
||||
if (b.ty == .atomic) return l.fail("E7005", a.span, "pass the cell as `ref a` (or a `ref` binding of it): atomic operations go through a shared borrow", &.{.{ .name = "suggestion", .value = try l.fmt("ref {s}", .{a.kind.name}) }});
|
||||
if (b.ty == .ref and b.ty.inner().? == .atomic) return .{ .name = b.value, .ty = b.ty };
|
||||
return l.fail("E7005", a.span, "an atomic operation takes a `ref<atomic<T>>`", &.{.{ .name = "found", .value = l.tyText(b.ty) }});
|
||||
}
|
||||
|
||||
fn atomicCall(l: *Lowerer, e: ast.Expr, p: []const u8, args: []const ast.Expr) Error!Value {
|
||||
const eq = std.mem.eql;
|
||||
if (eq(u8, p, "atomic.new")) {
|
||||
if (args.len != 1) return l.fail("E7005", e.span, "`atomic.new` takes the initial value", &.{});
|
||||
const v = try l.expr(args[0], null);
|
||||
if (!atomicScalar(v.ty)) return l.fail("E7005", args[0].span, "atomic<T> needs bool or an integer of at most 64 bits", &.{.{ .name = "found", .value = l.tyText(v.ty) }});
|
||||
return l.def("atomic", try l.wrap(.atomic, v.ty), try l.fmt("atomic.new %{s}", .{v.name}), e.span);
|
||||
}
|
||||
if (eq(u8, p, "atomic.take")) {
|
||||
if (args.len != 1) return l.fail("E7005", e.span, "`atomic.take` takes one cell", &.{});
|
||||
const c = try l.consuming(args[0], null);
|
||||
if (c.ty != .atomic) return l.fail("E7005", args[0].span, "`atomic.take` takes an `atomic<T>`", &.{.{ .name = "found", .value = l.tyText(c.ty) }});
|
||||
return l.def("v", c.ty.inner().?, try l.fmt("atomic.take %{s}", .{c.name}), e.span);
|
||||
}
|
||||
if (eq(u8, p, "atomic.fence")) {
|
||||
if (args.len != 1) return l.fail("E7005", e.span, "`atomic.fence` takes one ordering", &.{});
|
||||
try l.emit(try l.fmt("atomic.fence.{s}", .{try l.ordering(args[0])}), e.span);
|
||||
return .{ .name = "", .ty = .void };
|
||||
}
|
||||
if (eq(u8, p, "atomic.compare_exchange"))
|
||||
return l.fail("E7006", e.span, "`atomic.compare_exchange` has two results: `let (old, exchanged) = atomic.compare_exchange(c, expected, desired, success, failure);`", &.{});
|
||||
var temps: std.ArrayList([]const u8) = .empty;
|
||||
const want: usize = if (eq(u8, p, "atomic.load")) 2 else 3;
|
||||
if (args.len != want) return l.fail("E7005", e.span, "wrong number of operands", &.{ .{ .name = "operation", .value = p }, .{ .name = "expected", .value = try l.fmt("{d}", .{want}) } });
|
||||
const c = try l.cellOperand(args[0], &temps);
|
||||
const t = c.ty.inner().?.inner().?;
|
||||
const ord = try l.ordering(args[want - 1]);
|
||||
if (eq(u8, p, "atomic.load")) {
|
||||
const v = try l.def("v", t, try l.fmt("atomic.load.{s} %{s}", .{ ord, c.name }), e.span);
|
||||
try l.endNow(temps.items, e.span);
|
||||
return v;
|
||||
}
|
||||
const operand = try l.expr(args[1], t);
|
||||
try l.checkTy(args[1].span, operand.ty, t);
|
||||
if (eq(u8, p, "atomic.store")) {
|
||||
try l.emit(try l.fmt("atomic.store.{s} %{s}, %{s}", .{ ord, c.name, operand.name }), e.span);
|
||||
try l.endNow(temps.items, e.span);
|
||||
return .{ .name = "", .ty = .void };
|
||||
}
|
||||
const op: []const u8 = if (eq(u8, p, "atomic.swap")) "xchg" else p["atomic.".len..];
|
||||
if ((eq(u8, op, "add.wrap") or eq(u8, op, "sub.wrap")) and t != .int) return l.fail("E7005", args[1].span, "`atomic.add.wrap` and `atomic.sub.wrap` need an integer cell", &.{.{ .name = "found", .value = l.tyText(t) }});
|
||||
const v = try l.def("old", t, try l.fmt("atomic.{s}.{s} %{s}, %{s}", .{ op, ord, c.name, operand.name }), e.span);
|
||||
try l.endNow(temps.items, e.span);
|
||||
return v;
|
||||
}
|
||||
|
||||
/// `let (old, exchanged) = atomic.compare_exchange(c, expected, desired,
|
||||
/// success, failure);` (spec 15 §12): the second multi-result binding.
|
||||
fn compareExchangeLet(l: *Lowerer, s: ast.Stmt, names: []const []const u8, e: ast.Expr) Error!void {
|
||||
const args = e.kind.call.args;
|
||||
if (args.len != 5) return l.fail("E7005", e.span, "`atomic.compare_exchange` takes a cell, the expected and the desired value, and two orderings", &.{});
|
||||
var temps: std.ArrayList([]const u8) = .empty;
|
||||
const c = try l.cellOperand(args[0], &temps);
|
||||
const t = c.ty.inner().?.inner().?;
|
||||
const expected = try l.expr(args[1], t);
|
||||
try l.checkTy(args[1].span, expected.ty, t);
|
||||
const desired = try l.expr(args[2], t);
|
||||
try l.checkTy(args[2].span, desired.ty, t);
|
||||
const succ = try l.ordering(args[3]);
|
||||
const failure = try l.ordering(args[4]);
|
||||
const old = try l.freshName(names[0]);
|
||||
const ok = try l.freshName(names[1]);
|
||||
try l.emit(try l.fmt("%{s}, %{s} = atomic.cmpxchg.{s}.{s} %{s}, %{s}, %{s}", .{ old, ok, succ, failure, c.name, expected.name, desired.name }), s.span);
|
||||
try l.endNow(temps.items, s.span);
|
||||
try l.bindings.append(l.arena, .{ .name = names[0], .mutable = false, .value = old, .ty = t, .kind = .ssa });
|
||||
try l.bindings.append(l.arena, .{ .name = names[1], .mutable = false, .value = ok, .ty = .bool, .kind = .ssa });
|
||||
}
|
||||
|
||||
fn bindingOf(l: *Lowerer, a: ast.Expr, what: []const u8) Error!*Binding {
|
||||
if (a.kind != .name) return l.fail("E7005", a.span, try l.fmt("{s} is expected here, by name", .{what}), &.{});
|
||||
return l.lookup(a.kind.name) orelse l.unknown(a.span, a.kind.name);
|
||||
@@ -2926,15 +3048,17 @@ fn elemOf(t: Ty) ?Ty {
|
||||
/// `cap.narrow.budget`; null for anything else.
|
||||
fn pathOf(e: ast.Expr) ?[]const u8 {
|
||||
const known = [_][]const u8{
|
||||
"slice.copy", "own.new", "own.array", "own.take", "own.free",
|
||||
"region.create", "region.new", "region.array", "region.reset", "region.destroy",
|
||||
"cap.narrow.budget", "cap.narrow.prefix", "task.spawn", "task.join", "task.cancel",
|
||||
"task.cancelled", "channel.make", "channel.dup", "channel.send", "channel.recv",
|
||||
"channel.close_sender", "channel.close_receiver", "mutex.make", "mutex.take", "task.check_cancel",
|
||||
"task.join_cancel", "island.create", "island.alloc", "island.root", "island.at",
|
||||
"island.freeze", "island.destroy", "frozen.root", "frozen.at", "frozen.release",
|
||||
"arc.from_frozen", "arc.new", "arc.clone", "arc.release", "arc.root",
|
||||
"arc.at",
|
||||
"slice.copy", "own.new", "own.array", "own.take", "own.free",
|
||||
"region.create", "region.new", "region.array", "region.reset", "region.destroy",
|
||||
"cap.narrow.budget", "cap.narrow.prefix", "task.spawn", "task.join", "task.cancel",
|
||||
"task.cancelled", "channel.make", "channel.dup", "channel.send", "channel.recv",
|
||||
"channel.close_sender", "channel.close_receiver", "mutex.make", "mutex.take", "task.check_cancel",
|
||||
"task.join_cancel", "island.create", "island.alloc", "island.root", "island.at",
|
||||
"island.freeze", "island.destroy", "frozen.root", "frozen.at", "frozen.release",
|
||||
"arc.from_frozen", "arc.new", "arc.clone", "arc.release", "arc.root",
|
||||
"arc.at", "atomic.new", "atomic.take", "atomic.load", "atomic.store",
|
||||
"atomic.swap", "atomic.add.wrap", "atomic.sub.wrap", "atomic.and", "atomic.or",
|
||||
"atomic.xor", "atomic.compare_exchange", "atomic.fence",
|
||||
};
|
||||
var buf: [64]u8 = undefined;
|
||||
const text = dotted(e, &buf) orelse return null;
|
||||
|
||||
Reference in new issue
Block a user