sass2mlir / Manual

Register allocation

How the backend models register groups, checks coloring and audits writeback on current machine IR.

Backend guide

Allocation is part of a checked transformation

A SASS rewrite can change register pressure, introduce new values or change which values overlap. The allocator operates on the current MLIR function, using its control flow and dependencies. It handles the R, UR, P and UP classes, grouped operands, alignment, fixed inputs and instruction-field limits.

Pipeline

From SSA values to physical slots

The shared implementation is dialect/lib/SassRegalloc.cpp. Its public contracts are in dialect/inc/sass2mlir/dialect/SassRegalloc.h. A transformation needs all of these steps:

  1. Build allocation units from current SSA values, phi connections and required consecutive-register groups. Keep ABI inputs and frozen groups fixed.
  2. Account for physical source spans that are wider than their explicit SSA representation. Liveness uses individual (unit, slot) pairs.
  3. Build interference and add the pass’s register reservations and role budgets. Asynchronous matrix operands must remain available through their completion.
  4. Choose a coloring. The backend provides ordinary, constrained, bounded exact and interval strategies for different callers.
  5. Verify slot conflicts, register pools, fixed placements and value lifetimes before applying the coloring.
  6. Audit writeback independently by renaming the original decoded instruction AST and comparing the emitted instruction words.

A failed coloring reports unplaced units. That is not an automatic spill-code generator. Renaming registers also does not remove existing local-memory loads and stores or promise a lower occupancy cost; those require separate legal transformations and resource updates.

Validation

Identity is a baseline, not an equivalence theorem

Pinned coloring keeps every allocation unit at its original physical base. It supplies an identity baseline for model construction and writeback. Free coloring may move eligible units, subject to the same width, alignment, interference and field constraints. Reproducing pinned bytes alone does not prove that every free coloring or every GPU effect has been modeled.

The existing host-regalloc regression reads a recorded fixture covering loops, diamonds, guarded old values, wide destinations, matrix groups, uniform registers and memory fields. It checks units, conflicts, colors, independently renamed ASTs and emitted words. host-register-limits exercises register-budget checks. From an already configured and built MLIR tree:

ctest --test-dir build -j1 --output-on-failure \
  -R '^host-(regalloc|register-limits|liveness|physical-liveness)$'

The notes in docs/development/regalloc-cleanup.md explain the frozen fixture and the @!PT correction. The fixture is evidence for those cases; updating it requires understanding a changed result.

For Hopper, ordinary liveness is only one obligation. Pending matrix reads, role-specific register acquisition and shared-stage reuse need their own checks. See the scheduling guide for those obligations. The FlashAttention case study shows why allocation, scheduling, publication and a compatible launch must agree.