Computer Systems Lecture 15 Pipelining and Hazards CS 230 - Spring - - PowerPoint PPT Presentation

computer systems
SMART_READER_LITE
LIVE PREVIEW

Computer Systems Lecture 15 Pipelining and Hazards CS 230 - Spring - - PowerPoint PPT Presentation

CS 230 Introduction to Computers and Computer Systems Lecture 15 Pipelining and Hazards CS 230 - Spring 2020 3-1 Pipelining CS 230 - Spring 2020 3-2 ISA Requirements for Pipelining Constant length instructions (fetch, decode)


slide-1
SLIDE 1

CS 230 - Spring 2020 3-1

CS 230 – Introduction to Computers and Computer Systems Lecture 15 – Pipelining and Hazards

slide-2
SLIDE 2

CS 230 - Spring 2020 3-2

Pipelining

slide-3
SLIDE 3

CS 230 - Spring 2020 3-3

ISA Requirements for Pipelining

 Constant length instructions (fetch, decode)  Few instruction formats, source fields same

 register operands can be fetched while decoding

 Memory operands only in load and store

 one memory access per instruction  compute address during execute  no separate stage needed

 MIPS has all of this

slide-4
SLIDE 4

CS 230 - Spring 2020 3-4

Pipeline Diagram

 Shows what the MIPS processor is doing for

each clock cycle in the program

 Used for measuring CPI and detecting stalls

 Example:

addi $3, $0, 0x88 IF ID EX MEM WB sub $4, $7, $17 IF ID EX MEM WB add $5, $3, $9 IF ID EX MEM WB

time program execution

cycle 1 cycle 2 cycle 3 cycle 4 cycle 5 cycle 6 cycle 7

slide-5
SLIDE 5

CS 230 - Spring 2020 3-5

Pipeline Hazards

 Instructions are not completely independent  A pipeline hazard is a condition that blocks

pipelined flow

 data hazard: dependency between instructions

 need to wait for data read/write

 control hazard: dependency between instructions

 control depends on previous instruction

slide-6
SLIDE 6

CS 230 - Spring 2020 3-6

Data Hazard

 Consider the following MIPS code

addi $2, $0, 10 add $3, $2, $2

 The add depends on the result of the addi  We say there is a data hazard between these

instructions

slide-7
SLIDE 7

CS 230 - Spring 2020 3-7

Data Hazard

 The ID stage gets the values from registers  That means at time A the add instruction got

the old value in $2

 At time B the WB stage of the addi has put 10

into $2

 But it’s too late! The add used the old value of $2

addi $2, $0, 10 IF ID EX MEM WB add $3, $0, $2 IF ID EX MEM WB

A B

slide-8
SLIDE 8

CS 230 - Spring 2020 3-8

Forwarding

 Have any stage where the result is ready can

forward the result to the stage that needs it

 Here the result is ready in the EX of addi and

gets forwarded to the EX of add

 We draw an arrow in our pipeline diagram to

indicate forwarding

 Can only forward one cycle forwards in time

 Not 2 or more cycles, and not back in time

addi $2, $0, 10 IF ID EX MEM WB add $3, $0, $2 IF ID EX MEM WB

slide-9
SLIDE 9

CS 230 - Spring 2020 3-9

Stalling

 Forwarding it not always enough

 Sometimes the data is not ready in time

 An instruction may cause a stall to wait for data

to be ready at a future cycle, then forward

 Stalls are shown as bubbles in pipeline diagrams

 indicate the loss of an instruction’s worth of time

lw $4, 0($5) IF ID EX MEM WB add $6, $4, $3 IF ID EX MEM WB

slide-10
SLIDE 10

CS 230 - Spring 2020 3-10

Forwarding Special Case

 Never forward from WB  WB happens before ID within same cycle

 ID will get values from WB in same cycle without

needing to forward

 Corollary: two instructions with a data hazard

