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.
Pipeline. The processor splits each instruction into steps, such as fetch (read it from memory), decode (work out what it asks for) and execute, and works on several instructions at once, each at a different step, so that ideally one instruction finishes in every cycle. A taken branch, a jump to an instruction other than the next one, discards the instructions already fetched behind it, and the pipeline needs cycles to refill: on the Cortex-M4 a taken branch costs one cycle plus 1 to 3 refill cycles. An instruction that needs a result an earlier instruction has not yet produced must wait; such a wait is called a stall.
Caches. A cache holds copies of blocks of main memory, called cache lines, typically 32 or 64 bytes long. A load whose data is in the cache (a hit) is fast. A load whose data is not (a miss) waits while the whole line is fetched from slower memory. Whether a load hits depends on what earlier code touched.
Branch prediction. A branch predictor guesses, before a conditional branch executes, whether it will be taken and where it leads, so that fetching can continue. A wrong guess throws away the work done on the wrong path. The predictor learns from past branches, so the cost of a branch depends on history.
Variable-latency instructions. Some instructions take a number of cycles that depends on their operand values. On the Cortex-M4 the divide instructions
UDIVandSDIV(unsigned and signed divide) take 2 to 12 cycles, because they stop early when the operand values allow it.Memory outside the core. Flash memory §6.6 on a microcontroller §6.1 often cannot deliver an instruction in every cycle at full clock speed, so the chip inserts wait states, idle cycles in which the processor waits for the memory. A DMA §4.36 (direct memory access) controller, a circuit that copies data without the processor, can occupy the bus at the same moment and delay the processor.
Interrupts §6.11. An interrupt is a hardware signal that makes the processor stop, run a handler routine and then resume. The handler’s cycles add to the elapsed time of the interrupted code, and the handler can change the caches and the predictor.
Speculative and out-of-order execution. Large processors run instructions before they know the instructions are needed, and in a different order from the program, and discard the results of wrong guesses. The architectural results are exactly what the ISA defines, but the discarded work can leave traces in the caches.
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 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 cache lines of 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 hits. The loads cost cycles and the other work cycles, a total of cycles, or ns.
Second run, warm cache (the array is still cached from the first run). All 64 loads hit: cycles, or 3200 ns. The instructions and the computed sum are the same; only the starting microarchitectural state differs, and the first run took 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 extra cycles. The elapsed time is 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 and cycles, depending only on the values divided.
Stated precisely
Write for the architectural state and for the microarchitectural state, as defined above. Executing one instruction takes the processor from to and lasts cycles. The ISA contract says that for a function that the ISA defines for that instruction: the new architectural state depends on the old architectural state alone. The microarchitecture determines and , and these may depend on , on and on events outside the processor. For a run of instructions that starts in , let and belong to the -th instruction executed. The final architectural state is , and the running time is Two runs that start from the same therefore execute the same instructions and end in the same . They may differ in whenever 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 for one starting state. An upper bound on 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].