Docsity

Understanding Stalling and Data Forwarding in CPU Pipelining - Prof. Jian-Guo Liu, Study notes of Mathematics

Partial preview of the text

Download Understanding Stalling and Data Forwarding in CPU Pipelining - Prof. Jian-Guo Liu and more Study notes Mathematics in PDF only on Docsity!

Randal E. Bryant

Carnegie Mellon University

CS:APP

CS:APP Chapter 4

Computer Architecture

Pipelined

Implementation

Part II

CS:APP Chapter 4

CS:APP Chapter 4

Computer Architecture

Computer Architecture

Pipelined

Pipelined

Implementation

Implementation

Part II

Part II

http://csapp.cs.cmu.edu

CS:APP

Overview

Overview

Make the pipelined processor work!

Data Hazards

Data Hazards

n Instruction having register R as source follows shortly after

instruction having register R as destination

n Common condition, don’t want to slow down pipeline

Control Hazards

Control Hazards

n Mispredict conditional branch

l Our design predicts all branches as being taken

l Naïve pipeline executes two extra instructions

n Getting return address for ret instruction

l Naïve pipeline executes three extra instructions

Making Sure It Really Works Making Sure It Really Works

n What if multiple special cases happen simultaneously?

CS:APP

PIPE- Hardware

PIPE- Hardware

n Pipeline registers hold

intermediate values

from instruction

execution

Forward (Upward) Paths

Forward (Upward) Paths

n Values passed from one

stage to next

n Cannot jump past

stages

l e.g., valC passes

through decode

E

M

W

F

D

Instruction

memory

Instruction

memory

PC

increment

PC

increment

Register

file

Register

file

ALU

ALU

Data

memory

Data

memory

Select

PC

rB

dstE dstM

Select

A

ALU

A

ALU

B

Mem.

control

Addr

srcA srcB

read

write

ALU

fun.

Fetch

Decode

Execute

Memory

Write back

icode

data out

data in

A B

M

E

M_valA

W_valM

W_valE

M_valA

W_valM

d_rvalA

f_PC

Predict

PC

valE valM dstE dstM

icode Bch valE valA dstE dstM

icode ifun valC valA valB dstE dstM srcA srcB

icode ifun rA valC valP

predPC

CC

CC

d_srcA d_srcB

e_Bch

M_Bch

CS:APP

Data Dependencies: 2 Nop’s

Data Dependencies: 2 Nop’s

0x000: irmovl $10,%edx

1 2 3 4 5 6 7 8 9

FF DD EE MM WW

0x006: irmovl $3,%eax

FF DD EE MM WW

0x00c: nop

FF DD EE MM WW

0x00d: nop

FF DD EE MM WW

0x00e: addl %edx,%eax

FF DD EE MM WW

0x010: halt

FF DD EE MM WW

10

# demo-h2.ys

W

R[

%eax

] 3

D

valA R[ %edx

] = 10

valB R[ %eax

] = 0

W

R[

%eax

] 3

W

R[

%eax

] 3

D

valA R[ %edx

] = 10

valB R[ %eax

] = 0

D

valA R[ %edx

] = 10

valB R[ %eax

] = 0

Cycle 6

Error

CS:APP

Stalling for Data Dependencies

Stalling for Data Dependencies

n If instruction follows too closely after one that writes register,

slow it down

n Hold instruction in decode

n Dynamically inject nop into execute stage

0x000: irmovl $10,%edx

1 2 3 4 5 6 7 8 9

F D E M W

0x006: irmovl $3,%eax

F D E M W

0x00c: nop

F D E M W

bubble

F

E M W

0x00e: addl %edx,%eax

D D E M W

0x010: halt

F D E M W

10

# demo-h2.ys

F

F D E M W

0x00d: nop

11

CS:APP

Stall Condition

Stall Condition

Source Registers

Source Registers

n srcA and srcB of current

instruction in decode

stage

Destination Registers

Destination Registers

n dstE and dstM fields

n Instructions in execute,

memory, and write-back

stages

Special Case

Special Case

n Don’t stall for register ID

8

l Indicates absence of

register operand

E

M

W

F

D

Instruction

memory

Instruction

memory

PC

increment

PC

increment

Register

file

Register

file

ALU

ALU

Data

memory

Data

memory

Select

PC

rB

Select dstE dstM

A

ALU

A

ALU

B

Mem.

control

Addr

srcA srcB

read

write

ALU

fun.

Fetch

Decode

Execute

Memory

Write back

icode

data out

data in

A B

M

E

M_valA

W_valM

W_valE

M_valA

W_valM

d_rvalA

f_PC

Predict

PC

valE valM dstE dstM

icode Bch valE valA dstE dstM

icode ifun valC valA valB dstE dstM srcA srcB

icode ifun rA valC valP

predPC

CC

CC

d_srcA d_srcB

e_Bch

M_Bch

CS:APP

Stalling X

Stalling X

0x000: irmovl $10,%edx

1 2 3 4 5 6 7 8 9

F D E M W

0x006: irmovl $3,%eax

F D E M W

bubble

F

E M W

bubble

D

E M W

0x00c: addl %edx,%eax

D D E M W

0x00e: halt

F D E M W

10

# demo-h0.ys

F F

D

F

E M W

bubble

11

Cycle 4

W

W_dstE = %eax

D

srcA = %edx

srcB = %eax

M

M_dstE = %eax

D

srcA = %edx

srcB = %eax

E

E_dstE = %eax

D

srcA = %edx

srcB = %eax

Cycle 5

Cycle 6

CS:APP

What Happens When Stalling?

What Happens When Stalling?

n Stalling instruction held back in decode stage

n Following instruction stays in fetch stage

n Bubbles injected into execute stage

