<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:Consolas;
        panose-1:2 11 6 9 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;
        color:black;}
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
        {mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;
        color:black;}
pre
        {mso-style-priority:99;
        mso-style-link:"HTML Preformatted Char";
        margin:0in;
        margin-bottom:.0001pt;
        font-size:10.0pt;
        font-family:"Courier New";
        color:black;}
span.HTMLPreformattedChar
        {mso-style-name:"HTML Preformatted Char";
        mso-style-priority:99;
        mso-style-link:"HTML Preformatted";
        font-family:Consolas;
        color:black;}
p.msonormal0, li.msonormal0, div.msonormal0
        {mso-style-name:msonormal;
        mso-style-priority:99;
        mso-margin-top-alt:auto;
        margin-right:0in;
        mso-margin-bottom-alt:auto;
        margin-left:0in;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;
        color:black;}
span.EmailStyle21
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.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 bgcolor="white" lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:#1F497D">Restrict is supported by Clang for C++ via __restrict__, so it seems strange to block using this proposed option for C++.<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">That said, this kind of option can be dangerous and should come with a suitable warning.  We’ve had a similar option and in practice it’s been used to hunt for
 performance gains (i.e., turn it on and see what happens), but just because the code runs faster and produces the correct result with the option enabled doesn’t mean it is safe in all cases.  And if it crashes or gives you wrong answers, you still don’t know
 which pointer had the alias that caused that problem.  Either way, you still need to inspect all of the pointers and prove to yourself it is safe and at that point you might as well add restrict manually.<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">-Troy<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;color:windowtext">From:</span></b><span style="font-size:11.0pt;font-family:"Calibri",sans-serif;color:windowtext"> llvm-dev <llvm-dev-bounces@lists.llvm.org>
<b>On Behalf Of </b>Finkel, Hal J. via llvm-dev<br>
<b>Sent:</b> Tuesday, January 15, 2019 9:57 AM<br>
<b>To:</b> Zheng CZ Chen <czhengsz@cn.ibm.com>; llvm-dev@lists.llvm.org<br>
<b>Subject:</b> Re: [llvm-dev] Aggressive optimization opportunity<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p><o:p> </o:p></p>
<div>
<p class="MsoNormal">On 1/15/19 6:07 AM, Zheng CZ Chen via llvm-dev wrote:<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p><span style="font-size:10.0pt">Hi,</span><br>
<br>
<span style="font-size:10.0pt">There are some compilers with a aggressive optimization which restricts function pointer parameters. Let's say opt restrict_args. When restrict_args is turned on, compiler will treat all function pointer parameters as restrict
 one.</span><o:p></o:p></p>
</blockquote>
<p><o:p> </o:p></p>
<p>I certainly understand the use case, in a general sense. In my experience, these options are used with (fairly old) pre-C99 code bases (and specifically C, not C++), which follow something akin to a one-function-per-source-file model and which can't be modified
 (e.g., for licensing reasons). Using these options are certainly considered bad practice, and they only apply to certain legacy code bases. Does this match your experience and expected usage?<o:p></o:p></p>
<p>In an engineering sense, this seems like a trivial feature to support. I don't object to supporting it, but if we do, we probably want to:<o:p></o:p></p>
<p> 1. Restrict it's application to C (e.g., it should be an error to use with C++, OpenCL, CUDA, and any other languages that Clang supports).<o:p></o:p></p>
<p> 2. When used with C99 or later language standards, the use of this flag generates a warning on each function definition with a fixit hint showing where the restrict keyword should be placed (we can then, optionally of course, use these fixits to automatically
 upgrade code where possible using our corresponding infrastructure). This warning should have a separate flag, and is disabled by default for pre-C99 standard modes, and enabled by default otherwise, but can be toggled independently.<o:p></o:p></p>
<p> -Hal<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p style="margin-bottom:12.0pt"><br>
<i><span style="font-size:10.0pt">int foo(int * a) </span></i><span style="font-size:10.0pt">+ restrict_args opt
</span><br>
<br>
<span style="font-size:10.0pt">equals to:</span><br>
<br>
<i><span style="font-size:10.0pt">int foo(int * restrict a)</span></i><br>
<br>
<br>
<span style="font-size:10.0pt">Here is a complete example:</span><br>
<span style="font-size:10.0pt">source code:</span><br>
<span style="font-size:10.0pt">extern int num;</span><br>
<i><span style="font-size:10.0pt">int foo(int * a)</span></i><br>
<i><span style="font-size:10.0pt">{</span></i><br>
<i><span style="font-size:10.0pt">(*a) = 10;</span></i><br>
<i><span style="font-size:10.0pt">num++;</span></i><br>
<i><span style="font-size:10.0pt">(*a)++;</span></i><br>
<br>
<i><span style="font-size:10.0pt">return *a; </span></i><br>
<i><span style="font-size:10.0pt">}</span></i><br>
<br>
<span style="font-size:10.0pt">Using IBM xlc compiler with option -qrestrict at -O2, we get result:</span><br>
<br>
<span style="font-size:10.0pt">0000000000000000 <foo>:</span><br>
<span style="font-size:10.0pt">0: 00 00 4c 3c addis r2,r12,0</span><br>
<span style="font-size:10.0pt">4: 00 00 42 38 addi r2,r2,0</span><br>
<span style="font-size:10.0pt">8: 00 00 a2 3c addis r5,r2,0</span><br>
<span style="font-size:10.0pt">c: 00 00 a5 e8 ld r5,0(r5)</span><br>
<span style="font-size:10.0pt">10: 0b 00 00 38 li r0,11</span><br>
<span style="font-size:10.0pt">14: 00 00 03 90 stw r0,0(r3)</span><br>
<span style="font-size:10.0pt">18: 00 00 85 80 lwz r4,0(r5)</span><br>
<b><span style="font-size:10.0pt">1c: 0b 00 60 38 li r3,11 ------>since we confirm num will not change the content where pointer to, compiler can directly return 11.</span></b><br>
<span style="font-size:10.0pt">20: 01 00 04 38 addi r0,r4,1</span><br>
<span style="font-size:10.0pt">24: 00 00 05 90 stw r0,0(r5)</span><br>
<span style="font-size:10.0pt">28: 20 00 80 4e blr</span><br>
<br>
<span style="font-size:10.0pt">Seems clang does not have such optimization. And I don't find similar option in gcc either.</span><br>
<br>
<span style="font-size:10.0pt">Is it possible to add this optimization into clang?</span><br>
<br>
<span style="font-size:10.0pt">Thanks.</span><br>
<br>
<span style="font-size:10.0pt">BRS//</span><br>
<span style="font-size:10.0pt">Chen Zheng</span><br>
<span style="font-size:10.0pt">Power Compiler Backend Developer</span><o:p></o:p></p>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<pre>_______________________________________________<o:p></o:p></pre>
<pre>LLVM Developers mailing list<o:p></o:p></pre>
<pre><a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><o:p></o:p></pre>
<pre><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><o:p></o:p></pre>
</blockquote>
<pre>-- <o:p></o:p></pre>
<pre>Hal Finkel<o:p></o:p></pre>
<pre>Lead, Compiler Technology and Programming Languages<o:p></o:p></pre>
<pre>Leadership Computing Facility<o:p></o:p></pre>
<pre>Argonne National Laboratory<o:p></o:p></pre>
</div>
</div>
</body>
</html>