CS152 / Kubiatowicz Lec12.1 2/27/01©UCB Spring 2001 CS152 Computer Architecture and Engineering Lecture 12 Introduction to Pipelining February 27, 2001.

Slides:



Advertisements
Similar presentations
OMSE 510: Computing Foundations 4: The CPU!
Advertisements

1 IKI20210 Pengantar Organisasi Komputer Kuliah no. 25: Pipeline 10 Januari 2003 Bobby Nazief Johny Moningka
Chapter 8. Pipelining.
Review: Pipelining. Pipelining Laundry Example Ann, Brian, Cathy, Dave each have one load of clothes to wash, dry, and fold Washer takes 30 minutes Dryer.
Pipelining I (1) Fall 2005 Lecture 18: Pipelining I.
Pipelining Hwanmo Sung CS147 Presentation Professor Sin-Min Lee.
Computer Architecture
CS252/Patterson Lec 1.1 1/17/01 Pipelining: Its Natural! Laundry Example Ann, Brian, Cathy, Dave each have one load of clothes to wash, dry, and fold Washer.
Chapter Six 1.
Mary Jane Irwin ( ) [Adapted from Computer Organization and Design,
ECE 232 L15.Microprog.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 15 Microprogrammed.
ENEE350 Ankur Srivastava University of Maryland, College Park Based on Slides from Mary Jane Irwin ( )
Computer ArchitectureFall 2007 © October 24nd, 2007 Majd F. Sakr CS-447– Computer Architecture.
ECE 232 L19.Pipeline2.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 19 Pipelining,
Pipeline Exceptions & ControlCSCE430/830 Pipeline: Exceptions & Control CSCE430/830 Computer Architecture Lecturer: Prof. Hong Jiang Courtesy of Yifeng.
Computer ArchitectureFall 2007 © October 22nd, 2007 Majd F. Sakr CS-447– Computer Architecture.
1 CSE SUNY New Paltz Chapter Six Enhancing Performance with Pipelining.
Pipelining Datapath Adapted from the lecture notes of Dr. John Kubiatowicz (UC Berkeley) and Hank Walker (TAMU)
1 Atanasoff–Berry Computer, built by Professor John Vincent Atanasoff and grad student Clifford Berry in the basement of the physics building at Iowa State.
CS152 / Kubiatowicz Lec13.1 3/17/03©UCB Spring 2003 CS152 Computer Architecture and Engineering Lecture 13 Introduction to Pipelining: Datapath and Control.
Pipelining - II Adapted from CS 152C (UC Berkeley) lectures notes of Spring 2002.
Computer ArchitectureFall 2008 © October 6th, 2008 Majd F. Sakr CS-447– Computer Architecture.
Ceg3420 L13.1 DAP Fa97,  U.CB CEG3420 Computer Design Introduction to Pipelining.
EECC550 - Shaaban #1 Lec # 6 Winter Control may be designed using one of several initial representations. The choice of sequence control,
CS152 / Kubiatowicz Lec /17/01©UCB Fall 2001 CS152 Computer Architecture and Engineering Lecture 13 Introduction to Pipelining: Datapath and Control.
Prof. John Nestor ECE Department Lafayette College Easton, Pennsylvania ECE Computer Organization Lecture 17 - Pipelined.
Pipelining - II Rabi Mahapatra Adapted from CS 152C (UC Berkeley) lectures notes of Spring 2002.
ECE 232 L23.Exceptions.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 23 Exceptions.
Introduction to Pipelining Rabi Mahapatra Adapted from the lecture notes of Dr. John Kubiatowicz (UC Berkeley)
Lecture 12: Pipeline Datapath Design Professor Mike Schulte Computer Architecture ECE 201.
CS1104: Computer Organisation School of Computing National University of Singapore.
1 Pipelining Reconsider the data path we just did Each instruction takes from 3 to 5 clock cycles However, there are parts of hardware that are idle many.
B 0000 Pipelining ENGR xD52 Eric VanWyk Fall
Analogy: Gotta Do Laundry
CSE 340 Computer Architecture Summer 2014 Basic MIPS Pipelining Review.
CS.305 Computer Architecture Enhancing Performance with Pipelining Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005, and from.
Computer Organization CS224 Chapter 4 Part b The Processor Spring 2010 With thanks to M.J. Irwin, T. Fountain, D. Patterson, and J. Hennessy for some lecture.
1 Designing a Pipelined Processor In this Chapter, we will study 1. Pipelined datapath 2. Pipelined control 3. Data Hazards 4. Forwarding 5. Branch Hazards.
CS 152 Lec 12.1 CS 152: Computer Architecture and Engineering Lecture 12 Multicycle Controller Design Pipelining Randy H. Katz, Instructor Satrajit Chatterjee,
ECE 232 L18.Pipeline.1 Adapted from Patterson 97 ©UCBCopyright 1998 Morgan Kaufmann Publishers ECE 232 Hardware Organization and Design Lecture 18 Pipelining.
CECS 440 Pipelining.1(c) 2014 – R. W. Allison [slides adapted from D. Patterson slides with additional credits to M.J. Irwin]
EECS 322 March 27, 2000 Based on Dave Patterson slides Instructor: Francis G. Wolff Case Western Reserve University This presentation.

Cs 152 L1 3.1 DAP Fa97,  U.CB Pipelining Lessons °Pipelining doesn’t help latency of single task, it helps throughput of entire workload °Multiple tasks.
Chap 6.1 Computer Architecture Chapter 6 Enhancing Performance with Pipelining.
CSIE30300 Computer Architecture Unit 04: Basic MIPS Pipelining Hsin-Chou Chi [Adapted from material by and
CS152 Lec12.1 CS 152 Computer Architecture and Engineering Lecture 12 Multicycle Controller Design Exceptions.
CMSC 611: Advanced Computer Architecture Pipelining Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted.
Pipelining CS365 Lecture 9. D. Barbara Pipeline CS465 2 Outline  Today’s topic  Pipelining is an implementation technique in which multiple instructions.
CS252/Patterson Lec 1.1 1/17/01 معماري کامپيوتر - درس نهم pipeline برگرفته از درس : Prof. David A. Patterson.
CSE431 L06 Basic MIPS Pipelining.1Irwin, PSU, 2005 MIPS Pipeline Datapath Modifications  What do we need to add/modify in our MIPS datapath? l State registers.
Lecture 9. MIPS Processor Design – Pipelined Processor Design #1 Prof. Taeweon Suh Computer Science Education Korea University 2010 R&E Computer System.
CMSC 611: Advanced Computer Architecture Pipelining Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Advanced Computer Architecture CS 704 Advanced Computer Architecture Lecture 10 Computer Hardware Design (Pipeline Datapath and Control Design) Prof. Dr.
Lecture 18: Pipelining I.
CS 152: Computer Architecture and Engineering Lecture 11 Multicycle Controller Design Exceptions Randy H. Katz, Instructor Satrajit Chatterjee, Teaching.
CMSC 611: Advanced Computer Architecture
ECE232: Hardware Organization and Design
Pipelining Lessons 6 PM T a s k O r d e B C D A 30
Dave Patterson (http.cs.berkeley.edu/~patterson)
John Kubiatowicz (http.cs.berkeley.edu/~kubitron)
Chapter 4 The Processor Part 2
John Kubiatowicz (http.cs.berkeley.edu/~kubitron)
John Kubiatowicz (http.cs.berkeley.edu/~kubitron)
Pipelining Lessons 6 PM T a s k O r d e B C D A 30
An Introduction to pipelining
Pipelining Appendix A and Chapter 3.
Recall: Performance Evaluation
Presentation transcript:

CS152 / Kubiatowicz Lec12.1 2/27/01©UCB Spring 2001 CS152 Computer Architecture and Engineering Lecture 12 Introduction to Pipelining February 27, 2001 John Kubiatowicz (http.cs.berkeley.edu/~kubitron) lecture slides:

CS152 / Kubiatowicz Lec12.2 2/27/01©UCB Spring 2001 Recap: Microprogramming °Microprogramming is a convenient method for implementing structured control state diagrams: Random logic replaced by microPC sequencer and ROM Each line of ROM called a  instruction: contains sequencer control + values for control points limited state transitions: branch to zero, next sequential, branch to  instruction address from displatch ROM °Horizontal  Code: one control bit in  Instruction for every control line in datapath °Vertical  Code: groups of control-lines coded together in  Instruction (e.g. possible ALU dest) °Control design reduces to Microprogramming Part of the design process is to develop a “language” that describes control and is easy for humans to understand

CS152 / Kubiatowicz Lec12.3 2/27/01©UCB Spring 2001 Recap: Microprogramming °Microprogramming is a fundamental concept implement an instruction set by building a very simple processor and interpreting the instructions essential for very complex instructions and when few register transfers are possible overkill when ISA matches datapath 1-1 sequencer control datapath control micro-PC  -sequencer: fetch,dispatch, sequential microinstruction (  ) Dispatch ROM Opcode  -Code ROM Decode To DataPath Decoders implement our  -code language: For instance: rt-ALU rd-ALU mem-ALU

CS152 / Kubiatowicz Lec12.4 2/27/01©UCB Spring 2001 Recap: Exceptions °Exception = unprogrammed control transfer system takes action to handle the exception -must record the address of the offending instruction -record any other information necessary to return afterwards returns control to user must save & restore user state °Allows constuction of a “user virtual machine” normal control flow: sequential, jumps, branches, calls, returns user program System Exception Handler Exception: return from exception

CS152 / Kubiatowicz Lec12.5 2/27/01©UCB Spring 2001 Recap: Two Types of Exceptions: Interrupts and Traps °Interrupts caused by external events: -Network, Keyboard, Disk I/O, Timer asynchronous to program execution -Most interrupts can be disabled for brief periods of time -Some (like “Power Failing”) are non-maskable (NMI) may be handled between instructions simply suspend and resume user program °Traps caused by internal events -exceptional conditions (overflow) -errors (parity) -faults (non-resident page) synchronous to program execution condition must be remedied by the handler instruction may be retried or simulated and program continued or program may be aborted

CS152 / Kubiatowicz Lec12.6 2/27/01©UCB Spring 2001 Recap: Precise Interrupts °Precise  state of the machine is preserved as if program executed up to the offending instruction All previous instructions completed Offending instruction and all following instructions act as if they have not even started Same system code will work on different implementations Position clearly established by IBM Difficult in the presence of pipelining, out-ot-order execution,... MIPS takes this position °Imprecise  system software has to figure out what is where and put it all back together °Performance goals often lead designers to forsake precise interrupts system software developers, user, markets etc. usually wish they had not done this °Modern techniques for out-of-order execution and branch prediction help implement precise interrupts

CS152 / Kubiatowicz Lec12.7 2/27/01©UCB Spring 2001 Addressing the Exception Handler °Traditional Approach: Interupt Vector PC <- MEM[ IV_base + cause || 00] 370, 68000, Vax, 80x86,... °RISC Handler Table PC <– IT_base + cause || 0000 saves state and jumps Sparc, PA, M88K,... °MIPS Approach: fixed entry PC <– EXC_addr Actually very small table -RESET entry -TLB -other iv_base cause handler code iv_base cause handler entry code

CS152 / Kubiatowicz Lec12.8 2/27/01©UCB Spring 2001 Saving State °Push it onto the stack Vax, 68k, 80x86 °Save it in special registers MIPS EPC, BadVaddr, Status, Cause °Shadow Registers M88k Save state in a shadow of the internal pipeline registers

CS152 / Kubiatowicz Lec12.9 2/27/01©UCB Spring 2001 Additions to MIPS ISA to support Exceptions? °Exception state is kept in “coprocessor 0”. Use mfc0 read contents of these registers Every register is 32 bits, but may be only partially defined BadVAddr (register 8) register contained memory address at which memory reference occurred Status (register 12) interrupt mask and enable bits Cause (register 13) the cause of the exception Bits 5 to 2 of this register encodes the exception type (e.g undefined instruction=10 and arithmetic overflow=12) EPC (register 14) address of the affected instruction (register 14 of coprocessor 0). °Control signals to write BadVAddr, Status, Cause, and EPC °Mux to write exception address into PC ( hex ) °May have to undo PC = PC + 4, since want EPC to point to offending instruction (not its successor): PC = PC - 4

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Recap: Details of Status register °Mask = 1 bit for each of 5 hardware and 3 software interrupt levels 1 => enables interrupts 0 => disables interrupts °k = kernel/user 0 => was in the kernel when interrupt occurred 1 => was running user mode °e = interrupt enable 0 => interrupts were disabled 1 => interrupts were enabled °When interrupt occurs, 6 LSB shifted left 2 bits, setting 2 LSB to 0 run in kernel mode with interrupts disabled Status kekeke Mask oldprev current

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Recap: Details of Cause register °Pending interrupt 5 hardware levels: bit set if interrupt occurs but not yet serviced handles cases when more than one interrupt occurs at same time, or while records interrupt requests when interrupts disabled °Exception Code encodes reasons for interrupt 0 (INT) => external interrupt 4 (ADDRL) => address error exception (load or instr fetch) 5 (ADDRS) => address error exception (store) 6 (IBUS) => bus error on instruction fetch 7 (DBUS) => bus error on data fetch 8 (Syscall) => Syscall exception 9 (BKPT) => Breakpoint exception 10 (RI) => Reserved Instruction exception 12 (OVF) => Arithmetic overflow exception Status 1510 Pending 52 Code

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Example: How Control Handles Traps in our FSD °Undefined Instruction–detected when no next state is defined from state 1 for the op value. We handle this exception by defining the next state value for all op values other than lw, sw, 0 (R-type), jmp, beq, and ori as new state 12. Shown symbolically using “other” to indicate that the op field does not match any of the opcodes that label arcs out of state 1. °Arithmetic overflow–detected on ALU ops such as signed add Used to save PC and enter exception handler °External Interrupt – flagged by asserted interrupt line Again, must save PC and enter exception handler °Note: Challenge in real machine is to handle: Interactions between instructions and other exception-causing events such that control logic remains small and fast. Complex interactions makes the control unit the most challenging aspect of hardware design

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 How add traps and interrupts to state diagram? IR <= MEM[PC] PC <= PC + 4 R-type S <= A fun B R[rd] <= S S <= A op ZX R[rt] <= S ORi S <= A + SX R[rt] <= M M <= MEM[S] LW S <= A + SX MEM[S] <= B SW “instruction fetch” “decode” BEQ 0010 If A = B then PC <= S S<= PC +SX undefined instruction EPC <= PC - 4 PC <= exp_addr cause <= 10 (RI) other S <= A - B overflow EPC <= PC - 4 PC <= exp_addr cause <= 12 (Ovf) EPC <= PC - 4 PC <= exp_addr cause <= 0(INT) Handle Interrupt Pending INT

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 But: What has to change in our  -sequencer? °Need concept of branch at micro-code level R-type S <= A fun B 0100 overflow EPC <= PC - 4 PC <= exp_addr cause <= 12 (Ovf) µAddress Select Logic Opcode microPC 1 Adder Dispatch ROM Mux overflow pending interrupt 4? N? Cond Select Do  -branch  -offset Seq Select

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Example: Can easily use with for non-ideal memory IR <= MEM[PC] R-type A <= R[rs] B <= R[rt] S <= A fun B R[rd] <= S PC <= PC + 4 S <= A or ZX R[rt] <= S PC <= PC + 4 ORi S <= A + SX R[rt] <= M PC <= PC + 4 M <= MEM[S] LW S <= A + SX MEM[S] <= B BEQ PC <= Next(PC) SW “instruction fetch” “decode / operand fetch” Execute Memory Write-back ~wait wait ~waitwait PC <= PC + 4 ~wait wait

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Administrative Issues °Midterm I: Thursday 3/1 (day after tomorrow) 5:30 – 8:30 in 277 Cory Closed book, but can have one 8 ½  11 sheet of handwritten notes Covers: Chapters 1 – 5, Appendices A – C Make sure to check out the sample quizzes on the Web! °Get started reading Chapter 6! Complete chapter on Pipelining... °Sections: This week sections  433 Latimer (as usual) Next week sections  Cory You will demonstrate your processors on a mystery program -Report still due at midnight °Lab Reports: Up to you to do a good job of summarizing your work Part of grade will be on quality of your writing -Put code and schematics in appendices appropriately referenced -Use actual wordprocessor (Microsoft Word online)

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Question #1: Why do microcoding? °If simple instruction could execute at very high clock rate… °If you could even write compilers to produce microinstructions… °If most programs use simple instructions and addressing modes… °If microcode is kept in RAM instead of ROM so as to fix bugs … °If same memory used for control memory could be used instead as cache for “macroinstructions”… °Then why not skip instruction interpretation by a microprogram and simply compile directly into lowest language of machine? (microprogramming is overkill when ISA matches datapath 1-1)

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Recall: Performance Evaluation °What is the average CPI? state diagram gives CPI for each instruction type workload gives frequency of each type TypeCPI i for typeFrequency CPI i x freqI i Arith/Logic440%1.6 Load530%1.5 Store410%0.4 branch320%0.6 Average CPI:4.1

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Question #2: Can we get CPI < 4.1? °Seems to be lots of “idle” hardware Why not overlap instructions???

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 The Big Picture: Where are We Now? °The Five Classic Components of a Computer °Next Topics: Pipelining by Analogy Pipeline hazards Control Datapath Memory Processor Input Output

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Pipelining is Natural! °Laundry Example °Ann, Brian, Cathy, Dave each have one load of clothes to wash, dry, and fold °Washer takes 30 minutes °Dryer takes 40 minutes °“Folder” takes 20 minutes ABCD

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Sequential Laundry °Sequential laundry takes 6 hours for 4 loads °If they learned pipelining, how long would laundry take? ABCD PM Midnight TaskOrderTaskOrder Time

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Pipelined Laundry: Start work ASAP °Pipelined laundry takes 3.5 hours for 4 loads ABCD 6 PM Midnight TaskOrderTaskOrder Time

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Pipelining Lessons °Pipelining doesn’t help latency of single task, it helps throughput of entire workload °Pipeline rate limited by slowest pipeline stage °Multiple tasks operating simultaneously using different resources °Potential speedup = Number pipe stages °Unbalanced lengths of pipe stages reduces speedup °Time to “fill” pipeline and time to “drain” it reduces speedup °Stall for Dependences ABCD 6 PM 789 TaskOrderTaskOrder Time

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 The Five Stages of Load °Ifetch: Instruction Fetch Fetch the instruction from the Instruction Memory °Reg/Dec: Registers Fetch and Instruction Decode °Exec: Calculate the memory address °Mem: Read the data from the Data Memory °Wr: Write the data back to the register file Cycle 1Cycle 2Cycle 3Cycle 4Cycle 5 IfetchReg/DecExecMemWrLoad

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Note: These 5 stages were there all along! IR <= MEM[PC] PC <= PC + 4 R-type ALUout <= A fun B R[rd] <= ALUout ALUout <= A op ZX R[rt] <= ALUout ORi ALUout <= A + SX R[rt] <= M M <= MEM[ALUout] LW ALUout <= A + SX MEM[ALUout] <= B SW BEQ 0010 If A = B then PC <= ALUout ALUout <= PC +SX Execute Memory Write-back Decode Fetch

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Pipelining °Improve performance by increasing throughput Ideal speedup is number of stages in the pipeline. Do we achieve this?

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Basic Idea °What do we need to add to split the datapath into stages?

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Graphically Representing Pipelines °Can help with answering questions like: how many cycles does it take to execute this code? what is the ALU doing during cycle 4? use this representation to help understand datapaths

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Conventional Pipelined Execution Representation IFetchDcdExecMemWB IFetchDcdExecMemWB IFetchDcdExecMemWB IFetchDcdExecMemWB IFetchDcdExecMemWB IFetchDcdExecMemWB Program Flow Time

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Single Cycle, Multiple Cycle, vs. Pipeline Clk Cycle 1 Multiple Cycle Implementation: IfetchRegExecMemWr Cycle 2Cycle 3Cycle 4Cycle 5Cycle 6Cycle 7Cycle 8Cycle 9Cycle 10 LoadIfetchRegExecMemWr IfetchRegExecMem LoadStore Pipeline Implementation: IfetchRegExecMemWrStore Clk Single Cycle Implementation: LoadStoreWaste Ifetch R-type IfetchRegExecMemWrR-type Cycle 1Cycle 2

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Why Pipeline? °Suppose we execute 100 instructions °Single Cycle Machine 45 ns/cycle x 1 CPI x 100 inst = 4500 ns °Multicycle Machine 10 ns/cycle x 4.6 CPI (due to inst mix) x 100 inst = 4600 ns °Ideal pipelined machine 10 ns/cycle x (1 CPI x 100 inst + 4 cycle drain) = 1040 ns

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Why Pipeline? Because we can! I n s t r. O r d e r Time (clock cycles) Inst 0 Inst 1 Inst 2 Inst 4 Inst 3 ALU Im Reg DmReg ALU Im Reg DmReg ALU Im Reg DmReg ALU Im Reg DmReg ALU Im Reg DmReg

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Can pipelining get us into trouble? °Yes: Pipeline Hazards structural hazards: attempt to use the same resource two different ways at the same time -E.g., combined washer/dryer would be a structural hazard or folder busy doing something else (watching TV) control hazards: attempt to make a decision before condition is evaluated -E.g., washing football uniforms and need to get proper detergent level; need to see after dryer before next load in -branch instructions data hazards: attempt to use item before it is ready -E.g., one sock of pair in dryer and one in washer; can’t fold until get sock from washer through dryer -instruction depends on result of prior instruction still in the pipeline °Can always resolve hazards by waiting pipeline control must detect the hazard take action (or delay action) to resolve hazards

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Mem Single Memory is a Structural Hazard I n s t r. O r d e r Time (clock cycles) Load Instr 1 Instr 2 Instr 3 Instr 4 ALU Mem Reg MemReg ALU Mem Reg MemReg ALU Mem Reg MemReg ALU Reg MemReg ALU Mem Reg MemReg Detection is easy in this case! (right half highlight means read, left half write)

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Structural Hazards limit performance °Example: if 1.3 memory accesses per instruction and only one memory access per cycle then average CPI  1.3 otherwise resource is more than 100% utilized

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 °Stall: wait until decision is clear °Impact: 2 lost cycles (i.e. 3 clock cycles per branch instruction) => slow °Move decision to end of decode save 1 cycle per branch Control Hazard Solution #1: Stall I n s t r. O r d e r Time (clock cycles) Add Beq Load ALU Mem Reg MemReg ALU Mem Reg MemReg ALU Reg MemReg Mem Lost potential

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 °Predict: guess one direction then back up if wrong °Impact: 0 lost cycles per branch instruction if right, 1 if wrong (right ­ 50% of time) Need to “Squash” and restart following instruction if wrong Produce CPI on branch of (1 * *.5) = 1.5 Total CPI might then be: 1.5 * *.8 = 1.1 (20% branch) °More dynamic scheme: history of 1 branch (­ 90%) Control Hazard Solution #2: Predict I n s t r. O r d e r Time (clock cycles) Add Beq Load ALU Mem Reg MemReg ALU Mem Reg MemReg Mem ALU Reg MemReg

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 °Delayed Branch: Redefine branch behavior (takes place after next instruction) °Impact: 0 clock cycles per branch instruction if can find instruction to put in “slot” (­ 50% of time) °As launch more instruction per clock cycle, less useful Control Hazard Solution #3: Delayed Branch I n s t r. O r d e r Time (clock cycles) Add Beq Misc ALU Mem Reg MemReg ALU Mem Reg MemReg Mem ALU Reg MemReg Load Mem ALU Reg MemReg

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Data Hazard on r1 add r1,r2,r3 sub r4,r1,r3 and r6,r1,r7 or r8,r1,r9 xor r10,r1,r11

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Dependencies backwards in time are hazards Data Hazard on r1: I n s t r. O r d e r Time (clock cycles) add r1,r2,r3 sub r4,r1,r3 and r6,r1,r7 or r8,r1,r9 xor r10,r1,r11 IFIF ID/R F EXEX ME M WBWB ALU Im Reg Dm Reg ALU Im Reg DmReg ALU Im Reg DmReg Im ALU Reg DmReg ALU Im Reg DmReg

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 “Forward” result from one stage to another “or” OK if define read/write properly Data Hazard Solution: I n s t r. O r d e r Time (clock cycles) add r1,r2,r3 sub r4,r1,r3 and r6,r1,r7 or r8,r1,r9 xor r10,r1,r11 IFIF ID/R F EXEX ME M WBWB ALU Im Reg Dm Reg ALU Im Reg DmReg ALU Im Reg DmReg Im ALU Reg DmReg ALU Im Reg DmReg

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Reg Dependencies backwards in time are hazards Can’t solve with forwarding: Must delay/stall instruction dependent on loads Forwarding (or Bypassing): What about Loads? Time (clock cycles) lw r1,0(r2) sub r4,r1,r3 IFIF ID/R F EXEX ME M WBWB ALU Im Reg Dm ALU Im Reg DmReg

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Reg Dependencies backwards in time are hazards Can’t solve with forwarding: Must delay/stall instruction dependent on loads Forwarding (or Bypassing): What about Loads Time (clock cycles) lw r1,0(r2) sub r4,r1,r3 IFIF ID/R F EXEX ME M WBWB ALU Im Reg Dm ALU Im Reg DmReg Stall

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Designing a Pipelined Processor °Go back and examine your datapath and control diagram °associated resources with states °ensure that flows do not conflict, or figure out how to resolve °assert control in appropriate stage

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Control and Datapath: Split state diag into 5 pieces IR <- Mem[PC]; PC <– PC+4; A <- R[rs]; B<– R[rt] S <– A + B; R[rd] <– S; S <– A + SX; M <– Mem[S] R[rd] <– M; S <– A or ZX; R[rt] <– S; S <– A + SX; Mem[S] <- B If Cond PC < PC+SX; Exec Reg. File Mem Acces s Data Mem ABS Reg File Equal PC Next PC IR Inst. Mem DM

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Summary: Pipelining °Reduce CPI by overlapping many instructions Average throughput of approximately 1 CPI with fast clock °Utilize capabilities of the Datapath start next instruction while working on the current one limited by length of longest stage (plus fill/flush) detect and resolve hazards °What makes it easy all instructions are the same length just a few instruction formats memory operands appear only in loads and stores °What makes it hard? structural hazards: suppose we had only one memory control hazards: need to worry about branch instructions data hazards: an instruction depends on a previous instruction

CS152 / Kubiatowicz Lec /27/01©UCB Spring 2001 Summary: Where this class is going °We’ll build a simple pipeline and look at these issues Lab 5  Pipelined Processor Lab 6  With caches °We’ll talk about modern processors and what’s really hard: exception handling trying to improve performance with out-of-order execution, etc. Trying to get CPI < 1 (Superscalar execution)