A4 Solution
A4 Solution
Solution
1. (4 pts) Identify all the RAW data dependencies in the following code. Which dependencies are
data hazards that will be resolved by forwarding? Which dependencies are data hazards that will
cause a stall? Using a graphical representation of the pipeline, show the forwarding paths and
stalled cycles if any.
add $3, $4, $2
sub $5, $3, $1
lw $6, 200($3)
add $7, $3, $6
Solution:
RAW dependencies:
a) Without forwarding, the value being written into a register can only be read in the same
cycle. As a result, there will be a bubble of 2 cycles between a LW and the dependent
ADD to allow the LW to progress through the MEM and WB stages. Similarly, there
will be a bubble of 2 cycles between an ADD and the dependent LW.
b) With forwarding, there will be a bubble of 1 cycle between a LW and the dependent
ADD. However, no bubble exists between an ADD and the dependent LW.
Therefore, it takes only 3 cycles on average to to complete one LW and one ADD.
1 cycle (to complete LW) + 1 cycle (bubble) + 1 cycle (to complete ADD) = 3 cycles
3. (4 pts) A 10-stage instruction pipeline runs at a clock rate of 1 GHz. The instruction mix is such
that 15% of instructions cause one bubble to be inserted into the pipeline, and 10% of
instructions cause two bubbles to be inserted. The equivalent single-cycle implementation would
lead to a clock rate of 150 MHz.
a) What is the increase in the pipeline CPI over the ideal CPI as a result of bubbles?
b) What is the speedup of pipelined implementation over single-cycle?
Solution:
Increase in CPI due to bubbles = 0.15 * 1 + 0.1 * 2 = 0.35 cycles per instruction
Pipeline CPI with bubbles = 1 + 0.35 = 1.35 (35% increase over ideal CPI)
Solution: Yes, forwarding is possible and we can avoid stalling the pipeline. Consider:
We need a multiplexer at the input of EX/MEM.B register as show below. The data read from the
data memory in the MEM stage should be fed back at the input of this multiplexer. A control
signal “ForwardC” is needed to control the selection of this multiplexer. The Forwarding unit in
the DECODE stage will generate the “ForwardC” signal and pipeline it, after detecting the
dependency between a SW and a previous LW instruction. The SW instruction is currently in the
DECODE stage (MemWrite = 1). The LW instruction is in the EXE stage (ID/EX.MemRead = 1).
The ID/EX.RW register for the LW instruction contains the same register number as Rt for the
SW instruction. The Forwarding Unit can detect this situation and generate the ForwardC signal.
IF/ID ID/EX
ALUSrc MemtoReg
Imm26
Imm2
EX/MEM MEM/WB
Ex ALU
t
ALU result
m A
WriteData
Instruction
u Data m
A
Rs L
x m Memory u
Rt Register u
U
File
x
m x m
B
B
u u Data_in
x x
m
Rw
Rw
Rw
Rd u
x
ForwardB ForwardA
Forwarding Unit
ForwardC ForwardC
Op MemWrite
Control
Unit
MemRead MemRead