2. Pipelining
2์ฅ Pipelining ๊ฐ์
Pipelining์ ๋ช ๋ น์ด ์(= number of instructions)๊ฐ ์๋ ์คํ ์๋๋ฅผ ๋์ด๋ ๊ธฐ์ ์ด๋ค. ํ๋์ ๋ช ๋ น์ด ์ฒ๋ฆฌ๋ฅผ ์ฌ๋ฌ stage๋ก ๋๋๊ณ , ๊ฐ stage๋ฅผ ๋ ๋ฆฝ์ ์ผ๋ก ๋ณ๋ ฌ ์คํํด ์ฒ๋ฆฌ๋(throughput) ์ ๋์ธ๋ค.
Pipelining in Ordered System
pipelining์ ํต์ฌ ์์ด๋์ด: ์ผ์ ๋จ๊ณ๋ณ๋ก ์ชผ๊ฐ๊ณ , ๊ฐ ๋จ๊ณ๋ pipeline register์ ์ํด ๊ฒฉ๋ฆฌ๋์ด ๋์์ ์๋ก ๋ค๋ฅธ ๋ช ๋ น์ด์ ๋ค๋ฅธ ๋จ๊ณ๊ฐ ์ํ๋๋ค.
- Advance physical, but not latency
- Limitation: ๋จ์ผ ์์ ์ latency๋ ์ค์ด๋ค์ง ์์
- Pipeline stage์ ๊ฐ์ฅ ๋๋ฆฐ stage์ ์ํด ๊ฒฐ์ ๋จ (slowest stage๊ฐ ์ ์ฒด clock period๋ฅผ ๊ฒฐ์ )

5-Stage MIPS Pipeline
MIPS pipeline์ ๋ณดํต 5๋จ๊ณ๋ก ๊ตฌ์ฑ๋๋ค.
- IF (Instruction Fetch) โ ๋ฉ๋ชจ๋ฆฌ์์ ๋ช ๋ น์ด ์ฝ๊ธฐ
- ID (Instruction Decode + Register read) โ ๋์ฝ๋ฉ & ๋ ์ง์คํฐ ์ฝ๊ธฐ
- EX (Execute) โ ALU ์ฐ์ฐ
- MEM (Memory access) โ load/store ์ ๋ฉ๋ชจ๋ฆฌ ์ ๊ทผ
- WB (Write Back) โ ๋ ์ง์คํฐ์ ๊ฒฐ๊ณผ ๊ธฐ๋ก
Sequential vs Pipelined ์ฑ๋ฅ ๋น๊ต
๋์ผํ ๋ช ๋ น์ด๋ฅผ ์คํํ ๋:
- Sequential Execution: ๊ฐ ๋ช ๋ น์ด๋ฅผ ๋ชจ๋ stage ์๋ฃ ํ ๋ค์ ๋ช ๋ น์ด ์์ (์: 8ns ร 3 instructions)
- Pipelined Execution: ๊ฐ stage๊ฐ ๊ฒน์ณ์ ์คํ โ ์ ์ฒด ์๊ฐ์ด 2ns ร (3 + 4) = ์ฝ 14ns๋ก ๋จ์ถ
์ฆ ๋ช ๋ น์ด ์๊ฐ ๋ง์์๋ก ์ด์์ ์ผ๋ก๋ stage ์๋งํผ throughput ์ฆ๊ฐ.
Hazards โ ํ์ดํ๋ผ์ธ์ ์
Pipeline์ด ์ด์์ ์ผ๋ก ๋์ํ์ง ์๋๋ก ๋ง๋ hazard ์ธ ์ข ๋ฅ:
1. Structural Hazard
Multiple instructions are being processed at same time.
์: ํ๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ IF์ MEM์ด ๋์์ ์ธ ๋ ์ถฉ๋. โ Solutions:
- Replicate resources (I-cache / D-cache ๋ถ๋ฆฌ)
- pipeline register๋ก stage๋ณ ๊ฒฉ๋ฆฌ
2. Data Hazard
์ด์ ๋ช ๋ น์ด์ ๊ฒฐ๊ณผ๋ฅผ ๋ค์ ๋ช ๋ น์ด๊ฐ ์ฌ์ฉํด์ผ ํ๋ ๊ฒฝ์ฐ.
์:
add r1, r2, r3 # r1์ ๊ฐ์ ์ฐ๊ณ
sub r4, r1, r5 # ๋ฐ๋ก r1์ ์ฝ์ โ r1 ์์ง WB ์๋จ
์ด์ ๋ช ๋ น์ด๊ฐ ์์ง WB ๋จ๊ณ์ ๋๋ฌํ์ง ์์์ ๋ ๋ค ๋ช ๋ น์ด๊ฐ ์ฝ์ผ๋ฉด ์๋ชป๋ ๊ฐ์ ๊ฐ์ ธ๊ฐ. Worst case: suspend execution โ stall.
Solutions
- Delay second access โ ์ด์ ๋ช ๋ น์ด ๊ฒฐ๊ณผ๊ฐ ๋์ฐฉํ ๋๊น์ง ์ ์ง (stall / bubble)
- Resource Duplication
- Forwarding (Bypassing): ALU ๊ฒฐ๊ณผ๋ฅผ WB ๋๊ธฐํ์ง ์๊ณ ๊ณง๋ฐ๋ก EX ์ ๋ ฅ์ผ๋ก ์ ๋ฌ. Pipeline register์์ ๋ค์ stage๋ก ์ง์ ์ฐ๊ฒฐ.

