Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 158 of 9362. Instruction Set Architecture and CPU MicroarchitectureFree lesson

Reorder Buffers for Precise Exceptions

Implement a reorder buffer to ensure instructions commit their results in program order while maintaining speculative execution benefits.

In this lesson, you will learn how the Reorder Buffer (ROB) acts as a safety net to ensure that even when instructions execute out-of-order, the CPU state remains consistent and recoverable.

The Challenge of Out-of-Order Execution

We’ve previously discussed how dynamic scheduling and the Tomasulo algorithm allow CPUs to execute instructions as soon as operands are ready, rather than in the strict order defined by your code. While this boosts performance, it creates a massive headache for precise exceptions. If an instruction causes an error (like dividing by zero) but other, later instructions have already finished and updated the registers, the CPU's state becomes "messy." We need a way to make the hardware look like it executed instructions one-by-one, even when it didn't.

The Reorder Buffer (ROB) Solution

The ROB is a hardware structure that sits at the end of the execution pipeline. It functions like a circular buffer that records the status of every "in-flight" instruction. When an instruction is dispatched, it is assigned a slot in the ROB in strict program order.

Think of the ROB as a two-stage gatekeeper:

  1. Speculative Execution: Instructions finish execution and write their results to the ROB entry (or a rename register), not the architectural register file.
  2. Commit (Retire): The CPU only updates the actual architectural state (registers and memory) when the instruction reaches the "head" of the ROB and is verified to have completed without exceptions.

A Conceptual Flow

Imagine this sequence:

1. ADD R1, R2, R3  (Executes fast)
2. DIV R4, R5, R6  (Executes slow, triggers error)
3. SUB R7, R8, R9  (Executes fast)

Without an ROB, the SUB might update R7 before the DIV finishes. With an ROB:

  • The ADD finishes and marks its result as "ready" in the ROB.
  • The SUB finishes and marks its result as "ready," but it cannot commit because the DIV (which came first) hasn't finished yet.
  • If the DIV triggers an exception, the CPU simply flushes the ROB entries after the DIV, preventing the SUB from ever permanently changing the machine state.

Key Takeaway: The Reorder Buffer ensures precise exceptions by buffering results out-of-order, but enforcing their update to the architectural state in strict program order.