Section 4.39

Microarchitecture

Definition

Microarchitecture is the internal design of a processor: the hardware organization that carries out the instructions of an instruction set architecture §4.38 (ISA), the specification of the instructions, registers and memory that programs see. It includes the pipeline, which works on several instructions at once; caches, small fast memories that keep copies of recently used data; branch predictors, which guess where execution goes next; and the execution units and buses. The ISA fixes what each instruction computes. The microarchitecture decides how it is done and how many clock cycles it takes.

The idea

The Cortex-M4 §6.3 and the Cortex-M7 are processor cores designed by Arm. Both follow the same ISA, the Armv7E-M architecture, so a program that uses only its integer instructions runs on either core with the same results. The Cortex-M4 has a three-stage pipeline and no cache inside the core. The Cortex-M7 has a six-stage pipeline that can start two instructions in one cycle, a branch predictor and optional caches. The same loop therefore takes different numbers of cycles on the two cores, and its cycle count can differ even between two runs on one core.

The reason is that a processor holds two layers of state. The architectural state is what the ISA defines and programs can read: the registers, the condition flags §4.42, the contents of memory and the address of the next instruction. The microarchitectural state is everything else inside the processor: which blocks of memory the caches hold, what the branch predictor has learned, which instructions are partway through the pipeline. Each instruction changes the architectural state exactly as the ISA says, whatever the microarchitectural state is, and the microarchitectural state decides how many cycles the instruction takes. A program’s results depend on the first layer only; its running time depends on both.

In a picture, the ISA is a recipe and the microarchitecture a kitchen. Two kitchens that follow one recipe make the same dish at different speeds, and a kitchen with the ingredients already on the counter is faster. The picture fails in one respect: what a processor leaves on the counter can be detected by another program that times itself, which is the basis of cache side channels §1.29.

How it works

Each mechanism below changes the cycle count.

So the same code has different costs on different microarchitectures, and on one core its cost changes with the starting state (cold or warm caches, a trained or untrained predictor), with the data (operand values, which branches are taken) and with interference from outside the code (wait states, DMA transfers, interrupts).

Worked example

The numbers below belong to a hypothetical processor, chosen to keep the arithmetic simple. Its clock runs at 100 MHz (megahertz, million cycles per second), so one cycle lasts 1/(100×106)1 / (100 \times 10^{6}) seconds, which is 10 ns (nanoseconds). A load costs 2 cycles on a cache hit and 30 cycles on a miss, and cache lines are 32 bytes long. A loop adds up 64 integers of 4 bytes each, 256 bytes that start at an address divisible by 32. Each pass through the loop performs one load plus 3 cycles of other work: the addition, the index update and the branch back.

First run, cold cache (no part of the array §14.5 is cached). The array covers 256/32=8256 / 32 = 8 cache lines of 32/4=832 / 4 = 8 integers each. The first load in each line misses and brings the whole line in, and the next 7 loads in that line hit: 8 misses and 64−8=5664 - 8 = 56 hits. The loads cost 8×30+56×2=240+112=3528 \times 30 + 56 \times 2 = 240 + 112 = 352 cycles and the other work 64×3=19264 \times 3 = 192 cycles, a total of 352+192=544352 + 192 = 544 cycles, or 544×10=5440544 \times 10 = 5440 ns.

Second run, warm cache (the array is still cached from the first run). All 64 loads hit: 64×2+192=32064 \times 2 + 192 = 320 cycles, or 3200 ns. The instructions and the computed sum are the same; only the starting microarchitectural state differs, and the first run took 544/320=1.7544 / 320 = 1.7 times as long.

Third run, warm cache and one interrupt. Partway through, an interrupt handler §6.12 runs for 200 cycles, and its data evicts (pushes out of the cache) 2 array lines that the loop has not reached yet. Those two loads now miss, costing 2×(30−2)=562 \times (30 - 2) = 56 extra cycles. The elapsed time is 320+200+56=576320 + 200 + 56 = 576 cycles, before counting the cycles spent entering and leaving the handler, although the loop’s own instructions did nothing different.

Real hardware shows the effect from data alone. On a Cortex-M4, 1000 divisions take between 1000×2=20001000 \times 2 = 2000 and 1000×12=12,0001000 \times 12 = 12{,}000 cycles, depending only on the values divided.

