A data hazard happens when an instruction needs a value that an earlier instruction, still in the pipeline, has not yet written. In your 3-stage core there is exactly one such case: an instruction in X that reads the register the instruction in W is writing in the same cycle. This article shows why the register file returns the old value then, how often it happens in real programs, and how a single forwarding path fixes it. You write the forwarding select logic in exercise E14 and add the path to your pipeline in E15. The article also explains why a load needs no stall in this design, and what would change with a more realistic memory.

10 min read | Intermediate

What You'll Learn

  • Explain when a read-after-write hazard occurs in the 3-stage pipeline and why the register file cannot resolve it
  • Measure how often back-to-back dependences occur in real programs
  • Design forwarding select logic and the operand multiplexers that use it, including the x0 and valid-bit cases
  • Explain why loads need no stall here and when a load-use stall becomes necessary

Before You Start

The Verilog here is small: comparisons and multiplexers (Verilog Tutorial). Data Hazards and Forwarding in the first half of the tutorial explains hazards and forwarding in a five-stage pipeline, and Load-Use Stalls and Control Hazards explains load-use stalls. Pass E13 first; E14 can be done any time.

The Hazard

Take two instructions from the test program, one right after the other:

    addi  x2, x0, 5
    addi  x2, x2, 1            # x2 used right after it is written

In the single-cycle core this is no problem: the first instruction writes x2 at the end of its cycle, and the second reads it in the next one. In the pipeline the two overlap:

Pipeline diagram: addi x2, x0, 5 is in W in cycle 3 while addi x2, x2, 1 is in X; the second needs x2 during cycle 3 but the first writes it only at the end of cycle 3, so W's value is forwarded to X. In cycle 4 the following add needs x2 from the second addi, which is then in W, and gets it forwarded again.

In cycle 3, the first addi is in W. Its result, 5, is on the register file's write port, and it will be stored at the clock edge that ends the cycle. In the same cycle the second addi is in X and reads x2 through the register file's read port. Your register file has no write-through, by specification: a read during a write returns the old value. So the second addi gets whatever x2 held before, and computes the wrong result.

This is a read-after-write hazard: the instruction reads a register after, in program order, an instruction that writes it, but the pipeline makes the read happen before the write takes effect. In the 3-stage core it can only happen between neighbours, an instruction in X and the one just ahead of it in W. Any instruction further ahead has already left the pipeline and written its register.

How Often It Happens

Back-to-back dependences are not rare. Compilers often use a result immediately, especially in tight loops. Counting over every instruction each program executes, the share of instructions that read a register written by the instruction just before them:

ProgramInstructionsDependent on the previous oneShare
hello2057536.6%
sum8,20550.1%
memcpy21,5229,22342.9%
bubble13,4084,67334.9%
matmul90,51415,25416.9%
list152,6746,6664.4%
crc71,69525,60535.7%

The sum loop happens to be scheduled so that no instruction uses its neighbour's result, but in the memory copy almost half the instructions do. A pipeline that got these wrong would be useless, and one that waited a cycle each time would lose much of what pipelining gains.

Quick Check

Q: Suppose the pipeline had no forwarding and instead waited one cycle whenever an instruction depended on the one just before it. How many extra cycles would that cost the CRC program, and by what fraction would its cycle count grow?

Show Answer

One cycle for each of its 25,605 dependent pairs, on top of its 80,913 cycles with forwarding: about a third more cycles. Forwarding removes all of them.

Forwarding

The value X needs exists in the same cycle: it is on W's write-back path, about to be written. Forwarding (also called bypassing) takes it from there and gives it to X directly, skipping the register file. Each of X's two register operands gets a multiplexer: the register file's value, or W's write-back value. A select signal for each says when to use W's value.

The select logic is its own small module, cvr32_fwd, for E14:

module cvr32_fwd (
    input  logic       w_we,      // the instruction in W writes a register
    input  logic [4:0] w_rd,      // the register it writes
    input  logic [4:0] rs1,       // the registers the instruction in X reads
    input  logic [4:0] rs2,
    output logic       fwd1,      // 1: use W's value for rs1
    output logic       fwd2       // 1: use W's value for rs2
);

fwd1 must be 1 exactly when three things are true: W writes a register, the register is not x0, and it is the instruction in X's rs1; fwd2 the same for rs2. Each condition matters:

  • W writes a register. w_we must be the register file's actual write enable in W: the instruction's reg_write and its valid bit. A store or a branch in W has an rd field too, but writes nothing, and an invalid W writes nothing either.
  • Not x0. An instruction that writes x0 writes nothing; x0 stays 0. Forwarding its value would give the next instruction a non-zero x0.
  • The numbers match. Compare with the instruction's register fields as the decoder extracts them. An instruction that does not use rs2, such as addi, may still match on the rs2 field; that is harmless, because nothing uses that operand.

