1 RABLET 2 ------ 3 Last fall I produced the RABLE document which described the approach I thought should be taken to write a new register allocator for GCC. 4 5 A new register allocator written from scratch is a very long term project (measured in years), and there is no guarantee after all that work that we'd end up with something which is remarkably better. One would hope that it is a lot more maintainable, but the generated code is a crapshot. It might look better but will it really run faster? The current plate of spaghetti we call the register allocator has had a lot of fine tuning go into it over the years, and it generally generates pretty darn good code IF it doesn't have to spill much. 6 7 This describes my current work-in-progress, RABLET, which stands for RABLE-Themes. Rather than write a new allocator, I think there are things we can do that are a lot less work which will reap much of the benefits. Working from the premise that we generate good code when we don't have to spill, try to detect early that we are going to require spill code and do some of that work at a point where we have good analysis. 8 9 THEMES 10 ------ 11 12 13 1 - One of the core themes in RABLE was very early selection of instructions from patterns. RTL patterns are initially chosen by the EXPAND pass. EXPAND tends to generates better rtl patterns by being handed complex trees which it can process and get better combinations. 14 When TREE-SSA was first implemented, we got very poor RTL because expand was seeing very small trees. TER (Temporary Expression Replacement) was invented, which mashed any single-def/single-use ssa_names together into more complex trees. This gave expand a better chance of selecting better instructions, and made a huge difference. 15 16 2 - Rematerialization/register pressure reduction. The tree-ssa optimizers pay no attention to potential register pressure (which is as it should be). This sometimes results in very high register pressure entering the back end. RABLE defined passes performing expression rematerialization and other register pressure reducing techniques to reduce excessive register pressure before actually trying to do allocation. If you have 120 register live at some point, and only 16 physical registers, the allocator is clearly going to have a difficult time. Try to present it with something more manageable. 17 18 RABLET CORE 19 ----------- 20 21 On to the meat of the subject. RABLET involves reworking the out-of-ssa pass, rewriting expand such that it is tightly integrated with the out-of-ssa pass, and adding some new work to reduce register pressure. Thats a lot less work than a whole new register allocator. 22 23 If we look at the RABLE architecture, the passes before global allocation are as follows: 24 25 Live-range-disjointer 26 instruction-selection 27 Register coalescing 28 register pressure reduction 29 <.. then regular register allocation activity ...> 30 31 In the context of RABLET: 32 33 -Out of SSA naturally performs live range disjointing. 34 -Initial RTL pattern selection is performed by expand. 35 -Register coalescing - ssa_name coalescing is done by out-of-ssa, and ssa_name == register. 36 -Register pressure reduction – This is new work which would be implemented. 37 38 If we create a new black box super pass called out-of-ssa-expand-register-pressure-reducer, (ewww, lets just refer to it as ssa-to-rtl :-), then we'd have something close to the first part of RABLE. Not exactly of course, because 'instruction selection' means something slightly different. In RABLE it means choosing an instruction alternative from an RTL pattern , in RABLET it simple means "choose good RTL patterns". 39 40 “But wait...” I hear some clever person saying... “A lot of things happen between ssa-to-rtl and global register allocation.”. Hold that thought until I get through describing ssa-to-rtl since there are some important considerations which affect that discussion 41 42 SSA-TO-RTL 43 ---------- 44 45 When out-of-ssa was originally written, tree-ssa was still evolving. We didnt know exactly what was going to be expected of it, so it was written to be flexible and had a lot of gorp added after the fact. TREE-SSA is now mature and we understand much more fully what is expected of translation out of ssa. I have begun rewriting it to be smaller and faster and to eliminate all the unused features which were initially provided. It is also being rewritten with RABLET in mind. 46 47 The new generation out-of-ssa will eliminate PHIs, much as it does today. This is done by coalescing together ssa_names connected by copies (and PHIs are just copies), and issuing copies on edges required by the PHIs. Instead of then mapping these back to VAR_DECLs and writing this all back to the trees, it will simply be maintained in a partition list map. This is *key*. At this point, we have a mapping of what ssa_names have been coalesced together, and the copies required to do peform this. The trees themselves are unaltered, which means we are STILL IN SSA FORM. 48 49 OK, why is that so important. That means we still have all the SSA live range and data flow information which we can use to generate RTL insns. Today, expand produces better instructions selections by seeing larger hunks of trees at one. If expand is rewritten to be ssa-aware, then it can paw back through the IL to see what stmts and instructions feed this stmt. It can look at as large a hunk of the program as it sees fit. When it actually generates RTL, it will use the partition map to determine what ssa_names variables are actually substituted into the RTL stream. 50 51 “Interesting, but why is that so grand?”. A couple of reasons. One is that RTL doesn't have to be generated linearly in complete absence of flow information. We can generate code for a stmt, attach it to the stmt, and at a later stmt, determine that it would be better to combine it some other way and remove/modify the RTL that was first generated. It can also look forward to see how this stmt is going to be used, and how often in making decisions. Another nice feature is that it still has flow/use info for a variable. So if 'A' requires a load from a base register, we can tell if that base register has already been loaded, and where. If register pressure is high, it can force a new load of the base register thus splitting the live range. Which then brings home the point that we can look at the rtl being generated, and overlay what we know about the SSA_NAMES over the register being generated. Its not truly RTL-SSA, but it gives us some information to work with as addressing modes are exposed. 52 53 Furthermore, we can calculate more accurate register pressure at this point, which can help to determine the code we generate. For instance, local variables are all turned into registers today. If the register pressure is high, we could elect instead to load the local from its stack location instead of leaving it in a register all the time. This would prevent the register allocator fro having to perform this spill. Instead, we get a load from the home address. 54 55 Thats a flavor of what combining out-of-ssa and expand can do. I'm not terribly familiar with expand (yet), so there will be other things come up Im sure. As well as issues. This is just the initial plan. 56 57 Register Pressure Reduction 58 --------------------------- 59 60 On to the register pressure reduction component. As mentioned, some small level of register pressure reduction can be done during the rtl expansion phase. We can do some higher level work just before expansion to rtl. Once we have the ssa_name mappings, we can do rough register pressure calculations. Integer ssa_names are 1 int register, double ints are two, etc. This would not take into account any extra registers exposed by addressing modes and expansion, but it can give you a rough idea if pressure is excessive or not. 25 lives values and 6 hardware registers could use some register pressure reduction! 61 62 So register pressure calculations atr implemented, and then the same techniques RABLE would have used, rematerialization, live range splitting, pre-spilling, etc., ON SSA FORM. This makes the implementation much easier since it becomes simply another ssa pass, with some minor caveats. There are some minor issues to resolve, but in general, any of these types of operations will result in new local temporary registers which don't coalesce with any other partition, and results in exactly what RABLE would have done. The bonus is that it is performed *before* RTL expansion , so we should get better instruction selections. 63 64 65 And that forms the new ssa-to-rtl pass. A side effect of this is that I am suddenly very interested in the work done earlier (Honza was it?) to make opt0 go into ssa and do a few basic optimizations before generating rtl. If expand is going to be swallowed by ssa-to-rtl, we wont want to keep the old expand around for non-optimized code. 66 67 RTL optimizations 68 ----------------- 69 70 Back to that earlier observation, “But wait, A lot of things happen between ssa-to-rtl and global register allocation.” 71 72 If we substiture ssa-to-rtl into the RABLE architecture flow, it looks something like: 73 74 ssa-to-rtl 75 spill cost analysis 76 global allocation 77 spiller 78 spill location optimizer 79 instruction rewriter. 80 81 Since RABLET doesn't propose writing RABLE as such, we'll just have to substitute in the current register allocator passes for the RABLE ones. 82 83 What about all the stuff in between? CSE, GCSE, scheduling, and all that? 84 85 If expand is made much smarter, I would argue that much of GCSE and CSE isn't needed. We've already performed those optimizations at a high level, and we can hopefully do a lot of the factoring and things on addressing registers exposed during expand. I'm sure there are other things to do, but I would argue that they are significantly less than a "general purpose" CSE and GCSE pass. And in the cases of high register pressure, how much would you want them to do anyway? Its really these high register pressure areas that RABLET is attacking anyway. 86 87 If I recall, scheduling is register pressure aware and normally doesn't increase register pressure dramatically. If it does increase pressure, well, this won't solve every problem after all. 88 89 I don't see the need for anything between RTl generation and scheduling/register allocation that would undo all the work done in ssa-to-rtl. We may still have need for some RTL optimizations, sure, but their impact on register pressure is likely to be minimal, and should be so. Those that can increase pressure, like CSE and GCSE either arent needed, or can be handcuffed. (ie, don't work on registers which have been handled by ssa already, only look at registers that are not flagged as 'optimized' by the ssa-to-rtl phase, or dont run on areas where register pressure is above a threshold value, etc.). There are multiple options which can be considered. 90 91 Based on that, I would argue that all those things between ssa-to-rtl and the register allocator are irrelevent as far as un-doing the register pressure reductions done in RABLET. This makes the point of view on RTL optimizations to be more like “do good things only to registers which earlier optimizations couldn't see”, noting that ssa-to-rtl does more optimizations to rtl registers than expand did by itself. 92 93 Summary 94 ------- 95 96 Thats the project in a nutshell. There are quite a few more details of course, but that's the gist. Its not a perfect implementation of RABLE of course, but it takes the major themes, and with a small shoe-horn, you can fit the description of RABLET into the architectural diagram of RABLE. The hope is that although we certainly wont see 100% of the benefit of RABLE, we will see enough benefit to justify the work and perhps make RABLE unnecessary. If work on RABLE begins (or some other new allocator arrives...), it will still be presented with something better to work with than we have today, making its jobs easier. Well, making the job of allocating registers easier, although it might be a more difficult job to get performance gains :-P. 97 98 Unlike RABLE, I am currently working on RABLET, with the out-of-ssa rewrite underway. The new out-of-ssa will be ready for GCC 4.3. It should be faster and not suffer from slowdowns in some of the pathological cases we've found with the current implementation. 99 100 Once that is complete, the next step is to do some of the higher level register pressure reduction work in SSA. Combined with TER as it exists today (with perhaps a few modifications), I would expect to see some benefit in the generated code simply due to RA not having as much pressure to deal with. There is some possibility this will make it into 4.3 as well, but thats not a sure thing. There is also the impact MEM-SSA will have on this pass, which is not clear at this point. 101 102 Then its time for the integration of expand. I really don't know how long that would take, but I am pretty sure thats not in the 4.3 timeframe! :-) When tree-ssa was first implemented, a rewrite of expand was on the table as eventually being needed (as well as a new allocator). TER was stopgap solution until it could be tackled, and I doubt anyone will be sorry to see it go! :-) 103 104 I'd be more than happy to respond to any useful feedback anyone might have. As I said, I didn't delve deeply into some of the details, and I can think of half a dozen obvious questions/concerns people might have. With a much shorter timeframe, I think there will be enough benefit from this project to reduce the urgency of a new register allocator, at least from a performance point of view. As always with compilers, you can never be 100% sure :-) 105 106 I am not actually attending the conference in Ottawa next week, but I will be in Ottawa late Tuesday afternoon through Wednesday night if anyone wants to track me down, (or arrange to meet up with me), to discuss any of this. (Or even RABLE for that matter). 107 108 Andrew
Attached FilesTo refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
- [get | view] (2013-07-15 18:22:39, 157.0 KB) [[attachment:Cauldron-2013-slides.pdf]]
- [get | view] (2006-12-18 16:43:55, 779.2 KB) [[attachment:RABLE.pdf]]
- [get | view] (2006-12-18 16:44:31, 13.6 KB) [[attachment:RABLET.txt]]
- [get | view] (2009-03-19 20:13:00, 53.4 KB) [[attachment:debuglocus.pdf]]
- [get | view] (2013-06-21 15:18:05, 288.0 KB) [[attachment:gccrestructureplan2.1.odt]]
- [get | view] (2013-06-21 15:30:53, 298.3 KB) [[attachment:gccrestructureplan2.1.pdf]]
- [get | view] (2010-04-08 16:48:48, 25.5 KB) [[attachment:memorymodel.odt]]
You are not allowed to attach a file to this page.