자막
음성

Lec 4 · Pipelining 기초: 5-stage pipeline 과 hazard

정밀 정리 · 검증 2026-09-23 23:06

1개요

1.1single-cycle datapath 에서 RISC-V R·I·B·J type 명령어의 실행 경로를 복습하고, LD 의 600 ps critical path 로 한계를 보임.
1.2multi-cycle·microcoded CPU 를 거쳐 5-stage pipeline(200 ps 단계, 5 GHz)과 iron law 로 CPI·cycle time trade-off 를 비교.
1.3structural·data(RAW/WAR/WAW)·control hazard 와 해결책(hardware 추가, interlock, bypass, speculation, kill/bubble)을 다룸.
1.4branch delay slot 이 1990년 이후 사라진 이유와 5-stage 에서도 CPI > 1 인 이유로 마무리. exception(slide 42~58)은 05강으로 넘어감.

2섹션별 정리 상황·문제 → 원인 → 해결

2.12:30–7:05명령어 처리 5단계와 single-cycle datapath
상황·문제학부 CPU 설계 복습으로 pipelining 기초를 다룸. 명령어 처리는 IF·ID·EX·MEM·WB 5단계.
원인각 단계는 PC·instruction memory(I-cache)·register file·ALU·data memory(D-cache) 에서 수행되고, control logic 이 명령어를 보고 control signal 을 생성.
해결이 datapath 는 single-cycle CPU: 5단계를 한 cycle 에 모두 수행.
  • IF → ID(decode + register read) → EX(ALU) → MEM(load/store 만) → WB
  • PC+4 계산과 branch target 계산용 adder 가 따로 있음
  • control signal: RegWrite, MemRead, MemWrite, ALUSrc, MemtoReg 등
p.2
2.1.1 · p.2 · 3:30
p.3
2.1.2 · p.3 · 4:34
2.1.r.1Classic RISC pipeline — Wikipedia ↗ · IF/ID/EX/MEM/WB 5단계, hazard, bypassing, delay slot, exception 정리
2.1.r.2Computer Organization and Design RISC-V Edition, 2nd ed. — Elsevier ↗ · single-cycle·multi-cycle·pipelined datapath 그림(P&H CO&D) 출처
2.27:05–17:15RISC-V 명령어 유형별 datapath 동작 (R·I·B·J type)
상황·문제명령어 유형마다 datapath 에서 사용하는 component 와 경로가 다름.
원인R-type: 두 register → ALU → register. LD(I-type): base + sign-extended imm 으로 주소 계산 후 memory read. BEQ(B-type): ALU 로 비교, PC+imm 을 target 으로. JAL(J-type): PC+imm 으로 무조건 jump, PC+4 를 rd 에 저장.
해결모든 유형이 명령어 크기 4 byte 가정으로 PC+4 를 계산. rd=x0 이면 return 주소 저장을 버릴 수 있음(x0 은 항상 0).
  • LD 는 ALU(주소 계산)와 memory 를 모두 사용
  • branch 는 ALU 결과를 쓰지 않고 equal 신호만 사용, 조건+Branch 신호로 mux 선택
  • JAL 은 imm 이 더 길어 먼 거리 jump 가능, 함수 호출 후 복귀에 사용
p.5
2.2.1 · p.5 · 8:32
p.7
2.2.2 · p.7 · 10:33
p.9
2.2.3 · p.9 · 12:07
2.2.r.1RISC-V Ratified Specifications ↗ · RISC-V unprivileged ISA 명령어 형식(R/I/B/J type) 원문
2.2.r.2Computer Organization and Design RISC-V Edition, 2nd ed. — Elsevier ↗ · single-cycle·multi-cycle·pipelined datapath 그림(P&H CO&D) 출처
2.317:15–27:30Single-cycle 의 critical path 와 multi-cycle·microcoded CPU
상황·문제single-cycle CPU 는 모든 명령어를 한 cycle 에 맞춰야 해서 clock cycle 이 길어짐.
원인LD 가 모든 단계를 거치는 worst-case critical path: memory 200 + RF 50 + ALU 100 + memory 200 + RF 50 = 600 ps. R-type 등은 그보다 짧은데도 같은 시간을 씀.
해결multi-cycle CPU 로 명령어별 필요한 단계만 수행. ALU 하나를 여러 cycle 재사용하고, ROM 의 microcode 가 micro PC 로 단계별 control signal 을 생성.
  • 600 ps cycle 은 1 GHz 이상이지만 latency 는 예시용 가정값
  • micro PC 는 program 의 PC 와 별개, opcode 로 다음 micro 단계를 선택
  • branch 예: IF → ID(PC+4 계산) → EX(비교) → 조건 충족 시 target 계산 단계
