<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:Helvetica;
        panose-1:2 11 6 4 2 2 2 2 2 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;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.apple-converted-space
        {mso-style-name:apple-converted-space;}
span.EmailStyle19
        {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:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
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" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">And I found out that, according to the gcc description, __attribute__((pure)) and __attribute__((const)) functions must always return.<o:p></o:p></p>
<p class="MsoNormal">I put up a patch that adds the 'willreturn' attribute: <a href="https://reviews.llvm.org/D96960">
https://reviews.llvm.org/D96960</a><o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Greetings,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Jeroen Dobbelaere<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-left:solid blue 1.5pt;padding:0cm 0cm 0cm 4.0pt">
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm">
<p class="MsoNormal"><b>From:</b> Florian Hahn <florian_hahn@apple.com> <br>
<b>Sent:</b> Thursday, February 18, 2021 13:27<br>
<b>To:</b> Johannes Doerfert <johannesdoerfert@gmail.com>; llvm-dev@lists.llvm.org<br>
<b>Cc:</b> Jeroen Dobbelaere <dobbel@synopsys.com><br>
<b>Subject:</b> Re: willreturn<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal">On Feb 17, 2021, at 20:40, Johannes Doerfert <<a href="mailto:johannesdoerfert@gmail.com">johannesdoerfert@gmail.com</a>> wrote:<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif"><br>
On 2/17/21 2:19 PM, Florian Hahn wrote:<br style="caret-color: rgb(0, 0, 0);font-variant-caps: normal;text-align:start;-webkit-text-stroke-width: 0px;word-spacing:0px">
<br>
</span><o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">Hi,<br>
<br>
<br>
<o:p></o:p></span></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">On Feb 17, 2021, at 19:15, Johannes Doerfert <<a href="mailto:johannesdoerfert@gmail.com">johannesdoerfert@gmail.com</a>> wrote:<br>
<br>
<br>
On 2/17/21 12:04 PM, Jeroen Dobbelaere wrote:<br>
<br>
<o:p></o:p></span></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">I'm confused.<br>
<br>
Are you interested in adding `willreturn` to a function via clang?<o:p></o:p></span></p>
</blockquote>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">I think so (also getting a little confused :( ) ..<br>
What should the behavior be of a '__attribute__((const))' function ?<br>
Is the progress guaranteed ?<br>
<br>
See <a href="https://urldefense.com/v3/__https:/www.godbolt.org/z/GoPYhM__;!!A4F2R9G_pg!J_I-X0Nr0Kj2jNMZfP3ddh6HPKtrHE6QbWBBksmctUI0Za9kbc_kJmFs2a8tYYZcJivvgh3P$">
https://www.godbolt.org/z/GoPYhM</a><br>
<br>
<br>
// extern "C" ...<br>
extern int ptrfun1(int, int*, int*) __attribute__((const));<br>
<br>
int b[5];<br>
<br>
int foo() {<br>
    int a[10];<br>
    int index = ptrfun1(5, &a[0], &b[0]);<br>
    a[index]=10;<br>
    return a[index];<br>
}<br>
<br>
clang-11 is able to optimize this away<br>
<br>
clang-trunc is mixed:<br>
- for this case, the call will not optimize it away. (as far as I see, since D94106)<br>
- But if you do not use the return value upfront, it will be optimized away.<br>
    ...<br>
    int index = 3;<br>
    ptrfun1(5, &a[0], &b[0]);<br>
    ...<br>
<br>
If a '__attribute__((const))' function is allowed to not progress, then not all llvm passes are aware of this<br>
with the current mapping on llvm attributes and imho, we'll need an attribute to indicate that we want<br>
to have progress. Or, clang should map '__attribute__((const))' to 'readnone willreturn'.<o:p></o:p></span></p>
</blockquote>
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">FWIW, I think removal is correct because you run it as C++ program.<o:p></o:p></span></p>
</blockquote>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">Yep, I think that’s the case.<br>
<br>
<br>
<o:p></o:p></span></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">This is "just" a phase ordering issue. Run O3 again and trunk happily removes it:<span class="apple-converted-space"> </span><a href="https://urldefense.com/v3/__https:/www.godbolt.org/z/95qeah__;!!A4F2R9G_pg!J_I-X0Nr0Kj2jNMZfP3ddh6HPKtrHE6QbWBBksmctUI0Za9kbc_kJmFs2a8tYYZcJqBozlM8$">https://www.godbolt.org/z/95qeah</a><span class="apple-converted-space"> </span><<a href="https://urldefense.com/v3/__https:/www.godbolt.org/z/95qeah__;!!A4F2R9G_pg!J_I-X0Nr0Kj2jNMZfP3ddh6HPKtrHE6QbWBBksmctUI0Za9kbc_kJmFs2a8tYYZcJqBozlM8$">https://www.godbolt.org/z/95qeah</a>><br>
That said, I agree, we should check why this is happening and what to do about it.<o:p></o:p></span></p>
</blockquote>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">I think the reason this gets removed after another -O3 run is that there still are passes that may remove functions, even if they may not return. In this case it appears to
 be Bit-Tracking Dead Code Elimination.<br>
<br>
<br>
<o:p></o:p></span></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">Now wrt. the attribute lowering (const/pure) I think we could add willreturn iff the standard is C++11 or newer, but<br>
not otherwise. For C++ before 11 and C we can always have infinite loops with constant conditions, IIRC.<o:p></o:p></span></p>
</blockquote>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif">I think what we need to do is propagate information from function attributes to the call sites in the function. For mustprogress functions, all call sites should also be mustprogress
 I think. If the called function is also readnone (as in the const case), we should be able to add willreturn to the call site. So I think for the C++ case, all the needed information should already be available, we just need to make use of it. The question
 is mostly where we should do that? FunctionAttrs?<o:p></o:p></span></p>
</blockquote>
<p class="MsoNormal"><span style="font-size:9.0pt;font-family:"Helvetica",sans-serif"><br>
Attributor does that ;)</span><o:p></o:p></p>
</div>
</blockquote>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Sounds good.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I also put up a patch to add this logic to FunctionAttrs, so we have something we can easily pick for the 12.x release:  <a href="https://urldefense.com/v3/__https:/reviews.llvm.org/D96949__;!!A4F2R9G_pg!J_I-X0Nr0Kj2jNMZfP3ddh6HPKtrHE6QbWBBksmctUI0Za9kbc_kJmFs2a8tYYZcJn15a8UU$">https://reviews.llvm.org/D96949</a><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Cheers,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Florian<o:p></o:p></p>
</div>
</div>
</div>
</body>
</html>