Stalls and flushes Last time we discussed data

  • Slides: 25
Download presentation
Stalls and flushes § Last time, we discussed data hazards that can occur in

Stalls and flushes § Last time, we discussed data hazards that can occur in pipelined CPUs if some instructions depend upon others that are still executing. — Many hazards can be resolved by forwarding data from the pipeline registers, instead of waiting for the writeback stage. — The pipeline continues running at full speed, with one instruction beginning on every clock cycle. § Today we’ll see some real limitations of pipelining. — Forwarding may not work for data hazards from load instructions. — Branches affect the instruction fetch for the next clock cycle. § In both of these cases we may need to slow down, or stall, the pipeline. 25 September 2020 © 2003 Craig Zilles (derived from slides by Howard Huang) 1

Data hazard review § A data hazard arises if one instruction needs data that

Data hazard review § A data hazard arises if one instruction needs data that isn’t ready yet. — Below, the AND and OR both need to read register $2. — But $2 isn’t updated by SUB until the fifth clock cycle. § Dependency arrows that point backwards indicate hazards. sub $2, $1, $3 and $12, $5 or $13, $6, $2 25 September 2020 1 2 IM Reg IM Clock cycle 3 4 DM Reg IM Stalls and flushes 5 7 Reg DM Reg 6 Reg DM Reg 2

Forwarding to the rescue! § The desired value ($1 - $3) has actually already

Forwarding to the rescue! § The desired value ($1 - $3) has actually already been computed—it just hasn’t been written to the registers yet. § Forwarding allows other instructions to read ALU results directly from the pipeline registers, without going through the register file. sub $2, $1, $3 and $12, $5 or $13, $6, $2 25 September 2020 1 2 IM Reg IM Clock cycle 3 4 DM Reg IM Stalls and flushes 5 7 Reg DM Reg 6 Reg DM Reg 3

What about loads? § Imagine if the first instruction in the example was LW

What about loads? § Imagine if the first instruction in the example was LW instead of SUB. — How does this change the data hazard? lw $2, 20($3) and $12, $5 25 September 2020 1 2 Clock cycle 3 4 IM Reg DM IM Reg Stalls and flushes 5 Reg DM 6 Reg 4

Stalling § The easiest solution is to stall the pipeline. § We could delay

Stalling § The easiest solution is to stall the pipeline. § We could delay the AND instruction by introducing a one-cycle delay into the pipeline, sometimes called a bubble. lw $2, 20($3) and $12, $5 1 2 IM Reg IM Clock cycle 3 4 DM Reg 5 6 7 DM Reg § Notice that we’re still using forwarding in cycle 5, to get data from the MEM/WB pipeline register to the ALU. 25 September 2020 Stalls and flushes 6

Stalling and forwarding § Without forwarding, we’d have to stall for two cycles to

Stalling and forwarding § Without forwarding, we’d have to stall for two cycles to wait for the LW instruction’s writeback stage. lw $2, 20($3) and $12, $5 1 2 IM Reg 3 Clock cycle 4 5 DM IM 6 7 8 DM Reg Reg § In general, you can always stall to avoid hazards—but dependencies are very common in real code, and stalling often can reduce performance by a significant amount. 25 September 2020 Stalls and flushes 7

Stalling delays the entire pipeline § If we delay the second instruction, we’ll have

Stalling delays the entire pipeline § If we delay the second instruction, we’ll have to delay the third one too. — Why? (two reasons) 1 lw $2, 20($3) and $12, $5 or $13, $12, $2 25 September 2020 IM 2 3 Reg IM Clock cycle 4 5 DM Stalls and flushes 7 8 Reg IM 6 DM Reg 8

Stalling delays the entire pipeline § If we delay the second instruction, we’ll have

Stalling delays the entire pipeline § If we delay the second instruction, we’ll have to delay the third one too. — This is necessary to make forwarding work between AND and OR. — It also prevents problems such as two instructions trying to write to the same register in the same cycle. 1 lw $2, 20($3) and $12, $5 or $13, $12, $2 25 September 2020 IM 2 3 Reg IM Clock cycle 4 5 DM Stalls and flushes 7 8 Reg IM 6 DM Reg 9

Implementing stalls § One way to implement a stall is to force the two

Implementing stalls § One way to implement a stall is to force the two instructions after LW to pause and remain in their ID and IF stages for one extra cycle. lw $2, 20($3) and $12, $5 or $13, $12, $2 1 2 IM Reg IM 3 Clock cycle 4 5 DM Reg IM IM 6 7 DM Reg 8 Reg DM Reg § This is easily accomplished. — Don’t update the PC, so the current IF stage is repeated. — Don’t update the IF/ID register, so the ID stage is also repeated. 25 September 2020 Stalls and flushes 10

