Consider the following code sequence.
SUB R1, R4, R3; R1 <- R4-R3 ADD R5, R1, R6; R5 <- R1+R6 LW R8, 12(R6); R8 <- MEM[12+R6] ADD R9, R5, R8; R9 <- R5+R8 SW R9, 16(R6); MEM[16+R6] <- R9
Suppose the code sequence is executed in a five-stage (IF, ID, EX, MEM and WB) MIPS pipeline processor with hazard detection and data forwarding units. Assume the processor includes separate instruction and data memories so that the structural hazard for memory references can be avoided.
(a) (5 分) Identify all the data hazards which can be solved by forwarding.
(b) (5 分) Identify all the data hazards which can not be solved by forwarding.
(c) (5 分) Determine the total number of clock cycles required for the execution of the code sequence.
(d) (5 分) Suppose the clock rate of the MIPS processor is 1GHz. Find the CPU time of the code sequence (in terms of ns).
編號 I1~I5。找 RAW(後面的指令讀前面指令寫入的暫存器):
| producer → consumer | 暫存器 | 結果產生的時間 | consumer 需要的時間 |
|---|---|---|---|
| I1 → I2 | R1 | I1 的 EX 末 | I2 的 EX 初 |
| I2 → I4 | R5 | I2 的 EX 末 | I4 的 EX(相隔兩條) |
| I3 → I4 | R8 | I3(lw)的 MEM 末 | I4 的 EX 初 |
| I4 → I5 | R9 | I4 的 EX 末 | I5(sw)要存的資料 |
(a) 結果比 consumer 需要的時間早出現,把 EX/MEM、MEM/WB 暫存器的值轉送回 ALU 就行:
I1→I2(R1)、I2→I4(R5)、I4→I5(R9)
(b) lw 的資料要到 MEM 階段結束才有,而 I4 緊接在後、下一個 cycle 就要在 EX 用,轉送也來不及(得往回送到過去的時間):load-use hazard
I3→I4(R8):要 stall 1 個 cycle
(c) I4 在 ID 多停一個 cycle(hazard detection 插入 bubble),之後 R8 由 MEM/WB 轉送到 EX
| cycle | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| I1 SUB | IF | ID | EX | MEM | WB | |||||
| I2 ADD | IF | ID | EX | MEM | WB | |||||
| I3 LW | IF | ID | EX | MEM | WB | |||||
| I4 ADD | IF | ID | stall | EX | MEM | WB | ||||
| I5 SW | IF | stall | ID | EX | MEM | WB |
沒有 stall:\(5+4=9\) cycles;多 1 個 stall → 10 cycles
10 個 clock cycles
(d) 1 GHz → 每個 cycle 1 ns
CPU time = \(10\times1\text{ ns}=10\) ns