can have at most one instruction between them

 Two or more and it is not a data hazard

 Stall bubbles count as a potential instruction

addi $2, $0, 10 IF ID EX MEM WB sub $4, $5, $6 IF ID EX MEM WB sub $7, $8, $9 IF ID EX MEM WB add $3, $0, $2 IF ID EX MEM WB

slide-11
SLIDE 11

Forwarding “Connections” Diagram

CS 230 - Spring 2020 3-11

slide-12
SLIDE 12

CS 230 - Spring 2020 3-12

Control Hazard

 Conditional branches determine control flow

 fetching next instruction depends on outcome  how does the IF stage know which instruction to load?

 In MIPS pipeline

 add hardware to ID stage to check branch early  know if taken or not taken by end of ID stage

 forces one stall after every branch instruction

unless we can find a way to avoid this?

slide-13
SLIDE 13

CS 230 - Spring 2020 3-13

Control Hazard

 Example

 consider the below code and assume $2 = 0

beq $2, $0, x sub $6, $7, $8 x: add $3, $4, $5

 don’t know the branch is taken until the ID of the beq

beq $2, $0, x IF ID EX MEM WB x: add $3, $4, $5 IF ID EX MEM WB

slide-14
SLIDE 14

CS 230 - Spring 2020 3-14

Branch Prediction

 We can sometimes avoid stalls from control

hazards with branch prediction

 processor guesses if branch is taken

 guess wrong and wrong guess turns into a stall  guess correct and no stall at all

 how to guess?

 lots of methods  “backwards taken, forwards not taken”

convert branch label to number of instruction to skip (branch offset)

if branch offset negative: guess branch taken

if branch offset positive: guess branch not taken

 all branch instructions are control hazards

slide-15
SLIDE 15

CS 230 - Spring 2020 3-15

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 addi $2, $0, 2 lw $3, 0($1) add $4, $2, $3 x: addi $2, $2, -1 bne $2, $0, x jr $31

slide-16
SLIDE 16

CS 230 - Spring 2020 3-16

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 addi $2, $0, 2 lw $3, 0($1) add $4, $2, $3 x: addi $2, $2, -1 bne $2, $0, x jr $31

control hazard data hazard $2 data hazard $1 data hazard $2 data hazard $3

slide-17
SLIDE 17

CS 230 - Spring 2020 3-17

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 IF ID EX MEM WB addi $2, $0, 2 IF ID EX MEM WB

slide-18
SLIDE 18

CS 230 - Spring 2020 3-18

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 IF ID EX MEM WB addi $2, $0, 2 IF ID EX MEM WB lw $3, 0($1) IF ID EX MEM WB

slide-19
SLIDE 19

CS 230 - Spring 2020 3-19

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 IF ID EX MEM WB addi $2, $0, 2 IF ID EX MEM WB lw $3, 0($1) IF ID EX MEM WB add $4, $2, $3 IF ID EX MEM WB

slide-20
SLIDE 20

CS 230 - Spring 2020 3-20

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 IF ID EX MEM WB addi $2, $0, 2 IF ID EX MEM WB lw $3, 0($1) IF ID EX MEM WB add $4, $2, $3 IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB

slide-21
SLIDE 21

CS 230 - Spring 2020 3-21

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 IF ID EX MEM WB addi $2, $0, 2 IF ID EX MEM WB lw $3, 0($1) IF ID EX MEM WB add $4, $2, $3 IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB bne $2, $0, x IF ID EX MEM WB

slide-22
SLIDE 22

CS 230 - Spring 2020 3-22

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 IF ID EX MEM WB addi $2, $0, 2 IF ID EX MEM WB lw $3, 0($1) IF ID EX MEM WB add $4, $2, $3 IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB bne $2, $0, x IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB

slide-23
SLIDE 23

CS 230 - Spring 2020 3-23

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 IF ID EX MEM WB addi $2, $0, 2 IF ID EX MEM WB lw $3, 0($1) IF ID EX MEM WB add $4, $2, $3 IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB bne $2, $0, x IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB bne $2, $0, x IF ID EX MEM WB

