<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:"MS Mincho";
        panose-1:2 2 6 9 4 2 5 8 3 4;}
@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:"\@MS Mincho";
        panose-1:2 2 6 9 4 2 5 8 3 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;}
span.EmailStyle17
        {mso-style-type:personal-compose;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;}
@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;}
@list l1
        {mso-list-id:626352407;
        mso-list-type:hybrid;
        mso-list-template-ids:708083224 67698705 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l1:level1
        {mso-level-text:"%1\)";
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l1:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l1: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"><o:p> </o:p></p>
<p class="MsoNormal">Illustrative Example:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">clang -fveclib=SVML -O3 svml.c -mavx<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">#include <math.h><o:p></o:p></p>
<p class="MsoNormal">void foo(double *a, int N){<o:p></o:p></p>
<p class="MsoNormal">  int i;<o:p></o:p></p>
<p class="MsoNormal">#pragma clang loop vectorize_width(8)<o:p></o:p></p>
<p class="MsoNormal">  for (i=0;i<N;i++){<o:p></o:p></p>
<p class="MsoNormal">    a[i] = sin(i);<o:p></o:p></p>
<p class="MsoNormal">  }<o:p></o:p></p>
<p class="MsoNormal">}<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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">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">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">        vmovaps %ymm0, %ymm1<o:p></o:p></p>
<p class="MsoNormal">        vcvtdq2pd       %xmm1, %ymm0<o:p></o:p></p>
<p class="MsoNormal">        vextractf128    $1, %ymm1, %xmm1<o:p></o:p></p>
<p class="MsoNormal">        vcvtdq2pd       %xmm1, %ymm1<o:p></o:p></p>
<p class="MsoNormal">        callq   __svml_sin8<o:p></o:p></p>
<p class="MsoNormal">        vmovups %ymm1, 32(%r15,%r12,8)<o:p></o:p></p>
<p class="MsoNormal">        vmovups %ymm0, (%r15,%r12,8)<o:p></o:p></p>
<p class="MsoNormal">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">i.e., not legal to use for AVX.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">What we need to see instead is two calls to __svml_sin4(), like below.<o:p></o:p></p>
<p class="MsoNormal">        vmovaps %ymm0, %ymm1<o:p></o:p></p>
<p class="MsoNormal">        vcvtdq2pd       %xmm1, %ymm0<o:p></o:p></p>
<p class="MsoNormal">        vextractf128    $1, %ymm1, %xmm1<o:p></o:p></p>
<p class="MsoNormal">        vcvtdq2pd       %xmm1, %ymm1<o:p></o:p></p>
<p class="MsoNormal">        callq   __svml_sin4<o:p></o:p></p>
<p class="MsoNormal">        vmovups %ymm0, 32(%r15,%r12,8)<o:p></o:p></p>
<p class="MsoNormal">        vmovups %ymm1, ymm0<o:p></o:p></p>
<p class="MsoNormal">        callq   __svml_sin4<o:p></o:p></p>
<p class="MsoNormal">        vmovups %ymm0, (%r15,%r12,8)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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"><o:p> </o:p></p>
<p class="MsoNormal">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">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">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"><o:p> </o:p></p>
<p class="MsoNormal">Here are a few ideas I thought about:<o:p></o:p></p>
<p class="MsoListParagraph" style="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="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="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="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"><o:p> </o:p></p>
<p class="MsoNormal">Anything else?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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">Can we use TableGen to create a reverse map?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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"><o:p> </o:p></p>
<p class="MsoNormal">Thanks,<o:p></o:p></p>
<p class="MsoNormal">Hideki Saito<o:p></o:p></p>
<p class="MsoNormal">Intel Corporation<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</body>
</html>