A control hazard happens when the pipeline does not yet know which instruction comes next. In your 3-stage core, a branch or jump is decided in X, but by then F has already fetched the instruction after it in memory. If the branch is taken, that instruction is the wrong one, and it must be thrown away before it changes anything. This article explains how the flush works, why one cycle is lost for every taken branch and jump, and how to predict the exact cycle count of any program from its listing. With the flush, exercise E16, your pipeline runs every program the single-cycle core runs.
9 min read | Intermediate
What You'll Learn
- Explain why a taken branch or jump in X leaves a wrong instruction in F
- Flush that instruction with the F/X valid bit, and stop a flushed instruction from redirecting or writing
- Derive the cycle count of the 3-stage core: instructions + taken branches and jumps + 2
- Explain what "predict not taken" means and what a branch predictor would change
Before You Start
The Verilog change is small: a few gates on the valid bits (Verilog Tutorial). Branches and Jumps covers how branches are compiled and how often they are taken, and Load-Use Stalls and Control Hazards explains flushing in a five-stage pipeline. Pass E15 first: E16 adds the flush to your pipeline with forwarding.
The Hazard
The PC module fetches a new instruction every cycle, and without a redirect it fetches the next one in memory, at PC + 4. That is correct for every instruction except taken branches and jumps. Here is the loop from branch.S, whose bne jumps back to the loop's first instruction while the loop runs:
In cycle 2 the bne is in X, where the comparator finds x3 is not zero and the branch is taken. In the same cycle F is fetching the instruction at the next address, jal x5, sub1, because nobody has told it otherwise yet. At the end of cycle 2 the redirect takes effect: the PC becomes the branch target, and F fetches addi x4, x4, 3 in cycle 3. But the jal is already in the F/X register. If nothing stops it, it moves into X in cycle 3 and executes: it writes a return address into x5 and jumps to sub1, which the program never meant to do.
The Flush
Throwing an instruction away before it has any effect is called a flush. In your pipeline it takes one change: at the clock edge at which X redirects the PC, the F/X valid bit becomes 0 instead of 1. The wrongly fetched instruction then enters X as an empty slot, and everything that could make it act is already gated by the valid bits: it does not write a register or memory in W, because the X/W valid bit is 0 too, and it does not count as retired.
There is one more place to gate: the redirect itself. A flushed instruction that happens to be a branch or a jump must not redirect the PC when it is in X. So redirect must include the F/X valid bit: an instruction in X redirects only if it is valid and it is a taken branch, a jal or a jalr. In the picture, the flushed jal would otherwise jump to sub1 from X in cycle 3 even though it was flushed.
Quick Check
Q: X holds a taken beq and F holds a sw. What prevents the store from writing memory, and in which cycle would it have written?
Show Answer
The F/X valid bit is cleared at the end of this cycle, so the store enters X invalid, and its X/W valid bit is 0 in the cycle after. dmem_we is gated by the X/W valid bit, so the store never writes. It would have written two cycles from now, when it reached W.
Predict Not Taken
Fetching PC + 4 while a branch is being decided is a guess: the pipeline bets that the branch is not taken. When the bet is right, nothing is lost: the instruction in F is the correct one, and it continues. When it is wrong, one instruction is flushed and one cycle is lost. jal and jalr are always "taken", so they always cost one cycle. This strategy, called predict not taken, needs no hardware beyond the flush.
Real processors predict better. A branch predictor in the fetch stage guesses, from the history of each branch, whether it will be taken and where it will go, so that F can fetch the target immediately. Loops make this easy: the loop branch above is taken nine times out of ten. Why Branches Hurt and Static Prediction, BTBs and Return Address Stacks describe how predictors work; your core leaves them out to keep the pipeline simple.
Where Does This Matter?
The cost of a wrong guess grows with the depth of the pipeline: the later a branch is decided, the more instructions have been fetched behind it. A five-stage pipeline that decides branches in EX flushes two instructions per taken branch; a deep high-performance core can lose 15 or more cycles on a misprediction, which is why such cores have predictors that guess correctly well over 90 percent of the time. Pipeline Depth vs Frequency discusses the trade-off.
Counting the Cycles
Now the cycle count of a program is exact and easy to predict. Every instruction completes one cycle after the one before it, except after a taken branch or jump, which adds one empty slot, and two cycles are spent filling the pipeline at the start:
cycles = instructions + taken branches and jumps + 2
The number of taken branches and jumps, the redirects, comes from running the program on the single-cycle core, which counts them without paying for them. For the seven programs:
| Program | Instructions | Redirects | Predicted cycles | Measured, 3-stage core |
|---|---|---|---|---|
| hello | 205 | 27 | 234 | 234 |
| sum | 8,205 | 2,048 | 10,255 | 10,255 |
| memcpy | 21,522 | 3,071 | 24,595 | 24,595 |
| bubble | 13,408 | 3,110 | 16,520 | 16,520 |
| matmul | 90,514 | 20,567 | 111,083 | 111,083 |
| list | 152,674 | 37,552 | 190,228 | 190,228 |
| crc | 71,695 | 9,216 | 80,913 | 80,913 |
The formula is exact on every program, on both simulators. Taken branches and jumps are between about one in eight and one in four of the instructions here, so the flush costs these programs between about 13 and 25 percent more cycles than the single-cycle core. Whether the pipeline is still faster depends on the clock, which Single-Cycle vs Pipelined: Cycles and Clock Period measures.
Quick Check
Q: A loop of 4 instructions, ending with a branch back to its start, runs 100 times. How many cycles does it take on the 3-stage core, counting from the first instruction of the loop and leaving out the pipeline fill?
Show Answer
400 instructions plus one cycle for each taken branch. The branch is taken 99 times and falls through once, so 400 + 99 = 499 cycles. Unrolling the loop, so that it branches half as often, would remove about half of the 99.
How It Is Tested
E16 runs branch.S, every branch taken and not taken plus calls and returns, write by write against the reference, and requires 90 cycles: 68 instructions + 20 redirects + 2. Then it runs the seven C programs and checks each result, the retire count and the exact cycle count from the table. A pipeline without a flush fails at the first taken branch, because the instruction behind it executes:
FAIL: register write 4: expected x31 = 0x00000001, got x31 = 0x000003e8
0x3E8 is 1,000: the addi x31, x31, 1000 that the first beq of the test jumps over, executed anyway. A pipeline that flushes after every branch, taken or not, and one whose flushed instructions still write registers, fail too.
Common Mistakes
Mistake #1: Letting a flushed branch redirect
Clearing the F/X valid bit is not enough if X still redirects on the instruction's opcode alone. Gate the redirect with the valid bit, or a flushed jump in F/X sends the program somewhere it was never meant to go.
Mistake #2: Flushing on every branch
The instruction behind a branch that is not taken is the correct next instruction. Flushing it as well loses it: the program skips an instruction after every not-taken branch.
Mistake #3: Gating only the register write
A flushed instruction must not write a register, write memory or count as retired. If dmem_we or retire is not gated by the valid bit in W, a flushed store still writes and the retire count is wrong.
Your Turn in the Lab
The exercise opens with a visible test, testbench.v, next to your design: the program branch.S, also checked for its exact cycle count, run on your core and compared write by write with the reference core, naming the first write that differs. Simulate it as you work and look at the waveform where it fails. When you submit, it runs together with the hidden tests, and your edits to it do not change the grading.
Add the flush to your E15 pipeline for E16. When it passes, your pipelined core runs every program the single-cycle core runs, in exactly the predicted number of cycles. Run your own C on it with the Software panel, as for the single-cycle core.
Open exercise E16 in ChipVerify LabSummary
- Branches and jumps are decided in X, after F has fetched the next instruction in memory; if the branch is taken, that instruction is wrong.
- The flush clears the F/X valid bit at the redirecting edge; the valid bits then stop the instruction from redirecting, writing or retiring.
- The pipeline predicts not taken: not-taken branches cost nothing, and taken branches, jal and jalr cost one cycle each.
- The 3-stage core takes exactly instructions + taken branches and jumps + 2 cycles, which matches every test program.
- Branch predictors fetch the target early and matter more the deeper the pipeline, because each wrong guess flushes more instructions.