<div dir="ltr"><div>Hi Daniel, </div><div><br></div><div>I tried your patch. It indeed solves some (but not all) of the issues NaryReassociate aims to address. One thing NaryReassociate will work on but Reassociate will probably miss is to transform</div><div><br></div><div>p = &input[a]</div><div>q = &input[a + b]</div><div><br></div><div>to</div><div> </div><div>p = &input[a]</div><div>q = p + b;</div><div><br></div><div>I don't see an easy way to merge this into Reassociate unless we peep into GEPs. However, that doesn't say I don't like your approach. </div><div><br></div><div>That said, I would definitely like to see your patch in, and thanks for working on it. Some general comments on the implementation:<br></div><div><br></div><div>1. Instead of hoisting the rank of last pairing, can we assign them a minimum value (e.g. -1)? This allows us to maintain the original sorting order (highest first) and at the same time group last paired values. </div><div><br></div><div>2. LastPairingMap doesn't reflect dominance. I<span style="font-size:13.1999998092651px;line-height:19.7999992370605px">f you have</span></div><div><span style="font-size:13.1999998092651px;line-height:19.7999992370605px">a+c</span><br style="font-size:13.1999998092651px;line-height:19.7999992370605px"><span style="font-size:13.1999998092651px;line-height:19.7999992370605px">and</span><br style="font-size:13.1999998092651px;line-height:19.7999992370605px"><span style="font-size:13.1999998092651px;line-height:19.7999992370605px">a+b+c</span><br></div><div>but a+c doesn't dominate a+b+c, it might not be beneficial to put a and c next to each other. I suspect a simple way of making LastPairingMap per-bb will work for most cases. </div><div><br></div><div>3. FYI, Ken Kennedy et al. published a paper (<a href="http://dl.acm.org/citation.cfm?id=1454120">http://dl.acm.org/citation.cfm?id=1454120</a>) on a global reassociation algorithm, which shares the same spirit as your approach. Instead of considering last pairings, their algorithm collects more history and tends to group the pairs that appear most frequently. It's much more expensive than your approach, so I don't recommend to use their approach yet (unless there's enough need). Just let you know some work explored this area. </div><div><br></div><div>Jingyue</div><div><br><div class="gmail_quote">On Tue, May 5, 2015 at 2:00 PM Daniel Berlin <<a href="mailto:dberlin@dberlin.org">dberlin@dberlin.org</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On Tue, May 5, 2015 at 1:09 PM, Jingyue Wu <<a href="mailto:jingyue@google.com" target="_blank">jingyue@google.com</a>> wrote:<br>
><br>
><br>
> On Tue, May 5, 2015 at 10:31 AM, Daniel Berlin <<a href="mailto:dberlin@dberlin.org" target="_blank">dberlin@dberlin.org</a>> wrote:<br>
>><br>
>> On Tue, May 5, 2015 at 10:20 AM, Jingyue Wu <<a href="mailto:jingyue@google.com" target="_blank">jingyue@google.com</a>> wrote:<br>
>> > Hi Daniel,<br>
>> ><br>
>> > I presume you mean, instead of assigning function arguments distinct<br>
>> > ranks<br>
>> > (<a href="http://llvm.org/docs/doxygen/html/Reassociate_8cpp_source.html#l00282" target="_blank">http://llvm.org/docs/doxygen/html/Reassociate_8cpp_source.html#l00282</a>),<br>
>> > we<br>
>> > should group function arguments in favor of existing pairings.<br>
>><br>
>> Existing = pairings reassociate already chose before<br>
>> *not*<br>
>> existing = pairings that already exist in the source IR<br>
>><br>
>> Given that, we should probably group everything in favor of existing<br>
>> pairings when possible.<br>
><br>
><br>
> Makes sense.<br>
><br>
>><br>
>><br>
>><br>
>> > You are not<br>
>> > suggesting discarding the entire ranking system, right?<br>
>><br>
>> The only three cases that should matter hugely are constants,<br>
>> arguments, and non-movable instructions.<br>
>><br>
>> The rest should hopefully already end up with consistent decisions.<br>
>> If not, we have larger problems.<br>
>><br>
>><br>
>> > I'll look into how that works on my benchmarks. AFAIK, we encountered<br>
>> > some<br>
>> > cases that seem beyond the fix you suggested. These cases involve<br>
>> > constants,<br>
>> > and I attached one reduced example in<br>
>> > <a href="https://llvm.org/bugs/show_bug.cgi?id=22357" target="_blank">https://llvm.org/bugs/show_bug.cgi?id=22357</a>.<br>
>> ><br>
>><br>
>> > void foo(int a, int b, int c, int *x, int *y) {<br>
>> > *x = (a + b);<br>
>> > *y = (a + 2) + b;<br>
>> > }<br>
>> ><br>
>> > Reassociate assigns constants a lower rank than variables, which<br>
>> > prevents<br>
>> > Reassociate from transforming the above example to<br>
>> ><br>
>> > *x = a + b;<br>
>> > *y = (a + b) + 2;<br>
>> ><br>
>><br>
>> This is a variant of the problem above, except you are getting ranks<br>
>> 0, 1, 2 vs 1, 2<br>
><br>
><br>
> The key difference is that constants are designed to be lowest ranked, and<br>
> then the current reassociation algorithm always groups the constant 2 with<br>
> other variables. Looks like your new solution will favor existing pairings<br>
> regardless of the ranks. Then, it should be able to solve the a+b+2 case<br>
> nicely.<br>
<br>
<br>
So, right now, it rewrites it in optimally bad order to have this<br>
happen. Because it puts highest ranked values first, it is guaranteed<br>
that it will end up putting them not next to each other.<br>
<br>
This is because in the ops == 2 case, it places the first two into the<br>
same expression<br>
In the ops > 2 case, it'll place the first two into different expressions.<br>
<br>
So if you have<br>
a+c<br>
and<br>
a+b+c<br>
<br>
You are essentially guaranteed it will never put a and c next to each<br>
other in both cases, unless a and c are the lowest ranked elements.<br>
<br>
For the moment, i reversed the sort order, and we reassign ranks so<br>
that things that it has processed before should end up next to each<br>
other.<br>
<br>
Because the sort is stable, you are only guaranteed they will end up<br>
in the same expression, not necessarily ordered exactly the same way<br>
they were last time.<br>
<br>
This is fixable, but probably not worth it.<br>
<br>
The patch is not perfect (and it is only lightly tested), but i'd love<br>
to see how it goes.<br>
<br>
Some issues<br>
1. Because i reversed the sort order, it may do worse in other cases.<br>
I can fix the sort order reversal by changing how we assign ranks slightly.<br>
Basically, when we look for ops to pair with, we reassign the ranks to<br>
be greater than the maximum rank for that bb (so it doesn't conflict<br>
with anything).<br>
<br>
2. It will only pair it the same way as the immediately last time it<br>
paired it. I doubt there is anything much better unless you spend the<br>
time you do in naryreassociate.<br>
<br>
<br>
Honestly, after staring at this long enough, i'm pretty convinced we<br>
should take an approach similar to naryreassociate's underpinnings in<br>
reassociate.<br>
<br>
In particular, i think we should deliberately unbalance the tree if we<br>
can guarantee it will expose a redundancy (IE it will be an expression<br>
we know is computed by one of our dominators).<br>
<br>
This logic can be incorporated pretty easily into reassociate<br>
</blockquote></div></div></div>