09 Pipeline Hazards

Post on 04-Jun-2018

218 views 0 download

Transcript of 09 Pipeline Hazards

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 1/29

Pipeline Hazards

(Second Edition: Sections 6.4-6.6Fourth Edition: Sections 4.7, 4.8from Dr. Andrea Di Blas’ notes

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 2/29

CMPE 110 – Spring 2011 – J. Ferguson 

Outline•  Structural hazards: lack of resources

•  Data hazards: data dependencies betweeninstructions

•  Control hazards: branches

8 - 2

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 3/29

CMPE 110 – Spring 2011 – J. Ferguson 

Hazards

8 - 3

Hazards are situations that prevent the nextinstruction from moving through the pipeline

•  Structural Hazards are caused by conflicts inresource usage.

•  Data Hazards are caused by instructions thatrequire results from a previous (uncompleted)instruction.

•  Control Hazards are caused by instructions thatchange the Program Counter.

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 4/29

CMPE 110 – Spring 2011 – J. Ferguson 

Structural Hazards•  Overlapping instructions requires the

duplication of resources so that not all

instruction combinations are possible.•  The pipeline is said to have a structural

hazard if some combinations ofinstructions can not be accommodated.

9 - 4

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 5/29

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 6/29

CMPE 110 – Spring 2011 – J. Ferguson  9 - 6

When a structural hazard occurs the pipeline is stalled. The

latter of the stages requiring the resource does not advancewhile the resource is used by the earlier instruction.

bubble

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 7/29

CMPE 110 – Spring 2011 – J. Ferguson 

Stalls represented by a

“bubble” in the pipeline

9 - 7

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 8/29

CMPE 110 – Spring 2011 – J. Ferguson 

Data Hazards•  Data hazards occur when an instruction reads data

before it is written by a previous instruction.

•  Examplesadd $8, $7, $6 sub $9, $8, $5 -----------------

lw $7, 16($5) ori $8, $4, $7 

9 - 8

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 9/29

CMPE 110 – Spring 2011 – J. Ferguson  9 - 9

The next 2 instructions after the sub will getthe “old” value in $2 (assuming that written datainto registers will be read in same cycle).

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 10/29

CMPE 110 – Spring 2011 – J. Ferguson 

Forwarding or Bypassing

9 - 10

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 11/29

CMPE 110 – Spring 2011 – J. Ferguson 

Actual Forwarding

9 - 11

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 12/29

CMPE 110 – Spring 2011 – J. Ferguson 

Forwarding on DataPath

9 - 12

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 13/29

CMPE 110 – Spring 2011 – J. Ferguson 

More Detail (Forwarding Unit)

9 - 13

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 14/29

CMPE 110 – Spring 2011 – J. Ferguson 

Forwarding Unit logic

9 - 14

If((EX/MEM.Rdest = ID/EX.Rs) && EX/MEM.WB)Forward EX/MEM.ALUresult to Rs input of ALU

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 15/29

CMPE 110 – Spring 2011 – J. Ferguson 

Why not always forward from

EX/MEM buffer?

9 - 15

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 16/29

CMPE 110 – Spring 2011 – J. Ferguson 

Summary of “forwarded” hazards

•  “ALU to ALU” hazards:

•  Ifadd $3, $4, $8 add $6, $3, $8 Take register value from the EX/MEM pipeline register

•  Ifadd $3, $4, $8 a “non-dependent” instruction add $6, $3, $8

 Take register value from the MEM/WB pipeline register

9 - 16

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 17/29

CMPE 110 – Spring 2011 – J. Ferguson 

Store instruction data

dependency

9 - 17

New register value become available cc4, not cc3

Forwarding doesn’t work for this instruction!

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 18/29

CMPE 110 – Spring 2011 – J. Ferguson 

Load instruction data dependency

must stall…

9 - 18

… and be forwarded!

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 19/29

CMPE 110 – Spring 2011 – J. Ferguson 

What happens “in” the Bubble?

9 - 19

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 20/29

CMPE 110 – Spring 2011 – J. Ferguson 

Hazard Detection Unit (or Pipeline Interlock)

9 - 20

The HDU checks if EX instruction is a load and the EXinstruction destination register is source register in the IDinstruction. If so, it stalls the IF and ID stages for onecycle.

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 21/29

CMPE 110 – Spring 2011 – J. Ferguson 

Load Data Hazards adds a ClockCycle to the Pipeline

•  How much does this slow down the computer?–  One extra clock cycle for each instruction

– 

Is that a lot?•  Software solution: Whenever possible, reorderinstructions so that the next instruction is notdependent on the destination of the Loadinstruction.

9 - 21

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 22/29

CMPE 110 – Spring 2011 – J. Ferguson 

Example: InstructionReordering

9 - 22

Loop addi $5, $5, 4

lw $8, 0($5)add $7, $8, $9sw $7, 0($4)addi $4, $4, 4

bne $4, $10, Loop

Loop addi $5, $5, 4

lw $8, 0($5)addi $4, $4, 4add $7, $8, $9sw $7, -4($4)

bne $4, $10, Loop

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 23/29

CMPE 110 – Spring 2011 – J. Ferguson 

Control Hazards

9 - 23

Control instructions may change the flow of thenext instruction to be executed. We have thatevaluation in the EX stage.

Simple solution is to stall the pipeline until branch resolved

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 24/29

CMPE 110 – Spring 2011 – J. Ferguson 

Partial Solution:

Resolve Branch Sooner

9 - 24

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 25/29

CMPE 110 – Spring 2011 – J. Ferguson 

From 3 to 1 stage bubble

9 - 25

Must “flush” IF to get rid of bubble.

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 26/29

CMPE 110 – Spring 2011 – J. Ferguson 

Second Improvement:Speculative Execution

9 - 26

Start on next instruction, flush only if needed

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 27/29

CMPE 110 – Spring 2011 – J. Ferguson 

MIPS approach: Delayed Branch

•  Always execute the next instruction after thebranch.

•  Compiler reorders instructions to put useful

instruction after the branch (one that must beexecuted anyway).

•  If no useful instruction: put NOP after thebranch.

9 - 27

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 28/29

CMPE 110 – Spring 2011 – J. Ferguson 

Example of Delayed Branch

9 - 28

Loop addi $5, $5, 4

lw $8, 0($5)addi $4, $4, 4add $7, $8, $9sw $7, -4($4)bne $4, $10, Loop….

Loop addi $5, $5, 4

lw $8, 0($5)addi $4, $4, 4add $7, $8, $9 bne $4, $10, Loopsw $7, -4($4)….

8/13/2019 09 Pipeline Hazards

http://slidepdf.com/reader/full/09-pipeline-hazards 29/29

CMPE 110 – Spring 2011 – J. Ferguson 

Check our understanding

• 

Allowing jumps, branches and ALU instructions totake fewer stages (and cycles) will increaseperformance under all circumstances.

•  Allowing some instructions to take fewer cyclesdoes not help, because only one instruction will be

executed per clock cycle.•  Instead of trying to take fewer cycles, perhaps we

can explore making the pipeline longer by thecycles shorter.

8 - 29

Observation: not all instructions need allfive cycles of the pipeline. Which are correctif you ignore all hazards?