ELEC / Computer Architecture and Design Spring Pipelining (Chapter 6)

Slides:



Advertisements
Similar presentations
Review: MIPS Pipeline Data and Control Paths
Advertisements

ENEE350 Ankur Srivastava University of Maryland, College Park Based on Slides from Mary Jane Irwin ( )
ENEE350 Ankur Srivastava University of Maryland, College Park Based on Slides from Mary Jane Irwin ( )
©UCB CS 162 Computer Architecture Lecture 3: Pipelining Contd. Instructor: L.N. Bhuyan
1 Stalling  The easiest solution is to stall the pipeline  We could delay the AND instruction by introducing a one-cycle delay into the pipeline, sometimes.
55:035 Computer Architecture and Organization Lecture 10.
Pipeline Data Hazards: Detection and Circumvention Adapted from Computer Organization and Design, Patterson & Hennessy, © 2005, and from slides kindly.
Pipelined Datapath and Control
CMPE 421 Parallel Computer Architecture
Electrical and Computer Engineering University of Cyprus LAB3: IMPROVING MIPS PERFORMANCE WITH PIPELINING.
CMPE 421 Parallel Computer Architecture Part 2: Hardware Solution: Forwarding.
CSE431 L07 Overcoming Data Hazards.1Irwin, PSU, 2005 CSE 431 Computer Architecture Fall 2005 Lecture 07: Overcoming Data Hazards Mary Jane Irwin (
CSIE30300 Computer Architecture Unit 05: Overcoming Data Hazards Hsin-Chou Chi [Adapted from material by and
Fall 2015, Oct 5... ELEC / Lecture 6 1 ELEC / Computer Architecture and Design Fall 2015 Pipelining (Chapter 6) Vishwani.
Pipelining: Implementation CPSC 252 Computer Organization Ellen Walker, Hiram College.
Spr 2016, Mar 9... ELEC / Lecture 7 1 ELEC / Computer Architecture and Design Spring 2016 Pipeline Control and Performance.
CSE 340 Computer Architecture Spring 2016 Overcoming Data Hazards.
Computer Architecture Lecture 6.  Our implementation of the MIPS is simplified memory-reference instructions: lw, sw arithmetic-logical instructions:
Computer Organization
Exceptions Another form of control hazard Could be caused by…
Stalling delays the entire pipeline
Note how everything goes left to right, except …
CDA 3101 Spring 2016 Introduction to Computer Organization
Pipelining Chapter 6.
Computer Architecture
CSCI206 - Computer Organization & Programming
Morgan Kaufmann Publishers
Performance of Single-cycle Design
Single Clock Datapath With Control
Appendix C Pipeline implementation
ECS 154B Computer Architecture II Spring 2009
ECS 154B Computer Architecture II Spring 2009
CDA 3101 Spring 2016 Introduction to Computer Organization
\course\cpeg323-08F\Topic6b-323
ECE232: Hardware Organization and Design
Design of the Control Unit for Single-Cycle Instruction Execution
Forwarding Now, we’ll introduce some problems that data hazards can cause for our pipelined processor, and show how to handle them with forwarding.
Chapter 4 The Processor Part 3
Review: MIPS Pipeline Data and Control Paths
Chapter 6 Enhancing Performance with Pipelining
Morgan Kaufmann Publishers The Processor
Morgan Kaufmann Publishers The Processor
Chapter 4 The Processor Part 2
SOLUTIONS CHAPTER 4.
Pipelining review.
Single-cycle datapath, slightly rearranged
Pipelining Chapter 6.
A pipeline diagram Clock cycle lw $t0, 4($sp) IF ID
Pipelining in more detail
CSCI206 - Computer Organization & Programming
\course\cpeg323-05F\Topic6b-323
Data Hazards Data Hazard
The Processor Lecture 3.6: Control Hazards
The Processor Lecture 3.4: Pipelining Datapath and Control
The Processor Lecture 3.2: Building a Datapath with Control
Vishwani D. Agrawal James J. Danaher Professor
The Processor Lecture 3.5: Data Hazards
Instruction Execution Cycle
COSC 2021: Computer Organization Instructor: Dr. Amir Asif
Pipelining Appendix A and Chapter 3.
Morgan Kaufmann Publishers The Processor
Introduction to Computer Organization and Architecture
Pipelining - 1.
A relevant question Assuming you’ve got: One washer (takes 30 minutes)
Stalls and flushes Last time, we discussed data hazards that can occur in pipelined CPUs if some instructions depend upon others that are still executing.
The Processor: Datapath & Control.
©2003 Craig Zilles (derived from slides by Howard Huang)
Processor: Datapath and Control
Pipelined datapath and control
ELEC / Computer Architecture and Design Spring 2015 Pipeline Control and Performance (Chapter 6) Vishwani D. Agrawal James J. Danaher.
Presentation transcript:

ELEC 5200-001/6200-001 Computer Architecture and Design Spring 2016 Pipelining (Chapter 6) Vishwani D. Agrawal James J. Danaher Professor Department of Electrical and Computer Engineering Auburn University, Auburn, AL 36849 http://www.eng.auburn.edu/~vagrawal vagrawal@eng.auburn.edu Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

ILP: Instruction Level Parallelism Single-cycle and multi-cycle datapaths execute one instruction at a time. How can we get better performance? Answer: Execute multiple instructions at a time: Pipelining – Enhance a multi-cycle datapath to fetch one instruction every cycle. Parallelism – Fetch multiple instructions every cycle. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Automobile Team Assembly 1 hour 1 hour 1 hour 1 hour 1 car assembled every four hours 6 cars per day 180 cars per month 2,040 cars per year Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Automobile Assembly Line Task 2 1 hour Task 3 1 hour Task 4 1 hour Task 1 1 hour Mecahnical Electrical Painting Testing First car assembled in 4 hours (pipeline latency) thereafter, 1 car completed per hour 21 cars on first day, thereafter 24 cars per day 717 cars per month 8,637 cars per year What gives 4X increase? Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Throughput: Team Assembly Red car started Red car completed Mechanical Electrical Painting Testing Mechanical Electrical Painting Testing Blue car started Blue car completed Time Time of assembling one car = n hours where n is the number of nearly equal subtasks, each requiring 1 unit of time Throughput = 1/n cars per unit time Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Throughput: Assembly Line Car 1 Car 2 Car 3 Car 4 . Mechanical Electrical Painting Testing Mechanical Electrical Painting Testing Mechanical Electrical Painting Testing Mechanical Electrical Painting Testing Car 1 complete Car 2 complete time Time to complete first car = n time units (latency) Cars completed in time T = T – n + 1 Throughput = 1 – (n – 1)/ T cars per unit time Throughput (assembly line) 1 – (n – 1)/ T n(n – 1) ─────────────────── = ──────── = n – ───── → n Throughput (team assembly) 1/n T as T→∞ Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Some Features of Assembly Line Electrical parts delivered (JIT) Task 2 1 hour Task 3 1 hour Task 4 1 hour Task 1 1 hour Mechanical Electrical Painting Testing 3 cars in the assembly line are suspects, to be removed (flush pipeline) Stall assembly line to fix the cause of defect Defect found Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pros and Cons Advantages: Disadvantages: Efficient use of labor. Specialists can do better job. Just in time (JIT) methodology eliminates warehouse cost. Disadvantages: Penalty of defect latency. Lack of flexibility in production. Assembly line work is monotonous and boring. https://www.youtube.com/watch?v=IjarLbD9r30 https://www.youtube.com/watch?v=ANXGJe6i3G8 https://www.youtube.com/watch?v=5lp4EbfPAtI Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

A Single Cycle Example Clock Delay of 1-bit full adder = 1ns Clock period ≥ 32ns a31 . a2 a1 a0 c32 1-b full adder s31 . s2 s1 s0 1-b full adder b31 . b2 b1 b0 1-b full adder 1-b full adder Time of adding words ~ 32ns Time of adding bytes ~ 32ns Clock Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

A Multicycle Implementation . a2 a1 a0 Delay of 1-bit full adder = 1ns Clock period ≥ 1ns Time of adding words ~ 32ns Time of adding bytes ~ 8ns Shift 1-b full adder b31 . b2 b1 b0 s31 . s2 s1 s0 c32 FF Shift Shift Initialize to 0 Clock Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

A Pipeline Implementation Delay of 1-bit full adder = 1ns Clock period ≥ 1ns a31 . a2 a1 a0 P1 P2 P3 .. P31 c32 1-b full adder s31 . s2 s1 s0 1-b full adder b31 . b2 b1 b0 1-b full adder 1-b full adder Time of adding words ~ 1ns Time of adding bytes ~ 1ns Clock Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipelining in a Computer Divide datapath into nearly equal tasks, to be performed serially and requiring non-overlapping resources. Insert registers at task boundaries in the datapath; registers pass the output data from one task as input data to the next task. Synchronize tasks with a clock having a cycle time just enough to complete the longest task. Break each instruction down into a set of tasks so that instructions can be executed in a staggered fashion. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipelining of RISC Instructions (From Lecture 3, Slide 6) Fetch Instruction Examine Opcode ALU Operation Memory Read/Write Store Result IF ID EX MEM WB Instruction Decode Execute Memory Write Fetch instruction and Operation Back Fetch operands to Reg file Although an instruction takes five clock cycles, one instruction is completed every cycle. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipelining a Single-Cycle Datapath Instruction class Instr. fetch (IF) Instr. Decode (also reg. file read) (ID) Execution (ALU Operation) (EX) Data access (MEM) Write Back (Reg. file write) (WB) Total time lw 2ns 1ns 8ns sw R-format add, sub, and, or, slt B-format, beq No operation on data; idle times equalize instruction lengths. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Execution Time: Single-Cycle 0 2 4 6 8 10 12 14 16 . . Time (ns) lw $1, 100($0) lw $2, 200($0) lw $3, 300($0) IF ID EX MEM WB IF ID EX MEM WB IF ID EX MEM WB Clock cycle time = 8 ns Total time for executing three lw instructions = 24 ns Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipelined Datapath Instruction class Instr. fetch (IF) Instr. Decode (also reg. file read) (ID) Execu-tion (ALU Opera-tion) (EX) Data access (MEM) Write Back (Reg. file write) (WB) Total time lw 2ns 1ns 10ns sw R-format: add, sub, and, or, slt B-format: beq No operation on data; idle time inserted to equalize instruction lengths. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Execution Time: Pipeline 0 2 4 6 8 10 12 14 16 . . Time (ns) lw $1, 100($0) lw $2, 200($0) lw $3, 300($0) IF ID EX MEM RW IF ID EX MEM RW IF ID EX MEM RW Clock cycle time = 2 ns, four times faster than single-cycle clock Total time for executing three lw instructions = 14 ns Single-cycle time 24 Performance ratio = ──────────── = ── = 1.7 Pipeline time 14 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipeline Performance Clock cycle time = 2 ns 1,003 lw instructions: Total time for executing 1,003 lw instructions = 2,014 ns Single-cycle time 8,024 Performance ratio = ──────────── = ──── = 3.98 Pipeline time 2,014 10,003 lw instructions: Performance ratio = 80,024 / 20,014 = 3.998 → Clock cycle ratio (4) Pipeline performance approaches clock-cycle ratio for long programs. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Single-Cycle Datapath WB: write back ID: Instr. decode, reg. file read EX: Execute, address calc. MEM: mem. access IF: Instr. fetch 4 Add 1 mux 0 ALU Branch opcode MemtoReg CONTROL 26-31 RegWrite ALUSrc 21-25 zero MemWrite MemRead ALU Instr. mem. PC Reg. File Data mem. 16-20 1 mux 0 0 mux 1 1 mux 0 11-15 RegDst ALUOp ALU Cont. Sign ext. Shift left 2 0-15 0-5 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipeline Registers ALU ALU Data mem. 1 mux 0 Add opcode CONTROL Instr. IF/ID ID/EX EX/MEM 4 1 mux 0 Add ALU Branch opcode MemtoReg CONTROL 26-31 MEM/WB RegWrite ALUSrc 21-25 zero MemWrite MemRead ALU Instr. mem. PC Reg. File Data mem. 16-20 1 mux 0 0 mux 1 1 mux 0 11-15 This requires a CONTROL not too different from single-cycle RegDst ALUOp ALU Cont. Sign ext. Shift left 2 0-15 0-5 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipeline Register Functions Four pipeline registers are added: Register name Data held IF/ID PC+4, Instruction word (IW) ID/EX PC+4, R1, R2, IW(0-15) sign ext., IW(11-15) EX/MEM PC+4, zero, ALUResult, R2, IW(11-15) or IW(16-20) MEM/WB M[ALUResult], ALUResult, IW(11-15) or IW(16-20) Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipelined Datapath ALU ALU 1 mux 0 Add opcode Instr Reg. File mem PC IF/ID ID/EX EX/MEM MEM/WB 4 Add 1 mux 0 ALU opcode Shift left 2 26-31 zero 21-25 Instr mem ALU 16-20 Data mem. PC Reg. File 1 mux 0 0 mux 1 Sign ext. 11-15 for R-type 16-20 for I-type lw 0-15 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Five-Cycle Pipeline CC1 CC2 CC3 CC4 CC5 ID, REG. FILE DM REG. FILE IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM PC Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Add Instruction add $t0, $s1, $s2 Machine instruction word 000000 10001 10010 01000 00000 100000 opcode $s1 $s2 $t0 function CC1 CC2 CC3 CC4 CC5 DM IM ID, REG. FILE READ ALU REG. FILE WRITE PC IF/ID ID/EX EX/MEM MEM/WB IF ID EX MEM WB read $s1 add write $t0 read $s2 $s1+$s2 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipelined Datapath Executing add IF/ID ID/EX EX/MEM MEM/WB 4 Add 1 mux 0 ALU opcode Shift left 2 26-31 s1 zero $s1 21-25 Instr mem ALU addr Data mem data 16-20 PC Reg. File $s2 s2 1 mux 0 0 mux 1 Sign ext. 11-15 for R-type 16-20 for I-type lw t0 0-15 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Load Instruction lw $t0, 1200 ($t1) 100011 01001 01000 0000 0100 1000 0000 opcode $t1 $t0 1200 CC1 CC2 CC3 CC4 CC5 IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM PC IF ID EX MEM WB read $t1 add read write $t0 sign ext $t1+1200 M[addr] 1200 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipelined Datapath Executing lw IF/ID ID/EX EX/MEM MEM/WB 4 Add 1 mux 0 ALU opcode Shift left 2 26-31 t1 zero $t1 21-25 Instr mem ALU addr Data mem data 16-20 PC Reg. File 1 mux 0 0 mux 1 Sign ext. 11-15 for R-type 16-20 for I-type lw t0 0-15 1200 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Store Instruction sw $t0, 1200 ($t1) 101011 01001 01000 0000 0100 1000 0000 opcode $t1 $t0 1200 CC1 CC2 CC3 CC4 CC5 IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM PC IF ID EX MEM WB read $t1 add write sign ext $t1+1200 M[addr] 1200 (addr) ← $t0 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipelined Datapath Executing sw IF/ID ID/EX EX/MEM MEM/WB 4 Add 1 mux 0 ALU opcode Shift left 2 26-31 t1 zero $t1 21-25 Instr mem ALU addr Data mem data 16-20 PC Reg. File $t0 t0 1 mux 0 0 mux 1 Sign ext. 11-15 for R-type 16-20 for I-type lw 0-15 1200 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Executing a Program Consider a five-instruction segment: lw $10, 20($1) sub $11, $2, $3 add $12, $3, $4 lw $13, 24($1) add $14, $5, $6 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Program Execution time CC1 CC2 CC3 CC4 CC5 IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM lw $10, 20($1) IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM PC sub $11, $2, $3 IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM Program instructions add $12, $3, $4 PC IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM lw $13, 24($1) PC IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM add $14, $5, $6 PC Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

CC5 ALU ALU 1 mux 0 Add opcode Instr Reg. File mem PC 1 mux 0 Data sub $11, $2, $3 WB: lw $10, 20($1) IF: add $14, $5, $6 ID: lw $13, 24($1) EX: add $12, $3, $4 IF/ID ID/EX EX/MEM MEM/WB 4 Add 1 mux 0 ALU opcode Shift left 2 26-31 zero 21-25 Instr mem ALU 16-20 Data mem. PC Reg. File 1 mux 0 0 mux 1 Sign ext. 11-15 for R-type 16-20 for I-type lw 0-15 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Advantages of Pipeline After the fifth cycle (CC5), one instruction is completed each cycle; CPI ≈ 1, neglecting the initial pipeline latency of 5 cycles. Pipeline latency is defined as the number of stages in the pipeline, or The number of clock cycles after which the first instruction is completed. The clock cycle time is about four times shorter than that of single-cycle datapath and about the same as that of multicycle datapath. For multicycle datapath, CPI = 3. …. So, pipelined execution is faster, but . . . Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Science is always wrong. It never solves a problem without creating ten more. George Bernard Shaw Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Pipeline Hazards Definition: Hazard in a pipeline is a situation in which the next instruction cannot complete execution one clock cycle after completion of the present instruction. Three types of hazards: Structural hazard (resource conflict) Data hazard Control hazard Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Structural Hazard Two instructions cannot execute due to a resource conflict. Example: Consider a computer with a common data and instruction memory. The fourth cycle of a lw instruction requires memory access (memory read) and at the same time the first cycle of the fourth instruction requires instruction fetch (memory read). This will cause a memory resource conflict. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Example of Structural Hazard time CC1 CC2 CC3 CC4 CC5 IM/DM IM/DM ID, REG. FILE READ ALU REG. FILE WRITE IF/ID ID/EX EX/MEM MEM/WB lw $10, 20($1) IM/DM IM/DM ID, REG. FILE READ ALU REG. FILE WRITE PC IF/ID ID/EX EX/MEM MEM/WB sub $11, $2, $3 Common data and instr. Mem. IM/DM IM/DM ID, REG. FILE READ ALU REG. FILE WRITE add $12, $3, $4 PC IF/ID ID/EX EX/MEM MEM/WB Program instructions IM/DM IM/DM ID, REG. FILE READ ALU REG. FILE WRITE lw $13, 24($1) PC IF/ID ID/EX EX/MEM MEM/WB Nedded by two instructions Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Possible Remedies for Structural Hazards Provide duplicate hardware resources in datapath. Control unit or compiler can insert delays (no-op cycles) between instructions. This is known as pipeline stall or bubble. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Stall (Bubble) for Structural Hazard time CC1 CC2 CC3 CC4 CC5 IM/DM IM/DM ID, REG. FILE READ ALU REG. FILE WRITE lw $10, 20($1) IF/ID ID/EX EX/MEM MEM/WB IM/DM IM/DM ID, REG. FILE READ ALU REG. FILE WRITE sub $11, $2, $3 PC IF/ID ID/EX EX/MEM MEM/WB IM/DM IM/DM ID, REG. FILE READ ALU REG. FILE WRITE add $12, $3, $4 PC IF/ID ID/EX EX/MEM MEM/WB Program instructions Stall (bubble) IM/DM IM/DM ID, REG. FILE READ ALU REG. FILE WRITE lw $13, 24($1) PC IF/ID ID/EX EX/MEM MEM/WB Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Data Hazard Data hazard means that an instruction cannot be completed because the needed data, being generated by another instruction in the pipeline, is not available. Example: consider two instructions: add $s0, $t0, $t1 sub $t2, $s0, $t3 # needs $s0 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Example of Data Hazard We need to read s0 from reg file in cycle 3 time CC1 CC2 CC3 CC4 CC5 Write s0 in CC5 DM IM ID, REG. FILE READ ALU REG. FILE WRITE add $s0, $t0, $t1 PC IF/ID ID/EX EX/MEM MEM/WB DM IM ID, REG. FILE READ ALU REG. FILE WRITE sub $t2, $s0, $t3 PC IF/ID ID/EX EX/MEM MEM/WB Read s0 and t3 in CC3 We need to read s0 from reg file in cycle 3 But s0 will not be written in reg file until cycle 5 However, s0 will only be used in cycle 4 And it is available at the end of cycle 3 Program instructions Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Forwarding or Bypassing Output of a resource used by an instruction is forwarded to the input of some resource being used by another instruction. Forwarding can eliminate some, but not all, data hazards. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Forwarding for Data Hazard time CC1 CC2 CC3 CC4 CC5 Write s0 in CC5 DM IM ID, REG. FILE READ ALU REG. FILE WRITE add $s0, $t0, $t1 PC IF/ID ID/EX EX/MEM MEM/WB Forwarding DM IM ID, REG. FILE READ ALU REG. FILE WRITE PC IF/ID ID/EX EX/MEM sub $t2, $s0, $t3 MEM/WB Read s0 and t3 in CC3 Program instructions Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Forwarding Unit Hardware ID/EX EX/MEM MEM/WB FORW. MUX ALU Data Mem. MUX Data to reg. file FORW. MUX Destination registers Source reg. IDs from opcode Forwarding Unit Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Forwarding Alone May Not Work time CC1 CC2 CC3 CC4 CC5 Write s0 in CC5 DM IM ID, REG. FILE READ ALU REG. FILE WRITE lw $s0, 20($s1) PC IF/ID ID/EX EX/MEM MEM/WB DM IM ID, REG. FILE READ ALU REG. FILE WRITE PC sub $t2, $s0, $t3 IF/ID ID/EX EX/MEM MEM/WB Read s0 and t3 in CC3 Program instructions data needed by sub (data hazard) data available from memory only at the end of cycle 4 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Use Bubble and Forwarding time CC1 CC2 CC3 CC4 CC5 Write s0 in CC5 DM IM ID, REG. FILE READ ALU REG. FILE WRITE lw $s0, 20($s1) PC IF/ID ID/EX EX/MEM MEM/WB stall (bubble) Forwarding DM IM ID, REG. FILE READ ALU REG. FILE WRITE sub $t2, $s0, $t3 PC IF/ID ID/EX EX/MEM MEM/WB Program instructions Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Hazard Detection Unit Hardware Disable write Hazard Detection Unit ID/EX EX/MEM MEM/WB Control FORW. MUX ALU NOP MUX Data Mem. PC FORW. MUX Instruction to reg. file IF/ID Control signals Forwarding Unit register IDs from prev. instr. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Resolving Hazards Hazards are resolved by Hazard detection and forwarding units. Compiler’s understanding of how these units work can improve performance. Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Avoiding Stall by Code Reorder C code: A = B + E; C = B + F; MIPS code: lw $t1, 0($t0) . $t1 written lw $t2, 4($t0) . . $t2 written add $t3, $t1, $t2 . . . $t1, $t2 needed sw $t3, 12($t0) . . . . lw $t4, 8($t0) . . . . . $t4 written add $t5, $t1, $t4 . . . . . $t4 needed sw $t5, 16($t0) . . . . . . . . . . . . . . . Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Reordered Code C code: A = B + E; C = B + F; MIPS code: lw $t1, 0($t0) add $t3, $t1, $t2 no hazard sw $t3, 12($t0) add $t5, $t1, $t4 no hazard sw $t5, 16($t0) Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Control Hazard Instruction to be fetched is not known! Example: Instruction being executed is branch-type, which will determine the next instruction: add $4, $5, $6 beq $1, $2, 40 next instruction . . . 40 and $7, $8, $9 Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Stall on Branch time CC1 CC2 CC3 CC4 CC5 IM ID, REG. FILE READ ALU DM WRITE IF/ID ID/EX MEM/WB EX/MEM PC add $4, $5, $6 IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM beq $1, $2, 40 PC Program instructions Stall (bubble) IM ID, REG. FILE READ ALU DM REG. FILE WRITE IF/ID ID/EX MEM/WB EX/MEM next instruction or and $7, $8, $9 PC Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Why Only One Stall? Extra hardware in ID phase: Additional ALU to compute branch address Comparator to generate zero signal Hazard detection unit writes the branch address in PC Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Ways to Handle Branch Stall or bubble Branch prediction: Heuristics Next instruction Prediction based on statistics (dynamic) Hardware decision (dynamic) Prediction error: pipeline flush Delayed branch Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Delayed Branch Example Stall on branch add $4, $5, $6 beq $1, $2, skip next instruction . . . skip or $7, $8, $9 Delayed branch beq $1, $2, skip add $4, $5, $6 next instruction . . . skip or $7, $8, $9 Instruction executed irrespective of branch decision Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Delayed Branch time CC1 CC2 CC3 CC4 CC5 DM IM ID, REG. FILE READ ALU WRITE PC IF/ID ID/EX EX/MEM MEM/WB beq $1, $2, skip Program instructions DM IM ID, REG. FILE READ ALU REG. FILE WRITE add $4, $5, $6 PC IF/ID ID/EX EX/MEM MEM/WB DM next instruction or skip or $7, $8, $9 IM ID, REG. FILE READ ALU REG. FILE WRITE PC IF/ID ID/EX EX/MEM MEM/WB Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6

Summary: Hazards Structural hazards Data hazards Control hazards Cause: resource conflict Remedies: (i) hardware resources, (ii) stall (bubble) Data hazards Cause: data unavailablity Remedies: (i) forwarding, (ii) stall (bubble), (iii) code reordering Control hazards Cause: out-of-sequence execution (branch or jump) Remedies: (i) stall (bubble), (ii) branch prediction/pipeline flush, (iii) delayed branch/pipeline flush Spring 2016, Feb 29 . . . ELEC 5200-001/6200-001 Lecture 6