Docsity

Pipelined Processor II - Lecture Slides - Fall 2006 | ECE 154, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
ECE154
Computer Architecture
Lecture 12
Pipelined Processor II
November 9, 2006
Ryan Kastner
Electrical and Computer Engineering
University of California, Santa Barbara
Graphically Representing MIPS Pipeline
Can help with answering questions like:
how many cycles does it take to execute this
code?
what is the ALU doing during cycle 4?
is there a hazard, why does it occur, and how
can it be fixed?
ALU
IM Reg DM Reg
Recap last lecture
All recent processors use pipelining
Pipelining doesn’t help latency of single task, it
helps throughput or bandwidth of entire workload
Multiple tasks operating simultaneously using differ ent
resources
Potential speedup = Number of pipe stages
Pipeline rate limited by slowest pipeline stage
Unbalanced lengths of pipe stages reduces speedup
Time to “fill” pipeline and time to “drain” it reduces
speedup
Can Pipelining Get Us Into Trouble?
Yes: Pipeline Hazards
structural hazards: attempt to use the same resource by
two different instructions at the same time
data hazards: attempt to use data before it is ready
instruction source operands are produced by a prior instruction
still in the pipeline
load instruction followed immediatel y by an ALU instruction
that uses the load operand as a source value
control hazards: attempt to make a decision before
condition has been evaluated
branch instructions
Can always resolve hazards by waiting
pipeline control must detect the hazard
take action (or delay action) to resolve hazards
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Pipelined Processor II - Lecture Slides - Fall 2006 | ECE 154 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

ECE

Computer Architecture

Lecture 12

Pipelined Processor II

November 9, 2006 Ryan Kastner Electrical and Computer Engineering University of California, Santa Barbara

Graphically Representing MIPS Pipeline

Can help with answering questions like:

™how many cycles does it take to execute this

code?

™what is the ALU doing during cycle 4?

™is there a hazard, why does it occur, and how

can it be fixed?

ALU IM (^) Reg DM Reg

Recap last lecture

™ All recent processors use pipelining

™ Pipelining doesn’t help latency of single task, it

helps throughput or bandwidth of entire workload

™ Multiple tasks operating simultaneously using different

resources

™ Potential speedup = Number of pipe stages

™ Pipeline rate limited by slowest pipeline stage

™ Unbalanced lengths of pipe stages reduces speedup

™ Time to “fill” pipeline and time to “drain” it reduces

speedup

Can Pipelining Get Us Into Trouble?

™ Yes: Pipeline Hazards

™ structural hazards: attempt to use the same resource by

two different instructions at the same time

™ data hazards: attempt to use data before it is ready

™instruction source operands are produced by a prior instruction still in the pipeline ™load instruction followed immediately by an ALU instruction that uses the load operand as a source value

™ control hazards: attempt to make a decision before

condition has been evaluated

™branch instructions

™ Can always resolve hazards by waiting

™ pipeline control must detect the hazard

™ take action (or delay action) to resolve hazards

MIPS Pipeline Control Path Modifications

™ All control signals can be determined during Decode

™ and held in the state registers between pipeline stages

Read Address

Instruction Memory

Add

PC

4

0

1

Write Data

Read Addr 1 Read Addr 2 Write Addr

Register File

Read Data 1

Read Data 2

16 32

ALU

1

0

Shift left 2

Add

Data Memory Address Write Data

Read Data 1 0

IF/ID

Sign Extend

ID/EX EX/MEM

MEM/WB

Control

Control Settings

beq X 0 1 0 1 0 0 0 X

sw X 0 0 1 0 0 1 0 X

lw 0 0 0 1 0 1 0 1 1

R 1 1 0 0 0 0 0 1 0

Mem toReg

Reg Write

Mem Write

Mem Read

ALU Brch Src

ALU

Op

ALU

Op

RegDst

