5.3.7 · HinglishAdvanced Microarchitecture

Branch prediction (static and dynamic)

3,172 words14 min readRead in English

5.3.7 · Hardware › Advanced Microarchitecture

Branch Ki Problem

Ek pipelined processor mein, fetch stage next instruction ko pehle grab kar leti hai jab se pichla branch resolve ho. Consider karo:

100: CMP R1, #5
104: BEQ target    ; branch if equal
108: ADD R2, R3    ; fall-through path
...
200: SUB R4, R5    ; target path

Jab tak hum cycle 2 mein BEQ ko decode karte hain, hum 108 ki instruction already fetch kar chuke hain (cycle 1). Jab tak hum compare execute karte hain aur branch outcome pata chalta hai (cycle 5), hum 4-5 aur instructions fetch kar chuke hote hain.


Static Branch Prediction

Static prediction ek fixed rule use karta hai jo design time ya compile time par decide hoti hai—koi runtime learning nahi hoti.

Common Static Schemes

  1. Predict Not-Taken (PNT)

    • Hamesha fall-through instruction (PC + 4) fetch karo.
    • Kyun?: Simple hardware, koi state ki zaroorat nahi.
    • Kahan kaam karta hai: Forward branches ke liye (aksar not-taken, jaise error checks if (rare_error) goto handler).
  2. Predict Taken (PT)

    • Hamesha branch target fetch karo.
    • Kahan kaam karta hai: Backward branches ke liye (loops! for aur while zyada tar kai baar iterate karte hain, toh backwards jump 90%+ baar liya jaata hai).
  3. Backwards Taken, Forward Not-Taken (BTFNT)

    • Compiler hint: Agar branch target address < PC hai, toh predict taken. Warna, not-taken.
    • Accuracy: Typical code ke liye ~60-70% (loops mein branches dominant hote hain).

Dynamic Branch Prediction

Dynamic prediction adapt karne ke liye runtime history use karta hai. CPU ko yaad rehta hai "pichli baar jab maine yeh branch dekha, yeh taken/not-taken gaya tha."

1-Bit Predictor (Branch History Table)

Structure: Ek table jo PC ke low-order bits (branch address) se index hoti hai. Har entry ek 1-bit state hai:

  • 0 = Predict Not-Taken
  • 1 = Predict Taken

Update rule: Branch resolve hone ke baad, bit ko actual outcome par set karo.

2-Bit Saturating Counter (Bimodal Predictor)

1-bit predictor ki loop problem fix karne ke liye, hysteresis ke saath 2-bit counter use karo (behavior badalne ke liye do mispredictions chahiye).

States:

  • 00 = Strongly Not-Taken (SNT)
  • 01 = Weakly Not-Taken (WNT)
  • 10 = Weakly Taken (WT)
  • 11 = Strongly Taken (ST)

Prediction rule: Agar counter ≥ 10 ho toh Taken predict karo, warna Not-Taken.

Update rule: Taken par increment karo (11 par saturate), not-taken par decrement karo (00 par saturate).

Correlating Predictors (Two-Level Adaptive)

Insight: Ek branch ka outcome aksar recent branch history par depend karta hai, sirf apne khud ke past par nahi.

Example:

if (a > 0) {        // Branch B1
    if (b > 0) {    // Branch B2
        ...
    }
}

Agar B1 not-taken hai, toh B2 kabhi execute nahi hoga. Agar B1 taken hai, toh B2 ka outcome b par depend karta hai. Ek correlating predictor ek global history register (GHR) use karta hai jo last branch outcomes track karta hai, phir (PC, GHR) se ek table index karta hai.

Tournament Predictor (Hybrid)

Problem: Koi ek predictor sabhi branches ke liye nahi jeetta. Loops bimodal ko pasand karte hain (local history). Correlated code correlating predictors ko pasand karta hai.

Solution: Tournament predictor multiple predictors parallel mein chalata hai (jaise ek local 2-bit predictor + ek global correlating predictor), phir ek meta-predictor (ek aur 2-bit counter) use karta hai jo choose karta hai ki har branch ke liye kaun sa predictor trust karna hai.


Comparison: Static vs Dynamic

Scheme Accuracy (typical) Hardware Cost Cold Start Aliasing
Static BTFNT 60-70% Zero (just decode) N/A N/A
1-bit BHT 70-80% Small (1-bit/entry) Bad Yes
2-bit Bimodal 85-90% Small (2-bit/entry) OK Yes
(2,2) Correlating 90-93% Medium (4× bimodal) OK Yes
Tournament (Alpha 21264) 95-97% Large (3 tables) Good Reduced

Aliasing: Multiple branches ek hi BHT entry par hash ho jaate hain, ek doosre mein interfere karte hain. Bade tables isse reduce karte hain.



Recall Feynman Explanation (age 12)

