Part IV Data Path and Control Slide About This Presentation This presentation is intended to support the use of the textbook Computer Architecture: From Microprocessors to Supercomputers, Oxford University Press, 2005, ISBN 0-19-515455-X It is updated regularly by the author as part of his teaching of the upperdivision course ECE 154, Introduction to Computer Architecture, at the University of California, Santa Barbara Instructors can use these slides freely in classroom teaching and for other educational purposes Any other use is strictly prohibited © Behrooz Parhami Edition Released Revised Revised Revised Revised First July 2003 July 2004 July 2005 Mar 2006 Feb 2007 Slide A Few Words About Where We Are Headed Performance = / Execution time simplified to / CPU execution time CPU execution time = Instructions × CPI / (Clock rate) Performance = Clock rate / ( Instructions × Try to achieve CPI = with clock that is as high as that for CPI > designs; is CPI < feasible? (Chap 15-16) Design memory & I/O structures to support ultrahigh-speed CPUs (chap 17-24) Define an instruction set; make it simple enough to require a small number of cycles and allow high clock rate, but not so simple that we need many instructions, even for very simple tasks (Chap 5-8) CPI ) Design hardware for CPI = 1; seek improvements with CPI > (Chap 13-14) Design ALU for arithmetic & logic ops (Chap 9-12) Slide IV Data Path and Control Design a simple computer (MicroMIPS) to learn about: • Data path – part of the CPU where data signals flow • Control unit – guides data signals through data path • Pipelining – a way of achieving greater performance Topics in This Part Chapter 13 Instruction Execution Steps Chapter 14 Control Unit Synthesis Chapter 15 Pipelined Data Paths Chapter 16 Pipeline Performance Limits Slide 13 Instruction Execution Steps A simple computer executes instructions one at a time • Fetches an instruction from the loc pointed to by PC • Interprets and executes the instruction, then repeats Topics in This Chapter 13.1 A Small Set of Instructions 13.2 The Instruction Execution Unit 13.3 A Single-Cycle Data Path 13.4 Branching and Jumping 13.5 Deriving the Control Signals 13.6 Performance of the Single-Cycle Design Slide 13.1 A Small Set of Instructions R I 31 op 25 rs 20 rt 15 rd 10 sh fn bits bits bits bits bits bits Opcode Source or base Source or dest’n Destination Unused Opcode ext imm Operand / Offset, 16 bits J jta Jump target address, 26 bits inst Instruction, 32 bits Fig 13.1 MicroMIPS instruction formats and naming of the various fields We will refer to this diagram later Seven R-format ALU instructions (add, sub, slt, and, or, xor, nor) Six I-format ALU instructions (lui, addi, slti, andi, ori, xori) Two I-format memory access instructions (lw, sw) Three I-format conditional branch instructions (bltz, beq, bne) Four unconditional jump instructions (j, jr, jal, syscall) Slide The MicroMIPS Instruction Set Copy Arithmetic Logic Memory access Control transfer Table 13.1 Instruction Usage Load upper immediate Add Subtract Set less than Add immediate Set less than immediate AND OR XOR NOR AND immediate OR immediate XOR immediate Load word Store word Jump Jump register Branch less than Branch equal Branch not equal Jump and link System call lui rt,imm add rd,rs,rt sub rd,rs,rt slt rd,rs,rt addi rt,rs,imm slti rd,rs,imm and rd,rs,rt or rd,rs,rt xor rd,rs,rt nor rd,rs,rt andi rt,rs,imm ori rt,rs,imm xori rt,rs,imm lw rt,imm(rs) sw rt,imm(rs) j L jr rs bltz rs,L beq rs,rt,L bne rs,rt,L jal L syscall op fn 15 0 10 0 0 12 13 14 35 43 Slide 32 34 42 36 37 38 39 12 13.2 The Instruction Execution Unit beq,bne syscall R 31 I op 25 rs 20 15 rd 10 sh fn bits bits bits bits bits bits Opcode Source or base Source or dest’n Destination Unused Opcode ext imm Operand / Offset, 16 bits Next addr jta bltz,jr J jta Jump target address, 26 bits inst j,jal Instruction, 32 bits rs,rt,rd PC Instr cache rt (rs) Reg file inst 12 A/L, lui, lw,sw ALU 22 instructions Address Data Data cache (rt) imm op fn Control Fig 13.2 Abstract view of the instruction execution unit for MicroMIPS For naming of instruction fields, see Fig 13.1 Slide 13.3 A Single-Cycle Data Path Incr PC Next addr jta Next PC (PC) PC Instr cache rs rt inst rd 31 imm op Br&Jump Instruction fetch Fig 13.3 ALUOvfl Register writeback (rs) Ovfl Reg file ALU (rt) / 16 ALU out Data cache Data out Data in Func 32 SE / Data addr Register input fn RegDst RegWrite Reg access / decode ALUSrc ALUFunc ALU operation DataRead RegInSrc DataWrite Data access Key elements of the single-cycle MicroMIPS data path Slide Const′Var Shift function Constant amount Amount 5 Variable amount 00 01 10 11 No shift Logical left Logical right Arith right Shifter Function class 32 LSBs Shifted y x c0 32 Adder y 32 k / c 31 imm x±y or 00 01 10 11 Shift Set less Arithmetic Logic Shorthand symbol for ALU s MSB 32 32 An ALU for MicroMIPS lui Control c 32 x Func Add′Sub s ALU Logic unit AND OR XOR NOR 00 01 10 11 y 32input NOR Ovfl Zero Logic function Zero Ovfl Fig 10.19 A multifunction ALU with control signals (2 for function class, arithmetic, shift, logic) specifying the operation Slide 10 Hardware for Inserting Bubbles Stage Stage Stage Data hazard detector LoadPC (rs) rs Instr cache PC rt x2 Reg file (rt) Inst reg y2 Bubble Control signals from decoder All-0s t2 Controls or all-0s DataRead2 Fig 16.5 Data hazard detector for the pipelined MicroMIPS data path Slide 66 Augmentations to Pipelined Data Path and Control Branch predictor Next addr forwarders Hazard detector Stage Data cache forwarder Stage Stage Stage ALUOvfl PC inst Instr cache rs rt (rs) Reg file IncrPC rt rd 31 Func op Br&Jump RegDst fn RegWrite ALUSrc 1 2 SeqInst Data cache ALU imm SE Incr Data Data addr Address Ovfl (rt) Fig 15.10 Stage Next addr NextPC ALU forwarders ALUFunc DataRead RetAddr DataWrite RegInSrc Slide 67 16.3 Pipeline Branch Hazards Software-based solutions Compiler inserts a “no-op” after every branch (simple, but wasteful) Branch is redefined to take effect after the instruction that follows it Branch delay slot(s) are filled with useful instructions via reordering Hardware-based solutions Mechanism similar to data hazard detector to flush the pipeline Constitutes a rudimentary form of branch prediction: Always predict that the branch is not taken, flush if mistaken More elaborate branch prediction strategies possible Slide 68 16.4 Branch Prediction Predicting whether a branch will be taken • Always predict that the branch will not be taken • Use program context to decide (backward branch is likely taken, forward branch is likely not taken) • Allow programmer or compiler to supply clues • Decide based on past history (maintain a small history table); to be discussed later • Apply a combination of factors: modern processors use elaborate techniques due to deep pipelines Slide 69 Forward and Backward Branches Example 5.5 List A is stored in memory beginning at the address given in $s1 List length is given in $s2 Find the largest integer in the list and copy it into $t0 Solution Scan the list, holding the largest element identified thus far in $t0 lw addi loop: add beq add add add lw slt beq addi maximum j done: $t0,0($s1) $t1,$zero,0 $t1,$t1,1 $t1,$s2,done $t2,$t1,$t1 $t2,$t2,$t2 $t2,$t2,$s1 $t3,0($t2) $t4,$t0,$t3 $t4,$zero,loop $t0,$t3,0 # # # # # # # # # # initialize maximum to A[0] initialize index i to increment index i by if all elements examined, quit compute 2i in $t2 compute 4i in $t2 form address of A[i] in $t2 load value of A[i] into $t3 maximum < A[i]? if not, repeat with no change # if so, A[i] is the new loop # change completed; now repeat # continuation of the program Slide 70 A Simple Branch Prediction Algorithm Taken Not taken Not taken Predict taken Taken Not taken Predict taken again Taken Predict not taken Not taken Predict not taken again Taken Fig 16.6 Four-state branch prediction scheme Example 16.1 L1: -10 iter’s -20 iter’s L2: br L2 -br L1 Impact of different branch prediction schemes Solution Always taken: 11 mispredictions, 94.8% accurate 1-bit history: 20 mispredictions, 90.5% accurate 2-bit history: Same as always taken Slide 71 Other Branch Prediction Algorithms Problem 16.3 Taken Not taken Not taken Part a Predict taken Taken Not taken Predict taken again Taken Part b Predict taken Taken Taken Not taken Predict not taken Not taken Taken Predict taken again Taken Predict not taken again Not taken Taken Predict not taken Not taken Predict not taken again Not taken Taken Not taken Not taken Fig 16.6 Predict taken Taken Not taken Predict taken again Taken Predict not taken Not taken Predict not taken again Taken Slide 72 Hardware Implementation of Branch Prediction Low-order bits used as index Addresses of recent branch instructions Target addresses History bit(s) Incremented PC Next PC Read-out table entry From PC Fig 16.7 Compare = Logic Hardware elements for a branch prediction scheme The mapping scheme used to go from PC contents to a table entry is the same as that used in direct-mapped caches (Chapter 18) Slide 73 16.5 Advanced Pipelining Deep pipeline = superpipeline; also, superpipelined, superpipelining Parallel instruction issue = superscalar, j-way issue (2-4 is typical) Stage Stage Stage Stage Stage Variable # of stages Stage q−2 Stage q−1 Stage q Function unit Instr cache Instr decode Operand prep Instr issue Function unit Function unit Instruction fetch Retirement & commit stages Fig 16.8 Dynamic instruction pipeline with in-order issue, possible out-of-order completion, and in-order retirement Slide 74 Performance Improvement for Deep Pipelines Hardware-based methods Lookahead past an instruction that will/may stall in the pipeline (out-of-order execution; requires in-order retirement) Issue multiple instructions (requires more ports on register file) Eliminate false data dependencies via register renaming Predict branch outcomes more accurately, or speculate Software-based method Pipeline-aware compilation Loop unrolling to reduce the number of branches Loop: Compute with index i Increment i by Go to Loop if not done Loop: Compute with index i Compute with index i + Increment i by Go to Loop if not done Slide 75 CPI Variations with Architectural Features Table 16.2 Effect of processor architecture, branch prediction methods, and speculative execution on CPI Architecture Methods used in practice CPI Nonpipelined, multicycle Strict in-order instruction issue and exec 5-10 Nonpipelined, overlapped In-order issue, with multiple function units 3-5 Pipelined, static In-order exec, simple branch prediction 2-3 Superpipelined, dynamic Out-of-order exec, adv branch prediction 1-2 Superscalar 2- to 4-way issue, interlock & speculation 0.5-1 Advanced superscalar 4- to 8-way issue, aggressive speculation 0.2-0.5 Need 100 for TIPS performance Need 10,000 for 100 TIPS Need 100,000 for PIPS 3.3 inst / cycle × Gigacycles / s ≅ 10 GIPS Slide 76 Development of Intel’s Desktop/Laptop Micros In the beginning, there was the 8080; led to the 80x86 = IA32 ISA Half a dozen or so pipeline stages 80286 80386 80486 Pentium (80586) More advanced technology A dozen or so pipeline stages, with out-of-order instruction execution Pentium Pro Pentium II Pentium III Celeron More advanced technology Instructions are broken into micro-ops which are executed out-of-order but retired in-order Two dozens or so pipeline stages Pentium Slide 77 16.6 Dealing with Exceptions Exceptions present the same problems as branches How to handle instructions that are ahead in the pipeline? (let them run to completion and retirement of their results) What to with instructions after the exception point? (flush them out so that they not affect the state) Precise versus imprecise exceptions Precise exceptions hide the effects of pipelining and parallelism by forcing the same state as that of strict sequential execution (desirable, because exception handling is not complicated) Imprecise exceptions are messy, but lead to faster hardware (interrupt handler can clean up to offer precise exception) Slide 78 The Three Hardware Designs for MicroMIPS Incr PC Single-cycle Next addr jta Next PC (PC) PC Instr cache rd 31 imm op ALUOvfl rs rt inst Multicycle Inst Reg Ovfl Reg file ALU (rt) ALU out Data cache 1 Cache Data Reg ALUZero x Mux ALUOvfl Zero z Reg Ovfl (rs) Reg file Data y Mux ×4 (rt) imm 16 / 32 y Reg SE / 30 ×4 ALU Func ALU out Register input fn Inst′Data Br&Jump rs rt rd 31 SysCallAddr x Reg jta PC Data out Data in Func 32 SE / Data addr 30 / MSBs Address (rs) / 16 26 / RegDst RegWrite ALUSrc DataRead RegInSrc DataWrite ALUFunc 125 MHz CPI = Stage Stage ALUOvfl PC inst Instr cache rs rt (rs) fn RegInSrc RegDst ALUSrcX RegWrite Stage ALUSrcY Stage Reg file Incr IncrPC rt rd 31 op Data cache Func 500 MHz CPI ≅ Br&Jump RegDst fn RegWrite ALUSrc 1 2 SeqInst PCSrc JumpAddr Data addr ALU imm SE ALUFunc Ovfl (rt) 500 MHz CPI ≅ 1.1 IRWrite Stage Pipelined op MemWrite MemRead Next addr NextPC PCWrite ALUFunc DataRead RetAddr DataWrite RegInSrc Slide 79 Where Do We Go from Here? Memory Design: How to build a memory unit that responds in clock Input and Output: Peripheral devices, I/O programming, interfacing, interrupts Higher Performance: Vector/array processing Parallel processing Slide 80 ... Slide IV Data Path and Control Design a simple computer (MicroMIPS) to learn about: • Data path – part of the CPU where data signals flow • Control unit – guides data signals through data path •... The control state machine for multicycle MicroMIPS Slide 27 State and Instruction Decoding op /4 10 11 12 13 14 15 ControlSt0 ControlSt1 ControlSt2 ControlSt3 ControlSt4 ControlSt5 ControlSt6 ControlSt7... out Data addr Data cache Data out Data in Register input fn RegDst RegWrite ALUSrc ALUFunc DataRead RegInSrc DataWrite Key elements of the single-cycle MicroMIPS data path Slide 41 Multicycle Data