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
@@ -24,7 +24,7 @@ pub const Feature = struct {
|
||||
pub const registry = [_]Feature{
|
||||
.{ .name = "arc", .depends = &.{ "capabilities", "effects", "heap", "islands", "ownership", "resources" }, .status = .published, .phase = 4 },
|
||||
.{ .name = "arrays", .depends = &.{"ownership"}, .status = .published, .phase = 1 },
|
||||
.{ .name = "atomics", .depends = &.{ "effects", "ownership" }, .status = .reserved, .phase = 5 },
|
||||
.{ .name = "atomics", .depends = &.{ "effects", "ownership" }, .status = .published, .phase = 5 },
|
||||
.{ .name = "borrow_joins", .depends = &.{"ownership"}, .status = .reserved, .phase = null },
|
||||
.{ .name = "cancellation", .depends = &.{ "effects", "ownership", "tasks" }, .status = .published, .phase = 3 },
|
||||
.{ .name = "capabilities", .depends = &.{ "effects", "ownership" }, .status = .published, .phase = 2 },
|
||||
@@ -49,7 +49,7 @@ pub const registry = [_]Feature{
|
||||
};
|
||||
|
||||
/// Features implemented by this toolchain, sorted.
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "cancellation", "capabilities", "channels", "effects", "heap", "islands", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
pub const supported = [_][]const u8{ "arc", "arrays", "atomics", "cancellation", "capabilities", "channels", "effects", "heap", "islands", "ownership", "regions", "resources", "sync", "tasks", "unsafe" };
|
||||
|
||||
pub const Reason = enum {
|
||||
unknown_feature,
|
||||
|
||||
+67
-5
@@ -99,6 +99,8 @@ pub const Type = union(enum) {
|
||||
frozen: *const Type,
|
||||
arc: *const Type,
|
||||
iref: *const Type,
|
||||
// Feature `atomics` (spec 04 §23, §42): a linear atomic cell.
|
||||
atomic: *const Type,
|
||||
|
||||
pub const Array = struct { elem: Type, len: u64 };
|
||||
|
||||
@@ -107,7 +109,7 @@ pub const Type = union(enum) {
|
||||
return switch (a) {
|
||||
.int => |x| b.int == x,
|
||||
.named => |x| b.named == x,
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref => |x| x.eql(b.inner().?),
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |x| x.eql(b.inner().?),
|
||||
.region, .scope, .shield => true,
|
||||
.array => |x| x.len == b.array.len and x.elem.eql(b.array.elem),
|
||||
.cap => |k| b.cap == k,
|
||||
@@ -120,7 +122,7 @@ pub const Type = union(enum) {
|
||||
/// Element or pointee type of storage, borrow and slice types.
|
||||
pub fn inner(t: Type) ?Type {
|
||||
return switch (t) {
|
||||
.local, .ref, .mut, .slice, .slice_mut, .own, .own_array, .task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref => |x| x.*,
|
||||
.local, .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,
|
||||
};
|
||||
@@ -137,7 +139,7 @@ pub const Type = union(enum) {
|
||||
/// Linear values: consumed exactly once (spec 06 §6.2–§6.3).
|
||||
pub fn isLinear(t: Type) bool {
|
||||
return switch (t) {
|
||||
.resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc => true,
|
||||
.resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .atomic => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
@@ -171,7 +173,7 @@ pub const Type = union(enum) {
|
||||
/// Linear resources are neither values nor borrows: they are moved.
|
||||
pub fn isValue(t: Type) bool {
|
||||
return switch (t) {
|
||||
.void, .never, .local, .ref, .mut, .slice, .slice_mut, .resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc => false,
|
||||
.void, .never, .local, .ref, .mut, .slice, .slice_mut, .resource, .own, .own_array, .region, .scope, .task, .shield, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .atomic => false,
|
||||
else => true,
|
||||
};
|
||||
}
|
||||
@@ -239,6 +241,41 @@ pub const BitOp = enum { @"and", @"or", xor, shl, shr };
|
||||
pub const CmpOp = enum { eq, ne, lt, le, gt, ge };
|
||||
pub const CastMode = enum { checked, wrap, widen };
|
||||
|
||||
/// Memory orderings (spec 04 §24). There is no default and no `consume`.
|
||||
pub const Ordering = enum {
|
||||
relaxed,
|
||||
acquire,
|
||||
release,
|
||||
acq_rel,
|
||||
seq_cst,
|
||||
|
||||
pub fn hasAcquire(o: Ordering) bool {
|
||||
return o == .acquire or o == .acq_rel or o == .seq_cst;
|
||||
}
|
||||
|
||||
pub fn hasRelease(o: Ordering) bool {
|
||||
return o == .release or o == .acq_rel or o == .seq_cst;
|
||||
}
|
||||
};
|
||||
|
||||
/// Atomic read-modify-write operations (spec 04 §24.2).
|
||||
pub const RmwOp = enum {
|
||||
xchg,
|
||||
add_wrap,
|
||||
sub_wrap,
|
||||
@"and",
|
||||
@"or",
|
||||
xor,
|
||||
|
||||
pub fn text(op: RmwOp) []const u8 {
|
||||
return switch (op) {
|
||||
.add_wrap => "add.wrap",
|
||||
.sub_wrap => "sub.wrap",
|
||||
inline else => |o| @tagName(o),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const Op = union(enum) {
|
||||
// Feature `ownership` (spec 07a).
|
||||
local_alloc: Type,
|
||||
@@ -341,6 +378,15 @@ pub const Op = union(enum) {
|
||||
arc_root: ValueId,
|
||||
arc_borrow: struct { owner: ValueId, iref: ValueId },
|
||||
iref_eq: struct { a: ValueId, b: ValueId },
|
||||
// Feature `atomics` (spec 04 §42).
|
||||
atomic_new: ValueId,
|
||||
atomic_take: ValueId,
|
||||
atomic_load: struct { cell: ValueId, ord: Ordering },
|
||||
atomic_store: struct { cell: ValueId, value: ValueId, ord: Ordering },
|
||||
atomic_rmw: struct { op: RmwOp, cell: ValueId, value: ValueId, ord: Ordering },
|
||||
/// Two results: `Inst.result` (old value) and `Inst.result2` (exchanged).
|
||||
atomic_cmpxchg: struct { cell: ValueId, expected: ValueId, desired: ValueId, success: Ordering, failure: Ordering },
|
||||
atomic_fence: Ordering,
|
||||
ret: ?ValueId,
|
||||
trap,
|
||||
|
||||
@@ -375,7 +421,7 @@ pub const Op = union(enum) {
|
||||
|
||||
pub const Inst = struct {
|
||||
result: ?ValueId,
|
||||
/// Second result of `slice.split`.
|
||||
/// Second result of `slice.split` and `atomic.cmpxchg`.
|
||||
result2: ?ValueId = null,
|
||||
op: Op,
|
||||
span: Span,
|
||||
@@ -525,6 +571,22 @@ pub fn forEachUse(op: Op, ctx: anytype, comptime f: fn (@TypeOf(ctx), ValueId) v
|
||||
f(ctx, x.a);
|
||||
f(ctx, x.b);
|
||||
},
|
||||
.atomic_new, .atomic_take => |v| f(ctx, v),
|
||||
.atomic_load => |x| f(ctx, x.cell),
|
||||
.atomic_store => |x| {
|
||||
f(ctx, x.cell);
|
||||
f(ctx, x.value);
|
||||
},
|
||||
.atomic_rmw => |x| {
|
||||
f(ctx, x.cell);
|
||||
f(ctx, x.value);
|
||||
},
|
||||
.atomic_cmpxchg => |x| {
|
||||
f(ctx, x.cell);
|
||||
f(ctx, x.expected);
|
||||
f(ctx, x.desired);
|
||||
},
|
||||
.atomic_fence => {},
|
||||
.task_join_cancel => |x| f(ctx, x.task),
|
||||
.channel_make => |x| {
|
||||
f(ctx, x.cap);
|
||||
|
||||
+80
-3
@@ -560,7 +560,7 @@ const Parser = struct {
|
||||
boxed.* = inner;
|
||||
return if (eql(t.text, "own")) .{ .own = boxed } else .{ .own_array = boxed };
|
||||
}
|
||||
const Wrapper = enum { task, sender, receiver, mutex, guard, island, frozen, arc, iref };
|
||||
const Wrapper = enum { task, sender, receiver, mutex, guard, island, frozen, arc, iref, atomic };
|
||||
if (std.meta.stringToEnum(Wrapper, t.text)) |which| {
|
||||
try p.requireFeature(switch (which) {
|
||||
.task => "tasks",
|
||||
@@ -568,6 +568,7 @@ const Parser = struct {
|
||||
.mutex, .guard => "sync",
|
||||
.island, .frozen, .iref => "islands",
|
||||
.arc => "arc",
|
||||
.atomic => "atomics",
|
||||
}, t.span, t.text);
|
||||
_ = try p.expect(.lt, "`<`");
|
||||
// Element rules (spec 04 §38.1) are checked by the type stage.
|
||||
@@ -585,6 +586,7 @@ const Parser = struct {
|
||||
.frozen => .{ .frozen = boxed },
|
||||
.arc => .{ .arc = boxed },
|
||||
.iref => .{ .iref = boxed },
|
||||
.atomic => .{ .atomic = boxed },
|
||||
};
|
||||
}
|
||||
if (eql(t.text, "cap")) {
|
||||
@@ -1015,6 +1017,10 @@ const Parser = struct {
|
||||
}
|
||||
if (try p.parseAllocOp(op_tok)) |aop| break :op aop;
|
||||
if (try p.parseGraphOp(op_tok)) |gop| break :op gop;
|
||||
if (std.mem.startsWith(u8, op_tok.text, "atomic.")) {
|
||||
if (try p.parseAtomicOp(op_tok)) |aop| break :op aop;
|
||||
return p.unknownOpcode(op_tok);
|
||||
}
|
||||
if (try p.parseTaskOp(op_tok)) |top| break :op top;
|
||||
if (try p.parseCoordinationOp(op_tok)) |cop| break :op cop;
|
||||
if (eql(op_tok.text, "intrinsic")) {
|
||||
@@ -1046,6 +1052,7 @@ const Parser = struct {
|
||||
.own_take, .task_scope_begin, .task_spawn, .task_cancelled => true,
|
||||
.task_shield_begin, .channel_dup, .mutex_make, .mutex_take, .mutex_borrow => true,
|
||||
.island_root, .island_borrow, .island_freeze, .frozen_root, .frozen_borrow, .arc_from_frozen, .arc_clone, .arc_root, .arc_borrow, .iref_eq => true,
|
||||
.atomic_new, .atomic_take, .atomic_load, .atomic_rmw, .atomic_cmpxchg => true,
|
||||
.@"const", .copy, .arith, .bit, .cmp, .cast, .struct_make, .struct_get, .enum_make => true,
|
||||
.local_alloc, .local_load, .borrow_ref, .borrow_mut, .borrow_field, .load => true,
|
||||
.array_splat, .borrow_index, .slice_make, .slice_len, .slice_sub, .slice_split => true,
|
||||
@@ -1057,8 +1064,9 @@ const Parser = struct {
|
||||
} else if (produces_value) {
|
||||
return p.fail(op_tok, "result binding `%name =`");
|
||||
}
|
||||
if ((op == .slice_split) != (result2_tok != null)) {
|
||||
return p.fail(result_tok orelse op_tok, if (op == .slice_split) "two results `%left, %right =`" else "a single result");
|
||||
const two_results = op == .slice_split or op == .atomic_cmpxchg;
|
||||
if (two_results != (result2_tok != null)) {
|
||||
return p.fail(result_tok orelse op_tok, if (op == .slice_split) "two results `%left, %right =`" else if (op == .atomic_cmpxchg) "two results `%old, %exchanged =`" else "a single result");
|
||||
}
|
||||
const def: air.ValueInfo.Def = .{ .inst = .{ .block = block, .index = index } };
|
||||
const result = if (result_tok) |rt| try p.defineValue(rt, def) else null;
|
||||
@@ -1254,6 +1262,75 @@ const Parser = struct {
|
||||
}
|
||||
}
|
||||
|
||||
/// Instructions of the feature `atomics` (spec 04 §42). The orderings are
|
||||
/// part of the opcode; which are valid for an operation is the type
|
||||
/// stage's (`E4201`). Returns null for an opcode that is not one of them.
|
||||
fn parseAtomicOp(p: *Parser, op_tok: Token) Error!?air.Op {
|
||||
var buf: [5][]const u8 = undefined;
|
||||
var n: usize = 0;
|
||||
var it = std.mem.splitScalar(u8, op_tok.text, '.');
|
||||
while (it.next()) |part| {
|
||||
if (n == buf.len) return null;
|
||||
buf[n] = part;
|
||||
n += 1;
|
||||
}
|
||||
const parts = buf[0..n];
|
||||
const ord = struct {
|
||||
fn of(t: []const u8) ?air.Ordering {
|
||||
return std.meta.stringToEnum(air.Ordering, t);
|
||||
}
|
||||
}.of;
|
||||
const Name = enum { new, take, load, store, xchg, add, sub, @"and", @"or", xor, cmpxchg, fence };
|
||||
const name = std.meta.stringToEnum(Name, parts[1]) orelse return null;
|
||||
try p.requireFeature("atomics", op_tok.span, op_tok.text);
|
||||
switch (name) {
|
||||
.new, .take => {
|
||||
if (n != 2) return null;
|
||||
const v = try p.useValue();
|
||||
return if (name == .new) .{ .atomic_new = v } else .{ .atomic_take = v };
|
||||
},
|
||||
.load => {
|
||||
if (n != 3) return null;
|
||||
const o = ord(parts[2]) orelse return null;
|
||||
return .{ .atomic_load = .{ .cell = try p.useValue(), .ord = o } };
|
||||
},
|
||||
.store => {
|
||||
if (n != 3) return null;
|
||||
const o = ord(parts[2]) orelse return null;
|
||||
const c, const v = try p.twoOperands();
|
||||
return .{ .atomic_store = .{ .cell = c, .value = v, .ord = o } };
|
||||
},
|
||||
.xchg, .@"and", .@"or", .xor, .add, .sub => {
|
||||
const wraps = name == .add or name == .sub;
|
||||
if (n != (if (wraps) @as(usize, 4) else 3)) return null;
|
||||
if (wraps and !std.mem.eql(u8, parts[2], "wrap")) return null;
|
||||
const o = ord(parts[n - 1]) orelse return null;
|
||||
const op: air.RmwOp = switch (name) {
|
||||
.xchg => .xchg,
|
||||
.add => .add_wrap,
|
||||
.sub => .sub_wrap,
|
||||
.@"and" => .@"and",
|
||||
.@"or" => .@"or",
|
||||
else => .xor,
|
||||
};
|
||||
const c, const v = try p.twoOperands();
|
||||
return .{ .atomic_rmw = .{ .op = op, .cell = c, .value = v, .ord = o } };
|
||||
},
|
||||
.cmpxchg => {
|
||||
if (n != 4) return null;
|
||||
const sc = ord(parts[2]) orelse return null;
|
||||
const fl = ord(parts[3]) orelse return null;
|
||||
const c, const e = try p.twoOperands();
|
||||
_ = try p.expect(.comma, "`,`");
|
||||
return .{ .atomic_cmpxchg = .{ .cell = c, .expected = e, .desired = try p.useValue(), .success = sc, .failure = fl } };
|
||||
},
|
||||
.fence => {
|
||||
if (n != 3) return null;
|
||||
return .{ .atomic_fence = ord(parts[2]) orelse return null };
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Instructions of the features `heap` and `regions` (spec 03 §19–§20).
|
||||
fn parseAllocOp(p: *Parser, op_tok: Token) Error!?air.Op {
|
||||
const Name = enum { @"own.new", @"own.array", @"own.take", @"own.free", @"region.create", @"region.new", @"region.array", @"region.reset", @"region.destroy" };
|
||||
|
||||
+28
-1
@@ -122,7 +122,7 @@ pub fn printType(m: air.Module, t: air.Type, w: *Writer) Writer.Error!void {
|
||||
},
|
||||
.region => try w.writeAll("region"),
|
||||
.scope => try w.writeAll("scope"),
|
||||
.task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref => |inner| {
|
||||
.task, .sender, .receiver, .mutex, .guard, .island, .frozen, .arc, .iref, .atomic => |inner| {
|
||||
try w.print("{s}<", .{@tagName(t)});
|
||||
try printType(m, inner.*, w);
|
||||
try w.writeByte('>');
|
||||
@@ -422,6 +422,33 @@ fn printOp(m: air.Module, f: air.Function, op: air.Op, w: *Writer) Writer.Error!
|
||||
.frozen_borrow => |x| try two(f, w, "frozen.borrow", x.owner, x.iref),
|
||||
.arc_borrow => |x| try two(f, w, "arc.borrow", x.owner, x.iref),
|
||||
.iref_eq => |x| try two(f, w, "iref.eq", x.a, x.b),
|
||||
.atomic_new => |v| try one(f, w, "atomic.new", v),
|
||||
.atomic_take => |v| try one(f, w, "atomic.take", v),
|
||||
.atomic_load => |x| {
|
||||
try w.print("atomic.load.{s} ", .{@tagName(x.ord)});
|
||||
try value(f, x.cell, w);
|
||||
},
|
||||
.atomic_store => |x| {
|
||||
try w.print("atomic.store.{s} ", .{@tagName(x.ord)});
|
||||
try value(f, x.cell, w);
|
||||
try w.writeAll(", ");
|
||||
try value(f, x.value, w);
|
||||
},
|
||||
.atomic_rmw => |x| {
|
||||
try w.print("atomic.{s}.{s} ", .{ x.op.text(), @tagName(x.ord) });
|
||||
try value(f, x.cell, w);
|
||||
try w.writeAll(", ");
|
||||
try value(f, x.value, w);
|
||||
},
|
||||
.atomic_cmpxchg => |x| {
|
||||
try w.print("atomic.cmpxchg.{s}.{s} ", .{ @tagName(x.success), @tagName(x.failure) });
|
||||
try value(f, x.cell, w);
|
||||
try w.writeAll(", ");
|
||||
try value(f, x.expected, w);
|
||||
try w.writeAll(", ");
|
||||
try value(f, x.desired, w);
|
||||
},
|
||||
.atomic_fence => |o| try w.print("atomic.fence.{s}", .{@tagName(o)}),
|
||||
.cap_narrow => |x| {
|
||||
try w.print("cap.narrow.{s} ", .{x.form});
|
||||
try value(f, x.cap, w);
|
||||
|
||||
Reference in new issue
Block a user