Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pipelining: Basic and Intermediate Concepts

Similar presentations


Presentation on theme: "Pipelining: Basic and Intermediate Concepts"— Presentation transcript:

1 Pipelining: Basic and Intermediate Concepts
COE 403 Computer Architecture Prof. Muhamed Mudawar Computer Engineering Department King Fahd University of Petroleum and Minerals

2 Presentation Outline Pipelining Basics
MIPS 5-Stage Pipeline Microarchitecture Structural Hazards Data Hazards and Forwarding Load Delay and Pipeline Stall Control Hazards and Branch Prediction

3 What is Pipelining? Consider a task that can be divided into k subtasks The k subtasks are executed on k different stages Each subtask requires one time unit The total execution time of the task is k time units Pipelining is to overlap the execution The k stages work in parallel on k different tasks Tasks enter/leave pipeline at the rate of one task per time unit 1 2 k 1 2 k Serial Execution One completion every k time units Pipelined Execution One completion every 1 time unit

4 Synchronous Pipeline Uses clocked registers between stages
Upon arrival of a clock edge … All registers hold the results of previous stages simultaneously The pipeline stages are combinational logic circuits It is desirable to have balanced stages Approximately equal delay in all stages Clock period is determined by the maximum stage delay S1 S2 Sk Register Input Clock Output

5 Pipeline Performance Let ti = time delay in stage Si
Clock cycle t = max(ti) is the maximum stage delay Clock frequency f = 1/t = 1/max(ti) A pipeline can process n tasks in k + n – 1 cycles k cycles are needed to complete the first task n – 1 cycles are needed to complete the remaining n – 1 tasks Ideal speedup of a k-stage pipeline over serial execution k + n – 1 Pipelined execution in cycles Serial execution in cycles = Sk → k for large n nk Sk

6 Simple 5-Stage Processor Pipeline
Morgan Kaufmann Publishers 23 April, 2017 Simple 5-Stage Processor Pipeline Five stages, one cycle per stage IF: Instruction Fetch from instruction memory Select address: next instruction, jump target, branch target ID: Instruction Decode Determine control signals & read registers from the register file EX: Execute operation Load and Store: Calculate effective memory address Branch: Calculate address and outcome (Taken or Not Taken) MEM: Memory access for load and store only WB: Write Back result to register Chapter 4 — The Processor

7 Visualizing the Pipeline
Multiple instruction execution over multiple clock cycles Instructions are listed in program order from top to bottom Figure shows the use of resources at each stage and each cycle No interference between different instructions in adjacent stages Time (in cycles) sw r2, 10(r3) DM Reg CC5 ALU IM lw r6, 8(r5) IM CC1 Reg ori r4, r3, 7 ALU CC3 IM DM Reg sub r5, r2, r3 CC4 ALU IM add r9, r8, r7 CC2 Reg IM DM Reg CC6 ALU DM CC7 Reg ALU CC8 Reg DM Program Order

8 Timing the Instruction Flow
Time Diagram shows: Which instruction occupying what stage at each clock cycle Instruction flow is pipelined over the 5 stages Up to five instructions can be in the pipeline during the same cycle Instruction Level Parallelism (ILP) ALU instructions skip the MEM stage. Store instructions skip the WB stage lw r7, 8(r3) lw r6, 8(r5) ori r4, r3, 7 sub r5, r2, r3 sw r2, 10(r3) Instruction Order IF ID IF EX ID IF MEM EX ID IF WB MEM EX ID IF WB EX ID WB EX WB MEM Time CC1 CC4 CC5 CC6 CC7 CC8 CC9 CC2 CC3

9 Example of Pipeline Performance
Consider a 5-stage instruction execution pipeline … Instruction fetch = ALU = Data memory access = 350 ps Register read = Register write = 250 ps Compare single-cycle, multi-cycle, versus pipelined Assume: 20% load, 10% store, 40% ALU, and 30% branch Solution: Instruction Fetch Reg Read ALU Memory Reg Wr Time Load 350 ps 250 ps 1550 ps Store 1300 ps 1200 ps Branch 950 ps

10 Single-Cycle, Multi-Cycle, Pipelined
Each instruction = 1550 ps IF Reg ALU MEM Single-Cycle Execution: Tclock = = 1550 ps CPI = 1, but long clock cycle Multi-Cycle Execution: 350ps Load = 5 cycles Reg ALU IF MEM ALU = 4 cycles Branch = 3 cycles Tclock = 350 ps Average CPI = 5× × × ×0.3 = 3.9 350ps Pipelined Execution: IF Reg ALU MEM Tclock = 350 ps = max(350, 250) One instruction completes each cycle Average CPI = 1 Ignore time to fill pipeline