Load-Use Data Hazard
Forwarding์ผ๋ก๋ ํด๊ฒฐ ์ ๋๋ ๊ฒฝ์ฐ: Load ํ ๋ฐ๋ก ๊ทธ ๊ฐ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ. Load์ ๊ฐ์ MEM stage ๋์ ๋์ค๋ฏ๋ก, ๋ค์ ๋ช ๋ น์ด์ EX ์ ์ ๋๋ฌ ๋ชป ํจ โ 1 cycle stall ํ์.
MIPS architecture calls this delayed load, initial implementations required compiler to deal with this.
Stall / Bubble
- Key idea: Connect new value directly to next stage
- Still need to stall for stalls โ load instruction์ load-use ํด๊ฒฐ
- ALU results to next instruction (Stall X)
- Problem: what about load instructions? โ ๋ค์ ๋ช ๋ น์ด stall + forward

3. Control Hazard (Branch Hazard)
Branch determines flow of control. ๋ถ๊ธฐ ๊ฒฐ๊ณผ๊ฐ ํ์ ๋๊ธฐ ์ ์ ๋ค ๋ช ๋ น์ด๋ฅผ fetchํ๋ฉด, ์๋ชป๋ ๋ช ๋ น์ด๋ฅผ ์คํํ ์ ์์.
- Pipeline can''t always fetch correct instruction โ fetch ๋จ๊ณ์์ PC๋ฅผ ๊ฒฐ์ ํด์ผ ํ๋๋ฐ branch๋ EX ์ ์ ํ์ ๋์ง X
- 5-stage pipeline, branch 10% ๊ธฐ์ค branch penalty:
$$\text{CPI} = 1 + 0.1 \times 3 = 1.3$$
โ 10% branches ร 3 cycle stall = 15% branch impact โ MIPS๊ฐ 20% ์ฑ๋ฅ ์ ํ
Solutions
- Stall โ ๋จ์ํ์ง๋ง loading instructions until result is available
- Compromise branch processing โ simplified branch condition
- Prediction โ assume outcome and continue fetching (predict not-taken โ flush if wrong)
- Delayed branch โ compiler schedules a useful instruction into the delay slot (MIPS)
- Compile re-orders instructions into delay slot. Insert
nop(no operation) instructions when can't reorder.

Pipelined Implementation์ ์ฑ๋ฅ
CPI ๊ณ์ฐ ์
- Use "gcc" instruction mix to calculate CPI
- IW = 25% (2 cycles when load-use happen)
- SW = 10% (1 cycle)
- R-type = 52%
- Branch = 11% (1 cycle delayed branches)
- Jump = 2% (1 cycle)
๊ฐ์ : 50% of load instructions are followed by immed. use.
$$\text{CPI} = 0.5 \times (0.25 \times 2 + 0.75 \times 1) + 0.5 \times (0.25 \times 0.52 + 0.10 \times 0.11 + 0.02) = \text{์ฝ } 1.17$$
โ Pipelining ๋๋ถ์ 1.17 cycles per instruction์ ํจ๊ณผ.
Superpipelining
Key idea: Increase the number of stages. ์: Pentium 4 โ 20 stages.
- Advantages: Faster clock (๊ฐ stage ์งง์)
- Disadvantages: Longer pipeline โ higher branch penalty, flush ๋ง์
- Used in conjunction with other techniques to overcome disadvantages (branch prediction)
Superscalar
Key idea: Issue (and execute) multiple instructions in each clock cycle.
Example: Issue ALU/branch and load/store at MIPS:
| ALU / branch | Load / Store |
|---|---|
| nop | lw $t0, 0($s1) |
| add $s1, $s1, $s0 | sw $t0, 0($s1) | |
| nop | ... |
โ ๋ณต์ instruction์ ๋์์ issue. ๋จ dependency ์งํค๊ธฐ ์ํ scheduling ๋ณต์ก.

Software Manipulation to Increase ILP
Simple Superscalar Code Scheduling
Loop:
lw $t0, 0($s1) # $t0 = array element
addu $t0, $t0, $s2 # add scalar in $s2
sw $t0, 0($s1) # store result
addi $s1, $s1, -4 # decrement pointer
bne $s1, $zero, Loop # branch $s1 != 0
Reordering โ ์์กด์ฑ ์๋ ๋ช ๋ น์ด ์ฌ๋ฐฐ์น
Note: Update value of $t0 will not be available for next iteration โ ์์กด์ฑ ์๋ instruction๋ค์ ๋ฉ๋ฆฌ ๋ฐฐ์นํ๋ฉด stall์ ํผํ ์ ์๋ค.
Loop Unrolling
Assume loop count is multiple of 4, & unroll 4 loop iterations in 4 cycles.
Loop:
lw $t0, 0($s1) lw $t1, -4($s1)
lw $t2, -8($s1) lw $t3, -12($s1)
addu $t0, $t0, $s2 addu $t1, $t1, $s2
...
โ Superscalar์์ 4 ร IPC = 4 cycles per 4 iterations (์ด์์ )

