Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

7.4 Dependencies

A dependency is the cheapest ordering on Arm: no barrier, no acquire, just a register that carries a loaded value into a later instruction. The architecture honours dependencies syntactically. A dependency exists because of which registers an instruction reads, not because of whether the value actually influences the result. That makes dependencies a precise tool in assembly and a fragile one in C. This section defines the three kinds, says exactly what each one orders, and lists the cases where something that looks like a dependency orders nothing.

Definitions

DDI0487 B2 builds everything from a register data dependency: a chain from the destination register of a read effect, through any number of register-to-register instructions, to a source register of a later instruction in program order. The chain is broken where an instruction overwrites the carrying register with a value that does not depend on it. Instructions that read and write other registers do not affect it.

KindThe chain ends in…Example
Address dependencythe address of a later memory accessLDR x1, [x0] → LDR x2, [x1, #8]
Data dependencythe data register of a later storeLDR x1, [x0] → STR x1, [x3]
Control dependencythe condition of a conditional branch (B.cond via NZCV, CBZ/CBNZ, TBZ/TBNZ), with the dependent effect after the branch in program orderLDR w1, [x0] → CBZ w1, 1f → STR ...

A dependency is a syntactic property. This sequence has a real address dependency, although the offset is always zero:

    ldr     x1, [x_flag]
    eor     x2, x1, x1          // x2 = 0, but depends on x1
    ldr     x3, [x_data, x2]    // address-dependent on the flag load

The hardware must not satisfy the second load until the first one has returned its value. A design that uses value prediction still has to behave as though it waited. This “fake dependency” idiom is legal and common in hand-written code. Linux uses the same eor trick after an MRS of the counter, but there the source is a system-register read rather than a memory read, which is a different case (item 4 below and 4.7).

What each dependency orders

From the Dependency-ordered-before (dob) relation (7.3):

Source → targetOrdered?dob clause
Read → address-dependent read or writeYesaddr
Read → data-dependent writeYesdata
Read → any write after an address-dependent accessYesaddr; po; [W]
Read → write after a control-dependent branchYesctrl; [W]
Read → read after a control-dependent branchNo(none)
Read → read after a control dependency and an ISBYesctrl; [ISB]; po; [R]
Read → read after an address-dependent access and an ISBYesaddr; po; [ISB]; po; [R]
Read → later read of a store it fed (forwarding)Yes(addr | data); rfi

Why the asymmetry: stores are never speculatively visible

The rule behind the table is that no write may become visible to another observer while it is still speculative. A store after a branch that depends on a load cannot be released until the branch resolves, and the branch cannot resolve until the load has its value. The same applies to a store after an address-dependent access: until the address is known, the PE cannot rule out that the earlier access faults, so the later store stays speculative.

Loads have no such restriction. A PE may predict the branch, execute the load on the predicted path, and keep the value if the prediction was right. Architecturally that is indistinguishable from a load performed early. So ctrl gives no R→R order. In MP, a control-dependent read of the payload can return stale data (7.3).

Control + ISB

An ISB after the branch forbids that. Instructions after an ISB are fetched and executed only after the ISB completes, and the ISB cannot complete while it is itself on an unresolved speculative path. This works, but it is a pipeline flush on every pass. Prefer an address dependency, DMB ISHLD, or LDAR/LDAPR on the flag (7.7). The ctrl+ISB form matters mostly when the “read” you need ordered is not an ordinary load, for example an MRS of a counter or a system register (7.8).

Dependencies that do not create ordering

  1. Control to a later load. This is the case above, and the most common mistake.

  2. Dependencies that the compiler removes. C has no notion of a hardware dependency. Given r = *flag; x = data[r - r];, a compiler will fold r - r to zero and drop the dependency. Given if (p == &known) use(known.field), it may substitute the address it already knows. C11 memory_order_consume was meant to preserve such chains, but in practice compilers implement it as acquire. Firmware that relies on a dependency must create it where the compiler cannot see through it: in assembly, in inline asm with the dependent load inside, or by using an acquire load. Linux’s READ_ONCE plus address dependency relies on compiler behaviour and documented restrictions that bare-metal toolchains do not promise.

  3. Dependencies through the condition of a conditional select. CSEL x3, x4, x5, EQ, where the flags come from a loaded value, is not an ordinary register data dependency in the current model. Recent DDI0487 issues and herdtools7 aarch64.cat describe it with separate “pick” dependency relations. These still order later writes (pick-addr-dep; [W], pick-ctrl-dep; [W]), and an ISB can be added for reads. They do not order a later load whose address is chosen by the select. Branchless code (CSEL instead of CBZ) can therefore weaken ordering that the branching version had for stores: ctrl; [W] orders every later store after the branch, but a pick dependency orders only stores that consume the selected value. Neither version orders a later load without an ISB. A dependency through the selected data operand of CSEL is an ordinary data dependency.

  4. Reads that are not memory reads. The model covers memory effects. An MRS of CNTVCT_EL0 or of a system register is not a read effect in dob, so a register chain from it to a load orders nothing in model terms. 4.7 and 7.8 cover what is guaranteed.

  5. The wrong direction. A dependency orders its source read before the target. It does nothing for earlier accesses. In SB, an address dependency from P0’s load to some later access does not stop P0’s earlier store from being overtaken by that load (7.3). A dependency also orders nothing for accesses by other observers on its own. The cross-PE guarantee comes from combining it with obs edges.

  6. Implicit accesses. Dependencies order explicit accesses. They do not make your PE’s translation table walker or instruction fetch see a store (7.1).

One case that looks broken is in fact fine: dependencies through memory on the same PE. If a compiler spills the loaded value to the stack and reloads it, the chain is R1 -data-> Wstack -rfi-> Rstack -addr-> R2. (addr | data); rfi puts R1 before Rstack, so the order survives the spill.

Where dependencies earn their place in firmware

  • Pointer publication, consumer side. If the producer writes a structure and then publishes a pointer to it with STLR or after DMB ISHST, the consumer needs only an address dependency, LDR x1, [x_ptr]; LDR x2, [x1, #off], written in assembly. This is the cheapest correct consumer.
  • Descriptor rings. A dependency from the ring index you read to the descriptor address you compute orders the descriptor read, if the compiler keeps it.
  • LB-style safety. Any “load, decide, store” sequence is already ordered R→W by its control dependency. No barrier is needed for that direction.

Security note: The fact that ctrl gives no R→R ordering in the memory model is closely related to Spectre v1 (14.3): loads after an unresolved bounds check execute speculatively. In the memory model the speculative load is harmless because its value is discarded on misprediction. Its cache footprint is not. The memory model tells you about values, not about microarchitectural side effects. CSDB is the speculation barrier for that purpose, and it is not a memory-ordering barrier.

Validation note: MP+dmb.st+ctrl is allowed, and an aggressive out-of-order core should show it under litmus7 stress. If it never appears, the test is simply not stressing hard enough. MP+dmb.st+addr and MP+dmb.st+ctrlisb are forbidden, so any observation of those is a bug. Check the generated assembly before blaming silicon: a C-generated litmus harness that lost the eor dependency is a test bug (7.11). On cores with value prediction, the address-dependency variants are the ones to run longest.

Checkpoint

Q7.4.1 Which reader sequence is sufficient, without any barrier instruction, to forbid flag=1, data=old in MP when the writer uses DMB ISHST?

  • A. LDR w0,[flag]; CBZ w0, retry; LDR w1,[data]
  • B. LDR w0,[flag]; CMP w0,#0; CSEL x2, x_data, x_dummy, NE; LDR w1,[x2]
  • C. LDR w0,[flag]; MOV x2, x_data; LDR w1,[x2]
  • D. LDR w0,[flag]; AND x2, x0, xzr; LDR w1,[x_data, x2]
Answer

D. AND x2, x0, xzr reads x0, so it creates a syntactic address dependency even though the result is always zero. A is control only, which does not order R→R. B makes the address depend on the condition flags of a CSEL, a “pick” dependency that does not order a later load. C has no dependency on w0 at all.

Read more: Arm ARM (DDI0487), B2 “Dependency definitions” (Address dependency, Register data dependency)

Q7.4.2 Why does a control dependency order a later store but not a later load?

  • A. Stores may not become visible while speculative, but a load may be satisfied speculatively and its value kept if the branch prediction was correct
  • B. Branch predictors are disabled for branches whose condition comes from a load
  • C. Stores always drain in program order through the store buffer
  • D. The architecture forbids any load from executing before an older branch resolves
Answer

A. This is the reason for the asymmetry in dob. Option C is false: stores to different locations need DMB ST to be ordered. Option D is false, and it is exactly what Spectre exploits.

Read more: Arm ARM (DDI0487), B2 “Dependency-ordered-before”; speculation rules for writes

Q7.4.3 A C consumer is written p = *(volatile uintptr_t *)&head; v = ((struct d *)p)->val; and relies on the address dependency for ordering. Under which change is this most likely to stop being correct?

  • A. Compiling for an Armv9 core instead of Armv8
  • B. Moving head and the structure into the same cache line
  • C. A compiler optimisation that proves p equals a known address and loads val from that address directly
  • D. Running the code at EL3 instead of EL1
Answer

C. The hardware honours syntactic dependencies, but the compiler can replace a loaded pointer with an equal constant, and then no dependency remains. Use assembly or an acquire load if the ordering must be guaranteed. Cache-line placement (B) and Exception level (D) do not affect the model.

Read more: Arm ARM (DDI0487), B2 “Dependency definitions”; C11 memory_order_consume (ISO/IEC 9899:2011, 7.17.3)