3 ms·
Register allocation is a prime example. This is an NP-Hard problem. Plenty of heuristics exist for finding "generally good enough" solutions, but without exhaus
by userbinator 9y ago
Register allocation is a prime example. This is an NP-Hard problem. Plenty of heuristics exist for finding "generally good enough" solutions, but without exhausting the search space, it typically isn't possible to select the most optimal solution, or even to determine whether a given solution is optimal.
That's true only if you're not using SSA-based dataflow analysis (an idea so amazingly simple and powerful that I've often wondered why it didn't become popular sooner); otherwise, the interference graph is chordal and optimal register allocation essentially becomes a very simple linear-time algorithm.
http://compilers.cs.uni-saarland.de/projects/ssara/ http://compilers.cs.uni-saarland.de/projects/ssara/
http://www.cs.ucla.edu/~palsberg/paper/aplas05.pdf http://www.cs.ucla.edu/~palsberg/paper/aplas05.pdf
- fooker 9y agoThe graph coloring problem for chordal graphs is tractable, true. But register allocation involves a lot more than just graph coloring. For example where and how you insert spill code makes a huge difference. Also, for SSA, you can split some variables to have smaller live ranges which are more likely to get registers. The moment you do that, the theoretical advantages of having a chordal graph goes out of the window.
- cwzwarich 9y ago> Also, for SSA, you can split some variables to have smaller live ranges which are more likely to get registers. The moment you do that, the theoretical advantages of having a chordal graph goes out of the window. IMO, the most important insights of the work on SSA-based register allocation go beyond the idea that chordal interference graphs are easily colorable. In particular, 1) The idea that register allocation can be decoupled into separate allocation and assignment phases. Without this, you either have to view register allocation as an integrated problem, which makes it difficult to apply heuristics, or you have to have miniature register suballocators inside of different phases of your register allocator. The asymptotic horizon of the last possibility is GCC's reload. Of course, the further your architecture deviates from the theoretical model of SSA register allocation, the less you can see the benefit. In particular, estimating liveness with excessive use of subregisters is difficult, and splitting live ranges to satisfy register constraints can push the majority of the difficult problems back into copy elimination. 2) The recognition of the role of parallel copy semantics of phi pseudofunctions. Even if you were writing a conventional non-SSA register allocator, it would be wise to eliminate SSA form by introducing parallel copies and preserving those as late as possible to avoid spurious interferences. Both of these ideas are actually present in Lueh's work on fusion-based allocation (which decouples allocation by simply repeatedly checking whether graphs are still greedily k-colorable, rather than relying on chordality), but it is easier to see that in hindsight.
- bonzini 9y agoThere is still the issue of compiling phi functions effectively, though. The result in the end is less efficient than approximate graph coloring, if I remember correctly the work you cite.
- sddfd 9y agoFrom "Copy coalescing by graph recoloring" PLDI'08, which compared several SSA-based copy coalescing techniques (including an expensive, but optimal, ILP-based formulation) to iterated register coalescing (TOPLAS 1996): > We ran iterated register coalescing [11] (the state of the art in safe coalescing) on these graphs and compared the remaining costs and unsatisfied affinities: Iterated coalescing left 1.28 times more costs non-eliminated and 1.79 times more affinities unsatisfied than the algorithm presented in this paper. To put it another way, from the cost iterated coalescing left, we were able to optimize 22.5% away (and 44.3% of the affinities). The optimum, as determined by the ILP, is given by 35.9% of the costs (and 51.9% of the affinites) iterated coalescing left over.
- bonzini 9y agoThat does not take into account the extra moves or exchanges introduced by the out-of-SSA transformation, if I understand correctly.
- sddfd 9y agoMy understanding is that they are included. The paper talks about the problem it is addressing in Section 2. The following text is taken from that section and makes me think that extra moves/exchanges introduced by out-of-SSA are included. > Live-range splitting to handle register constraints and φ-functions make use of parallel copy instructions. These parallel copy instructions have of course to be implemented using real processor instructions (in the case of φ-functions this is called SSA destruction [5]). In contrast to traditional approaches, these parallel copies are implemented after register allocation and not before. [...] the goal of coalescing is to minimize the number of instructions to implement parallel copies by trying to give corresponding operands and results the same color.
- ytb100 9y agoHow does that help in practice? One still has to perform SSA conversion: http://www.cs.cmu.edu/afs/cs/academic/class/15745-s07/www/papers/pereira-fossacs06.pdf http://www.cs.cmu.edu/afs/cs/academic/class/15745-s07/www/pa...
- kannanvijayan 9y agoI had a question a while back about potential spill strategies that may be appropriate to ask here, given that a bunch of compiler people are probably crawling this thread. Do compilers ever do "computed spilling" (for lack of a better phrase)? For example, if you have code such as: int x = f(); int y = x & MASK; use(y); // ... bunch of code that uses x, but causes y to spill ... use(y); If we're spilling 'x' and 'y', the compiler could theoretically notice that 'y' is purely derived from 'x', and turn the "spill" of 'y' into a no-op (assuming 'x' is kept live), and the "load" of 'y' into re-computing y from x (in this case using a BITAND). Mostly academic curiosity, but is this technique used by any major compiler?
- tom_mellior 9y ago> (assuming 'x' is kept live), and the "load" of 'y' into re-computing y from x If I understand correctly, what you describe is called rematerialization (https://en.wikipedia.org/wiki/Rematerialization https://en.wikipedia.org/wiki/Rematerialization), and yes, it's standard. As with everything else in register allocation, it's difficult to decide when and how to do it.
- kannanvijayan 9y agoCool! Thanks for the info and correct terminology.