5.3.11 · D3 · Hardware › Advanced Microarchitecture › Speculative execution
Yeh Speculative execution ka hands-on companion hai. Parent note ne bataya tha ki speculation kya hai aur CPUs branches pe kyun gamble karte hain. Yahan hum arithmetic drill karte hain: har tarah ke branch ke liye jo ek program CPU ko de sakta hai, hum exact cycle cost compute karte hain, decide karte hain ki speculation jeetta hai ya haarta hai, aur prove karte hain.
Numbers chhhune se pehle, chaar quantities saara kaam karti hain. Har symbol ko samjhte hain.
Definition Chaar numbers jo har example use karta hai
Pipeline depth D = woh stages ki sankhya jinse ek instruction guzarta hai jis waqt se usse fetch kiya jaata hai us waqt tak jab woh commit karta hai (apna result architectural state mein likhta hai). Ek conveyor belt imagine karo jisme D workstations fixed order mein hain — fetch, decode, rename, issue, execute, … , commit. Kisi bhi waqt belt pe D alag-alag instructions ho sakti hain, har ek alag workstation pe ruki hui. Do clarifications jo baad mein matter karenge: (i) D saare stages count karta hai including fetch aur commit, sirf execution nahi; (ii) ek instruction belt se tabhi jaati hai (apna Reorder buffer (ROB) slot free karti hai) jab final commit stage aata hai, yahi wajah hai ki speculative results belt pe half-finished baith sakti hain aur cheaply throw away ho sakti hain. Agar D = 15 ho, toh belt pe ek waqt mein 15 partially-done instructions hoti hain.
Misprediction penalty P = woh cycles jo galat guess hone par waste hote hain (neeche derive kiya hai). Parent note se, P = D + R jahan R hai refetch latency (cache se sahi instruction grab karne ke 1–3 cycles). Hum P = 17 use karte hain (matlab D = 15 , R = 2 ) jab tak kuch aur na bataya jaaye.
Prediction accuracy a = woh fraction of branches jo sahi guess kiye jaate hain, 0 aur 1 ke beech ki ek number. Iska partner hai miss rate m = 1 − a .
Stall cost S = woh cycles jo ek CPU waste karta hai agar woh guess karne se mana kar de aur instead branch condition resolve hone tak wait kare. Khaas baat yeh hai ki S ek free parameter nahi hai: yeh us pipeline stage se fix hota hai jo branch condition compute karta hai. Us stage ko resolve stage r kaho (fetch stage ko stage 1 maante hue). Tab S = r − 1 : CPU agla sahi instruction tab tak fetch nahi kar sakta jab tak stage r jawab na de, isliye woh beech ke r − 1 cycles idle rehta hai. Jo branch jaldi resolve hoti hai (pipeline ke front ke paas) us ka S chhota hota hai; jo late resolve hoti hai uska bada. Yahi wajah hai ki S examples mein alag-alag hota hai — yeh track karta hai ki branch pipeline mein kahan hai, koi arbitrary choice nahi.
Ek derived idea a , m aur P ko ek saath baandhta hai: average cost per branch .
avg cost = m × P = ( 1 − a ) P
Multiply kyun karte hain? Sirf woh fraction jo miss karte hain penalty P pay karte hain; jo sahi hain woh 0 extra pay karte hain. "Kitni baar pay karte hain" ko "kitna pay karte hain" se multiply karna expected (average) cost ki definition hai.
Intuition Woh ek comparison jo har decision ke peeche hai
Speculation tab hi worth it hai jab guess karne ki average penalty chhoti ho safe alternative — S cycles stall karne — se zyada. Isliye har example yahi comparison karne pe aata hai:
gamble ( 1 − a ) P vs. safe khelna S
Agar gamble chhota hai, speculate karo. Agar bada hai, stall karo. Yaad raho S = r − 1 : yeh hardware ke resolve stage se set hota hai, isliye har example r (ya S ) explicitly batata hai.
Definition 2-bit saturating counter (Example 2 mein use hota hai)
Ek branch predictor ko ek chhoti memory chahiye "is branch ne pichli baar kya kiya?". Sabse simple useful wala hai 2-bit saturating counter : ek single number jo chaar states hold kar sakta hai, most-confident-taken se most-confident-not-taken tak:
3 = strongly taken , 2 = weakly taken , 1 = weakly not-taken , 0 = strongly not-taken
Rules: branch actually taken hone ke baad, 1 add karo (lekin 3 se upar kabhi nahi); not-taken hone ke baad, 1 subtract karo (0 se neeche kabhi nahi). "Saturating" ka matlab hai yeh ends pe stick karta hai — 3 se overflow ya 0 se under nahi ho sakta. Prediction rule: counter ≥ 2 hone par taken predict karo, ≤ 1 par not-taken .
Do bits kyun, ek kyun nahi? Ek 1-bit predictor har single miss pe apna guess flip karta hai, isliye woh loop mein do baar mispredicit karta hai (ek enter karte waqt, ek exit karte waqt). Do bits hysteresis add karte hain: ek surprise counter ko nudge karta hai lekin prediction flip nahi karta, isliye ek anomaly (jaise loop exit) sirf ek miss ka cost karti hai, do nahi. Yahi hysteresis hai poori wajah ki Example 2 mein exactly ek miss hai.
Branches infinite varieties mein nahi aate — woh kuch behaviour classes mein aate hain. Yeh table poori list hai. Har cell ka neeche worked example hai.
Cell
Case class
Kya khas baat hai
Example
A
Correct guess (m = 0 )
best case, penalty kabhi pay nahi
Ex 1
B
Single wrong guess lambi run mein
ek miss zyada hits mein amortise hota hai
Ex 2 (loop)
C
50/50 unpredictable (a = 0.5 )
worst realistic case, gamble haar sakta hai
Ex 3
D
Degenerate: a = 1
limiting value, penalty term vanish ho jaata hai
Ex 4
E
Degenerate: a = 0 (hamesha galat)
pathological, penalty har baar
Ex 4
F
Break-even point
exact accuracy jahan gamble = stall
Ex 5
G
Real-world word problem
real code mein mixed branch mix
Ex 6
H
Exam twist: deeper pipeline
P badhta hai, verdict badalta hai
Ex 7
I
Security cell (correctness, speed nahi)
Spectre leak count, cycles nahi
Ex 8
Ab har cell work karte hain.
Worked example Example 1 — Cell A: perfect guess
Ek branch taken predict ki gayi aur condition sach mein taken nikli. Pipeline depth D = 15 , penalty P = 17 . Ek magical CPU se jise jawab free mein pata tha, is branch ka extra cost kya hai?
Forecast: Aage padhne se pehle guess karo — cost 17 hai, 0 , ya kuch beech mein?
Miss rate identify karo. Guess sahi hai, isliye m = 0 . Yeh step kyun? m hi ek cheez hai jo penalty unlock karti hai; agar zero hai, baaki kuch matter nahi karta.
Average-cost formula lagao. cost = m × P = 0 × 17 = 0 cycles. Yeh step kyun? Formula kehta hai sirf misses pay karte hain; hit ka cost wahi hai jitna answer jaanna hota.
Interpret karo. Speculated instructions Reorder buffer (ROB) mein pehle se thi aur simply commit kar gayi. Koi flush nahi, koi refetch nahi.
Verify: Sanity check — ek correct prediction by definition perfect knowledge se cost mein alag nahi hoti, isliye 0 extra cycles sahi hai. Units: cycles × dimensionless fraction = cycles. ✓
Worked example Example 2 — Cell B: lambe loop mein ek miss
for ( int i = 0 ; i < 100 ; i ++ ) { sum += array [i]; }
Loop branch i < 100 99 baar taken hai aur not taken ek baar (exit). Penalty P = 17 . Yeh branch bahut last stage pe resolve hoti hai (r = 16 ek D = 15 pipeline mein, kyunki uski condition loop counter ka execution finish hone pe depend karti hai), jo stall cost S = r − 1 = 15 cycles deta hai — poora pipeline drain. Ek CPU se compare karo jo har iteration pe S = 15 cycles stall karta hai.
Forecast: Har strategy kitne total stall cycles burn karta hai?
Branches count karo. Branch har iteration ek baar execute hoti hai → 100 baar. Yeh step kyun? Cost branch count ke saath scale karti hai, isliye pehle yahi chahiye.
Misses count karo. 2-bit counter trace karo (upar define kiya hai). Yeh "weakly taken" (= 2 ) se shuru hota hai; har taken iteration 1 add karta hai aur do iterations mein 3 ("strongly taken") pe saturate ho jaata hai, aur iterations 0–98 ke liye taken sahi predict karta reh jaata hai. Final check pe i ban jaata hai 100 , toh branch not taken hai jab counter abhi bhi taken predict kar raha hai → exactly 1 miss . Kyunki counter mein hysteresis hai, woh us ek anomaly pe double-miss nahi karta. Yeh step kyun? Cell B ka poora point yahi hai: pattern regular hai sirf ek boundary ke alawa, aur 2-bit design guarantee karta hai ki us boundary ki cost ek miss hai, do nahi.
Speculation cost. 1 miss × 17 = 17 cycles total. Yeh step kyun? Penalty-per-miss ka direct application.
Stall cost. 100 branches × S = 100 × 15 = 1500 cycles. Yeh step kyun? Kyunki yeh branch last stage pe resolve hoti hai (S = 15 ), safe CPU har branch pe, hit ho ya na ho, poora pipeline depth idle karta hai.
Stall-cycle ratio. 17 1500 ≈ 88.2 : speculation lagbhag 88 × kam stall cycles waste karta hai hamesha-stalling se. Yeh step kyun? Yeh wasted cycles ka ratio hai, overall program speedup nahi — real end-to-end speedup chhota hoga kyunki useful kaam dono cases mein run karta hai. Hum isse sirf yeh dikhane ke liye report karte hain ki stall overhead kitna dramatically shrink hota hai.
Figure dekho: tall black bar stalling hai (1500), chhota red sliver speculation hai (17). Red bar hi poori wajah hai ki speculation exist karti hai.
Verify: 100 × 15 = 1500 ✓; 1 × 17 = 17 ✓; 1500/17 = 88.2 … ✓. Units: (branches)(cycles/branch) = cycles throughout. ✓
Worked example Example 3 — Cell C: coin-flip branch
if ( rand () % 2 == 0 ) process_even (); else process_odd ();
Accuracy a = 0.5 (pure guessing). Penalty P = 17 . Yahan condition (rand() % 2) jaldi compute hoti hai — value pehle se ek register mein baith rahi hai, isliye compare stage r = 3 pe resolve hoti hai, jo stall cost S = r − 1 = 2 cycles deta hai. Kaunsi strategy jeetti hai?
Forecast: Compute karne se pehle guess karo — kya speculation yahan help karta hai ya hurt karta hai?
Miss rate. m = 1 − a = 1 − 0.5 = 0.5 . Yeh step kyun? Coin flip mein aadhe guesses galat hote hain.
Gamble cost. ( 1 − a ) P = 0.5 × 17 = 8.5 cycles per branch on average. Yeh step kyun? Speculate karne ki expected penalty.
Safe cost. Kyunki yeh branch jaldi resolve hoti hai (stage r = 3 ), stalling sirf S = 2 cycles per branch cost karti hai, hamesha. Yeh step kyun? S seedha resolve stage se aata hai; jaldi resolve hone waali branch pe wait karna sasta hai.
Compare karo. 8.5 > 2 , isliye speculation HAART jaata hai 6.5 cycles per branch se. Yeh step kyun? Yeh naive belief "speculation hamesha acchi hai" ko tod deta hai — Cell C wahi hai jahan woh fail karti hai, precisely kyunki stall alternative yahan itna sasta hai.
Verify: 0.5 × 17 = 8.5 ✓; 8.5 − 2 = 6.5 ✓. Yahi wajah hai ki real chips confidence estimators ship karte hain jo stalling pe fall back karte hain jab predictor unsure ho. Units: cycles vs cycles. ✓
Worked example Example 4 — Cells D & E: do degenerate extremes
Same branch, same P = 17 , lekin do limiting accuracies. a = 1 (perfect) aur a = 0 (hamesha galat) pe average penalty compute karo.
Forecast: Do extremes pe, costs kya hain — aur kya a = 0 sach mein maximum possible penalty hai?
Cell D, a = 1 . m = 1 − 1 = 0 , isliye cost = 0 × 17 = 0 cycles. Yeh step kyun? Yeh limiting best case hai; penalty term puri tarah off ho jaati hai.
Cell E, a = 0 . m = 1 − 0 = 1 , isliye cost = 1 × 17 = 17 cycles har branch . Yeh step kyun? Yeh limiting worst case hai; har baar full penalty pay karo.
Poore formula ko bound karo. Kyunki m range karta hai [ 0 , 1 ] mein, average cost ( 1 − a ) P trapped hai [ 0 , P ] = [ 0 , 17 ] mein. Yeh step kyun? Yeh prove karta hai ki koi bhi branch kabhi full penalty P se zyada cost nahi kar sakti — ek useful sanity ceiling.
Verify: ( 1 − 1 ) ⋅ 17 = 0 ✓; ( 1 − 0 ) ⋅ 17 = 17 ✓; aur 0 ≤ 8.5 ≤ 17 Example 3 ko bound ke andar sahi jagah rakhta hai. ✓
Worked example Example 5 — Cell F: break-even accuracy
Example 3 jaisi same early-resolving branch lo, isliye stall cost S = 2 hai (resolve stage r = 3 ), P = 17 ke saath. Kis accuracy a pe speculation exactly us 2-cycle stall ke barabar hoti hai? Is se neeche, speculation net loss hai.
Forecast: Guess karo ki break-even accuracy 50% ke kareeb hai ya 90% ke.
Gamble = safe set karo. ( 1 − a ) P = S ⇒ ( 1 − a ) ⋅ 17 = 2 . Yeh step kyun? Break-even woh point hai jahan dono strategies same cost karti hain; hum wahi S = 2 use karte hain jo Example 3 mein tha kyunki yeh same branch hai.
Miss rate solve karo. 1 − a = 17 2 ≈ 0.1176 . Yeh step kyun? Miss rate pehle isolate karo; penalty usi ko multiply karti hai.
Accuracy solve karo. a = 1 − 17 2 = 17 15 ≈ 0.882 = 88.2% . Yeh step kyun? Miss rate ko waapas accuracy mein convert karo jo hardware quote karta hai.
Figure mein red line gamble cost ( 1 − a ) ⋅ 17 hai jo accuracy badhne se girti hai; black horizontal line fixed stall cost 2 hai. Woh 88.2% pe cross karti hain — cross se baayin taraf, stall karo; daayein taraf, speculate karo.
Verify: ( 1 − 15/17 ) ⋅ 17 = ( 2/17 ) ⋅ 17 = 2 ✓ equals S . Aur 15/17 = 0.882 … ✓. Yeh explain karta hai ki chips ko short-stall branch pe gamble justify karne ke liye > 88% accuracy kyun chahiye. ✓
Worked example Example 6 — Cell G: real-world mixed workload
Ek program 1 , 000 , 000 branches run karta hai. 80% loop/biased branches hain a = 0.98 par; 20% data-dependent branches hain a = 0.60 par. Penalty P = 17 . Mispredictions se kitne total cycles lose hote hain, aur overall miss rate kya hai?
Forecast: Overall accuracy guess karo — kya yeh 98% ke kareeb hai ya 60% group ke taraf khich jaata hai?
Branches split karo. Predictable: 0.80 × 1 0 6 = 800 , 000 . Data-dependent: 0.20 × 1 0 6 = 200 , 000 . Yeh step kyun? Har group ki apni miss rate hai, isliye hum unhe alag-alag cost karte hain.
Har group mein misses. Predictable: 800 , 000 × ( 1 − 0.98 ) = 16 , 000 . Data-dependent: 200 , 000 × ( 1 − 0.60 ) = 80 , 000 . Yeh step kyun? Miss count = branches × miss rate, per group.
Total misses & cycles. 16 , 000 + 80 , 000 = 96 , 000 misses; cost = 96 , 000 × 17 = 1 , 632 , 000 cycles. Yeh step kyun? Groups add karo, phir shared penalty apply karo.
Overall miss rate. 1 , 000 , 000 96 , 000 = 0.096 = 9.6% , toh overall accuracy = 90.4% . Yeh step kyun? Dikhata hai ki "chhota" 20% bad-branch slice misses pe dominate karta hai (96k mein se 80k), accuracy ko 98% se kaafi neeche kheench kar.
Verify: 16000 + 80000 = 96000 ✓; 96000 × 17 = 1632000 ✓; 96000/1 0 6 = 0.096 ✓. Note karo 80000 > 16000 — unpredictable minority zyaadatar dard cause karti hai. ✓
Worked example Example 7 — Cell H (exam twist): deeper pipeline
Ek exam tumhe 20-stage pipeline deta hai refetch R = 4 ke saath, isliye worst-case P = 20 + 4 = 24 . Ek branch mein accuracy a = 0.90 hai. Uski condition stage r = 4 pe resolve hoti hai — Examples 3 aur 5 ke register-ready compares jaisi wahi early resolve — kyunki woh bhi ek simple compare-and-jump hai jiske operands pehle se registers mein hain, isliye uski condition stage 4 tak pata chal jaati hai chahe baaki pipeline kitni bhi deep ho. Yeh stall cost S = r − 1 = 3 cycles deta hai. Kya deeper pipeline speculate/stall verdict badal deta hai?
Forecast: Deeper pipelines throughput ke liye "better" hain — lekin kya woh speculation ko safer banate hain ya riskier ?
Worst-case penalty. P = 20 + 4 = 24 cycles. Yeh step kyun? Deeper pipeline ⇒ zyada stages potentially flush hone ke liye ⇒ bada worst-case P . Hum P = 24 headline number ke taur pe use karte hain jo exam quote karta hai.
Stall cost. Branch stage r = 4 pe resolve hoti hai, isliye S = r − 1 = 3 cycles — yeh sirf resolve stage pe depend karta hai, total pipeline depth pe nahi. Yeh step kyun? Asymmetry note karo: pipeline deep karne se P blow up hua lekin S untouched raha, kyunki condition abhi bhi same early stage 4 pe resolve hoti hai.
Gamble cost. ( 1 − 0.90 ) × 24 = 0.10 × 24 = 2.4 cycles. Yeh step kyun? Same average-cost formula, naya P .
Stall se compare karo. 2.4 < 3 , isliye speculation abhi bhi jeetti hai — lekin sirf 0.6 cycles se. Yeh step kyun? Twist reveal karta hai: deeper pipelines P raise karte hain jabki S fixed rehta hai, speculation ka margin zero ki taraf shrink karta hai.
Shallow chip se contrast karo. P = 17 par same S = 3 ke saath: gamble = 0.10 × 17 = 1.7 < 3 , margin 1.3 . Yeh step kyun? Same accuracy aur same resolve stage, lekin deeper pipeline safety margin aadhe se zyada kar deta hai (1.3 → 0.6 ) — woh lesson jo exam test kar raha hai: deeper pipelines har misprediction ko zyada hurt karti hain, isliye woh ever-better predictors maangti hain.
Verify: 20 + 4 = 24 ✓; 0.10 × 24 = 2.4 ✓; 0.10 × 17 = 1.7 ✓; margins 3 − 2.4 = 0.6 aur 3 − 1.7 = 1.3 ✓. Deeper pipeline = higher misprediction stakes, unchanged stall cost. ✓
Worked example Example 8 — Cell I: security cell (correctness, cycles nahi)
Ek Spectre-style cache side-channel ek waqt mein ek byte leak karta hai. Har byte padhne ke liye attacker ko sahi se distinguish karna hota hai ki 256 possible values mein se kaun sa cache mein pull hua, timing probe use karke. Agar saare 256 cache lines probe karne mein 256 × 200 = 51 , 200 cycles per byte lagte hain, toh 16 -byte secret key leak karne mein kitne cycles lagte hain, aur yeh cell upar waale saare se kaise alag hai?
Forecast: Guess karo — kya yeh ek performance sawaal hai ya ek correctness sawaal?
Cycles per byte. Ek probe sweep = 256 × 200 = 51 , 200 cycles. Yeh step kyun? 256 possible byte values mein se har ek ek cache line se map hoti hai time karne ke liye; fast (already-cached) line value reveal karti hai.
Key ke liye total. 16 × 51 , 200 = 819 , 200 cycles. Yeh step kyun? Bytes independently leak hote hain, isliye per-byte cost ko key length se multiply karo.
Categorical difference. Cells A–H ne poochhaa "kya speculation fast hai?" Yeh cell poochhhta hai "kya speculation safe hai?" Chaar-numbers box se yaad karo ki ek speculative instruction belt se sirf commit pe jaati hai — isliye ek squashed wrong-path load commit nahi karta, lekin pehle hi ek secret-indexed line cache mein pull kar chuka hota hai. Woh footprint microarchitectural state mein rollback ke baad bhi survive karta hai aur timing se measure kiya ja sakta hai. Yeh step kyun? Cell I ka poora point yahi hai: architectural result bilkul sahi hai, aur speculation abhi bhi secret leak karti hai cache ke through — ek correctness/security failure jo program mein apne aap zero cycle-count symptom ke saath aata hai.
Figure mein, red cache line woh hai jise secret byte ne touch kiya — baaki har line cold (slow) hai; hot red line fast hai, aur uski position hi secret hai.
Verify: 256 × 200 = 51200 ✓; 16 × 51200 = 819200 ✓. Units: (lines)(cycles/line)(bytes) = cycles. Yahi wajah hai ki mitigations (lfence, retpolines, predictor flushing) exist karte hain chahe speculation kabhi architectural state corrupt na kare — parent note ki Spectre discussion dekho. ✓
Recall Quick self-test
Average penalty formula ::: ( 1 − a ) P , jahan a accuracy hai aur P misprediction penalty hai.
Precise vs headline penalty ::: Precise P = ( r − 1 ) + R ; headline P = D + R worst case hai jahan branch last stage pe resolve hoti hai (r − 1 = D ).
Stall cost S kya set karta hai ::: Resolve stage r jahan branch condition compute hoti hai; S = r − 1 . Jaldi resolve hone waali branches pe stall karna sasta hai, late ones pe mahanga.
2-bit counter loop exit pe sirf ek baar miss kyun karta hai ::: Uski hysteresis ka matlab hai ki ek surprise nudge karta hai lekin prediction flip nahi karta, isliye ek anomaly ek miss ki cost karti hai, wo do nahi jo ek 1-bit predictor karta.
Speculate-vs-stall rule ::: Speculate tabhi karo jab ( 1 − a ) P < S .
Break-even accuracy for P = 17 , S = 2 ::: a = 1 − 2/17 = 15/17 ≈ 88.2% .
Deeper pipeline speculation ke liye riskier kyun hai ::: Yeh worst-case P increase karta hai jabki S (resolve stage se set hota hai) fixed rehta hai, isliye same miss rate zyada cycles cost karti hai aur stalling se margin shrink ho jaata hai.
Kaunsa cell speed ke baare mein nahi correctness ke baare mein hai ::: Cell I — Spectre-style cache side channels; ek squashed speculative load abhi bhi cache footprint chhod jaata hai.
Mnemonic Poora page ek line mein
"Sirf misses pay karte hain, aur woh pipeline ko pay karte hain." Cost = ( 1 − a ) × P ; stall cost S = r − 1 — baaki sab numbers plug karna hai.
See also: Speculative execution · Out-of-order execution · Instruction-level parallelism (ILP) · Superscalar architecture · Pipeline hazards