[CS61C FA20] Lecture 23.2 - Pipelining III: Control Hazards — Transcript
Full transcript
- 0:01[Music]
- 0:12hi
- 0:12welcome back to pipelining so we have
- 0:15looked at structural hazards
- 0:17and data hazards so far
- 0:20and we have seen that we deal with
- 0:21structural hazards by
- 0:23provisioning enough hardware in our
- 0:25pipeline such that
- 0:26multiple instructions that are in
- 0:28different phases of execution
- 0:30can be executed concurrently on the
- 0:32other hand
- 0:34those data hazards that are associated
- 0:37with consecutive instructions of r type
- 0:40or i type that uh use the
- 0:44in the subsequent instruction in the
- 0:46following instruction use the
- 0:48um the
- 0:51value in the source register it was just
- 0:53written in
- 0:54to the destination register or supposed
- 0:56to be written in the destination
- 0:58register with the preceding instruction
- 1:02in order for them to execute correctly
- 1:04we need to use
- 1:05data forwarding or data bypassing so
- 1:08those are those added multiplexers
- 1:12in our pipeline
- 1:15so let's take a look at the third type
- 1:17of hazards that we can encounter
- 1:20in our code that is running
- 1:23through the pipeline those are the
- 1:25control hazards that are associated with
- 1:27branches and jumps
- 1:29and we're going to focus on branches
- 1:31because jumps are
- 1:32a more straightforward condition
- 1:35of a control hazard branches are
- 1:38conditional
- 1:38and we actually don't know um until
- 1:41later
- 1:42whether the branch is going to be taken
- 1:45or not so let's take a look at the
- 1:47branch execution
- 1:50so we
- 1:50[Music]
- 1:53fetch the the instruction and after the
- 1:56instruction decode phase we find out
- 1:58that it's a branch
- 2:00at that time we also fetch the
- 2:04values of t1 and t0 from the register
- 2:06file
- 2:07in the earliest time when we can figure
- 2:10out whether a branch is taken or not
- 2:12whether the values of t 0 and 2 1 are
- 2:15equal to each other
- 2:16is at the end of the execution phase
- 2:20that is the earliest time by that if we
- 2:23have
- 2:23appropriate hardware that we can update
- 2:25the program counter
- 2:27to the address that points to the label
- 2:32or um that we should be executing the
- 2:35next instruction
- 2:36in the stream
- 2:40so our next instruction is a sub
- 2:44in this case and this sub instruction
- 2:47will enter the pipeline
- 2:49before we actually know whether the
- 2:51previous instruction is a branch or not
- 2:54so it is going to be executed regardless
- 2:56of the outcome of the branch
- 2:58and the same thing holds for the the
- 3:01next instruction which is an or
- 3:03so these two instructions are going to
- 3:05be executed regardless of the branch
- 3:08outcome
- 3:10but what can happen if the
- 3:13branch is supposed to be taken
- 3:16we are supposed to update the program
- 3:19counter to a label that is somewhere
- 3:21else
- 3:21and these two instructions should not
- 3:24have been executed
- 3:26the subsequent instructions the sexor
- 3:30is okay um the
- 3:33the pc will be updated by that point and
- 3:36we'll know
- 3:37um whether the branch was taken or not
- 3:41so that one is okay
- 3:44and all the other instructions that
- 3:46follow that
- 3:48so uh in order to have correct execution
- 3:52um because we won't know what whether a
- 3:55branch is taken or not for two cycles
- 3:58after we fetch it we
- 4:01should not be executing the two
- 4:03following instructions
- 4:04so um we should have um
- 4:07two stall cycles after every single
- 4:10branch in a pipeline
- 4:13right we have no other option because we
- 4:15don't
- 4:17know what is going to be what is going
- 4:19to happen to the branch so we better not
- 4:20start executing something
- 4:24not quite let's take a look at how do
- 4:27can we minimize this fairly severe
- 4:29penalty of
- 4:30two clock cycles that are associated
- 4:32with two stalls
- 4:34so first a quick observation here
- 4:39if the branch is not taken then
- 4:40instructions fetched
- 4:43sequentially after the branch are
- 4:46correct
- 4:47we didn't have to cancel them so we
- 4:49could have just
- 4:50gone ahead and executed them but on the
- 4:52other hand
- 4:54if the branch is taken then we need to
- 4:57flush the pipeline so we can actually if
- 5:00the
- 5:00branches turns out to be taken 50 of a
- 5:03time
- 5:04we can reduce this penalty
- 5:08on the other hand the branch is somehow
- 5:10taken 99
- 5:11percent of a time that's not going to
- 5:13help us because 99 percent of the time
- 5:15would have to
- 5:17cancel these two instructions that are
- 5:18already in
- 5:20the in the pipeline let's recap how do
- 5:23we cancel them
- 5:24all we basically in flight convert them
- 5:26to knobs
- 5:27how do we do that well um the same way
- 5:31how we did that with
- 5:32the load instruction we alter
- 5:36the control bits for the instructions
- 5:38that are in flight
- 5:40such that they don't change the state of
- 5:43the processor
- 5:44so if the branch when we find out the
- 5:47branch is taken
- 5:48during the alu phase we go ahead
- 5:52and change this instruction sub
- 5:55to an off and we have to do the same
- 5:58thing
- 5:58with the or that follows that both of
- 6:01those instructions are
- 6:02essentially converted to knobs they do
- 6:05not alter the processor state
- 6:08now keep in mind what is different here
- 6:11is that we are not
- 6:12unlike what we have seen in low delayed
- 6:15hazards
- 6:16we are not going to go ahead and repeat
- 6:18these instructions because they
- 6:19shouldn't be taken
- 6:20after this branch so after we convert
- 6:24these two to knobs we can update our
- 6:27program counter is updated and we can
- 6:28execute
- 6:29the next instruction that may be in a
- 6:32different part of a code that is sitting
- 6:33at a label and
- 6:34there we may find an xor
- 6:38and all the other instructions are going
- 6:40to follow
- 6:42so the branch penalty here
- 6:46is reduced to the penalty
- 6:49that is that only applies when the
- 6:52branch is taken so when the branch is
- 6:54taken
- 6:54you have to insert these two knobs
- 6:58um in the in the pipeline
- 7:01so let's take a look at is there a way
- 7:03to try to reduce these branch penalties
- 7:06so every taken branch is the one that
- 7:07costs us and it costs us exactly
- 7:10two dead cycles of execution there is a
- 7:13way to do that
- 7:15we can observe that
- 7:19branches are either mostly taken or
- 7:21mostly not taken
- 7:23and we can predict whether they're going
- 7:25to be taken or not
- 7:27how do we do that i mean isn't that
- 7:29going to take us a lot of hardware and a
- 7:31lot of effort
- 7:32will turn out not to be the nature of
- 7:35the branches is
- 7:36that we generally use them for some kind
- 7:39of loopy code
- 7:41we have seen that we generally loop
- 7:43around some piece of a code
- 7:45in some say a while or a or a for loop
- 7:52so if our for loop is supposed to
- 7:55execute 100 times
- 7:57our branch is going to be taken only
- 7:59once
- 8:00and 99 times is not going to be taken
- 8:04so if we correct
- 8:09if we predict correctly that this is a
- 8:11branch
- 8:12that is very rarely one percent of a
- 8:14time
- 8:15being taken then we can
- 8:20save all you know we can eliminate most
- 8:22of its penalty we can reduce this
- 8:24um branch penalty to one percent
- 8:28of two stall cycles
- 8:32so we would go ahead and execute
- 8:35that code that is inside the loop
- 8:39all of the time and only occasionally
- 8:41cancel it if our prediction didn't turn
- 8:43out to be
- 8:44correct now how do we predict if a
- 8:47branch
- 8:48has been taken or not should be taken or
- 8:50not
- 8:53there are fairly sophisticated um
- 8:57predictors out there but the simplest
- 8:59one is just a single bit
- 9:01predictor that keeps track whether the
- 9:04branch
- 9:04was taken last time or not that's a
- 9:07pretty good indication if it is going to
- 9:08be
- 9:09taken the next time so
- 9:13you just keep one bit that says this
- 9:15branch was taken
- 9:17it's gonna be correct most of the time
- 9:21and that's
- 9:22what we essentially do there are much
- 9:25more sophisticated
- 9:27branch predictors as i mentioned if you
- 9:29take say cs152
- 9:31you will learn about some of them
- 9:34these branch predictors have been quite
- 9:36well tuned
- 9:37and they are accurate in
- 9:40high nineties of percent of a time
- 9:44regardless of a code it doesn't have to
- 9:46be a loop to a hundred
- 9:49so let's take a look at what does the
- 9:51branch prediction do for us
- 9:52we're still executing this branch of
- 9:55equal instruction
- 9:57and if the branch is taken
- 10:00we are going to you know
- 10:03predict that it is going to be taken and
- 10:05start executing code
- 10:07from the label we are immediately just
- 10:10loading that label into the program
- 10:11counter
- 10:14we are going to go ahead with that guest
- 10:17program counter execute the next few
- 10:19instructions but after two instructions
- 10:20we have an opportunity
- 10:22to check the our guests and correct
- 10:24ourselves if we are not
- 10:27if we did predict it right so if our
- 10:31guess was right everything is good if
- 10:33the guess was
- 10:34not right then we have to go ahead and
- 10:37convert those two instructions that have
- 10:39been executed into knobs
- 10:42and that is it this essentially wraps
- 10:45all of the hazards that we have been
- 10:47dealing with
- 10:48structural data and control hazards
- 10:52to the level that is good enough to
- 10:55enable us to build a functional pipeline
- 10:59and it's already fairly
- 11:02um high performance
- 11:06of course you can get much higher
- 11:07performance but for that you will have
- 11:09to take
- 11:10some other classes we're going to go
- 11:13cover one more topic
- 11:14but that will happen after a quick break
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 23.2 - Pipelining III: Control Hazards by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,567 words across 285 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.