Describe a scenario where pipeline forwarding eliminates a data hazard.
- User
Describe a scenario where pipeline forwarding eliminates a data hazard.
- Assistant
Below is a concrete, textbook-style example that shows
• the hazard,
• how many cycles it would normally cost, and
• how a single forwarding path lets the CPU run with zero stalls.
Assumptions
Pipeline: 5 stages (IF – ID – EX – MEM – WB)
Forwarding paths: ALU-result can be sent from the EX/MEM pipeline register or MEM/WB pipeline register directly back to an ALU input of the very next instruction.
Instruction sequence that triggers a RAW (read-after-write) hazard
- ADD R1, R2, R3 ; R1 ← R2 + R3
- SUB R4, R1, R5 ; R4 ← R1 – R5 ←––– needs the value just produced
Timeline WITHOUT forwarding (one bubble inserted)
Cycle → 1 2 3 4 5 6 ADD IF ID EX MEM WB SUB IF STALL ID EX MEM Explanation:
• During cycle 3, SUB is in ID and needs R1, but ADD hasn’t written it back (will happen in cycle 5).
• The control logic inserts a bubble, pushing SUB one cycle later, costing 1 extra cycle.
Timeline WITH forwarding (no bubble)
Cycle → 1 2 3 4 5 ADD IF ID EX MEM WB SUB IF ID EX MEM What changed?
• At the end of cycle 3, ADD’s ALU produces the result (R1) and stores it in the EX/MEM register.
• At the very start of cycle 4, the forwarding unit detects that SUB needs R1, grabs the value from that EX/MEM register, and routes it straight to one of the ALU inputs.
• SUB can therefore execute in cycle 4 without waiting for the normal WB stage.
Key Take-away
Pipeline forwarding converts what would have been a 2-cycle producer/consumer distance (EX→WB then read) into an effective 0-cycle distance (EX→EX), eliminating the stall and saving a full pipeline cycle.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.