Minimize Register Spills for Overlapping Live Ranges

Last updated: November 29, 2026

Quick Overview

A compiler backend has k physical vector registers and a set of virtual registers, each live over an inclusive interval of instruction indices. Choose which virtual registers to spill so that at most k are live at any instruction while spilling as few as possible, and assign registers to the rest.

AMD
Coding & Algorithms
Software Engineer
AMD
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A compiler backend has k physical vector registers and a set of virtual registers, each live over an inclusive interval of instruction indices. Choose which virtual registers to spill so that at most k are live at any instruction while spilling as few as possible, and assign registers to the rest.

A hard onsite problem in the style of AMD's AMDGPU LLVM backend team, where register pressure directly limits occupancy: more VGPRs per wavefront means fewer wavefronts per SIMD. The interviewer checks a greedy algorithm with two heaps, an exchange argument for optimality, and awareness that real allocators weigh spill cost, not just count.

What the Interviewer Expects
  • Sort live ranges by start
  • Expire ranges whose end is before the current start and reclaim registers
  • When registers run out, spill the live range that ends last
  • Hand the spilled range's register to the new range if it was not the one spilled
  • Argue optimality with an exchange argument
  • Give O(n log n) complexity
Key Topics to Cover
greedy algorithms
interval scheduling
heaps
compilers
register allocation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you change the rule if each range has a spill cost, such as being inside a hot loop?
  • How does register count interact with occupancy on a CDNA compute unit?
  • What does live range splitting buy over spilling a whole range?
  • How would you rematerialize constants instead of spilling them?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: list of (start, end) inclusive intervals and integer k >= 0. Output: (spilled_ids, assignment) where spilled ids are sorted and assignment maps each kept id to a register in 0..k-1 wi...

High-Level Design

Process intervals by start. Keep a min-heap by end to expire ranges and free their registers, and a max-heap by end of currently live kept ranges. If a free register exists, assign it. Otherwise k ran...


Submit Your Answer
Markdown supported

Related Questions