Skip to content
TheChatArchiveThe Chat Archive
GPT 5.1 Codex Max· openaiHugging Face Frontier Reasoning Traces

Analyze a 5-stage MIPS pipeline data hazard where ADD writes to a register that SUB…

  1. User

    Analyze a 5-stage MIPS pipeline data hazard where ADD writes to a register that SUB immediately reads. Design forwarding unit logic that bypasses the ALU result from EX stage directly to the next instruction's EX stage.

  2. Assistant

    Designing forwarding logic for EX stage hazard

    The forwarding unit compares the EX/MEM destination register with the current EX stage source registers and sets control signals to forward the ALU result from EX/MEM directly to the next instruction’s ALU inputs, resolving the RAW hazard without stalls by bypassing data one cycle early. In the textbook five–stage MIPS the architectural state is only updated in the WB stage. If you feed an ADD and then immediately a SUB that uses the ADD’s destination, the naive pipeline looks like this:

              C1    C2    C3    C4    C5    …
    ADD       IF    ID    EX    MEM   WB
    SUB             IF    ID    EX    MEM   WB
    

    SUB reads its operands out of the register file in its ID stage in cycle 3, but the ADD doesn’t write the new value back until its WB stage in cycle 5, so without any intervention SUB will see the old value – a classic RAW hazard.

    The key observation is that the ADD’s result is available a lot earlier than WB: it appears on the ALU result bus at the end of cycle 3 and is latched into EX/MEM in cycle 4. If we make that value an alternate source for the next instruction’s ALU inputs, we can avoid stalling.

    A typical forwarding unit therefore has:

    • Inputs: the source register numbers in ID/EX (ID_EX.Rs, ID_EX.Rt), the destination register numbers and RegWrite bits in EX/MEM and MEM/WB.
    • Outputs: control signals to two multiplexers in front of the ALU, usually called ForwardA and ForwardB.

    One way of coding the combinational logic is:

    // default: take operands from register file as latched in ID/EX
    ForwardA = 2'b00;
    ForwardB = 2'b00;
    
    // bypass from EX/MEM (result of previous ALU op)
    if (EX_MEM.RegWrite && EX_MEM.WriteReg != 5'd0 &&
        EX_MEM.WriteReg == ID_EX.Rs)
        ForwardA = 2'b10;
    
    if (EX_MEM.RegWrite && EX_MEM.WriteReg != 5'd0 &&
        EX_MEM.WriteReg == ID_EX.Rt)
        ForwardB = 2'b10;
    
    // otherwise, bypass from MEM/WB (older instruction)
    if (MEM_WB.RegWrite && MEM_WB.WriteReg != 5'd0 &&
        MEM_WB.WriteReg == ID_EX.Rs && ForwardA == 2'b00)
        ForwardA = 2'b01;
    
    if (MEM_WB.RegWrite && MEM_WB.WriteReg != 5'd0 &&
        MEM_WB.WriteReg == ID_EX.Rt && ForwardB == 2'b00)
        ForwardB = 2'b01;
    

    The ForwardA/B signals drive 3‑way muxes at the ALU inputs: 00 selects the register operand from ID/EX, 10 selects the ALU result from EX/MEM (the immediately preceding instruction’s EX result), and 01 selects the value being written back in MEM/WB. In the ADD/SUB sequence above, when SUB reaches EX in cycle 4 the forwarding unit asserts ForwardA/B = 2'b10 for the matching source register, and the ALU sees the ADD result directly from EX/MEM without waiting for WB.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.