Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pipelining concepts, datapath and hazards

Similar presentations


Presentation on theme: "Pipelining concepts, datapath and hazards"— Presentation transcript:

1 Pipelining concepts, datapath and hazards
Lecture 17 CDA 3103

2 Boolean Exprs for Controller
rtype = ~op5  ~op4  ~op3  ~op2  ~op1  ~op0, ori = ~op5  ~op4  op3  op2  ~op1  op0 lw = op5  ~op4  ~op3  ~op2  op1  op0 sw = op5  ~op4  op3  ~op2  op1  op0 beq = ~op5  ~op4  ~op3  op2  ~op1  ~op0 jump = ~op5  ~op4  ~op3  ~op2  op1  ~op0 Instruction<31:0> Inst Memory Op 0-5 are really Instruction bits 26-31 <0:5> <26:31> <21:25> <16:20> <11:15> <0:15> Func 0-5 are really Instruction bits 0-5 Adr Op Fun Rt Rs Rd Imm16 add = rtype  func5  ~func4  ~func3  ~func2  ~func1  ~func0 sub = rtype  func5  ~func4  ~func3  ~func2  func1  ~func0 ADD ss ssst tttt dddd d SUB ss ssst tttt dddd d ORI ss ssst tttt iiii iiii iiii iiii LW ss ssst tttt iiii iiii iiii iiii SW ss ssst tttt iiii iiii iiii iiii BEQ ss ssst tttt iiii iiii iiii iiii JUMP ii iiii iiii iiii iiii iiii iiii How do we implement this in gates? Dr Dan Garcia

3 Boolean Exprs for Controller
RegDst = add + sub ALUSrc = ori + lw + sw MemtoReg = lw RegWrite = add + sub + ori + lw MemWrite = sw nPCsel = beq Jump = jump ExtOp = lw + sw ALUctr[0] = sub + beq ALUctr[1] = ori (assume ALUctr is 00 ADD, 01 SUB, 10 OR) ADD ss ssst tttt dddd d SUB ss ssst tttt dddd d ORI ss ssst tttt iiii iiii iiii iiii LW ss ssst tttt iiii iiii iiii iiii SW ss ssst tttt iiii iiii iiii iiii BEQ ss ssst tttt iiii iiii iiii iiii JUMP ii iiii iiii iiii iiii iiii iiii How do we implement this in gates? Dr Dan Garcia

4 Controller Implementation
opcode func RegDst add ALUSrc sub MemtoReg ori RegWrite “AND” logic “OR” logic lw MemWrite nPCsel sw Jump beq ExtOp jump ALUctr[0] ALUctr[1] Dr Dan Garcia

5 Call home, we’ve made HW/SW contact!
temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; High Level Language Program (e.g., C) Compiler lw $t0, 0($2) lw $t1, 4($2) sw $t1, 0($2) sw $t0, 4($2) Assembly Language Program (e.g.,MIPS) Assembler Machine Language Program (MIPS) Machine Interpretation Hardware Architecture Description (e.g., block diagrams) Architecture Implementation Logic Circuit Description (Circuit Schematic Diagrams) Dr Dan Garcia

6 Review: Single-cycle Processor
Five steps to design a processor: 1. Analyze instruction set  datapath requirements 2. Select set of datapath components & establish clock methodology 3. Assemble datapath meeting the requirements 4. Analyze implementation of each instruction to determine setting of control points that effects the register transfer. 5. Assemble the control logic Formulate Logic Equations Design Circuits Control Datapath Memory Processor Input Output Dr Dan Garcia

7 Agenda Pipelining Performance Structural Hazards Data Hazards
Forwarding Load Delay Slot Control Hazards Dr Dan Garcia

8 Morgan Kaufmann Publishers
11 September, 2018 Performance Issues Longest delay determines clock period Critical path: load instruction Instruction memory  register file  ALU  data memory  register file Not feasible to vary period for different instructions Violates design principle Making the common case fast We will improve performance by pipelining Chapter 4 — The Processor

9 Single Cycle Performance
Morgan Kaufmann Publishers 11 September, 2018 Single Cycle Performance Assume time for actions are 100ps for register read or write; 200ps for other events Clock rate is? Instr Instr fetch Register read ALU op Memory access Register write Total time lw 200ps 100 ps 800ps sw 700ps R-format 600ps beq 500ps 1.25 GHz What can we do to improve clock rate? Will this improve performance as well? Want increased clock rate to mean faster programs Dr Dan Garcia Chapter 4 — The Processor

10 Single Cycle Performance
Morgan Kaufmann Publishers 11 September, 2018 Single Cycle Performance Assume time for actions are 100ps for register read or write; 200ps for other events Clock rate is? Instr Instr fetch Register read ALU op Memory access Register write Total time lw 200ps 100 ps 800ps sw 700ps R-format 600ps beq 500ps 1.25 GHz What can we do to improve clock rate? Will this improve performance as well? Want increased clock rate to mean faster programs Dr Dan Garcia Chapter 4 — The Processor

