Phase 5: race detector (P5-6) and the atomic_stats reference program (P5-9b)
Dynamic data-race detector for ordinary memory: vector clocks are the task-keyed part of the weak-memory views, so happens-before follows every synchronization edge already modelled; per-cell shadows with path granularity. Enabled for every conformance run test (433, no false positive); a unit test shows an unverified racy program caught on every schedule. Reference program atomic_stats in .aji and hand-written AIR by an independent author (equivalent:, 30 schedule seeds). Author feedback: 07a clarifications; Q-025 (no array construction) logged.
This commit is contained in:
1 parent
4242132b2a
commit
0879b9497b
10 files changed
+481
-10
No files matched your search
@@ -0,0 +1,145 @@
|
||||
; Phase 5 reference program (hand-written AIR): atomic statistics. Three workers sum
|
||||
; their parts of a lent array into a relaxed counter, raise a shared maximum with a
|
||||
; compare-exchange loop, and count themselves done with an acq_rel add; the last one
|
||||
; publishes the total, which it must see complete (release sequence of RMWs).
|
||||
; expect: run
|
||||
; schedules: 30
|
||||
; quantum: 3
|
||||
; result: 26091
|
||||
; phase: 5
|
||||
; spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module test.atomic_stats
|
||||
air 0
|
||||
lang 0.1
|
||||
features [arrays, atomics, cancellation, effects, ownership, tasks]
|
||||
|
||||
fn @work(%part: slice<u64>, %total: ref<atomic<u64>>, %max: ref<atomic<u64>>, %done: ref<atomic<u64>>, %report: ref<atomic<u64>>) -> u8
|
||||
effects [atomic, atomic.weak]
|
||||
entry:
|
||||
%i0 = const.usize 0
|
||||
%z = const.u64 0
|
||||
br @scan(%i0, %z, %z)
|
||||
scan(%i: usize, %sum: u64, %hi: u64):
|
||||
%n = slice.len %part
|
||||
%end = eq.usize %i, %n
|
||||
cbr %end, @publish(%sum, %hi), @elem(%i, %sum, %hi)
|
||||
elem(%j: usize, %s: u64, %h: u64):
|
||||
%e = borrow.index %part, %j
|
||||
%x = load %e
|
||||
borrow.end %e
|
||||
%s2 = add.checked.u64 %s, %x
|
||||
%bigger = gt.u64 %x, %h
|
||||
cbr %bigger, @next(%j, %s2, %x), @next(%j, %s2, %h)
|
||||
next(%k: usize, %s3: u64, %h3: u64):
|
||||
%one = const.usize 1
|
||||
%k2 = add.checked.usize %k, %one
|
||||
br @scan(%k2, %s3, %h3)
|
||||
publish(%psum: u64, %pmax: u64):
|
||||
%old = atomic.add.wrap.relaxed %total, %psum
|
||||
br @raise(%pmax)
|
||||
raise(%m: u64):
|
||||
%cur = atomic.load.relaxed %max
|
||||
%low = le.u64 %m, %cur
|
||||
cbr %low, @count(), @try(%m, %cur)
|
||||
try(%m2: u64, %seen: u64):
|
||||
%prev, %ok = atomic.cmpxchg.relaxed.relaxed %max, %seen, %m2
|
||||
cbr %ok, @count(), @raise(%m2)
|
||||
count:
|
||||
%uno = const.u64 1
|
||||
%f = atomic.add.wrap.acq_rel %done, %uno
|
||||
%two = const.u64 2
|
||||
%last = eq.u64 %f, %two
|
||||
cbr %last, @report(), @finish()
|
||||
report:
|
||||
%t = atomic.load.relaxed %total
|
||||
atomic.store.release %report, %t
|
||||
br @finish()
|
||||
finish:
|
||||
%r = const.u8 0
|
||||
ret %r
|
||||
|
||||
fn @main() -> u64
|
||||
effects [atomic, atomic.weak, task.spawn, task.wait]
|
||||
entry:
|
||||
%arr = local.alloc array<u64, 6>
|
||||
local.zero %arr
|
||||
%w = borrow.mut %arr
|
||||
%a0 = const.u64 3
|
||||
%a1 = const.u64 9
|
||||
%a2 = const.u64 4
|
||||
%a3 = const.u64 1
|
||||
%a4 = const.u64 7
|
||||
%a5 = const.u64 2
|
||||
%i0 = const.usize 0
|
||||
%i1 = const.usize 1
|
||||
%i2 = const.usize 2
|
||||
%i3 = const.usize 3
|
||||
%i4 = const.usize 4
|
||||
%i5 = const.usize 5
|
||||
%e0 = borrow.index %w, %i0
|
||||
store %e0, %a0
|
||||
borrow.end %e0
|
||||
%e1 = borrow.index %w, %i1
|
||||
store %e1, %a1
|
||||
borrow.end %e1
|
||||
%e2 = borrow.index %w, %i2
|
||||
store %e2, %a2
|
||||
borrow.end %e2
|
||||
%e3 = borrow.index %w, %i3
|
||||
store %e3, %a3
|
||||
borrow.end %e3
|
||||
%e4 = borrow.index %w, %i4
|
||||
store %e4, %a4
|
||||
borrow.end %e4
|
||||
%e5 = borrow.index %w, %i5
|
||||
store %e5, %a5
|
||||
borrow.end %e5
|
||||
borrow.end %w
|
||||
%z = const.u64 0
|
||||
%ctotal = atomic.new %z
|
||||
%cmax = atomic.new %z
|
||||
%cdone = atomic.new %z
|
||||
%creport = atomic.new %z
|
||||
%rtotal = borrow.ref %ctotal
|
||||
%rmax = borrow.ref %cmax
|
||||
%rdone = borrow.ref %cdone
|
||||
%rreport = borrow.ref %creport
|
||||
%ra = borrow.ref %arr
|
||||
%s = slice.make %ra
|
||||
%pa, %rest = slice.split %s, %i2
|
||||
%pb, %pc = slice.split %rest, %i2
|
||||
%sc = task.scope.begin
|
||||
%ta = task.spawn %sc, @work(%pa, %rtotal, %rmax, %rdone, %rreport)
|
||||
%tb = task.spawn %sc, @work(%pb, %rtotal, %rmax, %rdone, %rreport)
|
||||
%tc = task.spawn %sc, @work(%pc, %rtotal, %rmax, %rdone, %rreport)
|
||||
%xa = task.join %ta
|
||||
%xb = task.join %tb
|
||||
%xc = task.join %tc
|
||||
task.scope.end %sc
|
||||
borrow.end %pb
|
||||
borrow.end %pc
|
||||
borrow.end %pa
|
||||
borrow.end %rest
|
||||
borrow.end %s
|
||||
borrow.end %ra
|
||||
borrow.end %rtotal
|
||||
borrow.end %rmax
|
||||
borrow.end %rdone
|
||||
borrow.end %rreport
|
||||
%vtotal = atomic.take %ctotal
|
||||
%vmax = atomic.take %cmax
|
||||
%vdone = atomic.take %cdone
|
||||
%vreport = atomic.take %creport
|
||||
%k1000 = const.u64 1000
|
||||
%k10 = const.u64 10
|
||||
%p1 = mul.checked.u64 %vtotal, %k1000
|
||||
%p2 = mul.checked.u64 %vmax, %k10
|
||||
%p3 = add.checked.u64 %p1, %p2
|
||||
%same = eq.u64 %vreport, %vtotal
|
||||
cbr %same, @ok(%p3), @no(%p3)
|
||||
ok(%acc: u64):
|
||||
%one = const.u64 1
|
||||
%res = add.checked.u64 %acc, %one
|
||||
ret %res
|
||||
no(%acc2: u64):
|
||||
ret %acc2
|
||||
@@ -0,0 +1,85 @@
|
||||
// Reference program (Phase 5): atomic statistics. Three workers each sum one third
|
||||
// of a lent array into a relaxed counter and raise a shared maximum with a
|
||||
// compare_exchange loop. Each then counts itself done with an acq_rel add.wrap; the
|
||||
// last one to finish synchronizes with the others through that counter (a release
|
||||
// sequence of read-modify-writes), so its relaxed read of the total sees every add,
|
||||
// and it publishes the total as a report. Result: total * 1000 + max * 10 + (report == total).
|
||||
// expect: run
|
||||
// schedules: 30
|
||||
// quantum: 3
|
||||
// result: 26091
|
||||
// equivalent: phase5/run/program_atomic_stats.air
|
||||
// phase: 6
|
||||
// spec: docs/spec/04-concurrency-and-atomics.md
|
||||
module demo.atomic_stats
|
||||
|
||||
fn work(part: slice<u64>, total: ref<atomic<u64>>, max: ref<atomic<u64>>, done: ref<atomic<u64>>, report: ref<atomic<u64>>) -> u8
|
||||
effects [atomic, atomic.weak]
|
||||
{
|
||||
var sum: u64 = 0;
|
||||
var top: u64 = 0;
|
||||
var i: usize = 0;
|
||||
while i < part.len {
|
||||
sum = sum + part[i];
|
||||
if part[i] > top {
|
||||
top = part[i];
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
let before = atomic.add.wrap(total, sum, relaxed);
|
||||
loop {
|
||||
let current = atomic.load(max, relaxed);
|
||||
if top <= current {
|
||||
break;
|
||||
}
|
||||
let (old, exchanged) = atomic.compare_exchange(max, current, top, relaxed, relaxed);
|
||||
if exchanged {
|
||||
break;
|
||||
}
|
||||
}
|
||||
let finished = atomic.add.wrap(done, 1u64, acq_rel);
|
||||
if finished == 2 {
|
||||
let seen = atomic.load(total, relaxed);
|
||||
atomic.store(report, seen, release);
|
||||
}
|
||||
0u8
|
||||
}
|
||||
|
||||
fn main() -> u64
|
||||
effects [atomic, atomic.weak, task.spawn, task.wait]
|
||||
{
|
||||
var data: [u64; 6] = [0u64; 6];
|
||||
data[0] = 3;
|
||||
data[1] = 9;
|
||||
data[2] = 4;
|
||||
data[3] = 1;
|
||||
data[4] = 7;
|
||||
data[5] = 2;
|
||||
let total = atomic.new(0u64);
|
||||
let max = atomic.new(0u64);
|
||||
let done = atomic.new(0u64);
|
||||
let report = atomic.new(0u64);
|
||||
{
|
||||
ref t = total;
|
||||
ref m = max;
|
||||
ref d = done;
|
||||
ref r = report;
|
||||
ref all = data;
|
||||
let (first, rest) = all.split_at(2);
|
||||
let (second, third) = rest.split_at(2);
|
||||
scope s {
|
||||
let a = task.spawn(s, work, first, t, m, d, r);
|
||||
let b = task.spawn(s, work, second, t, m, d, r);
|
||||
let c = task.spawn(s, work, third, t, m, d, r);
|
||||
let ra = task.join(move a);
|
||||
let rb = task.join(move b);
|
||||
let rc = task.join(move c);
|
||||
}
|
||||
}
|
||||
let sum = atomic.take(move total);
|
||||
let top = atomic.take(move max);
|
||||
let n = atomic.take(move done);
|
||||
let published = atomic.take(move report);
|
||||
let flag: u64 = if published == sum { 1 } else { 0 };
|
||||
sum * 1000 + top * 10 + flag
|
||||
}
|
||||
Reference in new issue
Block a user