p.13
2.3.1 · p.13 · 19:03
p.17
2.3.2 · p.17 · 22:11
p.15
2.3.3 · p.15 · 25:02
2.3.r.1Microcode — Wikipedia ↗ · horizontal microcode, control store, microsequencer
2.3.r.2Computer Organization and Design RISC-V Edition, 2nd ed. — Elsevier ↗ · single-cycle·multi-cycle·pipelined datapath 그림(P&H CO&D) 출처
2.427:30–33:30Pipelining 개념과 iron law·CPI
상황·문제multi-cycle 보다 더 나은 성능을 위해 pipelined CPU 를 설계.
원인빨래 예: 세탁기가 끝나면 건조 중에 다음 빨래를 세탁 → 단계를 overlap. datapath 를 5단계로 나누고 clock 은 가장 긴 단계(200 ps)로 결정 → 5 GHz.
해결iron law: time = instructions/program × CPI × time/cycle. multi-cycle 은 cycle time 을 줄이지만 CPI 증가, pipeline 은 짧은 cycle time 과 이상적 CPI=1 을 동시에 얻음.
  • 5단계 pipeline 의 CPI 는 5가 아니라 이상적으로 1
  • n 개 명령어 ≈ n cycle (n 이 충분히 클 때)
  • microarchitecture 는 instructions/program(기계어)을 바꾸지 않음
p.18
2.4.1 · p.18 · 27:20
p.19
2.4.2 · p.19 · 29:11
p.21
2.4.3 · p.21 · 30:37
p.22
2.4.4 · p.22 · 32:33
2.4.r.1Instruction pipelining — Wikipedia ↗ · 단계 overlap 으로 throughput 을 높이는 원리
2.4.r.2Iron law of processor performance — Wikipedia ↗ · time/program = instructions/program × CPI × time/cycle
2.4.r.3Cycles per instruction — Wikipedia ↗ · CPI 정의와 multi-cycle MIPS 계산 예
2.533:30–37:05Hazard 종류와 bubble 이 CPI 에 주는 영향
상황·문제실제 program 에서 CPI=1 달성은 거의 불가능.
원인structural hazard(자원 부족), data hazard(앞 명령어 결과에 의존), control hazard(다음 PC 를 모름)가 있음.
해결기본 처리법은 bubble 삽입. bubble 1개면 3 명령어에 4 cycle → CPI 1.33, 2개면 1.67.
  • bubble = 유용한 일을 하지 않는 pipeline slot
  • hazard 처리로 CPI > 1 이 됨
p.23
2.5.1 · p.23 · 33:40
p.24
2.5.2 · p.24 · 36:20
2.5.r.1Hazard (computer architecture) — Wikipedia ↗ · structural·data(RAW/WAR/WAW)·control hazard 분류
2.5.r.2Pipeline stall (Bubble) — Wikipedia ↗ · hazard 해소를 위한 stall·bubble
2.637:05–40:00Structural hazard 해결
상황·문제두 명령어가 같은 hardware 자원을 동시에 필요로 할 때 발생.
원인classic 5-stage 정수 pipeline 은 단계마다 한 명령어만 자원을 쓰므로 설계상 없음. 그러나 multiplier·divider·FP unit·register write-back port 를 공유하는 구현에서는 발생.
해결hardware 추가(예: memory 에 두 번째 port) 또는 새 명령어 stall. 4-wide datapath 에 multiplier 4개는 비싸므로 성능-비용 trade-off.
  • hardware 추가는 area 비용
  • 동시에 multiplier 를 쓰는 경우는 드묾
