Pipelines: overlap work without erasing dependencies
Derive an ideal pipeline bound, then distinguish data hazards, structural conflicts, and control uncertainty.
On this page
The question for today
What you’ll understand
- Calculate fill-and-drain time for a simplified pipeline.
- Identify a true data dependency and a resource conflict.
- Explain why pipelining improves throughput without guaranteeing lower single-operation latency.
A pipeline divides work into stages so several instructions can be in progress at once. The image of an assembly line is useful only if you also account for the rules that keep instructions from consuming unavailable values or competing for one resource. We will use a fictional five-stage pipeline to reason about overlap. It is a teaching model, not a claim that a particular x86 or RISC-V implementation has five stages.
Throughput and latency measure different things
Suppose each stage takes one clock cycle and an instruction passes through five stages. One isolated instruction still needs five cycles from entry to completion. If the pipeline accepts a new independent instruction every cycle, however, ten instructions finish in fourteen cycles: five to complete the first, followed by one completion per cycle for the remaining nine. Pipelining creates overlap. It does not make one instruction magically skip its own work, and a longer clock period can offset apparent gains from a shallow stage count.
This ideal calculation assumes balanced stages, no stalls, enough input work, and a single result every cycle after filling. Real implementations may admit several operations per cycle or split instruction handling into many more steps. Branch recovery and finite queues complicate fill and drain. Treat the simple equation as a baseline that exposes missing costs, then add only the effects supported by the model or measurement. A pipeline diagram becomes misleading when its stage count is quietly substituted for a processor specification.
A hazard is a reason overlap needs coordination
A read-after-write dependency is a true data dependency: a consumer needs the result produced by an earlier operation. If an ADD creates a register value and the next ADD uses it, the second must receive the new value, not the stale register contents. Forwarding can deliver a result before ordinary register writeback when the implementation supports the path. Forwarding does not eliminate the time needed to produce the value; some producer-consumer combinations still require a stall.
A structural hazard occurs when simultaneous work needs a resource that cannot serve it all. In a teaching machine with one shared memory port, instruction fetch and a data load may conflict. A control hazard arises when the next path is unresolved. These categories suggest different remedies: a resource copy addresses one kind of conflict, prediction addresses some control uncertainty, and forwarding addresses some data availability. None is a general cure. Changing instructions can also shift pressure from one resource to another rather than remove the bottleneck.
Draw a schedule that states its assumptions
To test a schedule, label each cycle and mark when every source becomes ready and every resource is occupied. Ask whether a consumer reads the value before its producer can supply it. Then check whether two proposed actions claim the same exclusive resource. A legal schedule under an explicit model is more useful than an attractive diagram with unspecified forwarding. For a real kernel, use the vendor model and measurements because cache behavior and issue width can invalidate the fictional assumptions.
The queue lab illustrates a related capacity mismatch: arrivals enter at rate λ while service removes work at rate μ. Predict whether its fluid backlog grows or drains before pressing play. This is not a pipeline hazard simulator; it has no stages, registers, or forwarding paths. Its limited connection is that adding demand beyond a bottleneck’s service rate accumulates waiting work. A pipeline needs separate resources and dependency reasoning to explain why a particular instruction waits.
Work it through
Count fill, work, and stalls
A fictional five-stage, single-issue pipeline executes 20 instructions. Assume all stage times are one cycle, and the complete schedule contains six additional stall cycles with no overlap between them.
Without stalls, five cycles complete the first instruction and the remaining nineteen finish one per cycle. The ideal total is 5+20−1=24 cycles.
Add the stated six non-overlapped stall cycles to obtain 30 cycles. Average CPI is 30/20=1.5, even though each instruction traverses five stages.
At a fixed one-nanosecond cycle period, total time is 30 nanoseconds. The isolated pipeline traversal is five nanoseconds. Do not conclude that a different processor with fewer stages must finish the same workload sooner.
The model separates five-cycle latency, one-per-cycle ideal throughput, and a 1.5 measured-by-the-schedule average CPI.
The living notebook
Explore the model
Interactive lab · queue
A bottleneck accumulates waiting work
Change arrival rate λ and service rate μ in the fluid backlog max(0,4+(λ−μ)t). Predict growth or drainage. The separate M/M/1 residence estimate has its own assumptions; neither display simulates pipeline stages or register hazards.
- Fluid backlog
- 4
- Net rate · λ − μ
- −1 /s
- M/M/1 mean residence
- 1 s
Fluid backlog starts at 4 and follows max(0, 4 + (λ − μ)t). The separate M/M/1 residence-time formula assumes Poisson arrivals, exponential service and λ < μ. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forOverlapping work cannot exceed the service capacity of the limiting resource.
Pause & reason
Check your understanding
Your turn
Try it yourself
A four-stage single-issue teaching pipeline processes 12 independent instructions with three non-overlapped stall cycles. Calculate total cycles and CPI.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Count the first completion, the following completions, and then the stalls.
Compare with the solution
The ideal schedule uses 4+12−1=15 cycles. Adding three stalls gives 18 cycles and CPI=18/12=1.5. This result assumes one-cycle stages and no other delay; it does not specify the stage count or forwarding of any commercial processor.
Keep the idea
Take it with you
- Pipelining overlaps instructions and primarily improves throughput.
- Data, structural, and control hazards require different explanations.
- A schedule is meaningful only with explicit resource and forwarding assumptions.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.