S4 · Lesson 8 · The front end
Fetch bandwidth, taken branches, instruction caches, decoders and micro-op caches
An out-of-order core can only execute what its front end delivers. Every cycle the front end must predict where the program goes, fetch the right bytes from the instruction cache, find where instructions start, decode them and rename them, at the full width of the machine. Taken branches cut fetch blocks short, instruction-cache misses leave the pipeline empty, and variable-length x86 instructions are expensive to decode. Front-end stalls are a large share of the CPI of big server programs, and much of a modern core's design goes into keeping the front end ahead.
- The curve is the average fetch rate of a front end of width w when a taken branch ends a fetch block every t instructions and each redirection costs b bubble cycles: .
- With no bubbles, short runs between taken branches still waste slots: a 4-wide fetch delivers only 3 per cycle when blocks are 6 long.
- A bubble per taken branch hurts most when blocks are short, which is why the BTB must redirect fetch in the very next cycle.
Predict
A 4-wide front end fetches up to the first taken branch each cycle, with no extra bubbles. If a taken branch comes every 6 instructions, what fetch rate does it reach?
A run of 6 instructions takes two fetch cycles, 4 and then 2, because fetch stops at the taken branch: 6 instructions in 2 cycles, 3 per cycle. Fetching two blocks a cycle, or a trace or micro-op cache that stores runs across taken branches, recovers the lost slots.
Name it
Fetch blocks
Each cycle the fetch unit reads an aligned block from the instruction cache and stops at the first predicted-taken branch. With runs of t instructions between taken branches, a w-wide fetch takes cycles per run, plus b bubbles if redirecting isn't instantaneous:
A decoupled front end runs the branch predictor ahead, filling a fetch target queue, so predictions and cache accesses overlap.
Instruction-cache misses
Big programs (databases, web servers, browsers) have instruction footprints of megabytes. An instruction-cache miss leaves the back end starved: a component of penalty in the CPI. Next-line and fetch-directed prefetching follow the predicted path to hide it.
Decoding
Fixed-length instructions decode in parallel trivially. x86 instructions are 1 to 15 bytes, so finding their boundaries is serial work; decoders handle a few instructions a cycle and translate them into micro-operations (µops).
The micro-op cache
A cache of already decoded µops, indexed by instruction address, bypasses the decoders. Delivering u µops a cycle when it hits (fraction h) and d from the decoders when it misses, the average rate is
and it saves the decoders' power on every hit.
Worked example
A µop cache delivers 6 µops a cycle and hits 80% of the time; the legacy decoders deliver 4. What average rate does the front end reach?
Show the solution
- Time per µop. Rates combine as a harmonic mean, weighted by the share of µops each source delivers:
- Rate.
- A better hit rate. Raising the hit rate to 95% would give 5.85.
Answer the front end averages 5.45 µops a cycle.
Worked example
A server program misses the instruction cache 5 times per thousand instructions, and each miss costs 20 cycles from the L2. What CPI does that add to a core that would otherwise run at IPC 3?
Show the solution
- Added CPI. cycles per instruction.
- Compare with the ideal. An IPC of 3 is an ideal CPI of 0.33, so the misses make the core 30% slower.
- Why instruction misses hurt more. For the same program a data-cache miss rate of 5 MPKI would usually cost less, because the out-of-order back end overlaps data misses but has nothing to work on when instructions stop arriving.
Answer the misses add 0.1 CPI, making the core 30% slower.
Worked example
An 8-wide fetch loses one bubble cycle on every taken branch, and taken branches come every 8 instructions. What rate does it reach, and what without the bubble?
Show the solution
- With the bubble. Each run of 8 instructions takes one fetch cycle and one bubble: half the width.
- Without the bubble. It would deliver 8 per cycle.
- What that's worth. A zero-bubble BTB redirect is worth as much as doubling the fetch width here.
Answer 4 instructions a cycle with the bubble, and 8 without it.
Check
A 6-wide fetch stops at taken branches, which come every 5 instructions; each redirection costs 2 bubble cycle(s).
(a)
What fetch rate does it reach?
(b)
What fraction of the fetch slots carry instructions?
Check
A micro-op cache hits 0.8 of the time and delivers 6 µops a cycle; the decoders deliver 4.
(a)
What average rate does the front end reach?
Check
A program misses the instruction cache 5 times per thousand instructions, at 12 cycles each, on a core whose CPI is otherwise 0.5.
(a)
What CPI do the misses add?
(b)
What fraction of the time is the front end starving the core?
Check
Why does a micro-op cache save power as well as raising the delivery rate on an x86 core?
Check
Taken branches come every t instructions; fetch is w wide and loses b cycles per redirection. Treat as t/w.
Step 1
How many cycles does each run between taken branches take?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the fetch rate?
Step 3
By what fraction of the ideal time does the bubble lengthen each run?
Check
Write fetch_rate(runs, w, b): given a list of run lengths (instructions between taken branches), a fetch width w and b bubble cycles per redirection, return instructions per cycle, counting cycles per run.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why do instruction-cache misses usually hurt an out-of-order core more than data-cache misses of the same frequency?
When a load misses the data cache, the out-of-order back end keeps working on independent instructions already in its window, and it can have several data misses outstanding at once, so much of the latency overlaps with useful work. When the instruction cache misses, no new instructions enter the window at all; once the window drains, the core sits idle until the instructions arrive. The front end's misses therefore expose nearly their whole latency, while the back end's are partly hidden, which is why large-footprint server code is so sensitive to instruction-side caching and prefetching.
Practice
Keep practicing
6 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.