Chapter 4 The Processor 1. Chapter 4B. The Processor

Similar documents
Processor (II) - pipelining. Hwansoo Han

Full Datapath. Chapter 4 The Processor 2

Pipelining Analogy. Pipelined laundry: overlapping execution. Parallelism improves performance. Four loads: Non-stop: Speedup = 8/3.5 = 2.3.

Department of Computer and IT Engineering University of Kurdistan. Computer Architecture Pipelining. By: Dr. Alireza Abdollahpouri

COMPUTER ORGANIZATION AND DESIGN

Pipeline Hazards. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Chapter 4. The Processor

The Processor (3) Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

CSEE 3827: Fundamentals of Computer Systems

Full Datapath. Chapter 4 The Processor 2

COMPUTER ORGANIZATION AND DESIGN

ECE260: Fundamentals of Computer Engineering

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. 5 th. Edition. Chapter 4. The Processor

Lecture Topics. Announcements. Today: Data and Control Hazards (P&H ) Next: continued. Exam #1 returned. Milestone #5 (due 2/27)

Lecture 3: The Processor (Chapter 4 of textbook) Chapter 4.1

The Processor: Improving the performance - Control Hazards

Thomas Polzer Institut für Technische Informatik

LECTURE 3: THE PROCESSOR

Outline Marquette University

Chapter 4. The Processor

CENG 3420 Lecture 06: Pipeline

Determined by ISA and compiler. We will examine two MIPS implementations. A simplified version A more realistic pipelined version

Chapter 4. The Processor

Computer Architecture Computer Science & Engineering. Chapter 4. The Processor BK TP.HCM

Chapter 4. The Processor

zhandling Data Hazards The objectives of this module are to discuss how data hazards are handled in general and also in the MIPS architecture.

EIE/ENE 334 Microprocessors

Pipelined datapath Staging data. CS2504, Spring'2007 Dimitris Nikolopoulos

ECE473 Computer Architecture and Organization. Pipeline: Data Hazards

Computer Architecture Computer Science & Engineering. Chapter 4. The Processor BK TP.HCM

LECTURE 9. Pipeline Hazards

Chapter 4 The Processor 1. Chapter 4A. The Processor

3/12/2014. Single Cycle (Review) CSE 2021: Computer Organization. Single Cycle with Jump. Multi-Cycle Implementation. Why Multi-Cycle?

14:332:331 Pipelined Datapath

ECS 154B Computer Architecture II Spring 2009

Pipelining. CSC Friday, November 6, 2015

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Chapter 4. The Processor. Jiang Jiang

Pipelined Datapath. Reading. Sections Practice Problems: 1, 3, 8, 12 (2) Lecture notes from MKP, H. H. Lee and S.

Pipelined Datapath. Reading. Sections Practice Problems: 1, 3, 8, 12

DEE 1053 Computer Organization Lecture 6: Pipelining

ELE 655 Microprocessor System Design

Data Hazards Compiler Scheduling Pipeline scheduling or instruction scheduling: Compiler generates code to eliminate hazard

MIPS Pipelining. Computer Organization Architectures for Embedded Computing. Wednesday 8 October 14

Chapter 4. The Processor

Chapter 4 (Part II) Sequential Laundry

Chapter 4. The Processor

Pipeline Overview. Dr. Jiang Li. Adapted from the slides provided by the authors. Jiang Li, Ph.D. Department of Computer Science

COSC121: Computer Systems. ISA and Performance

Lecture 9 Pipeline and Cache

EE557--FALL 1999 MIDTERM 1. Closed books, closed notes

ECE Exam II - Solutions November 8 th, 2017

Design a MIPS Processor (2/2)

1 Hazards COMP2611 Fall 2015 Pipelined Processor

CISC 662 Graduate Computer Architecture Lecture 6 - Hazards

Computer Architecture

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface 5 th Edition. Chapter 4. The Processor

COMPUTER ORGANIZATION AND DESI

Lecture 3. Pipelining. Dr. Soner Onder CS 4431 Michigan Technological University 9/23/2009 1

MIPS An ISA for Pipelining

CS 251, Winter 2018, Assignment % of course mark

COSC 6385 Computer Architecture - Pipelining

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 4. The Processor

