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:
dev committed 2026-10-04 16:02:03 +03:00
1 parent 2289917af3
commit ab5814a7bb
31 files changed
+1252 -94

No files matched your search

@@ -0,0 +1,71 @@
; Two tasks receive the disjoint halves of one buffer.
; expect: accept
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_disjoint_halves
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> array<u64, 4>
effects [task.spawn]
entry:
%buf = local.alloc array<u64, 4>
local.zero %buf
%m = borrow.mut %buf
%s = slice.make %m
%i0 = const.usize 0
%i1 = const.usize 1
%i2 = const.usize 2
%i3 = const.usize 3
%e0 = borrow.index %s, %i0
%v1 = const.u64 1
store %e0, %v1
borrow.end %e0
%e1 = borrow.index %s, %i1
%v2 = const.u64 2
store %e1, %v2
borrow.end %e1
%e2 = borrow.index %s, %i2
%v3 = const.u64 3
store %e2, %v3
borrow.end %e2
%e3 = borrow.index %s, %i3
%v4 = const.u64 4
store %e3, %v4
borrow.end %e3
%two = const.usize 2
%l, %r = slice.split %s, %two
%sc = task.scope.begin
%t1 = task.spawn %sc, @square(%l)
%t2 = task.spawn %sc, @square(%r)
task.join %t1
task.join %t2
task.scope.end %sc
borrow.end %l
borrow.end %r
borrow.end %s
borrow.end %m
%out = local.load %buf
ret %out
fn @square(%s: slice_mut<u64>) -> void
effects []
entry:
%n = slice.len %s
%z = const.usize 0
br @loop(%z)
loop(%i: usize):
%c = lt.usize %i, %n
cbr %c, @body(), @done()
body:
%e = borrow.index.proven %s, %i
%v = load %e
%sq = mul.checked.u64 %v, %v
store %e, %sq
borrow.end %e
%one = const.usize 1
%j = add.checked.usize %i, %one
br @loop(%j)
done:
ret
@@ -0,0 +1,71 @@
; The parent reads a buffer half it lent exclusively to a running task.
; expect: reject E4101
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_access_lent
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> void
effects [task.spawn]
entry:
%buf = local.alloc array<u64, 4>
local.zero %buf
%m = borrow.mut %buf
%s = slice.make %m
%i0 = const.usize 0
%i1 = const.usize 1
%i2 = const.usize 2
%i3 = const.usize 3
%e0 = borrow.index %s, %i0
%v1 = const.u64 1
store %e0, %v1
borrow.end %e0
%e1 = borrow.index %s, %i1
%v2 = const.u64 2
store %e1, %v2
borrow.end %e1
%e2 = borrow.index %s, %i2
%v3 = const.u64 3
store %e2, %v3
borrow.end %e2
%e3 = borrow.index %s, %i3
%v4 = const.u64 4
store %e3, %v4
borrow.end %e3
%two = const.usize 2
%l, %r = slice.split %s, %two
%sc = task.scope.begin
%t1 = task.spawn %sc, @square(%l)
%z = const.usize 0
%e = borrow.index %l, %z
borrow.end %e
task.join %t1
task.scope.end %sc
borrow.end %l
borrow.end %r
borrow.end %s
borrow.end %m
ret
fn @square(%s: slice_mut<u64>) -> void
effects []
entry:
%n = slice.len %s
%z = const.usize 0
br @loop(%z)
loop(%i: usize):
%c = lt.usize %i, %n
cbr %c, @body(), @done()
body:
%e = borrow.index.proven %s, %i
%v = load %e
%sq = mul.checked.u64 %v, %v
store %e, %sq
borrow.end %e
%one = const.usize 1
%j = add.checked.usize %i, %one
br @loop(%j)
done:
ret
@@ -0,0 +1,23 @@
; A spawned function returning a borrow needs the reserved feature task_borrow_returns.
; expect: reject E0006
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_borrow_return
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> void
effects [task.spawn]
entry:
%x = local.alloc u8
local.zero %x
%r = borrow.ref %x
%sc = task.scope.begin
%t = task.spawn %sc, @same(%r)
trap
fn @same(%p: ref<u8>) -> ref<u8> from %p
effects []
entry:
ret %p
@@ -0,0 +1,68 @@
; A borrow lent to a task is ended before the join.
; expect: reject E4302
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_end_lent_borrow
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> void
effects [task.spawn]
entry:
%buf = local.alloc array<u64, 4>
local.zero %buf
%m = borrow.mut %buf
%s = slice.make %m
%i0 = const.usize 0
%i1 = const.usize 1
%i2 = const.usize 2
%i3 = const.usize 3
%e0 = borrow.index %s, %i0
%v1 = const.u64 1
store %e0, %v1
borrow.end %e0
%e1 = borrow.index %s, %i1
%v2 = const.u64 2
store %e1, %v2
borrow.end %e1
%e2 = borrow.index %s, %i2
%v3 = const.u64 3
store %e2, %v3
borrow.end %e2
%e3 = borrow.index %s, %i3
%v4 = const.u64 4
store %e3, %v4
borrow.end %e3
%two = const.usize 2
%l, %r = slice.split %s, %two
%sc = task.scope.begin
%t1 = task.spawn %sc, @square(%l)
borrow.end %l
task.join %t1
task.scope.end %sc
borrow.end %r
borrow.end %s
borrow.end %m
ret
fn @square(%s: slice_mut<u64>) -> void
effects []
entry:
%n = slice.len %s
%z = const.usize 0
br @loop(%z)
loop(%i: usize):
%c = lt.usize %i, %n
cbr %c, @body(), @done()
body:
%e = borrow.index.proven %s, %i
%v = load %e
%sq = mul.checked.u64 %v, %v
store %e, %sq
borrow.end %e
%one = const.usize 1
%j = add.checked.usize %i, %one
br @loop(%j)
done:
ret
@@ -0,0 +1,13 @@
; Task handles are function-bound; passing one needs the reserved feature task_transfer.
; expect: reject E0006
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_handle_as_parameter
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @f(%t: task<u8>) -> void
effects []
entry:
trap
@@ -0,0 +1,32 @@
; A borrow of a task handle cannot be sent to another task.
; expect: reject E4001
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_not_send
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> void
effects [task.spawn]
entry:
%sc = task.scope.begin
%t = task.spawn %sc, @noop()
%tm = borrow.mut %t
%u = task.spawn %sc, @hold(%tm)
task.join %u
borrow.end %tm
%r = task.join %t
task.scope.end %sc
ret
fn @noop() -> u8
effects []
entry:
%z = const.u8 0
ret %z
fn @hold(%x: mut<task<u8>>) -> void
effects []
entry:
ret
@@ -0,0 +1,29 @@
; A region is not Share: lending ref<region> to a task is rejected.
; expect: reject E4002
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_not_share
air 0
lang 0.1
features [capabilities, effects, heap, ownership, regions, resources, tasks]
fn @main(%heap: cap<mem.alloc>) -> void
effects [task.spawn, mem.alloc]
entry:
region.create %heap, ok -> @have, err -> @nomem
have(%reg: region):
%rb = borrow.ref %reg
%sc = task.scope.begin
%t = task.spawn %sc, @peek(%rb)
task.join %t
task.scope.end %sc
borrow.end %rb
region.destroy %reg
ret
nomem(%e: alloc_error):
trap
fn @peek(%r: ref<region>) -> void
effects []
entry:
ret
@@ -0,0 +1,70 @@
; The same exclusive borrow is lent to two live tasks.
; expect: reject E4101
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_same_mut_twice
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> void
effects [task.spawn]
entry:
%buf = local.alloc array<u64, 4>
local.zero %buf
%m = borrow.mut %buf
%s = slice.make %m
%i0 = const.usize 0
%i1 = const.usize 1
%i2 = const.usize 2
%i3 = const.usize 3
%e0 = borrow.index %s, %i0
%v1 = const.u64 1
store %e0, %v1
borrow.end %e0
%e1 = borrow.index %s, %i1
%v2 = const.u64 2
store %e1, %v2
borrow.end %e1
%e2 = borrow.index %s, %i2
%v3 = const.u64 3
store %e2, %v3
borrow.end %e2
%e3 = borrow.index %s, %i3
%v4 = const.u64 4
store %e3, %v4
borrow.end %e3
%two = const.usize 2
%l, %r = slice.split %s, %two
%sc = task.scope.begin
%t1 = task.spawn %sc, @square(%l)
%t2 = task.spawn %sc, @square(%l)
task.join %t1
task.join %t2
task.scope.end %sc
borrow.end %l
borrow.end %r
borrow.end %s
borrow.end %m
ret
fn @square(%s: slice_mut<u64>) -> void
effects []
entry:
%n = slice.len %s
%z = const.usize 0
br @loop(%z)
loop(%i: usize):
%c = lt.usize %i, %n
cbr %c, @body(), @done()
body:
%e = borrow.index.proven %s, %i
%v = load %e
%sq = mul.checked.u64 %v, %v
store %e, %sq
borrow.end %e
%one = const.usize 1
%j = add.checked.usize %i, %one
br @loop(%j)
done:
ret
@@ -0,0 +1,21 @@
; The scope ends while a task spawned from it is not joined.
; expect: reject E4301
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_scope_end_unjoined
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> void
effects [task.spawn]
entry:
%sc = task.scope.begin
%t = task.spawn %sc, @noop()
task.scope.end %sc
ret
fn @noop() -> void
effects []
entry:
ret
@@ -0,0 +1,22 @@
; task.spawn requires the effect task.spawn.
; expect: reject E3101
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_spawn_without_effect
air 0
lang 0.1
features [arrays, effects, ownership, tasks]
fn @main() -> void
effects []
entry:
%sc = task.scope.begin
%t = task.spawn %sc, @noop()
task.join %t
task.scope.end %sc
ret
fn @noop() -> void
effects []
entry:
ret
@@ -0,0 +1,31 @@
; cancel only requests: the child sees the flag, returns, and must still be joined.
; expect: run
; schedules: 10
; result: 7
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_cancel_request
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> u8
effects [task.spawn]
entry:
%sc = task.scope.begin
%t = task.spawn %sc, @worker()
task.cancel %t
%r = task.join %t
task.scope.end %sc
ret %r
fn @worker() -> u8
effects []
entry:
br @poll()
poll:
%c = task.cancelled
cbr %c, @stop(), @poll()
stop:
%v = const.u8 7
ret %v
@@ -0,0 +1,94 @@
; Every task waits and no event can wake one: the run is blocked.
; expect: run
; grant: listen=net.listen
; blocked: no_future_event
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_join_deadlock
air 0
lang 0.1
features [arrays, capabilities, effects, ownership, resources, tasks]
fn @main(%listen: cap<net.listen>) -> void
effects [task.spawn, net.listen]
entry:
%sc = task.scope.begin
%t = task.spawn %sc, @serve(%listen)
task.join %t
task.scope.end %sc
ret
fn @serve(%listen: cap<net.listen>) -> void
effects [net.listen]
entry:
%a = local.alloc array<u8, 11>
local.zero %a
%am = borrow.mut %a
%ai0 = const.usize 0
%ac0 = const.u8 49
%ae0 = borrow.index %am, %ai0
store %ae0, %ac0
borrow.end %ae0
%ai1 = const.usize 1
%ac1 = const.u8 50
%ae1 = borrow.index %am, %ai1
store %ae1, %ac1
borrow.end %ae1
%ai2 = const.usize 2
%ac2 = const.u8 55
%ae2 = borrow.index %am, %ai2
store %ae2, %ac2
borrow.end %ae2
%ai3 = const.usize 3
%ac3 = const.u8 46
%ae3 = borrow.index %am, %ai3
store %ae3, %ac3
borrow.end %ae3
%ai4 = const.usize 4
%ac4 = const.u8 48
%ae4 = borrow.index %am, %ai4
store %ae4, %ac4
borrow.end %ae4
%ai5 = const.usize 5
%ac5 = const.u8 46
%ae5 = borrow.index %am, %ai5
store %ae5, %ac5
borrow.end %ae5
%ai6 = const.usize 6
%ac6 = const.u8 48
%ae6 = borrow.index %am, %ai6
store %ae6, %ac6
borrow.end %ae6
%ai7 = const.usize 7
%ac7 = const.u8 46
%ae7 = borrow.index %am, %ai7
store %ae7, %ac7
borrow.end %ae7
%ai8 = const.usize 8
%ac8 = const.u8 49
%ae8 = borrow.index %am, %ai8
store %ae8, %ac8
borrow.end %ae8
%ai9 = const.usize 9
%ac9 = const.u8 58
%ae9 = borrow.index %am, %ai9
store %ae9, %ac9
borrow.end %ae9
%ai10 = const.usize 10
%ac10 = const.u8 57
%ae10 = borrow.index %am, %ai10
store %ae10, %ac10
borrow.end %ae10
borrow.end %am
%ar = borrow.ref %a
%as = slice.make %ar
intrinsic net.listen(%listen, %as), ok -> @listening, err -> @fail
listening(%l: listener):
%lm = borrow.mut %l
intrinsic net.accept(%lm), ok -> @accepted, err -> @fail2
accepted(%st: stream):
trap
fail2(%e2: io_error):
trap
fail(%e: io_error):
trap
@@ -0,0 +1,28 @@
; Spawning beyond the deterministic task limit is an environment outcome, not a trap.
; expect: run
; task_limit: 2
; limit_exceeded: tasks
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_limit
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> void
effects [task.spawn]
entry:
%sc = task.scope.begin
%a = task.spawn %sc, @noop()
%b = task.spawn %sc, @noop()
%c = task.spawn %sc, @noop()
task.join %a
task.join %b
task.join %c
task.scope.end %sc
ret
fn @noop() -> void
effects []
entry:
ret
@@ -0,0 +1,74 @@
; Parallel squaring of two halves gives the same result under every explored schedule.
; expect: run
; schedules: 20
; quantum: 3
; result: [1,4,9,16]
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_parallel_square
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> array<u64, 4>
effects [task.spawn]
entry:
%buf = local.alloc array<u64, 4>
local.zero %buf
%m = borrow.mut %buf
%s = slice.make %m
%i0 = const.usize 0
%i1 = const.usize 1
%i2 = const.usize 2
%i3 = const.usize 3
%e0 = borrow.index %s, %i0
%v1 = const.u64 1
store %e0, %v1
borrow.end %e0
%e1 = borrow.index %s, %i1
%v2 = const.u64 2
store %e1, %v2
borrow.end %e1
%e2 = borrow.index %s, %i2
%v3 = const.u64 3
store %e2, %v3
borrow.end %e2
%e3 = borrow.index %s, %i3
%v4 = const.u64 4
store %e3, %v4
borrow.end %e3
%two = const.usize 2
%l, %r = slice.split %s, %two
%sc = task.scope.begin
%t1 = task.spawn %sc, @square(%l)
%t2 = task.spawn %sc, @square(%r)
task.join %t1
task.join %t2
task.scope.end %sc
borrow.end %l
borrow.end %r
borrow.end %s
borrow.end %m
%out = local.load %buf
ret %out
fn @square(%s: slice_mut<u64>) -> void
effects []
entry:
%n = slice.len %s
%z = const.usize 0
br @loop(%z)
loop(%i: usize):
%c = lt.usize %i, %n
cbr %c, @body(), @done()
body:
%e = borrow.index.proven %s, %i
%v = load %e
%sq = mul.checked.u64 %v, %v
store %e, %sq
borrow.end %e
%one = const.usize 1
%j = add.checked.usize %i, %one
br @loop(%j)
done:
ret
@@ -0,0 +1,33 @@
; The same shared borrow of Share data may be lent to two tasks.
; expect: run
; schedules: 10
; result: 42
; phase: 3
; spec: docs/spec/04-concurrency-and-atomics.md
module test.tasks_shared_read
air 0
lang 0.1
features [arrays, ownership, tasks]
fn @main() -> u64
effects [task.spawn]
entry:
%x = local.alloc u64
%v = const.u64 21
local.store %x, %v
%r = borrow.ref %x
%sc = task.scope.begin
%t1 = task.spawn %sc, @read(%r)
%t2 = task.spawn %sc, @read(%r)
%a = task.join %t1
%b = task.join %t2
task.scope.end %sc
borrow.end %r
%sum = add.checked.u64 %a, %b
ret %sum
fn @read(%p: ref<u64>) -> u64
effects []
entry:
%v = load %p
ret %v