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.
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.
Two bypass highways feed EX operand muxes. Closest (most recent) result wins on address match.
| Forward | Source | Data | When valid |
|---|---|---|---|
| Forward A | EX/MEM | ex_mem_alu_result | ALU op in EX/MEM (not load) |
| Forward B | MEM/WB | mem_wb_wdata | Any op completing WB (incl. load) |
| Regfile | ID/EX read | rf_rdata | No 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.
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.
At cycle 5 the consumer's EX stage mux selects EX/MEM because rd==rs1 and the producer is an ALU op.
// 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;
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.
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: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);
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
// 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
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.
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
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.
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 gap | Forward? | Stall? |
|---|---|---|
| ALU β next instr EX | Yes (EX/MEM) | No |
| ALU β 2-away EX | Yes (MEM/WB) | No |
| Load β next instr EX | No | Yes (1 cycle) |
| Load β 2-away EX | Yes (MEM/WB) | No |
| Store (no rd) | N/A | No |
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.
!mem_read (not for loads)rd != 0 check)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.
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.