11 Single-Cycle, Multi-Cycle, Pipelined
Single-Cycle CPI = 1, but long clock cycle = 1550 ps Time of each instruction = 1550 ps Multi-Cycle Clock = 350 ps (faster clock than single-cycle) But average CPI = 3.9 (worse than single-cycle) Average time per instruction = 350 ps × 3.9 = 1365 ps Multi-cycle is faster than single-cycle by: 1550/1365 = 1.14x Pipeline Clock = 350 ps (same as multi-cycle) But average CPI = 1 (one instruction completes per cycle) Average time per instruction = 350 ps × 1 = 350 ps Pipeline is faster than single-cycle by: 1550/350 = 4.43x Pipeline is also faster than multi-cycle by: 1365/350 = 3.9x

12 Pipeline Performance Summary
Pipelining doesn’t improve latency of a single instruction However, it improves throughput of entire workload Instructions are initiated and completed at a higher rate In a k-stage pipeline, k instructions operate in parallel Overlapped execution using multiple hardware resources Potential speedup = number of pipeline stages k Unbalanced lengths of pipeline stages reduces speedup Pipeline rate is limited by slowest pipeline stage Also, time to fill and drain pipeline reduces speedup

13 Next . . . Pipelining Basics MIPS 5-Stage Pipeline Microarchitecture
Structural Hazards Data Hazards and Forwarding Load Delay and Pipeline Stall Control Hazards and Branch Prediction

14 Review of MIPS Instruction Formats
All instructions are 32-bit wide Three instruction formats: R-type, I-type, and J-type Op6: 6-bit opcode of the instruction Rs5, Rt5, Rd5: 5-bit source and destination register numbers sa5: 5-bit shift amount used by shift instructions funct6: 6-bit function field for R-type instructions immediate16: 16-bit immediate value or address offset immediate26: 26-bit target address of the jump instruction Op6 Rs5 Rt5 Rd5 funct6 sa5 Op6 Rs5 Rt5 immediate16 Op6 immediate26

15 Implementing Subset of Instructions
Only a subset of the MIPS instructions is considered ALU R-type instructions: dadd, dsub, and, or, xor, slt, sltu, . . . ALU I-type instructions: daddi, andi, ori, xori, slti, sltiu, . . . Load and Store (I-type): ld, sd, . . . Conditional Branch (I-type): beq, bne, . . . Jump (J-type): j and jal Jump Register (R-type): jr and jalr This subset does not include all the instructions Multiply, Divide, and Floating-Point instructions are not included But sufficient to illustrate design of datapath and control In today’s lecture, I will show you how to implement the following subset of MIPS instructions: add, subtract, or immediate, load, store, branch, and the jump instruction. The Add and Subtract instructions use the R format. The Op together with the Func fields together specified all the different kinds of add and subtract instructions. Rs and Rt specifies the source registers. And the Rd field specifies the destination register. The Or immediate instruction uses the I format. It only uses one source register, Rs. The other operand comes from the immediate field. The Rt field is used to specified the destination register. (Note that dest is the Rt field!) Both the load and store instructions use the I format and both add the Rs and the immediate filed together to from the memory address. The difference is that the load instruction will load the data from memory into Rt while the store instruction will store the data in Rt into the memory. The branch on equal instruction also uses the I format. Here Rs and Rt are used to specified the registers we need to compare. If these two registers are equal, we will branch to a location offset by the immediate field. Finally, the jump instruction uses the J format and always causes the program to jump to a memory location specified in the address field. I know I went over this rather quickly and you may have missed something. But don’t worry, this is just an overview. You will keep seeing these (point to the format) all day today. +3 = 13 min. (X:53)

16 Details of the MIPS Subset
Instruction Meaning Format R-type ALU DADD, DSUB, … op6=0 rs5 rt5 rd5 func6 dadd rd, rs, rt Rd  (Rs) + (Rt) 0x2C dsub rd, rs, rt Rd  (Rs) – (Rt) 0x2E I-type ALU DADDI, ANDI, … op6 Immediate16 daddi rt, rs, imm Rt  (Rs) + imm 0x18 andi rt, rs, imm Rt  (Rs) & imm 0x0C Load / Store LD, SD, LW, SW, … Displacement16 ld rt, imm(rs) RtMem[(Rs)+imm] 0x37 sd rt, imm(rs) RtMem[(Rs)+imm] 0x3F Cond Branch BEQ, BNE, … PC-Relative Offset beq rs, rt, target Branch if (Rs)==(Rt) 0x04 bne rs, rt, target Branch if (Rs)!=(Rt) 0x05 j target Jump 0x02 26-bit Instruction index jal target Jump and Link 0x03 jr rs Jump Register 0x08 jalr rd, rs Jump and Link Reg 0x09

