RTL Design / Module 8: Pipelining for Performance
Lesson 8.2

Pipeline hazards — structural, data & control

When multiple operations overlap in a pipeline, hazards threaten correctness or stall progress. Every pipelined block needs a hazard plan: detect, forward, stall, or flush.

You will implement pieces of this plan across the next three lessons — stalls and bubbles (8.3), forwarding (8.4), then the pipelined ALU lab (8.5) where they all connect.

Hazard

A condition where the pipeline would produce the wrong result, or two stages need the same hardware at the same time, unless you intervene with control logic.

1

Three hazard families

TypeCauseTypical fix
StructuralTwo stages need same resource (ALU, memory port)Duplicate hardware, schedule, or stall
DataOperand not ready when consumer needs itForwarding, stall, or compiler scheduling
ControlBranch/jump changes PC before pipe drainsFlush wrong-path ops, predict branch

Hazards are not bugs — they are the price of overlapping operations. Your job is to pick the cheapest fix per case: duplicate hardware beats stall if area allows; forward beats stall if timing allows; predict beats flush if accuracy is high.

Hazard taxonomy

Design order: Eliminate structural conflicts first (memory ports, single ALU), then add forwarding for RAW, then stall logic for load-use, then branch flush/predict for control. Each layer builds on the previous — skipping structural fixes makes stall logic a band-aid.

2

Structural hazards

Occurs when two pipeline stages need the same non-pipelined resource in the same cycle — e.g. instruction fetch and data load both hitting single-port memory.

Structural hazard single port memory

Modern SoCs rarely share one physical port between IF and MEM — but the pattern repeats everywhere: one DMA engine vs CPU, one AXI port vs two masters, one multiplier vs two MAC lanes. The RTL pattern is identical: arbitrate, stall the lower-priority client, or duplicate the resource.

Area trade-off

Dual-port SRAM costs ~1.6–2× the area of single-port at the same depth. If structural stalls happen <5% of cycles, a cheap arbiter + stall is often the right call. If stalls dominate, pay for the second port.

structural_arb.v
// Simple structural hazard arbiter — MEM wins over IF
wire mem_req = mem_stage_active && mem_read_en;
wire if_req  = if_stage_active  && !mem_req;

assign mem_port_en   = mem_req;
assign mem_port_addr = mem_req ? mem_addr : if_pc;
assign if_stall      = mem_req && if_req;  // IF waits for port
3

Data hazards — RAW, WAR, WAW

Data hazards appear when instruction B needs a value that instruction A has not finished writing. In RTL you detect them by comparing destination registers in later stages against source registers in EX.

NamePatternIn-order pipe?
RAW (Read After Write)Consumer reads before producer writesYes — most common; needs forward or stall
WAR (Write After Read)Writer overwrites before reader finishesRare in classic 5-stage; issue in OoO rename
WAW (Write After Write)Two writers, wrong commit orderRare in-order; rename handles in OoO
RAW data hazard timeline
raw_detect.v
// RAW detect: EX-stage consumer needs register written by prior op
wire raw_hazard =
     ex_reg_read_en && ex_rs_addr != 5'd0 && (
       (mem_reg_write && mem_rd_addr == ex_rs_addr) ||
       (wb_reg_write  && wb_rd_addr  == ex_rs_addr)
     );

// Action: assert forward mux select OR stall EX/ID
hazard_unit_top.v
// Combine hazard outputs — priority: structural stall overrides forward
wire stall_from_struct = mem_port_busy && if_mem_conflict;
wire stall_from_load   = load_use_hazard;
wire stall_if_id       = stall_from_struct | stall_from_load;
wire stall_id_ex       = stall_from_load;

wire use_fwd_a = raw_hazard_rs1 && !stall_from_load;
wire use_fwd_b = raw_hazard_rs2 && !stall_from_load;

// load-use: stall wins — forwarding cannot supply data yet
WAR / WAW — light touch

In a simple in-order pipeline with one writeback per cycle, WAR and WAW rarely appear because reads happen in ID and writes in WB — strictly ordered. They matter when you add bypass paths, dual-issue, or out-of-order retirement. Know the names; focus RTL effort on RAW.

