mips pipeline

51
Hakim Weatherspoon CS 3410, Spring 2012 Computer Science Cornell University MIPS Pipeline See P&H Chapter 4.6

Upload: china

Post on 24-Feb-2016

37 views

Category:

Documents


0 download

DESCRIPTION

MIPS Pipeline. See P&H Chapter 4.6. A Processor. Review: Single cycle processor. memory. register file. inst. alu. +4. +4. addr. =?. PC. d in. d out. control. cmp. offset. memory. new pc. target. imm. extend. Review: Single Cycle Processor. Advantages - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: MIPS Pipeline

Hakim WeatherspoonCS 3410, Spring 2012

Computer ScienceCornell University

MIPS Pipeline

See P&H Chapter 4.6

Page 2: MIPS Pipeline

2

A Processor

alu

PC

imm

memory

memory

din dout

addr

target

offset cmpcontrol

=?

new pc

registerfile

inst

extend

+4 +4

Review: Single cycle processor

Page 3: MIPS Pipeline

3

Review: Single Cycle ProcessorAdvantages• Single Cycle per instruction make logic and clock simple

Disadvantages• Since instructions take different time to finish, memory

and functional unit are not efficiently utilized.• Cycle time is the longest delay.

– Load instruction• Best possible CPI is 1

Page 4: MIPS Pipeline

4

Single Cycle vs Pipelined Processor

See: P&H Chapter 4.5

Page 5: MIPS Pipeline

5

The KidsAlice

Bob

They don’t always get along…

Page 6: MIPS Pipeline

6

The Bicycle

Page 7: MIPS Pipeline

7

The Materials

Saw Drill

Glue Paint

Page 8: MIPS Pipeline

8

The InstructionsN pieces, each built following same sequence:

Saw Drill Glue Paint

Page 9: MIPS Pipeline

9

Design 1: Sequential Schedule

Alice owns the roomBob can enter when Alice is finishedRepeat for remaining tasksNo possibility for conflicts

Page 10: MIPS Pipeline

10

Elapsed Time for Alice: 4Elapsed Time for Bob: 4Total elapsed time: 4*NCan we do better?

Sequential Performancetime1 2 3 4 5 6 7 8 …

Latency:Throughput:Concurrency:

Page 11: MIPS Pipeline

11

Design 2: Pipelined DesignPartition room into stages of a pipeline

One person owns a stage at a time4 stages4 people working simultaneouslyEveryone moves right in lockstep

AliceBobCarolDave

Page 12: MIPS Pipeline

12

Pipelined Performancetime1 2 3 4 5 6 7…

Latency:Throughput:Concurrency:

Page 13: MIPS Pipeline

13

LessonsPrinciple:

Throughput increased by parallel execution

Pipelining:• Identify pipeline stages• Isolate stages from each other• Resolve pipeline hazards (Thursday)

Page 14: MIPS Pipeline

14

A Processor

alu

PC

imm

memory

memory

din dout

addr

target

offset cmpcontrol

=?

new pc

registerfile

inst

extend

+4 +4

Review: Single cycle processor

Page 15: MIPS Pipeline

15

Write-BackMemory

InstructionFetch Execute

InstructionDecode

registerfile

control

A Processor

alu

imm

memory

din dout

addr

inst

PC

memory

computejump/branch

targets

new pc

+4

extend

Page 16: MIPS Pipeline

16

Basic PipelineFive stage “RISC” load-store architecture1. Instruction fetch (IF)

– get instruction from memory, increment PC2. Instruction Decode (ID)

– translate opcode into control signals and read registers3. Execute (EX)

– perform ALU operation, compute jump/branch targets4. Memory (MEM)

– access memory if needed5. Writeback (WB)

– update register file

Page 17: MIPS Pipeline

17

Time Graphs1 2 3 4 5 6 7 8 9

Clock cycle

Latency:Throughput:Concurrency:

IF ID EX MEM WB

IF ID EX MEM WB

IF ID EX MEM WB

IF ID EX MEM WB

IF ID EX MEM WB

Page 18: MIPS Pipeline

18

Principles of Pipelined ImplementationBreak instructions across multiple clock cycles

(five, in this case)

Design a separate stage for the execution performed during each clock cycle

Add pipeline registers (flip-flops) to isolate signals between different stages

Page 19: MIPS Pipeline

19

Pipelined Processor

See: P&H Chapter 4.6

Page 20: MIPS Pipeline

20

