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
+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