D13 · Lesson 13 · A computer of your own
Measuring where a processor's time goes, the iron law of instructions, cycles per instruction and clock rate, the hazards that stall a five-stage pipeline, profiling an execution trace for loads, branches and stalls, and what the compiler's optimizer changes
Your core runs one instruction per cycle, but at a slow clock, because each cycle must fit fetch, decode, execute, memory and write-back end to end. A pipelined core overlaps them and clocks several times faster, at the price of hazards: a value loaded from memory isn't ready for the very next instruction, and a taken branch has already let the wrong instructions start. How much that costs depends on the program, so you measure it: run the program, record every instruction, and count. The same counts show what the compiler's optimizer bought.
- The CRC program compiled at -O0, without optimization, on the simulator. Compare its listing with lesson 12's -O1 version: every variable lives on the stack, so almost every line loads and stores.
- Watch sp and the memory traffic: at -O0 the CRC runs 1686 instructions, at -O1 1042, for the same result.
- Notice that the branches and jumps are the same in both: the optimizer removed memory traffic, not control flow.
Predict
In a five-stage pipeline with forwarding, which of these still costs a stall cycle?
Forwarding hands an ALU result straight to the next instruction, but a load's data only arrives at the end of the memory stage, one cycle too late for an instruction right behind it: a load-use stall. Taken branches and jumps cost more, two cycles here, because the pipeline has already fetched two instructions that must be thrown away.
Name it
The iron law
Instructions N depend on the program and compiler, cycles per instruction on the microarchitecture and the program's hazards, and the clock f on the circuit (S4).
A pipeline model
For a classic five-stage pipeline with forwarding:
- every instruction takes one cycle;
- a load whose destination is read by the very next instruction adds one stall;
- a taken branch or any jump adds two cycles of fetched-and-flushed instructions.
So cycles = N + (load-use stalls) + 2 × (taken branches + jumps), and CPI = cycles/N.
Profiling
Run the program on your core, record each executed instruction with its registers and whether it branched, and count. The lab's numbers for the CRC:
| instructions | loads | load-use | taken + jumps | cycles | CPI | |
|---|---|---|---|---|---|---|
| -O0 | 1686 | 423 | 183 | 51 + 116 | 2203 | 1.31 |
| -O1 | 1042 | 21 | 19 | 51 + 116 | 1395 | 1.34 |
The optimizer keeps variables in registers, so loads fall twentyfold and the stalls with them; control flow doesn't change, so the 334 cycles of branch penalty stay, and at -O1 they're a quarter of the time. That points at the next improvement: branch prediction (C8, S4).
Worked example
At -O1 the CRC takes 1395 cycles on the pipeline. If the pipeline clocks at 200 MHz and the single-cycle core at 50 MHz, how do they compare?
Show the solution
- Pipeline.
- Single-cycle core. One cycle for each of the 1042 instructions:
- Compare. The pipeline is 3.0 times faster despite its CPI of 1.34, because its clock is 4 times faster.
Answer 6.975 µs on the pipeline against 20.8 µs on the single-cycle core, 3.0 times faster.
Worked example
A perfect branch predictor would remove the 2-cycle penalty on taken branches and jumps. What would the CRC's CPI be at -O1?
Show the solution
- Remove the penalties. The 51 taken branches and 116 jumps each lose 2 cycles:
- CPI. For 1042 instructions:
- Real predictors. Real predictors miss sometimes, and jumps through registers are harder to predict, so practical cores land in between.
Answer a CPI of 1.02.
Check
Profile execution traces and compare the optimizer's effect on cycles.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A program executes 2339 instructions at a CPI of 1.2 on a 1000 MHz clock. How long does it take?
Write units the way you normally would: 1.5 µs, 1500 ns and 0.0015 ms are the same answer.
Check
A trace has 1000 instructions, 19 load-use stalls and 343 taken branches and jumps, which cost 2 cycles each. What's its CPI?
Check
A program takes 3000 cycles unoptimized and 1000 optimized. What's the speedup?
Check
A five-stage pipeline stalls 1 cycle(s) on each load-use hazard, which follow 20% of instructions, and loses 2 cycles on each mispredicted branch: branches are 20% of instructions and 30% of them mispredict.
(a)
What is its CPI?
(b)
Against a multicycle version averaging 4.2 cycles per instruction at the same clock, what's the speedup?
Check
An optimized build runs a benchmark in 2 ms against 10 ms for the unoptimized one. What is the speedup?
Explain why
Why did the compiler's optimizer cut the CRC's loads twentyfold but leave its branch penalty untouched?
Without optimization, the compiler keeps every variable in its stack frame and loads it before each use and stores it after each change, so the loop's counters and the running CRC go through memory on every step. The optimizer keeps those variables in registers for the whole loop, which removes nearly all the loads and stores and the stalls that came with them. The branches and jumps implement the loops and the if-else, which the program needs at any optimization level, so the same number are taken and the pipeline pays the same flush penalty for each. Removing that cost takes hardware, a branch predictor, or a different algorithm with fewer branches.
Practice
Keep practicing
7 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.