Files
aji/docs/plan/phase-5-atomics-and-memory-model.md
T

90 lines
7.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Phase 5 — Atomics and memory model
Non-normative. Part of the [implementation plan](README.md).
**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:** the litmus suite (`reference/concurrency-litmus.md`) and atomic-publication conformance tests pass, including allowed weak outcomes.
**Spec inputs:** spec 04 §20–32, 11; `reference/concurrency-litmus.md`. RFC 0003 is proposed for acceptance (owner decision); acceptance integrates it into the spec.
**Blocking questions:** Q-015 operational weak-memory model, Q-017 replay format.
**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).
```text
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 `iref`s 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 | todo | 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/` | see "Regression gate" below |
| P5-1 | todo | 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 | todo | Normative litmus 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, and an independent enumerator of the RFC's axiomatic model reproduces every set | P5-1 | `reference/concurrency-litmus.md`, `tests/litmus/` | the enumerator agrees with every stated set; owner review |
| P5-2 | todo | Contract tables: `atomic.load`, `atomic.store`, `atomic.rmw`, `atomic.cmpxchg`, `atomic.fence` | P5-1 | spec 07 appendix | all fields present, including allowed orderings per op |
| P5-2a | todo | Publish feature `atomics` | P5-2 | `reference/air-features.md`, `src/air/features.zig` | feature_not_declared / missing_dependency tests |
| P5-3 | todo | Atomic types and ordering validation | P5-2 | `src/verifier/atomics.zig` | E4201 accept/reject for every op, including cmpxchg failure order |
| P5-4 | todo | `atomic` / `atomic.weak` effect marking | P5-3 | verifier + report | relaxed use without `atomic.weak` rejected per Q-011 outcome; weak sites listed in report |
| P5-5 | todo | Weak-memory model in the interpreter | P5-1 | `src/interp/weak.zig` | SB weak outcome reachable; MP forbidden outcome unreachable |
| P5-6 | todo | 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 | todo | Systematic schedule exploration with bounded budget | P5-5 | `src/interp/sched.zig` | `aji test --interleavings=N` explores distinct schedules deterministically |
| P5-8 | todo | Record and replay | P5-1 | `src/interp/replay.zig` | `aji run --record` / `aji replay` reproduce a failing concurrent run byte-for-byte |
| P5-9 | todo | Litmus suite runner | P5-5 | `tests/litmus/`, `zig build litmus` | each litmus test states allowed and forbidden outcomes; all pass |
| P5-10 | todo | 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.