<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 14 (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:"MS Mincho";
        panose-1:2 2 6 9 4 2 5 8 3 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:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
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:99.25pt 85.05pt 85.05pt 85.05pt;}
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">Hi all,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I would like to find out whether anyone will find it useful to add an x86-<o:p></o:p></p>
<p class="MsoNormal">specific calling convention for reducing emission of vzeroupper instructions.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Current implementation:<o:p></o:p></p>
<p class="MsoNormal">vzeroupper is inserted to any functions that use AVX instructions. The<o:p></o:p></p>
<p class="MsoNormal">insertion points are:<o:p></o:p></p>
<p class="MsoNormal">1) before a call instruction;<o:p></o:p></p>
<p class="MsoNormal">2) before a return instruction;<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Background:<o:p></o:p></p>
<p class="MsoNormal">vzeroupper is an AVX instruction; it is inserted to avoid performance penalty<o:p></o:p></p>
<p class="MsoNormal">when transitioning between x86 AVX mode and legacy SSE mode, e.g., when an<o:p></o:p></p>
<p class="MsoNormal">AVX function calls a SSE function. However, vzeroupper is a slow instruction; it<o:p></o:p></p>
<p class="MsoNormal">adds to register pressure and hurts performance for AVX-to-AVX calls.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">My proposal:<o:p></o:p></p>
<p class="MsoNormal">1) (LLVM part) Add an x86-specific calling convention to the LLVM IR which<o:p></o:p></p>
<p class="MsoNormal">specifies that an external function will be compiled with AVX support and its<o:p></o:p></p>
<p class="MsoNormal">function definition does not use any legacy SSE instructions, e.g.,<o:p></o:p></p>
<p class="MsoNormal">  declare x86_avxcc i32 @foo()<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">2) (Clang part) Add a function attribute to the clang front-end which specifies<o:p></o:p></p>
<p class="MsoNormal">this calling convention, e.g.,<o:p></o:p></p>
<p class="MsoNormal">  extern int foo() __attribute__((avx));<o:p></o:p></p>
<p class="MsoNormal">Function definitions in a translation unit compiled with -mavx architecture will<o:p></o:p></p>
<p class="MsoNormal">implicitly have this attribute.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Benefits:<o:p></o:p></p>
<p class="MsoNormal">No vzeroupper is needed before calling a function with this avx attribute, e.g.,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">  extern int foo() __attribute__((avx));<o:p></o:p></p>
<p class="MsoNormal">  void bar() {<o:p></o:p></p>
<p class="MsoNormal">    ...<o:p></o:p></p>
<p class="MsoNormal">    // some AVX instruction<o:p></o:p></p>
<p class="MsoNormal">    ...<o:p></o:p></p>
<p class="MsoNormal">    // no vzeroupper is needed before the call instruction<o:p></o:p></p>
<p class="MsoNormal">    foo();<o:p></o:p></p>
<p class="MsoNormal">    ...<o:p></o:p></p>
<p class="MsoNormal">    // still needs a vzeroupper before the return instruction<o:p></o:p></p>
<p class="MsoNormal">  }<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Reference:<o:p></o:p></p>
<p class="MsoNormal">A few months ago, I submitted a proposal for improving vzeroupper optimization<o:p></o:p></p>
<p class="MsoNormal">strategy by changing the default code-emission strategy. The proposal was rejected<o:p></o:p></p>
<p class="MsoNormal">on the ground that it would cause problems for existing operating systems.<o:p></o:p></p>
<p class="MsoNormal"><a href="http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-September/065720.html">http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-September/065720.html</a><o:p></o:p></p>
</div>
</body>
</html>