
         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         

         
{"id":"gen-1787747187-FWjQMfpYGfSLPeFCVMIv","object":"chat.completion","created":1787747187,"model":"minimax/minimax-m3:free","provider":"GMICloud","system_fingerprint":null,"service_tier":null,"choices":[{"index":0,"logprobs":null,"finish_reason":"stop","native_finish_reason":"stop","message":{"role":"assistant","content":"# Memory Ordering\n\n**Status:** Research document — proposal stage. No XH-1 implementation is fixed.\n**Scope:** Memory consistency model, ordering primitives, and their interaction with a 128-core RISC-V processor.\n**Audience:** XH-1 architecture team, verification engineers, OS porters.\n\n---\n\n## 1. Overview and Definitions\n\nMemory ordering defines the rules by which the memory accesses (loads and stores) issued by one or more cores become visible, in program order and globally, to other cores. It determines:\n\n- **Program order (PO):** the order in which a single hart issues memory accesses.\n- **Memory order (MO):** the global order in which accesses appear to execute as observed by other harts.\n- **Coherence order (CO):** the per-address total order of accesses required by the cache coherence protocol.\n- **Fence ordering:** explicit constraints inserted by hardware or software to restrict reordering.\n\nThese concepts are foundational to any multiprocessor memory model and are defined formally in the literature (Adve & Gharachorloo, 1996; Sorin, Hill & Wood, *A Primer on Memory Consistency and Cache Coherence*, 2011).\n\nXH-1 must adopt and document a precise model so that:\n\n1. Hardware designers know which reorderings are legal.\n2. The operating-system port (and any hypervisor) knows what atomicity guarantees it can rely on.\n3. Application and runtime software (compilers, language runtimes, synchronization libraries) can map higher-level primitives correctly.\n4. The verification team can construct litmus tests and formal models against an unambiguous specification.\n\n---\n\n## 2. RISC-V Architectural Requirements\n\n### 2.1 The RISC-V Weak Memory Model (RVWMM)\n\nRISC-V defines its memory model in:\n\n> *The RISC-V Instruction Set Manual, Volume I: Unprivileged Architecture*, Document Version 20191213 (and later), §Appendix A — \"RVWMM: RISC-V Weak Memory Model.\"\n\nKey facts established by that specification (status: **RISC-V specification requirement**):\n\n- RISC-V adopts a **release consistency** model (a variant of \"weak ordering\" with explicit acquire/release semantics), broadly modeled on the approach taken by Armv8.\n- Ordinary load and store instructions are divided by their **addressing mode** and **access type** into:\n  - `.aq` — acquire semantics (subsequent accesses in program order cannot be reordered before this access).\n  - `.rl` — release semantics (prior accesses in program order cannot be reordered after this access).\n  - The pair `aq+rl` yields sequential consistency (SC) per access when used on both sides of a synchronization pair.\n- Loads use acquire semantics (`LR`/`.aq` on AMOs and `LR.aq`); stores use release semantics (`.rl`).\n- `FENCE` instructions (including `FENCE.TSO`, `FENCE.I`, `FENCE.VMA`, `FENCE.GORC`, `FENCE.GW`, `FENCE.OW`, `FENCE.ORW`, `FENCE.RW`, `FENCE.RR`, `FENCE.WW`) provide explicit ordering at the hart front-end and the global memory system.\n- `FENCE.RW,RW` (the legacy full fence) orders all prior loads and stores against all subsequent loads and stores at the local hart and at the global system.\n- The model is **multi-copy atomic (MCA)** for devices that respect it, but **RISC-V explicitly does not require main memory to be multi-copy atomic** in general; I/O regions may opt into TSO or stronger ordering via `FENCE` and `aq/rl` annotations.\n\nThis is the model the XH-1 must conform to for RV64 compliance; deviations are not permitted for standard harts without explicit non-standard attributes.\n\n### 2.2 Atomic Memory Operations (AMOs)\n\nRISC-V provides:\n\n- **Load-Reserved / Store-Conditional (LR/SC):** `LR.W/D`, `SC.W/D`, optionally with `.aq`/`.rl`/`.aqrl`.\n- **AMO** instructions: `AMOSWAP`, `AMOADD`, `AMOAND`, `AMOOR`, `AMOXOR`, `AMOMAX[U]`, `AMOMIN[U]` on 32- and 64-bit values.\n\nAll AMOs implicitly perform a load and a store, and the addressing mode applies to the entire RMW operation.\n\n### 2.3 Reservation Sets and Forward Progress\n\nLR/SC semantics on RISC-V require that:\n\n- The reservation set be **at least as large as** the address and **contiguous in physical memory**, with forward-progress guarantees (no livelock).\n- The implementation may break a reservation for any reason, but must make progress under certain conditions (the exact rules are stated in the spec).\n\nThis matters for XH-1's L1 coherence protocol design — we must specify how reservation sets map onto cache lines and which events cause invalidation.\n\n### 2.4 I/O Ordering\n\nFor Memory-Mapped I/O (MMIO) and DMA, RISC-V provides `FENCE.I` for self-modifying code, and the I/O map can be marked as a **non-idempotent, strongly ordered** region via PMA (Physical Memory Attributes). XH-1 must define its PMAs and the interaction with the cache hierarchy carefully; this is treated in `research/05-memory/cache-coherence.md` and `research/06-io/`.\n\n---\n\n## 3. The 128-Core Scaling Problem\n\nXH-1 has 128 application cores. This dramatically amplifies the cost of any ordering primitive that requires global visibility.\n\n### 3.1 Why Fences Are Expensive\n\nA \"global\" fence (e.g., `FENCE.RW,RW` that orders against the entire memory system) must wait for:\n\n- All outstanding transactions from the issuing core to be **globally observed** (or at least ordered).\n- The interconnect to drain or acknowledge.\n\nAt 128 cores, global observation requires one of:\n\n- A **global counter** that is incremented on every coherence transaction and broadcast on the interconnect, or\n- A **token-based scheme** such as the Token Coherence (Marty et al., 2005) approach, or\n- **Hierarchical fences** that propagate down the directory tree.\n\nA naive \"drain the interconnect\" fence scales with O(N) (N cores) latency; in practice, on a 128-core mesh this may exceed 100s of ns — sufficient to dominate synchronization hot paths. *This is an estimate; actual latency depends on interconnect topology, link width, and clock frequency — currently unresolved for XH-1.*\n\n### 3.2 The Scalability of Atomics\n\nAMO and SC RMW operations require **directory invalidations** to all sharers and an **acknowledgment** before completion. At 128 cores:\n\n- Worst-case invalidation fanout is bounded by the number of cores (128), but typical working sets are much smaller.\n- **Fairness** and **starvation** become real concerns: a hot lock variable contested by 128 cores will generate enormous traffic. The hardware must not let one core perpetually lose a `SC` against another.\n- **NUMA effects**: if cores are organized in tiles/quads/clusters with distributed directories, an AMO on a remote line costs an extra hop in latency.\n\n### 3.3 False Sharing and Coherence Traffic\n\nA weak memory model encourages fine-grained synchronization, but a 128-core system makes **false sharing** (two cores writing to different bytes of the same line) catastrophically expensive — each write causes an invalidation round-trip. The memory model itself doesn't fix this, but it influences which data structures are used.\n\n### 3.4 Memory-Model Litmus Test Complexity\n\nFor verification, the number of distinct observable outcomes of small litmus tests grows combinatorially with core count. The team must be able to run RVWMM test suites (e.g., the official `riscv-tests` and `litmus` suites from Cambridge) and interpret results at 128 cores.\n\n---\n\n## 4. Implementation Approaches and Alternatives\n\nThis section discusses candidate mechanisms. **None of these are XH-1 commitments; all are proposals awaiting design review.**\n\n### 4.1 Fence Implementation Strategies\n\n| Strategy | Description | Pros | Cons |\n|----------|-------------|------|------|\n| **Naïve global fence** | Drain the issue queue, snoop outstanding transactions, wait for all acks. | Simple; obviously correct. | O(N) latency; does not scale to 128 cores. |\n| **FIFO completion tags (epoch counters)** | Each request gets a tag; fences compare local tag against a per-core \"completed\" tag. | Local fences are cheap; correctness well understood. | Global fences still need a global view. |\n| **Hierarchical directory-assisted fences** | Fences consult the directory tree to determine \"when is my request globally ordered?\" | Latency proportional to tree depth rather than N. | Requires directory tree to maintain completion info. |\n| **Token-based fences** | Borrow from Token Coherence (Marty et al., 2005) — fences consume tokens to know when stores are visible. | Distributed; no single global counter. | Token bookkeeping overhead; complex verification. |\n| **TSO hole** | Implement most of memory as TSO with weak operations for device access only (similar to x86). | Strong ordering for free for ordinary code; simpler programmer model. | Performance penalty for non-synchronization code; deviates from RVWMM. |\n\n**Note:** RISC-V explicitly disallows the TSO-only approach for general memory because the RVWMM is the architectural contract. Any TSO shortcut must be invisible to software.\n\n### 4.2 Store-Buffer and Load-Queue Design\n\nEach core needs:\n\n- **Store Buffer (SB):** holds pending stores until coherence acknowledgment.\n- **Load Queue (LQ):** may replay or forward in case of coherence invalidations.\n\nA common subtle correctness issue: a **store-to-load forwarding** race (a load that reads from a local store buffer entry that is later invalidated by a remote writer). The classic fix is the **MIPS R10000-style** load-queue replay: if a load matches a younger store in the SB, do not forward; if the load issues and a younger store then writes the same address, invalidate the load. A 128-core processor must also handle:\n\n- **Store forwarding across fences** — must respect `FENCE` placement.\n- **Acquired loads vs. earlier stores** — must not forward an acquired load to a store that lacks release semantics.\n- **Speculative load replay** — under invalidation, the LQ must replay; the replay must respect the original memory type (acquired vs. plain).\n\nThese are well-known issues from commercial designs (Hennessy & Patterson, *Computer Architecture: A Quantitative Approach*, recent editions; SPARC, Arm, x86 architecture manuals).\n\n### 4.3 Directory Organization\n\nAt 128 cores, a **flat broadcast directory** is impractical. The realistic choices are:\n\n- **Hierarchical directory** (cluster of N cores has a local directory; roots form a tree).\n- **Sparse directory** with limited pointers and a coarse bit-vector overflow (e.g., the *Directoryless* / *Incoherence* hybrid, but XH-1 will not be incoherent).\n- **Tagless directory** (Agarwal et al., *Adaptive Caches for Effective Interconnection and Memory Sharing*, 1991-era concepts).\n- **Cuckoo directory** (Zebchuk et al., 2009) for efficient coverage.\n\nXH-1's choice is **currently unresolved**; see `research/05-memory/cache-coherence.md` for a deeper treatment.\n\n### 4.4 Forward-Progress and Livelock Avoidance\n\nLR/SC on RISC-V requires a guarantee that some SC eventually succeeds. At 128 cores:\n\n- **Symmetric fairness** (round-robin arbitration of contested AMO targets) is straightforward but adds latency.\n- **Aging counters** (boost priority of cores that have failed SC) are fairer.\n- **Queue-based fairness** (FIFO ordering of AMO requests at the directory) is the cleanest in the literature.\n\nXH-1 should commit to one and document it precisely; the lack of a fairness spec is a frequent source of bugs in academic designs.\n\n### 4.5 I/O Fences and PMAs\n\nFor MMIO regions, XH-1 must define a **PMA** that:\n\n- Marks the region as **non-cacheable** or **device, non-idempotent** (strongly ordered).\n- Requires `FENCE` semantics that ensure completion before subsequent instructions.\n- Possibly exposes `FENCE.I` ordering for self-modifying code in cached regions.\n\nA reasonable proposal (pending review): device regions are marked as **Device, non-idempotent (nGnRnE in Arm terminology)**, requiring `FENCE` and `aq/rl` to be observed with full completion before the next instruction. This is consistent with RISC-V practice but **not yet decided for XH-1**.\n\n---\n\n## 5. Interaction with Other Subsystems\n\n### 5.1 Pipeline\n\n- **Issue width and queue depth** determine how many in-flight loads/stores can be reordered.\n- **Speculative load execution** must be invalidated on branch misprediction *and* on coherence invalidation. The LQ must distinguish these cases.\n- **Fences are pipeline barriers** — they prevent instructions behind the fence from issuing, even out-of-order. A `FENCE.RW,RW` is a full drain; a `FENCE.RW,RW` after a `FENCE.I` is worse. Compiler and hardware should minimize fence use.\n\n### 5.2 Cache Hierarchy\n\n- **Inclusion vs. exclusion** between L1, L2, L3 matters for fence invalidation: a fence does not need to flush the cache, but it does need to ensure stores are visible to the coherence point.\n- **Write-back vs. write-through** L1: write-back hides store latency but complicates fence completion.\n- **LR/SC reservation** must be invalidated on **any** event that could modify the reservation line, including internal writebacks, snoop hits, and external invalidations. The XH-1 spec for reservation invalidation events is **unresolved**.\n\n### 5.3 Memory System\n\n- **DRAM controllers** must respect ordering for writes to the same channel/bank. Modern controllers re-order aggressively; for fence correctness the controller must provide a **write-ordering fence** operation.\n- **Non-temporal stores** (if XH-1 supports them) must be specified carefully to interact correctly with `FENCE` and coherence.\n- **Persistent memory** (if targeted) requires explicit **drain and flush** semantics — e.g., `FENCE` followed by `SFENCE.VMA`-like operations.\n\n### 5.4 Interconnect\n\n- A 128-core system needs a **coherent interconnect** (e.g., a mesh, ring, or hierarchical bus). For ordering:\n\n  - **Ordered virtual channels** ensure that coherence messages from a given source maintain order — required for the protocol to be correct.\n  - **Completion acknowledgment** must be globally ordered to support `FENCE` and `aq/rl` — typically a separate response network or a tagged token system.\n\n- See `research/03-interconnect/` (placeholder).\n\n### 5.5 Coherence\n\n- Memory ordering is a **superset** of coherence: coherence gives per-address order; ordering extends this to multi-address relationships (e.g., a flag write must precede a data write).\n- The coherence protocol (MESI, MOESI, or similar) determines which transitions are **silent** (e.g., a clean shared → shared is silent) and which generate **write notices** to remote cores. Fences interact with these notifications.\n- XH-1's protocol choice is in `research/05-memory/cache-coherence.md` and is currently **proposed** as a directory-based MESI variant.\n\n### 5.6 Interrupts\n\n- An **interrupt** may inject a context switch at any instruction boundary. The OS expects that the interrupted hart's prior memory accesses are visible to the new context once the OS resumes another hart that observes the same state.\n- This requires that:\n  - Interrupt entry be treated as a **fence at the hart** (all prior loads/stores complete before the handler reads shared state).\n  - Interrupt exit (returning from handler) similarly observes a **release fence** before `sret`/`mret`.\n- The RISC-V spec does not mandate this explicitly, but it is the standard interpretation. **XH-1's contract with the OS is unresolved** and must be documented.\n\n### 5.7 Operating System\n\n- The OS port (likely Linux) requires:\n  - **I/O fences** around MMIO.\n  - **Acquire/release** semantics for `spin_lock`, RCU, seqlocks, etc.\n  - **TLB shootdown** ordering with respect to page-table updates (the famous \"shootdown TLB entry, then update PTE\" hazard).\n  - **SMP boot** synchronization using `hartip` (per RISC-V HSM extension).\n- Linux on RISC-V uses `.aq`/`.rl` extensively. **XH-1's conformance with these patterns is mandatory for Linux to boot**.\n\n### 5.8 Verification\n\n- Memory model verification is famously hard. The state of the art includes:\n  - **Litmus test execution** (e.g., the Cambridge `litmus` tool, herd7).\n  - **Model checking** with tools like **MemSAT**, **CBMC** with memory-model plugins.\n  - **Theorem proving** (Coq/Isabelle) for protocol proofs.\n  - **Stress testing** with random instruction streams.\n- At 128 cores, **scalability** of these tools is a concern — herd7 works on axiomatic models, not microarchitectures; we need a model that can express XH-1's specific microarchitecture (queue sizes, replay rules, fence completion).\n- **Coverage:** the team must run the official `riscv-tests` plus internally generated tests.\n- This document is deliberately conservative: the XH-1 microarchitecture is not yet specified, so formal verification of the model against the RTL is not currently scheduled.\n\n### 5.9 Performance\n\n- **Ordering cost** can dominate the runtime of synchronization-heavy workloads. Microbenchmarks to consider:\n  - Empty lock acquire/release round-trip.\n  - Producer-consumer queue latency.\n  - Atomic increment latency under contention.\n  - `FENCE` latency under no traffic vs. saturated interconnect.\n- Quantitative target (proposal, not requirement): empty lock round-trip should be **< 100 ns** on the XH-1 interconnect. *This is not yet validated against a specific frequency or topology.*\n- A 128-core chip that pays a 500 ns fence on every lock release would be uncompetitive on database, OS kernel, and runtime benchmarks. **The fence implementation strategy (§4.1) is therefore a first-order performance decision.**\n\n---\n\n## 6. Advantages and Disadvantages of a Pure RVWMM Approach\n\n### 6.1 Advantages\n\n- **Standards compliance:** software written for any RISC-V hart runs correctly.\n- **Compiler freedom:** the compiler can reorder aggressively for sequential regions.\n- **Hardware freedom:** out-of-order and speculation are unconstrained except where fences demand otherwise.\n- **Industry alignment:** Arm's success with a release-consistency model demonstrates viability.\n\n### 6.2 Disadvantages\n\n- **Programmer burden:** subtle bugs arise from missing fences (e.g., the famous \"Dekker's\" patterns).\n- **Performance cliffs:** adding one missing fence may inflate latency by orders of magnitude.\n- **Verification cost:** weak models have more behaviors to test.\n- **OS porting:** subtle bugs in porting Linux locking primitives have historically caused real defects (e.g., the itanium `mf` model, various Arm power-management races).\n\n### 6.3 Alternatives Considered\n\n- **TSO-everywhere (x86-style):** better programmer intuition, lower fence overhead for common cases, but contradicts RVWMM and is performance-suboptimal for non-synchronization code. **Rejected** for a fresh RISC-V design.\n- **Sequential consistency (SC):** simplest model, but **incompatible with RISC-V**, which explicitly defines a weaker model.\n- **C++ memory model directly in hardware:** not realistic; the hardware must support whatever language the compiler emits.\n\n---\n\n## 7. Identified Design Questions for XH-1\n\nThe following are **unresolved** and require design review before commitment:\n\n1. **Fence completion mechanism:** epoch counters vs. directory-assisted vs. token-based?\n2. **LR/SC reservation invalidation events:** which coherence events break a reservation?\n3. **Forward-progress fairness policy:** round-robin, aging, or queue-based?\n4. **PMA definition for I/O regions:** which regions are device, cacheable, idempotent?\n5. **Interrupt-entry fence semantics:** what guarantee does the OS get?\n6. **Speculative load replay rules:** under what conditions is a load replayed, and how does it interact with `aq/rl`?\n7. **Store-buffer forwarding rules:** what is the legal forwarding window?\n8. **Verification tool selection and test plan:** what coverage is required for tape-out?\n9. **Performance budgets:** what is the acceptable empty-fence latency?\n10. **Interaction with HSM (Hart State Management):** the SBI HSM extension has its own ordering requirements for hart start/stop.\n\n---\n\n## 8. Recommendations (Pending Evidence)\n\nThe following are conditional recommendations, contingent on design review and simulation:\n\n- **Adopt the RISC-V RVWMM verbatim** for the architectural contract. There is no compelling reason to invent a custom model.\n- **Implement fences with a hierarchical directory-assisted scheme** to maintain reasonable latency at 128 cores; validate via cycle-accurate simulation once the interconnect is defined.\n- **Specify LR/SC reservation invalidation events** as a precise list in a follow-up document, including: snoop invalidation, snoop downgrade, internal writeback to memory, software `SC` failure injection.\n- **Adopt a queue-based fairness policy for AMO arbitration** to bound worst-case starvation under high contention.\n- **Mark I/O regions as Device, non-idempotent (nGnRnE-equivalent)** and require `FENCE` for completion. *Pending review.*\n- **Commit to a verification plan** that includes the official `riscv-tests` RVWMM suite, internally generated litmus tests, and a formal axiomatic model expressed in herd7's `cat` language.\n- **Document the interrupt-entry fence semantics** explicitly in the privileged ISA supplement.\n\nThese are proposals. None are committed XH-1 features until design review.\n\n---\n\n## 9. References and Sources\n\n- RISC-V International, *The RISC-V Instruction Set Manual, Volume I: Unprivileged Architecture*, Document Version 20191213, Appendix A: \"RVWMM: RISC-V Weak Memory Model.\" [Specification; no URL fabricated — see riscv.org/technical/specifications/]\n- RISC-V International, *Volume II: Privileged Architecture*, HSM Extension.\n- Adve, S. V., & Gharachorloo, K. (1996). \"Shared Memory Consistency Models: A Tutorial.\" *IEEE Computer*.\n- Sorin, D. J., Hill, M. D., & Wood, D. A. (2011). *A Primer on Memory Consistency and Cache Coherence.* Morgan & Claypool. (Synthesis lectures in computer architecture.)\n- Hennessy, J. L., & Patterson, D. A. *Computer Architecture: A Quantitative Approach.* 5th/6th editions. (A definitive reference; specific page numbers not cited to avoid fabrication.)\n- Marty, M. R., et al. (2005). \"Token Coherence: Decoupling Performance and Correctness.\" *ISCA 2005.*\n- Zebchuk, J., et al. (2009). \"Cuckoo Directory: A Scalable Directory for Many-Core Systems.\" *HPCA 2009.*\n- Agarwal, A., et al. (1991). \"Adaptive Caches for Effective Interconnection and Memory Sharing.\" *ISCA 1991.*\n- Arm Architecture Reference Manual, Armv8-A, §B2.3 \"Memory ordering.\" (For comparison.)\n- SPARC Architecture Manual, §8 \"Memory Model.\" (Historical reference for TSO/RMO.)\n- The herd7 / diy7 tool suite, Cambridge. (Used for axiomatic memory-model analysis of RISC-V.)\n- `riscv-tests` repository. (Official test suite; see github.com/riscv-software-src/riscv-tests.)\n\nWhere URLs are not provided above, the reader should consult the official source rather than rely on a possibly stale or fabricated link.\n\n---\n\n## 10. Document Status\n\nThis is a **research document**, not a specification. It does not commit XH-1 to any specific implementation. It identifies the RISC-V architectural contract (RVWMM) that XH-1 must respect and surveys the implementation alternatives and tradeoffs. The unresolved questions in §7 must be addressed in follow-up design documents before any RTL is committed.","refusal":null,"reasoning":null}}],"usage":{"prompt_tokens":663,"completion_tokens":5333,"total_tokens":5996,"cost":0,"is_byok":false,"prompt_tokens_details":{"cached_tokens":135,"cache_write_tokens":0,"audio_tokens":0,"video_tokens":0},"cost_details":{"upstream_inference_cost":0,"upstream_inference_prompt_cost":0,"upstream_inference_completions_cost":0},"completion_tokens_details":{"reasoning_tokens":0,"image_tokens":0,"audio_tokens":0}}}