RTL Design / Module 8: Pipelining for Performance
Lesson 8.4

Forwarding / bypassing logic

Forwarding (bypassing) routes a result directly from a later pipeline stage back to the EX stage ALU inputs β€” so dependent instructions don't wait for WB. It eliminates most RAW stalls except the infamous load-use case.

Think of forwarding as a shortcut around the register file. The regfile still updates on WB for architectural state, but EX can see "future" results one or two stages early via combinational bypass muxes.

Forwarding

Instead of reading a stale value from the register file, mux the ALU operand from EX/MEM or MEM/WB when the producing instruction's destination matches the consumer's source register.

1

Forwarding paths β€” EX/MEM and MEM/WB β†’ EX

Forwarding paths diagram

Two bypass highways feed EX operand muxes. Closest (most recent) result wins on address match.

ForwardSourceDataWhen valid
Forward AEX/MEMex_mem_alu_resultALU op in EX/MEM (not load)
Forward BMEM/WBmem_wb_wdataAny op completing WB (incl. load)
RegfileID/EX readrf_rdataNo hazard or x0

Forwarding paths are combinational in the EX stage β€” they sit between pipeline register outputs and ALU inputs. They do not add pipeline latency, but they do widen the EX stage critical path. If timing fails after adding bypass muxes, register the forward data one stage earlier or pipeline the ALU.

Timing note: The forward mux sits on the EX critical path. Synthesis may duplicate comparators for rs1 and rs2 β€” expect modest area growth beyond the muxes themselves.

2

Mux priority β€” closest stage wins

When multiple stages match the same source register, pick the youngest producer β€” EX/MEM beats MEM/WB because it is more recent.

Consider three back-to-back instructions: add x1,x2,x3 then add x4,x1,x5 then sub x6,x1,x7. The second ADD needs x1 from EX/MEM (one away); the SUB needs x1 from MEM/WB (two away) if the first ADD is still in MEM. Detection is purely address compare + reg_write gating.

Forwarding timeline worked example

At cycle 5 the consumer's EX stage mux selects EX/MEM because rd==rs1 and the producer is an ALU op.

