Files
aji/docs/plan/phase-5-atomics-and-memory-model.md
2026-10-06 08:11:58 +03:00

13 KiB
Raw Permalink Blame History

Phase 5 — Atomics and memory model

Non-normative. Part of the implementation plan.

Goal: atomic types with all five orderings and fences, an interpreter that exhibits exactly the outcomes the memory model allows, systematic schedule exploration, and record/replay.

Exit criterion: every litmus test passes with exact equality between observed and allowed outcomes; atomic-publication conformance tests pass in AIR and .aji.

Spec inputs: spec 04 §20–§30, §42; spec 05 §13; spec 11; reference/concurrency-litmus.md. Decision record: RFC 0015 (Accepted, revision 2; supersedes RFC 0003).

Blocking questions: none. Q-015 was resolved by RFC 0015; Q-017 moved to Phase 8 with record/replay.

Implementation GO (owner, 2026-10-06). All three gates are closed:

Gate Result
M/C regression PASS
RFC 0015 Accepted
oracle self-tests 21/21
normative litmus corpus 20/20
Q-024 resolved

Order, strictly vertical:

AIR types and instructions → verifier → interpreter weak-memory exploration
→ .aji → Quick Reference → litmus conformance → reference program

The first thing the interpreter must show:

  • all 20 litmus tests give exactly the oracle's sets;
  • programs without atomic.weak keep their interleaving behavior, with unchanged run records.

Owner decision (2026-10-05, after the Phase 4 exit review). Phase 5 starts with the RFC. Implementation is on hold until all three of these are done:

  1. the RFC is reviewed and accepted;
  2. the M/C regression on the frozen Phase 4 commit passes (P5-0);
  3. the normative litmus corpus is ready (P5-1a).
Phase 4 commit frozen (tag p4-frozen)
        ├── private M/C regression (P5-0)
        └── RFC (P5-1) → review / Accepted → litmus corpus (P5-1a) → implementation (P5-2 …)
  • Scope. Scalar atomics and memory ordering only. Hazard pointers, epoch reclamation, RCU, lock-free collections and atomic irefs or references are left to a later RFC. Q-023 is not touched before Phase 5.
  • No atomics agent benchmark before the RFC. Without a normative model there is no correct result to measure. The litmus corpus comes first. Each litmus test states its set of allowed outcomes and its set of forbidden outcomes, not a single expected result.
  • Model statement. The Phase 3 sentence "a native execution equals some interleaving of the deterministic scheduler" is not enough for relaxed atomics. Phase 5 replaces it with three layers:
    • ordinary safe memory: DRF-SC;
    • atomic memory: the Aji atomic memory model;
    • unsafe code: its own proof obligations.
  • Questions the RFC must close before it can be accepted:
    • sequenced-before, synchronizes-with and happens-before;
    • data race, and the DRF-SC guarantee;
    • the five orderings, and which are allowed for load, store, RMW and CAS;
    • compare_exchange success and failure orderings;
    • fences;
    • mixing atomic and non-atomic access;
    • whether atomic.weak is a separate effect;
    • which atomic types the format guarantees and which depend on the target;
    • what counts as observable behavior of the deterministic interpreter;
    • how the deterministic interleaving model relates to weak memory;
    • what a native backend must preserve;
    • the safe/unsafe boundary for atomic pointers and reclamation.

Invariants

  • no atomic operation without an explicit, valid ordering;
  • safe code never accesses atomic storage as ordinary storage;
  • DRF-SC holds for programs without ordinary-memory races;
  • the interpreter neither forbids allowed outcomes (SB r1=0,r2=0) nor produces forbidden ones (MP).

Tasks

