L21-sb-pipelineI.ppt

advertisement

inst.eecs.berkeley.edu/~cs61c

CS61C : Machine Structures

Lecture #21 CPU Design: Pipelining to

Improve Performance

2007-7-31

Scott Beamer, Instructor

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (1)

Review:

Single cycle datapath

° 5 steps to design a processor

• 1. Analyze instruction set

 datapath requirements

• 2. Select set of datapath components & establish clock methodology

• 3. Assemble datapath meeting the requirements

• 4. Analyze implementation of each instruction to determine setting of control points that effects the register transfer.

Processor

• 5. Assemble the control logic

Control

° Control is the hard part

Memory

Input

° MIPS makes that easier

Datapath

• Instructions same size

• Source registers always in same place

• Immediates same size, location

• Operations always on registers/immediates

Output

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (2)

An Abstract View of the Critical Path

Ideal

Instruction

Memory

Instruction

Address

Rd

5

Critical Path (Load Instruction) =

Delay clock through PC (FFs) +

Instruction Memory’s Access Time +

Register File’s Access Time, +

ALU to Perform a 32-bit Add +

Data Memory Access Time +

Stable Time for Register File Write

Instruction

Rs

5

Rt

5

(Assumes a fast controller)

32

Rw Ra Rb

A

32

32

Data

Addr

Register

File

B

Ideal

Data

Memory

Data

In clk clk 32 clk

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (3)

Processor Performance

• Can we estimate the clock rate (frequency) of our single-cycle processor? We know:

• 1 cycle per instruction

• lw is the most demanding instruction.

• Assume approximate delays for major pieces of the datapath:

 Instr. Mem, ALU, Data Mem : 2ns each, regfile 1ns

 Instruction execution requires: 2 + 1 + 2 + 2 + 1 = 8ns

 

125 MHz

• What can we do to improve clock rate?

• Will this improve performance as well?

• We want increases in clock rate to result in programs executing quicker.

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (4)

Ways to Improve Clock Frequency

• Smaller Process Size

• Smallest feature possible in silicon fabrication

• Smaller process is faster because of EE reasons, and is smaller so things are closer

• Optimize Logic

• Re-arrange CL to be faster

• Sometimes more logic can be used to reduce delay

• Parallel

• Do more at once later…

• Cut Down Length of Critical Path

• Inserting registers (pipelining) to break up CL

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (5)

Gotta Do Laundry

° Ann, Brian, Cathy, Dave each have one load of clothes to wash, dry, fold, and put away

° Washer takes 30 minutes

° Dryer takes 30 minutes

° “Folder” takes 30 minutes

° “Stasher” takes 30 minutes to put clothes into drawers

CS61C L21 CPU Design : Pipelining to Improve Performance (6)

A B C D

Beamer, Summer 2007 © UCB

Sequential Laundry

6 PM 7 8 9 10 11 12 1 2 AM

T a s k

O r d e r

B

C

D

A

30 30 30 30 30 30 30 30 30 30 30 30 30 30 30

Time

30

Sequential laundry takes

8 hours for 4 loads

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (7)

Pipelined Laundry

6 PM 7 8 9 10 11

3030 30 30 30 30 30

Time s k

T a A

B

C

O e r r d

D

• Pipelined laundry takes

3.5 hours for 4 loads!

12 1 2 AM

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (8)

General Definitions

• Latency : time to completely execute a certain task (delay)

• for example, time to read a sector from disk is disk access time or disk latency

• Throughput : amount of work that can be done over a period of time (rate)

CS61C L21 CPU Design : Pipelining to Improve Performance (9) Beamer, Summer 2007 © UCB

Pipelining Lessons (1/2) s k

T a

O r d e r

6 PM 7 8 9

Time

• Pipelining doesn’t help latency of single task, it helps throughput of entire workload

30 30 30 30 30 30 30

A

• Multiple tasks operating simultaneously using different resources

B

C

• Potential speedup =

Number pipe stages

D

• Time to “ fill ” pipeline and time to “ drain ” it reduces speedup:

2.3X v. 4X in this example

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (10)

T a s k

O r d

Pipelining Lessons (2/2)

6 PM 7

A

30 30 30 30

8

30

9

Time

30 30

• Suppose new

Washer takes 20 minutes, new

Stasher takes 20 minutes. How much faster is pipeline?

B

C

D

• Pipeline rate limited by slowest pipeline stage e r

• Unbalanced lengths of pipe stages reduces speedup

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (11)

Steps in Executing MIPS

1) IFtch : Instruction Fetch, Increment PC

2) Dcd : Instruction Decode, Read Registers