forward_detect.v
// Operand A forwarding β€” rs1 in ID/EX
wire fwd_a_from_ex_mem =
     ex_mem_reg_write && !ex_mem_mem_read &&  // ALU result, not load addr
     (ex_mem_rd == id_ex_rs1) && (id_ex_rs1 != 5'd0);

wire fwd_a_from_mem_wb =
     mem_wb_reg_write &&
     (mem_wb_rd == id_ex_rs1) && (id_ex_rs1 != 5'd0) &&
     !fwd_a_from_ex_mem;  // EX/MEM wins if both match

// 2-bit mux select: 00=regfile, 01=EX/MEM, 10=MEM/WB
wire [1:0] fwd_a_sel =
  fwd_a_from_ex_mem ? 2'b01 :
  fwd_a_from_mem_wb ? 2'b10 : 2'b00;
Trap

Forwarding a load from EX/MEM is wrong β€” at EX/MEM the value is still a memory address, not loaded data. Gate EX/MEM forward with !ex_mem_mem_read. Load data only forwards from MEM/WB.

Double match

If EX/MEM and MEM/WB both write the same rd (impossible for adjacent ops in simple pipes) or if a multicycle op holds EX/MEM across cycles, MEM/WB must lose when EX/MEM is valid. The !fwd_a_from_ex_mem guard on the MEM/WB path enforces this.

3

Operand mux β€” Verilog sketch

forward_mux.v
// 3:1 forward mux for ALU operand A
function automatic [31:0] forward_mux (
  input [1:0]       sel,
  input [31:0]      rf_data,
  input [31:0]      ex_mem_result,
  input [31:0]      mem_wb_data
);
  case (sel)
    2'b00:   forward_mux = rf_data;
    2'b01:   forward_mux = ex_mem_result;
    2'b10:   forward_mux = mem_wb_data;
    default: forward_mux = rf_data;
  endcase
endfunction

wire [31:0] alu_a = forward_mux(fwd_a_sel, rf_rs1, ex_mem_alu, mem_wb_wdata);
wire [31:0] alu_b = forward_mux(fwd_b_sel, rf_rs2, ex_mem_alu, mem_wb_wdata);

wire [31:0] alu_result = alu(alu_a, alu_b, id_ex_alu_op);
forward_unit.v
module forward_unit (
  input  wire [4:0] id_ex_rs1, id_ex_rs2,
  input  wire       ex_mem_reg_write, ex_mem_mem_read,
  input  wire [4:0] ex_mem_rd,
  input  wire       mem_wb_reg_write,
  input  wire [4:0] mem_wb_rd,
  output wire [1:0] fwd_a_sel, fwd_b_sel
);
  wire ex_a = ex_mem_reg_write && !ex_mem_mem_read &&
              ex_mem_rd != 5'd0 && ex_mem_rd == id_ex_rs1;
  wire wb_a = mem_wb_reg_write && mem_wb_rd != 5'd0 &&
              mem_wb_rd == id_ex_rs1 && !ex_a;

  assign fwd_a_sel = ex_a ? 2'b01 : wb_a ? 2'b10 : 2'b00;
  // Mirror logic for rs2 β†’ fwd_b_sel
endmodule
forward_tb_check.sv
// Self-check in simulation β€” catch stale regfile use
always @(posedge clk) begin
  if (id_ex_valid && fwd_a_sel == 2'b00 && raw_hazard_rs1)
    $error("RAW on rs1 but no forward mux select");
  if (fwd_a_sel == 2'b01 && ex_mem_mem_read)
    $error("Forwarded load from EX/MEM β€” illegal");
end
4

When forwarding is not enough β€” load-use

If the producer is a LW (load word), data is not available until after MEM. The consumer in EX needs it now β€” no bypass can help. Must stall one cycle.

After the stall, the load result sits in MEM/WB and forwards normally on the next attempt. That is why load-use costs exactly one cycle in a classic 5-stage RISC pipe β€” not two.

Load-use hazard cannot forward
load_use_stall.v
wire load_use =
     id_ex_reg_read_rs1 && ex_mem_mem_read &&
     (ex_mem_rd == id_ex_rs1 || ex_mem_rd == id_ex_rs2) &&
     ex_mem_rd != 5'd0;

assign stall_if_id = load_use;
assign stall_id_ex = load_use;
assign stall_pc    = load_use;
// After stall, MEM/WB forward path supplies load data
Compiler angle

Compilers insert NOPs between load and use to avoid stalls. In RTL you still need the stall detector β€” software cannot always schedule around every dynamic dependency.

5

Forwarding vs stall β€” decision matrix

This matrix is the fastest way to debug IPC loss. If simulation shows a stall on an ALU→ALU pair, your forward detect is broken — not your stall logic.

Producer β†’ Consumer gapForward?Stall?
ALU β†’ next instr EXYes (EX/MEM)No
ALU β†’ 2-away EXYes (MEM/WB)No
Load β†’ next instr EXNoYes (1 cycle)
Load β†’ 2-away EXYes (MEM/WB)No
Store (no rd)N/ANo
Trap

Forwarding to the branch comparator in ID while also forwarding to EX duplicates hazard logic. Pick one stage for branch operand resolve β€” typically ID with MEM/WB forward only, or move branch to EX and accept 2-cycle penalty.

β–Ά Verify forwarding in simulator
6

Forwarding checklist

  • EX/MEM forward gated with !mem_read (not for loads)
  • Mux priority: EX/MEM over MEM/WB on double match
  • x0 never forwards (rd != 0 check)
  • Both rs1 and rs2 have independent forward muxes
  • Load-use stall wired; TB has back-to-back LW + ADD
  • Forward paths verified on both ALU operands independently
7

End-to-end hazard path β€” detect to mux

Forwarding is not a standalone block β€” it sits inside the hazard unit next to stall and flush logic. The data flow each cycle is: read pipeline register fields β†’ compare addresses β†’ generate mux selects and stall signals β†’ mux operands before ALU combo logic.

Hazard unit data flow

One hazard unit module avoids conflicting stall signals from scattered compare blocks.

Lab preview: The pipelined ALU lab ties these pieces together β€” pipe registers, forward mux, load-use stall, and branch flush in one block you can simulate end-to-end.

When integrating the forward unit, pass the same ex_mem_rd and mem_wb_rd fields your writeback logic uses β€” off-by-one stage wiring is the most common forwarding bug in student pipelines.

🎯 What to remember

  • Forwarding routes EX/MEM and MEM/WB results back to EX ALU inputs.
  • Closest matching stage wins the mux β€” EX/MEM before MEM/WB.
  • Never forward load data from EX/MEM β€” only from MEM/WB after memory returns.
  • Load-use is the one RAW case that still needs a 1-cycle stall.
  • Centralize hazard + forward logic in one module β€” avoid scattered compare blocks.
Advertisement