What Are Pipeline Hazards in a 5-Stage RISC-V Pipeline?
A pipeline hazard is a situation where an instruction can't move to its next stage in the next cycle without getting the wrong result. In the classic 5-stage RISC-V pipeline the common one is a data hazard: an instruction needs a register that an earlier instruction hasn't written back yet. Forwarding removes most of these stalls by passing results straight from one stage to another; a load followed immediately by a use of the loaded value still costs one stall.
What you'll learn
- The five stages and what each does
- The three kinds of hazard: structural, data and control
- How forwarding works and which hazard it can't remove
- A worked example with cycle counts, with and without forwarding
Conventions and assumptions
The textbook 5-stage pipeline: IF (fetch), ID (decode and read registers), EX (ALU), MEM (data memory) and WB (write back). The register file writes in the first half of a cycle and reads in the second, so an instruction in ID can read a value written back in the same cycle. Cycle counts in this post come from the pipeline visualizer on this site and are checked in its tests.
The three kinds of hazard
| Hazard | Cause | Usual fix |
|---|---|---|
| Structural | Two instructions need the same hardware in one cycle | Separate instruction and data memories; enough register file ports |
| Data | An instruction reads a register before an earlier one has written it | Forwarding, plus a stall after a load |
| Control | The next instruction to fetch depends on a branch not yet resolved | Predict, then flush the wrong-path instructions |
Data hazards and forwarding
Take add x1, x2, x3 followed by sub x4, x1, x5. Without forwarding, sub reads x1 in ID while add is still in EX, so sub has to wait in ID until add writes back: 2 stall cycles. But the sum exists at the end of add's EX stage. Forwarding adds multiplexers in front of the ALU that take a value from the EX/MEM or MEM/WB pipeline register instead of the register file, so sub can run EX the very next cycle with no stall.
The load-use hazard
lw x1, 0(x2) gets its data at the end of MEM, one stage later than an ALU result. If the next instruction needs x1 in its EX stage, that is the same cycle the load is still reading memory, so even forwarding can't help: the hazard unit inserts one bubble, and the value is forwarded from MEM/WB a cycle later. Compilers try to fill that slot with an independent instruction.
lw x1, 0(x2)
sub x4, x1, x5
and x6, x1, x7
or x8, x1, x9
add x10, x4, x6Worked example: counting the cycles
| Instruction | Stalls, forwarding | Stalls, no forwarding | Why |
|---|---|---|---|
| lw x1, 0(x2) | 0 | 0 | First instruction |
| sub x4, x1, x5 | 1 | 2 | Load-use on x1 |
| and x6, x1, x7 | 0 | 0 | x1 forwarded, or already in the register file |
| or x8, x1, x9 | 0 | 0 | x1 written back by now |
| add x10, x4, x6 | 0 | 1 | Needs x6 from and, two instructions earlier |
| Total cycles | 10 | 12 | 5 + 4 + stalls |
Control hazards
A branch isn't resolved until its comparison runs. If the pipeline predicts not taken and keeps fetching the next instructions, a taken branch resolved in EX means two wrong instructions must be flushed: a 2-cycle penalty. Moving the comparison into ID cuts that to 1 cycle, and real processors add branch predictors so that most branches cost nothing.
Common mistakes
- Counting a stall for an ALU result used next when forwarding is on. Only loads cost a stall.
- Forgetting the split-cycle register file: an instruction three after the producer reads the new value without any forwarding.
- Treating x0 as a dependency. It is always zero, so there is nothing to wait for.
- Assuming forwarding fixes control hazards. It moves data, not the decision of what to fetch.
Check your understanding
- Q1. With forwarding, how many stalls does add x1, x2, x3 followed by sub x4, x1, x5 cost? Answer: none. The sum is forwarded from EX/MEM to the ALU.
- Q2. Why does a load followed by a use still stall once with forwarding? Answer: the loaded value only exists at the end of MEM, the same cycle the next instruction needs it at the start of EX.
- Q3. How many cycles do 5 independent instructions take? Answer: 9, that is 5 + 4, with no stalls.
Related
Sources
Last verified
Cycle counts computed with the pipeline visualizer on 14 November 2026 and checked in its tests against the textbook rules: two stalls for a dependent instruction without forwarding, one for a load-use with forwarding.