ID Status Task Depends Output Done when
P5-0 done (PASS, owner 2026-10-06) M/C regression on the frozen Phase 4 commit (tag p4-frozen): M, C × Aji/source × N1–N3, 6 cells, same agents, tasks, evaluators and procedure as P6A-9 — docs/evaluation/results/2026-10-06-p5-0.md see "Regression gate" below; 6/6 PASS; R3 found frontend defect A (ref through mut), fixed with a regression test
P5-1 done RFC 0015: scalar atomics and the Aji memory model (supersedes RFC 0003); closes the owner's question list; resolves Q-015; scope of Q-017 — docs/rfcs/0015-… Accepted by the owner and integrated into spec 04, 05, 07a, 11, 15
P5-1a done (corpus normative, owner 2026-10-06) Litmus enumerator tools/litmus (an executable oracle derived from spec 04 §20, not the definition) and the normative corpus: the classes MP, SB, LB, release/acquire publication, seq_cst ordering (IRIW, SB with seq_cst), relaxed counter, CAS success/failure, fence publication, forbidden non-atomic race, atomic/non-atomic mixing, plus the handoffs (mutex, channel, spawn/join). Each test has allowed and forbidden outcome sets P5-1 tools/litmus/, reference/concurrency-litmus.md, tests/litmus/ the enumerator's allowed set equals every stated set, disagreements diagnosed (spec 04 §20.5); owner review
P5-2 done Contract tables: atomic.new, atomic.take, atomic.load, atomic.store, RMW, atomic.cmpxchg, atomic.fence P5-1 spec 04 §42, spec 07a all spec 07 §4 fields present, with the ordering tables of spec 04 §24 (integrated with RFC 0015)
P5-2a done Publish feature atomics (depends on effects, ownership) P5-3 reference/air-features.md, src/air/features.zig feature_not_declared / missing_dependency tests
P5-3 done AIR parse/print, types (linear atomic<T>, AtomicScalar, not IslandSafe), ordering tables, E4202 P5-1a src/air/, src/verifier/ E4201 accept/reject for every operation and every cmpxchg success/failure pair; E4202; E1001, E2111, E0006 cases
P5-4 done atomic / atomic.weak effects (spec 05 §13); weak sites in aji report P5-3 verifier + report each non-seq_cst ordering without atomic.weak is E3101; seq_cst needs only atomic; weak sites listed
P5-5 done Interpreter: atomic cells with message histories, views, weak reads drawn from the schedule stream; atomic operations are scheduling points P5-3 src/interp/ sound on the corpus and the differential test; no extra draw without weak accesses; existing records unchanged
P5-6 done Happens-before tracking and dynamic race detector (validates the verifier) P5-5 src/interp/ any race the verifier missed aborts the test run with a diagnostic
P5-7 done Exhaustive bounded exploration of schedules and weak-read choices P5-5 src/interp/ for each litmus test, the outcome set observed equals the oracle's allowed set exactly
P5-8 dropped Record and replay: moved to Phase 8 (P8-10) by RFC 0015 D13 — — —
P5-9 done Litmus runner and differential test (random small programs; interpreter outcomes ⊆ oracle outcomes) P5-7 tests/litmus/, zig build litmus all corpus tests pass with exact equality; differential test in CI
P5-9a done .aji layer 10 (spec 15 §12), Quick Reference subsection after mutexes, source coverage rows, bundle measurement P5-4 src/frontend/, docs/agent/ .aji run/reject tests for every row; Quick Reference excerpts checked; growth recorded against RFC 0015's estimate
P5-9b done Reference program with atomics in AIR (written by an independent author from the spec) and .aji, with equivalent: P5-9a tests/conformance/ passes under all stress modes and under aji explore with no forbidden outcome
P5-10 done Phase exit review all plan status exit criterion passes

Regression gate (P5-0)

These criteria were fixed before the runs. M and C are a regression corpus only. They decide no semantic question, and the language is not tuned to them.

  • Baseline. The P6A-9 Aji/source cells (tag p6a-candidate): M 3/3, C 2/3. N3 on C wrote no program.

  • Hard gates. A failure of any of these blocks Phase 5 code:

    • R1: all four Aji references (M and C × Aji/AIR and Aji/source) still pass their frozen evaluators on p4-frozen;
    • R2: no FAIL is classified toolchain_bug = yes or evaluator_integration_bug = yes;
    • R3: the source cells show no IR-form code (E0001–E0006, E5001, E5002), no E7900 and no E1001.
  • Checked and explained, but not gates on their own:

    • R4: a diagnostic code not seen in the P6A-9 source cells (E7001, E7005, E7006, E7007);
    • R5: a cell that passed in P6A-9 and fails now;
    • R6: the source/AIR ratio of the agents' programs (mechanical_air_eliminated_raw, 0.82–0.88 in P6A-9).

    Each of these is classified: toolchain, evaluator, or model. Only a toolchain or language cause is a regression.

  • Reported only. One run per cell is noise for these: output tokens, uncached input, tool calls, repair cycles, documentation read (the Quick Reference grew by about 640 tokens with §7 Graphs).

Conformance tests to add

