Download Branch Prediction in Computer Architecture: Static and Dynamic Techniques and more Lecture notes Brand Management in PDF only on Docsity!
Branch Prediction
▪ Static Branch Prediction
▪ Dynamic Branch Prediction
▪ Note: Assignment 1: due Feb 19th.
1
Inf3 Computer Architecture - 2017-
Static Branch Prediction
▪ Compiler determines whether branch is likely to be taken or likely to be not taken.
- How? When is a branch likely to be taken? When is a branch likely to be NOT taken? 2 int gtz=0; int i = 0; while (i < 100) { x = a[i]; if (x == 0) continue; gtz++; }
Inf3 Computer Architecture - 2017-2018 (^4)
Dynamic Branch Prediction
▪ Monitor branch behavior and learn
- Key assumption: past behavior indicative of future behavior ▪ Predict the present (current branch) using learned history ▪ (^) Identify individual branches by their PC or dynamic branch history ▪ (^) Predict:
- Outcome: taken or not taken
- Target: address of instruction to branch to ▪ (^) Check actual outcome and update the history ▪ Squash incorrectly fetched instructions
Inf3 Computer Architecture - 2017-2018 (^5)
Simplest dynamic predictor: 1-bit Prediction
▪ 1 bit indicating Taken (1) or Not Taken (0) ▪ Branch prediction buffers:
- Match branch PC during IF or ID stages ▪ Incurs at least 2 mis-predictions per loop Branch PC 0x135c 0x147e … Outcome 0 1 … … 0x135c4: add r1,r2,r 0x135c8: bne r1,r0,n … while (i < 100) { x = a[i]; if (x == 0) continue; gtz++; }
Problem: “unstable” behavior
Inf3 Computer Architecture - 2017-2018 (^7)
2-bit (Bimodal) Branch Prediction
▪ Predictor states: ▪ Learns biased branches ▪ N-bit predictor:
- Increment on Taken outcome and decrement on Not Taken outcome
- If counter>(2n-1)/2 then take, otherwise do not take
- Takes longer to learn, but sticks longer to the prediction Predict taken (10) Predict not taken (01) Taken Taken Predict taken (11) Predict not taken (00) Not taken Taken Not taken Taken Not taken Not taken
Inf3 Computer Architecture - 2017-2018 (^8)
Example of 2-bit (Bimodal) Branch Prediction
▪ Nested loop: ▪ 1 st^ outer loop execution:
- 00 →^ predict not taken; actually taken^ →^ update to 01^ (misprediction)
- 01 → predict not taken; actually taken → update to 10 (misprediction)
- 10 →^ predict taken; actually taken^ →^ update to 11
- 11 → predict taken; actually taken
- …
- 11 → predict taken; actually not taken → update to 10 (misprediction) Loop1: … … Loop2: … bne r1,r0,loop … bne r2,r0,loop
Inf3 Computer Architecture - 2017-
Bimodal (2-bit) predictor logic
10
Targets
BTB
PC
Tags
(optional)
Hit? Taken?
PC + 4
Next
Fetch
Address
P
H
T
Target
Inf3 Computer Architecture - 2017-
Correlating Predictors
▪ 1- and 2-bit predictors exploit most recent
history of the current branch
▪ Realization: branches are correlated!
▪ Local: A branch outcome maybe correlated
with past outcomes (multiple outcomes or
history, not just the most recent) of the same
branch
▪ Global: A branch outcome maybe correlated
with past outcomes of other branches
11
Inf3 Computer Architecture - 2017-
Correlating Predictors (Global)
▪ 1- and 2-bit predictors exploit most recent
history of the current branch
▪ Realization: Different branches maybe correlated!
13 *if (a == 2) a = 0; if (b == 2) b = 0; if (a != b) { …} If both branches are taken, the last branch definitely not taken char s1 = “Bob” ... if (s1 != NULL) reverse_str(s1); reverse_str(char s) { if (s1 == NULL) return; ... s1 definitely not Null in this calling context
Inf3 Computer Architecture - 2017-
Correlating Predictors (Global)
▪ 1- and 2-bit predictors exploit most recent
history of the current branch
▪ Realization: Different branches maybe correlated!
14 *if (a == 2) a = 0; if (b == 2) b = 0; if (a != b) { …} char s1 = “Bob” ... if (s1 != NULL) reverse_str(s1); reverse_str(char s) { if (s1 == NULL) return; ...
Idea: exploit recent history of other branches in prediction
Inf3 Computer Architecture - 2017-
Dynamic Predictor Performance Comparison
2-level predictor improves accuracy by >4%
16 2-level correlating predictor with a 15-bit global history
Inf3 Computer Architecture - 2017-
Does 4% accuracy improvement matter?
17 ▪ (^) Assume branches resolve in stage 10
- Reasonable for a modern high-frequency processor ▪ (^) 20% of instructions are branches ▪ (^) Correctly-predicted branches have a 0-cycle penalty (CPI=1) ▪ (^) 2-bit predictor: 92% accuracy ▪ (^) 2-level predictor: 96% accuracy 2-bit predictor: CPI = 0.8 + 0.2 * (100.08 + 10.92) = 1. 2-level predictor CPI = 0.8 + 0.2 * (100.04 + 10.96) = 1.
Speedup(2-level over 2-bit): 4%
Inf3 Computer Architecture - 2017-
Branch Prediction Logic of global 2-level predictor
Source: Onur Mutlu, CMU 19 0 1
Inf3 Computer Architecture - 2017-
Bimodal and Global 2-level
▪ Bimodal (2-bit) Branch Predictor
- Good for biased branches
- No interference
▪ Global 2-level Branch Predictor
- Leverages correlated branches
- Identifies patterns
- Cannot always take advantage of biased branches
- Interference 20