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ả.
BORA NIKOLIC
Hello, and welcome back to our pipelining module. So far we have studied the principles of pipelining, and then moved on to analyze hazards.
We have seen that we generally deal with the structural hazards by adding enough hardware so that we can execute multiple instructions in the pipeline.
Then we moved on to study the data hazards that stem from the fact that, or the condition that the following instruction depends on the results of the previous instruction that was in the pipeline.
Let's take a look at that. Let's revisit that so that we understand how we dealt with that by using forwarding.
So in this case, we had the instruction add that was in the pipeline already.
And then there was a sub instruction that was following it, and used the result that was stored in the destination registered as 0 as the source for that following instruction.
And then the instruction after that also used as 0 as the source register.
So as the add instruction is traveling through a pipeline through its five stages of pipeline, although the result of the execution of this addition is known at the end of the execute stage, it is not written back into the register file until two stages later, until halfway
through the write back stage. So the value of s0 still is the old value of 5 in this case.
And it gets updated to a new value that is 9 in the first phase of the write back phase.
So if the sub instruction that follows the add instruction reads the value from the register when it's supposed to be during the instruction decode phase, it would read the wrong value, which is 5, the old value.
Instead, we deal with that by forwarding the new value. And this is something that is possible because we already know that the new value is 9.
It is just not written back in the registered file, but it is handy.
It is sitting right there at the output of the ALU in the register that separates the execute phase with the memory access phase.
So what we need to do is to widen the multiplexer that feeds the ALU with the operand, in this case, ra, and it's there.
It is just going to show up at the input of the ALU.
Similarly, for the subsequent instruction or we would forward the result of this addition before it gets written in the register file from the pipeline register that divides the memory access phase and the write back phase, because we have it handy.
It still traveled a little bit deeper in the pipeline, so it is nicely aligned with this instruction, with the input of the instruction or.
So we learned how to deal with these data hazards by using hardware forwarding.
Without hardware forwarding, we would have had to stall the pipeline, meaning we would have to insert knobs and don't do something during particular cycles.
There is no such easy way out in another case of data hazard, which are the data hazards associated with loads. So here is a sequence of instructions that starts with a load.
This load takes the data from the memory and stores it in the destination register s2.
And then subsequent instructions would like to use that value from s2 as the source operand. So here is what happens.
Our load word addresses the memory. And this data is available at the end of the memory access phase.
That is one cycle later than what we have had in the r type instructions where it was available at the end of the execute phase.
So this new data is going to be written just one cycle after that, but the subsequent instruction here is looking at the values that are in the register file that are stale.
They're old. So data from the memory is there, but it is needed one cycle earlier. We can't go back.
We cannot jump time here.
There is no forwarding solution that is going to fix this here. We can't — forwarding basically helps us go kind of forward in the pipeline.
There is no solution that allows us to go back in the pipeline. What can we do? We need to stall. There is no practical way.
We don't have a time machine yet here, not in computer hardware, or anywhere else.
So this and instruction that needs s2 as its source operand cannot proceed until it becomes valid, until it is there.
We can forward from here, but that forwarding would make it only into the instruction that is delayed by one. It would make it in time for or, but and can't do it.
So we will stall and forward. And then all the instructions that are down the stream would not be affected.
So in a quick summary here, all types of load instructions require one cycle pipeline stall. Let's see how does that pipeline stall look like.
So our data is here in the pipeline register that follows the memory access stage. And it needs to be there. It can't happen.
So we need to stall the processor in order to slow down the execution, delay the execution of this and.
So instead, we convert this and into a nop and move all the instructions by one cycle down the stream.
So how do we convert in flight this and that is an and to a nop. I, mean it's already there. It is in the pipeline.
By the time we have figured out that this is a load at the end of the cycle, we have already fetched the and. We have — we didn't fetch something else.
We have an and on our hand that we are executing. So we have to have a hardware mechanism that basically cancels this and.
It invalidates the and, and repeats it in the next cycle. So how do we do that? What does the and do?
Well, it does flip some bits, but if it doesn't write them back, if it doesn't change the state of a processor, it's like it did not exist.
So that's a clue on what we need to do quickly when we find out that we have a load on our hands here in this instruction decode phase.
Immediately after that, turn off — set all the control signals that are associated with writing the new state into the processor to be disabled.
So we are not writing back into a register file. We are not writing into the memory. And we are not updating the program counter.
So if there is no state update, the next instruction is going to be the same one. It's going to be our and. And we just proceed with executing that and.
And it's as simple as that.
So we need a mechanism here, a piece of logic that is going to detect that we are dealing with a load and that the destination of that load is the source for the next instruction.
That requires us to stall. And this is the mechanism to stall. We basically make the latch on that door do nothing.
We disable the control signals and run the following instruction again.
When we run this and again, when we repeat the and, we are able to forward this using very similar principles to what we have seen before, actually exactly the same mechanism as we have seen before in mitigating data hazards.
So it becomes available at the input of the ALU. So we are going to repeat this and with the forwarding.
So in a quick summary here, in order to handle the load data hazards, we have to have one cycle pipeline stall implemented in hardware that prevents completion of the instruction, does not update the processor state, and forces reexecution of the following instruction.
So this is a very important concept in pipelines. It happens fairly frequently.
So that instruction that is sitting after the load word is called the load delay slot.
If that instruction uses the result of the load, then the hardware has to stall for one cycle. So what do we do with that?
Well, that's like inserting a knob in the slot, but knobs can be inserted during the compilation phase, and resulting in code bloat and performance loss.
So the key idea here is something that we've mentioned before.
Let's go through the list of instructions that you would like to execute and find one that does not — whose operands do not depend on the result of the load, then just put it into that delay slot.
So that's the idea. And there is no performance loss. So the burden of doing that is generally put on the compiler.
Let's see how does that work. So here is a piece of RISC-V assembly code that does two additions by using data that is in the memory.
So A3 sums A0 and A1 and A4 sums A0 and A2. So let's see a sample RISC-V assembly code.
So here is the original order of that code. We're going to go ahead load — t0 points to the 0-th element of the array A.
And we are going to go get the first element of the array, store it in t1, get the second element of the array, store it in t2, and immediately proceed with the addition.
So the problem here, we proceeded by executing two loads. They do not block each other.
There is no dependency between the source of the second load with the destination of the first load, but look at this one over here, t2 here, t2 there.
So there is a dependency there. So this is going to cause a stall.
So we have to stall this add execution until we can forward from the load pipeline into the add instruction. And then we are okay.
We proceed, and then we find out there is another hazard here. We see t4 here and t4 there. That's a hazard. We have to stall again.
When we insert that one extra cycle, we continue. How can we do better than this? Can we reorder this? And that's typically Java for a compiler.
So here is an alternative schedule of instructions.
We are going to go ahead and load all three of them, three operands here that we need, A0, A1, and A2 in three consecutive load cycles that enable us to insert this load instruction between the load of A1 and the consumption of A1
in this addition here. So we are using in the load delay slot. We are putting another independent load.
So we proceed with that.
And these two, the storing t2 in the register file, and the consumption of the data from the registered file are now separated by one cycle, and that's plenty enough for our forwarding in hardware
to help us avoid stalling.
Then the other one is also resolved because loading into t4 and using the data from t4 are now separated by two instructions.
And that is plenty enough. It's not even activating the forwarding.
So instead of nine cycles that we used initially for executing these seven instructions, we can execute all seven instructions in seven cycles.
So this is all what we had about the load delays slot and load data hazard. We are going to move on to the control hazards right after a quick break.
Đă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.