p.25
2.6.1 · p.25 · 37:05
2.6.r.1Hazard (computer architecture) — Wikipedia ↗ · structural·data(RAW/WAR/WAW)·control hazard 분류
2.740:00–43:20Data hazard 유형: RAW·WAR·WAW
상황·문제뒤 명령어가 앞 명령어의 결과 register 를 operand 로 쓰면 의존성이 생김.
원인RAW(true dependence), WAR(anti-dependence), WAW(output dependence). WAR·WAW 는 in-order CPU 에서는 순서상 문제 없음.
해결out-of-order core 에서는 실행 순서가 바뀌므로 WAR·WAW 도 처리해야 program semantics 보존. read-after-read 는 hazard 아님.
  • r3 ← r1 op r2 ; r5 ← r3 op r4 → RAW
  • WAR·WAW 는 이후 dynamic scheduling 강의와 연결
p.26
2.7.1 · p.26 · 40:16
2.7.r.1Hazard (computer architecture) — Wikipedia ↗ · structural·data(RAW/WAR/WAW)·control hazard 분류
2.843:20–52:20Data hazard 해결: interlock·bypass·speculation
상황·문제RAW 의존 명령어는 앞 명령어의 결과가 register file 에 쓰이기 전에는 값을 읽을 수 없음.
원인interlock 만 쓰면 add x1 → sub x2,x1 사이에 bubble 3개. decode 단계에서 source register 와 앞선 명령어의 destination 을 비교해 hazard 검출.
해결bypass(data forwarding): ALU·memory 출력을 mux 로 ALU 입력에 전달해 bubble 제거. value speculation 은 branch prediction·stack pointer update·memory address disambiguation 등 제한된 경우에만 효과적.
  • full bypass 는 ALU 앞 mux 2개 + store data 용 mux
  • W 단계 write 와 D 단계 read 가 같은 cycle 에 가능하다고 가정
  • branch prediction 은 사실 control hazard 쪽 speculation
p.27
2.8.1 · p.27 · 43:28
p.28
2.8.2 · p.28 · 45:40
p.30
2.8.3 · p.30 · 49:07
p.31
2.8.4 · p.31 · 50:39
2.8.r.1Operand forwarding — Wikipedia ↗ · register file 을 거치지 않고 결과를 다음 명령어에 전달
2.8.r.2Pipeline stall (Bubble) — Wikipedia ↗ · hazard 해소를 위한 stall·bubble
2.952:20–1:00:50Control hazard: 다음 PC 정보가 pipeline 에서 늦게 나옴
상황·문제IF 는 다음 PC 가 필요하지만, 그 PC 를 결정할 정보는 앞 명령어가 몇 단계 지나야 나옴.
원인opcode·offset 은 decode 에서, branch 조건과 jump register 값은 execute 에서 알 수 있음. PC+4 로 가져온 다음 명령어가 틀릴 수 있음.
해결unconditional PC-relative jump 는 decode 단계 adder 로 target 계산, FKill 신호로 방금 fetch 한 명령어를 bubble 로 바꿈 → bubble 1개.
  • jump register(jr): register + offset, 함수 return 에 사용
  • Kill 비트는 control 값을 모두 de-assert 해 명령어를 bubble 로
p.32
2.9.1 · p.32 · 52:30
p.33
2.9.2 · p.33 · 55:27
p.34
2.9.3 · p.34 · 57:39
p.35
2.9.4 · p.35 · 1:00:09
2.9.r.1Hazard (computer architecture) — Wikipedia ↗ · structural·data(RAW/WAR/WAW)·control hazard 분류
2.9.r.2Classic RISC pipeline — Wikipedia ↗ · IF/ID/EX/MEM/WB 5단계, hazard, bypassing, delay slot, exception 정리
2.101:00:50–1:08:35Branch delay slot 과 이를 버린 이유
상황·문제초기 MIPS 는 jump 뒤 bubble 을 없애려고 ISA semantics 를 바꿈: branch/jump 다음 명령어는 항상 실행.
원인software 가 delay slot 을 유용한 명령어로 채워야 하고, 못 찾으면 NOP. MIPS = Microprocessor without Interlocked Pipeline Stages.
해결1990년 이후 RISC ISA 는 delay slot 없음: microarchitecture 세부를 ISA 에 넣고, 30-stage·4-issue(최대 120 명령어) 에서는 무의미, NOP 이 I-cache 를 낭비. 좋은 branch predictor 가 필요성을 줄임.
  • NOP 은 CPI 를 줄이지만 instructions/program 을 늘림
  • load delay slot 도 같은 이유로 사라짐