Write-BackMemory

InstructionFetch Execute

InstructionDecode

extend

registerfile

control

Pipelined Processor

alu

memory

din dout

addrPC

memory

newpc

inst

IF/ID ID/EX EX/MEM MEM/WB

imm

BA

ctrl

ctrl

ctrl

BD D

M

computejump/branch

targets

+4

Page 21: MIPS Pipeline

21

IFStage 1: Instruction Fetch

Fetch a new instruction every cycle• Current PC is index to instruction memory• Increment the PC at end of cycle (assume no branches for now)

Write values of interest to pipeline register (IF/ID)• Instruction bits (for later decoding)• PC+4 (for later computing branch targets)

Page 22: MIPS Pipeline

22

IF

PC

instructionmemory

newpc

inst

addr mc

00 = read word

1

IF/ID

WE 1

Rest

of p

ipel

ine

+4

PC+4

pcsel

pcregpcrel

pcabs

Page 23: MIPS Pipeline

23

IDStage 2: Instruction Decode

On every cycle:• Read IF/ID pipeline register to get instruction bits• Decode instruction, generate control signals• Read from register file

Write values of interest to pipeline register (ID/EX)• Control information, Rd index, immediates, offsets, …• Contents of Ra, Rb• PC+4 (for computing branch targets later)

Page 24: MIPS Pipeline

24

ID

ctrl

ID/EX

Rest

of p

ipel

ine

PC+4

inst

IF/ID

PC+4

Stag

e 1:

Inst

ructi

on F

etch

registerfile

WERd

Ra Rb

DB

A

BA

extend imm

decode

result

dest

Page 25: MIPS Pipeline

25

EXStage 3: Execute

On every cycle:• Read ID/EX pipeline register to get values and control bits• Perform ALU operation• Compute targets (PC+4+offset, etc.) in case this is a branch• Decide if jump/branch should be taken

Write values of interest to pipeline register (EX/MEM)• Control information, Rd index, …• Result of ALU operation• Value in case this is a memory store instruction

Page 26: MIPS Pipeline

26

Stag

e 2:

Inst

ructi

on D

ecod

e

pcrel

pcabs

EX

ctrl

EX/MEM

Rest

of p

ipel

ine

BD

ctrl

ID/EX

PC+4

BA

alu

j+||

branch?

imm

pcselpcreg

targ

et

Page 27: MIPS Pipeline

27

MEMStage 4: Memory

On every cycle:• Read EX/MEM pipeline register to get values and control bits• Perform memory load/store if needed

– address is ALU result

Write values of interest to pipeline register (MEM/WB)• Control information, Rd index, …• Result of memory operation• Pass result of ALU operation

Page 28: MIPS Pipeline

28

MEM

ctrl

MEM/WB

Rest

of p

ipel

ine

Stag

e 3:

Exe

cute

MD

ctrl

EX/MEM

BD

memory

din dout

addr

mctarg

et

branch?pcsel

pcrel

pcabs

pcreg

Page 29: MIPS Pipeline

29

WBStage 5: Write-back

On every cycle:• Read MEM/WB pipeline register to get values and control bits• Select value and write to register file

Page 30: MIPS Pipeline

30

WBSt

age

4: M

emor

y

ctrl

MEM/WB

MD

result

dest

Page 31: MIPS Pipeline

31IF/ID

+4

ID/EX EX/MEM MEM/WB

mem

din dout

addrinst

PC+4

OP

BA

Rd

BD

MD

PC+4

imm

OP

Rd

OP

Rd

PC

instmem

Rd

Ra Rb

DB

A

Page 32: MIPS Pipeline

32

Administrivia

HW2 due today• Fill out Survey online. Receive credit/points on homework for survey.• Survey is anonymous

Project1 (PA1) due week after prelim• Continue working diligently. Use design doc momentum

Save your work!• Save often. Verify file is non-zero. Periodically save to Dropbox, email.• Beware of MacOSX 10.5 (leopard) and 10.6 (snow-leopard)

Use your resources• Lab Section, Piazza.com, Office Hours, Homework Help Session,• Class notes, book, Sections, CSUGLab

Page 33: MIPS Pipeline

33

Administrivia

Prelim1: next Tuesday, February 28th in evening• We will start at 7:30pm sharp, so come early• Prelim Review: This Wed / Fri, 3:30-5:30pm, in 155 Olin• Closed Book

• Cannot use electronic device or outside material• Practice prelims are online in CMS• Material covered everything up to end of this week

