CPE 631: Branch Prediction Electrical and Computer Engineering University of Alabama in Huntsville Aleksandar Milenkovic,

Slides:



Advertisements
Similar presentations
Instruction-Level Parallelism compiler techniques and branch prediction prepared and Instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University March.
Advertisements

Lecture 8 Dynamic Branch Prediction, Superscalar and VLIW Advanced Computer Architecture COE 501.
Dynamic Branch Prediction (Sec 4.3) Control dependences become a limiting factor in exploiting ILP So far, we’ve discussed only static branch prediction.
Computer Organization and Architecture (AT70.01) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Dr. Sumanta Guha Slide Sources: Based.
CPE 631: Branch Prediction Electrical and Computer Engineering University of Alabama in Huntsville Aleksandar Milenkovic,
Dynamic Branch Prediction
Copyright 2001 UCB & Morgan Kaufmann ECE668.1 Adapted from Patterson, Katz and Culler © UCB Csaba Andras Moritz UNIVERSITY OF MASSACHUSETTS Dept. of Electrical.
CPE 731 Advanced Computer Architecture ILP: Part II – Branch Prediction Dr. Gheith Abandah Adapted from the slides of Prof. David Patterson, University.
W04S1 COMP s1 Seminar 4: Branch Prediction Slides due to David A. Patterson, 2001.
Lecture 3: Branch Prediction Young Cho Graduate Computer Architecture I.
1 COMP 206: Computer Architecture and Implementation Montek Singh Wed., Oct. 8, 2003 Topic: Instruction-Level Parallelism (Dynamic Branch Prediction)
1 COMP 206: Computer Architecture and Implementation Montek Singh Mon., Oct. 7, 2002 Topic: Instruction-Level Parallelism (Dynamic Branch Prediction)
1 Lecture 7: Out-of-Order Processors Today: out-of-order pipeline, memory disambiguation, basic branch prediction (Sections 3.4, 3.5, 3.7)
EECE476: Computer Architecture Lecture 20: Branch Prediction Chapter extra The University of British ColumbiaEECE 476© 2005 Guy Lemieux.
EECC551 - Shaaban #1 lec # 5 Fall Reduction of Control Hazards (Branch) Stalls with Dynamic Branch Prediction So far we have dealt with.
EENG449b/Savvides Lec /17/04 February 17, 2004 Prof. Andreas Savvides Spring EENG 449bG/CPSC 439bG.
1 Lecture 8: Branch Prediction, Dynamic ILP Topics: branch prediction, out-of-order processors (Sections )
Branch Target Buffers BPB: Tag + Prediction
EECC551 - Shaaban #1 lec # 5 Winter Reduction of Control Hazards (Branch) Stalls with Dynamic Branch Prediction So far we have dealt with.
1 Lecture 8: Instruction Fetch, ILP Limits Today: advanced branch prediction, limits of ILP (Sections , )
Computer Architecture Instruction Level Parallelism Dr. Esam Al-Qaralleh.
1 Lecture 8: Branch Prediction, Dynamic ILP Topics: static speculation and branch prediction (Sections )
Branch Prediction Dimitris Karteris Rafael Pasvantidιs.
1 COMP 740: Computer Architecture and Implementation Montek Singh Thu, Feb 19, 2009 Topic: Instruction-Level Parallelism III (Dynamic Branch Prediction)
CSC 4250 Computer Architectures October 27, 2006 Chapter 3.Instruction-Level Parallelism & Its Dynamic Exploitation.
Dynamic Branch Prediction
EENG449b/Savvides Lec /25/05 March 24, 2005 Prof. Andreas Savvides Spring g449b EENG 449bG/CPSC 439bG.
CIS 429/529 Winter 2007 Branch Prediction.1 Branch Prediction, Multiple Issue.
Spring 2003CSE P5481 Control Hazard Review The nub of the problem: In what pipeline stage does the processor fetch the next instruction? If that instruction.
1 Lecture 7: Branch prediction Topics: bimodal, global, local branch prediction (Sections )
ENGS 116 Lecture 91 Dynamic Branch Prediction and Speculation Vincent H. Berk October 10, 2005 Reading for today: Chapter 3.2 – 3.6 Reading for Wednesday:
CPSC614 Lec 5.1 Instruction Level Parallelism and Dynamic Execution #4: Based on lectures by Prof. David A. Patterson E. J. Kim.
1 Dynamic Branch Prediction. 2 Why do we want to predict branches? MIPS based pipeline – 1 instruction issued per cycle, branch hazard of 1 cycle. –Delayed.
CSCI 6461: Computer Architecture Branch Prediction Instructor: M. Lancaster Corresponding to Hennessey and Patterson Fifth Edition Section 3.3 and Part.
Branch.1 10/14 Branch Prediction Static, Dynamic Branch prediction techniques.
Korea UniversityG. Lee CRE652 Processor Architecture Dynamic Branch Prediction.
CPE 631 Session 17 Branch Prediction Electrical and Computer Engineering University of Alabama in Huntsville.
Adapted from Computer Organization and Design, Patterson & Hennessy, UCB ECE232: Hardware Organization and Design Part 13: Branch prediction (Chapter 4/6)
Copyright 2016 Csaba Andras MoritzECE668 Power Aware Branching.1 Few slides adapted from Patterson, et al © UCB and Morgan Kaufmann Csaba Andras Moritz.
Yiorgos Makris Professor Department of Electrical Engineering University of Texas at Dallas EE (CE) 6304 Computer Architecture Lecture #13 (10/28/15) Course.
Dynamic Branch Prediction
Instruction-Level Parallelism Dynamic Branch Prediction
CS203 – Advanced Computer Architecture
Computer Structure Advanced Branch Prediction
Dynamic Branch Prediction
COMP 740: Computer Architecture and Implementation
Computer Architecture Advanced Branch Prediction
UNIVERSITY OF MASSACHUSETTS Dept
CS 704 Advanced Computer Architecture
CMSC 611: Advanced Computer Architecture
So far we have dealt with control hazards in instruction pipelines by:
Dynamic Hardware Branch Prediction
CPE 631: Branch Prediction
Branch statistics Branches occur every 4-6 instructions (16-25%) in integer programs; somewhat less frequently in scientific ones Unconditional branches.
12/2/2018 CPE 631 Lecture 11: Instruction Level Parallelism and Its Dynamic Exploitation Aleksandar Milenković, Electrical and Computer.
Dynamic Branch Prediction
/ Computer Architecture and Design
Pipelining and control flow
So far we have dealt with control hazards in instruction pipelines by:
Lecture 10: Branch Prediction and Instruction Delivery
So far we have dealt with control hazards in instruction pipelines by:
So far we have dealt with control hazards in instruction pipelines by:
Adapted from the slides of Prof
Dynamic Hardware Prediction
So far we have dealt with control hazards in instruction pipelines by:
So far we have dealt with control hazards in instruction pipelines by:
So far we have dealt with control hazards in instruction pipelines by:
So far we have dealt with control hazards in instruction pipelines by:
So far we have dealt with control hazards in instruction pipelines by:
CPE 631 Lecture 12: Branch Prediction
Presentation transcript:

