5.3.2 · HinglishAdvanced Microarchitecture

Out-of-order execution

2,883 words13 min readRead in English

5.3.2 · Hardware › Advanced Microarchitecture

Out-of-order execution kya hai?

YEH kyun chahiye?

  • In-order processors tab stall ho jaate hain jab unhe data dependency ya cache miss milti hai
  • Modern instructions ki latencies bahut alag hoti hain: register ops (1 cycle), L1 cache (4 cycles), memory (100+ cycles)
  • OoOE ke bina, CPU cycles waste karta hai wait karte hue, chahe independent kaam maujood ho

YEH kaise kaam karta hai? CPU instruction lifecycle ko alag phases mein separate karta hai jo independently operate kar sakte hain:

  1. Fetch/Decode (in-order): Instructions pipeline mein enter karte hain
  2. Dispatch (in-order): Instructions reservation stations mein allocate hoti hain
  3. Execute (OUT-OF-ORDER): Instructions tab fire hoti hain jab operands ready hon
  4. Commit/Retire (in-order): Results program order mein write back hote hain

Tomasulo algorithm ka foundation

Modern OoOE Tomasulo's algorithm (IBM 360/91, 1967) se trace hoti hai. Chaliye first principles se key insight derive karte hain.

Problem: Register renaming aur dynamic scheduling.

Yeh code consider karo:

R1 = R2 + R3    ; (I1)
R4 = R1 * R5    ; (I2) - I1 par depend karta hai
R1 = R6 - R7    ; (I3) - I1 ke saath WAW hazard
R8 = R1 + R9    ; (I4) - I3 par depend karta hai, I1 par NAHI

In-order execution: I2 waits for I1, I3 waits for I2, I4 waits for I3 → serial execution.

Insight: R1 ke do uses alag logical values hain. Agar hum unhe rename karein:

T1 = R2 + R3    ; (I1) renamed
R4 = T1 * R5    ; (I2) 
T2 = R6 - R7    ; (I3) renamed
R8 = T2 + R9    ; (I4)

Ab I3 aur I1 parallel mein execute ho sakte hain! I4 sirf I3 par depend karta hai, I1 par nahi.

YEH step kyun? False dependencies todne se true data-flow graph expose hota hai. CPU ab koi bhi instruction issue kar sakta hai jiske inputs ready hain.

Reservation stations aur execution

Ready condition ka derivation:

Maano instruction ko operands chahiye. Har operand ya toh:

  • Ready hai: value reservation station mein hai
  • Waiting hai: producer instruction ke tag ke saath

Jab producer complete hota hai, woh CDB par (tag: Pj, value: V) broadcast karta hai. Sare reservation stations CDB snoop karte hain aur:

Broadcast kyun? Multiple instructions ek hi result ka wait kar sakti hain. Sare stations ko parallel mein broadcast karna sequential wakeup delays se bachata hai.

Reorder buffer (ROB)

Problem: Out-of-order execution out-of-order completion create karta hai. Lekin humein precise exceptions chahiye: agar instruction 5 fault kare, toh instructions 1-4 commit honi chahiye, 6+ nahi.

Commit logic ka derivation:

ROB entries ko maano program order mein. Head par:

Agar ho:

  • ke baad ke sab entries flush karo
  • Architectural state ko ke input par roll back karo
  • OS ko exception signal karo

In-order commit kyun? Yeh illusion maintain karne ke liye ki instructions sequentially execute hue. External observers (memory system, interrupts, debuggers) results aise dekhte hain jaise program in-order chala ho.

Common mistakes

Memory model interaction

OoOE multi-core ke saath kaise interact karta hai:

Har core ka OoOE engine single-threaded illusion maintain karta hai. Lekin caches shared/coherent hain. Memory consistency model define karta hai ki kaunse reorderings cores ke beech visible hain.

x86 TSO ke liye, allowed aur forbidden reorderings yeh hain:

  1. Loads older loads ke saath reorder NAHI hote (Load→Load order preserved)
  2. Loads alag address par older stores ko pass KAR SAKTE hain (Store→Load reordering allowed; yeh woh ek relaxation hai jo TSO permit karta hai)
  3. Stores older stores ke saath reorder NAHI hote (Store→Store order preserved, commit par enforce hota hai)
  4. Stores older loads ke saath reorder NAHI hote (Load→Store order preserved)

