CS 152 Computer Architecture and Engineering
CS252A Graduate Computer Architecture Lecture 25 - Memory Consistency

Chris Fletcher
UC Berkeley, Spring 2026

Two Kinds of Synchronization

With plain loads and stores (initially flag = 0):

Producer:                    Consumer:
  sw xdata, (xdatap)           spin: lw   xflag, (xflagp)
  li xflag, 1                        beqz xflag, spin
  sw xflag, (xflagp)                 lw   xdata, (xdatap)

Correct? It depends on the memory consistency model.

Coherence vs. Consistency

CoherenceConsistency
ScopeA single addressAll addresses
QuestionWhat values can reads of flag return?Which orderings of flag and data can threads observe: which final states are legal?

Sequential Consistency (SC)

Lamport: "the result of any execution is the same as if the operations of all the processors were executed in some sequential order, and the operations of each individual processor appear in the order specified by the program."

Why Real Machines Aren't SC

Litmus Test: SC vs. TSO

P1:            P2:
  sw 1, X        sw 1, Y
  lw x2, Y       lw x2, X

Initially X = Y = 0

(P1.x2, P2.x2)SCTSO
(0, 1)✅✅
(1, 0)✅✅
(1, 1)✅✅
(0, 0)❌✅

Check Your Understanding

1. State the difference between coherence and a consistency model.
Coherence: legal values for reads and writes to a single address. Consistency: ordering and visibility rules across all addresses and threads.
2. Why can the litmus test give (0, 0) under TSO but not SC?
Under TSO each store waits in its local buffer while the later load to a different address reads memory early, so both loads can see 0 before either store drains.

Weak Memory Models and Fences

Producer:                    Consumer:
  sw    xdata, (xdatap)        spin: lw    xflag, (xflagp)
  li    xflag, 1                     beqz  xflag, spin
  fence w, w                         fence r, r
  sw    xflag, (xflagp)              lw    xdata, (xdatap)

Ordering Rules

$X \to Y$: X must be globally visible before Y

ModelR→RR→WW→RW→W
SC✅✅✅✅
TSO✅✅❌✅
PSO✅✅❌❌
WO / Alpha / RMO / PowerPC❌❌❌❌

Release Consistency (RC)

Compilers Reorder Too

// Producer             // Consumer
*datap = x / y;         while (!*flagp) ;
*flagp = 1;             d = *datap;

Multi-Copy Atomicity

Multi-copy atomicNon-multi-copy atomic
ExamplesSC, TSO, revised ARMv8, RISC-V RVWMOARMv7, original ARMv8, IBM POWER, Alpha
RuleA thread sees its own writes early, but once a store is visible to one other thread, it's visible to allShared cluster buffers let a neighbor see a write before distant cores do
CPU 1CPU 2CPU 3CPU 4 intermediate buffer Aintermediate buffer B shared memory CPU 2 can read CPU 1's write from buffer A before CPUs 3, 4 see it

Check Your Understanding

1. Which ordering does TSO relax vs. SC, and why does un-fenced producer-consumer still work on TSO?
Only W→R. The producer needs W→W and the consumer needs R→R, both of which TSO keeps.
2. What distinguishes multi-copy atomic from non-multi-copy atomic models?
Multi-copy atomic: once a store is visible to any other thread it's visible to all. Non-multi-copy atomic: one remote core can see a store before others (e.g., via shared cluster buffers).