Sub topic 16
Solving GATE-Style Pipelining Problems
Assumptions and the solving method
Assume you know CPU registers, memory, clock cycles, and simple assembly instructions such as add, lw, sw, and branch instructions. You should also know basic speedup and ratio calculations.
Most pipelining questions become manageable when you separate them into four parts:
- Find the clock period.
- Count the ideal pipeline cycles.
- Identify hazards and add their stall cycles.
- Compute the requested performance value.
A \(k\)-stage pipeline executing \(n\) instructions needs
\[ k+n-1 \]
cycles when there are no stalls. If the pipeline has \(s\) extra stall cycles, the total becomes
\[ k+n-1+s. \]
Timing and performance calculations
The clock period must be long enough for the slowest stage and the pipeline register:
\[ T_{\text{clock}}=\max(\text{stage delays})+\text{latch delay}. \]
For stage delays of 60, 80, 70, 90, and 50 ns, with a 10 ns latch delay:
\[ T_{\text{clock}}=90+10=100\text{ ns}. \]
For 100 instructions in this five-stage pipeline:
\[ T_{\text{pipeline}}=(5+100-1)\times100=10{,}400\text{ ns}. \]
Without pipelining, each instruction takes the sum of all stage delays:
\[ T_{\text{non-pipeline}}=100(60+80+70+90+50)=35{,}000\text{ ns}. \]
Therefore,
\[ \text{speedup}=\frac{35{,}000}{10{,}400}\approx3.37. \]
Do not assume that a five-stage pipeline always gives a five-times speedup. Latch overhead, unequal stage delays, pipeline fill time, and stalls reduce the speedup.
stage_delays = [60, 80, 70, 90, 50]
latch_delay = 10
n = 100
clock = max(stage_delays) + latch_delay
pipeline_time = (len(stage_delays) + n - 1) * clock
non_pipeline_time = n * sum(stage_delays)
print(pipeline_time) # 10400
print(non_pipeline_time) # 35000
print(round(non_pipeline_time / pipeline_time, 2)) # 3.37Hazard questions
First label every dependency.
| Hazard | Meaning | Typical example |
|---|---|---|
| Structural | Two instructions need one resource | Two memory accesses need one memory |
| RAW | A later instruction reads an earlier result | `add r1,...` followed by `sub ...,r1,...` |
| WAR | A later instruction writes before an earlier read | Common in out-of-order execution |
| WAW | Two writes complete in the wrong order | Common in out-of-order execution |
| Control | The next instruction depends on a branch | Conditional branch |
In a basic five-stage in-order pipeline, RAW and control hazards are the most common. Apply the forwarding assumption exactly as stated in the question.
- Without forwarding, an
addfollowed by a dependent instruction can require two stalls under the usual half-cycle register-file convention. - With forwarding, that same dependency may require no stall.
- A load-use dependency usually requires one stall even with forwarding because the loaded value is available only after the memory stage.
CPI and branch penalties
Use the average-stall formula:
\[ \text{CPI}{\text{effective}} =\text{CPI}{\text{ideal}}+\sum(\text{frequency}\times\text{penalty}). \]
For ideal CPI 1, branches occurring 20% of the time with a two-cycle penalty, and load-use stalls occurring 10% of the time with a one-cycle penalty:
\[ \text{CPI}=1+(0.20)(2)+(0.10)(1)=1.5. \]
If a branch predictor is wrong, add the misprediction frequency multiplied by the flush penalty. A common mistake is adding the penalty for every branch instead of only for mispredicted branches.
Exercise
A four-stage pipeline executes 20 instructions with no stalls. How many cycles are required?
Show answer
Use k + n - 1: 4 + 20 - 1 = 23 cycles.