<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Dec 16, 2014 at 7:23 AM, Chad Rosier <span dir="ltr"><<a href="mailto:mcrosier@codeaurora.org" target="_blank">mcrosier@codeaurora.org</a>></span> wrote:<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div class=""><div class="h5">> On Mon, Dec 15, 2014 at 2:26 PM, Chad Rosier <<a href="mailto:mcrosier@codeaurora.org">mcrosier@codeaurora.org</a>><br>
> wrote:<br>
>><br>
>> > On Mon, Dec 15, 2014 at 9:57 AM, Chad Rosier <<a href="mailto:mcrosier@codeaurora.org">mcrosier@codeaurora.org</a>><br>
>> > wrote:<br>
>> >> All,<br>
>> >> About two months ago I posted a patch that hoisted the hottest case<br>
>> >> statement from a switch statement during ISelLowering.<br>
>> >><br>
>> >> See: <a href="http://reviews.llvm.org/D5786" target="_blank">http://reviews.llvm.org/D5786</a><br>
>> >><br>
>> >> Sean was rather adamant about using a Huffman tree (and I agree this<br>
>> is<br>
>> >> a<br>
>> >> more complete solution), so I'd like to put a patch together.<br>
>> ><br>
>> > I think this sounds really cool!<br>
>> ><br>
>> >> That being<br>
>> >> said, I have a few questions.<br>
>> >><br>
>> >> The current switch lowering code sorts based on case values and is<br>
>> >> lowered<br>
>> >> with a combination of binary trees, lookup tables, bit tests and<br>
>> magic.<br>
>> >> If we lower the switch based on branch probabilities, I think the<br>
>> most<br>
>> >> reasonable approach would be to disable the lookup tables and stick<br>
>> with<br>
>> >> a<br>
>> >> purely tree structure.  Does anyone object or have opinions on this<br>
>> >> matter?<br>
>> ><br>
>> > Spontaneously I would have thought the Huffman tree would just replace<br>
>> > the binary trees, i.e. we'd use the branch probabilities to build the<br>
>> > tree differently.<br>
>><br>
>> The current implementation selects the pivot in such a way that it<br>
>> optimizes<br>
>> for lookup tables.<br>
><br>
><br>
> This seems like a potential point of friction: for lookup tables (and<br>
> other<br>
> techniques) you want to inherently keep the cases sorted by their values,<br>
> but a Huffman tree does not care about the actual values; it only cares<br>
> about their relative probabilities.<br>
<br>
</div></div>Exactly!  I can think of lots of ways to deal with this "friction", but<br>
none of them sit well.  As you mentioned, building a Huffman tree and then<br>
dealing with corner cases would be one approach.  Another, which was<br>
Owen's suggestion, would be to peel the hot cases and then fall-back to<br>
the current logic.<br></blockquote><div><br></div><div>Another possibility might be to use a hybrid approach.</div><div><br></div><div>For example, while building the Huffman tree, propagate various information up the tree as you build it. Then you can very easily start at the top of the huffman tree and have all this information available to you (e.g. "peel off the most probable cases until the relative probabilty has some relation to the subtree case density").</div><div><br></div><div>On the other hand (more likely), you could keep the cases ordered (i.e. not a Huffman tree) and integrate the branch probability info as the extra data propagated up the tree (along with other stuff); e.g. the max probability of any node in the subtree, the total probability sum in the subtree. This would also allow some highly non-trivial properties, like "maximum number of consecutive cases observed in each subtree" to be maintained, along with O(1) access to the root of the subtree containing the consecutive cases (and if we keep the tree balanced, O(log n) time to extract the consecutive cases and update all data we are propagating up the tree). This would allow a really clean lowering algorithm like:</div><div><br></div><div>while (we have subtrees with property X)</div><div>  extract and handle subtree in a particular way</div><div>while (we have subtrees with property Y)</div><div>  extract and handle subtree in a different way</div><div>etc.</div><div><br></div><div>A prototype of this can be done really easily with a non-balanced binary tree. In order to make it reliably fast the binary tree with need to be upgraded to a balanced binary tree (red-black, AVL, treap, etc.), which I will go ahead and volunteer to do (I've actually spent more time than I care to admit investigating balanced binary trees and their implementation).</div><div><br></div><div>For a textbook reference on maintaining extra data in a tree, see e.g. section 14.2 of CLRS.</div><div><br></div><div>-- Sean Silva</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><div class="h5"><br>
> -- Sean Silva<br>
><br>
><br>
>>   Lowering the binary tree based on branch probabilities<br>
>> doesn't fit well with this approach.  However, I think we could start by<br>
>> building a Huffman tree and once we've handled the most heavily weighted<br>
>> nodes,<br>
>> we could fall back to the current logic.  Make sense?<br>
>><br>
>> > For how many hot cases is a Huffman tree faster than a jump table? I<br>
>> > suspect we'll still want to build jump tables when we can.<br>
>><br>
>> I take back my previous comment; I agree we should have a hybrid<br>
>> approach,<br>
>> per my comments above.<br>
>><br>
>> > Thanks,<br>
>> > Hans<br>
>><br>
>><br>
>><br>
>><br>
>> _______________________________________________<br>
>> LLVM Developers mailing list<br>
>> <a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
>> <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
>><br>
><br>
<br>
<br>
</div></div></blockquote></div></div></div>