slide-24
SLIDE 24

CS 230 - Spring 2020 3-24

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 IF ID EX MEM WB addi $2, $0, 2 IF ID EX MEM WB lw $3, 0($1) IF ID EX MEM WB add $4, $2, $3 IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB bne $2, $0, x IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB bne $2, $0, x IF ID EX MEM WB jr $31 IF ID EX MEM WB

slide-25
SLIDE 25

CS 230 - Spring 2020 3-25

Example

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $1, $0, 0xA0 IF ID EX MEM WB addi $2, $0, 2 IF ID EX MEM WB lw $3, 0($1) IF ID EX MEM WB add $4, $2, $3 IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB bne $2, $0, x IF ID EX MEM WB x: addi $2, $2, -1 IF ID EX MEM WB bne $2, $0, x IF ID EX MEM WB jr $31 IF ID EX MEM WB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

17 cycles total

slide-26
SLIDE 26

CS 230 - Spring 2020 3-26

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 lw $2, 4($7) beq $7, $0, e y: addi $7, $7, -150 slt $3, $7, $0 beq $3, $0, y e: jr $31

slide-27
SLIDE 27

CS 230 - Spring 2020 3-27

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 lw $2, 4($7) beq $7, $0, e y: addi $7, $7, -150 slt $3, $7, $0 beq $3, $0, y e: jr $31

data hazard $7 control hazard control hazard data hazard $3 data hazard $7 data hazard $7

slide-28
SLIDE 28

CS 230 - Spring 2020 3-28

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB

slide-29
SLIDE 29

CS 230 - Spring 2020 3-29

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB beq $7, $0, e IF ID EX MEM WB

slide-30
SLIDE 30

CS 230 - Spring 2020 3-30

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB beq $7, $0, e IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB

slide-31
SLIDE 31

CS 230 - Spring 2020 3-31

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB beq $7, $0, e IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB

slide-32
SLIDE 32

CS 230 - Spring 2020 3-32

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB beq $7, $0, e IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB beq $3, $0, y IF ID EX MEM WB

slide-33
SLIDE 33

CS 230 - Spring 2020 3-33

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB beq $7, $0, e IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB beq $3, $0, y IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB

slide-34
SLIDE 34

CS 230 - Spring 2020 3-34

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB beq $7, $0, e IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB beq $3, $0, y IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB

slide-35
SLIDE 35

CS 230 - Spring 2020 3-35

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB beq $7, $0, e IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB beq $3, $0, y IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB beq $3, $0, y IF ID EX MEM WB

slide-36
SLIDE 36

CS 230 - Spring 2020 3-36

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB beq $7, $0, e IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB beq $3, $0, y IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB beq $3, $0, y IF ID EX MEM WB jr $31 IF ID EX MEM WB

slide-37
SLIDE 37

CS 230 - Spring 2020 3-37

Try it Yourself

Identify the hazards, and draw a pipeline diagram for the below MIPS code. How many cycles does it take to execute?

addi $7, $0, 0xF8 IF ID EX MEM WB lw $2, 4($7) IF ID EX MEM WB beq $7, $0, e IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB beq $3, $0, y IF ID EX MEM WB y: addi $7, $7, -150 IF ID EX MEM WB slt $3, $7, $0 IF ID EX MEM WB beq $3, $0, y IF ID EX MEM WB jr $31 IF ID EX MEM WB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

17 cycles total

slide-38
SLIDE 38

CS 230 - Spring 2020 3-38

Pipelining Summary

 Pipelining improves performance by increasing

instruction throughput

 multiple instructions executed in parallel  unchanged latency

 an instruction still take 5 cycles to execute

 Subject to hazards  Instruction set affects pipeline complexity

 different instructions cause stalls in different ways  other processors may have different ways of pipelining