Socho tum ek choose-your-own-adventure book padh rahe ho, lekin bahut tezi se—tum page 10 par ho, lekin tumne pehle se page 11, 12, 13 sab ek saath padh liye hain. Phir page 10 kehta hai "Agar tumne talwar chuni, toh page 50 par jao. Agar tumne dhaal chuni, toh page 60 par jao."

Oh no! Tumhe nahi pata ki tumhe kaun sa page padhna chahiye tha. Tumne galat pages padhne mein waqt barbaad kar diya.

Branch prediction CPU ka guess hai: "Mujhe lagta hai unhone talwar chuni (kyunki pichli baar unhone ki thi), toh main abhi page 50 padhna shuru karta hoon." Agar guess sahi hua, toh waqt bacha. Agar galat hua, toh pages 11-13 phenk do aur page 60 se naya shuru karo (yahi pipeline flush hai).

Static prediction: "Main hamesha talwar guess karta hoon" (ek fixed rule). Dynamic prediction: "Mujhe tumhare pichle 5 choices yaad hain, aur main uss pattern se guess karta hoon."

CPU guessing mein bahut acha ho jaata hai (95%+ sahi), toh book bahut tezi se aage chalti hai!



Connections

  • Pipeline Hazards: Branch prediction control hazards solve karta hai (yeh nahi pata ki agla instruction kahan se fetch karein).
  • Speculative Execution: Prediction speculation enable karti hai; mispredictions ke liye rollback mechanisms chahiye.
  • Instruction-Level Parallelism (ILP): High ILP ke liye high prediction accuracy chahiye (zyada instructions in-flight = misprediction ka zyada cost).
  • Cache Performance: Wrong-path instructions instruction cache pollute karte hain; high accuracy predictors isse reduce karte hain.
  • Superscalar Processors: Ek cycle mein multiple issue branch penalties amplify karta hai; advanced predictors critical hain.
  • Compiler Optimizations: Profile-guided optimization (PGO) static hints insert kar sakta hai ya code reorder kar sakta hai predictors ki madad ke liye.

#flashcards/hardware

Pipelined CPU mein branch penalty kya hota hai? :: Jab branch misprediction hoti hai tab ke wasted cycles ki sankhya, usually 5-10 cycles (pipeline ki depth flush karni padti hai aur sahi path se restart karna padta hai).

Static "backwards taken, forward not-taken" kyun achha kaam karta hai?
Loops (backward branches) kai baar iterate karte hain (90%+ baar taken), jabki forward branches (error checks) zyada tar not-taken hote hain. Yeh heuristic typical code ke natural behavior ko exploit karta hai.
1-bit aur 2-bit branch predictor mein kya fark hai?
1-bit predictor har misprediction par prediction badal deta hai (loop exits par oscillate karta hai). 2-bit predictor ke liye do consecutive mispredictions chahiye flip karne ke liye, jo hysteresis provide karta hai aur loop behavior stable karta hai.
Correlating (two-level) predictor bimodal predictor se kaise better hai?
Yeh recent branch outcomes track karne ke liye ek global history register (GHR) use karta hai, jisse yeh liye gaye path ke basis par alag prediction tables select kar sakta hai. Yeh branch correlation capture karta hai (jaise nested ifs jahan ek branch ka outcome doosre ko affect karta hai).
Tournament predictor kya karta hai?
Yeh multiple predictors (jaise local bimodal + global correlating) parallel mein chalata hai aur ek meta-predictor (selector) use karta hai jo choose karta hai ki har branch ke liye kaun sa trust karna hai, yeh seekhte hue ki har branch pattern ke liye kaun sa predictor best kaam karta hai.
Ek 2-bit predictor sabhi loop mispredictions kyun eliminate nahi kar sakta?
Loop exit har loop invocation mein ek baar hamesha mispredict hoti hai—counter "strongly taken" par saturate ho jaata hai, toh not-taken exit mispredict hoti hai. Yeh 1 error per loop hai, jo lambe loops ke liye ~1% hai lekin chhote loops ke liye ~33% hai.
Branch prediction mein aliasing kya hota hai?
Jab multiple branches limited table size ki wajah se ek hi BHT entry par hash ho jaate hain, toh yeh ek doosre ke seekhe hue patterns mein interfere karte hain, accuracy reduce karte hain. Bade tables ya tagged entries aliasing reduce karte hain.
Branch mispredictions ke saath average CPI ka formula kya hai?
CPI = 1 + P_branch × P_mispredict × Penalty. Example: 20% branches, 10% mispredict rate, 10-cycle penalty → CPI = 1.2 (20% slowdown).

Concept Map

resolve se pehle fetch karta hai

wajah banta hai

wasted flush cycles

solve hota hai

speculatively fetch karo

fixed rule

runtime learning

fall-through PC+4

branch target

rules combine karta hai

backward loops ke liye achha

forward checks ke liye achha

accuracy ~60-70%

Pipelined Fetch

Branch Problem

Branch Penalty

Higher CPI

Branch Prediction

Static Prediction

Dynamic Prediction

Predict Not-Taken

Predict Taken

BTFNT