Outline. A pipelined datapath Pipelined control Data hazards and forwarding Data hazards and stalls Branch (control) hazards Exception

Modern Computer Architecture

EI338: Computer Systems and Engineering (Computer Architecture & Operating Systems)

ECE154A Introduction to Computer Architecture. Homework 4 solution

Midnight Laundry. IC220 Set #19: Laundry, Co-dependency, and other Hazards of Modern (Architecture) Life. Return to Chapter 4

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 4. The Processor

Chapter 4. Instruction Execution. Introduction. CPU Overview. Multiplexers. Chapter 4 The Processor 1. The Processor.

ECE331: Hardware Organization and Design

COMP2611: Computer Organization. The Pipelined Processor

Pipelining. Ideal speedup is number of stages in the pipeline. Do we achieve this? 2. Improve performance by increasing instruction throughput ...

The Processor. Z. Jerry Shi Department of Computer Science and Engineering University of Connecticut. CSE3666: Introduction to Computer Architecture

Pipeline Data Hazards. Dealing With Data Hazards

CS 61C: Great Ideas in Computer Architecture Pipelining and Hazards

Lecture 9. Pipeline Hazards. Christos Kozyrakis Stanford University

Lecture 2: Processor and Pipelining 1

Instruction word R0 R1 R2 R3 R4 R5 R6 R8 R12 R31

ECE232: Hardware Organization and Design

Unresolved data hazards. CS2504, Spring'2007 Dimitris Nikolopoulos

Processor Design Pipelined Processor (II) Hung-Wei Tseng

Lecture 05: Pipelining: Basic/ Intermediate Concepts and Implementation

CS 251, Winter 2019, Assignment % of course mark

Computer and Information Sciences College / Computer Science Department Enhancing Performance with Pipelining

COMPUTER ORGANIZATION AND DESIGN. 5 th Edition. The Hardware/Software Interface. Chapter 4. The Processor

Instr. execution impl. view

COMPUTER ORGANIZATION AND DESIGN

Computer Organization and Structure

Control Hazards - branching causes problems since the pipeline can be filled with the wrong instructions.

Chapter 6 Exercises with solutions

CPE 335 Computer Organization. Basic MIPS Pipelining Part I

ECE 3056: Architecture, Concurrency, and Energy of Computation. Sample Problem Sets: Pipelining

CS 110 Computer Architecture. Pipelining. Guest Lecture: Shu Yin. School of Information Science and Technology SIST

ECE Exam II - Solutions October 30 th, :35 pm 5:55pm

5 th Edition. The Processor We will examine two MIPS implementations A simplified version A more realistic pipelined version

CS232 Final Exam May 5, 2001

EE557--FALL 1999 MAKE-UP MIDTERM 1. Closed books, closed notes

What is Pipelining? Time per instruction on unpipelined machine Number of pipe stages

CSE Lecture 13/14 In Class Handout For all of these problems: HAS NOT CANNOT Add Add Add must wait until $5 written by previous add;

Transcription:

Chapter 4 The Processor 1 Chapter 4B The Processor

Chapter 4 The Processor 2 Control Hazards Branch determines flow of control Fetching next instruction depends on branch outcome Pipeline can t always fetch correct instruction Next: still at ID stage of branch before calculating the branch target address In MIPS pipeline Need to compare registers and compute target early in the pipeline Add hardware to do it in ID stage

Chapter 4 The Processor 3 Branch Instruction When branch decision is made in MEM, I n s t r. O r d e r beq lw Inst 3 Inst 4 ALU IM Reg DM Reg ALU IM Reg DM Reg ALU IM Reg DM Reg ALU IM Reg DM Reg

Chapter 4 The Processor 4 Branch Instruction When branch decision is made in EXE, I n s t r. O r d e r beq lw Inst 3 Inst 4 ALU IM Reg DM Reg ALU IM Reg DM Reg ALU IM Reg DM Reg ALU IM Reg DM Reg

Chapter 4 The Processor 5 Stall for a branch (2 cycles) I n s t r. beq stall ALU IM Reg DM Reg Fix branch hazard by waiting stall but affects CPI O r d e r stall lw ALU IM Reg DM Reg Inst 3 ALU IM Reg DM