• Appendix C (logic, gates, FSMs, memory, ALUs) • Chapter 4 (pipelined [and non-pipeline] MIPS processor with

hazards)• Chapters 2 (Numbers / Arithmetic, simple MIPS instructions)• Chapter 1 (Performance)• HW1, HW2, Lab0, Lab1, Lab2

Page 34: MIPS Pipeline

34

Administrivia

Check online syllabus/schedule •http://www.cs.cornell.edu/Courses/CS3410/2012sp/schedule.htmlSlides and Reading for lecturesOffice HoursHomework and Programming AssignmentsPrelims (in evenings): • Tuesday, February 28th • Thursday, March 29th • Thursday, April 26th

Schedule is subject to change

Page 35: MIPS Pipeline

35

Collaboration, Late, Re-grading Policies

“Black Board” Collaboration Policy•Can discuss approach together on a “black board”•Leave and write up solution independently•Do not copy solutions

Late Policy•Each person has a total of four “slip days”•Max of two slip days for any individual assignment•Slip days deducted first for any late assignment, cannot selectively apply slip days•For projects, slip days are deducted from all partners •20% deducted per day late after slip days are exhausted

Regrade policy•Submit written request to lead TA,

and lead TA will pick a different grader •Submit another written request,

lead TA will regrade directly •Submit yet another written request for professor to regrade.

Page 36: MIPS Pipeline

36

Example: Sample Code (Simple)Assume eight-register machineRun the following code on a pipelined datapath

add 3 1 2 ; reg 3 = reg 1 + reg 2 nand 6 4 5 ; reg 6 = ~(reg 4 & reg 5) lw 4 20 (2) ; reg 4 = Mem[reg2+20] add 5 2 5 ; reg 5 = reg 2 + reg 5 sw 7 12(3) ; Mem[reg3+12] = reg 7

Slides thanks to Sally McKee

Page 37: MIPS Pipeline

37

Example: : Sample Code (Simple)add r3, r1, r2; nand r6, r4, r5; lw r4, 20(r2); add r5, r2, r5; sw r7, 12(r3);

Page 38: MIPS Pipeline

38

Time Graphs1 2 3 4 5 6 7 8 9

add

nand

lw

add

sw

Clock cycle

Latency:Throughput:Concurrency:

IF ID EX MEM WB

IF ID EX MEM WB

IF ID EX MEM WB

IF ID EX MEM WB

IF ID EX MEM WB

Page 39: MIPS Pipeline

39

0:add1:nand2:lw3:add4:sw

r0r1r2r3r4r5r6r7

0369

12187

4122

IF/ID

+4

ID/EX EX/MEM MEM/WB

mem

din dout

addrinst

PC+4

OP

BA

Rd

BD

MD

PC+4

imm

OP

Rd

OP

Rd

PC

instmem

77

add r3, r1, r2nand r6, r4, r5 add r3, r1, r2lw r4, 20(r2) nand r6, r4, r5 add r3, r1, r2add r5, r2, r5 lw r4, 20(r2) nand r6, r4, r5 add r3, r1, r2sw r7, 12(r3) add r5, r2, r5 lw r4, 20(r2) nand r6, r4, r5 add r3, r1, r2sw r7, 12(r3) add r5, r2, r5 lw r4, 20(r2) nand r6, r4, r5sw r7, 12(r3) add r5, r2, r5 lw r4, 20(r2)sw r7, 12(r3) add r5, r2, r5 sw r7, 12(r3)

Rd

Ra Rb

DB

A

Page 40: MIPS Pipeline

40

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

op

Rt

imm

valB

valA

PC+4PC+4target

ALUresult

op

dest

valB

op

dest

ALUresult

mdata

instruction

0

R2

R3

R4

R5

R1

R6

R0

R7

regAregB

Bits 26-31

data

dest

IF/ID ID/EX EX/MEM MEM/WB

extend

MUX

Rd

Page 41: MIPS Pipeline

41

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

nop

0

0

0

000

0

nop

0

0

nop

0

0

0

0

nop

912187

36

41

0

22

R2

R3

R4

R5

R1

R6

R0

R7

Bits 26-31

data

dest

InitialState

IF/ID ID/EX EX/MEM MEM/WB

extend

0MUX

0

Page 42: MIPS Pipeline

42

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

nop

0

0

0

040

0

nop

0

0

nop

0

0

0

0

add 3 1 2