17 MIPS Single-Cycle Datapath
R31 Op Branch Target Address ID = Instruction Decode & Register Read flags Z, N, C, V PCSrc ALU Reg Write A L U Address Instruction Cache Rs Rd Ext Rt Jump Target = Imm26 ALU result clk PC 00 Data Data_in Data_out Register File RA RB BusA BusB RW BusW +1 Mem Read Select Result EX = Execute Calculate Address IF = Instruction Fetch MEM = Memory Access (Load and Store) WB = Write Back 3 2 1 + Imm16 Jump Register Address Next PC Address Src Dst Return Address

18 Control Signal Values X is a don’t care to minimize control logic Op
Reg Dst Write ALU Src Mem Read Select Result PCSrc ALU-R Rd = 1 1 BusB = 0 func ALU = 1 Next PC = 0 ALU-I Rt = 0 Imm = 1 op Load ADD MEM = 0 Store X J Jump = 1 JR Jump Reg = 2 JAL R31 = 2 RA = 2 JALR BEQ SUB BR = 3 if (Z) else 0 BNE X is a don’t care to minimize control logic

19 Controlling the Single-Cycle Datapath
32 Address Instruction Cache PC 3 2 1 NextPC Jump JReg Branch A L U Flags ALU Control Op ALUOp Func RegDst RegWrite ALUSrc MemRead MemWrite SelectResult Main Control Op PC Control PCSrc Op Func Flags PC Control Input: 6-bit opcode field 6-bit function (JR, JALR) ALU Flags (for branch) Output: PCSrc signal Main Control Input: 6-bit opcode field Output: Main control signals ALU Control Input: 6-bit opcode field 6-bit function (R-type) Output: ALUOp signal

20 How to Pipeline the Datapath?
Answer: Introduce pipeline register at end of each stage R31 Op ID = Instruction Decode & Register Read flags Z, N, C, V PCSrc ALU Reg Write A L U Address Instruction Cache Rs Rd Ext Rt ALU result clk PC 00 Data Data_in Data_out Register File RA RB BusA BusB RW BusW +1 Mem Read Select Result EX = Execute Calculate Address IF = Instruction Fetch MEM = Memory Access (Load and Store) WB = Write Back 3 2 1 + Imm16 B Imm NPC2 IR NPC NPC3 Y D Src Dst Return Address Branch Target Address Jump Target = Imm26 Jump Register Address Next PC Address Wrong destination register

21 ID = Instruction Decode
MIPS 5-Stage Pipeline IF = Instruction Fetch ID = Instruction Decode & Register Read EX = Execute Calculate Address MEM = Memory Access (Load and Store) PCSrc 3 2 1 Branch Target Address Jump Register Address Jump Target = Imm26 NPC2 NPC3 Return Address Next PC Address NPC + Imm Imm16 WB = Write Back Ext ALU result flags Z, N, C, V +1 Address Instruction Cache Register File RA RB BusA BusB RW BusW A A L U Data Cache Address Data_in Data_out Rs PC 00 Y 2 1 Result Rt IR B 1 D ALU Op Select Result ALU Src Reg Write Mem Read Mem Write Destination register should be pipelined from ID to WB stage 2 1 Rd Rd2 Rd3 Rd4 R31 Reg Dst Op

22 Pipelined Control Pipeline the control signals as the instruction moves Extend the pipeline registers to include the control signals Each stage uses some of the control signals Instruction Decode (ID) Stage Generate all control signals Select destination register: RegDst control signal PC control uses: J (Jump) control signal for PCSrc Execution Stage  ALUSrc, and ALUOp PC control uses: JR, Beq, Bne, and ALU flags for PCSrc Memory Stage  MemRead, MemWrite, and SelectResult Write Back Stage  RegWrite is used in this stage

23 Pipelined Datapath + Control
PC Control J JR flags BEQ BNE PCSrc Jump Register Address R31 Branch Target Address flags A L U Address Instruction Cache Rs Rd Ext Rt Jump Address ALU result PC 00 Data Data_in Data_out Register File RA RB BusA BusB RW BusW +1 WB = Write Back 3 2 1 + Imm16 IR B Imm NPC2 NPC3 NPC Next PC Address Y Result D Return Address Rd2 Rd3 Rd4 ID = Instruction Decode EX = Execute MEM = Memory Access ALU Op Src JR BEQ BNE MEM WB Select Result Mem Read Write Reg Write Pass control Signals along pipeline just like data Control Signals Reg Dst Main & ALU Op, func J EX

