Files
aji/tests/conformance/phase4/run/program_island_pipeline.air
T
dev c699529fec 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.
2026-10-05 22:14:05 +03:00

205 lines
5.8 KiB
LLVM

; 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