1 / 38

Scott Beamer, Instructor

inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture #22 CPU Design: Pipelining to Improve Performance II 2007-8-1. Scott Beamer, Instructor. Review: Processor Pipelining (1/2).

fstuart
Download Presentation

Scott Beamer, Instructor

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. inst.eecs.berkeley.edu/~cs61cCS61C : Machine StructuresLecture #22 CPU Design: Pipelining to Improve Performance II2007-8-1 Scott Beamer, Instructor

  2. Review: Processor Pipelining (1/2) • “Pipeline registers” are added to the datapath/controller to neatly divide the single cycle processor into “pipeline stages”. • Optimal Pipeline • Each stage is executing part of an instruction each clock cycle. • One inst. finishes during each clock cycle. • On average, execute far more quickly. • What makes this work well? • Similarities between instructions allow us to use same stages for all instructions (generally). • Each stage takes about the same amount of time as all others: little wasted time.

  3. Review: Pipeline (2/2) • Pipelining is a BIG IDEA • widely used concept • What makes it less than perfect? • Structural hazards: Conflicts for resources. Suppose we had only one cache?  Need more HW resources • Control hazards: Branch instructions effect which instructions come next.  Delayed branch • Data hazards: Data flow between instructions. Forwarding

  4. Time (clock cycles) I n s t r. O r d e r I$ Reg ALU Load D$ Reg I$ Add ALU D$ Reg Reg I$ Reg ALU Store D$ Reg D$ Reg Sub ALU I$ Reg I$ D$ Reg ALU Or Reg Graphical Pipeline Representation (In Reg, right half highlight read, left half write)

  5. Time (clock cycles) I n s t r. O r d e r I$ D$ Reg Reg ALU beq I$ D$ Reg Reg Instr 1 ALU I$ D$ Reg Reg ALU Instr 2 I$ D$ Reg Reg Instr 3 ALU I$ D$ Reg Reg ALU Instr 4 Control Hazard: Branching (1/8) Where do we do the compare for the branch?

  6. Control Hazard: Branching (2/8) • We had put branch decision-making hardware in ALU stage • therefore two more instructions after the branch will always be fetched, whether or not the branch is taken • Desired functionality of a branch • if we do not take the branch, don’t waste any time and continue executing normally • if we take the branch, don’t execute any instructions after the branch, just go to the desired label

  7. Control Hazard: Branching (3/8) • Initial Solution: Stall until decision is made • insert “no-op” instructions (those that accomplish nothing, just take time) or hold up the fetch of the next instruction (for 2 cycles). • Drawback: branches take 3 clock cycles each (assuming comparator is put in ALU stage)

  8. Control Hazard: Branching (4/8) • Optimization #1: • insert special branch comparator in Stage 2 • as soon as instruction is decoded (Opcode identifies it as a branch), immediately make a decision and set the new value of the PC • Benefit: since branch is complete in Stage 2, only one unnecessary instruction is fetched, so only one no-op is needed • Side Note: This means that branches are idle in Stages 3, 4 and 5.

  9. Time (clock cycles) I n s t r. O r d e r I$ D$ Reg Reg ALU beq I$ D$ Reg Reg Instr 1 ALU I$ D$ Reg Reg ALU Instr 2 I$ D$ Reg Reg Instr 3 ALU I$ D$ Reg Reg ALU Instr 4 Control Hazard: Branching (5/8) Branch comparator moved to Decode stage.

  10. I$ ALU I$ ALU bubble bubble bubble bubble bubble ALU Control Hazard: Branching (6a/8) • User inserting no-op instruction I n s t r. O r d e r Time (clock cycles) D$ Reg Reg add D$ Reg Reg beq nop lw D$ Reg Reg I$ • Impact: 2 clock cycles per branch instruction  slow

  11. I$ ALU I$ ALU bubble ALU Control Hazard: Branching (6b/8) • Controller inserting a single bubble I n s t r. O r d e r Time (clock cycles) D$ Reg Reg add D$ Reg Reg beq lw D$ Reg Reg I$ • Impact: 2 clock cycles per branch instruction  slow

  12. Control Hazard: Branching (7/8) • Optimization #2: Redefine branches • Old definition: if we take the branch, none of the instructions after the branch get executed by accident • New definition: whether or not we take the branch, the single instruction immediately following the branch gets executed (called the branch-delay slot) • The term “Delayed Branch” meanswe always execute inst after branch • This optimization is used on the MIPS

  13. Control Hazard: Branching (8/8) • Notes on Branch-Delay Slot • Worst-Case Scenario: can always put a no-op in the branch-delay slot • Better Case: can find an instruction preceding the branch which can be placed in the branch-delay slot without affecting flow of the program • re-ordering instructions is a common method of speeding up programs • compiler must be very smart in order to find instructions to do this • usually can find such an instruction at least 50% of the time • Jumps also have a delay slot…

  14. or $8, $9 ,$10 add $1 ,$2,$3 sub $4, $5,$6 add $1 ,$2,$3 beq $1, $4, Exit sub $4, $5,$6 beq $1, $4, Exit or $8, $9 ,$10 xor $10, $1,$11 xor $10, $1,$11 Exit: Exit: Example: Nondelayed vs. Delayed Branch Nondelayed Branch Delayed Branch

  15. bubble Out-of-Order Laundry: Don’t Wait A depends on D; rest continue; need more resources to allow out-of-order 2 AM 12 6 PM 1 8 7 11 10 9 Time 30 30 30 30 30 30 30 T a s k O r d e r A B C D E F

  16. 30 30 30 30 30 (light clothing) A (dark clothing) B (very dirty clothing) C (light clothing) (dark clothing) (very dirty clothing) Superscalar Laundry: Parallel per stage More resources, HW to match mix of parallel tasks? 2 AM 12 6 PM 1 8 7 11 10 9 Time T a s k O r d e r D E F

  17. (light clothing) B A D C Superscalar Laundry: Mismatch Mix 2 AM 12 6 PM 1 8 7 11 10 9 Task mix underutilizes extra resources Time 30 30 30 30 30 30 30 T a s k O r d e r (light clothing) (dark clothing) (light clothing)

  18. Problem: need torun 2 hr test before done..help! Real-world pipelining problem • You’re the manager of a HUGE assembly plant to build computers. Box • Main pipeline • 10 minutes/pipeline stage • 60 stages • Latency: 10hr

  19. Problem: need torun 2 hr test before done..help! Real-world pipelining problem solution 1 • You remember: “a pipeline frequency is limited by its slowest stage”, so… Box • Main pipeline • 10 minutes/pipeline stage • 60 stages • Latency: 10hr 2hours/ 120hr

  20. Real-world pipelining problem solution 2 • Create a sub-pipeline! Box • Main pipeline • 10 minutes/pipeline stage • 60 stages 2hr test(12 CPUsin this pipeline)

  21. Peer Instruction (1/2) Assume 1 instr/clock, delayed branch, 5 stage pipeline, forwarding, interlock on unresolved load hazards (after 103 loops, so pipeline full) Loop: lw $t0, 0($s1) addu $t0, $t0, $s2 sw $t0, 0($s1) addiu $s1, $s1, -4 bne $s1, $zero, Loop nop • How many pipeline stages (clock cycles) per loop iteration to execute this code? 1 2 3 4 5 6 7 8 9 10

  22. 8. (delayed branch so exec. nop) Peer Instruction Answer (1/2) • Assume 1 instr/clock, delayed branch, 5 stage pipeline, forwarding, interlock on unresolved load hazards. 103 iterations, so pipeline full. Loop: lw $t0, 0($s1) addu $t0, $t0, $s2 sw $t0, 0($s1) addiu $s1, $s1, -4 bne $s1, $zero, Loop nop • How many pipeline stages (clock cycles) per loop iteration to execute this code? 2. (data hazard so stall) 1. 3. 4. 6. (!= in DCD) 5. 7. 1 2 3 4 5 6 7 8 9 10

  23. Peer Instruction (2/2) Assume 1 instr/clock, delayed branch, 5 stage pipeline, forwarding, interlock on unresolved load hazards (after 103 loops, so pipeline full). Rewrite this code to reduce pipeline stages (clock cycles) per loop to as few as possible. Loop: lw $t0, 0($s1) addu $t0, $t0, $s2 sw $t0, 0($s1) addiu $s1, $s1, -4 bne $s1, $zero, Loop nop • How many pipeline stages (clock cycles) per loop iteration to execute this code? 1 2 3 4 5 6 7 8 9 10

  24. (modified sw to put past addiu) Peer Instruction (2/2) How long to execute? • Rewrite this code to reduce clock cycles per loop to as few as possible: Loop: lw $t0, 0($s1) addiu $s1, $s1, -4 addu $t0, $t0, $s2 bne $s1, $zero, Loop sw $t0, +4($s1) • How many pipeline stages (clock cycles) per loop iteration to execute your revised code? (assume pipeline is full) (no hazard since extra cycle) 1. 2. 3. 4. 5. 1 2 3 4 5 6 7 8 9 10

  25. “And in Early Conclusion..” • Pipeline challenge is hazards • Forwarding helps w/many data hazards • Delayed branch helps with control hazard in 5 stage pipeline • Load delay slot / interlock necessary • More aggressive performance: • Superscalar • Out-of-order execution

  26. Administrivia • Assignments • HW7 due 8/2 • Proj3 due 8/5 • Midterm Regrades due Today • Logisim in lab is now 2.1.6 • java -jar ~cs61c/bin/logisim • Valerie’s OH on Thursday moved to 10-11 for this week

  27. Why Doesn’t It Work? • DO NOT MESS WITH THE CLOCK • Crafty veterans may do it very rarely and carefully • Doing so will cause unpredictable and hard to track errors • Following slides are from CS 150 Lecture by Prof. Katz

  28. Q0 Q1 D D Q Q IN OUT CLK Cascading Edge-triggered Flip-Flops • Shift register • New value goes into first stage • While previous value of first stage goes into second stage • Consider setup/hold/propagation delays (prop must be > hold) 100 IN Q0 Q1 CLK

  29. Q0 Q1 D D Q Q IN OUT CLK Clk1 Cascading Edge-triggered Flip-Flops • Shift register • New value goes into first stage • While previous value of first stage goes into second stage • Consider setup/hold/propagation delays (prop must be > hold) Clk1 Delay 100 IN Q0 Q1 CLK

  30. Clock Skew • The problem • Correct behavior assumes next state of all storage elements determined by all storage elements at the same time • Difficult in high-performance systems because time for clock to arrive at flip-flop is comparable to delays through logic (and will soon become greater than logic delay) • Effect of skew on cascaded flip-flops: 100 In Q0 Q1 CLK CLK1 CLK1 is a delayed version of CLK original state: IN = 0, Q0 = 1, Q1 = 1 due to skew, next state becomes: Q0 = 0, Q1 = 0, and not Q0 = 0, Q1 = 1

  31. LD Reg Reg Clk Clk LD Why Gating of Clocks is Bad! gatedClK BAD GOOD Do NOT Mess With Clock Signals!

  32. Clk LD generated by FSM shortly after rising edge of CLK LD gatedClk Runt pulse plays HAVOC with register internals! NASTY HACK: delay LD throughnegative edge triggered FF toensure that it won’t change duringnext positive edge event Clk LDn gatedClk Clk skew PLUS LD delayed by half clock cycle …What is the effect on your register transfers? Why Gating of Clocks is Bad! Do NOT Mess With Clock Signals!

  33. Keyboard, Mouse Computer Processor (active) Devices Memory (passive) (where programs, data live when running) Input Disk, Network Control (“brain”) Output Datapath (“brawn”) Display, Printer The Big Picture

  34. Memory Hierarchy Storage in computer systems: • Processor • holds data in register file (~100 Bytes) • Registers accessed on nanosecond timescale • Memory (we’ll call “main memory”) • More capacity than registers (~Gbytes) • Access time ~50-100 ns • Hundreds of clock cycles per memory access?! • Disk • HUGE capacity (virtually limitless) • VERY slow: runs ~milliseconds

  35. Motivation: Why We Use Caches (written $) µProc 60%/yr. 1000 CPU • 1989 first Intel CPU with cache on chip • 1998 Pentium III has two levels of cache on chip 100 Processor-Memory Performance Gap:(grows 50% / year) Performance 10 DRAM 7%/yr. DRAM 1 1989 1990 1980 1981 1982 1983 1984 1985 1986 1987 1988 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000

  36. Memory Caching • Mismatch between processor and memory speeds leads us to add a new level: a memory cache • Implemented with same IC processing technology as the CPU (usually integrated on same chip): faster but more expensive than DRAM memory. • Cache is a copy of a subset of main memory. • Most processors have separate caches for instructions and data.

  37. Processor Increasing Distance from Proc.,Decreasing speed Levels in memory hierarchy Level 1 Level 2 Level 3 . . . Level n Size of memory at each level Memory Hierarchy Higher Lower As we move to deeper levels the latency goes up and price per bit goes down.

  38. Memory Hierarchy • If level closer to Processor, it is: • smaller • faster • subset of lower levels (contains most recently used data) • Lowest Level (usually disk) contains all available data (or does it go beyond the disk?) • Memory Hierarchy presents the processor with the illusion of a very large very fast memory.

More Related