11 Gotta Do Laundry Ann, Brian, Cathy, Dave each have one load of clothes to wash, dry, fold, and put away Washer takes 30 minutes Dryer takes 30 minutes “Folder” takes 30 minutes “Stasher” takes 30 minutes to put clothes into drawers A B C D Dr Dan Garcia

12 Sequential Laundry Sequential laundry takes 8 hours for 4 loads 30
Time 6 PM 7 8 9 10 11 12 1 2 AM T a s k O r d e B C D A Dr Dan Garcia

13 Pipelined Laundry Pipelined laundry takes 3.5 hours for 4 loads! 12
2 AM 6 PM 7 8 9 10 11 1 Time 30 T a s k O r d e B C D A Dr Dan Garcia

14 Pipelining Lessons (1/2)
6 PM 7 8 9 Time B C D A 30 T a s k O r d e Pipelining doesn’t help latency of single task, it helps throughput of entire workload Multiple tasks operating simultaneously using different resources Potential speedup = Number pipe stages Time to “fill” pipeline and time to “drain” it reduces speedup: 2.3X v. 4X in this example Dr Dan Garcia

15 Pipelining Lessons (2/2)
6 PM 7 8 9 Time B C D A 30 T a s k O r d e Suppose new Washer takes 20 minutes, new Stasher takes 20 minutes. How much faster is pipeline? Pipeline rate limited by slowest pipeline stage Unbalanced lengths of pipe stages reduces speedup Dr Dan Garcia

16 Steps in Executing MIPS
1) IFtch: Instruction Fetch, Increment PC 2) Dcd: Instruction Decode, Read Registers 3) Exec: Mem-ref: Calculate Address Arith-log: Perform Operation 4) Mem: Load: Read Data from Memory Store: Write Data to Memory 5) WB: Write Data Back to Register Dr Dan Garcia

17 Single Cycle Datapath instruction memory +4 rt rs rd registers ALU
PC instruction memory +4 rt rs rd registers ALU Data imm 2. Decode/ Register Read 1. Instruction Fetch 5. Write Back 3. Execute 4. Memory Dr Dan Garcia

18 Pipeline registers Need registers between stages
PC instruction memory +4 rt rs rd registers ALU Data imm 2. Decode/ Register Read 1. Instruction Fetch 5. Write Back 3. Execute 4. Memory Need registers between stages To hold information produced in previous cycle Dr Dan Garcia

19 More Detailed Pipeline
Morgan Kaufmann Publishers 11 September, 2018 More Detailed Pipeline Dr Dan Garcia Chapter 4 — The Processor

20 Morgan Kaufmann Publishers
11 September, 2018 IF for Load, Store, … Dr Dan Garcia Chapter 4 — The Processor

21 Morgan Kaufmann Publishers
11 September, 2018 ID for Load, Store, … Dr Dan Garcia Chapter 4 — The Processor

22 Morgan Kaufmann Publishers
11 September, 2018 EX for Load Dr Dan Garcia Chapter 4 — The Processor

23 Morgan Kaufmann Publishers
11 September, 2018 MEM for Load Dr Dan Garcia Chapter 4 — The Processor

24 Morgan Kaufmann Publishers
11 September, 2018 WB for Load – Oops! Wrong register number Chapter 4 — The Processor

25 Corrected Datapath for Load
Morgan Kaufmann Publishers 11 September, 2018 Corrected Datapath for Load Dr Dan Garcia Chapter 4 — The Processor

26 Pipelined Execution Representation
Time IF ID EX MEM WB Each instruction has identical latency! Every instruction must take same number of steps, so some stages will idle e.g. MEM stage for any arithmetic instruction Dr Dan Garcia

27 Graphical Pipeline Diagrams
1. Instruction Fetch 2. Decode/ Register Read 3. Execute 4. Memory 5. Write Back PC instruction memory +4 Register File rt rs rd ALU Data imm MUX Use datapath figure below to represent pipeline: IF ID EX Mem WB ALU I$ Reg D$ Dr Dan Garcia

28 Graphical Pipeline Representation
RegFile: left half is write, right half is read Time (clock cycles) I n s t r O d e I$ ALU Reg Reg I$ D$ ALU I$ Reg I$ ALU Reg D$ I$ Load Add Store Sub Or D$ Reg ALU Dr Dan Garcia

29 Pipelining Performance (1/3)
Morgan Kaufmann Publishers Pipelining Performance (1/3) Use Tc (“time between completion of instructions”) to measure speedup Equality only achieved if stages are balanced (i.e. take the same amount of time) If not balanced, speedup is reduced Speedup due to increased throughput Latency for each instruction does not decrease Dr Dan Garcia Chapter 4 — The Processor

