<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Feb 17, 2021, at 20:40, Johannes Doerfert <<a href="mailto:johannesdoerfert@gmail.com" class="">johannesdoerfert@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta charset="UTF-8" class=""><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">On 2/17/21 2:19 PM, Florian Hahn wrote:</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><blockquote type="cite" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;" class="">Hi,<br class=""><br class=""><blockquote type="cite" class="">On Feb 17, 2021, at 19:15, Johannes Doerfert <<a href="mailto:johannesdoerfert@gmail.com" class="">johannesdoerfert@gmail.com</a>> wrote:<br class=""><br class=""><br class="">On 2/17/21 12:04 PM, Jeroen Dobbelaere wrote:<br class=""><blockquote type="cite" class=""><blockquote type="cite" class="">I'm confused.<br class=""><br class="">Are you interested in adding `willreturn` to a function via clang?<br class=""></blockquote>I think so (also getting a little confused :( ) ..<br class="">What should the behavior be of a '__attribute__((const))' function ?<br class="">Is the progress guaranteed ?<br class=""><br class="">See <a href="https://www.godbolt.org/z/GoPYhM" class="">https://www.godbolt.org/z/GoPYhM</a><br class=""><br class=""><br class="">// extern "C" ...<br class="">extern int ptrfun1(int, int*, int*) __attribute__((const));<br class=""><br class="">int b[5];<br class=""><br class="">int foo() {<br class="">    int a[10];<br class="">    int index = ptrfun1(5, &a[0], &b[0]);<br class="">    a[index]=10;<br class="">    return a[index];<br class="">}<br class=""><br class="">clang-11 is able to optimize this away<br class=""><br class="">clang-trunc is mixed:<br class="">- for this case, the call will not optimize it away. (as far as I see, since D94106)<br class="">- But if you do not use the return value upfront, it will be optimized away.<br class="">    ...<br class="">    int index = 3;<br class="">    ptrfun1(5, &a[0], &b[0]);<br class="">    ...<br class=""><br class="">If a '__attribute__((const))' function is allowed to not progress, then not all llvm passes are aware of this<br class="">with the current mapping on llvm attributes and imho, we'll need an attribute to indicate that we want<br class="">to have progress. Or, clang should map '__attribute__((const))' to 'readnone willreturn'.<br class=""></blockquote>FWIW, I think removal is correct because you run it as C++ program.<br class=""><br class=""></blockquote>Yep, I think that’s the case.<br class=""><br class=""><blockquote type="cite" class="">This is "just" a phase ordering issue. Run O3 again and trunk happily removes it:<span class="Apple-converted-space"> </span><a href="https://www.godbolt.org/z/95qeah" class="">https://www.godbolt.org/z/95qeah</a><span class="Apple-converted-space"> </span><<a href="https://www.godbolt.org/z/95qeah" class="">https://www.godbolt.org/z/95qeah</a>><br class="">That said, I agree, we should check why this is happening and what to do about it.<br class=""><br class=""></blockquote>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 class=""><br class=""><blockquote type="cite" class="">Now wrt. the attribute lowering (const/pure) I think we could add willreturn iff the standard is C++11 or newer, but<br class="">not otherwise. For C++ before 11 and C we can always have infinite loops with constant conditions, IIRC.<br class=""></blockquote>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?<br class=""></blockquote><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""><span style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none; float: none; display: inline !important;" class="">Attributor does that ;)</span><br style="caret-color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;" class=""></div></blockquote></div><br class=""><div class=""><br class=""></div><div class="">Sounds good.</div><div class=""><br class=""></div><div class="">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://reviews.llvm.org/D96949" class="">https://reviews.llvm.org/D96949</a></div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Florian</div></body></html>