p.36
2.10.1 · p.36 · 1:00:55
p.37
2.10.2 · p.37 · 1:05:44
2.10.r.1Delay slot — Wikipedia ↗ · MIPS branch delay slot·load delay slot
2.10.r.2Branch predictor — Wikipedia ↗ · control hazard 를 speculation 으로 줄이는 장치 (이후 강의 주제)
2.111:08:35–1:10:35Conditional branch 와 jump register 의 bubble
상황·문제conditional branch 는 조건이 ALU(EX) 에서 나오므로 jump 보다 늦게 결정됨.
원인EX 단계에 도달할 때 이미 잘못된 경로에서 명령어 2개를 fetch 함.
해결FKill·DKill 로 두 명령어를 죽여 bubble 2개. jump register 도 register 값이 필요해 bubble 2개.
  • branch 여부 + 조건 결과 + target 비교로 kill 신호 생성
p.38
2.11.1 · p.38 · 1:09:00
p.39
2.11.2 · p.39 · 1:10:13
p.40
2.11.3 · p.40 · 1:10:22
2.11.r.1Classic RISC pipeline — Wikipedia ↗ · IF/ID/EX/MEM/WB 5단계, hazard, bypassing, delay slot, exception 정리
2.121:10:35–1:16:505단계 pipeline 에서 CPI > 1 이 되는 이유
상황·문제bypass 가 있어도 모든 bubble 을 없앨 수는 없음.
원인branch predictor 도 항상 맞지 않음. full bypass 는 deep pipeline·superscalar 에서 비용이 너무 큼. load 결과는 바로 다음 명령어의 EX 에 전달 불가.
해결자주 쓰는 bypass 경로만 제공하고 area 는 cache 등에 사용. load-use 에는 bubble 1개. MIPS-I 의 load delay slot(slot 명령어는 load 이전 값을 봄)은 MIPS-II 에서 제거됨.
  • 드물게 쓰는 bypass 는 cycle time 을 늘려 CPI 이득을 상쇄할 수 있음
  • jump/branch 는 delay slot 이 없으면 뒤 명령어를 kill
p.41
2.12.1 · p.41 · 1:10:35
2.12.r.1Delay slot — Wikipedia ↗ · MIPS branch delay slot·load delay slot
2.12.r.2MIPS architecture — Wikipedia ↗ · MIPS I 의 delay slot, MIPS II 에서 load delay slot 제거
2.12.r.3Operand forwarding — Wikipedia ↗ · register file 을 거치지 않고 결과를 다음 명령어에 전달
2.131:16:50–1:18:10마무리: project proposal 안내, 다음 주 추석 휴강
상황·문제exception 부분(slide 42~) 은 시작하지 못하고 종료. project proposal guideline 을 PLMS 에 올릴 예정, 다음 주는 추석이라 그다음 주에 수업.
p.42
2.13.1 · p.42 · 1:16:44

3핵심 개념