` EX Stage MEM Stage WB Stage

Q: Why not show write enable for pipeline registers? A: Written every clock cycle (like PC) Q: Why not show control for IF and ID stages? A: Control same for all instructions in IF and ID stages: fetch instruction, increment PC

Read Address

Instruction Memory

Add

PC

4

0

1

Write Data

Read Addr 1 Read Addr 2 Write Addr

Register File

Read Data 1

Read Data 2

16 32

ALU

1

0

Shift left 2

Add

Data Memory Address Write Data

Read Data 1 0

IF/ID

Sign Extend

ID/EX EX/MEM

MEM/WB

Control

0 1

ALU cntrl

RegWrite

MemWrite MemRead

MemtoReg

RegDst

ALUOp

ALUSrc

Branch

PCSrc

MIPS Pipeline Data and Control Paths Register Usage Can Cause Data Hazards

™ Dependencies backward in time cause hazards

I n s t r.

O r d e r

add r1,r2,r

sub r4,r1,r

and r6,r1,r

xor r4,r1,r

or r8, r1, r

ALU IM Reg DM Reg

ALU IM Reg DM Reg

ALU IM Reg DM Reg

ALU IM Reg DM Reg

ALU IM Reg DM Reg

Read before write data hazards

Datapath with Forwarding Hardware

PCSrc

Read Address

Instruction Memory

Add

PC

4

0

1

Write Data

Read Addr 1 Read Addr 2 Write Addr

Register File

Read Data 1

Read Data 2

16 32

ALU 1 0

Shift left 2

Add

Data Memory Address Write Data

Read Data 1 0

IF/ID

Sign Extend

ID/EX EX/MEM

MEM/WB

Control

0 1

ALU cntrl

Branch

Forward Unit

Data Forwarding Control Conditions

  1. EX/MEM hazard: if (EX/MEM.RegisterRd == ID/EX.RegisterRs)) ForwardA = 10 ™ “RegisterRd” is number of register to be written (RD or RT) ™ “RegisterRs” is number of RS register ™ “RegisterRt” is number of RT register ™ “ForwardA, ForwardB” controls forwarding muxes

if (EX/MEM.RegisterRd = ID/EX.RegisterRt)) ForwardB = 10

  1. MEM/WB hazard: if (MEM/WB.RegisterRd == ID/EX.RegisterRs)) ForwardA = 01 if (MEM/WB.RegisterRd == ID/EX.RegisterRt)) ForwardB = 01

Forwards the result from the previous instr. to either input of the ALU.

Forwards the result from the second previous instr. to either input

What’s wrong with this hazard control? of the ALU.

(When might it forward when it shouldn’t?)

(Which sequences would reveal this bug?)

Data Forwarding Control Conditions

1. EX/MEM hazard:

if (EX/MEM.RegWrite and (EX/MEM.RegisterRd == ID/EX.RegisterRs)) ForwardA = 10 if (EX/MEM.RegWrite and (EX/MEM.RegisterRd == ID/EX.RegisterRt)) ForwardB = 10

2. MEM/WB hazard:

if (MEM/WB.RegWrite and (MEM/WB.RegisterRd == ID/EX.RegisterRs)) ForwardA = 01 if (MEM/WB.RegWrite and (MEM/WB.RegisterRd == ID/EX.RegisterRt)) ForwardB = 01

Forwards the result from the previous instr. to either input of the ALU provided it writes.

Forwards the result from the second previous instr. to either input of the ALU provided it writes.

What’s wrong with this hazard control?

(When might it forward when it shouldn’t?)

(Which sequences would reveal this bug?)

Data Forwarding Control Conditions

  1. EX/MEM hazard: if (EX/MEM.RegWrite and (EX/MEM.RegisterRd != 0) and (EX/MEM.RegisterRd == ID/EX.RegisterRs)) ForwardA = 10 if (EX/MEM.RegWrite and (EX/MEM.RegisterRd != 0) and (EX/MEM.RegisterRd == ID/EX.RegisterRt)) ForwardB = 10
  2. MEM/WB hazard: if (MEM/WB.RegWrite and (MEM/WB.RegisterRd != 0) and (MEM/WB.RegisterRd == ID/EX.RegisterRs)) ForwardA = 01 if (MEM/WB.RegWrite and (MEM/WB.RegisterRd != 0) and (MEM/WB.RegisterRd == ID/EX.RegisterRt)) ForwardB = 01

Forwards the result from the previous instr. to either input of the ALU provided it writes and != R0.

Forwards the result from the second previous instr. to either input of the ALU provided it writes and != R0.

What’s wrong with this hazard control?

Yet Another Complication!

™ Another potential data hazard can occur when there is

a conflict between the result of the WB stage

instruction and the MEM stage instruction – which

should be forwarded? More recent result!

I n s t r.

O r d e r

add $1,$1,$

ALU IM Reg DM Reg

add $1,$1,$

add $1,$1,$

ALU IM Reg DM Reg

ALU IM Reg DM Reg

Corrected Data Forwarding Control Conditions

2. MEM/WB hazard:

if (MEM/WB.RegWrite and (MEM/WB.RegisterRd != 0) and (MEM/WB.RegisterRd == ID/EX.RegisterRs) and (EX/MEM.RegisterRd != ID/EX.RegisterRs || ~ EX/MEM.RegWrite) ) ForwardA = 01

if (MEM/WB.RegWrite and (MEM/WB.RegisterRd != 0) and (MEM/WB.RegisterRd == ID/EX.RegisterRt) and (EX/MEM.RegisterRd != ID/EX.RegisterRt || ~ EX/MEM.RegWrite) )) ForwardB = 01

Forward if this instruction writes AND its not writing R

AND this dest reg == source AND in between instr either

dest. reg. doesn’t match OR it doesn’t write reg.

Datapath with Forwarding Hardware

Read Address

Instruction Memory

Add

PC

4

0

1

Write Data

Read Addr 1 Read Addr 2 Write Addr

Register File

Read Data 1

Read Data 2

16 32

ALU 1 0

Shift left 2

Add

Data Memory Address Write Data

Read Data 1 0

IF/ID

Sign Extend

ID/EX EX/MEM

MEM/WB

Control

0 1

ALU cntrl

Branch

Forward Unit

IF/ID.RegisterRs IF/ID.RegisterRt

EX/MEM.RegisterRd

MEM/WB.RegisterRd

PCSrc

Forwarding with Load-use Data Hazards

™ Will still need one stall cycle even with forwarding

I n s t r.

O r d e r

lw r1,100(r2)

sub r4,r1,r

and r6,r1,r

xor r4,r1,r

or r8, r1, r

ALU IM Reg DM Reg

ALU IM (^) Reg DM Reg

ALU IM Reg DM Reg

ALU IM Reg DM Reg

ALU IM Reg DM Reg

Branch Instructions Cause Control Hazards

™ Dependencies backward in time cause hazards

I n s t r.

O r d e r

lw

Inst 4

Inst 3

beq

ALU IM (^) Reg DM Reg

ALU IM Reg DM Reg

ALU IM Reg DM Reg

ALU IM (^) Reg DM Reg

One Way to “Fix” a Control Hazard

stall

stall

stall

I n s t r.

O r d e r

beq

ALU IM Reg DM Reg

lw

ALU IM Reg DM Reg

ALU

Inst 3

IM Reg DM

Can fix branch hazard by waiting – stall – but affects throughput

Control Hazards

™ When the flow of instruction addresses is not what the pipeline

expects; incurred by change of flow instructions

™ Conditional branches (beq, bne) ™ Unconditional branches (j)

™ Possible solutions

™ Stall ™ Move decision point earlier in the pipeline ™ Predict ™ Delay decision (requires compiler support)

™ Control hazards occur less frequently than data hazards; there is

nothing as effective against control hazards as forwarding is for

data hazards

Moving Branch Decisions Earlier in Pipe

™ Move the branch decision hardware back to the EX stage

™ Reduces the number of stall cycles to two ™ Adds an and gate and a 2x1 mux to the EX timing path

™ Add hardware to compute the branch target address and evaluate

the branch decision to the ID stage

™ Reduces the number of stall cycles to one (like with jumps) ™ Computing branch target address can be done in parallel with RegFile read (done for all instructions – only used when needed) ™ Comparing the registers can’t be done until after RegFile read, so comparing and updating the PC adds a comparator, an and gate, and a 3x1 mux to the ID timing path ™ Need forwarding hardware in ID stage

™ For longer pipelines, decision points are later in the pipeline,

incurring more stalls, so we need a better solution

Branch Prediction

™ Resolve branch hazards by assuming a given outcome and

proceeding without waiting to see the actual branch outcome

1. Predict not taken – always predict branches will not be taken,

continue to fetch from the sequential instruction stream, only

when branch is taken does the pipeline stall

™ If taken, flush instructions in the pipeline after the branch ™ in IF, ID, and EX if branch logic in MEM – three stalls ™ in IF if branch logic in ID – one stall ™ ensure that those flushed instructions haven’t changed machine state– automatic in the MIPS pipeline since machine state changing operations are at the tail end of the pipeline (MemWrite or RegWrite) ™ restart the pipeline at the branch destination

Flushing with Misprediction (Not Taken)

™ To flush the IF stage instruction, add a IF.Flush

control line that zeros the instruction field of the

IF/ID pipeline register (transforming it into a noop)

I 4 beq $1,$2,

n s t r.

O r d e r

ALU IM Reg DM Reg

ALU

8 sub $4,$1,$5 IM^ Reg^ DM^ Reg

Flushing with Misprediction (Not Taken)

™ To flush the IF stage instruction, add a IF.Flush

control line that zeros the instruction field of the

IF/ID pipeline register (transforming it into a noop)

flush

I 4 beq $1,$2,

n s t r.

O r d e r

ALU IM Reg DM Reg

16 and $6,$1,$

20 or r8,$1,$

ALU IM Reg DM Reg

ALU IM (^) Reg DM Reg

ALU

8 sub $4,$1,$5 IM^ Reg^ DM^ Reg

Branch Prediction, con’t

™ Resolve branch hazards by statically assuming a given

outcome and proceeding

2. Predict taken – always predict branches will be taken

™ Predict taken always incurs a stall (if branch destination hardware has been moved to the ID stage)

™ As the branch penalty increases (for deeper pipelines), a

simple static prediction scheme will hurt performance

™ With more hardware, possible to try to predict branch

behavior dynamically during program execution

3. Dynamic branch prediction – predict branches at run-time

using run-time information

Delayed Decision

™ First, move the branch decision hardware and target address

calculation to the ID pipeline stage

™ A delayed branch always executes the next sequential

instruction – the branch takes effect after that next instruction

™ MIPS software moves an instruction to immediately after the branch that is not affected by the branch (a safe instruction) thereby hiding the branch delay

™ As processor go to deeper pipelines and multiple issue, the

branch delay grows and need more than one delay slot.

™ Delayed branching has lost popularity compared to more expensive but more flexible dynamic approaches ™ Growth in available transistors has made dynamic approaches relatively cheaper

Scheduling Branch Delay Slots

™ A is the best choice, fills delay slot & reduces instruction count (IC) ™ In B, the sub instruction may need to be copied, increasing IC ™ In B and C, must be okay to execute sub when branch fails

add $1,$2,$ if $2=0 then delay slot

A. From before branch B. From branch target C. From fall through

add $1,$2,$ if $1=0 then delay slot

add $1,$2,$ if $1=0 then delay slot

sub $4,$5,$

sub $4,$5,$

becomes becomes becomes

if $2=0 then add $1,$2,$ add $1,$2,$ if $1=0 then sub $4,$5,$

add $1,$2,$ if $1=0 then sub $4,$5,$

In Conclusion

™ Data dependencies in pipelines often solved by forwarding

™ Need to be sure prior instructions will write, destination

matches source, and no earlier instruction has priority

™ Need forwarding hardware every place where can forward,

stall if stage needs to wait for result

™ EX stage, MEM stage for store, ID stage for early branch

™ Loads require stall since overlap EX and MEM stages

™ Branches may require stall too

™ Control hazards improved via delayed branch/jump in ISA,

static prediction for branches, dynamic prediction for

branches

™ If predict, hard part of design is recovering from misprediction