Trap

Detecting RAW on x0 as destination causes false stalls — RISC-V x0 is not a real register. Always gate with rd != 5'd0 on both producer and consumer sides.

4

Control hazards — branches & jumps

Branch outcome is known in EX (or later). Meanwhile IF/ID already fetched fall-through or wrong-target instructions. You must flush the wrong path.

The penalty is measured in lost fetch slots: a 5-stage pipe with branch in EX typically wastes 2 instructions (IF + ID) per taken branch. Branch prediction moves the penalty to mispredicts only — covered in advanced CPU design; for now implement correct flush-on-taken.

Control hazard branch flush
branch_flush.v
wire branch_taken = ex_branch && ex_cond_true;
wire flush_if_id  = branch_taken;
wire flush_id_ex  = branch_taken;  // optional if branch in EX

assign next_pc = branch_taken ? ex_target_pc : pc_plus4;

// IF/ID registers load NOP when flush_if_id asserted
Trap

Forgetting to squash reg_write on flushed ops can corrupt the register file. Flush must zero side effects: no writeback, no memory store, no CSR update.

JAL / JALR note

Unconditional jumps also need PC redirect + flush, but there is no "not taken" path — every jump is a control hazard. Link register write (rd=ra) must still commit from the jump instruction in EX while IF/ID flush.

5

Hazard response summary

Use this table as your implementation checklist. When sim fails on a dependent instruction pair, identify which row applies before randomly adding stalls.

HazardDetectFix priority
StructuralResource busy signalStall lower-priority stage; add port if frequent
RAW (EX needs WB)Addr match + reg_writeForward from MEM/WB; else stall 1 cycle
Load-use RAWLoad in MEM, use in EXMust stall — data not ready until WB
ControlBranch resolved takenFlush IF/ID; redirect PC; predict to hide
▶ Simulate hazard scenarios
6

Hazard checklist

  • Every shared resource has arbitration or duplicate hardware
  • RAW detection covers EX/MEM and EX/WB paths
  • Load-use case identified — forwarding alone is insufficient
  • Branch flush kills side effects on wrong-path ops
  • Simulation includes back-to-back dependent instructions
  • Structural arbiter priority documented (MEM vs IF)
7

Simulating hazards in your testbench

Pipeline bugs rarely show on isolated instructions. Build directed tests that exercise each hazard row in the summary table — one test per row, run back-to-back in the same sim.

hazard_tb_tasks.sv
// Directed hazard tests — run sequentially in one sim
task test_raw_alu_alu;  // ADD then SUB on same reg — expect forward
  reset_pipe();
  inject_instr(ADD, rd=1, rs1=2, rs2=3);
  inject_instr(SUB, rd=4, rs1=1, rs2=5);
  @(posedge clk); @(posedge clk); @(posedge clk);
  check_reg(4, expected_sub_result);
endtask

task test_load_use;     // LW then ADD — expect 1-cycle stall
  reset_pipe();
  inject_instr(LW,  rd=1, rs1=2);
  inject_instr(ADD, rd=3, rs1=1, rs2=4);
  monitor_stall(1);  // exactly one stall cycle
endtask

task test_branch_taken; // branch + wrong-path ops — expect flush
  reset_pipe();
  inject_instr(BEQ, taken=1, target=0x100);
  inject_instr(ADD, ...);  // wrong path — must not write
  @(posedge clk);
  check_no_reg_write_from_wrong_path();
endtask

Debug tip: Waveform markers at stall_if_id, flush_if_id, and fwd_a_sel let you correlate a wrong register value with the exact cycle the hazard unit failed.

🎯 What to remember

  • Structural = hardware conflict; data = wrong operand timing; control = wrong PC path.
  • RAW dominates in-order RTL — detect register addr match across stages.
  • WAR/WAW matter in advanced cores; know them, don't over-engineer a simple pipe.
  • Every hazard fix is stall, forward, flush, or duplicate — pick the cheapest that meets timing.
  • Directed TB tasks per hazard type catch regressions before full program sims.
Advertisement