3) Exec :

Mem-ref: Calculate Address

Arith-log: Perform Operation

4) Mem :

Load: Read Data from Memory

Store: Write Data to Memory

5) WB : Write Data Back to Register

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (12)

Pipelined Execution Representation

Time

IFtch Dcd Exec Mem WB

IFtch Dcd Exec Mem WB

IFtch Dcd Exec Mem WB

IFtch Dcd Exec Mem WB

IFtch Dcd Exec Mem WB

IFtch Dcd Exec Mem WB

• Every instruction must take same number of steps, also called pipeline “ stages ”, so some will go idle sometimes

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (13)

Review: Datapath for MIPS rd rs rt imm

ALU

+4

1. Instruction

Fetch

2. Decode/

Register Read

3. Execute 4. Memory

5. Write

Back

• Use datapath figure to represent pipeline

IFtch Dcd Exec Mem WB

I$ Reg

CS61C L21 CPU Design : Pipelining to Improve Performance (14)

D$ Reg

Beamer, Summer 2007 © UCB

Graphical Pipeline Representation

O r d e r s t r.

I n

(In Reg, right half highlight read, left half write)

Time (clock cycles)

Load

Add

Store

I$ Reg

I$ Reg

I$

D$

Reg

Reg

D$ Reg

D$ Reg

Sub

Or

I$ Reg

I$ Reg

D$ Reg

D$ Reg

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (15)

Example

• Suppose 2 ns for memory access, 2 ns for ALU operation, and 1 ns for register file read or write; compute instr rate

• Nonpipelined Execution:

• lw : IF + Read Reg + ALU + Memory + Write

Reg = 2 + 1 + 2 + 2 + 1 = 8 ns

• add : IF + Read Reg + ALU + Write Reg

= 2 + 1 + 2 + 1 = 6 ns

(recall 8ns for single-cycle processor)

• Pipelined Execution:

• Max(IF,Read Reg,ALU,Memory,Write Reg) =

2 ns

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (16)

Administrivia

• Assignments

• HW7 due 8/2

• Proj3 due 8/5

• Midterm Regrades due Wed 8/1

• Logisim in lab is now 2.1.6

• Valerie’s OH on Thursday moved to 10-

11 for this week

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (17)

Pipeline Hazard : Matching socks in later load

6 PM 7 8 9 10 11

Time s k

T a A

O r d e r

B

C

D

E

F

3030 30 30 30 30 30 bubble

12 1 2 AM

A depends on D; stall since folder tied up

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (18)

Problems for Pipelining CPUs

• Limits to pipelining: Hazards prevent next instruction from executing during its designated clock cycle

• Structural hazards : HW cannot support some combination of instructions (single person to fold and put clothes away)

• Control hazards : Pipelining of branches causes later instruction fetches to wait for the result of the branch

• Data hazards : Instruction depends on result of prior instruction still in the pipeline (missing sock)

• These might result in pipeline stalls or

“bubbles” in the pipeline.

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (19)

Structural Hazard #1: Single Memory (1/2)

Time (clock cycles)

I n s t r.

Load

Instr 1

I$ Reg

I$ Reg

D$ Reg

D$ Reg

I$ Reg D$ Reg

Instr 2

O r d e r

Instr 3

Instr 4

I$ Reg

I$ Reg

D$ Reg

D$ Reg

Read same memory twice in same clock cycle

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (20)

Structural Hazard #1: Single Memory (2/2)

• Solution:

• infeasible and inefficient to create second memory

• (We’ll learn about this more next week)

• so simulate this by having two Level 1

Caches (a temporary smaller [of usually most recently used] copy of memory)

• have both an L1 Instruction Cache and an L1 Data Cache

• need more complex hardware to control when both caches miss

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (21)

Structural Hazard #2: Registers (1/2)

Time (clock cycles) s t

I n r.

sw

Instr 1

I$ Reg

I$ Reg

D$ Reg

D$ Reg

I$ Reg D$ Reg

O r d e r

Instr 2

Instr 3

Instr 4

I$ Reg

I$ Reg

D$ Reg

D$ Reg

Can we read and write to registers simultaneously?

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (22)

Structural Hazard #2: Registers (2/2)

• Two different solutions have been used:

1) RegFile access is VERY fast: takes less than half the time of ALU stage

 Write to Registers during first half of each clock cycle

 Read from Registers during second half of each clock cycle

2) Build RegFile with independent read and write ports

• Result: can perform Read and Write during same clock cycle

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (23)

Data Hazards (1/2)

