---
title: The front end
heading: Fetch bandwidth, taken branches, instruction caches, decoders and micro-op caches
eyebrow: S4 · Lesson 8 · The front end
xp: 50
---

::: hook
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.
:::

::: play
- 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: $t/(\lceil t/w\rceil + b)$.
- 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.
:::

```sim plot
view: graph
x: t = 1..32
y: r = 0..8
samples: 400
slider: w = 2..8 step 1 = 4
slider: b = 0..2 step 1 = 0
plot: t/(ceil(t/w - 1e-9) + b) | label: fetch rate
plot: w + 0*t | dashed | label: width
```

::: 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?

- [ ] 4 per cycle
- [x] 3 per cycle
- [ ] 6 per cycle
- [ ] 1.5 per cycle

::: reveal
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.
:::
:::

::: nameit
## 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 $\lceil t/w\rceil$ cycles per run, plus b bubbles if redirecting isn't instantaneous:
$$\text{fetch rate} = \frac{t}{\lceil t/w\rceil + b}$$
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 $\text{MPKI}/1000 \times$ 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
$$\frac{1}{h/u + (1 - h)/d}$$
and it saves the decoders' power on every hit.
:::

::: 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?

::: solution
1. **Time per µop.** Rates combine as a harmonic mean, weighted by the share of µops each source delivers:
   $$\begin{aligned} t &= 0.8/6 + 0.2/4 \\ &= 0.183\ \text{cycles} \end{aligned}$$
2. **Rate.**
   $$1/t = 5.45\ \text{µops a cycle}$$
3. **A better hit rate.** Raising the hit rate to 95% would give 5.85.

**Answer:** the front end averages 5.45 µops a cycle.
:::
:::

::: 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?

::: solution
1. **Added CPI.**
   $$5/1000 \times 20 = 0.1$$
   cycles per instruction.
2. **Compare with the ideal.** An IPC of 3 is an ideal CPI of 0.33, so the misses make the core 30% slower.
3. **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.
:::
:::

::: 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?

::: solution
1. **With the bubble.** Each run of 8 instructions takes one fetch cycle and one bubble:
   $$8/(1 + 1) = 4\ \text{per cycle}$$
   half the width.
2. **Without the bubble.** It would deliver 8 per cycle.
3. **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
kind: parts
given: w = 4 | 6 | 8; t = 5 | 7 | 12; b = 1 | 2

## A {w}-wide fetch stops at taken branches, which come every {t} instructions; each redirection costs {b} bubble cycle(s).

::: part
answer: t/(ceil(t/w - 1e-9) + b)
tolerance: 0.5%

### What fetch rate does it reach?

::: misses
- [t/ceil(t/w - 1e-9)] Include the bubbles.
- [any] $t/(\lceil t/w\rceil + b)$.
:::
:::

::: part
answer: t/(ceil(t/w - 1e-9) + b)/w*100
unit: %
tolerance: 0.5%

### What fraction of the fetch slots carry instructions?

::: misses
- [any] Rate over width.
:::
:::
:::

::: check
kind: parts
given: h = 0.6 | 0.8 | 0.95; u = 6 | 8; d = 3 | 4

## A micro-op cache hits {h} of the time and delivers {u} µops a cycle; the decoders deliver {d}.

::: part
answer: 1/(h/u + (1 - h)/d)
tolerance: 0.5%

### What average rate does the front end reach?

::: misses
- [h*u + (1 - h)*d] Rates combine by time per µop: a harmonic mean.
- [any] $1/(h/u + (1 - h)/d)$.
:::
:::
:::

::: check
kind: parts
given: m = 2 | 5 | 20; p = 12 | 20 | 40; c = 0.3 | 0.5

## A program misses the instruction cache {m} times per thousand instructions, at {p} cycles each, on a core whose CPI is otherwise {c}.

::: part
answer: m/1000*p
tolerance: 0.5%

### What CPI do the misses add?

::: misses
- [any] $\text{MPKI}/1000 \times$ penalty.
:::
:::

::: part
answer: (m/1000*p)/(c + m/1000*p)*100
unit: %
tolerance: 0.5%

### What fraction of the time is the front end starving the core?

::: misses
- [any] Stall CPI over total CPI.
:::
:::
:::

::: check
kind: choice

## Why does a micro-op cache save power as well as raising the delivery rate on an x86 core?

- [ ] Micro-ops are smaller than x86 instructions
- [x] On a hit the variable-length decoders, which find instruction boundaries and translate them, stay idle; their work was done once and saved
- [ ] It removes the need for an instruction cache
- [ ] It reduces branch mispredictions

::: misses
- [1] Decoded µops usually take more bits than the instructions they came from.
- [3] The instruction cache still holds the code for misses and for refilling the µop cache.
- [4] Prediction is a separate structure; the µop cache only changes where decoded instructions come from.
:::
:::

::: check
kind: derivation
vars: t = 1..40, w = 1..16, b = 0..3

## Taken branches come every t instructions; fetch is w wide and loses b cycles per redirection. Treat $\lceil t/w\rceil$ as t/w.

::: step
answer: t/w + b

### How many cycles does each run between taken branches take?
:::

::: step
answer: t/(t/w + b)

### What is the fetch rate?
:::

::: step
answer: b*w/t

### By what fraction of the ideal time does the bubble lengthen each run?
:::

::: right
Right. Bubbles matter in proportion to w/t: wider fetch and shorter runs make zero-bubble redirection essential.
:::
:::

::: check
kind: python

## 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 $\lceil t/w\rceil + b$ cycles per run.

```python starter
def fetch_rate(runs, w, b):
    return float(w)
```

```python solution
def fetch_rate(runs, w, b):
    cycles = sum(-(-t // w) + b for t in runs)
    return sum(runs) / cycles
```

```python tests
assert fetch_rate([6] * 100, 4, 0) == 3.0, "The predict: 3 per cycle."
assert fetch_rate([8] * 10, 8, 1) == 4.0, "One bubble per 8-instruction run halves an 8-wide fetch."
assert abs(fetch_rate([3, 9, 5, 12], 4, 1) - 29 / 13) < 1e-12, "Mixed runs: 29 instructions in 13 cycles."
```

::: right
Right. Average run length alone isn't enough; the rounding depends on each run, which is why real traces are used.
:::
:::

::: explain
## Why do instruction-cache misses usually hurt an out-of-order core more than data-cache misses of the same frequency?

::: model
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
::: check
given: w = 4 | 8; t = 6 | 10; b = 0 | 1
answer: t/(ceil(t/w - 1e-9) + b)
tolerance: 0.5%

## Fetch rate: width {w}, runs of {t}, {b} bubble(s)?

::: misses
- [any] $t/(\lceil t/w\rceil + b)$.
:::
:::

::: check
given: h = 0.7 | 0.9; u = 6 | 8
answer: 1/(h/u + (1 - h)/4)
tolerance: 0.5%

## Average µop rate, hit rate {h}, {u} per cycle on a hit, 4 on a miss?

::: misses
- [any] Harmonic combination.
:::
:::

::: check
given: m = 3 | 10; p = 15 | 30
answer: m/1000*p
tolerance: 0.5%

## CPI from {m} instruction-cache MPKI at {p} cycles?

::: misses
- [any] MPKI/1000 × penalty.
:::
:::
:::
