Skip to content

Register Allocation 06.4 regalloc pbqp

Mugundan edited this page Nov 27, 2026 · 1 revision

PBQP Register Allocator

Relevant source files

The following files were used as context for generating this wiki page:

PBQP (Partitioned Boolean Quadratic Problem) turns register allocation into an optimisation problem on a graph and hands it to a solver. It is not the default on any target, but it is useful for machines with irregular constraints (register pairs, banks, restricted operand combinations) that are awkward to express as hints.

Purpose and Scope

This page explains the PBQP model, the allocator's main loop, and how a target adds its own constraints. The solver internals are out of scope.

The Model

PBQP element Means in register allocation
Node One virtual register
Node cost vector One entry per option: "spill" plus each allowed physical register. The spill entry is the spill weight; register entries are usually 0
Edge Two virtual registers that interact
Edge cost matrix Cost of each combination of choices. Interfering registers get infinite cost for the same (or overlapping) physical register. Copy-related registers get a negative or lower cost for the same register (coalescing)

The solver picks one option per node so the total cost is as small as possible. An infinite cost means "forbidden". Choosing "spill" for a node means that virtual register is spilled.

The Main Loop

The comment in runOnMachineFunction summarises it:

  // Allocator main loop:
  //
  // * Map current regalloc problem to a PBQP problem
  // * Solve the PBQP problem
  // * Map the solution back to a register allocation
  // * Spill if necessary
  //
  // This process is continued till no more spills are generated.
flowchart TD
  A["calculateSpillWeightsAndHints"] --> B["findVRegIntervalsToAlloc"]
  B --> C["build constraint list:<br/>SpillCosts, Interference,<br/>Coalescing (optional), target constraints"]
  C --> D["initializeGraph: one node per vreg"]
  D --> E["apply constraints to graph"]
  E --> F["PBQP::RegAlloc::solve"]
  F --> G["mapPBQPToRegAlloc:<br/>assign or spill each vreg"]
  G -->|"something was spilled"| D
  G -->|"no spills"| H["finalizeAlloc, postOptimization"]
Loading

Spilling creates new short intervals (around reloads), so the problem changes and the graph is rebuilt and solved again. The loop ends when a round produces no spills.

    ConstraintsRoot->addConstraint(std::make_unique<SpillCosts>());
    ConstraintsRoot->addConstraint(std::make_unique<Interference>());
    if (PBQPCoalescing)
      ConstraintsRoot->addConstraint(std::make_unique<Coalescing>());
    ConstraintsRoot->addConstraint(Subtarget.getCustomPBQPConstraints());
    ...
    while (!PBQPAllocComplete) {
      PBQPRAGraph G(PBQPRAGraph::GraphMetadata(MF, LIS, MBFI));
      initializeGraph(G, VRM, *VRegSpiller);
      ConstraintsRoot->apply(G);
      ...
      PBQP::Solution Solution = PBQP::RegAlloc::solve(G);
      PBQPAllocComplete = mapPBQPToRegAlloc(G, Solution, VRM, *VRegSpiller);
      ++Round;
    }

PBQP uses the same VirtRegMap, InlineSpiller and VirtRegRewriter as Greedy, so everything after allocation is identical.

Sources: llvm/lib/CodeGen/RegAllocPBQP.cpp:783-879, llvm/lib/CodeGen/RegAllocPBQP.cpp:579-773

Adding Target Constraints

A target can add its own costs by returning a PBQPRAConstraint from TargetSubtargetInfo::getCustomPBQPConstraints(). The constraint's apply(PBQPRAGraph &G) may change any node vector or edge matrix. Examples of what can be expressed:

  • "these two registers should be an even/odd pair": low cost on the matching pairs in the edge matrix;
  • "this operand is cheaper in the low 8 registers": lower node cost for those entries;
  • "these two must be in different banks": infinite cost for same-bank combinations.

This is the main reason to choose PBQP: costs between pairs of registers are first-class, whereas Greedy only has per-register hints.

Sources: llvm/include/llvm/CodeGen/TargetSubtargetInfo.h:349, llvm/lib/CodeGen/RegAllocPBQP.cpp:837

Trying It

llc -mtriple=aarch64 -O2 -regalloc=pbqp file.ll

On the 24-load spill test it produced 82 instructions (31 spill/reload/save lines) against Greedy's 83, a difference that is noise on this example. -pbqp-coalescing adds the coalescing constraint. In assertion builds, -pbqp-dump-graphs writes each round's graph to a file (see the PBQPDumpGraphs block in the source).

Sources: llvm/lib/CodeGen/RegAllocPBQP.cpp:98-104

When To Consider It

Use PBQP if Prefer Greedy if
Your target has strong pairwise register constraints Compile time matters
You want to experiment with cost models You want the best-tested, best-tuned code
Functions are small (the solver can be slow on big graphs) Functions are large

Start here

Computer Organization & Architecture

Hexagon ISA

Operating Systems

C++ for LLVM Development

LLVM IR

SelectionDAG

Backend Assignments

Target Bring-up

Calling Conventions

Frame Lowering

MC Layer

Register Allocation

Scheduling

Clone this wiki locally