Stated precisely

Write AA for the architectural state and μ\mu for the microarchitectural state, as defined above. Executing one instruction takes the processor from (A,μ)(A, \mu) to (A′,μ′)(A', \mu') and lasts cc cycles. The ISA contract says that A′=f(A)A' = f(A) for a function ff that the ISA defines for that instruction: the new architectural state depends on the old architectural state alone. The microarchitecture determines μ′\mu' and cc, and these may depend on AA, on μ\mu and on events outside the processor. For a run of nn instructions that starts in (A0,μ0)(A_0, \mu_0), let fif_i and cic_i belong to the ii-th instruction executed. The final architectural state is An=fn(fn−1(…f1(A0)))A_n = f_n(f_{n-1}(\dots f_1(A_0))), and the running time is T=c1+c2+…+cn cycles.T = c_1 + c_2 + \dots + c_n \text{ cycles.} Two runs that start from the same A0A_0 therefore execute the same instructions and end in the same AnA_n. They may differ in TT whenever μ0\mu_0 or the outside events differ.

The contract rests on three assumptions: the run reads no outside input that changes independently of the program (peripheral registers, memory written by a DMA transfer, timer values); no interrupt handler changes the registers or memory that the run uses; and every instruction executed has a result that the ISA defines exactly. Where an ISA leaves a result open, two microarchitectures may legally give different results.

A statement about time is complete only if it names the core, its clock frequency and memory configuration (wait states, caches on or off), the microarchitectural state at the start (cold or warm), and the interference that may occur (interrupts, DMA). A measurement gives one value of TT for one starting state. An upper bound on TT over every admitted starting state and interference is the subject of worst-case execution time §9.33 (WCET) analysis.

Limits and common mistakes

The same ISA does not mean the same timing. A delay loop tuned on one chip waits a different time on another core, at another clock frequency, or with the cache switched on.

A measured cycle count is one sample from one starting state, not an upper bound: a later run with a cold cache, a mispredicted branch or an interrupt can take longer. Timing code after a run that warmed the caches measures the warm case only, which the Warmup and steady state §11.27 entry discusses.

Cycle tables in a core’s technical reference manual assume ideal memory. A real chip adds flash wait states and bus delays that such tables do not include.

Microarchitectural state does not show in a program’s results, but it does show in its timing. When a secret decides which memory addresses a program touches, which branches it takes, or the operands of a variable-latency instruction, the running time can reveal the secret: a timing side channel. Cache-timing attacks have recovered cryptographic keys this way, and the Spectre attacks, published in 2018, make a processor speculatively touch secret-dependent addresses on a path that is later discarded, then read the secret back from cache timing [252]. Constant-time programming §5.36 avoids secret-dependent branches, addresses and variable-latency instructions.

Microarchitecture is not only about speed. Where the ISA leaves a result open, or where a processor has a design error (published by the vendor as an erratum), the microarchitecture decides the architectural result as well.

Questions answered

Why can the same instruction sequence take different numbers of cycles?

The same instruction sequence can take different numbers of cycles because the instruction set architecture (ISA) defines only what each instruction computes, not how long it takes. The time is set by the microarchitecture, the internal design that carries the instructions out, and by the state that design is in. Two things vary. First, different processor cores that implement the same ISA have different pipelines, caches and branch predictors. Second, on one core each instruction’s cost depends on hidden state, on the data and on outside events: whether the data is already in the cache (a hit costs a few cycles, a miss many more), whether the branch predictor guessed right, the operand values of variable-latency instructions such as division (2 to 12 cycles on the Cortex-M4), memory wait states, bus contention with DMA (direct memory access) transfers, and interrupts. The results are identical in every case; only the time differs.

A cycle count therefore means something only together with the core, its clock and memory configuration, its starting state (cold or warm cache) and the interference allowed. A single measurement gives one value, not a bound.

Related entries: Instruction set architecture (ISA) (§4.38), Side channel (§1.29), Counter quantization (§11.35), Worst-case execution time (WCET) (§9.33), Warmup and steady state (§11.27), Arm Cortex-M4 (§6.3). Further reading: [5, 252].