What about EXE, MEM, WB § But what about the ALU during cycle 4,

What about EXE, MEM, WB § But what about the ALU during cycle 4, the data memory in cycle 5, and the register file write in cycle 6? lw $2, 20($3) and $12, $5 or $13, $12, $2 1 2 IM Reg IM 3 Clock cycle 4 5 DM Reg IM IM 6 7 DM Reg 8 Reg DM Reg § Those units aren’t used in those cycles because of the stall, so we can set the EX, MEM and WB control signals to all 0 s. 25 September 2020 Stalls and flushes 11

Stall = Nop conversion lw $2, 20($3) and -> nop and $12, $5 or

Stall = Nop conversion lw $2, 20($3) and -> nop and $12, $5 or $13, $12, $2 1 2 IM Reg IM 3 Clock cycle 4 5 DM Reg IM 7 8 Reg DM Reg IM 6 Reg DM Reg § The effect of a load stall is to insert an empty or nop instruction into the pipeline 25 September 2020 Stalls and flushes 12

Detecting stalls § Detecting stall is much like detecting data hazards. § Recall the

Detecting stalls § Detecting stall is much like detecting data hazards. § Recall the format of hazard detection equations: 25 September 2020 Stalls and flushes Reg DM memwb ex/mem id/ex Reg DM ex/mem IM id/ex and $12, $5 Reg if/id sub $2, $1, $3 IM if/id if (EX/MEM. Reg. Write = 1 and EX/MEM. Register. Rd = ID/EX. Register. Rs) then Bypass Rs from EX/MEM stage latch Reg 14

Detecting Stalls, cont. § DM memwb Reg ex/mem Reg memwb IM DM id/ex and

Detecting Stalls, cont. § DM memwb Reg ex/mem Reg memwb IM DM id/ex and $12, $5 Reg if/id IM id/ex $2, 20($3) if/id lw ex/mem When should stalls be detected? if/id § Reg What is the stall condition? if ( ) then stall 25 September 2020 Stalls and flushes 15

Adding hazard detection to the CPU Hazard Unit ID/EX EX/MEM WB Control PC WB

Adding hazard detection to the CPU Hazard Unit ID/EX EX/MEM WB Control PC WB MEM/WB EX M WB IF/ID Read register 1 Addr M Instr Read data 1 Read register 2 Write register Instruction memory Write data Read data 2 Registers 0 1 2 ALU Zero ALUSrc 0 1 2 Result 0 Address Data memory 1 Instr [15 - 0] Reg. Dst Extend Rt Write Read data 1 0 0 Rd 1 Rs EX/MEM. Register. Rd Forwarding Unit MEM/WB. Register. Rd 25 September 2020 Stalls and flushes 17

Generalizing Forwarding/Stalling § § § What if data memory access was so slow, we

Generalizing Forwarding/Stalling § § § What if data memory access was so slow, we wanted to pipeline it over 2 cycles? 1 2 IM Reg Clock cycle 3 4 DM 5 6 Reg How many bypass inputs would the muxes in EXE have? Which instructions in the following require stalling and/or bypassing? lw add April 2, 2003 r 13, 0(r 11) r 7, r 8, r 9 r 15, r 7, r 13 Stalls and flushes 20

Branches in the original pipelined datapath 1 0 PCSrc Control IF/ID 4 When are

Branches in the original pipelined datapath 1 0 PCSrc Control IF/ID 4 When are they resolved? ID/EX WB EX/MEM M WB MEM/WB EX M WB Add P C Add Reg. Write Read Instruction address [31 -0] Instruction memory Read register 1 Read data 1 Read register 2 Read data 2 Write register Write data Instr [15 - 0] Instr [20 - 16] Instr [15 - 11] 25 September 2020 Shift left 2 ALU 0 Mem. Write Zero Result 1 Registers ALUOp ALUSrc Sign extend Address Data memory Write data Reg. Dst Mem. To. Reg Read data 1 Mem. Read 0 0 1 Stalls and flushes 21

Branches § Most of the work for a branch computation is done in the

Branches § Most of the work for a branch computation is done in the EX stage. — The branch target address is computed. — The source registers are compared by the ALU, and the Zero flag is set or cleared accordingly. § Thus, the branch decision cannot be made until the end of the EX stage. — But we need to know which instruction to fetch next, in order to keep the pipeline running! — This leads to what’s called a control hazard. beq $2, $3, Label ? ? ? 25 September 2020 1 2 3 IM Reg Clock cycle 4 5 DM 6 7 8 Reg IM Stalls and flushes 22

Stalling is one solution § Again, stalling is always one possible solution. beq $2,

