<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=us-ascii">
<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;}
/* 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:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
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.EmailStyle19
        {mso-style-type:personal;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
span.EmailStyle20
        {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;}
/* List Definitions */
@list l0
        {mso-list-id:499975529;
        mso-list-type:hybrid;
        mso-list-template-ids:96917996 67698705 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
        {mso-level-text:"%1\)";
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0in;}
ul
        {margin-bottom:0in;}
--></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="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi Saito,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">At AMD we have our own version of vector library and faced similar problems, we followed the SVML path and from vectorizer generated the respective vector calls. When vectorizer generates the respective calls i.e __svml_sin_4 or __amdlibm_sin_4,
 later one can perform only string matching to identify the vector lib call. I’m not sure it’s the proper way, may be instead of generating respective calls it’s better to generate some standard call (may be intrinsics) and lower it later. A late IR pass can
 be introduced to perform lowering, this will lower the intrinsic calls to specific lib calls(__svml_sin_4 or __amdlibm_sin_4 or … ). This can be table driven to decide the action based on the vector library, function name, VF and target information, the action
 can be full-serialize, partial-serialize(VF8 to 2 VF4) or generate the lib call with same VF.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<p class="MsoNormal">Ashutosh<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-left:.5in"><b>From:</b> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<b>On Behalf Of </b>Saito, Hideki via llvm-dev<br>
<b>Sent:</b> Friday, June 29, 2018 7:41 AM<br>
<b>To:</b> 'Saito, Hideki via llvm-dev' <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [llvm-dev] [RFC][VECLIB] how should we legalize VECLIB calls?<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Illustrative Example:<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">clang -fveclib=SVML -O3 svml.c -mavx<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">#include <math.h><o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">void foo(double *a, int N){<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">  int i;<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">#pragma clang loop vectorize_width(8)<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">  for (i=0;i<N;i++){<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">    a[i] = sin(i);<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">  }<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">}<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Currently, this results in a call to <8 x double> __svml_sin8(<8 x double>) after the vectorizer.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">This is 8-element SVML sin() called with 8-element argument. On the surface, this looks very good.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Later on, standard vector type legalization kicks-in but only the argument and return data are legalized.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vmovaps %ymm0, %ymm1<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vcvtdq2pd       %xmm1, %ymm0<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vextractf128    $1, %ymm1, %xmm1<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vcvtdq2pd       %xmm1, %ymm1<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        callq   __svml_sin8<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vmovups %ymm1, 32(%r15,%r12,8)<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vmovups %ymm0, (%r15,%r12,8)<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Unfortunately, __svml_sin8() doesn’t use this form of input/output. It takes zmm0 and returns zmm0.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">i.e., not legal to use for AVX.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">What we need to see instead is two calls to __svml_sin4(), like below.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vmovaps %ymm0, %ymm1<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vcvtdq2pd       %xmm1, %ymm0<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vextractf128    $1, %ymm1, %xmm1<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vcvtdq2pd       %xmm1, %ymm1<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        callq   __svml_sin4<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vmovups %ymm0, 32(%r15,%r12,8)<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vmovups %ymm1, ymm0<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        callq   __svml_sin4<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">        vmovups %ymm0, (%r15,%r12,8)<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">What would be the most acceptable way to make this happen? Anybody having had a similar need previously?<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Easiest workaround is to serialize the call above “type legal” vectorization factor. This can be done with a few lines of code,<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">plus the code to recognize that the call is “SVML” (which is currently string match against “__svml” prefix in my local workspace).<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">If higher VF is not forced, cost model will likely favor lower VF. Functionally correct, but obviously not an ideal solution.<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Here are a few ideas I thought about:<o:p></o:p></p>
<p class="MsoListParagraph" style="margin-left:1.0in;text-indent:-.25in;mso-list:l0 level1 lfo2">
<![if !supportLists]><span style="mso-list:Ignore">1)<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]>Standard LegalizeVectorType() in CodeGen/SelectionDAG doesn’t seem to work. We could define a generic ISD::VECLIB<br>
and try to split into two or more VECLIB nodes, but at that moment we lost the information about which function to call.<br>
We can’t define ISD opcode per function. There will be too many libm entries to deal with. We need a scalable solution.<o:p></o:p></p>
<p class="MsoListParagraph" style="margin-left:1.0in;text-indent:-.25in;mso-list:l0 level1 lfo2">
<![if !supportLists]><span style="mso-list:Ignore">2)<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]>We could write an IR to IR pass to perform IR level legalization. This is essentially duplicating the functionality of LegalizeVectorType()<br>
but we can make this available for other similar things that can’t use ISD level vector type legalization. This looks to be attractive enough<br>
from that perspective.<o:p></o:p></p>
<p class="MsoListParagraph" style="margin-left:1.0in;text-indent:-.25in;mso-list:l0 level1 lfo2">
<![if !supportLists]><span style="mso-list:Ignore">3)<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]>We have implemented something similar to 2), but legalization code is specialized for SVML legalization. This was much quicker than<br>
trying to generalize the legalization scheme, but I’d imagine community won’t like it.<o:p></o:p></p>
<p class="MsoListParagraph" style="margin-left:1.0in;text-indent:-.25in;mso-list:l0 level1 lfo2">
<![if !supportLists]><span style="mso-list:Ignore">4)<span style="font:7.0pt "Times New Roman"">     
</span></span><![endif]>Vectorizer emit legalized VECLIB calls. Since it can emit instructions in scalarized form, adding legalized call functionality is in some sense<br>
similar to that. Vectorizer can’t simply choose type legal function name with illegal vector ---- since LegalizeVectorType() will still<br>
end up using one call instead of two. <o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Anything else?<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Also, doing any of this requires reverse mapping from VECLIB name to scalar function name. What’s the most recommended way to do so?<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Can we use TableGen to create a reverse map?<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Your input is greatly appreciated. Is there a real need/desire for 2) outside of VECLIB (or outside of SVML)?<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Thanks,<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Hideki Saito<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in">Intel Corporation<o:p></o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
</body>
</html>