1 / 49

第七章

第七章. PIPELINING. Outline. What is pipelining? The basic pipeline for a RISC instruction set The major hurdle of pipelining – pipeline hazards Data hazards Control hazards Performance. What is pipelining?.

kirkan
Télécharger la présentation

第七章

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第七章 PIPELINING

  2. Outline • What is pipelining? • The basic pipeline for a RISC instruction set • The major hurdle of pipelining – pipeline hazards • Data hazards • Control hazards • Performance

  3. What is pipelining? • Pipelining is an implementation technique whereby multiple instructions are overlapped in execution. • Not visible to the programmer • Each step in the pipeline completes a part of an instruction. • Each step is completing different parts of different instructions in parallel • Each of these steps is called a pipe stage or a pipe segment.

  4. What is pipelining? • Like Assembly line in the factory • The time required between moving an instruction one step down the pipeline is a machine cycle. • All the stages must be ready to proceed at the same time • Slowest pipe stage dominates • Machine cycle is usually one clock cycle (sometimes two, rarely more) • The pipeline designer’s goal is to balance the length of each pipeline stage.

  5. 時間 I I I 1 2 3 F E F E F E 1 1 2 2 3 3 (a) 循序執行 中間階段緩衝器 B1 指令提取 單元 執行單元 (b) 硬體組織 時間 時脈週期 1 2 3 4 指令 I F E 1 1 1 I F E 2 2 2 I F E 3 3 3 (c) 利用管線處理的執行 圖8.1 管線處理指令的基本觀念

  6. Pipeline Stages • Every stage is completed within one cpu clock • F (Fetch) • D (Decode) • E (Execute) • W (Write)

  7. 時間 時脈週期 1 2 3 4 5 6 7 指令 I F D E W 1 1 1 1 1 I F D E W 2 2 2 2 2 I F D E W 3 3 3 3 3 I F D E W 4 4 4 4 4 (a) 指令的執行被分成四個步驟 中間階段緩衝器 D : 解碼指令 與 提取運算元 E: 執行運作 F : 提取指令 W : 寫入結果 B1 B2 B3 (b) 硬體組織 圖8.2 管線的4 階段

  8. How about the clock of IF • Because all the stages must be completed at the same time, we can not expect IF to access main memory • Thus IF must access L1 cache • When cache is built in CPU, the access time of cache is almost the same as the time to execute other operations within CPU • Slowest pipe stage dominates

  9. Performance of Pipeline • If the stages are perfectly balanced, then the time per instruction on the pipelined machine – assuming ideal conditions--is equal to

  10. Pipeline Hazards • Structural hazards • Caused by resource conflict • Possible to avoid by adding resources – but may be too costly • Data hazards • Instruction depends on the results of a previous instruction in a way that is exposed by the overlapping of instructions in the pipeline • Can be mitigated somewhat by a smart compiler • Control hazards • When the PC does not get just incremented • Branches and jumps - not too bad

  11. 管線可能遇到的問題(data hazard) 時間 時脈週期 1 2 3 4 5 6 7 8 9 指令 I F D E W 1 1 1 1 1 Stall (拖延) I F D E W 除法 2 2 2 2 2 I F D E W 3 3 3 3 3 I F D E W 4 4 4 4 4 I F D E 5 5 5 5 圖8.3 執行作業耗費一個以上的時序週期所造成的影響 I3 waits I2’s result to do ALU

  12. 管線可能遇到的問題(control hazard or instrction hazard) 時間 時脈週期 1 2 3 4 5 6 7 8 9 指令 I F D E W 1 1 1 1 1 I F D E W 2 2 2 2 2 I F D E W 3 3 3 3 3 (a) 在連續時序週期中的指令執行步驟 時間 時脈週期c 1 2 3 4 5 6 7 8 9 階段 Bubble(氣泡), stall(間隔) F: 提取 F F F F F F 1 2 2 2 2 3 D: 解碼 D idle idle idle D D 1 2 3 E: 執行 E idle idle idle E E 1 2 3 W: 寫入 W idle idle idle W W 1 2 3 (b) 在連續時序週期中,每一個處理器階段所執行的功能 圖8.4 在快取中,F2的失誤所造成的管線拖延

  13. 管線可能遇到的問題(structural hazard) 時間 時脈週期 1 2 3 4 5 6 7 指令 I F D E W 1 1 1 1 1 I (載入) F D E M W 2 2 2 2 2 2 I F D E W 3 3 3 3 3 I F D E 4 4 4 4 I F D 5 5 5 圖8.5 位於管線時序上的Load 指令所造成的影響

  14. Pipeline can not accelerate the execution time of instructions, actually it will prolong the instruction execution time due to the latch added between stages • But pipeline will promote productivity • Shorten the completion time of one instruction

  15. Data Hazard • Add R2,R3,R4 • Sub R5,R4,R6 • One instruction’s source registers is the destination register of previous one

  16. Data Hazard 時間 時脈週期 1 2 3 4 5 6 7 8 9 指令 I (Add) F D E W 1 1 1 1 1 F D D E W I (Sub) 2 2 2A 2 2 2 F D E W I 3 3 3 3 3 I F D E W 4 4 4 4 4 圖8.6 由於D2與W1之間的資料相關性所造成的管線間隔

  17. F D E W 4 4 4 4 Forwarding also called bypassing, shorting, short-circuiting 時間 時脈週期 1 2 3 4 5 6 7 8 9 指令 I (Add) F D E W 1 1 1 1 1 F D E W I (Sub) 2 2 2 2 2 E F D W I 3 3 3 3 3 I 4 D2與W1之間的資料相關性利用forwarding解除間隔(stall)

  18. Forwarding(轉交) • Key is to keep the ALU result around • Add R2,R3,R4 • Sub R5,R4,R6 • How do we handle this in general? • Forwarded value can be at ALU output or Mem stage output • ADD produces R4 value at ALU output • SUB needs it again at the ALU input

  19. 來源 1 來源 2 SRC1 SRC2 暫存器檔 ALU RSL T 目的 (a) 資料路徑 SRC1,SRC2 RSL T E:執行 (ALU) E: Ex ecute W : 寫入 (ALU) (暫存器檔) 轉交路徑 (b) P osition of the source and result registers in the processor pipeline (b) 在處理器管線中之來源與結果暫存器的位置 圖8.7 利用管線處理的處理器中的運算元轉交處理

  20. Instruction side effect • An implicit data hazard • Auto inc/dec addressing mode (push, pop) • Carry flag • Add R1,R3 • AddwithCarry R2,R4 • R4  [R2] + [R4] + carry

  21. Control hazards (instruction hazard) 時間 時脈週期 1 2 3 4 5 6 指令 I F E 1 1 1 執行單元閒置 I (分支) F E 2 2 2 Branch penalty I F X 3 3 I F E k k k I F E k +1 k+ 1 k+ 1 圖8.8 分支指令所造成的閒置週期

  22. 時間 時脈週期 1 2 3 4 5 6 7 8 F D E W I 1 1 1 1 1 F I (分支) D E 2 2 2 2 F D X I 3 3 3 F X I 4 4 F D E W I k k k k k F D E I k+ 1 k+ 1 k+ 1 k+ 1 (a) 在Execute 階段中計算分支位址 時間 時脈週期 1 2 3 4 5 6 7 F D E W I 1 1 1 1 1 F D I (分支) 2 2 2 F X I 3 3 F D E W I k k k k k F D E I k+ 1 k+ 1 k+ 1 k+ 1 (b) 在Decode 階段中計算分支位址(減少branch penalty) 圖8.9 分支時序

  23. The instruction queue is used mainly for detecting and dealing with branches 指令提取單元 指令佇列 F : 提取指令 E : 執行 單元 W : 寫入 結果 D : 分派/ 解碼單元 圖8.10 在圖8.2b 的硬體組織中,使用指令佇列(預先提取)

  24. Branch Instructions • 20% of Dynamic execution is branch instructions, that is, every 5 instructions been executed has one branch • Branch has huge impact on pipeline, thus modern CPU has various strategies to handle this control hazard such as delay branch, branch prediction, branch folding

  25. Delayed branch (make the stall cycle useful) • Branch delay slot • Compiler has to find instructions to fill in the slot(85% complex compiler technique can handle one delay slot) • Maybe more than one slot, but it is hard for compiler to locate suitable instructions to fill in. • Compiler need to find an instruction which is always executed to fill in the slot whether the branch is taken or not

  26. LOOP Shift_left R1 Decrement R2 Branch=0 LOOP NEXT Add R1,R3 (a) 原來的程式迴圈 LOOP Decrement R2 Branch=0 LOOP Branch delay slot Shift_left R1 NEXT Add R1,R3 (b) 重新排序過的指令 圖8.12 為延遲分支重新排序的指令

  27. 時間 時脈週期 1 2 3 4 5 6 7 8 指令 遞減 F E 分支 F E Shift (延遲槽) F E 遞減(發生分支) F E 分支 F E Shift (延遲槽) F E Add (Branch not tak en) F E 圖8.13 在圖8.12b 的迴圈中,在最後兩回期間 說明填入延遲槽的執行時序

  28. Branch prediction(Speculative execution) • Static branch prediction • Fixed prediction for every branch • Branch taken • Branch not taken • Dynamic branch prediction • branch-prediction buffer • Branch target buffer (BTB)

  29. Branch-prediction buffer • this buffer contains a bit as to whether the branch was last taken or not (1 time history) • upon fetching the instruction, we also fetch the prediction bit (from a special cache) • if the bit is set, we predict that the branch is taken • if the bit is not set, we predict that the branch is not taken

  30. LT ST 發生分支 (BT) BNT LNT BT LT 沒有發生分支(BNT) (a) 2-狀態演算法(1 bit history) BT LNT SNT BNT BNT BNT BT ST: strongly taken LT: likely taken LNT: likely not taken SNT: strongly not taken BT BT BNT (b) 4-狀態演算法(2 bit history) Better Approach 圖8.15 分支預測演算法的狀態機表示法

  31. Branch target buffer (BTB) • we need to know both the branch condition outcome and the branch address by the end of the IF stage • we need to know where we are branching to before we have even decode the instruction as a branch! • doesn’t seem possible, but we will use a prediction buffer like before but enhanced: • We will use a branch-target buffer • This will store for a given branch • prediction of the branch outcome • andif the branch is taken, the predicted branch target

  32. The Target Buffer Send PC of current instruction to target buffer in IF stage If we get a hit (PC is in the table) then look up the predicted PC and branch prediction If predict taken, then update the PC with the predicted PC in the buffer otherwise increment PC as usual Thus, we predict the branch location before we have even decoded the current instruction to see if it is a branch! On a branch miss or misprediction, update the buffer by moving missing PC into buffer, or updating predicted PC/branch prediction bit

  33. Branch Folding • Notice that by using the branch target buffer • we are fetching the new PC value (or the offset for the PC) from the buffer • and then updating the PC • and then fetching the branch target location instruction • Instead, why not just fetch the next instruction? • In Branch folding, the buffer stores the instruction at the predicted location • If we use this scheme for unconditional branches, we wind up with a penalty of –1 (we are in essence removing the unconditional branch) • Note: for this to work, we must also update the PC, so we must store both the target location and the target instruction • this approach won’t work well for conditional branches

  34. PowerPC 601 architecture

  35. Branch folding • The instruction queue is used mainly for detecting and dealing with branches. The 601's branch unit scans the bottom four entries of the queue, identifying branch instructions and determining what type they are (conditional, unconditional, etc.). In cases where the branch unit has enough information to resolve the branch right then and there (e.g. in the case of an unconditional branch, or a conditional branch whose condition is dependent on information that's already in the condition register) then the branch instruction is simply deleted from the instruction queue and replaced with the instruction located at the branch target. • This branch-elimination technique, called branch folding, speeds performance in two ways. First, it eliminates an instruction (the branch) from the code stream, which frees up dispatch bandwidth for other instructions.Second, it eliminates the single-cycle pipeline bubble that usually occurs immediately after a branch.

  36. Pipeline and addressing mode • Complex addressing mode doesn’t save any clock cycle than simple addressing mode • Load (X(R1)),R2  complex addressing mode • Add #X,R1,R2 • Load (R2),R2  simple addressing mode • Load (R2),R2

  37. 時間 時脈週期 1 2 3 4 5 6 7 Load F D X + [R1] [X + [R1]] [[X + [R1]]] W F 轉交 下一個指令 F D E W (a) 複雜定址模式 Add F D X + [R1] W Load F D [X + [R1]] W Load F D [[X + [R1]]] W 下一個指令 F D E W (b) 簡單定址模式 圖8.16 使用複雜與簡單定址模式的等價作業

  38. 現代處理器的定址模式特色(RISC優先強調的部分)現代處理器的定址模式特色(RISC優先強調的部分) • 存取運算元時不需要存取記憶體一次以上 • 只有載入(load)與儲存(store)指令存取記憶體運算元 • 使用的定址模式沒有副作用 • 暫存器定址模式(不須計算EA) • 暫存器間接定址模式(不須計算EA) • 索引定址模式(只須一個clock計算EA)

  39. Add R1,R2 Compare R3,R4 Branch=0 . . . (a) 程式片段 Compare R3,R4 Add R1,R2 Branch=0 . . . (b) 重新排序過的指令 圖8.17 指令的重新排序 • 條件碼旗標所導入的相關性會降低compiler替指令重新排序的彈性

  40. Pipeline Datapath 暫存器檔 匯流排 A A 匯流排 B ALU R B 匯流排 C PC 控制訊號管線 遞增器 IMAR 指令解碼器 記憶體位址 (指令提取) 指令佇列 MDR/Write DMAR MDR/Read 指令快取 記憶體位址 (資料存取) 資料快取 Figure 8.18. Datapath modified for pipelined e x ecution, with 圖8.18 針對管線執行,以位於ALU 的輸入與輸出中的中間階段緩衝器來修改之資料路徑 interstage b uf fers at the input and output of the ALU.

  41. 超純量作業(superscalar) • Multiple-issue • Multiple pipeline • Wide bus to cache • Separate Integer’s Execute Unit and Floating’s Execute Unit • Usually multiple IEUs, FEUs

  42. F : 指令提取 單元 指令佇列 浮點數單元 分派單元 W : 寫入 結果 整數單元 圖8.19 包含兩個執行單元的處理器

  43. 時間 時脈週期 1 2 3 4 5 6 7 I (F add) F D E E E W 1 1 1 1A 1B 1C 1 I (Add) F D E W 2 2 2 2 2 I (Fsub) F D E E E W 3 3 3 3 3 3 3 I (Sub) F D E W 4 4 4 4 4 圖8.20 在圖8.19 的處理器中的指令執行流程範例,假定沒有遇到危障

  44. Out of order execution(脫序的執行) • In order issue • To avoid dead lock • Out of order execution • Reorder buffer • In order retire • Register renaming • Temporary registers for commitment • Imprecise exception • Allow instructions completion after exception instruction • Precise exception • instructions completion after exception instruction are aborted

  45. 系統交互連接匯流排 外部快取 單元 指令 資料 載入佇列 儲存佇列 預先提取 與分派單元 記憶體管理 單元 D-Cache I-Cache iTLB dTLB 指令緩衝區 浮點 暫存器 整數 暫存器 整數執行 單元 浮點單元 圖8.23 UltraSPARC II 處理器的主要構成要素

  46. 兩條整數管線處理 解碼 快取 延遲 寫入 提取 群組 執行 延遲 檢查  E C N1 N2 N3 W   E C N1 N2 N3 W 執行 執行 寫入 F D G 暫存器 執行 檢查  R X1 X2 X3 N3 W   R X1 X2 X3 N3 W 兩條浮點管線處理 指令緩衝區 圖8.24 UltraSPARC II 處理器的管線組織

  47. 效能考量 • T=(N x S)/R • Instruction throughput • Ps = R/S = N/T  一秒所能執行的指令數 • Only one L1 cache • Dmiss = ((1-hi) + d(1-hd)) x Mp • Dmiss = (0.05 + 0.03) x 17 = 1.36 cycles • L1,L2 cache • Dmiss = ((1-hi) + d(1-hd)) x (hs x Ms + (1-hs) x Mp) = 0.46 cycles

  48. 管線階段的數目 • 較遠的指令在管線中形成相關性 • 管線變長,分支的損失更嚴重 • 成本增加 • UltraSPARC II  9 stages • Intel Pentium pro  12 stages • Pentium 4  12 stages

  49. 結論 • 管線設計的考量 • 處理器的指令集 • 管線硬體的設計 • 編譯程式的設計 • 三者之間互動強烈

More Related