Dynamic Pipeline Scheduling
ํ๋ก๊ทธ๋จ์ด ๋์ ์ค ๋ช ๋ น์ด๋ค์ ์์๋ฅผ ๋์ ์ผ๋ก ๋ณ๊ฒฝํ๋ ๊ธฐ๋ฒ. ์ปดํ์ผ๋ฌ๊ฐ ์๋ ํ๋์จ์ด๊ฐ ์ํ.
Out-of-order Execute, In-order Commit
Instruction Fetch & Decode unit (In-order issue)
โ
โโโโโโโผโโโโโโฌโโโโโโฌโโโโโโ
โผ โผ โผ โผ
Reservation stations (ร N)
โ โ โ โ
โผ โผ โผ โผ
Integer Integer Floating point Load/Store
โ โ โ โ
โผ โผ โผ โผ
Commit unit (In-order commit)
- Example: Power PC 604, Pentium Pro, Alpha 21264, MIPS R10000
- Reservation station์ instruction์ ๋น์ทํ ๊ฒ๋ผ๋ฆฌ ๋ถ๋ฅ(์ปดํ์ผ ์์ ์๋)
- Out-of-order execute โ ์ฒ๋ฆฌ ๊ฐ๋ฅํ ๊ฒ๋ถํฐ ๋จผ์ ์คํ
- Commit์ ์์๋ฅผ ๋ง์ถฐ์ in-order
Overcomes Key Performance Limitations
- Structural hazard โ Replicate pipelines
- Data hazard โ Execute instructions out of program order, Rename registers as needed
- Control hazard โ Execute instructions speculatively across branches (ํฌ๊ธฐ์ ์คํ)
์ฒ๋ฆฌ ๊ฐ์ค์น๊ฐ ์ด๋ ค์ด ๊ฒ๋ถํฐ skip ๊ฐ๋ฅ โ stall ๊ฐ์.
Dynamic Branch Prediction
ํ๋ก๊ทธ๋จ์ด ๋์ํ๋ ์ค์ ๋ถ๊ธฐ๋ฅผ ์์ธกํ๋ ๊ฒ (โ Static์ ์คํ ์ ).
One-bit Prediction Scheme
- ๊ฐ branch์ ๋ํด ์ด์ ์ taken๋๋์ง not-taken๋๋์ง๋ฅผ ํ ๋นํธ๋ก ์ ์ฅํด์ ์์ธก
- ์:
for (i=0; i<10000; i++)๊ฐ์ ๋ฃจํ๋ Good (๋๋ถ๋ถ taken) - Problem: Loop case (๋ง์ง๋ง iteration์์ ์๋ชป ์์ธก)
๋ ์ง์คํฐ๋ฅผ ํ๋ ์ค์ ์ด์ ์ taken๋๋์ง not-taken๋๋์ง ์ ์ฅํ์ฌ ์์ธก.
Two-bit Prediction Scheme
state machine: predict taken / not-taken ๋ ์ํ๊ฐ ์๋๋ผ 4 ์ํ (strong taken, weak taken, weak not-taken, strong not-taken).
- ํ ๋ฒ ํ๋ ค๋ ๋ฐ๋ก ์์ธก ๋ค์ง์ง ์์ โ Loop case์์๋ ์ ํ๋ ๋์
- Nested loop์์ ์ for๋ฌธ / ๋ฐ๊นฅ for๋ฌธ ๋ชจ๋ ์ ์์ธก
for (i=0; i<10000; i++) โ ๋ฐ๊นฅ
for (j=0; j<10000; j++) โ ์์ชฝ (taken์ด ๋ ๋ง์)

์ ๋ฆฌ
| ๊ธฐ๋ฒ | ๋ชฉ์ | ๋ํ ์ |
|---|---|---|
| Pipelining | Throughput โ | 5-stage MIPS |
| Forwarding | Data hazard ์ต์ํ | EX โ EX bypass |
| Delayed branch | Control hazard ์ํ | MIPS 1 cycle delay slot |
| Superscalar | IPC โ | Pentium Pro |
| Superpipelining | Clock โ | Pentium 4 (20 stages) |
| Out-of-order | ํ๋์จ์ด ์ค์ผ์ค๋ง | R10000, Alpha 21264 |
| Branch Prediction (2-bit) | ๋ถ๊ธฐ ์ ํ๋ โ | ํ๋ CPU ํ์ค |
pipelining๊ณผ ๊ทธ ๋ณํ๋ค์ ํ๋์จ์ด ๋ณ๋ ฌ์ฑ์ ๊ธฐ๋ณธ์ด๊ณ , ์ดํ ์บ์ ๊ณ์ธต, virtual memory, multi-core๊น์ง ์ด์ด์ง๋ ํ ๋๊ฐ ๋๋ค.
Comments (0)
No comments yet. Be the first to comment!
Please to write a comment.