30 Pipelining Performance (2/3)
Morgan Kaufmann Publishers Pipelining Performance (2/3) Assume time for stages is 100ps for register read or write 200ps for other stages What is pipelined clock rate? Compare pipelined datapath with single-cycle datapath Instr Instr fetch Register read ALU op Memory access Register write Total time lw 200ps 100 ps 800ps sw 700ps R-format 600ps beq 500ps Dr Dan Garcia Chapter 4 — The Processor

31 Pipelining Performance (3/3)
Morgan Kaufmann Publishers Pipelining Performance (3/3) Single-cycle Tc = 800 ps Here using Tc as “time between completion of instructions.” Pipelined Tc = 200 ps Dr Dan Garcia Chapter 4 — The Processor

32 Morgan Kaufmann Publishers
Pipelining Hazards A hazard is a situation that prevents starting the next instruction in the next clock cycle Structural hazard A required resource is busy (e.g. needed in multiple stages) Data hazard Data dependency between instructions Need to wait for previous instruction to complete its data read/write Control hazard Flow of execution depends on previous instruction Dr Dan Garcia Chapter 4 — The Processor

33 Agenda Pipelining Performance Structural Hazards Data Hazards
Forwarding Load Delay Slot Control Hazards Dr Dan Garcia

34 Morgan Kaufmann Publishers
1. Structural Hazards Conflict for use of a resource MIPS pipeline with a single memory? Load/Store requires memory access for data Instruction fetch would have to stall for that cycle Causes a pipeline “bubble” Hence, pipelined datapaths require separate instruction/data memories Separate L1 I$ and L1 D$ take care of this Dr Dan Garcia Chapter 4 — The Processor

35 Structural Hazard #1: Single Memory
Load Instr 1 Instr 2 Instr 3 Instr 4 ALU Reg D$ I n s t r O d e Time (clock cycles) Trying to read same memory twice in same clock cycle Dr Dan Garcia

36 Structural Hazard #2: Registers (1/2)
Load Instr 1 Instr 2 Instr 3 Instr 4 ALU Reg D$ I n s t r O d e Time (clock cycles) Can we read and write to registers simultaneously? Dr Dan Garcia

37 Structural Hazard #2: Registers (2/2)
Two different solutions have been used: Split RegFile access in two: Write during 1st half and Read during 2nd half of each clock cycle Possible because RegFile access is VERY fast (takes less than half the time of ALU stage) Build RegFile with independent read and write ports Conclusion: Read and Write to registers during same clock cycle is okay Dr Dan Garcia

38 Agenda Pipelining Performance Structural Hazards Data Hazards
Forwarding Load Delay Slot Control Hazards Dr Dan Garcia

39 2. Data Hazards (1/2) Consider the following sequence of instructions:
add $t0, $t1, $t2 sub $t4, $t0, $t3 and $t5, $t0, $t6 or $t7, $t0, $t8 xor $t9, $t0, $t10 Dr Dan Garcia

40 2. Data Hazards (2/2) Data-flow backwards in time are hazards
sub $t4,$t0,$t3 ALU I$ Reg D$ and $t5,$t0,$t6 or $t7,$t0,$t8 xor $t9,$t0,$t10 add $t0,$t1,$t2 IF ID/RF EX MEM WB I n s t r O d e Time (clock cycles) Dr Dan Garcia

41 Data Hazard Solution: Forwarding
Forward result as soon as it is available OK that it’s not stored in RegFile yet sub $t4,$t0,$t3 ALU I$ Reg D$ and $t5,$t0,$t6 or $t7,$t0,$t8 xor $t9,$t0,$t10 add $t0,$t1,$t2 IF ID/RF EX MEM WB Dr Dan Garcia

42 Datapath for Forwarding (1/2)
Morgan Kaufmann Publishers Datapath for Forwarding (1/2) What changes need to be made here? Dr Dan Garcia Chapter 4 — The Processor

43 Datapath for Forwarding (2/2)
Handled by forwarding unit Scan figure 4.54 on p. 368. Dr Dan Garcia

44 Data Hazard: Loads (1/4) Recall: Dataflow backwards in time are hazards Can’t solve all cases with forwarding Must stall instruction dependent on load, then forward (more hardware) sub $t3,$t0,$t2 ALU I$ Reg D$ lw $t0,0($t1) IF ID/RF EX MEM WB Dr Dan Garcia

45 Data Hazard: Loads (2/4) Hardware stalls pipeline lw $t0, 0($t1)
Schematically, this is what we want, but in reality stalls done “horizontally” Hardware stalls pipeline Called “hardware interlock” sub $t3,$t0,$t2 ALU I$ Reg D$ bubble and $t5,$t0,$t4 or $t7,$t0,$t6 lw $t0, 0($t1) IF ID/RF EX MEM WB How to stall just part of pipeline? Dr Dan Garcia