912187

36

41

0

22

R2

R3

R4

R5

R1

R6

R0

R7

Bits 26-31

data

dest

Fetch: add 3 1 2

add 3 1 2

Time: 1 IF/ID ID/EX EX/MEM MEM/WB

extend

0MUX

0

Page 43: MIPS Pipeline

43

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

add

3

9

36

480

0

nop

0

0

nop

0

0

0

0nand 6 4 5

912187

36

41

0

22

R2

R3

R4

R5

R1

R6

R0

R7

12

Bits 26-31

data

dest

Fetch: nand 6 4 5

nand 6 4 5 add 3 1 2

Time: 2 IF/ID ID/EX EX/MEM MEM/WB

extend

2MUX

3

Page 44: MIPS Pipeline

44

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

nand

6

7

18

8124

45

add

3

9

nop

0

0

0

0lw 4 20(2)

912187

36

41

0

22

R2

R3

R4

R5

R1

R6

R0

R7

45

Bits 26-31

data

dest

Fetch: lw 4 20(2)

lw 4 20(2) nand 6 4 5 add 3 1 2

Time: 3

36

9

3

IF/ID ID/EX EX/MEM MEM/WB

extend

5MUX

6 32

Page 45: MIPS Pipeline

45

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

lw

20

18

9

12168

-3

nand

6

7

add

3

45

0

0add 5 2 5

912187

36

41

0

22

R2

R3

R4

R5

R1

R6

R0

R7

24

Bits 26-31

data

dest

Fetch: add 5 2 5

add 5 2 5 lw 4 20(2) nand 6 4 5 add 3 1 2

Time: 4

18

7

6

45

3

IF/ID ID/EX EX/MEM MEM/WB

extend

4MUX

0 65

Page 46: MIPS Pipeline

46

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

add

5

7

9

162012

29

lw

4

18

nand

6

-3

0

0sw 7 12(3)

945187

36

41

0

22

R2

R3

R4

R5

R1

R6

R0

R7

25

Bits 26-31

data

dest

Fetch: sw 7 12(3)

sw 7 12(3) add 5 2 5 lw 4 20 (2) nand 6 4 5 add 3 1 2

Time: 5

9

20

4

-3

6

45

3

IF/ID ID/EX EX/MEM MEM/WB

extend

5MUX

5 04

Page 47: MIPS Pipeline

47

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

sw

12

22

45

20 16

16

add

5

7

lw

4

29

99

09

45187

36

-3

0

22

R2

R3

R4

R5

R1

R6

R0

R7

37

Bits 26-31

data

dest

No moreinstructions

sw 7 12(3) add 5 2 5 lw 4 20(2) nand 6 4 5

Time: 6

9

7

5

29

4

-3

6

IF/ID ID/EX EX/MEM MEM/WB

extend

7MUX

0 55

Page 48: MIPS Pipeline

48

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

20

57

sw

7

22

add

5

16

0

09

45997

36

-3

0

22

R2

R3

R4

R5

R1

R6

R0

R7

Bits 26-31

data

dest

No moreinstructions

nop nop sw 7 12(3) add 5 2 5 lw 4 20(2)

Time: 7

45

7

12

16

5

99

4

IF/ID ID/EX EX/MEM MEM/WB

extend

MUX

07

Page 49: MIPS Pipeline

49

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

sw

7

57

0

9

459916

36

-3

0

22

R2

R3

R4

R5

R1

R6

R0

R7

Bits 26-31

data

dest

No moreinstructions

nop nop nop sw 7 12(3) add 5 2 5

Time: 8

2257

22

16

5

Slides thanks to Sally McKee

IF/ID ID/EX EX/MEM MEM/WB

extend

MUX

Page 50: MIPS Pipeline

50

PC Instmem

Regi

ster

file

MUXA

LU

MUX

4

Datamem

+

MUX

Bits 11-15Bits 16-20

9

459916

36

-3

0

22

R2

R3

R4

R5

R1

R6

R0

R7

Bits 21-23

data

dest

No moreinstructions

nop nop nop nop sw 7 12(3)

Time: 9 IF/ID ID/EX EX/MEM MEM/WB

extend

MUX

Page 51: MIPS Pipeline

51

Pipelining RecapPowerful technique for masking latencies• Logically, instructions execute one at a time• Physically, instructions execute in parallel

– Instruction level parallelism

Abstraction promotes decoupling• Interface (ISA) vs. implementation (Pipeline)