Hazards: CSE378 W, 2001 CSE378 W, 2001
Hazards: CSE378 W, 2001 CSE378 W, 2001
Hazards: CSE378 W, 2001 CSE378 W, 2001
174 175
176 177
Resolving Data Hazards Hazard Detection and Stalling
• There are several options:
•Build a hazard detection unit, which stalls the pipeline until the
IM REG ALU DM REG
hazard has passed. It does this by inserting “bubbles”
(essentially nops) in the pipeline. This isn’t a great idea. We’d
add $7,...
like to avoid it, if possible.
•Forwarding. Forward the result as an ALU source.
IM bubble bubble bubble REG ALU DM REG
•Software (static) scheduling. Leave it up to the compiler. It must
schedule instructions to avoid hazards. Often it won’t be able sub $8, $7, $12
to, so it will issue no-ops (an instruction that does nothing)
instead. This is the cheapest (in terms of hardware) solution.
IM REG ALU DM REG
•Hardware (dynamic) scheduling. Build special hardware that
schedules instructions dynamiclly. and $9, $13, $7
178 179
180 181
Forwarding Forwarding Example
• Inserting bubbles is a pessimistic solution, since data that is $7 is computed here
written during the writeback stage is often computed much earlier:
•At the end of the EX stage for arithmetic instructions
IM REG ALU DM REG
•At the end of the MEM stage for a load.
• So why not forward the result of the computation (or load) directly add $7,...
to the input of the ALU if it is required there?
• Forwarding is sometimes called bypassing. IM REG ALU DM REG
182 183
184 185
Loads Scheduling
• Forwarding cannot save the day in the face of a dependent • Other important approaches include scheduling the instructions to
instruction which immediately follows a load. avoid hazards, in hardware or software.
• The only solution is to insert a bubble after loads if the next • This is particularly important for processors which have multiple or
operation is dependent, so we still need a hazard detection unit. very deep pipelines (most modern processors).
• Good compilers will attempt to schedule instructions in the “load • Dependences force a partial ordering on the instruction stream.
delay slot” so as to avoid these kinds of stalls. lw $t2, 0($t0) # 1
add $t5, $t2, $t3 # 2
sub $t3, $t1, $t8 # 3
mult $t7, $t8, $t8 # 4
addi $t5, $t7, 16 # 5
• Three kinds of dependence: data (read-after-write), anti-
dependence (write-after-read), output (write-after-write).
• Above: data dependences (1->2); anti-depencences (2->3, 4->5);
output (2->5).
• How can we reorder these instructions to do better?
186 187
188 189
Resolving Control Hazards Assume Branch Not Taken
• Detecting one is easy: just look at the opcode! • We need to be able to flush the pipeline in case the branch
• At least 4 possibilities: actually was taken.
•Always stall. Stall as soon as we see a branch. This costs a bit • If the branch is taken:
of control hardware and 3 cycles for every branch. •For the IF stage, we zero out the instruction field in IF/ID register.
•Assume branch not taken. Just go ahead and start executing •For the ID stage, since this is where we determine control, we
the next instructions, but find a way to flush those instructions if just set all control lines to zero, creating the effect of a nop.
the branch was taken. This costs more control hardware and 3 •For the EX stage, we use an extra mux to zero out the result of
cycles only if the branch is taken. the ALU.
•Delayed branches. Change the semantics of your branch • This approach costs additional control hardware and costs cycles
instruction to force the compiler/assembler to deal with the only when the branch is taken.
problem.
• A rule of thumb says that forward branches are taken 60% of the
•Branch prediction. Try to guess whether the branch will be taken time, and backward branches (as in loops) are taken 85% of the
or not and do the right thing. Be ready to flush the pipeline in time.
case you were wrong...
190 191
192 193
Exceptions How to Handle Exceptions
• Historical definitions: • We must save the program counter of the offending instruction in
•An exception is an unexpected event from within the processor the EPC (Exception PC), and then transfer control to the
(such as arithmetic overflow). operating system.
•An interrupt is an unexpected event from outside of the • The OS can then take appropriate action (provide an IO service
processor (such as IO requests). for the program, kill the program, etc). If it chooses to restart the
program, it can jump back to the EPC.
• MIPS doesn’t distinguish the source of the event, and calls both of
the above exceptions. • How does the OS know what kind of exception? MIPS includes a
cause register.
• Kinds:
• In hardware, the cause is saved into the cause register, the PC is
•IO device request (external) saved in EPC, and control transfers to a predefined address in the
•System call (internal) kernel (0x4000 0040).
•Arithmetic overflow (internal) • Exceptions are hard to deal with because we have several
•Undefined instruction (internal)
instructions in the pipeline.
• Suppose we get an arithmetic overflow (in the EX stage). We
•Hardware malfunctions (either)
need to be sure to let the downstream instructions finish, while
• Note that we can view system calls as exceptions! flushing the upstream instructions.
194 195
196 197