[CS61C FA20] Lecture 22.2 - Pipelining II: Pipeline Hazards — Transcript
Full transcript
- 0:11Hello. Welcome back to the module that
- 0:14deals with the pipeline processor
- 0:16design.
- 0:17So far we took our single cycle CPU and
- 0:21modified it
- 0:22to make it a pipeline processor. In that
- 0:25process we added some pipeline registers
- 0:28and move some resources around so the
- 0:30things look a little bit easier to
- 0:33design.
- 0:36We didn't quite design a good pipeline
- 0:39processor. It
- 0:41you know really badly suffers
- 0:43from some
- 0:45common issues that are associated with
- 0:48pipeline hazards.
- 0:50Now pipeline hazards
- 0:52um
- 0:53stands for a collection of issues that
- 0:55pipeline processors encounter.
- 0:59When we talk about pipeline processors,
- 1:03the basic thing that differs between a
- 1:05pipeline processor
- 1:07and a single cycle processor is that we
- 1:10have multiple instructions in flight.
- 1:12In a single cycle processor
- 1:16our instructions
- 1:18really depend only on themselves. Every
- 1:20instruction completes and then the next
- 1:23instruction
- 1:24um
- 1:25starts to execute. And there is no
- 1:28really dependency between them. In
- 1:31pipeline processors, there are multiple
- 1:34instructions that are simultaneously
- 1:35being executed.
- 1:37And if the results of one instruction
- 1:40affect the following instruction
- 1:43then we have an issue.
- 1:45The following instruction cannot
- 1:48proceed.
- 1:49There are generally three types of
- 1:51hazards.
- 1:53So it is important to understand them
- 1:55because they have different solutions
- 1:58that will
- 2:00um avoid
- 2:03running into those hazards.
- 2:05Um in general, why they're all called
- 2:07hazards because especially earlier on,
- 2:11it was not clear who was going to take
- 2:14care of these issues.
- 2:16They can be addressed by either a
- 2:18compiler or the pipeline
- 2:21pipeline processor designer, but
- 2:23somebody has to take care of these
- 2:25hazards. If nobody does, the processor
- 2:27doesn't work.
- 2:29Or even worse, works most of the time,
- 2:31but fails from time to time and it's
- 2:35really difficult to find out when and
- 2:37why it fails.
- 2:40So, as I said, there are three types of
- 2:42hazards that we can encounter and those
- 2:45are structural hazards, data hazards,
- 2:47and control hazards.
- 2:49It is always a situation that prevents
- 2:52starting a new instruction while there
- 2:54is another instruction that is in
- 2:56flight.
- 2:58Structural hazards
- 3:01appear because of contention over a
- 3:04resource.
- 3:05In most modern processors, at least
- 3:08these that are not that complicated like
- 3:11the ones that we are designing in this
- 3:13class, we are rarely going to run into
- 3:15structural hazards.
- 3:18And the pipeline is designed to match
- 3:21the ISA and ISA prevents the existence
- 3:24of these hazards.
- 3:25Then the second type are data hazards.
- 3:28Data hazards essentially exist because
- 3:32of the dependency between the
- 3:34instructions. So, if there is one
- 3:37instruction that is in flight that is
- 3:39going to write the result into the
- 3:42register file, the result that the
- 3:45following instruction cannot really
- 3:49um depend, cannot work with the same
- 3:53operand, that is before it gets written
- 3:56back into the register file.
- 3:59Otherwise, we will be working with an
- 4:01incorrect value. We're going to talk a
- 4:03little bit more about that just in a
- 4:06bit. And finally, there is a control
- 4:08hazard. The control hazards happen when
- 4:10you have branches. If the following
- 4:13instruction that is being executed in
- 4:14the pipeline is
- 4:18following a branch,
- 4:20so if there is a branch in the pipeline
- 4:21and there is another instruction after
- 4:23that, that next instruction may be
- 4:25invalid if the branch ends up being
- 4:28taken.
- 4:30So, let's talk about structural hazard
- 4:33hazards in a bit more of a detail.
- 4:36The simplest type of a structural hazard
- 4:39that we already addressed in our design
- 4:42even of a single cycle CPU
- 4:44is the contention between incrementing
- 4:48the program counter and performing
- 4:51arithmetic operations and
- 4:55um when addressing the memory.
- 4:59For example, we could have made a
- 5:02mistake and left out the PC plus four
- 5:06adder and said, "Let's use the ALUs ALU
- 5:09instead." So, every time we would have
- 5:11to add PC plus four to our instruction,
- 5:14we would use the ALU.
- 5:18Well, we have an issue there,
- 5:21right? Every time we
- 5:25need to add four to the program counter,
- 5:29our ALU would not be available. So, our
- 5:32um processor would not work very well.
- 5:34It would first, you know, in one cycle
- 5:37would add PC plus four and then in the
- 5:40next cycle would have to execute things.
- 5:42So, it was a very natural decision and
- 5:44we we just went through that to have a
- 5:47dedicated adder that increments the
- 5:49program counter.
- 5:51Um
- 5:52So, in general, structural hazards
- 5:56appear because two instructions in the
- 6:00pipeline compete for the same resource.
- 6:03There are two ways to address structural
- 6:05hazards. One is to stall.
- 6:08And that's what I've just described as a
- 6:10possible outcome for not having a PC
- 6:12plus four adder.
- 6:14Um essentially, the next instruction
- 6:17would stall until the previous
- 6:19instruction is not is is done is
- 6:22finished uh using that resource.
- 6:26A stall um often means introducing
- 6:30no-ops. We'll see that in a bit.
- 6:33Then more com- a more more common
- 6:35solution is to add more hardware to our
- 6:38pipeline such that all instructions can
- 6:41execute concurrently.
- 6:46So,
- 6:47um let's take a look at the
- 6:49fairly straightforward structural hazard
- 6:52that can exist because of the register
- 6:54file.
- 6:55So, in RISC-V ISA,
- 6:59our instructions can
- 7:02access the register file three times.
- 7:04There are two operands to be read and
- 7:06one to be written. So, our
- 7:09register file has to be able to support
- 7:12that. Otherwise, we would have an a
- 7:14shortage of of resources. So, our
- 7:17register file is designed to have two
- 7:19read ports
- 7:20and a write port.
- 7:23There are, remember, three addresses
- 7:25that go into the input of the register
- 7:27file.
- 7:28There's another subtlety um in there.
- 7:32In order to be able to complete all of
- 7:34this all of the operations in a single
- 7:37cycle or in a pipeline processor, we
- 7:40made one assumption that the register
- 7:42file is fairly fast. So, it is faster
- 7:45than many of the other units that we
- 7:47have in a processor. We can read the
- 7:49register file in a 100 picoseconds, and
- 7:52we can also write to a register file in
- 7:54a 100 picoseconds. We'll see that that's
- 7:56a convenient thing that allows us to
- 7:58both read and write the register file in
- 8:01a single cycle.
- 8:05But, the key thing here is register file
- 8:07has to be designed to support three
- 8:09accesses per cycle
- 8:12simultaneously.
- 8:15Let's take a look at another type of a
- 8:17structural hazard that we may have.
- 8:19Uh it may be there may be a memory
- 8:21contention.
- 8:23So, in
- 8:25pretty much every instruction needs to
- 8:27access the instruction memory.
- 8:30Um but, some instructions need to
- 8:34work you read or write the data to the
- 8:38memory.
- 8:40And we said that we have this unified
- 8:42concept of a memory that we're working
- 8:44with a stored program computer, so there
- 8:46should be just one memory.
- 8:47So, every time there is a load or store,
- 8:51it is going to contend with every other
- 8:53instruction for the memory access. In
- 8:55this case, it is shown
- 8:58that if you have a load word
- 9:01during the memory access phase, another
- 9:04instruction that would perhaps be trying
- 9:07to read an instruction if you just had
- 9:08one memory, this add immediate would
- 9:11have to stall and would have to wait
- 9:14until the load word is done if you just
- 9:17had one memory.
- 9:19So, a solution to this is to have two
- 9:23memories.
- 9:24But, didn't you
- 9:25just contradict yourself? Well, then
- 9:27don't we have just one memory?
- 9:31And what's the story about two memories?
- 9:33Well,
- 9:34we do have one main memory that lives in
- 9:36DRAM, but we generally have cache that
- 9:40is on our
- 9:41processor die on on our processor chip.
- 9:45And you're going to see that in just
- 9:46next module in a in a couple of
- 9:47lectures.
- 9:49Um essentially, there are pieces of of
- 9:52um
- 9:53of uh there are chunks of memory on our
- 9:56processor chip that
- 9:59have copies of the instructions that are
- 10:01have been copied from the main memory
- 10:03and copies of the data that has been
- 10:05copied from the main memory with two
- 10:07separate ways to access it. So, we don't
- 10:09have to contend for the main memory
- 10:12when we are fetching an instruction
- 10:15um while another instruction is
- 10:17completing uh a read or a write.
- 10:22All right.
- 10:24So, in summary,
- 10:25structural hazards can exist, but they
- 10:28are avoided um
- 10:31by typically by the
- 10:35by the hardware design that matches the
- 10:37ISA. So, we'll look through all the
- 10:39instructions that are in the ISA, and we
- 10:42make sure that our hardware is pro-
- 10:44provisioned to support all all of these
- 10:47instructions
- 10:48during the depth of the pipeline that
- 10:51can exist concurrently during the depth
- 10:53of a pipeline.
- 10:55So, what we have seen is that our
- 11:00register file has to support whatever
- 11:03um the ISA requires us to have for it
- 11:08and that we need to have separate
- 11:11instruction and data memories or at
- 11:13least copies of instruction and data
- 11:15memory so we can access them separately.
- 11:19That's it for structural hazards.
- 11:21They're relatively straightforward to
- 11:23understand. The next ones are data
- 11:25hazards, which are a little bit
- 11:27trickier.
- 11:29See you in a bit.
About this transcript
This page contains the full transcript of [CS61C FA20] Lecture 22.2 - Pipelining II: Pipeline Hazards by CS 61C Departmental, generated from the public captions YouTube serves with the video. The transcript has 1,503 words across 269 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 choose a caption track available on the video.
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, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.