Download Lecture Slide on Pipelined Processor I | ECE 154 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!
ECE
Computer Architecture
Lecture 11
Pipelined Processor I
November 2, 2006
Ryan Kastner
Electrical and Computer Engineering
University of California, Santa Barbara
The Five Stages of Load Instruction
IFetch: Instruction Fetch and Update PC
Dec: Registers Fetch and Instruction Decode
Exec: Execute R-type; calculate memory address
Mem: Read/write the data from/to the Data
Memory
WB: Write the result data into the register file
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5
lw IFetch Dec Exec Mem WB
MIPS Datapath
Read Address
Instruction Memory
Add
PC
4
Write Data
Read Addr 1
Read Addr 2
Write Addr
Register
File
Read Data 1
Read Data 2
16 32
ALU
Shift left 2
Add
Data Memory
Address
Write Data
Read Data
IFetch/Dec Dec/Exec Exec/Mem Mem/WB
IF:IFetch ID:Dec EX:Execute MEM: MemAccess
WB:
WriteBack
System Clock
Sign Extend
Pipelined MIPS Processor
Start the next instruction while still working on the current one
improves throughput or bandwidth - total amount of work done in a
given time (average instructions per second or per clock)
instruction latency is not reduced (time from the start of an instruction to
its completion)
pipeline clock cycle (pipeline stage time) is limited by the slowest stage
for some instructions, some stages are wasted cycles
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5
lw IFetch Dec Exec Mem WB
Cycle 6 Cycle 7Cycle 8
sw IFetch^ Dec^ Exec^ Mem^ WB
R-type IFetch^ Dec^ Exec^ Mem^ WB
Single Cycle, Multiple Cycle, vs. Pipeline
Clk
Cycle 1
Multiple Cycle Implementation:
IFetch Dec Exec Mem WB
Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9Cycle 10
lw IFetch Dec Exec Mem WB
IFetch Dec Exec Mem
lw sw
Pipeline Implementation:
sw IFetch Dec Exec Mem WB
Clk
Single Cycle Implementation:
Load Store Waste
IFetch
R-type
R-typeIFetch Dec Exec Mem WB
Cycle 1 Cycle 2
“wasted” cycles
Multiple Cycle v. Pipeline, Bandwidth v. Latency
Clk
Cycle 1
Multiple Cycle Implementation:
IFetch Dec Exec Mem WB
Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9 Cycle 10
lw IFetch Dec Exec Mem WB
IFetch Dec Exec Mem
lw sw
Pipeline Implementation:
sw IFetch Dec Exec Mem WB
IFetch
R-type
R-typeIFetch Dec Exec Mem WB
• Latency per lw = 5 clock cycles for both
• Bandwidth of lw is : 1 per clock (IPC) for pipeline
vs.1/5 IPC for multicycle
• Pipelining improves instruction bandwidth, not
instruction latency
Pipelining the MIPS ISA
What makes it easy
all instructions are the same length (32 bits)
easier to fetch in 1 st^ stage and decode in 2nd^ stage
few instruction formats (three) with symmetry across formats
can begin reading register file in 2nd^ stage
memory operations can occur only in loads and stores
can use the execute stage to calculate memory addresses
each MIPS instruction writes at most one result and does so near
the end of the pipeline
What makes it hard
structural hazards: what if we had only one memory?
control hazards: what about branches?
data hazards: what if an instruction’s input operands depend on
the output of a previous instruction?
MIPS Pipeline Datapath Modifications
What do we need to add/modify in our MIPS datapath?
registers between pipeline stages to isolate them
Read Address
Instruction Memory
Add
PC
4
Write Data
Read Addr 1
Read Addr 2
Write Addr
Register
File
Read Data 1
Read Data 2
16 32
ALU
Shift left 2
Add
Data Memory
Address
Write Data
Read Data
IFetch/Dec Dec/Exec Exec/Mem Mem/WB
IF:IFetch ID:Dec EX:Execute MEM: MemAccess
WB:
WriteBack
System Clock
Sign Extend
Solution 2: Delay R-type’s Write by One Cycle
Delay R-type’s register write by one cycle:
Now R-type instructions also use Reg File’s write port at
Stage 5
Mem stage is a NOP stage: nothing is being done.
Clock
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7 Cycle 8 Cycle 9
R-type Ifetch Reg/Dec Mem Wr
R-type Ifetch Reg/Dec Mem Wr
Load Ifetch Reg/Dec Exec Mem Wr
R-type Ifetch Reg/Dec Mem Wr
R-type Ifetch Reg/Dec Mem Wr
R-type Ifetch Reg/Dec Exec Mem Wr
Exec
Exec
Exec
Exec
1 2 3 4 5
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
A Single Memory Would Be a Structural Hazard
I
n
s
t
r.
O
r
d
e
r
Time (clock cycles)
lw
Inst 1
Inst 2
Inst 4
Inst 3
ALU
Mem Reg Mem Reg
ALU
Mem Reg Mem Reg
ALU
Mem Reg Mem Reg
ALU
Mem Reg Mem Reg
ALU
Mem (^) Reg Mem Reg
Reading data from
memory
Reading instruction
from memory
How About Register File Access?
I
n
s
t
r.
O
r
d
e
r
Time (clock cycles)
add r1,
Inst 1
Inst 2
Inst 4
add r2,r1,
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
Potential read before write data hazard
How About Register File Access?
I
n
s
t
r.
O
r
d
e
r
Time (clock cycles)
Inst 1
Inst 2
Inst 4
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
Can fix register file
access hazard by
doing reads in the
second half of the
cycle and writes in
the first half.
add r1,
add r2,r1,
Potential read before write data hazard
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
Which are read before write data hazards?
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
Loads Can Cause Data Hazards
Dependencies backward in time cause hazards
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
Load-use data hazard
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
MIPS Datapath
Read Address
Instruction Memory
Add
PC
4
Write Data
Read Addr 1
Read Addr 2
Write Addr
Register
File
Read Data 1
Read Data 2
16 32
ALU
Shift left 2
Add
Data Memory
Address
Write Data
Read Data
IFetch/Dec Dec/Exec Exec/Mem Mem/WB
IF:IFetch ID:Dec EX:Execute MEM: MemAccess
WB:
WriteBack
System Clock
Sign Extend
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
Write Data
Read Addr 1
Read Addr 2
Write Addr
Register
File
Read Data 1
Read Data 2
16 32
ALU
Shift left 2
Add
Data Memory
Address
Write Data
Read Data
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
RegW
rite
Mem
Write
Mem
Read
ALU Brch
Src
ALU
Op
ALU
Op
RegD
st
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
Other Pipeline Structures Are Possible
What about (slow) multiply operation?
let it take two cycles
ALU
IM Reg DM Reg
MUL
ALU
IM Reg DM1 DM2 Reg
What if the data memory access is twice as slow as
the instruction memory?
make the clock twice as slow or …
let data memory access take two cycles (and keep the
same clock rate)
Sample Pipeline Alternatives (for ARM ISA)
ARM
(3-stage
pipeline)
StrongARM-
(5-stage
pipeline)
XScale
(7-stage
pipeline)
ALU
IM1 IM2 DM1 Reg DM
IM Reg (^) EX
PC update IM access
decode reg access
ALU op DM access shift/rotate commit result (write back)
ALU
IM Reg DM Reg
Reg (^) SHFT
PC update BTB access start IM access IM access
decode reg 1 access
shift/rotate reg 2 access
ALU op
start DM access exception
DM write reg write
Peer Instruction
Suppose a big data cache results in a data cache latency of 2 clock cycles and a
6-stage pipeline. (Pipelined, so can do 1 access / clock cycle.) What is the
impact?
1. Instruction bandwidth is now 5/6-ths of the 5-stage pipeline
2. Instruction bandwidth is now 1/2 of the 5-stage pipeline
3. The branch delay slot is now 2 instructions
4. The load-use hazard can be with 2 instructions following load
5. Both 3 and 4: branch delay and load-use now 2 instructions
6. None of the above
Clock
Cycle 1 Cycle 2 Cycle 3 Cycle 4 Cycle 5 Cycle 6 Cycle 7
1st lw
2nd lw
3rd lw
Ifetch Reg/Dec Exec Mem1 Mem2 Wr
Ifetch Reg/Dec Exec Mem1 Mem2 Wr
Ifetch Reg/Dec Exec Mem1 Mem2 Wr