<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Hi Jakob,<div><br></div><div>The attached patch introduces a last chance recoloring mechanism when the current allocation scheme fails to assign a register.</div><div>Thanks for your review.</div><div><br></div><div>** Context **</div><div><br></div><div>In some extreme conditions the current allocation heuristic may fail to find a valid allocation solution whereas one exists.</div><div>This is demonstrated with the (big) test case that is contained in that patch.</div><div>Basically, in that test case, the greedy register allocator runs out of registers because of a combination of:</div><div>- The way the machine scheduler extends some physical register live-ranges, which end up putting a lot of contraints on the available registers.</div><div>- The relocation model, which consumes one register.</div><div>- The function attributes, which forces to keep a register for the frame pointer.</div><div>- The weight of the different variables, which affect the allocation order.</div><div><br></div><div>To simplify, here is what is going on during register allocation:</div><div>vA can use {R1, R2    }</div><div><div style="margin: 0px;">vB can use {    R2, R3}</div><div style="margin: 0px;"> vC can use {R1        }</div><div style="margin: 0px;">Where vA, vB, and vC cannot be split anymore (they are reloads for instance) and they all interfere.</div><div style="margin: 0px;"><br></div><div style="margin: 0px;">vA is assigned R1</div><div style="margin: 0px;">vB is assigned R2</div><div style="margin: 0px;">vC tries to evict vA but vA is already done.</div><div style="margin: 0px;">=> register allocation fails.</div></div><div><br></div><div><br></div><div>** Proposed Solution **</div><div><br></div><div>For the problem at stake, tweaking the machine scheduler to be less aggressive on extending the live-ranges of physical registers would do the trick. Likewise tweaking the allocation cost model to change the allocation order would have worked.</div><div>Anyhow, these approaches would just hide a problem that eventually will happen.</div><div>Instead, the proposed patch introduced a (last chance) recoloring mechanism.</div><div><br></div><div>Basically the idea is to choose a color for the variable that cannot be allocated and recolor its interferences around. Unlike the current register allocation scheme, it is allowed to change the color of an already assigned (but maybe not splittable or spillable) live interval while propagating this change to its neighbors.</div><div>In other word, there are two things that may help finding an available color:</div><div>- Already assigned variables (RS_Done) can be recolored to different color.</div><div>- The recoloring allows to catch solutions that needs to touch more that just the neighbors of the current allocated variable.</div><div><br></div><div>On the previous example, here is how it works:</div><div>After the regular allocation scheme fails, l<span style="font-size: 13px;">ast chance recoloring kicks in:</span></div><div><div style="margin: 0px; font-size: 13px;">vC does as if vA was evicted => vC uses R1.</div><div style="margin: 0px; font-size: 13px;">vC is marked as fixed.</div><div style="margin: 0px; font-size: 13px;">vA needs to find a color.</div><div style="margin: 0px; font-size: 13px;">None are available.</div><div style="margin: 0px; font-size: 13px;">vA cannot evict vC: vC is a fixed virtual register now.</div><div style="margin: 0px; font-size: 13px;">vA does as if vB was evicted => vA uses R2.</div><div style="margin: 0px; font-size: 13px;">vB needs to find a color.</div><div style="margin: 0px; font-size: 13px;">R3 is available.</div><div style="margin: 0px; font-size: 13px;">Recoloring => vC = R1, vA = R2, vB = R3</div></div><div style="margin: 0px; font-size: 13px;"><br></div><div style="margin: 0px; font-size: 13px;">The recoloring mechanism is a backtracking algorithm. It is supposed to kick in rarely so I did not spend a lot of effort to cut branches earlier or limit in a more comprehensive way the search space.</div><div style="margin: 0px; font-size: 13px;"><br></div><div style="margin: 0px; font-size: 13px;"><<a href="rdar://problem/15947839">rdar://problem/15947839</a>></div><div style="margin: 0px; font-size: 13px;"><br></div><div style="margin: 0px; font-size: 13px;">Cheers,</div><div><div apple-content-edited="true">
<div style="color: rgb(0, 0, 0); font-family: Helvetica;  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">-Quentin</div><div style="color: rgb(0, 0, 0); font-family: Helvetica;  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br></div><div style="color: rgb(0, 0, 0); font-family: Helvetica;  font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: -webkit-auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "></div></div></div></body></html>