24 Events on Every Pipe Stage
Register Transfer IF IR  I-Cache[PC]; NPC  PC+1 (Lower 2 bits are implicitly zeros) Branch = BEQ&Z | BNE&~Z (True if branch is taken, more branches can be added) PC  NPC2+Imm if (Branch); else A if (JR); else PCHI ǁ IR.Imm26 if (J); else PC+1 ID A  Reg[IR.Rs]; B  Reg[IR.Rt]; Imm  extend(IR.Imm16); NPC2  NPC Generate Control signals; EX control  signals sent to EX, MEM, and WB stages Rd2  Rt if (I-ALU or Load); Rd if (R-ALU); R31 if (JAL); else X (don’t care) EX ALUOp = func (R-ALU); Op if (I-ALU); ADD if (Load/Store); SUB if (Branch); else NOP ALUSrc = B if (R-ALU or Branch); Imm if (I-ALU or Load/Store); else X (don’t care) Y  ALUOp(A, B or Imm); D  B; Rd3  Rd2; NPC3  NPC2 MEM control  signals sent to MEM and WB stages MEM Result  D-Cache[Y] if (MemRead); Y if (R-ALU or I-ALU); NPC3 if (JAL or JALR); else X D-Cache[Y]  D if (MemWrite) Rd4  Rd3; WB control  RegWrite WB Reg[Rd4]  Result if (RegWrite)

25 Next . . . Pipelining Basics MIPS 5-Stage Pipeline Microarchitecture
Structural Hazards Data Hazards and Forwarding Load Delay and Pipeline Stall Control Hazards and Branch Prediction

26 Pipeline Hazards Hazard: Situation that would cause incorrect execution If next instruction were launched during its designated clock cycle Structural hazard Caused by hardware resource contention Using same resource by two instructions during same clock cycle Data hazard An instruction may depend on the result of a prior instruction still in the pipeline, that did not write back its result into the register file Control hazards Caused by instructions that change control flow (branches/jumps) Delays in changing the flow of control Hazards complicate pipeline control and limit performance

27 Structural Hazard Definition Example
Attempt to use the same hardware resource by two different instructions during the same clock cycle Example Writing back ALU result in stage 4 Conflict with writing load data in stage 5 Structural Hazard Two instructions are attempting to write the register file during same cycle WB EX ID MEM IF Time CC1 CC4 CC5 CC6 CC7 CC8 CC9 CC2 CC3 lw r6, 8(r5) ori r4, r13, 7 sub r5, r2, r13 sw r2, 10(r3) Instructions

28 Resolving Structural Hazard
Is it serious? Yes! cannot be ignored Solution 1: Delay access to resource Delay Write Back to Stage 5 Solution 2: Add more hardware Two write ports for register file (costly) Does not improve performance One fetch  one completion per cycle Resolving Structural Hazard Delay access to register file Write Back occurs only in Stage 5 WB EX ID MEM IF Time CC1 CC4 CC5 CC6 CC7 CC8 CC9 CC2 CC3 lw r6, 8(r5) ori r4, r13, 7 sub r5, r2, r13 sw r2, 10(r3) Instructions -

29 Example 2 of Structural Hazard
One Cache Memory for both Instructions & Data Instruction fetch requires cache access each clock cycle Load/store also requires cache access to read/write data Cannot fetch instruction and load data if one address port Time (in cycles) Program Order add r9, r8, r7 CC2 Reg Mem sub r5, r2, r3 CC4 ALU CC5 CC6 CC7 CC8 lw r6, 8(r5) CC1 ori r4, r3, 7 CC3 Structural Hazard

30 Stalling the Pipeline Delay Instruction Fetch  Stall pipeline (inject bubble) Reduces performance: Stall pipeline for each load and store! Better Solution: Use Separate Instruction & Data Caches Addressed independently: No structural hazard and No stalls Time (in cycles) Program Order add r9, r8, r7 CC2 Reg Mem CC4 ALU CC5 CC6 CC7 CC8 lw r6, 8(r5) CC1 ori r4, r3, 7 CC3 Stall Pipeline Inject Bubble sub r5, r2, r3 Mem Reg ALU bubble

31 Resolving Structural Hazards
Serious Hazard: structural hazard cannot be ignored Can be eliminated with careful design of the pipeline Solution 1: Delay Access to Hardware Resource Such as having all write backs to register file in the last stage, or Stall the pipeline until resource is available Solution 2: Add more Hardware Resources Add more hardware to eliminate the structural hazard Such as having two cache memories for instructions & data I-Cache and D-Cache can be addressed in parallel (same cycle) Known as Harvard Architecture Better than having two address ports for same cache memory

32 Performance Example Processor A: I-Cache + D-Cache (Harvard Architecture) Processor B: single-ported cache for instructions & data B has a clock rate 1.05X faster than clock rate of A Loads + Stores = 40% of instructions executed Ideal pipelined CPI = 1 (if no stall cycles) Which processor is faster and by what factor? Solution: CPIA = 1, CPIB = (due to structural hazards)

33 Next . . . Pipelining Basics MIPS 5-Stage Pipeline Microarchitecture
Structural Hazards Data Hazards and Forwarding Load Delay and Pipeline Stall Control Hazards and Branch Prediction

