Docsity

Pipelining - Computer Architecture - Slides | CS 472, Study notes of Computer Architecture and Organization

Partial preview of the text

Download Pipelining - Computer Architecture - Slides | CS 472 and more Study notes Computer Architecture and Organization in PDF only on Docsity!

CS472 1

Chapter 6

Pipelining

cont.

CS472 2

5 Stage Pipeline

• IF instruction fetch

• ID instruction decode and register file read

• EX execution or address calculation

• Mem data memory access

• WB write back

We can have up to 5 instructions in execution

during any single clock cycle.

CS472 3

Label 5 Stages in the Single Cycle Datapath

Instruction memory

Address

Add Add result

Shift left 2

Instruction

M

u x

Add

PC

Write data

M

u x

Registers

Read data 1

Read data 2

Read register 1

Read register 2

Sign extend

Write register

Write data

Read Address data Data memory 1

ALU

result M u x

ALU

Zero

IF: Instruction fetch ID: Instruction decode/

register file read

EX: Execute/

address calculation

MEM: Memory access WB: Write back

CS472 4

Add Intermediate Registers

Instruction memory

Address

Add

Add result

Shift left 2

Instruction

IF/ID EX/MEM MEM/WB
M

u x

Add

PC

Write data

M

u x

Registers

Read data 1

Read data 2

Read register 1

Read register 2

Sign extend

Write register

Write data

Read data

ALU

result M u x

ALU

Zero

ID/EX

Data memory

Address

Do you see a problem in the datapath?

CS472 7

Path with Control Lines

PC

Instruction memory

Instruction

Add

Instruction [20– 16]

MemtoReg

ALUOp

Branch

RegDst

ALUSrc

4

16 32 Instruction [15– 0]

0

0

M u x

0

1

Add Add result

Registers Write register

Write data

Read data 1

Read data 2

Read register 1

Read register 2

Sign extend

M u x 1

ALU result

Zero

Write data

Read data M u x

1

ALU control

Shift left 2 RegWrite

MemRead

Control

ALU

Instruction [15– 11]

6

EX

M

WB

M

WB

WB IF/ID

PCSrc

ID/EX

EX/MEM

MEM/WB

M u x

0

1

MemWrite

Address Data memory

Address

CS472 8

3 Kinds of Hazards

• Structural hazards

• Data Hazards

• Control Hazards

CS472 9

Data Dependency

IM Reg

IM Reg

CC 1 CC 2 CC 3 CC 4 CC 5 CC 6

Time (in clock cycles)

sub $2, $1, $

Program

execution

order

(in instructions)

and $12, $2, $

IM

Reg

DM Reg

IM DM Reg

IM DM Reg

CC 7 CC 8 CC 9

or $13, $6, $

add $14, $2, $

sw $15, 100($2)

Value of

register $2:

DM

Reg

Reg

Reg

Reg

DM

When the data dep.

lines go backward,

we have a

data hazard.

It is OK to read

a register in the

same clk cycle

that it is written

CS472 10

Software Fix

• Have compiler guarantee no hazards

– compiler could insert independent instructions

– if no independent instr. available, then insert “nops”

• Where do we insert the “nops”?

sub $2, $1, $

and $12, $2, $

or $13, $6, $

add $14, $2, $

sw $15, 100($2)

• In reality, these dependencies happen too often

for the compiler to take care of

CS472 13

Can’t Forward Sometimes (ld instr.)

Reg

IM

Reg

Reg

IM

CC 1 CC 2 CC 3 CC 4 CC 5 CC 6

Time (in clock cycles)

lw $2, 20($1)

Program

execution

order

(in instructions)

and $4, $2, $

IM Reg DM Reg

IM DM Reg

IM DM Reg

CC 7 CC 8 CC 9

or $8, $2, $

add $9, $4, $

slt $1, $6, $

DM Reg

Reg

Reg

DM

CS472 14

Control Hazards

An instruction must be fetched at every clock cycle to

sustain our pipeline. But, in this design, the decision to

branch doesn’t occur until the MEM pipeline stage.This

delay in determining the proper instruction to fetch is

called a control hazard.

Solutions

– always predict branch not taken

– reduce delay

– use 1 or 2 bit algorithm to predict branches

– branch delay slot

CS472 15

Control Hazards

Reg

Reg

CC 1

Time (in clock cycles)

40 beq $1, $3, 7

Program

execution

order

(in instructions)

IM Reg

IM DM

IM DM

IM DM

DM

DM Reg

Reg Reg

Reg

Reg

IM Reg

44 and $12, $2, $

48 or $13, $6, $

52 add $14, $2, $

72 lw $4, 50($7)

CC 2 CC 3 CC 4 CC 5 CC 6 CC 7 CC 8 CC 9

Reg

When we decide to branch, other instructions are in the

pipeline. Here we are simply predicting branch not taken.

If the branch is

taken, we must

flush pipeline of

3 instructions.

CS472 16

Reduce Delay

• Many MIPS implementations move the branch

decision to ID stage rather than the MEM stage.

• Now only 1 instruction (instead of 3) has to be

flushed if we guess wrong.

CS472 19

Dynamic Branch Prediction - 2 bits

• In a 2-bit scheme, a prediction must be wrong

twice in a row before it is changed.

• In our 100 iteration loop example, this prediction

scheme is wrong only on the final iteration.

• Steady state is n-1.

• A branch prediction buffer can be implemented

as a small, special buffer accessed with the

instruction address during the IF pipe stage.

CS472 20

A Delayed Branch

• With special hardware we can move the branch

decision up to the ID stage.

• At that time only one instruction is fetched that

may have to be flushed.

• What if we redefined the branch as a delayed

branch? A delayed branch always executes the

following instruction and then (possibly) branches.

• Compilers and assemblers try to place a valid,

useful instruction in the branch delay slot.

CS472 21

Scheduling the Branch Delay Slot

add $s1, $s2, $s

if $s2 = 0 then if $s2 = 0 then

Delay slot

add $s1, $s2, $s

The add and branch instructions are independent of each other. The

pipeline is not delayed.

CS472 22

Scheduling the Branch Delay Slot

sub $t4, $t5, $t

add $s1, $s2, $s3 add $s1, $s2, $s

if $s1 = 0 then if $s1 = 0 then

Delay slot sub $t4, $t5, $t

Not as preferred as previous slide’s situation. The add and branch

are not independent. This solution is preferred when branch is taken

with a high probability.