Control Hazards. Branch Prediction

Similar documents
Control Hazards. Prediction

Instruction-Level Parallelism Dynamic Branch Prediction. Reducing Branch Penalties

Branch statistics. 66% forward (i.e., slightly over 50% of total branches). Most often Not Taken 33% backward. Almost all Taken

Looking for Instruction Level Parallelism (ILP) Branch Prediction. Branch Prediction. Importance of Branch Prediction

Reduction of Control Hazards (Branch) Stalls with Dynamic Branch Prediction

Announcements. ECE4750/CS4420 Computer Architecture L10: Branch Prediction. Edward Suh Computer Systems Laboratory

Looking for Instruction Level Parallelism (ILP) Branch Prediction. Branch Prediction. Importance of Branch Prediction

Instruction-Level Parallelism. Instruction Level Parallelism (ILP)

Instruction Level Parallelism (Branch Prediction)

Memory Hierarchy Basics. Ten Advanced Optimizations. Small and Simple

As the amount of ILP to exploit grows, control dependences rapidly become the limiting factor.

Instruction-Level Parallelism and Its Exploitation (Part III) ECE 154B Dmitri Strukov

Chapter 3 (CONT) Instructor: Josep Torrellas CS433. Copyright J. Torrellas 1999,2001,2002,2007,2013 1

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

November 7, 2014 Prediction

The Processor: Improving the performance - Control Hazards

Complex Pipelines and Branch Prediction

Memory Hierarchy Basics

Computer Architecture: Branch Prediction. Prof. Onur Mutlu Carnegie Mellon University

LRU. Pseudo LRU A B C D E F G H A B C D E F G H H H C. Copyright 2012, Elsevier Inc. All rights reserved.

Lecture 7: Static ILP, Branch prediction. Topics: static ILP wrap-up, bimodal, global, local branch prediction (Sections )

ECE 4750 Computer Architecture, Fall 2017 T13 Advanced Processors: Branch Prediction

Processor (IV) - advanced ILP. Hwansoo Han

Static Branch Prediction

Dynamic Control Hazard Avoidance

The Processor: Instruction-Level Parallelism

Chapter 4. The Processor

CS433 Homework 2 (Chapter 3)

ECE 571 Advanced Microprocessor-Based Design Lecture 9

Branch Prediction & Speculative Execution. Branch Penalties in Modern Pipelines

Branch prediction ( 3.3) Dynamic Branch Prediction

Real Processors. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Lecture 13: Branch Prediction

COMPUTER ORGANIZATION AND DESI

LECTURE 3: THE PROCESSOR

Lecture 8 Dynamic Branch Prediction, Superscalar and VLIW. Computer Architectures S

Exploitation of instruction level parallelism

Advanced Computer Architecture

Instruction Level Parallelism

Dynamic Hardware Prediction. Basic Branch Prediction Buffers. N-bit Branch Prediction Buffers

Lec 25: Parallel Processors. Announcements

Lecture 8: Instruction Fetch, ILP Limits. Today: advanced branch prediction, limits of ILP (Sections , )

Advanced d Instruction Level Parallelism. Computer Systems Laboratory Sungkyunkwan University

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

CISC 662 Graduate Computer Architecture Lecture 13 - CPI < 1

Processors, Performance, and Profiling

HY425 Lecture 05: Branch Prediction

Full Datapath. Chapter 4 The Processor 2

Multithreaded Processors. Department of Electrical Engineering Stanford University

ASSEMBLY LANGUAGE MACHINE ORGANIZATION

Micro-Architectural Attacks and Countermeasures

CS146 Computer Architecture. Fall Midterm Exam

ECE 571 Advanced Microprocessor-Based Design Lecture 7

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

Ti Parallel Computing PIPELINING. Michał Roziecki, Tomáš Cipr

Instruction Level Parallelism (ILP)

In embedded systems there is a trade off between performance and power consumption. Using ILP saves power and leads to DECREASING clock frequency.

CS433 Homework 2 (Chapter 3)

Lecture 7: Static ILP and branch prediction. Topics: static speculation and branch prediction (Appendix G, Section 2.3)

CS 654 Computer Architecture Summary. Peter Kemper

ECE232: Hardware Organization and Design

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;

Dynamic Branch Prediction

Some material adapted from Mohamed Younis, UMBC CMSC 611 Spr 2003 course slides Some material adapted from Hennessy & Patterson / 2003 Elsevier

Topic 14: Dealing with Branches

Topic 14: Dealing with Branches

Architectural Performance. Superscalar Processing. 740 October 31, i486 Pipeline. Pipeline Stage Details. Page 1

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

ECE 571 Advanced Microprocessor-Based Design Lecture 8

Multiple Instruction Issue. Superscalars

CS Computer Architecture

Chapter 4. The Processor

ILP concepts (2.1) Basic compiler techniques (2.2) Reducing branch costs with prediction (2.3) Dynamic scheduling (2.4 and 2.5)

Lecture 12 Branch Prediction and Advanced Out-of-Order Superscalars