46 Data Hazard: Loads (3/4) Stall is equivalent to nop lw $t0, 0($t1) nop
sub $t3,$t0,$t2 and $t5,$t0,$t4 or $t7,$t0,$t6 I$ ALU Reg D$ lw $t0, 0($t1) bubble nop Dr Dan Garcia

47 Data Hazard: Loads (4/4) Slot after a load is called a load delay slot
If that instruction uses the result of the load, then the hardware interlock will stall it for one cycle Letting the hardware stall the instruction in the delay slot is equivalent to putting a nop in the slot (except the latter uses more code space) Idea: Let the compiler put an unrelated instruction in that slot  no stall! Dr Dan Garcia

48 Code Scheduling to Avoid Stalls
Morgan Kaufmann Publishers Code Scheduling to Avoid Stalls Reorder code to avoid use of load result in the next instruction! MIPS code for D=A+B; E=A+C; # Method 1: lw $t1, 0($t0) lw $t2, 4($t0) add $t3, $t1, $t2 sw $t3, 12($t0) lw $t4, 8($t0) add $t5, $t1, $t4 sw $t5, 16($t0) # Method 2: lw $t1, 0($t0) lw $t2, 4($t0) lw $t4, 8($t0) add $t3, $t1, $t2 sw $t3, 12($t0) add $t5, $t1, $t4 sw $t5, 16($t0) 11 cycles Stall! Stall! 13 cycles Dr Dan Garcia Chapter 4 — The Processor

49 Agenda More Pipelining Structural Hazards Data Hazards Control Hazards
Forwarding Load Delay Slot Control Hazards Dr Dan Garcia

50 Morgan Kaufmann Publishers
3. Control Hazards Branch (beq, bne) determines flow of control Fetching next instruction depends on branch outcome Pipeline can’t always fetch correct instruction Still working on ID stage of branch Simple Solution: Stall on every branch until we have the new PC value How long must we stall? Dr Dan Garcia Chapter 4 — The Processor

51 Branch Stall When is comparison result available? Time (clock cycles)
beq Instr 1 Instr 2 Instr 3 Instr 4 ALU Reg D$ I n s t r O d e Time (clock cycles) TWO bubbles required per branch! Dr Dan Garcia

52 Summary Hazards reduce effectiveness of pipelining Structural Hazards
Cause stalls/bubbles Structural Hazards Conflict in use of datapath component Data Hazards Need to wait for result of a previous instruction Control Hazards Address of next instruction uncertain/unknown Dr Dan Garcia

53 No stalls with forwarding B)
Question: For each code sequences below, choose one of the statements below: 1: lw $t0,0($t0) add $t1,$t0,$t0 2: addi $t2,$t0,5 addi $t4,$t1,5 3: addi $t1,$t0,1 addi $t2,$t0,2 addi $t3,$t0,2 addi $t3,$t0,4 addi $t5,$t1,5 No stalls as is A) No stalls with forwarding B) Must stall C) Dr Dan Garcia

54 Code Sequence 1 Time (clock cycles) I n s Must stall lw t r O add d e
instr ALU Reg D$ I n s t r O d e Time (clock cycles) Must stall Dr Dan Garcia

55 No stalls with forwarding B)
Question: For each code sequences below, choose one of the statements below: 1: lw $t0,0($t0) add $t1,$t0,$t0 2: addi $t2,$t0,5 addi $t4,$t1,5 3: addi $t1,$t0,1 addi $t2,$t0,2 addi $t3,$t0,2 addi $t3,$t0,4 addi $t5,$t1,5 No stalls as is A) No stalls with forwarding B) Must stall C) Dr Dan Garcia

56 Code Sequence 2 Time (clock cycles) I n s add t
addi instr ALU Reg D$ I n s t r O d e Time (clock cycles) forwarding no forwarding No stalls with forwarding Dr Dan Garcia

57 No stalls with forwarding B)
Question: For each code sequences below, choose one of the statements below: 1: lw $t0,0($t0) add $t1,$t0,$t0 2: addi $t2,$t0,5 addi $t4,$t1,5 3: addi $t1,$t0,1 addi $t2,$t0,2 addi $t3,$t0,2 addi $t3,$t0,4 addi $t5,$t1,5 No stalls as is A) No stalls with forwarding B) Must stall C) Dr Dan Garcia

58 Code Sequence 3 Time (clock cycles) I n No stalls as is s addi t r O d
ALU Reg D$ I n s t r O d e Time (clock cycles) No stalls as is Dr Dan Garcia


Download ppt "Pipelining concepts, datapath and hazards"

Similar presentations


Ads by Google