Skip to content

Branch predictor

gem5 only, with cpu_model: DerivO3CPU. Fault models: none apply, because this fault removes entries instead of flipping bits. fault: branch_pred.

A processor that runs ahead of its branches has to guess where each one goes. For a function return, the guess comes from the return address stack (RAS): every call pushes the address it will come back to, and every return pops the top entry and fetches from there before the return itself executes. The branch_pred target corrupts this stack. At one instant of the run, HFIM pops one to three entries off it, so the next returns are predicted toward the wrong caller. As in every HFIM campaign, each run applies one fault, and HFIM compares the result with a fault-free golden run of the same program.

Unlike most HFIM targets, nothing is bit-flipped. The fault is a structural change to the stack, recorded as branch_pred.ras_pop so you can tell it apart from faults that change a stored value.

Two return address stacks side by side. In the golden run the top entry, 0x80000a14, predicts the next return. After the fault, HFIM has popped the top two entries, so the next return is predicted to the outer caller's address 0x80000100. Two return address stacks side by side. In the golden run the top entry, 0x80000a14, predicts the next return. After the fault, HFIM has popped the top two entries, so the next return is predicted to the outer caller's address 0x80000100.

How HFIM applies the fault

The instant is a committed-instruction count drawn from the campaign seed, as for every gem5 target. When the CPU commits that instruction, the gem5 controller looks up the branch predictor of the out-of-order CPU model DerivO3CPU and takes the return address stack of hardware thread 0.

If the stack holds at least one entry, the controller records the address at its top, pops between one and three entries, and records the new top. When the pops empty the stack, the new top is recorded as 0x0. The number of pops comes from gem5's own random generator, not from the campaign seed.

The popped entries are simply gone from the stack's point of view. The next return reads what was a deeper entry before the pops, the return address of an outer caller, and the CPU starts fetching there. Each extra pop shifts one more return onto the wrong address.

A wrong guess is a misprediction, not a wrong result. The out-of-order CPU checks every predicted return when the return instruction executes, discards the instructions it fetched on the wrong path, and continues from the real return address, which the program keeps in the ra register and on its own stack in memory. The fault therefore costs time: discarded instructions and extra cycles. The program's data and control flow stay the same.

The controller tries once per run. If the stack is empty at the drawn instant, or the CPU is not DerivO3CPU, nothing is changed and the run records no fault (see Limits).

A campaign

The gem5 template does not ship a campaign for this target. This one fits the template's Fibonacci benchmark, whose 64-number result array is the output HFIM compares. Save it beside the other Fibonacci campaigns as campaigns/fibonacci/branch_pred.yaml:

include: fibonacci.yaml

campaign_name: fibonacci-branch_pred

simulator_backend: gem5
cpu_model: DerivO3CPU

seed: 7
num_injections: 10
num_workers: 1

target_types: [branch_pred]
fault_model: single_bit_flip

timeout: 120
output_dir: results

The template's board sets MinorCPU, an in-order model, so the campaign states cpu_model itself; a key the campaign states wins over the board. fault_model is required for every campaign, but this target ignores it. Run the campaign from the template folder:

hfim run campaigns/fibonacci/branch_pred.yaml

What you see in the results

Each run writes one row of injections.csv, the comma-separated values (CSV) file of the campaign, and one folder, injection_NNNN/. In the row, target_type is branch_pred. The value columns hold return addresses, not data:

Column For branch_pred
original_value the address at the top of the stack before the pops
faulty_value the address at the top after the pops, or 0x0 when the stack is empty
bit_position drawn by the faultlist like for every target, but not used: no bit changes

The run's injection_report.json has "target": "branch_pred.ras_pop". Neither the report nor the CSV says how many entries were popped; that count only appears in gem5's debug output.

Because the CPU corrects every misprediction, a run where the fault lands should end masked: the program writes the same result as the golden run. The fault shows up in the time the run takes, not in the outcome. An sdc (silent data corruption, a wrong result with no error reported), detected or crash would mean the misprediction reached committed state, which the CPU model is designed to prevent. A timeout means the run went past the wall-clock limit set by timeout. Every outcome: Fault classification.

Keys

Key Values Default
fault (or target_types) branch_pred required
cpu_model DerivO3CPU the board's, else AtomicSimpleCPU
fault_model any accepted value (single_bit_flip, stuck_at_0, stuck_at_1); it is ignored required

Limits

  • DerivO3CPU only, and no early refusal. On another CPU model HFIM does not stop the campaign. Each run starts, gem5 warns UltraInjector failed: Branch predictor injection requires DerivO3CPU, writes no injection report, and the row is infrastructure_failure.
  • An empty stack records no fault. If the stack holds no return address at the drawn instant, gem5 warns UltraInjector failed: RAS is empty -- no return addresses to corrupt, and the row is infrastructure_failure. The run is not retried: HFIM tries once per run.
  • Only the return address stack. The direction predictor and the branch target buffer, the other parts of a branch predictor, are never touched.
  • gem5's predictor, not the board's. The predictor is the default one of gem5's DerivO3CPU. The branch_predictor entry in a board file describes the real chip for a reader; gem5 does not build it.
  • Thread 0 only. The fault always hits the return address stack of hardware thread 0 of the CPU the controller watches.
  • No fault model, no seed for the pop count. Single-bit, multi-bit and stuck-at do not apply, fault_persistence: permanent has no effect, and the one to three pops are drawn by gem5, so the seed fixes the instant but not how deep the stack is cut.

See also: gem5 backend · Reorder buffer · Fault targets · Configuration keys