Hardware-based speculation (2.6) Multiple-issue plus static scheduling = VLIW (2.7) Multiple-issue, dynamic scheduling, and speculation (2.

Instruction Fetch and Branch Prediction. CprE 581 Computer Systems Architecture Readings: Textbook (4 th ed 2.3, 2.9); (5 th ed 3.

Lecture 5: Instruction Pipelining. Pipeline hazards. Sequential execution of an N-stage task: N Task 2

ECE 341. Lecture # 15

(Refer Slide Time: 00:02:04)

Homework 5. Start date: March 24 Due date: 11:59PM on April 10, Monday night. CSCI 402: Computer Architectures

CPU Pipelining Issues

CS252 Spring 2017 Graduate Computer Architecture. Lecture 8: Advanced Out-of-Order Superscalar Designs Part II

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

Lecture 8: Compiling for ILP and Branch Prediction. Advanced pipelining and instruction level parallelism

Advanced Parallel Architecture Lessons 5 and 6. Annalisa Massini /2017

Memory Hierarchy Computing Systems & Performance MSc Informatics Eng. Memory Hierarchy (most slides are borrowed)

SISTEMI EMBEDDED. Computer Organization Pipelining. Federico Baronti Last version:

Memory Hierarchy Computing Systems & Performance MSc Informatics Eng. Memory Hierarchy (most slides are borrowed)

Pipelining and Vector Processing

COMPUTER ORGANIZATION AND DESIGN

Instruction Level Parallelism

William Stallings Computer Organization and Architecture 8 th Edition. Chapter 14 Instruction Level Parallelism and Superscalar Processors

Hardware-based Speculation

BRANCH PREDICTORS. Mahdi Nazm Bojnordi. CS/ECE 6810: Computer Architecture. Assistant Professor School of Computing University of Utah

CS425 Computer Systems Architecture

Static, multiple-issue (superscaler) pipelines

CS6303 Computer Architecture Regulation 2013 BE-Computer Science and Engineering III semester 2 MARKS

Quiz 5 Mini project #1 solution Mini project #2 assigned Stalling recap Branches!

Instruction-Level Parallelism and Its Exploitation

ELE 655 Microprocessor System Design

Transcription:

Control Hazards The nub of the problem: In what pipeline stage does the processor fetch the next instruction? If that instruction is a conditional branch, when does the processor know whether the conditional branch is taken (execute code at the target address) or not taken (execute the sequential code)? What is the difference in cycles between them? The cost of stalling until you know which way to branch number of cycles in between these 2 stages * branch frequency = the contribution to CPI due to branches Predict the branch outcome to avoid stalling 1 Branch Branch prediction: Resolve a branch hazard by predicting which path will be taken Proceed under that assumption If the prediction is correct, avoid delay of the branch hazard If the prediction is incorrect, flush the wrong-path instructions from the pipeline & fetch the right path Performance improvement depends on: how often the prediction is correct (producing correct predictions is most of the innovation) how soon you can check the prediction 2 1

Branch Dynamic branch prediction: the prediction is determined at runtime & changes as program behavior changes branch prediction mechanism implemented in hardware common algorithm based on branch history predict the branch taken if branched the last time predict the branch not-taken if didn t branch the last time Alternative: static branch prediction compiler-determined prediction fixed for the life of the program A likely algorithm? 3 Branch Buffer Branch prediction buffer (BPB) small memory indexed by the lower bits of the address of a branch instruction during the fetch stage contains a 1-bit prediction (which path the last branch to index to this BPB location took) do what the prediction says to do if the prediction is taken & it is correct only incur a one-cycle penalty (in our 5-stage pipeline)- why? if the prediction is not taken & it is correct incur no penalty - why? if the prediction is incorrect change the prediction also flush the pipeline penalty is the same as if there were no branch prediction 4 2

Two-bit A single prediction bit does not work well with loops mispredicts the first & last iterations of a nested loop Two-bit branch prediction for loops Algorithm: have to be wrong twice in a row before prediction is changed 5 Two-bit Works well when branches predominantly go in the same direction A second check is made to make sure that a short & temporary change of direction does not change the prediction away from the dominant direction Why does it work? What architecture design principle is involved? 6 3

Is Branch More Important Today? Think about: Is the number of branches in code changing? Is modern hardware design changing the dynamic frequency of branches? Is it getting harder or easier to predict branch outcomes? Is the misprediction penalty changing? 7 Branch is More Important Today Conditional branches still comprise about 20% of instructions Still correct predictions are more important today pipelines deeper branch not resolved until more cycles from fetching therefore the misprediction penalty greater cycle times smaller: more emphasis on throughput (performance) more functionality between fetch & execute multiple instruction issue (superscalars & VLIW) & multiple threads branch occurs almost every cycle flushing & re-fetching more instructions object-oriented programming more indirect branches which harder to predict dual of Amdahl s Law other forms of pipeline stalling are being addressed so the portion of CPI due to branch delays is relatively larger All this means that the potential stalling due to branches is greater 8 4

Branch is More Important Today On the other hand, chips are denser so we can consider sophisticated HW solutions hardware cost is small compared to the performance gain 9 Technical Directions in Branch 1: Improve the prediction 2-level, correlating (or adaptive) predictor (Intel Core i7, ARM Cortex-A8, Intel Pentiums) use both history & branch address (Cortex-A8, MIPS) tournament predictor (Pentium 4, IBM Power 5) 2: Determine the target earlier branch target buffer (everybody) next address in I-cache (Sun UltraSPARC) return address stack (everybody) 3: Reduce misprediction penalty fetch both instruction streams (IBM mainframes) 4: Eliminate branch execution predicated execution (Intel Itanium) 10 5

1: Correlating (Adaptive) Predictor The rationale: having the prediction depend on the outcome of only 1 branch might produce bad predictions some branch outcomes are correlated example: same condition variable if (d==0)... if (d!=0) example: related condition variables if (d==0) b=1; if (b==1) 11 1: Correlating Predictor more complicated example: related condition variables if (x==2) /* branch 1 */ x=0; if (y==2) /* branch 2 */ y=0; if (x!=y) /* branch 3 */ do this; else do that; if branches 1 & 2 are taken, branch 3 is not taken use a history of the past m branches represents an execution path through the program (but still 1 or 2 bits of prediction) 12 6

1: Correlating Predictor General idea of correlating branch prediction: put the global branch history in a global history register global history is a shift register: shift left in the new branch outcome use its value to access a pattern history table (PHT) of 2-bit saturating counters (the predictions) 13 1: Correlating Predictor Many implementation variations the number of branch history registers 1 history register for all branches (global) table of history registers, strive for 1 for each branch path (private: model only) table of history registers, each shared by several branch paths (shared) the history length (number of entries in each history register) the number of PHTs how access the PHT What is the trade-off? 14 7

1: Tournament Predictor Combine branch predictors local, per-branch prediction, accessed by the low PC bits correlated prediction based on the last m branches, assessed by the global history register hardware that tracks which is currently the best predictor for this branch 2-bit counter: increase for one, decrease for the other 15 2: Branch Target Buffer (BTB) Cache that stores: the addresses of branches the predicted target address branch prediction bits (optional) Accessed by PC address in fetch stage if hit: address was for this branch instruction fetch the target instruction if a hit (and if prediction bits say taken) No branch delay if: prediction is taken & is correct branch target is found in BTB (assume BTB update is done in the next cycles) 16 8

2: Return Address Stack The bad news: indirect jump targets are hard to predict registers for target calculation are accessed several stages after fetch The good news: most indirect jumps (85%) are returns from functions optimize for this common case Return address stack return address pushed on a call, popped on a return provides the return target early best for procedures that are called from multiple call sites BTB would predict address of the return from the last call if big enough, can predict returns perfectly these days 1-32 entries 17 Calculating the Cost of Branches Factors to consider: branch frequency (every 4-6 instructions) correct prediction rate 1 bit: ~ 80% to 85% 2 bit: ~ high 80s to low 90% correlated branch prediction: ~ 96% misprediction penalty RISCs: 4-7 cycles Intel Core i7: 15 cycles ARM Cortex A8: 13 cycles then have to multiply by the instruction width or misfetch penalty have the correct prediction but not know the target address yet 18 9

Calculating the Cost of Branches What is the probability that a branch is taken? Given: 20% of branches are unconditional branches of conditional branches, 66% branch forward & are evenly split between taken & not taken the rest branch backwards & are always taken 19 Calculating the Cost of Branches What is the contribution to CPI of conditional branch stalls, given: 15% branch frequency a BTB for conditional branches only with a 10% miss rate 3-cycle miss penalty branch prediction hardware 92% prediction accuracy 7 cycle misprediction penalty base CPI is 1 BTB result Frequency (per instruction) Penalty (cycles) Stalls miss --.15 *.10 =.015 3.045 hit correct.15 *.90 *.92 =.124 0 0 hit incorrect.15 *.90 *.08 =.011 7.076 Total contribution to CPI.121 20 10

Dynamic Branch, the Executive Summary Stepping back from the designs & looking forward to the future, how do you figure out whether branch prediction (or any other aspect of a processor design) is still important to improve? Look at technology trends What is cheap now that was expensive before? (cheap = cost, time, chip area, ease of programming, ) How do the trends affect different aspects of prediction performance (or hardware cost or power consumption, etc.)? Given these effects, which factors become bottlenecks? What techniques can we devise to eliminate the bottlenecks? 21 Research Predicting branches based on machine-learning algorithms Predicting load addresses Predicting variable values Predicting which cache block will be accessed next Predicting which thread will hold a lock next Predicting which thread should execute on a multithreaded processor Predicting power consumption & when it pays to power-down processor components Predicting when a fault might occur 22 11

Important Issues Anything in red or green: what causes branch hazards what is branch prediction dynamic vs. static branch prediction why branch prediction is important today BPB, two-bit, correlating, tournament BTB, return address stack factors that determine branch performance how do you determine what aspect of design is important to improve 23 12