Phase 4: islands, frozen graphs and arc through every layer (RFC 0014)
AIR: types island<T>, frozen<T>, arc<T>, iref<T>; 16 instructions (island.create/alloc/root/borrow/freeze/destroy, frozen.root/borrow/ release, arc.from_frozen/new/clone/release/root/borrow, iref.eq); features islands (depends on heap) and arc (depends on islands) published. Verifier: IslandSafe/ArcSafe (E2111 at the owner type), container child borrows (one mut access per island), Send/Share rows (island not Share), linear owners, mem.alloc effect, element rules (recursion through iref). Interpreter: graphs with never-reused identities, T0010 for a foreign iref, O(1) freeze/conversion/transfer, arc counts in a trace hook (Options.arc_events), budgets per the quota table, collector support. .aji layer 9: types, operations, island.at/frozen.at/arc.at as borrow places with explicit container borrows, iref ==/!=. Frontend fix: joins carry only variables the construct assigns (and that are read again), so an unchanged linear value is never a block argument while borrowed. Tests: 10 AIR (phase4), 6 .aji run + 12 rejects (layer9), the reference program island-pipeline in .aji, equivalent to an independently hand-written AIR program (36 on 30 schedule seeds); unit tests for the arc count trace and O(1) transfer. Quick Reference §7 graphs (checked excerpt of quickref/graphs.aji); coverage 61/61. Bundle growth: AIR +7.0%, Quick Reference +0.6k, source docs +4.2k (recorded). Spec: quota rows match the header-reserved count; idioms and iref recursion in spec 03 §22; P4-11 (air-text.ebnf gaps) recorded.
This commit is contained in:
1 parent
ca84ab4348
commit
c699529fec
58 files changed
+2189
-98
No files matched your search
@@ -0,0 +1,20 @@
|
||||
; Each arc handle is released once: releasing the same handle twice is E2002.
|
||||
; expect: reject E2002
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.arc_release_twice
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arc, capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%v = const.u64 1
|
||||
arc.new %v, %heap, ok -> @made, err -> @oom
|
||||
made(%a: arc<u64>):
|
||||
arc.release %a
|
||||
arc.release %a
|
||||
ret %v
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,19 @@
|
||||
; A frozen graph is linear: never released, it leaks (E3002).
|
||||
; expect: reject E3002
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.frozen_leak
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%v = const.u64 1
|
||||
island.create %v, %heap, ok -> @made, err -> @oom
|
||||
made(%i: island<u64>):
|
||||
%f = island.freeze %i
|
||||
ret %v
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,14 @@
|
||||
; iref.eq compares handles of the same type.
|
||||
; expect: reject E1001
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.iref_eq_types
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @same(%a: iref<u64>, %b: iref<u8>) -> bool
|
||||
effects []
|
||||
entry:
|
||||
%c = iref.eq %a, %b
|
||||
ret %c
|
||||
@@ -0,0 +1,25 @@
|
||||
; island.alloc needs a mutable borrow of the island.
|
||||
; expect: reject E1001
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.island_alloc_via_ref
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%v = const.u64 1
|
||||
island.create %v, %heap, ok -> @made, err -> @oom
|
||||
made(%i: island<u64>):
|
||||
%g = borrow.ref %i
|
||||
island.alloc %g, %v, ok -> @got, err -> @full
|
||||
got(%r: iref<u64>):
|
||||
borrow.end %g
|
||||
island.destroy %i
|
||||
ret %v
|
||||
full(%e2: alloc_error):
|
||||
trap
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,19 @@
|
||||
; A graph holds only IslandSafe values: an island of an owner is E2111.
|
||||
; expect: reject E2111
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.island_content_owner
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>, %o: own<u64>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
island.create %o, %heap, ok -> @made, err -> @oom
|
||||
made(%i: island<own<u64>>):
|
||||
island.destroy %i
|
||||
%z = const.u64 0
|
||||
ret %z
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,30 @@
|
||||
; One mutable access per island at a time, even through different irefs.
|
||||
; expect: reject E2101
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.island_two_mut
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%v = const.u64 1
|
||||
island.create %v, %heap, ok -> @made, err -> @oom
|
||||
made(%i: island<u64>):
|
||||
%g = borrow.mut %i
|
||||
%a = island.root %g
|
||||
island.alloc %g, %v, ok -> @got, err -> @full
|
||||
got(%b: iref<u64>):
|
||||
%x = island.borrow %g, %a
|
||||
%y = island.borrow %g, %b
|
||||
borrow.end %y
|
||||
borrow.end %x
|
||||
borrow.end %g
|
||||
island.destroy %i
|
||||
ret %v
|
||||
full(%e2: alloc_error):
|
||||
trap
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,22 @@
|
||||
; island.freeze consumes the island: a later use is E2001.
|
||||
; expect: reject E2001
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.island_use_after_freeze
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%v = const.u64 1
|
||||
island.create %v, %heap, ok -> @made, err -> @oom
|
||||
made(%i: island<u64>):
|
||||
%f = island.freeze %i
|
||||
%g = borrow.ref %i
|
||||
borrow.end %g
|
||||
frozen.release %f
|
||||
ret %v
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,13 @@
|
||||
; iref<T> belongs to the feature `islands`, which this module does not declare.
|
||||
; expect: reject E0006
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.islands_not_declared
|
||||
air 0
|
||||
lang 0.1
|
||||
features [ownership]
|
||||
|
||||
fn @keep(%r: iref<u64>) -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
@@ -0,0 +1,41 @@
|
||||
; island → frozen → arc keeps one graph: an iref taken from the island reads the same
|
||||
; object through the arc; a clone and two releases; the last release frees the graph,
|
||||
; so the same budget fits a new graph.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:24
|
||||
; result: 7
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.arc_lifecycle
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arc, capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%v = const.u64 7
|
||||
island.create %v, %heap, ok -> @made, err -> @oom
|
||||
made(%i: island<u64>):
|
||||
%g = borrow.ref %i
|
||||
%r = island.root %g
|
||||
borrow.end %g
|
||||
%f = island.freeze %i
|
||||
%a = arc.from_frozen %f
|
||||
%ar = borrow.ref %a
|
||||
%b = arc.clone %ar
|
||||
%x = arc.borrow %ar, %r
|
||||
%w = load %x
|
||||
borrow.end %x
|
||||
borrow.end %ar
|
||||
arc.release %a
|
||||
arc.release %b
|
||||
%one = const.u64 1
|
||||
arc.new %one, %heap, ok -> @again, err -> @oom2
|
||||
again(%c: arc<u64>):
|
||||
arc.release %c
|
||||
ret %w
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
oom2(%e2: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,42 @@
|
||||
; The root iref of island A used with island B traps T0010; iref.eq of the two is false.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:64
|
||||
; trap: T0010
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.island_foreign_iref
|
||||
air 0
|
||||
lang 0.1
|
||||
features [capabilities, effects, heap, islands, ownership, resources]
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc]
|
||||
entry:
|
||||
%v = const.u64 1
|
||||
island.create %v, %heap, ok -> @a_made, err -> @oom
|
||||
a_made(%a: island<u64>):
|
||||
island.create %v, %heap, ok -> @b_made, err -> @oom2
|
||||
b_made(%b: island<u64>):
|
||||
%ga = borrow.ref %a
|
||||
%gb = borrow.ref %b
|
||||
%ra = island.root %ga
|
||||
%rb = island.root %gb
|
||||
%same = iref.eq %ra, %rb
|
||||
cbr %same, @wrong(), @cross()
|
||||
cross:
|
||||
%x = island.borrow %gb, %ra
|
||||
%w = load %x
|
||||
borrow.end %x
|
||||
br @done()
|
||||
wrong:
|
||||
br @done()
|
||||
done:
|
||||
borrow.end %gb
|
||||
borrow.end %ga
|
||||
island.destroy %a
|
||||
island.destroy %b
|
||||
ret %v
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
oom2(%e2: alloc_error):
|
||||
trap
|
||||
@@ -0,0 +1,204 @@
|
||||
; Phase 4 exit program (hand-written AIR): island pipeline. A producer task builds a
|
||||
; cyclic graph in an island and moves it through a channel; the consumer freezes it,
|
||||
; lends it to two readers, turns it into an arc and sends a clone to a late reader.
|
||||
; expect: run
|
||||
; grant: heap=mem.alloc:1024
|
||||
; schedules: 30
|
||||
; quantum: 3
|
||||
; result: 36
|
||||
; phase: 4
|
||||
; spec: docs/spec/03-memory-model.md
|
||||
module test.island_pipeline
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arc, cancellation, capabilities, channels, effects, heap, islands, ownership, resources, tasks]
|
||||
|
||||
type @Node = struct { value: u64, next: @Link }
|
||||
type @Link = enum { End, To(iref<@Node>) }
|
||||
|
||||
fn @main(%heap: cap<mem.alloc>) -> u64
|
||||
effects [mem.alloc, task.spawn, task.wait]
|
||||
entry:
|
||||
%cap = const.usize 1
|
||||
channel.make island<@Node>, %cap, %heap, ok -> @made, err -> @oom
|
||||
made(%tx: sender<island<@Node>>, %rx: receiver<island<@Node>>):
|
||||
%sc1 = task.scope.begin
|
||||
%prod = task.spawn %sc1, @producer(%tx, %heap)
|
||||
%rxb = borrow.mut %rx
|
||||
channel.recv %rxb, ok -> @got, closed -> @none, cancelled -> @none
|
||||
got(%i: island<@Node>):
|
||||
borrow.end %rxb
|
||||
; the graph is complete and owned here: freeze it and share it with two readers
|
||||
%f = island.freeze %i
|
||||
%fb = borrow.ref %f
|
||||
%sc2 = task.scope.begin
|
||||
%six = const.u64 6
|
||||
%w1 = task.spawn %sc2, @walk(%fb, %six)
|
||||
%w2 = task.spawn %sc2, @walk(%fb, %six)
|
||||
%s1 = task.join %w1
|
||||
%s2 = task.join %w2
|
||||
task.scope.end %sc2
|
||||
borrow.end %fb
|
||||
%t1 = add.checked.u64 %s1, %s2
|
||||
; shared ownership from here on: one handle stays, a clone goes to the late reader
|
||||
%a = arc.from_frozen %f
|
||||
%cap2 = const.usize 1
|
||||
channel.make arc<@Node>, %cap2, %heap, ok -> @made2, err -> @oom2
|
||||
made2(%atx: sender<arc<@Node>>, %arx: receiver<arc<@Node>>):
|
||||
%sc3 = task.scope.begin
|
||||
%late = task.spawn %sc3, @late(%arx)
|
||||
%ab = borrow.ref %a
|
||||
%a2 = arc.clone %ab
|
||||
borrow.end %ab
|
||||
%atxb = borrow.mut %atx
|
||||
channel.send %atxb, %a2, ok -> @sent, closed -> @refused, cancelled -> @refused
|
||||
sent:
|
||||
br @after_send()
|
||||
refused(%back: arc<@Node>):
|
||||
arc.release %back
|
||||
br @after_send()
|
||||
after_send:
|
||||
borrow.end %atxb
|
||||
channel.close_sender %atx
|
||||
%s3 = task.join %late
|
||||
task.scope.end %sc3
|
||||
%t2 = add.checked.u64 %t1, %s3
|
||||
; the last release frees the graph
|
||||
arc.release %a
|
||||
br @finish(%t2)
|
||||
none:
|
||||
borrow.end %rxb
|
||||
%z = const.u64 0
|
||||
br @finish(%z)
|
||||
finish(%total: u64):
|
||||
channel.close_receiver %rx, ok -> @rx_closed, pending -> @rx_pending
|
||||
rx_pending(%rx2: receiver<island<@Node>>):
|
||||
; the producer sends one island and it was received: nothing can be buffered
|
||||
trap
|
||||
rx_closed:
|
||||
%pr = task.join %prod
|
||||
task.scope.end %sc1
|
||||
ret %total
|
||||
oom(%e: alloc_error):
|
||||
trap
|
||||
oom2(%e2: alloc_error):
|
||||
trap
|
||||
|
||||
fn @producer(%tx: sender<island<@Node>>, %heap: cap<mem.alloc>) -> u8
|
||||
effects [mem.alloc, task.wait]
|
||||
entry:
|
||||
%one = const.u64 1
|
||||
%end = enum.make @Link.End()
|
||||
%root = struct.make @Node(%one, %end)
|
||||
island.create %root, %heap, ok -> @created, err -> @oom_create
|
||||
created(%i: island<@Node>):
|
||||
%ib = borrow.mut %i
|
||||
%r = island.root %ib
|
||||
%three = const.u64 3
|
||||
%to_r = enum.make @Link.To(%r)
|
||||
%nc = struct.make @Node(%three, %to_r)
|
||||
island.alloc %ib, %nc, ok -> @have_c, err -> @oom_alloc_c
|
||||
have_c(%c: iref<@Node>):
|
||||
%two = const.u64 2
|
||||
%to_c = enum.make @Link.To(%c)
|
||||
%nb = struct.make @Node(%two, %to_c)
|
||||
island.alloc %ib, %nb, ok -> @have_b, err -> @oom_alloc_b
|
||||
have_b(%b: iref<@Node>):
|
||||
; close the ring: root.next = b, so 1 -> 2 -> 3 -> 1
|
||||
%rp = island.borrow %ib, %r
|
||||
%nf = borrow.field @Node.next, %rp
|
||||
%to_b = enum.make @Link.To(%b)
|
||||
store %nf, %to_b
|
||||
borrow.end %nf
|
||||
borrow.end %rp
|
||||
borrow.end %ib
|
||||
%txb = borrow.mut %tx
|
||||
channel.send %txb, %i, ok -> @sent, closed -> @refused, cancelled -> @refused
|
||||
sent:
|
||||
; %i is gone: the receiver owns the graph now
|
||||
borrow.end %txb
|
||||
br @finish()
|
||||
refused(%back: island<@Node>):
|
||||
borrow.end %txb
|
||||
island.destroy %back
|
||||
br @finish()
|
||||
finish:
|
||||
channel.close_sender %tx
|
||||
%z = const.u8 0
|
||||
ret %z
|
||||
oom_create(%e: alloc_error):
|
||||
trap
|
||||
oom_alloc_c(%e2: alloc_error):
|
||||
trap
|
||||
oom_alloc_b(%e3: alloc_error):
|
||||
trap
|
||||
|
||||
fn @walk(%f: ref<frozen<@Node>>, %steps: u64) -> u64
|
||||
effects []
|
||||
entry:
|
||||
%r = frozen.root %f
|
||||
%z = const.u64 0
|
||||
br @loop(%r, %z, %z)
|
||||
loop(%cur: iref<@Node>, %acc: u64, %k: u64):
|
||||
%done = eq.u64 %k, %steps
|
||||
cbr %done, @fin(), @step()
|
||||
step:
|
||||
%p = frozen.borrow %f, %cur
|
||||
%n = load %p
|
||||
borrow.end %p
|
||||
%v = struct.get @Node.value, %n
|
||||
%acc2 = add.checked.u64 %acc, %v
|
||||
%one = const.u64 1
|
||||
%k2 = add.checked.u64 %k, %one
|
||||
%nx = struct.get @Node.next, %n
|
||||
enum.switch @Link %nx, End -> @stay, To -> @move
|
||||
stay:
|
||||
br @loop(%cur, %acc2, %k2)
|
||||
move(%to: iref<@Node>):
|
||||
br @loop(%to, %acc2, %k2)
|
||||
fin:
|
||||
ret %acc
|
||||
|
||||
fn @late(%rx: receiver<arc<@Node>>) -> u64
|
||||
effects [task.wait]
|
||||
entry:
|
||||
%rxb = borrow.mut %rx
|
||||
channel.recv %rxb, ok -> @got, closed -> @none, cancelled -> @none
|
||||
got(%a: arc<@Node>):
|
||||
borrow.end %rxb
|
||||
%ab = borrow.ref %a
|
||||
%r = arc.root %ab
|
||||
%z = const.u64 0
|
||||
br @loop(%r, %z, %z)
|
||||
loop(%cur: iref<@Node>, %acc: u64, %k: u64):
|
||||
%six = const.u64 6
|
||||
%done = eq.u64 %k, %six
|
||||
cbr %done, @walked(), @step()
|
||||
step:
|
||||
%p = arc.borrow %ab, %cur
|
||||
%n = load %p
|
||||
borrow.end %p
|
||||
%v = struct.get @Node.value, %n
|
||||
%acc2 = add.checked.u64 %acc, %v
|
||||
%one = const.u64 1
|
||||
%k2 = add.checked.u64 %k, %one
|
||||
%nx = struct.get @Node.next, %n
|
||||
enum.switch @Link %nx, End -> @stay, To -> @move
|
||||
stay:
|
||||
br @loop(%cur, %acc2, %k2)
|
||||
move(%to: iref<@Node>):
|
||||
br @loop(%to, %acc2, %k2)
|
||||
walked:
|
||||
borrow.end %ab
|
||||
arc.release %a
|
||||
br @close(%acc)
|
||||
none:
|
||||
borrow.end %rxb
|
||||
%zero = const.u64 0
|
||||
br @close(%zero)
|
||||
close(%sum: u64):
|
||||
channel.close_receiver %rx, ok -> @closed, pending -> @pending
|
||||
pending(%rx2: receiver<arc<@Node>>):
|
||||
trap
|
||||
closed:
|
||||
ret %sum
|
||||
Reference in new issue
Block a user