Phase 5: atomics through every layer (RFC 0015)

AIR: linear atomic<T> (AtomicScalar), atomic.new/take/load/store/RMW/
cmpxchg (two results)/fence with explicit orderings; feature atomics
published. Verifier: ordering tables (E4201), ordinary access (E4202),
effects [atomic] / [atomic, atomic.weak], linearity, take while lent
(E4101, also for mutex.take). Interpreter: timestamped message machine
(src/interp/weak.zig); views travel through spawn/join, channels, mutexes,
close and cancel; seq_cst reads the latest message and programs without
weak reads make no extra draw (existing records unchanged). aji explore:
exhaustive exploration that branches only before visible operations.
Litmus: 20/20 tests give exactly the oracle's sets; differential test
(1500 random programs) found no unsound outcome. .aji layer 10, Quick
Reference atomics, source coverage 67/67, audit report atomic_operations.
AIR bundle +5.9 %, QUICK.md +0.5k.
This commit is contained in:
dev committed 2026-10-06 08:05:01 +03:00
1 parent ce9ef03bc0
commit 4242132b2a
70 files changed
+2630 -80

No files matched your search

@@ -0,0 +1,19 @@
; add.wrap is defined for integers only (spec 04 §24.2).
; expect: reject E1001
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.add_bool
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> bool
effects [atomic]
entry:
%t = const.bool true
%c = atomic.new %t
%r = borrow.ref %c
%x = atomic.add.wrap.seq_cst %r, %t
borrow.end %r
%v = atomic.take %c
ret %v
@@ -0,0 +1,21 @@
; A failed compare-exchange is a read: no release part (spec 04 §24.1).
; expect: reject E4201
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.cmpxchg_failure_release
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic, atomic.weak]
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
%one = const.u64 1
%o, %ok = atomic.cmpxchg.acq_rel.acq_rel %r, %z, %one
borrow.end %r
%v = atomic.take %c
%k = const.u64 0
ret %k
@@ -0,0 +1,21 @@
; A compare-exchange failure ordering is never stronger than its success ordering (spec 04 §24.1).
; expect: reject E4201
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.cmpxchg_failure_stronger
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic, atomic.weak]
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
%one = const.u64 1
%o, %ok = atomic.cmpxchg.relaxed.acquire %r, %z, %one
borrow.end %r
%v = atomic.take %c
%k = const.u64 0
ret %k
@@ -0,0 +1,20 @@
; There is no relaxed fence (spec 04 §27).
; expect: reject E4201
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.fence_relaxed
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic, atomic.weak]
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
atomic.fence.relaxed
borrow.end %r
%v = atomic.take %c
%k = const.u64 0
ret %k
@@ -0,0 +1,20 @@
; An atomic cell is not IslandSafe: a frozen graph is deeply immutable (spec 02 §6).
; expect: reject E2111
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.island_content
air 0
lang 0.1
features [atomics, capabilities, effects, heap, islands, ownership, resources]
fn @main(%heap: cap<mem.alloc>) -> u64
effects [mem.alloc]
entry:
%z = const.u64 0
%c = atomic.new %z
island.create %c, %heap, ok -> @made, err -> @oom
made(%i: island<atomic<u64>>):
island.destroy %i
ret %z
oom(%e: alloc_error):
trap
+15
View File
@@ -0,0 +1,15 @@
; An atomic cell is linear: it is taken, moved or returned (spec 04 §23).
; expect: reject E3002
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.leak
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects []
entry:
%z = const.u64 0
%c = atomic.new %z
ret %z
@@ -0,0 +1,20 @@
; A load cannot be release (spec 04 §24).
; expect: reject E4201
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.load_release
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic, atomic.weak]
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
%x = atomic.load.release %r
borrow.end %r
%v = atomic.take %c
%k = const.u64 0
ret %k
@@ -0,0 +1,20 @@
; Every atomic memory operation needs the effect atomic (spec 04 §25).
; expect: reject E3101
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.missing_atomic_effect
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects []
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
%x = atomic.load.seq_cst %r
borrow.end %r
%v = atomic.take %c
%k = const.u64 0
ret %k
@@ -0,0 +1,20 @@
; A non-seq_cst access needs atomic.weak besides atomic (spec 04 §25).
; expect: reject E3101
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.missing_weak_effect
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic]
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
%x = atomic.load.acquire %r
borrow.end %r
%v = atomic.take %c
%k = const.u64 0
ret %k
@@ -0,0 +1,21 @@
; Atomic operations take ref<atomic<T>>, never mut (spec 04 §23).
; expect: reject E1001
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.mut_operand
air 0
lang 0.1
features [atomics, effects, ownership]
fn @peek(%c: mut<atomic<u64>>) -> u64
effects [atomic]
entry:
%x = atomic.load.seq_cst %c
%k = const.u64 0
ret %k
fn @main() -> u64
effects []
entry:
%k = const.u64 0
ret %k
@@ -0,0 +1,20 @@
; atomic<T> needs the feature atomics in the header (spec 07 §8.1).
; expect: reject E0006
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.not_declared
air 0
lang 0.1
features [effects, ownership]
fn @peek(%c: ref<atomic<u64>>) -> u64
effects []
entry:
%k = const.u64 0
ret %k
fn @main() -> u64
effects []
entry:
%k = const.u64 0
ret %k
@@ -0,0 +1,20 @@
; An atomic cell is read only by atomic operations (spec 04 §28).
; expect: reject E4202
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.ordinary_load
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic, atomic.weak]
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
%x = load %r
borrow.end %r
%v = atomic.take %c
%k = const.u64 0
ret %k
@@ -0,0 +1,21 @@
; A store cannot be acquire (spec 04 §24).
; expect: reject E4201
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.store_acquire
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic, atomic.weak]
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
%one = const.u64 1
atomic.store.acquire %r, %one
borrow.end %r
%v = atomic.take %c
%k = const.u64 0
ret %k
@@ -0,0 +1,16 @@
; atomic<T> is linear: as a struct field it needs linear_aggregates (spec 04 §23).
; expect: reject E0006
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.struct_field
air 0
lang 0.1
features [atomics, effects, ownership]
type @S = struct { n: atomic<u64> }
fn @main() -> u64
effects []
entry:
%k = const.u64 0
ret %k
@@ -0,0 +1,19 @@
; atomic.take needs every borrow of the cell ended (spec 04 §28).
; expect: reject E2101
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.take_while_borrowed
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic]
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
%v = atomic.take %c
%x = atomic.load.seq_cst %r
borrow.end %r
ret %v
@@ -0,0 +1,28 @@
; atomic.take while the cell is lent to a live task is a race (spec 04 §28).
; expect: reject E4101
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.take_while_lent
air 0
lang 0.1
features [atomics, cancellation, effects, ownership, tasks]
fn @reader(%c: ref<atomic<u64>>) -> u64
effects [atomic]
entry:
%x = atomic.load.seq_cst %c
ret %x
fn @main() -> u64
effects [atomic, task.spawn, task.wait]
entry:
%z = const.u64 0
%c = atomic.new %z
%r = borrow.ref %c
%s = task.scope.begin
%t = task.spawn %s, @reader(%r)
%v = atomic.take %c
%x = task.join %t
task.scope.end %s
borrow.end %r
ret %v
@@ -0,0 +1,20 @@
; atomic<T> holds bool or an integer of at most 64 bits (spec 04 §23).
; expect: reject E1001
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.u128_cell
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic]
entry:
%z = const.u128 0
%c = atomic.new %z
%r = borrow.ref %c
%x = atomic.load.seq_cst %r
borrow.end %r
%v = atomic.take %c
%k = const.u64 0
ret %k
@@ -0,0 +1,27 @@
; A strong compare-exchange returns the old value and whether it exchanged; a non-seq_cst one needs atomic.weak.
; expect: run
; result: 14
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.atomic_cmpxchg
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u64
effects [atomic, atomic.weak]
entry:
%five = const.u64 5
%c = atomic.new %five
%r = borrow.ref %c
%seven = const.u64 7
%old1, %ok1 = atomic.cmpxchg.seq_cst.seq_cst %r, %five, %seven
assert %ok1
%old2, %ok2 = atomic.cmpxchg.acq_rel.acquire %r, %five, %five
%no = const.bool true
%failed = xor.bool %ok2, %no
assert %failed
borrow.end %r
%v = atomic.take %c
%sum = add.checked.u64 %v, %old2
ret %sum
@@ -0,0 +1,36 @@
; Two tasks add to one cell with relaxed add.wrap: atomicity makes the total exact on every schedule.
; expect: run
; schedules: 30
; quantum: 1
; result: 6
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.atomic_counter
air 0
lang 0.1
features [atomics, cancellation, effects, ownership, tasks]
fn @bump(%c: ref<atomic<u64>>) -> u64
effects [atomic, atomic.weak]
entry:
%one = const.u64 1
%a = atomic.add.wrap.relaxed %c, %one
%b = atomic.add.wrap.relaxed %c, %one
%d = atomic.add.wrap.relaxed %c, %one
ret %d
fn @main() -> u64
effects [atomic, atomic.weak, task.spawn, task.wait]
entry:
%z = const.u64 0
%cell = atomic.new %z
%r = borrow.ref %cell
%s = task.scope.begin
%t1 = task.spawn %s, @bump(%r)
%t2 = task.spawn %s, @bump(%r)
%x1 = task.join %t1
%x2 = task.join %t2
task.scope.end %s
borrow.end %r
%total = atomic.take %cell
ret %total
@@ -0,0 +1,48 @@
; Release/acquire publication: whenever the reader sees the flag, it sees the data (never 1 with 0).
; expect: run
; schedules: 30
; quantum: 1
; result: 1
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.atomic_publication
air 0
lang 0.1
features [atomics, cancellation, effects, ownership, tasks]
fn @writer(%d: ref<atomic<u64>>, %f: ref<atomic<bool>>) -> u64
effects [atomic, atomic.weak]
entry:
%v = const.u64 42
atomic.store.relaxed %d, %v
%t = const.bool true
atomic.store.release %f, %t
ret %v
fn @main() -> u64
effects [atomic, atomic.weak, task.spawn, task.wait]
entry:
%z = const.u64 0
%no = const.bool false
%data = atomic.new %z
%flag = atomic.new %no
%rd = borrow.ref %data
%rf = borrow.ref %flag
%s = task.scope.begin
%t = task.spawn %s, @writer(%rd, %rf)
%seen = atomic.load.acquire %rf
%val = atomic.load.relaxed %rd
%w = task.join %t
task.scope.end %s
borrow.end %rd
borrow.end %rf
%dv = atomic.take %data
%fv = atomic.take %flag
cbr %seen, @published(), @done()
published:
%ok = eq.u64 %val, %w
assert %ok
br @done()
done:
%one = const.u64 1
ret %one
@@ -0,0 +1,44 @@
; Every read-modify-write returns the old value; add.wrap and sub.wrap are modular; bool cells take xchg, and, or, xor.
; expect: run
; result: 10
; phase: 5
; spec: docs/spec/04-concurrency-and-atomics.md
module test.atomic_rmw_ops
air 0
lang 0.1
features [atomics, effects, ownership]
fn @main() -> u8
effects [atomic]
entry:
%z = const.u8 0
%c = atomic.new %z
%r = borrow.ref %c
%one = const.u8 1
%o1 = atomic.sub.wrap.seq_cst %r, %one
%v6 = const.u8 6
%o2 = atomic.xchg.seq_cst %r, %v6
%v3 = const.u8 3
%o3 = atomic.and.seq_cst %r, %v3
%v8 = const.u8 8
%o4 = atomic.or.seq_cst %r, %v8
%o5 = atomic.xor.seq_cst %r, %v3
%o6 = atomic.add.wrap.seq_cst %r, %one
borrow.end %r
%final = atomic.take %c
%e1 = const.u8 255
%k1 = eq.u8 %o2, %e1
assert %k1
%e2 = const.u8 2
%k2 = eq.u8 %o4, %e2
assert %k2
%t = const.bool true
%b = atomic.new %t
%rb = borrow.ref %b
%ob = atomic.xor.seq_cst %rb, %t
borrow.end %rb
%fb = atomic.take %b
assert %ob
%nf = xor.bool %fb, %t
assert %nf
ret %final