CPE 631: Branch Prediction Electrical and Computer Engineering University of Alabama in Huntsville Aleksandar Milenkovic,

 AM LaCASALaCASA 2 The Case for Branch Prediction Dynamic scheduling increases the amount of ILP => control dependence becomes the limiting factor Multiple issue processors Branches will arrive up to N times faster in an n-issue processor Amdahl’s Law => relative impact of the control stalls will be larger with the lower potential CPI in an n-issue processor What have we done? Static schemes for dealing with branches – compiler optimizes the the branch behavior by scheduling it at compile time

 AM LaCASALaCASA 3 7 Branch Prediction Schemes 1-bit Branch-Prediction Buffer 2-bit Branch-Prediction Buffer Correlating Branch Prediction Buffer Tournament Branch Predictor Branch Target Buffer Integrated Instruction Fetch Units Return Address Predictors

 AM LaCASALaCASA 4 Basic Branch Prediction (1) Performance = ƒ(accuracy, cost of misprediction) Branch History Table: a small table of 1-bit values indexed by the lower bits of PC address Says whether or not branch taken last time Useful only to reduce branch delay when it is longer than the time to compute the possible target PC No address check – BHT has no address tags, so the prediction bit may have been put by another branch that has the same low-order bits Prediction is a hint, assumed to be correct – fetching begins in the predicted direction; if it turns out to be wrong, the prediction bit is inverted and stored back

 AM LaCASALaCASA 5 Basic Branch Prediction (2) Problem: in a loop, 1-bit BHT will cause 2 mispredictions (avg is 9 iterations before exit): End of loop case, when it exits instead of looping as before First time through loop on next time through code, when it predicts exit instead of looping Only 80% accuracy even if loop 90% of the time Ideally for highly regular branches, the accuracy of predictor = taken branch frequency Solution: use two-bit prediction schemes

 AM LaCASALaCASA 6 2-bit Scheme States in a two-bit prediction scheme Red: stop, not taken Green: go, taken Adds hysteresis to decision making process NT T T Predict Taken Predict Not Taken Predict Taken Predict Not Taken T NT T

 AM LaCASALaCASA 7 BHT Implementation 1) Small, special “cache” accessed with the instruction address during the IF pipe stage 2) Pair of bits attached to each block in the instruction cache and fetched with the instruction How many branches per instruction? Complexity? Instruction is decoded as branch, and branch is predicted as taken => fetch from the target as soon as the PC is known Note: Does this scheme help for simple MIPS?

 AM LaCASALaCASA 8 BHT Performance Prediction accuracy of 2-bit predictor with 4096 entries is ranging from over 99% to 82% or misprediction rate of 1% to 18% Real impact on performance: prediction accuracy + branch cost + branch frequency How to improve prediction accuracy? Increase the size of the buffer (number of entries) Increase the accuracy for each prediction (increase the number of bits) Both have limited impact!

 AM LaCASALaCASA 9 Case for Correlating Predictors Basic two-bit predictor schemes use recent behavior of a branch to predict its future behavior Improve the prediction accuracy look also at recent behavior of other branches if (aa == 2) aa = 0; if (bb == 2) bb = 0; if (aa != bb) { } subi R3, R1, #2 bnez R3, L1 ; b1 add R1, R0, R0 L1:subi R3, R2, #2 bnez R3, L2 ; b2 add R2, R0, R0 L2:sub R3, R1, R2 beqz R3, L3; b3 b3 is correlated with b1 and b2; If b1 and b2 are both untaken, then b3 will be taken. => Use correlating predictors or two-level predictors.

 AM LaCASALaCASA 10 An Example if (d == 0) d = 1; if (d == 1) {... } bnez R1, L1 ; b1 addi R1, R0, #1 L1:subi R3, R1, #1 bnez R3, L2 ; b2... L2:... Initial value of d d==0?b1Value of d before b2 d==1?b2 0YesNT1YesNT 1NoT1YesNT 2NoT2 T => if b1 is NT, then b2 is NT d=?b1 prediction b1 action New b1 prediction b2 predictionb2 action new b2 prediction 2NTTT TT 0T T 2 TT TT 0T T Behavior of one-bit Standard Predictor initialized to not taken; d alternates between 0 and 2. => All branches are mispredicted

 AM LaCASALaCASA 11 An Example Introduce one bit of correlation Each branch has two separate prediction bits: one prediction assuming the last branch executed was not taken, and another prediction assuming it was taken d=?b1 prediction b1 action New b1 prediction b2 predictionb2 action new b2 prediction 2NT/NTTT/NTNT/NTTNT/T 0T/NTNTT/NTNT/TNTNT/T 2T/NTT NT/TT 0T/NTNTT/NTNT/TNTNT/T => Only misprediction is on the first iteration Prediction bits Prediction if last branch NT Prediction if last branch T NT/NTNT NT/TNTT T/NTTNT T/TTT Behavior of one-bit predictor with one bit of correlation initialized to NT/NT; Assume last branch NT ?NT b1T b2T b1NT b2NT b1T b2T b1NT b2NT

 AM LaCASALaCASA 12 (1,1) Predictor (1, 1) predictor from the previous example Uses the behavior of the last branch to choose from among a pair of one-bit branch predictors (m, n) predictor Uses the behavior of the last m branches to choose from among 2 m predictors, each of which is a n-bit predictor for a single branch Global history of the most recent branches can be recorded in an m-bit shift register (each bit records whether a branch is taken or not)

 AM LaCASALaCASA 13 (2,2) Predictor 2-bit global history to choose from among 4 predictors for each branch address 2-bit local predictor Branch address (4 bits) 2-bits per branch local predictors Prediction 2-bit global branch history (01 = not taken then taken) (2, 2) predictor is implemented as a linear memory array that is 2 bits wide; the indexing is done by concatenating the global history bits and the number of required bits from the branch address.

 AM LaCASALaCASA 14 Fair Predictor Comparison Compare predictors that use the same number of state bits number of state bits for (m, n): 2 m *n*(number of prediction entries) number of state bits for (0, n): n*(number of prediction entries) Example: How many branch selected entries are in a (2,2) predictor that has a total of 8K state bits => 2 2 *2*(number of entries) = 8K => number of branch selected entries is 1K

 AM LaCASALaCASA 15 Accuracy of Different Schemes 4096 Entries 2-bit BHT Unlimited Entries 2-bit BHT 1024 Entries (2,2) BHT Frequency of Mispredictions

 AM LaCASALaCASA 16 Re-evaluating Correlation Several of the SPEC benchmarks have less than a dozen branches responsible for 90% of taken branches: program branch %static# = 90% compress14%23613 eqntott25%4945 gcc15% mpeg10% real gcc13% Real programs + OS more like gcc Small benefits beyond benchmarks for correlation? problems with branch aliases?

 AM LaCASALaCASA 17 Predicated Execution Avoid branch prediction by turning branches into conditionally executed instructions: if (x) then A = B op C else NOP If false, then neither store result nor cause exception Expanded ISA of Alpha, MIPS, PowerPC, SPARC have conditional move; PA-RISC can annul any following instr. IA-64: 64 1-bit condition fields selected so conditional execution of any instruction This transformation is called “if-conversion” Drawbacks to conditional instructions Still takes a clock even if “annulled” Stall if condition evaluated late Complex conditions reduce effectiveness; condition becomes known late in pipeline x A = B op C

 AM LaCASALaCASA 18 Predicated Execution: An Example if (R1 > R2) { R3 = R1 + R2; R4 = R2 + 1; } else R3 = R1 – R2; SGT R5, R1, R2 BZ L1 ADD R3, R1, R2 ADDI R4, R2, #1 J After L1:SUB R3, R1, R2 After:... CMP R1, R2 ; set condition code ADD.GT R3, R1, R2 ADDI.GT R4, R2, #1 SUB.LE R3, R1, R2

 AM LaCASALaCASA 19 BHT Accuracy Mispredict because either: Wrong guess for that branch Got branch history of wrong branch when index the table 4096 entry table programs vary from 1% misprediction (nasa7, tomcatv) to 18% (eqntott), with spice at 9% and gcc at 12% For SPEC92, 4096 about as good as infinite table

 AM LaCASALaCASA 20 Tournament Predictors Motivation for correlating branch predictors is 2-bit predictor failed on important branches; by adding global information, performance improved Tournament predictors use several levels of branch prediction tables together with an algorithm for choosing among predictors Hopes to select right predictor for right branch

 AM LaCASALaCASA 21 Tournament Predictor in Alpha (1) 4K 2-bit counters to choose from among a global predictor and a local predictor 0/00/0 Use LsUse Gs Use L Use G 1/01/0 1/11/1 0/10/1 1/01/0 0/00/0 1/11/1 0/00/0 1/11/1 0/00/0 0/10/1 1/11/1 1/01/0 0/10/1 0/10/1 1/01/0 Legend: 0/0 – Prediction for L is incorrect, Prediction for G is incorrect

 AM LaCASALaCASA 22 Tournament Predictor in Alpha (2) Global predictor also has 4K entries and is indexed by the history of the last 12 branches; each entry in the global predictor is a standard 2-bit predictor 12-bit pattern: ith bit 0 => ith prior branch not taken; ith bit 1 => ith prior branch taken; Local predictor consists of a 2-level predictor: Top level a local history table consisting of bit entries; each 10-bit entry corresponds to the most recent 10 branch outcomes for the entry. 10-bit history allows patterns 10 branches to be discovered and predicted. Next level Selected entry from the local history table is used to index a table of 1K entries consisting a 3-bit saturating counters, which provide the local prediction Total size: 4K*2 + 4K*2 + 1K*10 + 1K*3 = 29K bits! (~180,000 transistors)

 AM LaCASALaCASA 23 % of predictions from local predictor in Tournament Prediction Scheme

 AM LaCASALaCASA 24 Accuracy of Branch Prediction

 AM LaCASALaCASA 25 Accuracy v. Size (SPEC89)

 AM LaCASALaCASA 26 Branch Target Buffers Prediction in DLX need to know from what address to fetch at the end of IF need to know whether the as-yet-undecoded instruction is branch, and if so, what the next PC should be Branch prediction cache that stores the predicted address for the next instruction after a branch is called a branch target buffer (BTB)

 AM LaCASALaCASA 27 BTB Branch PCPredicted PC =? PC of instruction FETCH Extra prediction state bits Yes: instruction is branch and use predicted PC as next PC No: branch not predicted, proceed normally (Next PC = PC+4) Keep only predicted-taken branches in BTB, since an untaken branch follows the same strategy as a nonbranch

 AM LaCASALaCASA 28 Special Case Return Addresses Register Indirect branch hard to predict address SPEC89 85% such branches for procedure return Since stack discipline for procedures, save return address in small buffer that acts like a stack: 8 to 16 entries has small miss rate

 AM LaCASALaCASA 29 Pitfall: Sometimes bigger and dumber is better uses tournament predictor (29 Kbits) Earlier uses a simple 2-bit predictor with 2K entries (or a total of 4 Kbits) SPEC95 benchmarks, outperforms avg mispredictions per 1000 instructions avg mispredictions per 1000 instructions Reversed for transaction processing (TP)! avg. 17 mispredictions per 1000 instructions avg. 15 mispredictions per 1000 instructions TP code much larger & hold 2X branch predictions based on local behavior (2K vs. 1K local predictor in the 21264)

 AM LaCASALaCASA 30 Dynamic Branch Prediction Summary Prediction becoming important part of scalar execution Branch History Table: 2 bits for loop accuracy Correlation: Recently executed branches correlated with next branch. Either different branches Or different executions of same branches Tournament Predictor: more resources to competitive solutions and pick between them Branch Target Buffer: include branch address & prediction Predicated Execution can reduce number of branches, number of mispredicted branches Return address stack for prediction of indirect jump