Yeh rules kyun? Performance balance karna (common Store→Load relaxation ko store buffer ke through allow karna) vs. programmer sanity (baaki jagah intuitive ordering preserve karna).

Performance metrics

ILP wall: typical programs mein average ~2-3 independent instructions per cycle hoti hain. Perfect OoOE bhi is limit ko hit karta hai. Modern CPUs ise compensate karte hain:

  • Wider issue (4-6 wide dispatch)
  • Deeper speculation (branch prediction)
  • Simultaneous multithreading (SMT/Hyper-Threading)

Connections

Recall Ek 12-saal ke bacche ko explain karo

Socho tum homework kar rahe ho 5 problems ke saath. Problem 1 ke liye library se ek book chahiye (tumhare parent 10 minute mein laayenge). Problem 2, 3, 4 easy math hai jo tum abhi kar sakte ho. Problem 5 ko problem 1 ka answer chahiye.

Buri strategy (in-order): Problem 1 ka wait karo (10 min), karo, phir 2, 3, 4, phir 5. Total: 10 + 1 + 1 + 1 = 14 minutes.

Smart strategy (out-of-order): Problems 2, 3, 4 abhi karo (3 min). Jab book aaye, 1 karo (1 min), phir 5 (1 min). Total: 3 + 1 + 1 = 5 minutes.

Tum 3× faster finish karte ho! CPU bhi yahi karta hai: slow memory ka wait karte waqt ("library book"), woh dusri instructions par kaam karta hai ("easy math"). Final answers sahi order mein rakhta hai taaki teacher (programmer) ko kabhi pata na chale ki tumne out-of-order kiya.

#flashcards/hardware

In-order aur out-of-order execution mein key difference kya hai?
In-order instructions ko strictly program order mein execute karta hai, dependencies par stall karta hai. Out-of-order execution ko dynamically reorder karta hai taaki instructions tab issue hon jab operands ready hon, saath mein correct architectural state ke liye in-order commit maintain karta hai.
Teen types ke hazards kya hain, aur register renaming kaun se eliminate kar sakta hai?
RAW (Read-After-Write) = true dependency, eliminate nahi ho sakta. WAR (Write-After-Read) aur WAW (Write-After-Write) = false dependencies jo register reuse se hoti hain, alag physical registers par rename karke eliminate ho jaati hain.
Reorder buffer (ROB) ka purpose kya hai?
ROB in-order commit maintain karta hai chahe execution out-of-order ho. Yeh precise exceptions enable karta hai fault ke baad speculative instructions flush karke, aur ensure karta hai ki architectural state program order mein update ho.
Ek reservation station ko kaise pata chalta hai ki instruction execute ke liye ready hai?
Ek instruction ready hoti hai jab sab source operands ya toh (1) values ke saath already present hain, ya (2) unke producer tags Common Data Bus (CDB) par broadcast ho chuke hain. Station CDB snoop karta hai aur results aane par operands update karta hai.
x86 TSO ke under, kaun sa ek reordering allowed hai?
Sirf alag addresses par Store→Load reordering: ek load ek older store ko alag address par pass kar sakta hai (store buffer ke through). Load→Load, Store→Store, aur Load→Store orderings sab preserve hote hain.
Infinite instruction window size ke saath bhi IPC scaling ko kya limit karta hai?
Critical path (longest dependency chain) aur Amdahl's law. Programs mein data dependencies ki wajah se limited instruction-level parallelism hoti hai, isliye unlimited resources bhi serial bottlenecks ko parallelize nahi kar sakta. Typical saturation IPC ≈ 2-4 par hoti hai.

Concept Map

stalls on

motivates

based on

maintains

uses

removes

cannot remove

managed by

maps arch to

exposes

feeds

fire when ready via

results committed

In-order execution

Data dependency or cache miss

Out-of-order execution

Tomasulo algorithm

Architectural state illusion

Register renaming

False dependencies WAR WAW

True dependency RAW

Register Alias Table

Physical registers

True data-flow graph

Reservation stations

Common Data Bus

Commit and retire in-order