<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;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",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;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.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;}
--></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"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">If I understood it correctly, __declspec(noalias) is not the same as specifying restrict on each parameter.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">It means in the mentioned example a, b & c don't modify or reference any global state, but they are free to alias one another.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">You could specify restrict on each one to indicate that they do not alias each other.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">For more details refer:
<a href="https://msdn.microsoft.com/en-us/library/k649tyc7.aspx">https://msdn.microsoft.com/en-us/library/k649tyc7.aspx</a><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Regards,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Ashutosh<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D"><o:p> </o:p></span></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><span style="font-size:11.0pt;font-family:"Calibri",sans-serif">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif"> llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org]
<b>On Behalf Of </b>Phil Tomson via llvm-dev<br>
<b>Sent:</b> Friday, November 18, 2016 12:23 AM<br>
<b>To:</b> LLVM Developers Mailing List <llvm-dev@lists.llvm.org><br>
<b>Subject:</b> [llvm-dev] Loop invariant not being optimized<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">I've got an example where I think that there should be some loop-invariant optimization happening, but it's not.  Here's the C code:<br>
<br>
<span style="font-family:"Courier New"">#define DIM 8<br>
#define UNROLL_DIM DIM<br>
typedef double InArray[DIM][DIM];<br>
<br>
__declspec(noalias) void f1( InArray c, const InArray a, const InArray b )<br>
{<br>
<br>
#pragma clang loop unroll_count(UNROLL_DIM)<br>
    for( int i=0;i<DIM;i++)<br>
#pragma clang loop unroll_count(UNROLL_DIM)<br>
        for( int j=0;j<DIM;j++)<br>
#pragma clang loop  unroll_count(UNROLL_DIM)<br>
            for( int k=0;k<DIM;k++) {<br>
                c[i][k] = c[i][k] + a[i][j]*b[j][k];<br>
            }<br>
}</span><o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">The "a[i][j]" there is invariant in that inner loop. I've unrolled the loops with the unroll pragma to make the assembly easier to read, here's what I see (LVM 3.9, compiling with: clang -fms-compatibility -funroll-loops
 -O3   -c fma.c -o fma.o )<br>
<br>
<br>
<span style="font-family:"Courier New"">0000000000000000 <f1>:<br>
       0: 29580c0000000000  load  r3,r0,0x0,64<br>
       8: 2958100200000000  load  r4,r1,0x0,64 #r4 <- a[0][0]<br>
      10: 2958140400000000  load  r5,r2,0x0,64<br>
      18: c0580c0805018000  fmaf  r3,r4,r5,r3,64<br>
      20: 79b80c0000000000  store r3,r0,0x0,64<br>
      28: 2958100000000008  load  r4,r0,0x8,64<br>
      30: 2958140200000000  load  r5,r1,0x0,64 #r5 <- a[0][0]<br>
      38: 2958180400000008  load  r6,r2,0x8,64<br>
      40: c058100a06020000  fmaf  r4,r5,r6,r4,64<br>
      48: 79b8100000000008  store r4,r0,0x8,64<br>
      50: 2958140000000010  load  r5,r0,0x10,64<br>
      58: 2958180200000000  load  r6,r1,0x0,64 #r6 <- a[0][0]<br>
      60: 29581c0400000010  load  r7,r2,0x10,64<br>
      68: c058140c07028000  fmaf  r5,r6,r7,r5,64<br>
      70: 79b8140000000010  store r5,r0,0x10,64<br>
      78: 2958180000000018  load  r6,r0,0x18,64<br>
      80: 29581c0200000000  load  r7,r1,0x0,64 #r7 <- a[0][0]<br>
      88: 2958200400000018  load  r8,r2,0x18,64<br>
      90: c058180e08030000  fmaf  r6,r7,r8,r6,64<br>
... </span><o:p></o:p></p>
</div>
<p class="MsoNormal">(fmaf semantics are: fmaf r1,r2,r3,r4, SIZE    r1 <- r2*r3+r4 )<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">(load semantics are: load r1,r2,imm, SIZE     r1<- mem[r2+imm] )<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">All three of the addresses are loaded in every loop. Only two need to be reloaded in the inner loop. I added the 'noalias' declspec in the C code above thinking that it would indicate that the pointers going
 into the function are not aliased and that that would allow the optimization, but it didn't make any difference.<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-bottom:12.0pt">Of course it's easy to rewrite the example code to avoid this extra load/inner loop, but I would have thought this would be a fairly straighforward optimization for the optimizer. Am I missing something?<o:p></o:p></p>
</div>
<p class="MsoNormal">Phil<o:p></o:p></p>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>