Chapter 4 The Processor 6 Branch Instruction When branch decision is made in ID, I n s t r. O r d e r beq lw Inst 3 Inst 4 ALU IM Reg DM Reg ALU IM Reg DM Reg ALU IM Reg DM Reg ALU IM Reg DM Reg

Chapter 4 The Processor 7 Stall for a branch (1 cycles) I n s t r. O r d e r beq stall lw Inst 3 ALU IM Reg DM Reg ALU IM Reg DM Reg ALU IM Reg DM

Chapter 4 The Processor 8 Control Hazards When the flow of instruction addresses is not sequential (i.e., PC = PC + 4); incurred by change of flow instructions Conditional branches (beq, bne) Unconditional branches (j, jal, jr) Exceptions Possible approaches Stall (impacts CPI) Delayed branch (requires compiler support) Move decision point as early in the pipeline as possible, thereby reducing the number of stall cycles Predict and hope for the best! Control hazards occur less frequently than data hazards, but there is nothing as effective against control hazards as forwarding is for data hazards

Chapter 4 The Processor 9 Stall on Branch Wait until branch outcome determined before fetching next instruction

Chapter 4 The Processor 10 Two Types of Stalls Bubble: nop instruction (or bubble) inserted between two instructions in the pipeline (as done for load-use situations) Flush (or instruction squashing) : an instruction in the pipeline is replaced with a nop instruction (as done for instructions located sequentially after j instructions) Zero the control bits for the instruction to be flushed

Exceptions Detect Exception Capture exception PC, exception cause Flush pipeline Begin fetching at new PC Chapter 4 The Processor 11

Delayed Branch If the branch hardware has been moved to the ID stage, then we can eliminate all branch stalls with delayed branches which are defined as always executing the next sequential instruction after the branch instruction the branch takes effect after that next instruction MIPS compiler moves an instruction to immediately after the branch that is not affected by the branch (a safe instruction) thereby hiding the branch delay With deeper pipelines, the branch delay grows requiring more than one delay slot Delayed branches have lost popularity compared to more expensive but more flexible (dynamic) hardware branch prediction Growth in available transistors has made hardware branch prediction relatively cheaper Chapter 4 The Processor 12

Branch Delay Slots Since we need to have a dead cycle anyway, let s put a useful instruction there Advantage: Do more useful work Potentially get rid of all stalls ADD $t2,$t3,$t4 BNEZ $t5,_loop NOP BNEZ $t5,_loop ADD $t2,$t3,$t4 Disadvantage: Exposes microarchitecture to ISA Deeper pipelines require more delay slots Chapter 4 The Processor 13

Chapter 4 The Processor 14 Scheduling Branch Delay Slots A. From before branch B. From branch target C. From fall through add $1,$2,$3 if $2=0 then delay slot sub $4,$5,$6 add $1,$2,$3 if $1=0 then delay slot add $1,$2,$3 if $1=0 then delay slot sub $4,$5,$6 becomes becomes becomes add $1,$2,$3 sub $4,$5,$6 if $2=0 then if $1=0 then add $1,$2,$3 add $1,$2,$3 if $1=0 then sub $4,$5,$6 sub $4,$5,$6 sub $4,$5,$6 A is the best choice, fills delay slot and reduces IC In B and C, the sub instruction may need to be copied, increasing IC In B and C, must be okay to execute sub when branch fails

Branch Delay Slots: A Solution? Not really Exposes stuff to the ISA that is better kept hidden Not scalable as the pipeline changes Chapter 4 The Processor 15

Branch Prediction Longer pipelines can t readily determine branch outcome early Stall penalty becomes unacceptable Predict outcome of branch No stall if prediction is correct Only stall if prediction is wrong In MIPS pipeline Can predict branches not taken Fetch instruction after branch, with no delay Chapter 4 The Processor 16

Static Branch Prediction Resolve branch hazards by assuming a given outcome and proceeding without waiting to see the actual branch outcome Predict not taken always predict branches will not be taken, continue to fetch from the sequential instruction stream, only when branch is taken does the pipeline stall If taken, flush instructions after the branch (earlier in the pipeline) in IF and ID stages if branch logic in EX two stalls in IF stage if branch logic in ID one stall ensure that those flushed instructions haven t changed the machine state automatic in the MIPS pipeline since machine state changing operations are at the tail end of the pipeline (MemWrite (in MEM) or RegWrite (in WB)) restart the pipeline at the branch destination Chapter 4 The Processor 17