34 Data Hazard Occurs when one instruction depends on the result of a previous instruction still in the pipeline Previous instruction did not write back its result to register file Next instruction reads data before it is written Data Dependence between instructions Given two instructions I and J, where I comes before J Instruction J reads an operand written by I I: add r8, r6, r10 ; I writes r8 J: sub r7, r8, r14 ; J reads r8 Read After Write: RAW Hazard Hazard occurs when J reads register r8 before I writes it

35 Example of a RAW Data Hazard
Time (cycles) CC1 CC2 CC3 CC4 CC5 CC6 CC7 CC8 value of r8 10 10 10 10 10 20 20 20 DM Reg IM ALU sub r8, r9, r13 add r4, r8, r15 Program Execution Order or r16, r13, r8 and r17, r14, r8 sw r18, 16(r8) Result of sub is needed by add, or, and, & sw instructions Instructions add & or will read old value of r8 from reg file During CC5, r8 is written at end of cycle, old value is read

36 Solution 1: Stalling the Pipeline
Time (in cycles) CC1 CC2 CC3 CC4 CC5 CC6 CC7 CC8 CC9 value of r8 10 10 10 10 10 20 20 20 20 sub r8, r9, r13 IM Reg DM Reg ALU Instruction Order IM ALU add r4, r8, r15 Reg Reg Reg Reg DM Reg IM ALU or r16, r13, r8 DM Reg stall stall stall Three stall cycles during CC3 thru CC5 (wasting 3 cycles) Stall cycles delay execution of add & fetching of or instruction The add instruction cannot read r8 until beginning of CC6 The add instruction remains in the Instruction register until CC6 The PC register is not modified until beginning of CC6

37 Solution 2: Forwarding ALU Result
The ALU result is forwarded (fed back) to the ALU input No bubbles are inserted into the pipeline and no cycles are wasted ALU result is forwarded from ALU, MEM, and WB stages DM Reg Time (cycles) Program Execution Order value of r8 sub r8, r9, r13 IM CC1 10 CC2 add r4, r8, r15 CC3 or r16, r13, r8 ALU CC4 and r17, r16, r8 CC6 20 CC7 CC8 CC5 sw r18, 16(r8)

38 Implementing Forwarding
Two multiplexers added at the inputs of A & B registers Data from ALU stage, MEM stage, and WB stage is fed back Two signals: ForwardA and ForwardB control forwarding ForwardA ForwardB NPC3 Imm Return Address Imm16 Ext A L U ALU result Register File RB BusA BusB RW BusW RA 1 2 3 A Y Instruction Rs Data Cache Address Data_in Data_out Rt 1 2 1 Result B D 2 1 Rd Rd2 Rd3 Rd4 R31 clk

39 Forwarding Control Signals
Explanation ForwardA = 0 First ALU operand comes from register file = Value of (Rs) ForwardA = 1 Forward result of previous instruction to A (from ALU stage) ForwardA = 2 Forward result of 2nd previous instruction to A (from MEM stage) ForwardA = 3 Forward result of 3rd previous instruction to A (from WB stage) ForwardB = 0 Second ALU operand comes from register file = Value of (Rt) ForwardB = 1 Forward result of previous instruction to B (from ALU stage) ForwardB = 2 Forward result of 2nd previous instruction to B (from MEM stage) ForwardB = 3 Forward result of 3rd previous instruction to B (from WB stage)

40 Forwarding Example Instruction sequence: lw r4, 4(r8) ori r7, r9, 2
sub r3, r4, r7 When sub instruction is fetched ori will be in the ALU stage lw will be in the MEM stage ForwardA = 2 from MEM stage ForwardB = 1 from ALU stage lw r4,4(r8) ori r7,r9,2 sub r3,r4,r7 ForwardA=2 1 2 3 Y Rs Instruction ALU result Data Cache Address Data_in Data_out Rd4 A L U Rd3 Rd2 B Result D Imm Register File RB BusA BusB RW BusW RA Rt R31 Rd Imm16 Ext Return Address NPC3 ForwardB=1

41 RAW Hazard Detection Current instruction being decoded is in Decode stage Previous instruction is in the Execute stage Second previous instruction is in the Memory stage Third previous instruction in the Write Back stage If ((Rs != 0) and (Rs == Rd2) and (EX.RegWrite)) ForwardA  1 Else if ((Rs != 0) and (Rs == Rd3) and (MEM.RegWrite)) ForwardA  2 Else if ((Rs != 0) and (Rs == Rd4) and (WB.RegWrite)) ForwardA  3 Else ForwardA  0 If ((Rt != 0) and (Rt == Rd2) and (EX.RegWrite)) ForwardB  1 Else if ((Rt != 0) and (Rt == Rd3) and (MEM.RegWrite)) ForwardB  2 Else if ((Rt != 0) and (Rt == Rd4) and (WB.RegWrite)) ForwardB  3 Else ForwardB  0

