Tác giả giữ toàn bộ bản quyền (All Rights Reserved) — không phải giấy phép mở. Bạn vẫn xem và học miễn phí trên OpenER; muốn tái sử dụng cần xin phép tác giả.
PROFESSOR
Hello, and welcome back to our pipelining module. We're continuing to talk about hazards. We have identified three types of hazards, namely, structural hazards, data hazards, and control hazards.
We have talked about structural hazards, which are generally addressed by the data path design that identifies all the needs that the ISA presents so the data path is provisioned to support efficient pipelining.
Data hazards are a little bit different and let's try to first understand them and then figure out how do we address them.
So we have talked about the structural hazard of having to address the memory for both instructions and data.
There is a hazard also that affects the register file access.
So there may be instructions like this one add or there are definitely instructions like the add that writes the value into the register t0.
That is going to happen during the write back phase here, when the register file is accessed. So the register file, during the write back phase, is going to be accessed for writing.
That's why it is shaded on the left-hand side.
Now, at some point, an instruction like this one, store word, will be in the pipeline in the instruction decode phase when it will also wish to read the register file.
So the register file is going to be accessed for both reading and writing.
And it will happen that a add instruction is going to be writing a value of t0 while store would also like to read the t0.
So will stored instruction end up reading the old value that is in there or should it read the new value as it's supposed to?
The answer to that the register files are designed to support single cycle read-write operation. What does that mean?
That means that the add instruction that we have here is going to write during the first phase of this cycle write back and complete writing in 100 picoseconds, as we said before.
Then, in the remaining 100 picoseconds of a 200 picosecond clock cycle, store word is going to read t0 out of the register file.
So this is very much dependent on the circuit and logic implementation of a register file that needs to support writing and reading in the same cycle.
That avoids this data hazard that happens during the register update.
So if that is true, the stage will update the value in the register and the next instruction that is accessing it during the instruction decode is going to get the new value.
And that will be indicated in this diagram, where we have the write back phase of the add shading the first part of the left-hand part of the register access and then the store word shades the right-hand side.
Now, keep in mind that this is certainly true for most of standard five stage pipelines but may not be true for some more complicated higher frequency pipelines.
We are not touching those for now. They may be a subject of 152 if you take that class.
But for now, just make sure that you clearly read the assumptions that are stated in any exam or midterm questions that you see. Do they permit simultaneous read and write in one cycle?
Simultaneous meaning, in this case, consecutive that the write is completed before we want to read from the register.
But let's take a look at a little bit more complex data hazard, and here it is.
Let's say that the add instruction, this time, writes its result into the destination s0. Then, we have this kind of crazy code down here that perhaps doesn't make any sense.
I haven't really checked if it does anything useful. But every subsequent instruction would like to use that s0 as the source.
So the sub use it as a source, this or use it as a source, this xor uses as a source, and stored word uses as a source.
So all these instructions depend on add zero completing what it's doing and writing back the value into the register file. But let's take a look at what is happening here.
Let's say the value of s0 starting when this add instruction started its execution was five and it stays five. At this point, the ALU result is nine.
And this nine is supposed to be written back into a register file but that doesn't happen until mid through the write back stage of add.
So we still have five during the execution stage of the add and the memory access stage of the add instruction and for the first half of the write back stage.
So if the next instruction would like to use that when it goes to the register fetch stage to retrieve the values from the register during the instruction decode stage, what is it going to find?
Five — that's the wrong value. That is not going to be good. What is the or instruction going to find in the register file? Also five — that's a problem.
Now, this xor is going to do better because nine is going to be available in the second phase of the instruction decode when it is going to be read correctly.
And store word is also going to find the correct value. Now, what are we going to do with these sub and or that are going to get incorrect values from the register file?
We need to do something, otherwise we are executing an incorrect program. So the first solution is so-called stalling. What is stalling?
It's like when you have an old car and you drive a lot and you stall. And then you start again and you drive a little bit and stall.
So that's how your program would actually execute with the stalling.
When the next instruction depends on the result of the previous instruction, we cannot start this execution until the result is written back.
So if the sub follows the add, the sub execution needs to be delayed by two cycles by inserting two bubbles in between.
These bubbles are essentially knobs that are going to help correctly align the register write back stage with the register read.
The bubbles are, perhaps, not the most useful thing that is out there because during that time, processor is doing nothing.
Although we accomplish the main thing to get the right result, the correct execution of our program, what may happen is that we are going to lose performance.
Now, compilers generally try to do something to avoid that.
So it'll go through our list of instructions that are following that add and try to find some instruction that is not dependent on the result of that add and it is going to try to swap that sub with the instruction that does not depend on the result of the add.
So that way, we are not going to lose any cycles.
But in case it can't find any instructions, we have to instead insert these knobs which will cause stalls so that we can correctly align the register read with the register write.
This knob is anything that writes into the x0. By convention in RISC-V assembly, knob is at immediate x0, x0, x0.
The second solution is a hardware solution that we need to implement by modifying the data path.
Here is the thing, when this add instruction is at the end of its execute phase, we have the value somewhere in the pipeline that is a correct value that should be read by the register files by instructions sub and or from the register file.
But that value is not in the register file yet. It is somewhere in the pipeline, but it's not in the register file.
So the idea here is to take that value from the ALU or a cycle later from the memory access stage and forward it to the appropriate input port of the ALU.
So we'll take this output of the ALU and essentially fast forward it to the input of the ALU for the dub instruction, and then sub is going to perform correctly.
Sub is executing in the next cycle and we just need to make sure that this shows up straight at its input.
Similarly, that result is going to propagate in the next cycle to the output to the end of the memory access phase and we would need to forward it to the input of the ALU to correctly execute the order.
So we need to have these forwarding or so-called bypass paths in our data path to enable data to be forwarded to the right place without being written into register file.
It's going to get written in the register file, but this is just for the subsequent instructions to use the result immediately.
So this is basically outlining what we need to do. We need to basically have sort of a short circuit between the output of the ALU here and the input of the ALU.
That is something that is relatively straightforward to implement.
You guessed that there will be a another multiplexer that is going to be sitting in front of or together with a cell A operant.
By the way, the forwarding here is just shown for the operant A to the ALU. We also need to forward the operant B. Now, what do we need to control this forwarding?
There will be a multiplexer and we need to somehow control that multiplexer. So what kind of information do we have that is going to help us with that?
Remember, our data path saves the instructions that are currently in flight, so we'll know which registers are being accessed by the two consecutive instructions.
So what we need to do here is we need to take a look at the instruction that is the destination of this instruction that is in the execute stage and compare it with the instruction that is in the instruction decode stage.
If they are the same and not x0, then we have a data hazard and we should forward.
So we just compare those two and that is going to select our forwarding multiplexer.
We are going to have the same comparison between this ALU and between this ALU and the memory access stage to allow us to forward again there.
So what we are seeing here that the multiplexer that selects the operation A is going to have its two standard values, pc and the register file output, and then it is going to have two more forwarding paths.
Let's take a look at how is this implemented in the data path. This is our pipeline RV32I data path. All the registers are where they are supposed to be.
So what we did here, we added these two forwarding paths. The forwarding paths are not really in this picture shown that they are going forward.
They look like they're going backward but, actually, they're going forward to the execution of the next instruction. They're bypassing for the next instruction.
In this case, from the output ALU back to the input of the ALU after one cycle delay.
And then after two cycle delays, from the write back right back stage back to the input of the ALU. And that is it.
All what we need to do is to implement a forward and control logic.
This one is shown to be working only just on this multiplexer forwarding, comparing the resistors that are being used in these two instructions that are in flight, that are in the instruction decode and the execute stage, we would need to control to figure out what
is in instruction decode and the memory access and repeat all of that for the operant B, as well. And that is it. That is the story of the forwarding.
The best thing here is to look at a few code examples and see when the forwarding is needed and implementation is relatively straightforward. We're going to break here.
We are going to return to the control hazards after the break. See you then.
Đăng nhập để đánh giá học liệu này.
Đăng nhập để bình luận — chưa ai bình luận về học liệu này.
Không có khóa học liên quan.