<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Monaco;
panose-1:0 0 0 0 0 0 0 0 0 0;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
p.msonormal0, li.msonormal0, div.msonormal0
{mso-style-name:msonormal;
mso-margin-top-alt:auto;
margin-right:0in;
mso-margin-bottom-alt:auto;
margin-left:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
span.apple-converted-space
{mso-style-name:apple-converted-space;}
span.EmailStyle19
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">The legalization itself is simple; the issue all the places that assume shifts are legal. For example, you probably don’t want “a+a” to become a libcall.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">-Eli<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> Joan Lluch <joan.lluch@icloud.com> <br>
<b>Sent:</b> Monday, June 10, 2019 3:05 PM<br>
<b>To:</b> Eli Friedman <efriedma@quicinc.com><br>
<b>Cc:</b> llvm-dev <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [EXT] Re: [llvm-dev] Bug: Library functions for ISD::SRA, ISD::SHL, and ISD::SRL<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Hi Eli,<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal">Thanks for pointing to the CTLZ_ZERO_UNDEF “LibCall” implementation. I have not it in the version that I am currently using, so it’s nice to know that it’s implemented now. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Incidentally, the CTLZ… implementation is IDENTICAL to what I am proposing for the Shifts. This is not just adding support for “out-of-tree-targets”, but giving consistency to the fact that we have perfectly defined LibCalls for Shifts,
but they can’t be used because there’s an omission in the sources.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I only proposed to add a missing case statement to the ConvertNodeToLibCall.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">To my understanding this is totally harmless to any existing or future targets because all what it does is to call the Library function when setOperationAction is set to “LibCall”. This is not different that any other case, and you just
confirmed that you added the same for the CTLZ… functions. So I do not understand what you mean by “terrible quality in a lot of cases”: Targets with native shift support are not affected, and neither are targets with custom lowering.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I would suggest that you look in more detail at the actual implementation of the ConvertNodeToLibCall and from where it is called and its actual purpose. There’s nothing else required. So I would appreciate that you elaborate on why you
think that this is not a good idea.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">John Lluch<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">On 10 Jun 2019, at 21:09, Eli Friedman <<a href="mailto:efriedma@quicinc.com">efriedma@quicinc.com</a>> wrote:<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">All in-tree targets have variable shift instructions for native integer types, except for AVR. And AVR implements custom lowering. I’m not sure what else would be required to actually make marking a shift as “libcall” actually work well;
technically, the change you’re proposing might produce valid code, but it would be terrible quality in a lot of cases. So I’m not eager to add partial support just for out-of-tree targets.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Marking CTLZ_ZERO_UNDEF as “LibCall” was implemented in<span class="apple-converted-space"> </span><a href="https://reviews.llvm.org/D47917"><span style="color:#954F72">https://reviews.llvm.org/D47917</span></a><span class="apple-converted-space"> </span>.
Probably straightforward to extend that to cover CTTZ_ZERO_UNDEF and CTPOP.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">-Eli<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<div>
<p class="MsoNormal"><b>From:</b><span class="apple-converted-space"> </span>llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org">llvm-dev-bounces@lists.llvm.org</a>><span class="apple-converted-space"> </span><b>On Behalf Of<span class="apple-converted-space"> </span></b>Joan
Lluch via llvm-dev<br>
<b>Sent:</b><span class="apple-converted-space"> </span>Monday, June 10, 2019 8:31 AM<br>
<b>To:</b><span class="apple-converted-space"> </span>llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>><br>
<b>Subject:</b><span class="apple-converted-space"> </span>[EXT] [llvm-dev] Bug: Library functions for ISD::SRA, ISD::SHL, and ISD::SRL<o:p></o:p></p>
</div>
</div>
</div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">LLVM appears to support Library functions for <span style="font-size:8.5pt;font-family:"Monaco",serif;color:#4F8187;background:white">ISD</span><span style="font-size:8.5pt;font-family:"Monaco",serif;background:white">::<span style="color:#31595D">SRA </span></span>,<span style="font-size:8.5pt;font-family:"Monaco",serif;color:#4F8187;background:white">ISD</span><span style="font-size:8.5pt;font-family:"Monaco",serif;background:white">::<span style="color:#31595D">SHL</span></span>,
and <span style="font-size:8.5pt;font-family:"Monaco",serif;color:#4F8187;background:white">ISD</span><span style="font-size:8.5pt;font-family:"Monaco",serif;background:white">::<span style="color:#31595D">SRL</span></span>, as they are properly defined in
RuntimeLibCalls.def.<o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">The library functions defined in RuntimeLibCalls.def (among others) are these:<o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">HANDLE_LIBCALL(SRA_I16, "__ashrhi3")</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">HANDLE_LIBCALL(SRA_I32, "__ashrsi3")</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif">HANDLE_LIBCALL(SRA_I64, "__ashrdi3")</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">However, setting<o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif;color:#31595D">setOperationAction</span><span style="font-size:8.5pt;font-family:"Monaco",serif">(<span style="color:#4F8187">ISD</span>::<span style="color:#31595D">SRA</span>, <span style="color:#4F8187">MVT</span>::<span style="color:#31595D">i16</span>, <span style="color:#31595D">LibCall</span>);</span><o:p></o:p></p>
</div>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">in the TargetLowering constructor causes LLVM to stop with an assert as the shift instruction can’t be selected. <o:p></o:p></p>
</div>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">The problem is in <span style="font-size:8.5pt;font-family:"Monaco",serif;color:#4F8187;background:white">SelectionDAGLegalize</span><span style="font-size:8.5pt;font-family:"Monaco",serif;background:white">::ConvertNodeToLibcall </span>because
there’s no switch case for any of the Shift instructions. <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">The problem gets solved by just adding switch cases like this (and similar for the other shift instructions):<o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif;color:#BA2DA2">case</span><span class="apple-converted-space"><span style="font-size:8.5pt;font-family:"Monaco",serif"> </span></span><span style="font-size:8.5pt;font-family:"Monaco",serif;color:#4F8187">ISD</span><span style="font-size:8.5pt;font-family:"Monaco",serif">::<span style="color:#31595D">SRA</span>:</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> Results.<span style="color:#31595D">push_back</span>(<span style="color:#31595D">ExpandIntLibCall</span>(Node,<span class="apple-converted-space"> </span><span style="color:#BA2DA2">false</span>,</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> <span class="apple-converted-space"> </span><span style="color:#4F8187">RTLIB</span>::<span style="color:#31595D">SRA_I16</span>,</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal" style="background:white"><span style="font-size:8.5pt;font-family:"Monaco",serif"> <span class="apple-converted-space"> </span><span style="color:#4F8187">RTLIB</span>::<span style="color:#31595D">SRA_I16</span>,<span class="apple-converted-space"> </span><span style="color:#4F8187">RTLIB</span>::<span style="color:#31595D">SRA_I32</span>,</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:"Monaco",serif;background:white"> <span style="color:#4F8187">RTLIB</span>::<span style="color:#31595D">SRA_I64</span>,<span class="apple-converted-space"> </span><span style="color:#4F8187">RTLIB</span>::<span style="color:#31595D">SRA_I128</span>));</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">I think this is a BUG by omission of necessary switch cases.<o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">Similarly, the following ISD codes <span style="font-size:8.5pt;font-family:"Monaco",serif;color:#4F8187;background:white">ISD</span><span style="font-size:8.5pt;font-family:"Monaco",serif;background:white">::<span style="color:#31595D">CTTZ, </span><span style="color:#4F8187">ISD</span>::<span style="color:#31595D">CTLZ, </span><span style="color:#4F8187">ISD</span>::<span style="color:#31595D">CTPOP </span></span>do
not define any Library calls, despite LLVM being able to fully expand them into rather large code for targets that do not natively implement them.<o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">I regard this also as an omission/bug, because not all targets would benefit from the custom expansion of these ISD codes, which as said can get unnecessarily long and costly. <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">Instead, LLVM should have them available as possible LibCalls.<o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">Any comments or opinions on these subjects are appreciated<o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal"> <o:p></o:p></p>
</div>
</div>
<div>
<div>
<p class="MsoNormal">Joan Lluch<o:p></o:p></p>
</div>
</div>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</div>
</body>
</html>