Docsity

Pipelining in Computer Systems: Overcoming Structural, Data, and Control Hazards, Slides of Computer Science

Partial preview of the text

Download Pipelining in Computer Systems: Overcoming Structural, Data, and Control Hazards and more Slides Computer Science in PDF only on Docsity!

CSE 410Computer Systems

L

t^

Pi

li i

II

Lecture 10 – Pipelining II

Pipelining Goal: Better ThroughputPipelining

Goal: Better Throughput

overlapped

increasingnumber of

overlapped

number ofinstructions

increasing time

sequential

increasing time

3

Pipeline HazardsPipeline

Hazards

•^

Structural hazards– Instructions in different stages need the same

resource, eg, memory controller, arithmetic unit

•^

Data hazards– data not available to perform next operation when

neededneeded

•^

Control hazards– data not available to make branch decision soon

enough

5

Structural HazardsStructural

Hazards

•^

Example 1: two instructions need the same resource

p

  • lw instruction in stage four (memory access)– another instruction in stage one (instruction fetch)– both of these actions require access to memory -^

Solution: add more hardware to eliminate problem– separate instruction and data memory caches or

memory unit ports

•^

Or stall – do things one at a timeOr stall

do things one at a time

  • Not a good idea, particularly on memory access

6

Data HazardsData

Hazards

•^

When an instruction depends on the results of aprevious instruction still in the pipelineprevious instruction still in the pipeline

-^

This is a data dependency

$s

is

add

$s

,

$s1,

$s

IF

ID

EX

MEM

WB

written here

add

$s4,

$s3,

$s

IF

ID

EX

MEM

WB

$s

is read here

8

Stall for register data dependencyStall

for register data dependency

•^

One solution: Stall the pipeline until the result is

-^

One solution: Stall the pipeline until the result isavailable– this would create a 3-cycle

p

ipeline bubble

y

p p

add

s0,s1,s

IF

ID

EX

MEM

WB

add

s0,s1,s

add

s4,s3,s

IF

ID

EX

MEM

WB

IF

ID

EX

MEM

WB

stall

9

Solution: ForwardingSolution:

Forwarding

•^

The value of $s0 is known internally after cycle 3(after the first instruction’s EX stage)

•^

The value of $s0 isn’t needed until cycle 4(before the second instruction’s EX stage)

•^

If we

forward

the result there isn’t a stall

add

s0,s1,s

IF

ID

EX

MEM

WB

add

s4,s3,s

IF

ID

EX

MEM

WB

11

Another

example with dependencies

Another

example with dependencies

sub

$2,

$1,

$

and

$12,

$2,

$

or

$13,

$6,

$

add

$

$

$

add

$14,

$2,

$

sw

$15,

100($2)

How would this code sequence fare in our pipelineddatapath without forwarding?

12

Things that are okay

Clock cycle

1

2

3

4

5

6

7

8

9

Things

that are okay

1

2

3

4

5

6

7

8

9

sub

$2, $1, $

IF

ID

EX

MEM

WB

and

$12, $2, $

IF

ID

EX

MEM

WB

and

$12, $2, $

IF

ID

EX

MEM

WB

or

$13, $6, $

IF

ID

EX

MEM

WB

add

$14, $2, $

IF

ID

EX

MEM

WB

add

$^

, $ , $

M M

W

sw

$15, 100($2)

IF

ID

EX

MEM

WB

-^

The ADD instruction is okay because of the register file

-^

The

ADD instruction is okay, because of the register file

design.– Registers are written at the beginning of a clock cycle.– The new value will be available by the end of that cycle

The new value will be available by the end of that cycle.

-^

The SW is no problem at all, since it reads $2 after the SUBfinishes.

14

Dependency arrows

Clock cycle

1

2

3

4

5

6

7

8

9

Dependency

arrows

sub

$2, $1, $

IF

ID

EX

MEM

WB

and

$12, $2, $

IF

ID

EX

MEM

WB

or

$13, $6, $

IF

ID

EX

MEM

WB

add

$14, $2, $

IF

ID

EX

MEM

WB

sw

$15, 100($2)

IF

ID

EX

MEM

WB

-^

Arrows indicate the flow of data between instructionsArrows indicate the flow of data between instructions.– The tails of the arrows show when register $2 is written.– The heads of the arrows show when $2 is read.

-^

Any arrow that points backwards in time represents a dataAny arrow that points backwards in time represents a datahazard in our basic pipelined processor. Here, hazards existbetween instructions 1 & 2 and 1 & 3.

15

What about loads?What

about loads?

•^

What if the first instruction is lw?

-^

This is a true data hazard: s0 isn’t known until after theMEM stage

We can’t forward back into the past

  • We can t forward back into the past -^

Either

stall

or

reorder

instructions

lw

s

,0(s2)

IF

ID

EX

MEM

WB

add

s4,s3,

s

IF

ID

EX

MEM

WB

NO!

17

Stall for

lw

hazard

Stall

for

lw

hazard

•^

We can stall for one cycle and then forward– but can we avoid the stall?

but can we avoid the stall?

lw

s

0(s2)

IF

ID

EX

MEM

WB

lw

s

,0(s2)

add

s4,s3,s

IF

ID

EX

MEM

WB

IF

ID

EX

MEM

WB

stall

add

s

,s3,s

IF

ID

EX

MEM

WB

stall

18

Control HazardsControl

Hazards

•^

Branch instructions cause

control hazards

b

d

’t k

hi h i

t^

ti

t^

f t h

b

ecause we don’t know which instruction to fetch

next

we don’t knowuntil here

IF

ID

EX

MEM

WB

bne $s0, $s1, skip

IF

ID

EX

MEM

WB

$^

, $

,^

p

add $s4, $s3, $s0...

do we fetch the add

or the

sub

?

skip:

sub $s4, $s3, $s

20

One solution: StallOne

solution: Stall

•^

Stall until we know which instruction to execute next– We know the result at the end of the EX state (or

earlier if we compare the registers in the ID stage)Still causes a 1 or 2 cycle stall even with forwarding

  • Still causes a 1 or 2 cycle stall even with forwarding– We can’t completely avoid the stall unless we know if

the branch will be taken

IF

ID

EX

MEM

WB

bne

$s0,

$s1,

next

IF

ID

EX

MEM

WB

stall

sub

$s4,

$s3,

$s

21