• Consider the following sequence of instructions add $t0 , $t1, $t2 sub $t4, $t0 ,$t3 and $t5, $t0 ,$t6 or $t7, $t0 ,$t8 xor $t9, $t0 ,$t10

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (24)

Data Hazards (2/2)

I n s t r.

Data-flow backward in time are hazards

Time (clock cycles) add $t0 ,$t1,$t2 sub $t4, $t0 ,$t3

IF ID/RF EX MEM WB

I$ Reg

D$

Reg

I$ Reg D$ Reg

I$ Reg D$

O r d e r and $t5, $t0 ,$t6 or $t7, $t0 ,$t8 xor $t9, $t0 ,$t10

I$ Reg

I$ Reg

Reg

D$ Reg

D$ Reg

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (25)

Data Hazard Solution: Forwarding

• Forward result from one stage to another add $t0 ,$t1,$t2

IF ID/RF EX MEM WB

I$ Reg

D$

Reg sub $t4, $t0 ,$t3

I$ Reg D$ Reg and $t5, $t0 ,$t6

I$ Reg D$ Reg or $t7, $t0 ,$t8

I$ Reg D$ Reg xor $t9, $t0 ,$t10

I$ Reg D$ Reg

“ or ” hazard solved by register hardware

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (26)

Data Hazard: Loads (1/4)

• Dataflow backwards in time are hazards lw $t0 ,0($t1)

IF ID/RF EX MEM WB

I$ Reg

D$

Reg sub $t3, $t0 ,$t2

I$ Reg D$ Reg

• Can’t solve all cases with forwarding

• Must stall instruction dependent on load, then forward (more hardware)

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (27)

Data Hazard: Loads (2/4)

• Hardware stalls pipeline

• Called “ interlock ” lw $t0 , 0($t1)

IF ID/RF EX MEM WB

I$ Reg

D$

Reg sub $t3, $t0 ,$t2 and $t5, $t0 ,$t4 or $t7, $t0 ,$t6

I$ Reg bub ble

I$ bub ble

Reg

D$ Reg

D$ Reg bub ble

I$ Reg D$

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (28)

Data Hazard: Loads (3/4)

• Instruction slot after a load is called

“ load delay slot ”

• If that instruction uses the result of the load, then the hardware interlock will stall it for one cycle.

• If the compiler puts an unrelated instruction in that slot, then no stall

• Letting the hardware stall the instruction in the delay slot is equivalent to putting a nop in the slot (except the latter uses more code space)

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (29)

Data Hazard: Loads (4/4)

• Stall is equivalent to nop lw $t0, 0 ($t1)

I$ Reg

D$

Reg nop bub ble bub ble bub ble bub ble bub ble

I$ Reg

D$

Reg sub $t3, $t0 ,$t2 and $t5, $t0 ,$t4 or $t7, $t0 ,$t6

I$ Reg

I$ Reg

D$

Reg

D$

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (30)

Historical Trivia

• First MIPS design did not interlock and stall on load-use data hazard

• Real reason for name behind MIPS:

M icroprocessor without

I nterlocked

P ipeline

S tages

• Word Play on acronym for

Millions of Instructions Per Second, also called MIPS

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (31)

Peer Instruction

A.

Thanks to pipelining, I have reduced the time it took me to wash my shirt.

B.

C.

Longer pipelines are work per stage & a faster clock).

We can always a win rely on compilers to help us avoid data hazards by reordering instrs.

(since less

ABC

0: FFF

1: FF T

2: F T F

3: F TT

4: T FF

5: T F T

6: TT F

7: TTT

CS61C L21 CPU Design : Pipelining to Improve Performance (32) Beamer, Summer 2007 © UCB

Peer Instruction Answer

A. Throughput better, not execution time

B.

“…longer pipelines do usually mean faster clock, but branches cause problems!

C.

“they happen too often & delay too long.”

Forwarding! (e.g, Mem

ALU)

A.

Thanks to pipelining, I have reduced the time it F A L S E

B.

Longer pipelines are always a win (since less F A L S E

C.

We can rely on compilers to help us avoid data F A L S E

ABC

0: FFF

1: FF T

2: F T F

3: F TT

4: T FF

5: T F T

6: TT F

7: TTT

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (33)

Things to Remember

• Optimal Pipeline

• Each stage is executing part of an instruction each clock cycle.

• One instruction finishes during each clock cycle.

• On average, execute far more quickly.

• What makes this work?

• Similarities between instructions allow us to use same stages for all instructions

(generally).

• Each stage takes about the same amount of time as all others: little wasted time.

Beamer, Summer 2007 © UCB CS61C L21 CPU Design : Pipelining to Improve Performance (34)

Download