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๋ฅผ ๊ฒฐ์ •)

Pipelining ๊ฐœ์š” ๋ฐ Basic Pipelined Processor

5-Stage MIPS Pipeline

MIPS pipeline์€ ๋ณดํ†ต 5๋‹จ๊ณ„๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

  1. IF (Instruction Fetch) โ€” ๋ฉ”๋ชจ๋ฆฌ์—์„œ ๋ช…๋ น์–ด ์ฝ๊ธฐ
  2. ID (Instruction Decode + Register read) โ€” ๋””์ฝ”๋”ฉ & ๋ ˆ์ง€์Šคํ„ฐ ์ฝ๊ธฐ
  3. EX (Execute) โ€” ALU ์—ฐ์‚ฐ
  4. MEM (Memory access) โ€” load/store ์‹œ ๋ฉ”๋ชจ๋ฆฌ ์ ‘๊ทผ
  5. 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๋กœ ์ง์ ‘ ์—ฐ๊ฒฐ.

Hazard ์ข…๋ฅ˜ ๋ฐ Data Hazard

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

Forwarding, Stall, Load-Use hazard

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.

Control Hazard ๋ฐ Branch ํ•ด๊ฒฐ์ฑ…

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 ๋ณต์žก.

Superpipelining, Superscalar

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 (์ด์ƒ์ )

Superscalar scheduling ๋ฐ Loop Unrolling

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์ด ๋” ๋งŽ์Œ)

Dynamic Pipeline Scheduling ๋ฐ Branch Prediction

์ •๋ฆฌ

๊ธฐ๋ฒ• ๋ชฉ์  ๋Œ€ํ‘œ ์˜ˆ
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!