Chapter 4 The Processor 18 MIPS with Predict Not Taken Prediction correct Prediction incorrect flush

Chapter 4 The Processor 19 Flushing if Taken I n s t r. O r d e r 4 beq $1,$2,1 8 sub $4,$1,$5 16 and $6,$1,$7 20 or r8,$1,$9 ALU IM Reg DM Reg ALU IM Reg DM Reg ALU flushed IM Reg DM Reg ALU IM Reg DM Reg To flush the IF stage instruction, assert IF.Flush to zero the instruction field of the IF/ID pipeline register (transforming it into a nop)

Chapter 4 The Processor 20 Branching Structures Predict not taken works well for top of the loop branching structures But such loops have jumps at the bottom of the loop to return to the top of the loop and incur the jump stall overhead Loop: beq $1,$2,Out 1 nd loop instr... last loop instr j Loop Out: fall out instr Predict not taken doesn t work well for bottom of the loop branching structures Loop: 1 st loop instr 2 nd loop instr... last loop instr bne $1,$2,Loop fall out instr

More-Realistic Branch Prediction Static branch prediction Based on typical branch behavior Example: loop and if-statement branches Predict backward branches taken Predict forward branches not taken Dynamic branch prediction Hardware measures actual branch behavior e.g., record recent history of each branch Assume future behavior will continue the trend When wrong, stall while re-fetching, and update history Will be explained later again! Chapter 4 The Processor 21

Pipeline Summary The BIG Picture Pipelining improves performance by increasing instruction throughput Executes multiple instructions in parallel Each instruction has the same latency Subject to hazards Structure, data, control Instruction set design affects complexity of pipeline implementation Chapter 4 The Processor 22

Chapter 4 The Processor 23 MIPS Pipelined Datapath jump 4.6 Pipelined Datapath and Control MEM Right-to-left flow leads to hazards WB

Pipeline registers Need registers between stages To hold information produced in previous cycle Chapter 4 The Processor 24

Pipeline Operation Cycle-by-cycle flow of instructions through the pipelined datapath Single-clock-cycle pipeline diagram Shows pipeline usage in a single cycle Highlight resources used c.f. multi-clock-cycle diagram Graph of operation over time We ll look at single-clock-cycle diagrams for load & store Chapter 4 The Processor 25

IF for Load, Store, Chapter 4 The Processor 26

ID for Load, Store, Chapter 4 The Processor 27

EX for Load Chapter 4 The Processor 28

MEM for Load Chapter 4 The Processor 29

Chapter 4 The Processor 30 WB for Load Wrong register number

Corrected Datapath for Load Chapter 4 The Processor 31

EX for Store Chapter 4 The Processor 32

MEM for Store Chapter 4 The Processor 33

WB for Store Chapter 4 The Processor 34

Chapter 4 The Processor 35 Multi-Cycle Pipeline Diagram Traditional form

Chapter 4 The Processor 36 Single-Cycle Pipeline Diagram State of pipeline in a given cycle

Chapter 4 The Processor 37 Another Pipeline Diagram Form showing resource usage

Chapter 4 The Processor 38 Pipelined Control (Simplified) rs rt rd rt rd

Pipelined Control Control signals derived from instruction As in single-cycle implementation Chapter 4 The Processor 39

Pipelined Control rs rt rd rt rd Chapter 4 The Processor 40

Data Hazards in ALU Instructions Consider this sequence: sub $2, $1,$3 and $12,$2,$5 or $13,$6,$2 add $14,$2,$2 sw $15,100($2) We can resolve hazards with forwarding 4.7 Data Hazards: Forwarding vs. Stalling How do we detect when to forward? Chapter 4 The Processor 41

Dependencies & Forwarding Chapter 4 The Processor 42

Chapter 4 The Processor 43 Detecting the Need to Forward Pass register numbers along pipeline e.g., ID/EX.RegisterRs = register number for Rs sitting in ID/EX pipeline register ALU operand register numbers in EX stage are given by ID/EX.RegisterRs, ID/EX.RegisterRt Data hazards when 1a. EX/MEM.RegisterRd = ID/EX.RegisterRs 1b. EX/MEM.RegisterRd = ID/EX.RegisterRt 2a. MEM/WB.RegisterRd = ID/EX.RegisterRs 2b. MEM/WB.RegisterRd = ID/EX.RegisterRt Fwd from EX/MEM pipeline reg Fwd from MEM/WB pipeline reg