42 Hazard Detect and Forward Logic
1 2 3 Y Rs Instruction ALU result Data Cache Address Data_in Data_out Rd4 A L U Rd3 Rd2 B Result D Imm Register File RB BusA BusB RW BusW RA Rt R31 Rd Imm16 Ext Return Address NPC3 ForwardB ForwardA ALUOp RegDst Main & ALU Control MEM EX WB RegWrite Op, func RegWrite Hazard Detect and Forward Rs Rt

43 Next . . . Pipelining Basics MIPS 5-Stage Pipeline Microarchitecture
Structural Hazards Data Hazards and Forwarding Load Delay and Pipeline Stall Control Hazards and Branch Prediction

44 However, load can forward data to 2nd next and later instructions
Load Delay Unfortunately, not all data hazards can be forwarded Load has a delay that cannot be eliminated by forwarding In the example shown below … The LD instruction does not read data until end of CC4 Cannot forward data to DADD at end of CC3 - NOT possible Reg Time (cycles) Program Order CC2 dadd r14, r12, r15 IM CC3 or r16, r13, r12 ALU CC6 DM CC7 CC8 ld r12, 24(r10) CC1 CC4 and r17, r12, r13 CC5 However, load can forward data to 2nd next and later instructions

45 Detecting RAW Hazard after Load
Detecting a RAW hazard after a Load instruction: The load instruction will be in the EX stage Instruction that depends on the load data is in the decode stage Condition for stalling the pipeline if ((EX.MemRead == 1) // Detect Load in EX stage and (ForwardA==1 or ForwardB==1)) Stall // RAW Hazard Insert a bubble into the EX stage after a load instruction Bubble is a no-op that wastes one clock cycle Delays the dependent instruction after load by once cycle Because of RAW hazard

46 Stall the Pipeline for one Cycle
DADD instruction depends on LD  stall at CC3 Allow Load instruction in ALU stage to proceed Freeze PC and Instruction registers (NO instruction is fetched) Introduce a bubble into the ALU stage (bubble is a NO-OP) Load can forward data to next instruction after delaying it Time (cycles) Program Order CC2 CC3 CC6 CC7 CC8 CC1 CC4 CC5 dadd r14, r12, r15 IM Reg ld r12, 24(r10) stall ALU bubble DM Reg Reg or r16, r13, r12 IM DM ALU

47 Stall Cycles Stall cycles are shown on a timing diagram
Hazard is detected in the Decode stage Stall indicates that instruction is delayed (bubble inserted) Instruction fetching is also delayed after a stall Example: Data forwarding is shown using green arrows ld r11, (r15) MEM WB EX ID IF ld r12, 8(r11) MEM WB EX ID Stall IF dadd r3, r12, r13 MEM WB EX ID Stall IF dsub r4, r11, r3 MEM WB EX ID IF Time CC1 CC4 CC5 CC6 CC7 CC8 CC9 CC2 CC3 CC10

48 Hazard Detect, Forward, and Stall
1 2 3 Y Rs Instruction ALU result Data Cache Address Data_in Data_out Rd4 A L U Rd3 Rd2 B Result D Imm Register File RB BusA BusB RW BusW RA Rt R31 Rd Imm16 Ext Return Address NPC3 PC ForwardB ForwardA Stall Disable PC Disable IR RegDst Main & ALU Control MEM EX WB RegWrite Op, func Control Signals Bubble = 0 1 RegWrite MemRead Hazard Detect Forward, & Stall Rs Rt

49 Compiler Scheduling to Avoid Stalls
Compilers reorder code in a way to avoid load stalls Consider the translation of the following statements: A = B + C; D = E – F; // A thru F are in Memory Original code: two stall cycles ld r10, 8(r16) ; &B = 8+(r16) ld r11, 16(r16) ; &C = 16+(r16) dadd r12, r10, r11 ; stall cycle sd r12, 0(r16) ; &A = (r16) ld r13, 32(r16) ; &E = 32+(r16) ld r14, 40(r16) ; &F = 40+(r16) dsub r15, r13, r14 ; stall cycle sd r15, 24(r16) ; &D = 24+(r16) Faster code: No Stalls ld r10, 8(r16) ld r11, 16(r16) ld r13, 32(r16) ld r14, 40(r16) dadd r12, r10, r11 sd r12, 0(r16) dsub r15, r13, r14 sd r15, 24(r16)

50 Name Dependence: Write After Read
Instruction J should write its result after it is read by I I: dsub r14, r11, r13 ; r11 is read J: dadd r11, r12, r13 ; r11 is written Called Anti-Dependence: Re-use of register r11 NOT a data hazard in the 5-stage pipeline because: Reads are always in stage 2 Writes are always in stage 5, and Instructions are processed in order Anti-dependence can be eliminated by renaming Use a different destination register for dadd (eg, r15)

51 Name Dependence: Write After Write
Same destination register is written by two instructions I: dsub r11, r14, r13 ; r11 is written J: dadd r11, r12, r13 ; r11 is written again Called Output Dependence: Re-use of register r11 Not a data hazard in the 5-stage pipeline because: All writes are ordered and always take place in stage 5 However, can be a hazard in more complex pipelines If Instruction J completes and writes r11 before instruction I Output dependence can be eliminated by renaming r11 Read After Read is NOT a name dependence

52 Next . . . Pipelining Basics MIPS 5-Stage Pipeline Microarchitecture
Structural Hazards Data Hazards and Forwarding Load Delay and Pipeline Stall Control Hazards and Branch Prediction

53 What is needed to calculate next PC?
For Unconditional Jumps Opcode (J or JAL), PC and 26-bit address (immediate) For Jump Register Opcode + function (JR or JALR) and Register[Rs] value For Conditional Branches Opcode, branch outcome (taken or not), PC and 16-bit offset For Other Instructions Opcode and PC value Opcode is decoded in ID stage  Jump delay = 1 cycle Branch outcome is computed in EX stage Branch delay = 2 clock cycles

54 2-Cycle Branch Delay Control logic detects a Branch instruction in the 2nd Stage ALU computes the Branch outcome in the 3rd Stage Next1 and Next2 instructions will be fetched anyway Convert Next1 and Next2 into bubbles if branch is taken beq r8,r9,L1 IF cc1 Next1 cc2 Reg IF Next2 L1: target instruction cc3 Branch Target Addr ALU Reg IF cc4 cc5 cc6 cc7 IF Reg DM ALU Bubble

55 Predict Branch NOT Taken
Branches can be predicted to be NOT taken If branch outcome is NOT taken then Next1 and Next2 instructions can be executed Do not convert Next1 & Next2 into bubbles No wasted clock cycles cc2 Reg IF Next2 beq r8,r9,L1 IF cc1 Next1 cc3 NOT Taken ALU Reg IF Reg cc4 cc5 cc6 cc7 ALU DM

56 Pipelined Jump and Branch
Address Instruction Cache Rs Rd Ext Rt Jump Address = Imm26 PC 00 Register File RA RB BusA BusB RW BusW +1 1 2 3 + A Imm16 IR B Imm NPC2 NPC3 NPC Next PC Address Y D Rd2 Rd3 L U Branch Target Address Jump Register Address Forward & Stall Stall Disable PC Disable IR Bubble = 0 Rs Rt Rd2, Rd3, Rd4 Bubble = 0 PCSrc Kill Kill2 J JR, BEQ, BNE flags PC Control Main & ALU Control Op, func Control Signals MEM EX 1 Jump kills next instruction Taken branch kills next two

57 Jump and Branch Impact on CPI
Base CPI = 1 without counting jump and branch stalls Unconditional Jump = 5%, Conditional branch = 20% and 90% of conditional branches are taken 1 stall cycle per jump, 2 stall cycles per taken branch What is the effect of jump and branch stalls on the CPI? Solution: Jump adds 1 stall cycle for 5% of instructions = 1 × 0.05 Branch adds 2 stall cycles for 20% × 90% of instructions = 2 × 0.2 × 0.9 = 0.36 New CPI = = 1.41

58 Branch Hazard Alternatives
Predict Branch Not Taken (previously discussed) Successor instruction is already fetched Do NOT Flush instruction after branch if branch is NOT taken Flush only instructions appearing after Jump or taken branch Delayed Branch Define branch to take place AFTER the next instruction Compiler/assembler fills the branch delay slot (only one slot) Dynamic Branch Prediction Loop branches are taken most of time How to predict the branch behavior at runtime? Must reduce the branch delay to zero, but how?

59 Delayed Branch Define branch to take place after the next instruction
MIPS defines one delay slot Reduces branch penalty Compiler fills the branch delay slot By selecting an independent instruction from before the branch Must be okay to execute instruction in the delay slot whether branch is taken or not If no instruction is found Compiler fills delay slot with a NO-OP label: . . . dadd r8,r9,r10 beq r5,r6,label Branch Delay Slot label: . . . beq r5,r6,label dadd r8,r9,r10

60 Drawback of Delayed Branching
New meaning for branch instruction Branching takes place after next instruction (Not immediately!) Impacts software and compiler Compiler is responsible to fill the branch delay slot However, modern processors and deeply pipelined Branch penalty is multiple cycles in deeper pipelines Multiple delay slots are difficult to fill with useful instructions MIPS used delayed branching in earlier pipelines However, delayed branching lost popularity in recent processors Dynamic branch prediction has replaced delayed branching

61 Branch Target Buffer (IF Stage)
The branch target buffer is implemented as a small cache Stores the target addresses of recent branches and jumps We must also have prediction bits To predict whether branches are taken or not taken The prediction bits are determined by the hardware at runtime mux PC Branch Target & Prediction Buffer Addresses of Recent Branches Target Addresses low-order bits used as index Predict Bits Inc = predict_taken

62 Branch Target Buffer – cont’d
Each Branch Target Buffer (BTB) entry stores: Address of a recent jump or branch instruction Target address of jump or branch Prediction bits for a conditional branch (Taken or Not Taken) To predict jump/branch target address and branch outcome before instruction is decoded and branch outcome is computed Use the lower bits of the PC to index the BTB Check if the PC matches an entry in the BTB (jump or branch) If there is a match and the branch is predicted to be Taken then Update the PC using the target address stored in the BTB The BTB entries are updated by the hardware at runtime

63 Dynamic Branch Prediction
Prediction of branches at runtime using prediction bits Prediction bits are associated with each entry in the BTB Prediction bits reflect the recent history of a branch instruction Typically few prediction bits (1 or 2) are used per entry We don’t know if the prediction is correct or not If correct prediction then Continue normal execution – no wasted cycles If incorrect prediction (or misprediction) then Kill the instructions that were incorrectly fetched – wasted cycles Update prediction bits and target address for future use

64 Dynamic Branch Prediction – Cont’d
IF ID EX Use PC to address Instruction Cache and Branch Target Buffer Increment PC PC = target address BTB Predict taken? No Yes Yes Jump? Enter jump & target address in BTB Kill fetched instruction. Restart PC at jump target address No Taken branch? Taken branch? No Yes No Yes Normal Execution Enter branch & target address, and set prediction in BTB entry. Kill fetched instructions. Restart PC at target address Mispredicted branch Kill fetched instructions Update prediction bits Restart PC after branch Correct Prediction No stall cycles

65 1-bit Prediction Scheme
Prediction is just a hint that is assumed to be correct If incorrect then fetched instructions are killed 1-bit prediction scheme is simplest to implement 1 bit per branch instruction (associated with BTB entry) Record last outcome of a branch instruction (Taken/Not taken) Use last outcome to predict future behavior of a branch Predict Not Taken Taken Not

66 1-Bit Predictor: Shortcoming
Morgan Kaufmann Publishers 23 April, 2017 1-Bit Predictor: Shortcoming Inner loop branch mispredicted twice! Mispredict as taken on last iteration of inner loop Then mispredict as not taken on first iteration of inner loop next time around outer: … … inner: … bne …, …, inner … bne …, …, outer Chapter 4 — The Processor

67 2-bit Prediction Scheme
1-bit prediction scheme has a performance shortcoming 2-bit prediction scheme works better and is often used 4 states: strong and weak predict taken / predict not taken Implemented as a saturating counter Counter is incremented to max=3 when branch outcome is taken Counter is decremented to min=0 when branch is not taken Not Taken Taken Strong Predict Weak

68 Evaluating Branch Alternatives
Branch Scheme Jump Branch Not Taken Branch Taken Predict not taken Penalty = 2 cycles Penalty = 0 cycles Penalty = 3 cycles Delayed branch Penalty = 1 cycle BTB Prediction Assume: Jump = 3%, Branch-Not-Taken = 5%, Branch-Taken = 15% Assume a branch target buffer with hit rate = 90% for jump & branch Prediction accuracy for jump = 100%, for conditional branch = 80% What is the impact on the CPI? (Ideal CPI = 1 if no control hazards) Branch Scheme Jump = 3% Branch NT = 5% Branch Taken = 15% CPI Predict not taken 0.03 × 2 0.15 × 3 = 0.45 1+0.51 Delayed branch 0.03 × 1 0.15 × 2 = 0.30 1+0.33 BTB Prediction 0.03×0.1×2 0.05×0.9×0.2×3 0.15×( ×0.2)×3 1+0.16

69 In Summary Three types of pipeline hazards
Structural hazards: conflict using a resource during same cycle Data hazards: due to data dependencies between instructions Control hazards: due to branch and jump instructions Hazards limit the performance and complicate the design Structural hazards: eliminated by careful design or more hardware Data hazards can be eliminated by forwarding However, load delay cannot be eliminated and stalls the pipeline Delayed branching reduces branch delay  compiler support BTB with branch prediction can reduce branch delay to zero Branch misprediction should kill the wrongly fetched instructions


Download ppt "Pipelining: Basic and Intermediate Concepts"

Similar presentations


Ads by Google