Negative corpus entry owned by this phase: invalid atomic ordering. Litmus: MP, SB, mutex handoff, channel handoff, spawn/join, invalid ordinary race.

All present: load_release.air, store_acquire.air, fence_relaxed.air, the two compare-exchange cases and invalid_order.aji (invalid ordering, E4201); the litmus corpus covers MP, SB, the handoffs and spawn/join; the invalid ordinary race is tasks_access_lent.air (E4101, Phase 3) plus the race detector's unit test.

Exit review

  • Exit criterion met.

    • The interpreter's exhaustive exploration (aji explore) of every test of the normative litmus corpus gives exactly the allowed set: 20/20, python3 tools/litmus/run_interp.py, in CI.
    • The atomic-publication tests pass in AIR (atomic_publication.air) and .aji (layer10/publication.aji) on 30 schedule seeds.
  • Decision records.

    • RFC 0015 was accepted (revision 2, with the owner's editorial corrections) and integrated into spec 02, 04, 05, 07, 07a, 09, 10, 11, 13 and 15.
    • Q-015 and Q-024 are resolved; Q-017 moved to Phase 8 with record/replay (P8-10); Q-025 is logged.
    • The feature atomics is published.
  • Gates before implementation, all met:

    • P5-0, the M/C regression on p4-frozen: PASS, with one latent frontend defect found and fixed;
    • the independent oracle: 21/21 self-tests;
    • the corpus: 20/20 against the oracle.
  • Invariants, each with tests:

    Invariant Evidence
    no atomic operation without an explicit, valid ordering orderings are part of the opcode; E4201 tests for load, store, fence, both compare-exchange tables, in AIR and .aji
    safe code never accesses atomic storage as ordinary storage E4202 (ordinary_load.air, ordinary_read.aji); a cell leaves only through atomic.take under exclusive ownership (E2101, E4101)
    DRF-SC for programs without ordinary races the race detector runs on every conformance run test (433, none reported) and catches an unverified racy program on every schedule (unit test); sc_only programs need only effects [atomic]
    the interpreter neither forbids allowed outcomes nor produces forbidden ones exact litmus equality (20/20); the differential test (1 650 random programs across seeds, in CI 150 per run) found no unsound outcome, and 98 % were exact
  • Vertical rule. Every construct went through spec → AIR → verifier → interpreter → .aji (layer 10) → Quick Reference (Atomics, excerpt checked by quickref/atomics.aji) → conformance in AIR and .aji → reference program. The reference program is atomic_stats in .aji, equivalent to hand-written AIR by an independent author. Source coverage: 67/67 constructs.

  • Conformance. 433 tests. Phase 5 adds:

    • 22 AIR tests (5 run, the reference program included, and 17 reject);
    • 18 .aji tests (4 run, 12 reject, the Quick Reference excerpt and the reference program).
  • Determinism.

    • Programs without non-seq_cst reads make no new choice, and every existing run record is unchanged (all earlier tests pass unmodified).
    • Weak choices come from the schedule stream.
    • Collect-every-step and --stress 200 pass.
  • Complexity budget (cl100k):

    Bundle Before (P4-11) After RFC 0015 estimate
    AIR bundle 39 154 42 275 (+8.0 %) +3–4k (≈ +9 %)
    QUICK.md 5 013 5 511 +500–700
    Source docs 26.7k 28.5k +2–3k

    The formal axioms (spec 04 §20) stay out of both bundles.

  • Known limitations:

    • aji explore is exhaustive only for small programs. With loops over weak reads the tree grows quickly, and --max-runs bounds it (the reference program: 200 000 runs, one outcome, incomplete).
    • The interpreter is complete only on the corpus, as the spec allows. Its soundness is checked (corpus and differential test), not proved; seq_cst accesses go through a global SC view.
    • The race detector does not check I/O buffers filled by intrinsics. Joining the SC view can hide a race (false negatives are possible); false positives have not been seen.
    • Atomics are excluded from aggregates, arrays, graphs and arc (linear_aggregates, U4).
    • There is no weak compare-exchange and no 128-bit cells.
    • The native-backend obligations (NO-THIN-AIR mappings, atomic_widths) are P7 work.
  • Not run in this phase: the M/C regression after Phase 5. The agent-facing language changed only by additions (layer 10, Quick Reference Atomics), and M/C use no atomics.

  • Next: an owner decision on the next phase (P7 native backend, P8 agent protocol, or the independent held-out benchmark first).