Chapter 4 The Processor 44 Another Pipeline Diagram Form showing resource usage

Detecting the Need to Forward But only if forwarding instruction will write to a register! EX/MEM.RegWrite, MEM/WB.RegWrite And only if Rd for that instruction is not $zero EX/MEM.RegisterRd 0, MEM/WB.RegisterRd 0 Chapter 4 The Processor 45

Forwarding Paths Chapter 4 The Processor 46

Forwarding Conditions EX hazard if (EX/MEM.RegWrite and (EX/MEM.RegisterRd 0) and (EX/MEM.RegisterRd = ID/EX.RegisterRs)) ForwardA = 10 if (EX/MEM.RegWrite and (EX/MEM.RegisterRd 0) and (EX/MEM.RegisterRd = ID/EX.RegisterRt)) ForwardB = 10 MEM hazard if (MEM/WB.RegWrite and (MEM/WB.RegisterRd 0) and (MEM/WB.RegisterRd = ID/EX.RegisterRs)) ForwardA = 01 if (MEM/WB.RegWrite and (MEM/WB.RegisterRd 0) and (MEM/WB.RegisterRd = ID/EX.RegisterRt)) ForwardB = 01 Chapter 4 The Processor 47

Double Data Hazard Consider the sequence: add $1,$1,$2 add $1,$1,$3 add $1,$1,$4 Both hazards occur Want to use the most recent EX hazard condition does not need to revise Revise MEM hazard condition Only fwd if EX hazard condition isn t true Chapter 4 The Processor 48

Revised Forwarding Condition MEM hazard if (MEM/WB.RegWrite and (MEM/WB.RegisterRd 0) and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd 0) and (EX/MEM.RegisterRd = ID/EX.RegisterRs)) and (MEM/WB.RegisterRd = ID/EX.RegisterRs)) ForwardA = 01 if (MEM/WB.RegWrite and (MEM/WB.RegisterRd 0) and not (EX/MEM.RegWrite and (EX/MEM.RegisterRd 0) and (EX/MEM.RegisterRd = ID/EX.RegisterRt)) and (MEM/WB.RegisterRd = ID/EX.RegisterRt)) ForwardB = 01 Chapter 4 The Processor 49

Datapath with Forwarding Chapter 4 The Processor 50

Control for Forwarding Muxes Chapter 4 The Processor 51

Chapter 4 The Processor 52 Load-Use Data Hazard Need to stall for one cycle

Chapter 4 The Processor 53 Another Pipeline Diagram Form showing resource usage Need to stall a cycle: not yet available

Chapter 4 The Processor 54 Load-Use Hazard Detection Check when using instruction is decoded in ID stage ALU operand register numbers in ID stage are given by IF/ID.RegisterRs, IF/ID.RegisterRt Load-use hazard when ID/EX.MemRead and ((ID/EX.RegisterRt = IF/ID.RegisterRs) or (ID/EX.RegisterRt = IF/ID.RegisterRt)) If detected, stall and insert bubble

How to Stall the Pipeline Force control values in ID/EX register to 0 EX, MEM and WB do nop (no-operation) Prevent update of PC and IF/ID register Using instruction is decoded again IF/IDWrite=0 Following instruction is fetched again PCWrite=0 1-cycle stall allows MEM to read data for lw Can subsequently forward to EX stage Chapter 4 The Processor 55

Datapath with Hazard Detection Chapter 4 The Processor 56

Stall/Bubble in the Pipeline Stall inserted here and and or and or and is stalled a cycle in ID or is stalled a cycle in IF Chapter 4 The Processor 57

Chapter 4 The Processor 58 Another Pipeline Diagram Form showing resource usage $2 becomes and and nop nop nop or and $4 or

Chapter 4 The Processor 59 Another Pipeline Diagram Form showing resource usage $2 equal values becomes and and $2 or and $4 or

Stalls and Performance The BIG Picture Stalls reduce performance But are required to get correct results Compiler can arrange code to avoid hazards and stalls Requires knowledge of the pipeline structure Chapter 4 The Processor 60