Stalling is one solution § Again, stalling is always one possible solution. beq $2, $3, Label ? ? ? 1 2 IM Reg 3 Clock cycle 4 5 DM Reg IM Reg 6 7 8 DM Reg § Here we just stall until cycle 4, after we do make the branch decision. 25 September 2020 Stalls and flushes 23

Branch prediction § Another approach is to guess whether or not the branch is

Branch prediction § Another approach is to guess whether or not the branch is taken. — In terms of hardware, it’s easier to assume the branch is not taken. — This way we just increment the PC and continue execution, as for normal instructions. § If we’re correct, then there is no problem and the pipeline keeps going at full speed. beq $2, $3, Label next instruction 1 next instruction 2 25 September 2020 1 2 IM Reg IM Clock cycle 3 4 DM Reg IM Stalls and flushes 5 7 Reg DM Reg 6 Reg DM Reg 24

Branch misprediction § If our guess is wrong, then we would have already started

Branch misprediction § If our guess is wrong, then we would have already started executing two instructions incorrectly. We’ll have to discard, or flush, those instructions and begin executing the right ones from the branch target address, Label. beq $2, $3, Label next instruction 1 next instruction 2 Label: 1 2 IM Reg IM 3 Clock cycle 4 5 DM flush IM flush 25 September 2020 IM Stalls and flushes 7 8 DM Reg Reg . . . 6 Reg 25

Performance gains and losses § Overall, branch prediction is worth it. — Mispredicting a

Performance gains and losses § Overall, branch prediction is worth it. — Mispredicting a branch means that two clock cycles are wasted. — But if our predictions are even just occasionally correct, then this is preferable to stalling and wasting two cycles for every branch. § All modern CPUs use branch prediction. — Accurate predictions are important for optimal performance. — Most CPUs predict branches dynamically—statistics are kept at runtime to determine the likelihood of a branch being taken. § The pipeline structure also has a big impact on branch prediction. — A longer pipeline may require more instructions to be flushed for a misprediction, resulting in more wasted time and lower performance. — We must also be careful that instructions do not modify registers or memory before they get flushed. 25 September 2020 Stalls and flushes 26

Implementing branches § We can actually decide the branch a little earlier, in ID

Implementing branches § We can actually decide the branch a little earlier, in ID instead of EX. — Our sample instruction set has only a BEQ. — We can add a small comparison circuit to the ID stage, after the source registers are read. (will require additional forwarding…) § Then we would only need to flush one instruction on a misprediction. beq $2, $3, Label next instruction 1 Label: . . . 25 September 2020 1 2 IM Reg Clock cycle 3 4 DM 5 6 7 DM Reg flush IM IM Stalls and flushes Reg 27

Branching (without forwarding and load stalls) 1 ID/EX 0 EX/MEM WB IF/ID Control PCSrc

Branching (without forwarding and load stalls) 1 ID/EX 0 EX/MEM WB IF/ID Control PCSrc M WB MEM/WB EX M WB 4 The other stuff just won’t fit! Add P C Shift left 2 Read register 1 Addr Instr Read register 2 Write register Instruction memory IF. Flush Read data 1 Write data ALU Zero = ALUSrc Read data 2 0 Registers 1 Address Data memory Reg. Dst Extend Rt Rd 25 September 2020 Result Stalls and flushes Write Read data 1 0 0 1 28

Implementing flushes § We must flush one instruction (in its IF stage) if the

Implementing flushes § We must flush one instruction (in its IF stage) if the previous instruction is BEQ and its two source registers are equal. § We can flush an instruction from the IF stage by replacing it in the IF/ID pipeline register with a harmless nop instruction. — MIPS uses sll $0, 0 as the nop instruction. — This happens to have a binary encoding of all 0 s: 0000. § Flushing introduces a bubble into the pipeline, which represents the onecycle delay in taking the branch. § The IF. Flush control signal shown on the previous page implements this idea, but no details are shown in the diagram. 25 September 2020 Stalls and flushes 29

Summary § Three kinds of hazards conspire to make pipelining difficult. § Structural hazards

Summary § Three kinds of hazards conspire to make pipelining difficult. § Structural hazards result from not having enough hardware available to execute multiple instructions simultaneously. — These are avoided by adding more functional units (e. g. , more adders or memories) or by redesigning the pipeline stages. § Data hazards can occur when instructions need to access registers that haven’t been updated yet. — Hazards from R-type instructions can be avoided with forwarding. — Loads can result in a “true” hazard, which must stall the pipeline. § Control hazards arise when the CPU cannot determine which instruction to fetch next. — We can minimize delays by doing branch tests earlier in the pipeline. — We can also take a chance and predict the branch direction, to make the most of a bad situation. 25 September 2020 Stalls and flushes 30