Video
Đăng bởi Ban biên tập OpenER ·
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ả.
Okay, so we're going to look at the pipeline question, which one, which is the one that I made. This is a pretty long question, right?
Được rồi, bây giờ chúng ta sẽ xem câu hỏi về đường ống, câu nào ấy nhỉ, là câu do tôi soạn. Đây là một câu khá dài, đúng không?
And so let's just go through the solutions and what's the answers that you're supposed to get. Okay. Okay.
Vậy nên chúng ta cứ đi qua lời giải và xem các đáp án mà các bạn cần tìm ra là gì. Được rồi. Được rồi.
So the question is, we want to, it's about forwarding, right? It's about different ways to support forwarding.
Câu hỏi này nói về forwarding, đúng không? Nó nói về những cách khác nhau để hỗ trợ forwarding.
And we're going to do that in a kind of an incremental way by starting out with a baseline five-stage pipeline datapath. So we have that without the forwarding logic.
Và chúng ta sẽ làm theo kiểu tăng dần, bằng cách bắt đầu với một datapath đường ống năm giai đoạn cơ sở. Tức là ta có datapath đó mà chưa có logic forwarding.
And some details of what the datapath is: the register file is a synchronous write, asynchronous read, where we can do the reading and writing to the same register in one cycle.
Và một vài chi tiết về datapath: register file ghi đồng bộ, đọc không đồng bộ, nghĩa là ta có thể đọc và ghi cùng một thanh ghi trong một chu kỳ.
So we can do things like write at one cycle and then read it in a single cycle for the same register.
Vậy ta có thể làm những việc như ghi ở một chu kỳ rồi đọc chính thanh ghi đó trong cùng một chu kỳ.
So this is kind of a, it's also a type of forwarding that happens inside the register file. And we're allowing that to make things a little simpler.
Vậy đây cũng là một loại, một kiểu forwarding xảy ra bên trong register file. Và ta cho phép điều đó để mọi thứ đơn giản hơn một chút.
And for the memory blocks, it's just the usual synchronous write, asynchronous read. It's really not, you know, it doesn't really matter for the question.
Còn với các khối bộ nhớ, chúng chỉ là ghi đồng bộ, đọc không đồng bộ như thông thường. Thật ra điều này không quan trọng lắm đối với câu hỏi.
But we're going to assume all memory operations complete within one cycle, obviously, so that you're not going to worry about, you know, cache misses or anything like that.
Nhưng chúng ta sẽ giả sử mọi thao tác bộ nhớ hoàn thành trong một chu kỳ, hiển nhiên là vậy, để các bạn không phải lo về cache miss hay những thứ tương tự.
And the idea is, we already have a critical path for these three stages: fetch, memory, and write back.
Và ý tưởng là, chúng ta đã có đường tới hạn cho ba giai đoạn này: fetch, memory, và write back.
So for F, M, and W, for fetch, memory, and write back, we already have critical path numbers.
Vậy với F, M, và W, tức fetch, memory, và write back, ta đã có sẵn các giá trị đường tới hạn.
But for decode and execute, we don't. And the idea is we have to do that ourselves. And we're going to calculate that by using these delay numbers, right?
Nhưng với decode và execute thì chưa có. Và ý tưởng là chúng ta phải tự tính. Chúng ta sẽ tính bằng cách dùng các số liệu độ trễ này, đúng không?
We have given all the delay numbers for all the components that are in the D and X stage. So this should be sufficient enough for you to calculate the critical path yourself.
Chúng tôi đã cho tất cả các độ trễ của mọi thành phần trong giai đoạn D và X. Vậy những số liệu này đủ để các bạn tự tính đường tới hạn.
So, yeah, that's the idea. It's pretty straightforward, hopefully. Any questions about the problem description?
Vâng, ý tưởng là như vậy. Hy vọng là khá đơn giản. Có câu hỏi nào về phần mô tả bài toán không?
Cool. Yeah.
Tốt. Vâng.
So the first part, we have part A and B and C, right? Or A and B. And the idea is we want to now start adding forwarding logic to the datapath.
Phần đầu có các phần A, B và C, đúng không? Hay chỉ A và B. Và ý tưởng là bây giờ ta bắt đầu thêm logic forwarding vào datapath.
And again, we're going to do it step by step. The first step is we're going to add just ALU to ALU forwarding. Okay, let me just highlight that.
Và một lần nữa, ta sẽ làm từng bước. Bước đầu tiên là chỉ thêm forwarding từ ALU đến ALU. Được rồi, để tôi tô sáng chỗ đó.
Just for RS1, so that the diagram here is not complicated. So we have these thick black arrows that go to just the RS1 side of the ALU. And that goes through the forwarding logic.
Chỉ cho RS1 thôi, để sơ đồ ở đây không bị phức tạp. Vậy ta có những mũi tên đen đậm này chỉ đi tới phía RS1 của ALU. Và nó đi qua logic forwarding.
And we do that by taking instructions, use the instructions from execute and memory stages.
Và chúng ta làm điều đó bằng cách lấy các lệnh, sử dụng các lệnh từ giai đoạn execute và memory.
And using this, and also considering the delay numbers in the previous page, what's the minimum clock period achievable, which is equivalent to the critical path of the design, right?
Và dựa vào đây, đồng thời xét các con số về độ trễ ở trang trước, chu kỳ xung nhịp nhỏ nhất có thể đạt được là bao nhiêu, tương đương với đường tới hạn của thiết kế, đúng không?
If we have the critical path, we can shrink the clock period down to the critical path, and that's going to be the fastest we can go.
Nếu ta có đường tới hạn, ta có thể rút ngắn chu kỳ xung nhịp xuống bằng đường tới hạn, và đó sẽ là tốc độ nhanh nhất mà ta có thể đạt được.
So the idea is, if you have these two instructions, we can compare the register fields of those two instructions.
Vậy ý tưởng là, nếu bạn có hai lệnh này, ta có thể so sánh các trường thanh ghi của hai lệnh đó.
And we can tell if we can forward the data from the M stage to the X stage, which is the ALU to ALU forwarding, for the instruction at the execute stage, right?
Và ta có thể biết liệu có thể chuyển tiếp dữ liệu từ giai đoạn M sang giai đoạn X hay không, tức là chuyển tiếp từ ALU đến ALU, cho lệnh đang ở giai đoạn execute, đúng không?
This part is the part that receives the forwarded data. And that's what actually needs the up-to-date data, right? And this is the instruction that produces the data.
Phần này là phần nhận dữ liệu được chuyển tiếp. Và đây chính là phần thực sự cần dữ liệu mới nhất, đúng không? Còn đây là lệnh tạo ra dữ liệu.
So this is the use part, this is the produce. We obviously have to have a custom block that can tell whether or not that forwarding needs to happen or not, right?
Vậy đây là phần sử dụng, còn đây là phần tạo ra. Rõ ràng ta phải có một khối tùy biến để cho biết có cần chuyển tiếp hay không, đúng không?
Okay, so what the question asks is just a number, the critical path of this design. And to do that, you obviously have to consider the entire pipeline, right?
Được rồi, câu hỏi chỉ yêu cầu một con số, đó là đường tới hạn của thiết kế này. Và để làm vậy, rõ ràng bạn phải xét toàn bộ đường ống, đúng không?
So we have these numbers for fetch, And for the fetch, it was 500 milliseconds.
Vậy ta có các con số cho giai đoạn fetch, Và với fetch, nó là 500 mili giây.
For memory, it was also given as 600, right, back for 50 milliseconds. And now we have to calculate these delays for the decode and execute ourselves.
Với memory, nó cũng được cho là 600, à, quay lại là 650 mili giây. Và bây giờ ta phải tự tính các độ trễ này cho decode và execute.
So for the decode, what happens in the decode stage is we just have one giant register file block. So we are obviously going to have a register file.
Vậy với decode, những gì xảy ra ở giai đoạn decode là ta chỉ có một khối register file khổng lồ. Nên rõ ràng ta sẽ có một register file.
We're just going to draw the critical paths. And this took 500 milliseconds, right?
Ta chỉ cần vẽ các đường tới hạn. Và nó mất 500 mili giây, đúng không?
So this takes 500. But we also have pipeline registers, right? So before the register file, it actually takes time for the clock signal.
Vậy nó mất 500. Nhưng ta cũng có các thanh ghi đường ống, đúng không? Vậy trước register file, thực ra cần có thời gian cho tín hiệu xung nhịp.
After the clock signal arrives, then this data from D to Q, it takes some time to travel there, get latched there.
Sau khi tín hiệu xung nhịp đến, dữ liệu từ D đến Q mất một khoảng thời gian để truyền đến đó và được chốt lại.
So we have to take into account that time too, which is shown as the clock to Q delay. So we have to add that 50 milliseconds.
Vậy ta cũng phải tính đến thời gian đó, được thể hiện là độ trễ từ xung nhịp đến Q. Vậy ta phải cộng thêm 50 mili giây đó.
And the total critical path for decode is 550. So it's definitely not the critical path for the entire CPU, right? Because we already have memory stage that's longer.
Và tổng đường tới hạn của decode là 550. Vậy chắc chắn nó không phải đường tới hạn của toàn bộ CPU, đúng không? Vì ta đã có giai đoạn memory dài hơn.
Okay, so what about the execute stage? For the execute stage, there are a number of different delay paths that are happening in parallel.
Được rồi, vậy còn giai đoạn execute thì sao? Với giai đoạn execute, có một số đường trễ khác nhau diễn ra song song.
There's one path that's going through this MUX and ALU.
Có một đường đi qua MUX và ALU này.
And actually there's another path that goes through the immediate generator to MUX to ALU. There's also another path that goes through the forwarding logic, then the MUX to ALU.
Và thực ra còn một đường khác đi qua bộ tạo số tức thời đến MUX rồi đến ALU. Còn một đường khác nữa đi qua logic chuyển tiếp, rồi MUX đến ALU.
Lastly, there's the path that goes inside the branch comparator, right? So we have to figure out the delays for all these paths and get the longest path, or get the longest delay.
Cuối cùng, còn có đường đi bên trong bộ so sánh nhánh, đúng không? Vậy ta phải tính độ trễ của tất cả các đường này và lấy đường dài nhất, hay nói cách khác là lấy độ trễ lớn nhất.
That can be the critical path delay. All right, so let's start with the shortest one, which is from this register file or the Python register to the branch comparator.
Đó chính là độ trễ của đường tới hạn. Được rồi, ta bắt đầu từ đường ngắn nhất, là đường từ tệp thanh ghi này, hay thanh ghi Python, đến bộ so sánh nhánh.
So that is this register to branch.
Tức là đường từ thanh ghi này đến nhánh.
And if you look at the numbers, branch was 200. So this is in total 150 milliseconds, right?
Nếu nhìn vào các con số, nhánh là 200. Vậy tổng cộng là 150 mili giây, đúng không?
Now you might wonder, why is this branch comparator not connected to other parts, right? And that was actually one of the easier mistakes to make.
Có thể bạn thắc mắc vì sao bộ so sánh nhánh này không nối với các phần khác, đúng không? Và đó thực ra là một trong những lỗi dễ mắc phải nhất.
The reason is the branch comparator, the result of that is not used inside the ALU, right?
Lý do là bộ so sánh nhánh, kết quả của nó không được dùng bên trong ALU, đúng không?
What the branch comparator really does is it just tells if the, you know, compares the RS1 and RS2 value, tells to the control signal if one or the other is taken or not, and the control signal will decide if the branch is taken or not, right?
Bộ so sánh nhánh thực chất chỉ làm việc là, so sánh giá trị RS1 và RS2, báo cho tín hiệu điều khiển biết nhánh có được rẽ hay không, và tín hiệu điều khiển sẽ quyết định có rẽ nhánh hay không, đúng không?
So that path does not extend into the ALU. So that's kind of one tricky thing there. And this is just an independent path, one of the independent paths.
Vì vậy đường đó không kéo dài vào ALU. Đó là một điểm hơi khó ở đây. Và đây chỉ là một đường độc lập, một trong các đường độc lập.
Second path is where we start from the register, then the immediate generator.
Đường thứ hai bắt đầu từ thanh ghi, rồi đến bộ tạo giá trị tức thời.
We're talking about this path, and then MUX with two inputs, and then ALU.
Ta đang nói về đường này, rồi đến bộ dồn kênh hai đầu vào, và sau đó là ALU.
So now this one has 50, 100, 100, 300. This is in total 550 picoseconds. We're still, you know, shorter or equal to the decode stage.
Đường này có 50, 100, 100, 300. Tổng cộng là 550 picosecond. Ta vẫn ngắn hơn hoặc bằng giai đoạn giải mã.
Now, the last path here is the one that goes through the forwarding logic.
Đường cuối cùng ở đây là đường đi qua logic chuyển tiếp.
So that starts from the register, and then the, you know, the mysterious forwarding logic, which takes about 250 picoseconds, and then the MUX with three inputs, and then ALU.
Nó bắt đầu từ thanh ghi, rồi đến logic chuyển tiếp đầy bí ẩn, mất khoảng 250 picosecond, rồi đến bộ dồn kênh ba đầu vào, và sau đó là ALU.
Now, there's one path that I omitted, which is the path that goes through here. We don't have to consider that because that path is shorter than the path that goes through the forwarding logic.
Có một đường tôi đã bỏ qua, là đường đi qua chỗ này. Ta không cần xét đường đó vì nó ngắn hơn đường đi qua logic chuyển tiếp.
So by the time that the selection signal arrives here, that data will be ready at that input. So that's not going to contribute to the critical path.
Nên khi tín hiệu chọn đến được đây, dữ liệu đó đã sẵn sàng ở đầu vào này. Vì thế nó không đóng góp vào đường tới hạn.
It's kind of like happening in the parallel, and that data or that delay is completely overlapped. Okay, so this is going to happen in total 50, 250, 100, 300.
Nó diễn ra song song, và độ trễ đó bị che phủ hoàn toàn. Được rồi, vậy đường này tổng cộng là 50, 250, 100, 300.
Now that sums up to 600, is it? 400, no, 700 picoseconds, right? That's pretty long.
Tổng là 600, phải không? 400, không, 700 picosecond, đúng không? Khá dài đấy.
And that's going to be the critical path for the entire CPU, because all the other stages, fetch, memory, write back, decode, they all have shorter critical path.
Và đó sẽ là đường tới hạn của toàn bộ CPU, vì tất cả các giai đoạn khác, nạp lệnh, bộ nhớ, ghi lại, giải mã, đều có đường tới hạn ngắn hơn.
Now you can see kind of the problem of this design, right?
Giờ bạn có thể thấy vấn đề của thiết kế này, đúng không?
By adding this forwarding logic, that extends the whole critical path to execute stage, which is already kind of long with the bulk ALU there.
Khi thêm logic chuyển tiếp này, điều đó kéo dài toàn bộ đường tới hạn sang giai đoạn thực thi, vốn đã khá dài vì có khối ALU lớn ở đó.
So that's what kind of leads to the next problem. So do you have any problems or questions for this problem?
Và đó là điều dẫn đến vấn đề tiếp theo. Vậy các bạn có vấn đề hay câu hỏi nào về bài này không?
Oh, cool. Everyone got it right.
Ồ, tuyệt. Mọi người đều làm đúng.
So the next problem is thinking about another way to add the forwarding logic, which is now we're going to move this forwarding logic over to the left.
Vậy bài tiếp theo là nghĩ đến một cách khác để thêm logic chuyển tiếp, đó là bây giờ chúng ta sẽ dời logic chuyển tiếp này sang bên trái.
Now we're going to use the instructions coming from the decode and execute stage. So the idea is still the same.
Bây giờ chúng ta sẽ dùng các lệnh đến từ giai đoạn giải mã và thực thi. Ý tưởng vẫn như cũ.
We want to compare the register bits of the decode instruction and execute instruction, and we're going to find out if this instruction here at the decode stage is ready to receive the data from the forwarding path.
Chúng ta muốn so sánh các bit thanh ghi của lệnh giải mã và lệnh thực thi, và chúng ta sẽ xác định xem lệnh này ở giai đoạn giải mã đã sẵn sàng nhận dữ liệu từ đường chuyển tiếp hay chưa.
But the problem is, the actual forwarding happens at the execute stage, right? Because that's where the mux selects from the forwarding path, right?
Nhưng vấn đề là việc chuyển tiếp thực sự xảy ra ở giai đoạn thực thi, đúng không? Vì đó là nơi mux chọn dữ liệu từ đường chuyển tiếp, đúng không?
So even though we have that data at the decode stage, we have to kind of stage that signal for one cycle, so that we have to wait until that instruction enters the execute stage.
Vì vậy dù chúng ta có dữ liệu đó ở giai đoạn giải mã, chúng ta vẫn phải lưu tín hiệu đó lại một chu kỳ, để phải đợi đến khi lệnh đó vào giai đoạn thực thi.
And that's where we're going to use the control signal to select the forwarding data. Does that make sense?
Và đó là lúc chúng ta dùng tín hiệu điều khiển để chọn dữ liệu chuyển tiếp. Các bạn hiểu chứ?
Now there is a timing difference between when it is determined if the forwarding is possible or not to when the forwarding actually happens.
Bây giờ có một độ lệch thời gian giữa lúc xác định có thể chuyển tiếp hay không và lúc việc chuyển tiếp thực sự xảy ra.
So we have to use this pipeline register to stage that signal and make up for that timing difference. Yeah. So now for this one, the idea is the same, right?
Vì vậy chúng ta phải dùng thanh ghi đường ống này để lưu tín hiệu đó và bù cho độ lệch thời gian ấy. Vâng. Với bài này, ý tưởng vẫn như vậy, đúng không?
So we already have the data for numbers again for the fetch stage, memory stage, and write back stage.
Chúng ta lại đã có số liệu cho giai đoạn nạp lệnh, giai đoạn bộ nhớ và giai đoạn ghi lại.
Oh wait, let me just check here. I'm not muted there. Hello? Oh wait. Please don't sound muted.
Ồ khoan, để tôi kiểm tra chỗ này. Tôi không bị tắt tiếng chứ? Xin chào? Ồ khoan. Đừng để nghe như bị tắt tiếng nhé.
I think I can find it. And we also... Oh yeah, so that's that. And we're going to again work with the decode stage first.
Tôi nghĩ tôi tìm được rồi. Và chúng ta cũng... Ồ vâng, vậy là xong. Và chúng ta lại làm với giai đoạn giải mã trước.
Now at the decode stage, there are two things happening, right? One is we work with the register file that was already there.
Ở giai đoạn giải mã có hai việc xảy ra, đúng không? Một là chúng ta làm việc với tập thanh ghi vốn đã có sẵn.
The other is we have to do forwarding logic, which now is included in the decode stage. So for here, there are two different paths. One is the register file.
Hai là chúng ta phải làm logic chuyển tiếp, mà bây giờ được đưa vào giai đoạn giải mã. Vậy ở đây có hai đường khác nhau. Một là tập thanh ghi.
The other is the forwarding logic.
Hai là logic chuyển tiếp.
And that path is going to be from here to the forwarding, and then it just ends there, right? So for that path, for the register file, this was 550 in total.
Và đường đó sẽ đi từ đây đến bộ chuyển tiếp, rồi kết thúc ở đó, đúng không? Với đường qua tập thanh ghi, tổng cộng là 550.
For this path, this is 300. So for the decode stage alone, the critical delay is still 550.
Với đường này, là 300. Vậy riêng giai đoạn giải mã, độ trễ tới hạn vẫn là 550.
All right, but what about the execute stage? So now we still have three different delay paths, right?
Được rồi, nhưng còn giai đoạn thực thi thì sao? Bây giờ chúng ta vẫn có ba đường trễ khác nhau, đúng không?
One is the register to the branch comparator, which has no reason to change in any way. So it's still 5 plus 200, 250 per second.
Một là từ thanh ghi đến bộ so sánh rẽ nhánh, đường này không có lý do gì để thay đổi. Vậy nó vẫn là 5 cộng 200, 250 trên giây.
Second is through the RS1. So we have this path right here going on, which is also this path as well. But those two paths have the same delay, right?
Đường thứ hai đi qua RS1. Vậy là ta có đường này ở ngay đây, và nó cũng chính là đường này. Nhưng hai đường đó có cùng độ trễ, đúng không?
Because only the difference is whether the signal arrives at the input or the oscillation. And that wire does nothing on its path.
Vì chỉ khác nhau ở chỗ tín hiệu đến ở đầu vào hay ở dao động. Và dây dẫn đó không gây thêm gì trên đường đi của nó.
So we have the RS1 MUX, and then the ALU, which is 50 times...
Vậy ta có bộ dồn kênh RS1, rồi đến ALU, là 50 nhân...
What's a MUX 50? A MUX takes 100. So 50 times 100 times 300, or 50.
Bộ dồn kênh là 50 à? Một bộ dồn kênh mất 100. Vậy 50 cộng 100 cộng 300, hay 50.
Second, the third is the one that goes through the immediate generator in gen.
Đường thứ ba là đường đi qua bộ sinh giá trị tức thời.
And then the RS2 MUX, which is here, to ALU.
Rồi đến bộ dồn kênh RS2, ở ngay đây, rồi tới ALU.
Let me make sure this is MUX. That's going to be 50, 100. This immediate generator is 100. Oops, and another 100.
Để tôi kiểm tra lại đây có đúng là bộ dồn kênh không. Cái đó sẽ là 50, 100. Bộ sinh giá trị tức thời này là 100. Ối, cộng thêm 100 nữa.
And then ALU, which is 550 picoseconds.
Rồi đến ALU, tổng cộng là 550 picô giây.
So that's going to be the critical delay for the exe stage. Now, is that the critical path for the entire pipeline? No, right? Because the memory stage is longer now.
Đó sẽ là độ trễ tới hạn của tầng exe. Vậy đó có phải là đường tới hạn của toàn bộ đường ống không? Không, đúng không? Vì giờ tầng bộ nhớ dài hơn.
So now, even the execution stage, the critical path... The execution stage is no longer the critical path, and that has moved on to the memory stage.
Nên bây giờ, ngay cả tầng thực thi, đường tới hạn... Tầng thực thi không còn là đường tới hạn nữa, và nó đã chuyển sang tầng bộ nhớ.
So the critical path for the entire design is at the memory stage with 600 picoseconds.
Vậy đường tới hạn của toàn bộ thiết kế nằm ở tầng bộ nhớ, với 600 picô giây.
So, yeah, now you get the idea.
Vâng, giờ các bạn đã hiểu ý tưởng rồi.
We moved the forwarding logic to happen one stage earlier, where we staged this control signal using a pipeline register, and that kind of cuts the delay path in the middle.
Ta chuyển logic chuyển tiếp sang xảy ra sớm hơn một tầng, nơi ta lưu tín hiệu điều khiển này bằng một thanh ghi đường ống, và điều đó cắt đường trễ ra làm đôi.
And now the forwarding logic happens over two cycles. You can think of it, right?
Giờ logic chuyển tiếp diễn ra qua hai chu kỳ. Các bạn có thể hình dung như vậy.
It determines whether it's going to happen or not on the critical stage, and actually executes that in the execute stage. So if we were to explain this, is...
Nó quyết định có xảy ra hay không ở tầng tới hạn, và thực sự thực hiện việc đó ở tầng thực thi. Vậy nếu phải giải thích thì...
Why?
Tại sao?
Well, obviously the answer for which design achieves the higher clock frequency is the number two, because the critical path is shorter by 600 compared to 700.
Rõ ràng, đáp án cho câu hỏi thiết kế nào đạt tần số xung nhịp cao hơn là thiết kế số hai, vì đường tới hạn ngắn hơn, 600 so với 700.
And the reason why that happens is the pipeline register, which is for the A cell, A cell D to L cell X, cuts the delay path, or the forwarding data path,
Và lý do là nhờ thanh ghi đường ống, dành cho A cell, từ A cell D đến L cell X, cắt đường trễ, hay đường dữ liệu chuyển tiếp,
into two separate clock cycles.
thành hai chu kỳ xung nhịp riêng biệt.
And now the forwarding logic, or forwarding control logic, happens in the D stage, where it is overlapped with the register file.
Và bây giờ logic chuyển tiếp, hay logic điều khiển chuyển tiếp, diễn ra ở tầng D, nơi nó chồng lên với tệp thanh ghi.
Register file takes longer to finish, and that completely hides the forwarding logic delay, and it no longer lengthens the execute critical path.
Tệp thanh ghi mất nhiều thời gian hơn để hoàn thành, và điều đó che hoàn toàn độ trễ của logic chuyển tiếp, và nó không còn kéo dài đường tới hạn của giai đoạn execute.
Any questions? All right.
Có câu hỏi nào không? Được rồi.
Nice. Everyone got their full points. I have to check that later. Yeah. All right. Can you hear me? Okay.
Tuyệt. Mọi người đều được điểm tối đa. Lát nữa tôi phải kiểm tra lại. Vâng. Được rồi. Các em nghe thấy tôi không? Được rồi.
Give me a thumbs up. Can you hear me?
Giơ ngón cái lên giúp tôi. Các em nghe thấy tôi không?
Oh, I hope I'm not muted. Yes. Okay, thank you. All right. So for the part B.
Ôi, hy vọng là tôi không bị tắt tiếng. Vâng. Được rồi, cảm ơn các em. Được rồi. Vậy với phần B.
So now the question is, so far we only played with just ALU to ALU data path, right? Now we add mem to ALU forwarding path.
Câu hỏi bây giờ là, cho đến giờ ta mới chỉ làm với đường dữ liệu ALU tới ALU, đúng không? Bây giờ ta thêm đường chuyển tiếp từ mem tới ALU.
And now the data path looks like this, where the forwarding logic obviously has to take three inputs because it has two different paths to get the forwarding data from.
Và giờ đường dữ liệu trông như thế này, trong đó logic chuyển tiếp rõ ràng phải nhận ba đầu vào vì nó có hai đường khác nhau để lấy dữ liệu chuyển tiếp.
Now the one that's the receiving side is at the decode stage, and the data can come from either the execute stage or the memory stage, right?
Phía nhận là ở giai đoạn decode, và dữ liệu có thể đến từ giai đoạn execute hoặc giai đoạn memory, đúng không?
The path where the actual data comes from is over here. That's where the register data or the ALU or the memory result is going to flow through.
Đường mà dữ liệu thực sự đi tới là ở đây. Đó là nơi dữ liệu thanh ghi hoặc ALU hoặc kết quả bộ nhớ sẽ chạy qua.
But in order to determine if that's possible or not, that's what's going to happen inside the forwarding logic, and that's going to select the mux signal. All right.
Nhưng để xác định điều đó có khả thi hay không, việc đó sẽ diễn ra bên trong logic chuyển tiếp, và nó sẽ chọn tín hiệu cho mux. Được rồi.
So the first question asks, would the memory to ALU forwarding path completely eliminate stalls in the following code sequence?
Câu hỏi đầu tiên hỏi, liệu đường chuyển tiếp từ memory tới ALU có loại bỏ hoàn toàn các stall trong đoạn mã sau không?
So what's happening here is there was one correction that was made in the exam, which is this not LD. LD is, there's no LD in RISC-V. It's actually LW.
Ở đây có một chỗ đã được sửa trong đề thi, đó là chỗ này không phải LD. LD thì không có trong RISC-V. Thực ra là LW.
It really doesn't matter for the question. Here we have the load and then another arithmetic instruction that actually uses that data. So we have a data hazard over X2.
Điều này thực sự không ảnh hưởng đến câu hỏi. Ở đây ta có lệnh load và sau đó một lệnh số học dùng chính dữ liệu đó. Vậy ta có một data hazard trên X2.
And by the way, that's on the RS1 path, right? So even though we don't have forwarding for RS2, that's not a problem here.
Và nhân tiện, đó là trên đường RS1, đúng không? Nên dù ta không có chuyển tiếp cho RS2, điều đó không thành vấn đề ở đây.
We still need to do forwarding or we can still think about doing forwarding or not. So can we forward that data using memory to ALU and eliminate the stalls completely?
Ta vẫn cần chuyển tiếp, hoặc ít nhất có thể xem xét có chuyển tiếp hay không. Vậy ta có thể chuyển tiếp dữ liệu đó bằng memory tới ALU và loại bỏ hoàn toàn các stall không?
So for that, you can just draw the pipeline stages, right? So the first one is going to proceed with F, D, X, M, and W. And the second one will go F, D.
Để trả lời, em chỉ cần vẽ các giai đoạn của đường ống, đúng không? Lệnh đầu tiên sẽ chạy qua F, D, X, M và W. Còn lệnh thứ hai sẽ là F, D.
But the problem is, say we have the load stage here and the addI is right here.
Nhưng vấn đề là, giả sử giai đoạn load ở đây và addI nằm ngay đây.
And the addI needs to get that X2 value, the up-to-date X2 value, which will be produced here.
Và addI cần lấy giá trị X2, giá trị X2 mới nhất, sẽ được tạo ra ở đây.
Because the X2 value will be produced by reading from the data memory and being output right here. But right now, can we forward that data or not? We can't forward it, right?
Vì giá trị X2 sẽ được tạo ra bằng cách đọc từ bộ nhớ dữ liệu và được xuất ra ngay tại đây. Nhưng lúc này, ta có thể chuyển tiếp dữ liệu đó không? Không thể chuyển tiếp, đúng không?
Because the data is right here. But the actual forwarding path happens here. Because that's what the MX forwarding is. So we have to wait until the load instruction reaches here.
Vì dữ liệu đang ở ngay đây. Nhưng đường chuyển tiếp thực sự lại nằm ở đây. Vì đó chính là đường chuyển tiếp MX. Nên ta phải đợi đến khi lệnh load đến đây.
At that point, we can get that data through the MX wire to addi.
Lúc đó, ta có thể lấy dữ liệu qua đường MX đến lệnh addi.
So the idea is, we have to wait until the memory stage finishes, or the load instruction finishes the memory stage. And at that point, we can only get the up-to-date x2 register.
Vậy ý tưởng là ta phải đợi cho đến khi giai đoạn bộ nhớ kết thúc, hay lệnh load hoàn tất giai đoạn bộ nhớ. Và chỉ đến lúc đó ta mới có được giá trị mới nhất của thanh ghi x2.
So we do know that there has to be one stall cycle. Now the question is, where should we stall in this cycle? Which stage do you think we have to stall at?
Vậy ta biết chắc phải có một chu kỳ stall. Câu hỏi bây giờ là, ta nên stall ở đâu trong chu kỳ này? Các bạn nghĩ ta phải stall ở giai đoạn nào?
Do we have to stall at D or X?
Ta phải stall ở D hay X?
X. X. Why doesn't it work if we stall at D?
X. X. Tại sao stall ở D lại không được?
So we're going to discard the data, whatever data that comes from the decode stage, by just selecting the other wire, right? So actually, it doesn't matter if we stall at D or X.
Vì ta sẽ loại bỏ dữ liệu, bất kỳ dữ liệu nào đến từ giai đoạn giải mã, bằng cách chọn đường dây còn lại, đúng không? Thực ra, stall ở D hay X đều không quan trọng.
I think stalling at X makes a little more sense, because now we're at X, but we're waiting for the right data there.
Tôi nghĩ stall ở X hợp lý hơn một chút, vì lúc này ta đang ở X, nhưng đang chờ dữ liệu đúng ở đó.
So it kind of makes sense to make the most progress as possible.
Nên cũng hợp lý khi tiến được càng xa càng tốt.
But it doesn't really matter if you stall at D or X, because in the end, what we experience is just one stall cycle. So I'm just going to stall at D.
Nhưng stall ở D hay X thật ra không quan trọng, vì cuối cùng, ta chỉ thấy đúng một chu kỳ stall. Nên tôi sẽ stall ở D.
But obviously, that's not going to affect the grading. And the idea is we're not going to be able to avoid that one stall cycle.
Nhưng tất nhiên, điều đó sẽ không ảnh hưởng đến việc chấm điểm. Và ý chính là ta sẽ không thể tránh được một chu kỳ stall đó.
So what's the conceptual reason for that? So what you have to think about is when the data is produced, at which stage, and when the data is used, at which stage.
Vậy lý do về mặt khái niệm là gì? Điều bạn cần nghĩ đến là dữ liệu được tạo ra ở giai đoạn nào, và dữ liệu được dùng ở giai đoạn nào.
So if you think about it, the load instruction, or the load word instruction, produces data at the memory stage.
Nếu nghĩ kỹ, lệnh load, hay lệnh load word, tạo ra dữ liệu ở giai đoạn bộ nhớ.
But the addi actually uses that data at the X stage.
Nhưng lệnh addi lại dùng dữ liệu đó ở giai đoạn X.
And because producing of the data happens at a later stage, that means we cannot eliminate the stall, because we have to preserve the order from produce to use.
Và vì dữ liệu được tạo ra ở giai đoạn muộn hơn, nên ta không thể loại bỏ stall, vì ta phải giữ đúng thứ tự từ tạo ra đến sử dụng.
Because that order has to be still there. We cannot use the data that is not produced already.
Vì thứ tự đó vẫn phải được giữ nguyên. Ta không thể dùng dữ liệu chưa được tạo ra.
So these are one stage apart between produce and use.
Vậy chúng cách nhau một giai đoạn giữa lúc tạo ra và lúc sử dụng.
Therefore, they have to be one instruction apart, or at least one instruction apart, to preserve produce-to-use order.
Do đó, chúng phải cách nhau một lệnh, hoặc ít nhất một lệnh, để giữ đúng thứ tự từ tạo ra đến sử dụng.
Does that make sense? Right? Yeah.
Các bạn hiểu chứ? Đúng không? Vâng.
So what forwarding helps us is, it helps us to reduce the produce-to-use delay.
Vậy chuyển tiếp giúp ta giảm độ trễ từ lúc tạo ra đến lúc sử dụng.
Like, if the data is produced at the X stage, and the next instruction also wants that data at the X stage, then forwarding can work to eliminate any data-related stalls.
Ví dụ, nếu dữ liệu được tạo ra ở giai đoạn X, và lệnh kế tiếp cũng cần dữ liệu đó ở giai đoạn X, thì chuyển tiếp có thể loại bỏ mọi stall liên quan đến dữ liệu.
But it cannot help us with reversing that order for produce-to-use.
Nhưng nó không thể giúp ta đảo ngược thứ tự từ tạo ra đến sử dụng.
So even with the most advanced forwarding, we cannot eliminate the stall cycles, unless the load instruction somehow produces the data at the same stage as addI.
Vì vậy, dù chuyển tiếp tiên tiến nhất cũng không thể loại bỏ các chu kỳ stall, trừ khi lệnh load bằng cách nào đó tạo ra dữ liệu ở cùng giai đoạn với addI.
So, at least for our pipeline design, this is an inevitable stall cycle.
Vì vậy, ít nhất với thiết kế đường ống của chúng ta, đây là một chu kỳ stall không thể tránh khỏi.
Alright, so the next problem is, how do we implement the forwarding logic in Verilog? So the idea is, you're going to have to do this in your project, right?
Được rồi, vấn đề tiếp theo là, làm thế nào để cài đặt logic forwarding trong Verilog? Ý tưởng là, các bạn sẽ phải làm việc này trong project của mình, đúng không?
You're going to have to write your Verilog code to implement forwarding for your pipeline. And how can you do that? It's kind of like a sneak peek into what code you're going to write.
Các bạn sẽ phải viết mã Verilog để cài đặt forwarding cho đường ống của mình. Vậy làm cách nào để làm được điều đó? Đây coi như là một cái nhìn lướt qua về đoạn mã mà các bạn sẽ viết.
So for that, we have the code here.
Vì vậy, chúng ta có đoạn mã ở đây.
We're just going to go over the constants. We have all the mux selection signals, ASEL, PC. These are all for this mux right here, right?
Chúng ta chỉ xem qua các hằng số. Chúng ta có tất cả các tín hiệu chọn của mux, ASEL, PC. Tất cả những tín hiệu này dành cho mux ngay đây, đúng không?
And depending on the selection signal, we might have to choose the RS1 from PC, which is done when the ALU has to do PC plus immediate.
Và tùy theo tín hiệu chọn, ta có thể phải chọn RS1 từ PC, điều này xảy ra khi ALU phải tính PC cộng giá trị tức thời.
Or you can choose one to select the ALU-to-ALU forwarding data, or two to do MEM-to-ALU forwarding.
Hoặc có thể chọn một để lấy dữ liệu forwarding ALU-to-ALU, hoặc hai để thực hiện forwarding MEM-to-ALU.
And then three to just use the data that comes right from the register file, which is when forwarding does not happen.
Và ba để dùng dữ liệu đến thẳng từ tệp thanh ghi, tức là khi không xảy ra forwarding.
So that's going to be set here. And we already handled the PC case. PC case is when the branch is like a jump or branch instruction, which we don't want to worry about.
Vậy nó sẽ được thiết lập ở đây. Và chúng ta đã xử lý trường hợp PC rồi. Trường hợp PC là khi lệnh là lệnh nhảy hoặc rẽ nhánh, mà chúng ta không cần bận tâm.
We just assume that this control signal comes from the decode stage, which after the decoding of the output already happens.
Ta chỉ giả định rằng tín hiệu điều khiển này đến từ giai đoạn giải mã, sau khi việc giải mã đầu ra đã diễn ra.
And also, when no forwarding case is possible, we're going to default back to selecting from the register file. All right, so what's going to be the actual condition?
Và cũng vậy, khi không thể có trường hợp forwarding nào, ta sẽ mặc định quay về chọn từ tệp thanh ghi. Được rồi, vậy điều kiện thực tế sẽ là gì?
So when forwarding actually happens is when you have these three instructions. And say this instruction stores to RD by reading from RS1.
Forwarding thực sự xảy ra khi bạn có ba lệnh này. Giả sử lệnh này ghi vào RD bằng cách đọc từ RS1.
Let's not worry about RS2. This instruction stores to RD and RS1. And also it does RS1. And forwarding cannot happen when these registers match, right?
Đừng lo về RS2. Lệnh này ghi vào RD và RS1. Và nó cũng thực hiện RS1. Và forwarding không thể xảy ra khi các thanh ghi này khớp nhau, đúng không?
When they're the same, right?
Khi chúng giống nhau, đúng không?
So what's going to have to happen in this condition is we're going to compare those register bits. And if they're the same, then we can select the forwarding signal for the MUX.
Vì vậy, điều sẽ phải xảy ra trong điều kiện này là chúng ta sẽ so sánh các bit thanh ghi đó. Và nếu chúng giống nhau, thì ta có thể chọn tín hiệu forwarding cho MUX.
And also for these two cases, this one is the ALU-to-ALU forwarding, right? And this one is the MEM-to-ALU forwarding.
Và cũng với hai trường hợp này, đây là forwarding ALU-to-ALU, đúng không? Và đây là forwarding MEM-to-ALU.
Let me make that clear by actually drawing the pipeline here.
Để làm rõ hơn, tôi sẽ vẽ đường ống ra đây.
So for this part, that data will flow from the execute stage to here, which is the ALU-to-ALU forwarding.
Với phần này, dữ liệu sẽ chảy từ giai đoạn thực thi đến đây, đó là forwarding ALU-to-ALU.
And for these three instructions, which are one instruction apart, that has to flow from here, which is MEM-to-ALU forwarding.
Còn với ba lệnh này, cách nhau một lệnh, dữ liệu phải chảy từ đây, đó là forwarding MEM-to-ALU.
So now we know the signals or the conditions for ALU-to-ALU or MEM-to-ALU forwarding. But the problem is which one has to go to which one with these lengths?
Vậy bây giờ chúng ta đã biết các tín hiệu hay các điều kiện cho forwarding ALU-to-ALU hoặc MEM-to-ALU. Nhưng vấn đề là với các khoảng cách này thì cái nào phải đi tới cái nào?
Does this one have to have ALU-to-ALU and this one has to have MEM-to-ALU or vice versa? What do you think? Which one has to go for the first condition?
Lệnh này phải dùng ALU-to-ALU và lệnh này phải dùng MEM-to-ALU, hay ngược lại? Các bạn nghĩ sao? Cái nào phải đi với điều kiện đầu tiên?
There are two different conditions for ALU-to-ALU forwarding and then MEM-to-ALU forwarding. Which one do you have to check first? Which one do you have to prioritize?
Có hai điều kiện khác nhau cho chuyển tiếp ALU-to-ALU và chuyển tiếp MEM-to-ALU. Bạn phải kiểm tra cái nào trước? Phải ưu tiên cái nào?
Which is ALU-to-ALU forwarding, right? Because if these registers all match, we want to have the up-to-date data being forwarded to C.
Đó là chuyển tiếp ALU-to-ALU, đúng không? Vì nếu tất cả các thanh ghi này đều khớp, ta muốn dữ liệu mới nhất được chuyển tiếp tới C.
And B is going to be the one that produces the up-to-date data because B is actually executed after A, and that's going to produce the latest data.
Và B sẽ là lệnh tạo ra dữ liệu mới nhất đó vì B thực sự được thực thi sau A, nên nó sẽ tạo ra dữ liệu mới nhất.
So we always have to prioritize ALU-to-ALU forwarding. So on the first blank, it has to go against...
Vì vậy ta luôn phải ưu tiên chuyển tiếp ALU-to-ALU. Vậy ở chỗ trống đầu tiên, nó phải so với...
Oh, and by the way, if you actually look at these...
À, nhân tiện, nếu các bạn nhìn vào những chỗ này...
If you're confused about which one is the instruction in the decode, which one is the instruction at the X stage, you can look at this cycle, which is when the forwarding is actually detected.
Nếu bạn bối rối không biết lệnh nào đang ở giai đoạn giải mã, lệnh nào đang ở giai đoạn X, bạn có thể nhìn vào chu kỳ này, là lúc chuyển tiếp thực sự được phát hiện.
And the newest one, which is going to receive the data, is actually the one at the decode stage. So this is the INSTD.
Và lệnh mới nhất, tức lệnh sẽ nhận dữ liệu, chính là lệnh ở giai đoạn giải mã. Đây là INSTD.
And the ALU-to-ALU forwarding will be from the INSTX, MEM-to-ALU from the INSTM. So now it's clear, right?
Và chuyển tiếp ALU-to-ALU sẽ đến từ INSTX, còn MEM-to-ALU từ INSTM. Vậy giờ rõ rồi chứ?
For the first condition, it's going to be INSTD.RS1 equals INSTX.RD. All these signals are given as the input.
Với điều kiện đầu tiên, sẽ là INSTD.RS1 bằng INSTX.RD. Tất cả các tín hiệu này được cho làm đầu vào.
And for the second one, INSTD.RS1 equals INSTM.RD. Make sense?
Và với điều kiện thứ hai, INSTD.RS1 bằng INSTM.RD. Hiểu chứ?
And for the actual ALU signal, or the MUX signal, we're going to select ASEL forward ALU and ASEL forward MEM.
Còn với tín hiệu ALU thực tế, hay tín hiệu MUX, ta sẽ chọn ASEL forward ALU và ASEL forward MEM.
And if neither of those cases is satisfied, we're going to fall back to just doing no forwarding.
Và nếu không trường hợp nào thỏa mãn, ta quay về mặc định là không chuyển tiếp.
Okay, is this everything that we have to check?
Được rồi, đây đã là tất cả những gì ta phải kiểm tra chưa?
There's one case that we missed. Anyone know what that is?
Ta còn bỏ sót một trường hợp. Ai biết đó là gì không?
The hint is right over here.
Gợi ý ở ngay đây.
I don't think many people have thought this hint. So no one really bothers to read the comments. The zero register? Yeah.
Tôi nghĩ không nhiều người để ý đến gợi ý này. Vậy là chẳng ai buồn đọc phần chú thích. Thanh ghi zero à? Đúng.
But what's the case? What about the zero register? Sometimes you can't override it. Yeah.
Nhưng trường hợp đó là gì? Thanh ghi zero thì sao? Đôi khi bạn không thể ghi đè lên nó. Đúng.
That's a trick. So we're talking about this case where, say, we have two ADD instructions. And the first one writes to the X0 register.
Đó là một mẹo. Ta đang nói đến trường hợp, chẳng hạn, có hai lệnh ADD. Và lệnh đầu tiên ghi vào thanh ghi X0.
Sorry, yeah, I came up with that. And the second one reads from the X0, whatever this is.
Xin lỗi, đúng rồi, ví dụ này là do tôi nghĩ ra. Và lệnh thứ hai đọc từ X0, dù giá trị đó là gì.
And with just these conditions, we're going to just happily forward this data, which is the X1 plus X2, over to here.
Và chỉ với các điều kiện này, ta sẽ vui vẻ chuyển tiếp dữ liệu này, tức X1 cộng X2, sang đây.
But X0, that register is kind of like a fake register. It's always zero. So if the destination register is X0, we have to kind of cancel that forwarding because that data is already ready.
Nhưng X0, thanh ghi đó giống như một thanh ghi giả. Nó luôn bằng không. Vậy nếu thanh ghi đích là X0, thì ta phải hủy việc chuyển tiếp đó, vì dữ liệu ấy đã sẵn sàng rồi.
And so the final answer is you're going to have to AND that with INST DRS1 is not equal to X0.
Và vì vậy, đáp án cuối cùng là bạn phải AND điều kiện đó với INST DRS1 không bằng X0.
I'm sorry to do this for you. But the idea is you're going to make the same mistake in your project. We're going to have this instruction in the testbench.
Xin lỗi vì đã làm thế này với các bạn. Nhưng ý ở đây là các bạn sẽ mắc đúng lỗi này trong project của mình. Chúng tôi sẽ đưa lệnh này vào testbench.
So I'd rather have you just make the same mistake earlier in the midterm than in the project. Because otherwise, it's going to take a while to really realize this. Yeah.
Nên tôi muốn các bạn mắc lỗi này ở bài giữa kỳ sớm hơn là trong project. Vì nếu không thì sẽ mất khá lâu mới nhận ra được. Vâng.
But it's actually one of the more important cases you have to consider. And one other assumption here is we're only handling the R and I type of instructions.
Nhưng đây thực ra là một trong những trường hợp quan trọng hơn mà bạn phải cân nhắc. Và một giả định khác ở đây là ta chỉ xử lý các lệnh loại R và loại I.
So there are some instructions that don't even have the RS1 and RD bits. So those inputs are not always valid.
Có một số lệnh thậm chí không có các bit RS1 và RD. Nên các đầu vào đó không phải lúc nào cũng hợp lệ.
So when you actually write the code, you're going to include some more conditions to check if this is R or I type instructions.
Vì vậy khi bạn thực sự viết mã, bạn sẽ thêm một số điều kiện nữa để kiểm tra xem đó có phải lệnh loại R hay I không.
But that part we just omitted because that's too complicated. All right. So hope you enjoyed the problem. It's just there to prevent full points from anyone.
Nhưng phần đó chúng tôi bỏ qua vì nó quá phức tạp. Được rồi. Hy vọng các bạn thích bài toán này. Nó chỉ có ở đó để không ai được điểm tuyệt đối.
So that's it. And for the part C, this is one of the more classical problems where you fill out the table and count the number of cycles.
Vậy là xong. Còn phần C, đây là một trong những bài toán kinh điển hơn, nơi bạn điền vào bảng và đếm số chu kỳ.
And here's the actual code we're going to run. So let's actually look at how the code runs first. Now we're going to first use the addI to store this value to T1.
Và đây là đoạn mã mà ta sẽ chạy. Vậy trước hết hãy xem mã chạy như thế nào. Đầu tiên ta dùng addI để lưu giá trị này vào T1.
And then use branching to use that T1 data and see if we have to branch or not.
Rồi dùng lệnh rẽ nhánh với dữ liệu T1 đó để xem có phải rẽ nhánh hay không.
So there's one tiny error here in this problem, which is the addI only takes 12 bits immediate. And that is way too long for 12 bits.
Ở đây có một lỗi nhỏ trong bài toán này, đó là addI chỉ nhận hằng số tức thời 12 bit. Mà giá trị này dài hơn 12 bit rất nhiều.
So it's actually one of the errors in this problem. But we just decided to not clarify this because it doesn't really matter for solving the question.
Vậy nên đây thực ra là một lỗi của bài toán. Nhưng chúng tôi quyết định không đính chính vì nó không thực sự ảnh hưởng đến việc giải câu hỏi.
So in effect, only these four nibbles will get read into T1.
Nên thực tế chỉ bốn nibble này được đọc vào T1.
And okay, so that T1 is definitely not going to be equal to zero. This is the branch not equal to zero.
Và vâng, T1 chắc chắn sẽ không bằng không. Đây là lệnh rẽ nhánh nếu không bằng không.
And the T1 has something that's not equal to zero. So this will actually be taken. And we're going to jump to the load word instruction there.
Và T1 có giá trị khác không. Nên nhánh này thực sự sẽ được thực hiện. Và ta sẽ nhảy đến lệnh load word ở đó.
And for the next one, like I said in the midterm review lecture, the first thing you're going to want to do is you want to mark all the data hazards.
Còn với phần tiếp theo, như tôi đã nói trong buổi ôn tập giữa kỳ, việc đầu tiên bạn nên làm là đánh dấu tất cả các hazard dữ liệu.
This is going to annotate all the dependencies between the registers. So there is one for T2. For T3, we have this. T3, this.
Việc này sẽ chú thích mọi phụ thuộc giữa các thanh ghi. Có một cái cho T2. Với T3, ta có cái này. T3, cái này.
T4, we have this. And I think that's it. Yeah.
T4, ta có cái này. Và tôi nghĩ vậy là hết. Vâng.
So knowing that, now we're going to assume that the branches are predicted not taken. But this branch is taken, right? So we know that this is a misprediction here.
Biết được điều đó, giờ ta sẽ giả định rằng các lệnh rẽ nhánh được dự đoán là không được thực hiện. Nhưng nhánh này được thực hiện, đúng không? Vậy ta biết đây là một lần dự đoán sai.
And that said, mispredictions are handled by Python flushes. So we know we're going to have some Python flushing going. Assume all memory access are valid for obvious reasons.
Và như vậy, các lần dự đoán sai được xử lý bằng cách flush đường ống. Vậy ta biết sẽ có một số lần flush đường ống diễn ra. Giả sử mọi truy cập bộ nhớ đều hợp lệ, vì những lý do hiển nhiên.
And now we're going to consider that we have implemented both ALU-TLU and memTLU forwarding for both ALU and branch comparator.
Và bây giờ ta sẽ xét trường hợp đã triển khai cả forwarding ALU-ALU và forwarding từ bộ nhớ, cho cả ALU và bộ so sánh nhánh.
So in the class material, you never saw forwarding being implemented for the branch comparator, right? At least in the data path. Because the branch comparator also can use the forwarded data.
Trong tài liệu của lớp, các bạn chưa từng thấy forwarding được cài đặt cho bộ so sánh nhánh, đúng không? Ít nhất là trong datapath. Vì bộ so sánh nhánh cũng có thể dùng dữ liệu được forward.
So actually that wire should also go there for the full forwarding to happen, right? Because the actual branch comparison is going to happen here.
Nên thực ra đường dây đó cũng phải nối tới đây để forwarding đầy đủ diễn ra, đúng không? Vì phép so sánh nhánh thực sự sẽ diễn ra ở đây.
So in a sense, you're going to also have another mux there in your implementation to do forwarding. But we're going to assume that is also there.
Nên theo nghĩa nào đó, các bạn cũng sẽ có thêm một mux ở đó trong phần cài đặt của mình để thực hiện forwarding. Nhưng ta sẽ giả sử là nó cũng đã có.
So this forwarding can happen between the addI and pNEC. And for both RS1 and RS2. So, so far we only, the datapath only has shown forwarding for RS1.
Vậy forwarding này có thể xảy ra giữa addI và BNEZ. Và cho cả RS1 và RS2. Cho đến giờ, datapath chỉ cho thấy forwarding cho RS1.
But here we have all the RS1 and RS2. So this is kind of the full package forwarding, right? So that, you know, I don't want you to worry too much about the corner cases.
Nhưng ở đây ta có cả RS1 và RS2. Vậy đây là forwarding đầy đủ, đúng không? Nên tôi không muốn các bạn lo quá nhiều về các trường hợp biên.
All right. So we're going to count all cycles starting from the fetch state of the first instruction to the write back state of the last instruction.
Được rồi. Ta sẽ đếm tất cả các chu kỳ, bắt đầu từ trạng thái fetch của lệnh đầu tiên đến trạng thái write back của lệnh cuối cùng.
So just count everything, including the time it takes to fill up the pipeline. And some other details: like for the instruction column, you can just write the operator to save time on the writing.
Cứ đếm hết, kể cả thời gian để lấp đầy đường ống. Và một vài chi tiết khác: ở cột lệnh, các bạn chỉ cần viết toán tử để đỡ mất thời gian viết.
And also, this is kind of a small grammatical error. It's not that you may not use all the rows. You are not required to. You don't have to use all the rows.
Ngoài ra, đây là một lỗi ngữ pháp nhỏ. Ý là không nhất thiết phải dùng hết các hàng. Các bạn không bắt buộc. Các bạn không phải dùng hết các hàng.
And I think that was clarified in the example. So let's actually fill this out. So we have this addI.
Và tôi nghĩ điều đó đã được làm rõ trong ví dụ. Giờ ta hãy điền bảng này. Ta có lệnh addI này.
And then we have a branch, branch not equals to zero, which had a dependency on addI on the T1 register. Now, here, this will actually get predicted as not taken, right?
Rồi có một lệnh rẽ nhánh, branch not equal to zero, phụ thuộc dữ liệu vào addI ở thanh ghi T1. Ở đây, lệnh này thực ra sẽ bị dự đoán là không rẽ nhánh, đúng không?
So for the first few cycles, we're going to go down the wrong path, which is this path. It's not taken, so we're going to just go here, here, here.
Nên trong vài chu kỳ đầu, ta sẽ đi theo đường sai, chính là đường này. Nó không rẽ nhánh, nên ta cứ đi xuống đây, đây, đây.
So we're going to have addI, load word, and XORI, where this addI is actually from the false label. It's from the normal label.
Vậy ta sẽ có addI, load word, và XORI, trong đó addI này thực ra thuộc nhãn false. Nó thuộc nhãn bình thường.
You don't have to actually annotate this in your tables.
Các bạn không cần ghi chú điều này trong bảng.
And to fill out the table, the BNEZ is going to go with the F, D, X, M, W, where there's going to be ALU to ALU forwarding happening for the T1 register.
Và để điền bảng, BNEZ sẽ đi theo F, D, X, M, W, trong đó có forwarding ALU-ALU cho thanh ghi T1.
Because if you saw here, there's T1 that we use.
Vì nếu nhìn ở đây, có T1 mà ta sử dụng.
And for addI, for this path, we can just assume that everything is correct. And you're just going to do the normal forwarding whenever possible.
Còn với addI, ở đường này, ta có thể giả sử mọi thứ đều đúng. Và các bạn chỉ cần forwarding bình thường bất cứ khi nào có thể.
So addI, it's going to just do F, D, X, M, W. This didn't have any data hazards. Load word, also do F, D, X, M, W.
Vậy addI sẽ chỉ đi F, D, X, M, W. Lệnh này không có data hazard nào. Load word cũng đi F, D, X, M, W.
For load word, this actually uses the data from BNEZ, right? But it should be fine because, yeah, we can do the memory to ALU forwarding there.
Với load word, lệnh này thực ra dùng dữ liệu từ BNEZ, đúng không? Nhưng như vậy vẫn ổn, vì ở đó ta có thể dùng forwarding từ bộ nhớ tới ALU.
So that's kind of a, yeah, there's another data dependency on the T1 there. And XORI is going to do something like FDX.
Vậy nên đó là một kiểu, ừ, ở đó còn có thêm một phụ thuộc dữ liệu trên T1. Còn XORI thì sẽ làm gì đó như FDX.
But we don't really want to worry about these cycles because that's when everything is already flushed.
Nhưng ta không thực sự cần bận tâm đến các chu kỳ này vì lúc đó mọi thứ đã bị flush hết rồi.
And if you look at the pipeline, the branch is evaluated, the branch is like kind of resolved at, oh, not that one, the end stage, right?
Và nếu nhìn vào đường ống, lệnh rẽ nhánh được đánh giá, lệnh rẽ nhánh được xác định ở, ồ, không phải chỗ này, ở giai đoạn cuối, đúng không?
And we already went over this a number of times in the homework and also in the past exam. So I hope that's clear enough.
Và ta đã xem qua điều này nhiều lần trong bài tập về nhà và cả trong đề thi trước đây. Hy vọng là điều này đã đủ rõ.
But the branch is going to resolve in the memory stage. And only after that stage, we're going to know which one is the correct path and what address is that correct path.
Nhưng lệnh rẽ nhánh sẽ được xác định ở giai đoạn bộ nhớ. Và chỉ sau giai đoạn đó, ta mới biết đường nào là đường đúng và địa chỉ của đường đúng đó là gì.
So starting from there, we're going to go down the correct path again. And that's going to start from normal.
Vì vậy, bắt đầu từ đó, ta lại đi theo đường đúng. Và nó sẽ bắt đầu chạy bình thường.
And that's where we know the branch target address from the memory stage of BNEZ.
Và đó là lúc ta biết địa chỉ đích rẽ nhánh từ giai đoạn bộ nhớ của BNEZ.
And these ones are going to be flushed at that same cycle, right? And let me just mark this as the wrong path.
Và những lệnh này sẽ bị flush ngay trong chu kỳ đó, đúng không? Để tôi đánh dấu chỗ này là đường sai.
And so we're going to start again from the correct path where load word is going to go FDX and W.
Và như vậy ta bắt đầu lại từ đường đúng nơi load word sẽ đi qua F, D, X và W.
And then for XORI, the XORI has a dependency on the load word. And also the shift left instruction has a dependency on load word. So we're going to just mark that here.
Rồi với XORI, XORI có phụ thuộc vào load word. Và lệnh dịch trái cũng có phụ thuộc vào load word. Vậy ta sẽ đánh dấu ở đây.
And another dependency here is that this load word has been done on this one, on T2. So this one is also there.
Và một phụ thuộc khác ở đây là load word này đã được thực hiện trên cái này, trên T2. Vậy cái này cũng có ở đó.
And lastly for XORI, or just XOR, that uses the data coming from these arithmetic instructions. From here, T3 and T4. So those should be all of the data dependencies.
Và cuối cùng với XORI, hay đúng hơn là XOR, lệnh này dùng dữ liệu đến từ các lệnh số học này. Từ đây, T3 và T4. Vậy đó là tất cả các phụ thuộc dữ liệu.
So for XORI, we're going to go FD. But again, we have this chain that we talked about in the previous question, right?
Với XORI, ta sẽ đi F, D. Nhưng một lần nữa, ta có chuỗi này mà ta đã nói ở câu hỏi trước, đúng không?
That was this question. Where we do the load word and then we immediately use it. And we cannot avoid one stall cycle.
Chính là câu này. Nơi ta thực hiện load word rồi dùng ngay kết quả. Và ta không thể tránh được một chu kỳ stall.
So using that fact, we know that the memory to ALU forwarding is going to happen here. And here we have the stall. Again, it doesn't really matter which stage is stalled at there.
Vậy dựa vào điều đó, ta biết forwarding từ bộ nhớ tới ALU sẽ xảy ra ở đây. Và ở đây ta có stall. Một lần nữa, stall ở giai đoạn nào ở đó thực ra không quan trọng.
I'm just going to fill in D. But it doesn't matter if you filled out X there. It's just up to the implementation. And go to memory on the right.
Tôi sẽ điền D. Nhưng nếu bạn điền X ở đó cũng không sao. Điều đó tùy vào cách triển khai. Và sang giai đoạn bộ nhớ ở bên phải.
So yeah, we're at the correct cycle. And for the load word, we enter a fetch stage. But we have to stay at fetch stage here because the decode stage is occupied by XORI.
Vậy là ta đang ở đúng chu kỳ. Và với load word, ta vào giai đoạn fetch. Nhưng ta phải ở lại giai đoạn fetch ở đây vì giai đoạn decode đang bị lệnh XORI chiếm.
And then move on to DXMW. Now, this one had data dependencies to that load word. But that can actually flow through the register file.
Rồi chuyển sang DXMW. Lệnh này có phụ thuộc dữ liệu vào lệnh load word đó. Nhưng thực ra dữ liệu có thể đi qua register file.
Because when the load word writes the T2 value, we can read that in the same cycle of the decode stage. So that's kind of the other type of forwarding happening.
Vì khi load word ghi giá trị T2, ta có thể đọc giá trị đó ngay trong cùng chu kỳ ở giai đoạn decode. Đó là một kiểu forwarding khác đang diễn ra.
For the shift left register, again, this is another case of that load to use sequence. So we enter here. And then go D.
Với lệnh shift left register, đây lại là một trường hợp khác của chuỗi load to use. Vậy ta vào đây. Rồi đến D.
But we can only use that data from here, using memory to ALU. So we have to stall here. And do that. Lastly, for XOR. Again, we have the stalled fetch cycle.
Nhưng ta chỉ dùng được dữ liệu đó từ đây, qua đường từ memory đến ALU. Nên ta phải stall ở đây. Và làm như vậy. Cuối cùng là XOR. Lại có một chu kỳ fetch bị stall.
Simply because the decode stage is occupied. And then just go through the pipeline.
Đơn giản vì giai đoạn decode đang bị chiếm. Rồi cứ chạy qua đường ống.
Because this one is dependent on the previous one where we can just do ALU to ALU forwarding. And for this data hazard, we already know that the XORI has finished right back.
Vì lệnh này phụ thuộc vào lệnh trước đó nên ta chỉ cần forwarding từ ALU đến ALU. Còn với data hazard này, ta đã biết XORI đã hoàn thành từ trước rồi.
So we can read the up-to-date value. So in total, there is one data hazard stall cycle there. Two, another data stall.
Nên ta đọc được giá trị mới nhất. Vậy tổng cộng có một chu kỳ stall do data hazard ở đó. Hai, một stall dữ liệu khác.
And also these three control flow hazard stall cycles. And in total, we have 16 cycles that is taken to execute the code.
Và còn ba chu kỳ stall do control flow hazard này. Và tổng cộng, ta mất 16 chu kỳ để thực thi đoạn mã này.
Any questions here?
Có câu hỏi nào không?
I hope that's clear enough. Lastly, there's a 251A only question. Which I'm sure none of you really read through. Because, you know, to save the time.
Hy vọng là đủ rõ. Cuối cùng, có một câu hỏi chỉ dành cho 251A. Mà tôi chắc là không ai trong các bạn đọc kỹ. Vì, bạn biết đấy, để tiết kiệm thời gian.
But let's just go through this real quick to cover all of the questions.
Nhưng ta cứ lướt qua nhanh để bao quát hết các câu hỏi.
So this question is asking to show how you can reorder the instructions to eliminate some of the data hazard stalls that we've seen in the table.
Câu hỏi này yêu cầu chỉ ra cách bạn có thể sắp xếp lại các lệnh để loại bỏ bớt một số stall do data hazard mà ta đã thấy trong bảng.
So we actually have the freedom to move around these instructions now. Reorder the instructions to eliminate some of the data hazard stalls. Without changing the final values of the registers and data memory.
Vậy bây giờ ta được tự do di chuyển các lệnh này. Sắp xếp lại các lệnh để loại bỏ bớt một số stall do data hazard. Mà không làm thay đổi giá trị cuối cùng của các thanh ghi và bộ nhớ dữ liệu.
So that means this program still has to do the same thing. Whatever operation that is. At the end, the program state still has to remain the same.
Nghĩa là chương trình này vẫn phải làm đúng việc cũ. Dù đó là phép toán gì đi nữa. Cuối cùng, trạng thái chương trình vẫn phải giữ nguyên.
So what can we do to remove some of those cycles? So here, the idea is, well, you cannot reorder these instructions across the label.
Vậy ta có thể làm gì để bỏ bớt một số chu kỳ đó? Ý tưởng ở đây là, bạn không thể sắp xếp lại các lệnh này qua nhãn (label).
So things like we cannot just move this addI to here or there. So there's only limited choice that we have. And we definitely cannot reorder these instructions.
Nên ta không thể chỉ việc chuyển lệnh addI này đến đây hay chỗ kia. Vì vậy ta chỉ có lựa chọn hạn chế. Và chắc chắn ta không thể đổi thứ tự các lệnh này.
Because this one is producing Q1 and that one is using Q1. We cannot change those causal relationships. This is only one instruction there, so no reordering.
Vì lệnh này tạo ra Q1 còn lệnh kia dùng Q1. Ta không thể thay đổi những quan hệ nhân quả đó. Chỗ này chỉ có một lệnh, nên không có gì để sắp xếp lại.
So the only choice we have for reordering is inside the normal block. And what can happen is, let me just write the data dependencies between the five instructions.
Vậy lựa chọn duy nhất để sắp xếp lại là bên trong khối bình thường. Và điều có thể xảy ra là, để tôi viết ra các phụ thuộc dữ liệu giữa năm lệnh này.
We have these immediate load to use dependency. And then dependency between the loads. And also dependency between these arithmetic instructions.
Chúng ta có phụ thuộc từ lệnh load ngay lập tức đến lệnh sử dụng. Rồi phụ thuộc giữa các lệnh load. Và cả phụ thuộc giữa các lệnh số học này.
And if you recall, this part is what caused the hazard stalls. Immediate load to use. So the idea is, can we make those arrows to be at least one instruction apart?
Và nếu còn nhớ, phần này là nguyên nhân gây ra các stall do hazard. Từ load đến lệnh sử dụng ngay sau đó. Vậy ý tưởng là, liệu ta có thể làm cho các mũi tên đó cách nhau ít nhất một lệnh không?
Because then we can hide the stall. But at the same time, we have to preserve these orderings of those arrows. Because those are the causal relationships.
Vì khi đó ta có thể che giấu được stall. Nhưng đồng thời, ta phải giữ nguyên thứ tự của các mũi tên đó. Vì đó là các quan hệ nhân quả.
We cannot reverse those order, otherwise we're going to read the wrong value. So what can happen?
Ta không được đảo ngược thứ tự đó, nếu không ta sẽ đọc sai giá trị. Vậy điều gì có thể xảy ra?
The constraint is all dependency order is preserved, meaning the arrows still point upwards, not downwards.
Ràng buộc là mọi thứ tự phụ thuộc đều được giữ nguyên, nghĩa là các mũi tên vẫn hướng lên trên, không hướng xuống dưới.
What can we do? What's the thing we can do?
Ta có thể làm gì? Ta có thể làm gì đây?
Let's swap the XOR I with the second load word. Then I can get rid of the load hazard. Yeah, that's correct.
Hãy hoán đổi lệnh XOR I với lệnh load word thứ hai. Khi đó tôi có thể loại bỏ hazard của lệnh load. Vâng, đúng rồi.
So load word. We're going to do load word first, and then do XOR I, and then SLI, and XOR. So now, the load-to-use dependency is two instructions apart.
Vậy là load word. Ta sẽ thực hiện load word trước, rồi đến XOR I, rồi SLI, và XOR. Như vậy bây giờ, phụ thuộc từ load đến lệnh sử dụng cách nhau hai lệnh.
And there's still dependency between load and load. And there's arithmetic dependency between there. So now, these are two instructions apart, or one instruction apart, however you think.
Và vẫn còn phụ thuộc giữa load và load. Và có phụ thuộc số học ở giữa. Vậy bây giờ, những cặp này cách nhau hai lệnh, hoặc một lệnh ở giữa, tùy bạn hiểu thế nào.
And there's no longer going to be data hazards from there. Right? But does this eliminate all the cycles? All the stall cycles?
Và từ chỗ đó sẽ không còn data hazard nữa. Đúng không? Nhưng liệu điều này có loại bỏ hết các chu kỳ không? Tất cả các chu kỳ stall?
Are we happy with zero stall cycles now? So the trick is between these two loads. This one does T2 comma zero T1.
Bây giờ ta đã hài lòng với số chu kỳ stall bằng không chưa? Mấu chốt nằm ở giữa hai lệnh load này. Lệnh này thực hiện T2 phẩy zero T1.
Now, this one actually loads from that T2 to T3. So we still have data dependency between T2.
Còn lệnh này thì thực sự load từ T2 đó vào T3. Vậy ta vẫn còn phụ thuộc dữ liệu ở T2.
And if you draw the pipeline diagram, that T2 value is going to be produced here, and that T2 value is going to be used at the X stage, because we have to do address calculation of T2 plus zero.
Và nếu vẽ sơ đồ đường ống, giá trị T2 đó sẽ được tạo ra ở đây, và giá trị T2 đó sẽ được dùng ở giai đoạn X, vì ta phải tính địa chỉ T2 cộng zero.
So still, that X, we have that dependency, and still, we have this stall cycle, one stall cycle between the loads. So it's not really possible to eliminate all of the hazard stalls.
Vậy ở giai đoạn X đó, ta vẫn có phụ thuộc này, và vẫn có chu kỳ stall này, một chu kỳ stall giữa các lệnh load. Vậy nên không thể loại bỏ hết các stall do hazard.
But then again, the problem is if we can eliminate some of the data hazard stalls.
Nhưng vấn đề là liệu ta có thể loại bỏ được một số stall do data hazard hay không.
So the idea is there are two data hazard stalls removed, but one data stall introduced.
Vậy ý tưởng là có hai stall do data hazard được loại bỏ, nhưng có một stall dữ liệu được đưa vào.
And just to mark that here, this is where it got introduced. So introduced, removed.
Và để đánh dấu ở đây, đây là chỗ nó được đưa vào. Vậy là đưa vào, loại bỏ.
So in total, the number of cycles go down from 16 to 15. And we're not going to grade the actual number, but if you got the reordering correct, you get the full point.
Như vậy tổng số chu kỳ giảm từ 16 xuống 15. Và chúng tôi sẽ không chấm theo con số thực tế, nhưng nếu bạn sắp xếp lại đúng thì bạn được trọn điểm.
So the idea is this is one of the many optimizations that the compiler can do. The compiler actually has some freedom to reorder the instructions, as long as it does the same operation, right?
Vậy ý tưởng là đây là một trong nhiều tối ưu hóa mà trình biên dịch có thể thực hiện. Trình biên dịch thực sự có một chút tự do để sắp xếp lại các lệnh, miễn là nó thực hiện cùng một phép toán, đúng không?
You're going to input whatever C program or Python program, and the compiler is going to output some assembly program.
Bạn sẽ đưa vào bất kỳ chương trình C hay chương trình Python nào, và trình biên dịch sẽ xuất ra một chương trình hợp ngữ.
But we as a programmer are happy as long as the assembly program does the same thing. Does the same code. So for the compiler, they actually have to think about what microarchitecture we're targeting.
Nhưng với tư cách là lập trình viên, chúng ta hài lòng miễn là chương trình hợp ngữ đó làm đúng cùng một việc. Cùng một đoạn mã. Vì vậy, đối với trình biên dịch, nó thực sự phải nghĩ đến vi kiến trúc mà chúng ta nhắm tới.
Like, how can we reorder these instructions for this particular pipeline datapath to make this code run faster for that specific pipeline? Yeah, and that's pretty much just 251A scope.
Ví dụ, làm sao để sắp xếp lại các lệnh này cho đường dữ liệu đường ống cụ thể này để mã chạy nhanh hơn trên đường ống cụ thể đó? Vâng, và phần đó về cơ bản chỉ thuộc phạm vi của 251A.
So that's why the question is for 251A students. Yeah, so that's all for the pipeline questions. Any other questions?
Đó là lý do câu hỏi này dành cho sinh viên 251A. Vâng, vậy là hết các câu hỏi về đường ống. Còn câu hỏi nào khác không?
Pretty clear, right? I hope there wasn't too much confusion. Yeah.
Khá rõ ràng nhỉ? Hy vọng không có quá nhiều chỗ gây nhầm lẫn. Vâng.
And next week we're going to start with, again, covering the lecture material. All right, see you in the next week.
Và tuần sau chúng ta sẽ lại bắt đầu với phần nội dung bài giảng. Được rồi, hẹn gặp lại các bạn vào tuần sau.
Đă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.