Phase 3: structured tasks and the deterministic scheduler (RFC 0011)
- interpreter core rewritten around explicit per-task frame stacks so a task can be suspended at any instruction; behavior of all existing tests unchanged - tasks: scope and task<T> (function-bound; task_transfer and task_borrow_returns reserved), spawn/join/cancel/cancelled/scope.end; cooperative scheduler with seeded choice rule, separate schedule seed, quantum = fuel unit, task limit (limit_exceeded kind tasks), global quiescence over waiting tasks; schedule field in the run record - verifier: Send/Share table (E4001, E4002), loans as task-owned borrows (E4302 on early end, E4101 on conflicts), E4301 at scope end, task effects (E3101) - CLI --quantum, --task-limit, --schedule-seed; runner schedules:, quantum:, task_limit:, limit_exceeded: - fixed: schedule seed default used orelse/^ precedence wrongly - 15 phase-3 conformance tests; 4000 mutants without a crash - Agent Reference Bundle +10.9 % tokens (recorded)
This commit is contained in:
1 parent
2289917af3
commit
ab5814a7bb
31 files changed
+1252
-94
No files matched your search
@@ -29,6 +29,8 @@ const Seg = union(enum) {
|
||||
|
||||
const Mode = enum { shared, exclusive };
|
||||
|
||||
const Loan = struct { task: ValueId, borrow: ValueId, root: ValueId, path: []const Seg, mode: Mode };
|
||||
|
||||
/// Why a resource is no longer live (spec 06 §6.3).
|
||||
const Dead = enum { none, closed, moved };
|
||||
|
||||
@@ -65,6 +67,11 @@ const Walker = struct {
|
||||
live: []bool,
|
||||
/// How a resource stopped being live, for diagnostics.
|
||||
dead: []Dead,
|
||||
/// Borrows lent to tasks (spec 04 §34). A loan is active while its task
|
||||
/// handle is live, so it is part of the per-path state through `live`.
|
||||
loans: std.ArrayList(Loan) = .empty,
|
||||
/// Scope of each task handle value.
|
||||
task_scope: []?ValueId,
|
||||
block: u32 = 0,
|
||||
index: u32 = 0,
|
||||
span: air.Span = .{ .line = 0, .col = 0 },
|
||||
@@ -127,6 +134,14 @@ const Walker = struct {
|
||||
try w.report(code, message, who, .{ "conflicts_with", @intCast(cv) });
|
||||
return;
|
||||
}
|
||||
// Borrows lent to live tasks are accessed concurrently (spec 04 §34).
|
||||
for (w.loans.items) |l| {
|
||||
if (!w.live[l.task] or l.root != root) continue;
|
||||
if (mode == .shared and l.mode == .shared) continue;
|
||||
if (!overlap(path, l.path)) continue;
|
||||
try w.report("E4101", "conflicts with a borrow lent to a live task", who, .{ "task", l.task });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/// Records a new owned borrow and checks it against live borrows.
|
||||
@@ -262,7 +277,9 @@ fn checkFunction(c: *Context, f: air.Function, types: []const air.Type) !void {
|
||||
.info = try c.arena.alloc(?Info, f.values.len),
|
||||
.live = try c.arena.alloc(bool, f.values.len),
|
||||
.dead = try c.arena.alloc(Dead, f.values.len),
|
||||
.task_scope = try c.arena.alloc(?ValueId, f.values.len),
|
||||
};
|
||||
@memset(w.task_scope, null);
|
||||
@memset(w.info, null);
|
||||
@memset(w.live, false);
|
||||
@memset(w.dead, .none);
|
||||
@@ -377,6 +394,9 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
if (!w.live[v]) return w.report("E2106", "borrow ended twice", v, null);
|
||||
// Ended even when rejected, so one mistake yields one diagnostic.
|
||||
defer w.live[v] = false;
|
||||
for (w.loans.items) |l| if (w.live[l.task] and (l.borrow == v or w.isAncestor(v, l.borrow))) {
|
||||
return w.report("E4302", "borrow ended while it is lent to a live task", v, .{ "task", l.task });
|
||||
};
|
||||
for (w.live, 0..) |is_live, cv| {
|
||||
if (is_live and w.info[cv] != null and w.info[cv].?.parent == v) {
|
||||
return w.report("E2102", "borrow ended while a borrow derived from it is live", v, .{ "child", @intCast(cv) });
|
||||
@@ -397,6 +417,38 @@ fn step(w: *Walker, inst: air.Inst) !void {
|
||||
};
|
||||
},
|
||||
.own_free, .own_take => |v| try w.consume(v, .closed),
|
||||
.task_scope_begin => w.live[inst.result.?] = true,
|
||||
.task_spawn => |x| {
|
||||
const t = inst.result.?;
|
||||
w.task_scope[t] = x.scope;
|
||||
for (x.args) |a| {
|
||||
if (w.types[a].isBorrow()) {
|
||||
if (!try w.usable(a)) continue;
|
||||
const ai = w.info[a].?;
|
||||
// The loan must not conflict with live borrows or loans.
|
||||
try w.accessCode(ai.root, ai.path, ai.mode, a, a, "E4101", "lending a borrow that conflicts with a live borrow");
|
||||
try w.loans.append(w.c.arena, .{ .task = t, .borrow = a, .root = ai.root, .path = ai.path, .mode = ai.mode });
|
||||
} else if (w.isResource(a)) try w.consume(a, .moved);
|
||||
}
|
||||
w.live[t] = true;
|
||||
},
|
||||
.task_join => |t| {
|
||||
// Joining ends the task's loans (they are active only while `t` lives).
|
||||
try w.consume(t, .closed);
|
||||
if (inst.result) |r| if (w.isResource(r)) {
|
||||
w.live[r] = true;
|
||||
};
|
||||
},
|
||||
.task_cancel => |t| _ = try w.resourceUsable(t),
|
||||
.task_scope_end => |s| {
|
||||
for (w.live, 0..) |is_live, v| {
|
||||
if (is_live and w.task_scope[v] != null and w.task_scope[v].? == s) {
|
||||
try w.report("E4301", "scope ends while a task spawned from it is not joined", @intCast(v), .{ "scope", s });
|
||||
w.live[v] = false; // reported once, in preference to E3002
|
||||
}
|
||||
}
|
||||
try w.consume(s, .closed);
|
||||
},
|
||||
.region_reset => |v| {
|
||||
if (!try w.resourceUsable(v)) return;
|
||||
// Reset invalidates the generation: no reference may be live.
|
||||
|
||||
@@ -28,7 +28,7 @@ pub fn accountedSize(m: air.Module, t: air.Type, depth: usize) ?u64 {
|
||||
return switch (t) {
|
||||
.int => |it| it.bits() / 8,
|
||||
.bool => 1,
|
||||
.ref, .mut, .cap, .resource, .own, .region => 8,
|
||||
.ref, .mut, .cap, .resource, .own, .region, .scope, .task => 8,
|
||||
.own_array => 16,
|
||||
.slice, .slice_mut => 16,
|
||||
.builtin => |b| tagSize(b.variants().len),
|
||||
|
||||
@@ -16,6 +16,20 @@ pub fn check(c: *Context) !void {
|
||||
if (!m.declares("effects")) return;
|
||||
for (m.functions) |f| {
|
||||
for (f.blocks) |b| for (b.insts) |inst| switch (inst.op) {
|
||||
.task_spawn => |x| {
|
||||
if (!contains(f.effects, "task.spawn")) try c.report("E3101", inst.span, "task.spawn requires the effect task.spawn", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "effect", .value = .{ .str = "task.spawn" } },
|
||||
});
|
||||
const callee = m.functions[x.callee];
|
||||
for (callee.effects) |e| if (!std.mem.eql(u8, e, "unsafe") and !contains(f.effects, e)) {
|
||||
try c.report("E3101", inst.span, "task function effect not declared by the spawning function", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "callee", .value = .{ .str = callee.name } },
|
||||
.{ .name = "effect", .value = .{ .str = e } },
|
||||
});
|
||||
};
|
||||
},
|
||||
.call => |x| {
|
||||
const callee = m.functions[x.callee];
|
||||
// `unsafe` is checked by the unsafe stage, which knows about
|
||||
|
||||
+96
-6
@@ -85,7 +85,7 @@ 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 => pos != .member,
|
||||
.resource, .own, .own_array, .region, .scope, .task => pos != .member,
|
||||
else => true,
|
||||
};
|
||||
if (!ok) try c.report("E1001", span, "type not allowed in this position", &.{
|
||||
@@ -93,9 +93,9 @@ fn wellFormed(c: *Context, span: air.Span, t: Type, pos: Position, owner: []cons
|
||||
.{ .name = "position", .value = .{ .str = @tagName(pos) } },
|
||||
.{ .name = "found", .value = .{ .str = try c.typeName(t) } },
|
||||
});
|
||||
// The pointee of ref/mut may be a linear value (`mut<file<read>>`);
|
||||
// every other compound holds value types only.
|
||||
if (t.inner()) |inner| try wellFormed(c, span, inner, if (t == .ref or t == .mut) .borrowed else .member, owner);
|
||||
// The pointee of ref/mut may be a linear value (`mut<file<read>>`), and
|
||||
// a task may return one; every other compound holds value types only.
|
||||
if (t.inner()) |inner| try wellFormed(c, span, inner, if (t == .ref or t == .mut or t == .task) .borrowed else .member, owner);
|
||||
}
|
||||
|
||||
/// `local.alloc` results are checked separately: their own type is storage.
|
||||
@@ -210,6 +210,8 @@ fn resultType(m: air.Module, op: air.Op) ?Type {
|
||||
.enum_make => |x| .{ .named = x.decl },
|
||||
.call => |x| m.functions[x.callee].ret,
|
||||
.slice_len => .{ .int = .usize },
|
||||
.task_scope_begin => .scope,
|
||||
.task_cancelled => .bool,
|
||||
.intrinsic => |x| blk: {
|
||||
const reg = air.authority.intrinsics[x.index];
|
||||
if (reg.fallible or reg.results.len == 0) break :blk .void;
|
||||
@@ -307,6 +309,80 @@ fn checkIntrinsic(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "intrinsic")
|
||||
}
|
||||
}
|
||||
|
||||
/// `Send` (spec 04 §35): may move to another task.
|
||||
pub fn isSend(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap, .resource, .region => true,
|
||||
.own, .own_array => |inner| isSend(m, inner.*),
|
||||
.ref, .slice => |inner| isShare(m, inner.*),
|
||||
.mut, .slice_mut => |inner| isSend(m, inner.*),
|
||||
.array => |a| isSend(m, a.elem),
|
||||
.named => |i| aggregateAll(m, i, isSend),
|
||||
.scope, .task, .local, .void, .never => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// `Share` (spec 04 §35): may be observed concurrently through shared borrows.
|
||||
pub fn isShare(m: air.Module, t: Type) bool {
|
||||
return switch (t) {
|
||||
.int, .bool, .builtin, .cap => true,
|
||||
.resource, .region, .mut, .slice_mut, .scope, .task, .local, .void, .never => false,
|
||||
.own, .own_array, .ref, .slice => |inner| isShare(m, inner.*),
|
||||
.array => |a| isShare(m, a.elem),
|
||||
.named => |i| aggregateAll(m, i, isShare),
|
||||
};
|
||||
}
|
||||
|
||||
fn aggregateAll(m: air.Module, i: u32, comptime pred: fn (air.Module, Type) bool) bool {
|
||||
return switch (m.types[i].kind) {
|
||||
.@"struct" => |fs| for (fs) |f| {
|
||||
if (!pred(m, f.ty)) break false;
|
||||
} else true,
|
||||
.@"enum" => |vs| {
|
||||
for (vs) |v| for (v.payload) |p| if (!pred(m, p)) return false;
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/// `task.spawn` (spec 04 §33.2, §35): arguments like a call, Send/Share
|
||||
/// rules per argument, and a borrow-free, Send result.
|
||||
fn checkSpawn(fc: *FnCtx, inst: air.Inst, x: @FieldType(air.Op, "task_spawn")) !void {
|
||||
const m = fc.c.module;
|
||||
const f = fc.f;
|
||||
const span = inst.span;
|
||||
if (fc.typeOf(x.scope) != .scope) try fc.kindError(span, x.scope, "scope");
|
||||
const callee = m.functions[x.callee];
|
||||
const tys = try fc.c.arena.alloc(Type, callee.params.len);
|
||||
for (callee.params, tys) |p, *t| t.* = p.ty;
|
||||
try fc.expectArgs(span, x.args, tys);
|
||||
if (x.args.len == tys.len) for (x.args, tys) |a, pt| {
|
||||
const shared = pt == .ref or pt == .slice;
|
||||
const ok = if (shared) isShare(m, pt.inner().?) else isSend(m, pt);
|
||||
if (!ok) try fc.c.report(if (shared) "E4002" else "E4001", span, if (shared) "data lent to a task through a shared borrow must be Share" else "a value moved or exclusively lent to a task must be Send", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "task_function", .value = .{ .str = callee.name } },
|
||||
.{ .name = "argument", .value = .{ .str = try fc.c.valueName(f, a) } },
|
||||
.{ .name = "type", .value = .{ .str = try fc.c.typeName(pt) } },
|
||||
});
|
||||
};
|
||||
const r = callee.ret;
|
||||
if (callee.ret_from != null or r.isBorrow()) {
|
||||
try fc.c.report("E0006", span, "construct belongs to a feature the module does not declare", &.{
|
||||
.{ .name = "reason", .value = .{ .str = "feature_not_declared" } },
|
||||
.{ .name = "feature", .value = .{ .str = "task_borrow_returns" } },
|
||||
.{ .name = "construct", .value = .{ .str = "spawned function returning a borrow" } },
|
||||
});
|
||||
} else if (r.isTaskBound()) {
|
||||
try fc.c.report("E1001", span, "a task result must not contain a scope or task handle", &.{.{ .name = "function", .value = .{ .str = f.name } }});
|
||||
} else if (r != .void and r != .never and !isSend(m, r)) {
|
||||
try fc.c.report("E4001", span, "a task result must be Send", &.{
|
||||
.{ .name = "function", .value = .{ .str = f.name } },
|
||||
.{ .name = "task_function", .value = .{ .str = callee.name } },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
fn boxed(arena: std.mem.Allocator, t: Type) !*const Type {
|
||||
const b = try arena.create(Type);
|
||||
b.* = t;
|
||||
@@ -328,6 +404,8 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
.borrow_index => |x| fc.ty[x.borrow] orelse return null,
|
||||
.slice_sub => |x| fc.ty[x.slice] orelse return null,
|
||||
.own_take => |v| fc.ty[v] orelse return null,
|
||||
.task_join => |v| fc.ty[v] orelse return null,
|
||||
.task_spawn => |x| return .{ .task = try boxed(a, m.functions[x.callee].ret) },
|
||||
.slice_split => |x| fc.ty[x.slice] orelse return null,
|
||||
else => return null,
|
||||
};
|
||||
@@ -336,7 +414,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 => .{ .ref = try boxed(a, t) },
|
||||
.resource, .region, .task, .scope => .{ .ref = try boxed(a, t) },
|
||||
.own => |inner| .{ .ref = inner },
|
||||
.own_array => |inner| .{ .slice = inner },
|
||||
.slice, .slice_mut => .{ .slice = try boxed(a, t.inner().?) },
|
||||
@@ -344,7 +422,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
},
|
||||
.borrow_mut => switch (t) {
|
||||
.local, .mut => .{ .mut = try boxed(a, t.inner().?) },
|
||||
.resource, .region => .{ .mut = try boxed(a, t) },
|
||||
.resource, .region, .task, .scope => .{ .mut = try boxed(a, t) },
|
||||
.own => |inner| .{ .mut = inner },
|
||||
.own_array => |inner| .{ .slice_mut = inner },
|
||||
.slice_mut => .{ .slice_mut = try boxed(a, t.inner().?) },
|
||||
@@ -376,6 +454,7 @@ fn derivedType(fc: *FnCtx, op: air.Op) !?Type {
|
||||
},
|
||||
.slice_sub, .slice_split => if (t.isSlice()) t else invalid,
|
||||
.own_take => if (t == .own) t.own.* else invalid,
|
||||
.task_join => if (t == .task) t.task.* else invalid,
|
||||
else => unreachable,
|
||||
};
|
||||
}
|
||||
@@ -589,6 +668,17 @@ fn checkInst(fc: *FnCtx, inst: air.Inst) !void {
|
||||
try fc.expectOkErr(span, x.ok, x.err, .{ .slice_mut = try boxed(fc.c.arena, t) });
|
||||
},
|
||||
.region_reset, .region_destroy => |v| if (fc.typeOf(v) != .region) try fc.kindError(span, v, "region"),
|
||||
.task_scope_begin, .task_cancelled => {},
|
||||
.task_scope_end => |v| if (fc.typeOf(v) != .scope) try fc.kindError(span, v, "scope"),
|
||||
.task_cancel => |v| if (fc.typeOf(v) != .task) try fc.kindError(span, v, "task<T>"),
|
||||
.task_join => |v| {
|
||||
const t = fc.typeOf(v);
|
||||
if (t != .task) return fc.kindError(span, v, "task<T>");
|
||||
const r = t.task.*;
|
||||
if (inst.result != null and r == .void) try fc.c.report("E1001", span, "joining a void task must not bind a result", &.{.{ .name = "function", .value = .{ .str = f.name } }});
|
||||
if (inst.result == null and r != .void and r != .never) try fc.c.report("E1001", span, "the result of task.join must be bound", &.{.{ .name = "function", .value = .{ .str = f.name } }});
|
||||
},
|
||||
.task_spawn => |x| try checkSpawn(fc, inst, x),
|
||||
.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