In the core (E15), the forwarded operand values must replace the register file values everywhere X uses them: as ALU operands, in the branch comparator, and as the store data that goes into X/W. Missing one of these is the most common forwarding bug: the test catches a core whose branch comparator, or whose store data, still uses the register file's value.

Quick Check

Q: W holds sw x5, 0(x6) and X holds add x7, x5, x0. The store's rs2 field is 5 and its rd field (bits 11 to 7) happens to be 00101 as well. Should fwd1 be 1?

Show Answer

No. A store does not write a register: its bits 11 to 7 are part of its immediate, and its reg_write is 0, so w_we is 0. The add must read x5 from the register file. This is why the select logic uses the write enable, not just the rd field.

Why Loads Need No Stall Here

A load in W gets its data from the memory in the same cycle, through the load/store unit, onto the write-back path. So the forwarding path that carries an ALU result also carries a loaded value: a load followed directly by an instruction that uses its result works with no extra cycle. In the bubble sort, 2,016 instructions use a value loaded by the instruction just before them, one per comparison of the sort, and all of them run without waiting.

That is a consequence of the idealized memory, which answers reads in the same cycle. Real on-chip memories usually return data one clock edge after they receive the address. With such a memory the loaded value would arrive only at the end of W, too late to forward to the instruction in X, and that instruction would have to wait one cycle: a load-use stall. The bubble sort would then take 2,016 cycles longer. Five-stage pipelines have this stall even with fast memory, because the load's data arrives in MEM while the next instruction is already in EX; Load-Use Stalls and Control Hazards describes it.

Forwarding a load result has a cost in time, too. The path from the X/W register through the data memory, the load/store unit and the forwarding multiplexer into the ALU is the longest in the pipelined core, and it sets its clock; Single-Cycle vs Pipelined: Cycles and Clock Period measures it.

Where Is This Used?

Every pipelined processor forwards. A five-stage core needs two forwarding sources (the results in MEM and in WB) for each operand, and a wide out-of-order core forwards between dozens of execution units through a bypass network that is one of its largest and most timing-critical structures. Your two multiplexers and one comparison per operand are the smallest version of the same idea.

How It Is Tested

E14 tests the select logic exhaustively: every combination of w_we, w_rd and rs1, with rs2 equal to rs1 half the time and random otherwise, 4,096 checks. It catches forwarding from x0, forwarding when W writes nothing, and an fwd2 that compares with rs1.

E15 runs pipe_nohaz.S again and then pipe_fwd.S: 27 instructions with 20 register writes and 3 stores, and back-to-back dependences of every kind: ALU to ALU, load to ALU, into store data and into a store address, into a branch, into both operands at once, and a write to x0 that must not be forwarded. It still has no taken branches when the core is right. Both programs are checked write by write, and both must take instructions + 2 cycles. A pipeline without forwarding fails at the first instruction that depends on its neighbour, the third register write:

Output
FAIL: register write 3: expected x2 = 0x00000006, got x2 = 0xxxxxxxxx

It read the old x2, which in this program has never been written, so in Icarus Verilog it is X, and the X propagates into the result.

Common Mistakes

Mistake #1: Forwarding into the ALU only

The branch comparator and the store data also read rs1 and rs2. If they still use the register file's values, the ALU instructions work and the first dependent branch or store goes wrong.

Mistake #2: Forwarding from an instruction that writes nothing

Compare with W's real write enable, valid bit included. Stores, branches and empty stages have rd fields that mean nothing.

Mistake #3: Forwarding x0

An instruction may "write" x0, and the register file correctly ignores it. The forwarding logic must ignore it too, or the next instruction sees a non-zero x0.

Your Turn in the Lab

Each exercise opens with a visible test, testbench.v, next to your design: for the forwarding logic, a short list of directed checks; for the core, the program pipe_fwd.S, compared write by write with the reference. Simulate it as you work and add your own checks to it. When you submit, it runs together with the hidden tests, and your edits to it do not change the grading.

  1. E14: write cvr32_fwd.
  2. E15: add the forwarding multiplexers to your E13 pipeline and use the forwarded values everywhere X uses rs1 and rs2.
Open exercise E14 in ChipVerify Lab Open exercise E15 in ChipVerify Lab

Summary

  • In the 3-stage core, an instruction in X that reads the register the instruction in W is writing gets the old value, because the write takes effect only at the end of the cycle.
  • This happens often: up to 42.9% of instructions in the test programs depend on the instruction just before them.
  • Forwarding gives X the value on W's write-back path; it is selected when W really writes, the register is not x0, and it matches rs1 or rs2.
  • The forwarded values replace the register values for the ALU, the branch comparator and the store data.
  • Loads need no stall because memory answers in the same cycle; with a memory that answers a cycle later, each load-use pair, 2,016 in the bubble sort, would cost a stall.
Did you find what you were looking for?

Quiz

No quiz questions available for this article.