Implement Linear Scan Register Allocation With Spill Heuristics
Last updated: November 29, 2026
Quick Overview
Given live intervals for virtual registers as (name, start, end) and a fixed number of physical registers, assign each interval a register or mark it spilled using linear scan allocation, spilling the interval that lives longest when registers run out.
Arm
November 29, 20260
0
0 solved
Given live intervals for virtual registers as (name, start, end) and a fixed number of physical registers, assign each interval a register or mark it spilled using linear scan allocation, spilling the interval that lives longest when registers run out.
Arm's compiler teams work on code generation for Cortex-A, Cortex-M and Neoverse targets, and register pressure is a constant concern, especially with only a handful of registers on M-profile cores. The interviewer wants the classic Poletto and Sarkar algorithm implemented cleanly and a sensible discussion of why the spill heuristic works.
What the Interviewer Expects
- Sort intervals by start point
- Keep an active list ordered by end point and expire finished intervals
- Return registers to a free pool on expiry
- Spill the interval with the furthest end when no register is free, possibly stealing its register
- Analyse complexity and compare with graph coloring
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you handle intervals with holes, as LLVM's greedy allocator does?
- How would you account for spill cost such as loop depth instead of just interval length?
- How do fixed registers from the calling convention (x0 to x7 for arguments) fit in?
- When is graph coloring worth its extra compile time?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Each interval is live from start to end inclusive. Two intervals conflict if they overlap. With k registers, no two simultaneously live intervals can share a register. Output a mapping to a regi...
High-Level Design
Walk intervals in order of start. Maintain active, intervals currently holding a register, sorted by end. Before handling a new interval, expire every active interval whose end is before the new sta...