3.1critical path(임계 경로)
clock cycle 을 결정하는 최장 latency 경로. single-cycle 에서는 LD 의 600 ps.
3.2microcode(마이크로코드)
ROM 에 저장된 단계별 control signal row. micro PC 로 순서 제어.
3.2.r.1Microcode — Wikipedia ↗ · horizontal microcode, control store, microsequencer
3.3iron law of processor performance(철칙)
time = instructions/program × CPI × time/cycle.
3.3.r.1Iron law of processor performance — Wikipedia ↗ · time/program = instructions/program × CPI × time/cycle
3.4pipeline bubble(버블)
hazard 해소를 위해 비워 두는 slot. bubble 1개/3 명령어면 CPI 1.33.
3.4.r.1Pipeline stall (Bubble) — Wikipedia ↗ · hazard 해소를 위한 stall·bubble
3.5structural hazard(구조적 해저드)
같은 hardware 자원을 동시에 요구. hardware 추가 또는 stall.
3.5.r.1Hazard (computer architecture) — Wikipedia ↗ · structural·data(RAW/WAR/WAW)·control hazard 분류
3.6RAW / WAR / WAW hazard
true / anti / output dependence. in-order 에서는 RAW 만 문제.
3.6.r.1Hazard (computer architecture) — Wikipedia ↗ · structural·data(RAW/WAR/WAW)·control hazard 분류
3.7interlock(인터록)
hazard 가 풀릴 때까지 의존 명령어를 issue 단계에 대기시킴.
3.8bypass / data forwarding(바이패스)
결과를 register file 대신 mux 경로로 다음 명령어 ALU 입력에 전달.
3.8.r.1Operand forwarding — Wikipedia ↗ · register file 을 거치지 않고 결과를 다음 명령어에 전달
3.9control hazard(제어 해저드)
다음 PC 결정 정보가 decode·execute 에서야 나와 잘못 fetch 한 명령어를 kill.
3.9.r.1Hazard (computer architecture) — Wikipedia ↗ · structural·data(RAW/WAR/WAW)·control hazard 분류
3.10branch delay slot(분기 지연 슬롯)
branch/jump 다음 명령어를 항상 실행하는 ISA semantics. 1990년 이후 RISC 에서 폐기.
3.10.r.1Delay slot — Wikipedia ↗ · MIPS branch delay slot·load delay slot
3.11load-use delay(로드 사용 지연)
load 결과는 다음 명령어 EX 에 바로 전달 불가 → bubble 1개.
3.11.r.1Delay slot — Wikipedia ↗ · MIPS branch delay slot·load delay slot

4스스로 확인

4.1single-cycle CPU 에서 clock cycle 을 결정하는 명령어와 그 latency 는?
답
모든 단계를 쓰는 LD. 200+50+100+200+50 = 600 ps.
4.25-stage pipeline 의 이상적 CPI 가 5 가 아닌 이유는?
답
명령어 latency 는 5 cycle 이지만 매 cycle 새 명령어를 issue 해 overlap 하므로 CPI=1.
4.3in-order 5-stage 에서 WAR·WAW 가 문제가 되지 않는 이유는?
답
앞 명령어가 read/write 를 먼저 마친 뒤 뒤 명령어가 write 하기 때문. out-of-order 에서는 처리 필요.
4.4add x1 → sub x2,x1 에서 interlock 만 쓸 때와 bypass 를 쓸 때 bubble 수는?
답
interlock 3개, ALU 출력 bypass 0개.
4.5conditional branch 와 unconditional jump 의 bubble 수가 다른 이유는?
답
jump target 은 decode 에서 계산(bubble 1), branch 조건은 EX 에서 결정(bubble 2).
4.6branch delay slot 이 현대 ISA 에서 사라진 이유 2가지는?
답
microarchitecture 세부를 ISA 에 고정해 deep/wide pipeline 에서 무의미, NOP 으로 I-cache 낭비. branch prediction 이 대체.

5참고 자료

5.1Computer Architecture: A Quantitative Approach, 6th ed. — Elsevier ↗ · 교재. 2.1-2.2, Appendix B.1-B.6 읽기 과제, R4000 그림(Figure C.36-C.47) 출처
5.2Computer Organization and Design RISC-V Edition, 2nd ed. — Elsevier ↗ · single-cycle·multi-cycle·pipelined datapath 그림(P&H CO&D) 출처
5.3Classic RISC pipeline — Wikipedia ↗ · IF/ID/EX/MEM/WB 5단계, hazard, bypassing, delay slot, exception 정리
5.4Hazard (computer architecture) — Wikipedia ↗ · structural·data(RAW/WAR/WAW)·control hazard 분류
5.5RISC-V Ratified Specifications ↗ · RISC-V unprivileged ISA 명령어 형식(R/I/B/J type) 원문

7한국어판 강의자료

단축키
Ctrl K · s자료 검색
[ ]이전·다음 강의
Space영상 재생·정지
j k다음·이전 section
n p다음·이전 slide
b강의 목록 접기·펴기
t맨 위
/강의 검색
Esc닫기