l Like dynamically generated nop’s

l Move through later stages

0x000: irmovl $10,%edx

0x006: irmovl $3,%eax

0x00c: addl %edx,%eax

Cycle 4

0x00e: halt

0x000: irmovl $10,%edx

0x006: irmovl $3,%eax

0x00c: addl %edx,%eax

# demo-h0.ys

0x00e: halt

0x000: irmovl $10,%edx

0x006: irmovl $3,%eax

bubble

0x00c: addl %edx,%eax

Cycle 5

0x00e: halt

0x006: irmovl $3,%eax

bubble

0x00c: addl %edx,%eax

bubble

Cycle 6

0x00e: halt

bubble

bubble

0x00c: addl %edx,%eax

bubble

Cycle 7

0x00e: halt

bubble

bubble

Cycle 8

0x00c: addl %edx,%eax

0x00e: halt

Write Back

Memory

Execute

Decode

Fetch

CS:APP

Pipeline Register Modes

Pipeline Register Modes

Rising

clock

Rising

clock

Output = y

yy

Rising

clock

Rising

clock

Output = x

xx

xx

n

o

p

Rising

clock

Rising

clock

Output = nop

Input = y Output = x

stall

= 0

bubble

= 0

xx

Normal

Input = y Output = x

stall

= 1

bubble

= 0

xx

Stall

Input = y Output = x

stall

= 0

bubble

= 1

Bubble

CS:APP

Data Forwarding

Data Forwarding

Naïve Pipeline

Naïve Pipeline

n Register isn’t written until completion of write-back stage

n Source operands read from register file in decode stage

l Needs to be in register file at start of stage

Observation

Observation

n Value generated in execute or memory stage

Trick

Trick

n Pass value directly from generating instruction to decode

stage

n Needs to be available at end of decode stage

CS:APP

Bypass Paths

Bypass Paths

Decode Stage

Decode Stage

n Forwarding logic selects

valA and valB

n Normally from register

file

n Forwarding: get valA or

valB from later pipeline

stage

Forwarding Sources

Forwarding Sources

n Execute: valE

n Memory: valE, valM

n Write back: valE, valM

CC

CC

ALU

ALU

Data

memory

Data

memory

Decode

Execute

Memory

Write back

Register

file

Register

file

A B

M

E

Register

file

Register

file

A B

M

E

valP

d_srcA,

d_srcB

valA, valB

Bch

e_valE

Addr, Data

m_valM

W_icode, W_valM W_valE, W_valM, W_dstE, W_dstM

icode, ifun,

rA, rB, valC

E

M

W

D

valP

M_icode,

M_Bch,

M_valA

M_valE

W_valE

W_valM

E_valA, E_valB,

E_srcA, E_srcB

Forward

CS:APP

Data Forwarding Example #

Data Forwarding Example #

Register Register %%edxedx

n Generated by ALU

during previous cycle

n Forward from memory

as valA

Register Register %%eaxeax

n Value just generated

by ALU

n Forward from execute

as valB

0x000: irmovl $10,%edx

1 2 3 4 5 6 7 8

F D E M

W

0x006: irmovl $3,%eax

F D E M

W

F D E M W

0x00c: addl %edx,%eax

F D E M W

0x00e: halt

# demo-h0.ys

Cycle 4

M

D

valA f M_valE = 10

valB f e_valE = 3

M_dstE = %edx

M_valE = 10

srcA = %edx

srcB = %eax

E

E_dstE = %eax

e_valE f 0 + 3 = 3

CS:APP

Implementing Forwarding

Implementing Forwarding

Instruction

nstruction

memory

PC

PC

increment

Register

file

Register

file

CC

CC ALU

ALU

Data

memory

Data

memory

rB

dstE dstM

ALU

A

ALU

B

Mem.

control

Addr

srcA srcB

read

write

ALU

fun.

data out

data in

A B

M

E

M_valA

W_valE

W_valM

W_valE

Predict

PC

Bch valE valA dstE dstM

valC valA valB dstE dstM srcA srcB

rA valC valP

d_srcA d_srcB

e_Bch

Sel+Fwd

A

Fwd

B

valE valM dstE dstM

m_valM

W_valM

M_valE

e_valE

## What should be the A value?

int new_E_valA = [

# Use incremented PC

D_icode in { ICALL, IJXX } : D_valP;

# Forward valE from execute

d_srcA == E_dstE : e_valE;

# Forward valM from memory

d_srcA == M_dstM : m_valM;

# Forward valE from memory

d_srcA == M_dstE : M_valE;

# Forward valM from write back

d_srcA == W_dstM : W_valM;

# Forward valE from write back

d_srcA == W_dstE : W_valE;

# Use value read from register file

1 : d_rvalA;

];

CS:APP

Limitation of Forwarding

Limitation of Forwarding

Load-use dependency

Load-use dependency

n Value needed by end of

decode stage in cycle 7

n Value read from memory in

memory stage of cycle 8

0x000: irmovl $128,%edx

1 2 3 4 5 6 7 8 9

F D E M
W

0x006: irmovl $3,%ecx F D E M

W

0x00c: rmmovl %ecx, 0(%edx)

F D E M W

0x012: irmovl $10,%ebx F D E M W

0x018: mrmovl 0(%edx), %eax # Load %eax F D E M W

# demo-luh.ys

0x01e: addl %ebx, %eax # Use %eax

0x020: halt

F D E M W
F D E M W

10

F D E M W

11

Error

M

M_dstM = %eax

m_valM f M[128] = 3

Cycle 7 Cycle 8

D

valA f M_valE = 10

valB f R[ %eax ] = 0

D

valA f M_valE = 10

valB f R[ %eax ] = 0

M

M_dstE = %ebx

M_valE = 10