Advanced Topics

 AM LaCASALaCASA 32 Branch Instructions Branches may change the instruction control flow Type of branches Conditional or Unconditional Direct or Indirect Branch parameters Branch outcome (branch will be taken or not) Branch target address (if taken)

 AM LaCASALaCASA 33 Branch Prediction Deeper and wider pipelines An Example 10 pipeline stages where one instruction is at the each stage Upon decoding, branch target of the direct/unconditional branches known Penalty is 3 cycles – 3 pipeline stages flushed Upon execution, branch outcome/target of the indirect/conditional branches known Penalty is 7 cycles – 7 pipeline stages flushed If CPI IDEAL = 1 and 20% of all instructions are branches with 60% of them taken Consider only outcome penalty: CPI = 1+ (20% × 60% × 7) = 1.84 => Must predict the branch outcome and the target address in instruction fetch stage (before the instruction is decoded)

 AM LaCASALaCASA 34 Branch Target Prediction Instruction fetch address is used to recognize and predict a branch Use Branch Target Buffer A cache-like structure containing the branch target addresses Indexed by a part of the IP address Stores partial tag Indirect Branch Target Buffer A cache-like structure containing the indirect branch target addresses Indexed and tagged by a shift register containing the program path taken to reach the indirect branch

 AM LaCASALaCASA 35 Branch Outcome Prediction Branch Predictor Table (BPT) Indexed by a part of the IP address or by a register recording the program path taken to the branch 2-level (GShare) Combine branch history (kept in a BHR) with address bits Local predictors Better prediction for branches with strong local correlation (e.g., loop branches) More advanced branch predictors Tournament, Hybrid, Agree, Bi-mode, Yags, Gskewed, Loop Predictor

 AM LaCASALaCASA 36 Branch Outcome Prediction Branch Predictor Table (BPT) Indexed by a part of the IP address or by a register recording the program path taken to the branch 2-level (GShare) Combine branch history (kept in a BHR) with address bits Local predictors Better prediction for branches with strong local correlation (e.g., loop branches) More advanced branch predictors Tournament, Hybrid, Agree, Bi-mode, Yags, Gskewed, Loop Predictor

 AM LaCASALaCASA 37 Branch Predictor Design Space Goal: Achieve maximum accuracy, with minimal cost (complexity), latency, and power consumption

 AM LaCASALaCASA 38 Pentium M: Branch Predictor Unit 38