파이프라인
도입됨이 수업의 내용
| English | 의미 |
|---|---|
| Pipelining/ˈpaɪplaɪnɪŋ/ | overlapping the stages of several instructions, like an assembly line, so one can finish every cycle |
| Forwarding/ˈfɔːwədɪŋ/ | passing a result straight from one pipeline stage to a later instruction before it is written back, to avoid a stall |
| read-after-write hazard/riːd ˈæftə raɪt ˈhæzəd/ | an instruction needs a value that an earlier instruction has not yet written |
| 2-bit predictor/tuː bɪt prɪˈdɪktə/ | a branch predictor that changes its guess only after two wrong predictions in a row |
The assembly line
- Pipelining 流水线 overlaps instructions in five stages (IF, ID, EX, MEM, WB), like an assembly line: instruction $i+1$ is fetched while $i$ is decoded. Once the pipeline is full, one instruction finishes every cycle.
- $n$ instructions in $k$ stages take $n + k - 1$ cycles. Five instructions in five stages take $5 + 5 - 1 = 9$ cycles, not $25$; at $n = 100$ they take $104$.

Ten instructions on a five-stage pipeline take —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
n + k − 1 = 10 + 5 − 1 = 14: fill four, then one per cycle.
Put the five pipeline stages in the order an instruction passes through them.
난이도 3/5 · 200코인 + 오늘 첫 완료 시 작은 보너스 추가
Each stage works on a different instruction in the same cycle; the register write in WB is why a dependent instruction right behind it needs forwarding.
Stage time and CPI
- The stage time is the time of the slowest stage:
| Stage | IF | ID | EX | MEM | WB |
|---|---|---|---|---|---|
| ps | 250 | 150 | 200 | 250 | 100 |
- The five add up to 900 ps, the single-cycle time. Pipelined, every stage must take 250 ps, the slowest one: 250 ps per instruction.
- The ideal CPI is 1, and stalls raise it. A program of 1000 instructions, 25% of them loads, with half the loads followed by a use (one stall each): $1000 + 125 + 4 = 1129$ cycles, CPI $1.129$.
1000 instructions, 25% loads, half load-use: total cycles —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
1000 + 125 stalls + 4 fill = 1129, CPI 1.129.
Hazards and forwarding
- A read-after-write hazard 写后读冒险:
add R1, R2, R3and thensub R4, R1, R1. The sub reads R1 in ID before the add has written it in WB. - Forwarding 转发 sends the ALU output straight back to the ALU inputs for the next instruction, which hides the wait. It cannot help when the producer is a load:
lw R1, …; add R3, R1, R2still stalls one cycle, because the data exists only after MEM. A taken branch, decided in EX, also costs one bubble.
Forwarding hides ALU-to-ALU waits, but load-use still costs —
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Load data exists only after MEM, so the dependent instruction waits one bubble.
Branch prediction
- A pipeline must fetch before it knows which way a branch goes. With $20\%$ branches, $60\%$ of them taken, a one-cycle penalty and the guess "not taken", the cost is $0.2 \times 0.6 \times 1 = 0.12$ extra CPI.
- A 2-bit predictor 两位预测器 learns each branch's habit. At $90\%$ accuracy the cost is $0.2 \times 0.1 \times 1 = 0.02$.
- Deep pipelines make this more important: with a $15$-cycle penalty the same $90\%$ costs $0.30$ CPI, so modern cores predict correctly more than $95\%$ of the time.
25% of instructions are branches, the predictor is right 80% of the time, and a miss costs 3 cycles. With base CPI 1, what is the CPI?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
0.25 x 0.2 x 3 = 0.15 extra, so CPI = 1.15.
How many cycles do 20 instructions take in a 5-stage pipeline with no stalls?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
n + k - 1 = 20 + 5 - 1 = 24.
Deeper is faster, up to a point
- Split the 900 ps of logic into $k$ stages, with 20 ps of latch overhead per stage. The stage time is $900/k + 20$: 200 ps at $k = 5$, 110 ps at 10 and only 65 ps at 20.
- Going from 10 to 20 stages cuts the stage time by 41%, not 50%, and every wrong branch guess now costs more cycles. Real cores settled at about 14 to 20 stages.
900 ps of logic in 10 stages with 20 ps latch overhead each: stage time (ps)?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
$900/10 + 20 = 110$.
Doubling the pipeline depth halves the cycle time.
난이도 1/5 · 100코인 + 오늘 첫 완료 시 작은 보너스 추가
Latch overhead stays per stage: 110 ps at 10 stages becomes 65 ps at 20, not 55.
Worked example: reorder to remove load-use stalls
lw R1, 0(R4); add R3, R1, R2; lw R5, 4(R4)stalls once. Move the independent second load up:lw R1; lw R5; add R3, R1, R2. Now the add finds R1 ready.- If the compiler fixes 80% of the 125 load-use pairs, 25 stalls remain: $1000 + 25 + 4 = 1029$ cycles, CPI $\mathbf{1.029}$ instead of 1.129, with no new hardware.
1000 instructions, 25 remaining load-use stalls, 5-stage fill: CPI (three decimals)?
난이도 2/5 · 140코인 + 오늘 첫 완료 시 작은 보너스 추가
$(1000 + 25 + 4)/1000 = 1.029$.
Marks that slip away
- Counting $nk$ cycles. $n$ instructions in $k$ stages take $n + k - 1$: $9$ for five in five, not $25$.
- Expecting forwarding to fix every hazard. A load followed by a use still stalls one cycle, because the value exists only after MEM.
- Clocking at the fastest stage. The stage time is the slowest stage, $250$ ps here.
Recap
- $n$ instructions in $k$ stages take $n + k - 1$ cycles; the stage time is the slowest stage.
- Forwarding hides ALU-to-ALU hazards; a load-use pair still stalls one cycle.
- Branch cost $=$ branch fraction $\times$ miss rate $\times$ penalty.
어떤 문제가 있었나요? 사이트 팀만 이 내용을 볼 수 있습니다.