<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 3, 2017 at 10:47 AM, Michael Kuperstein via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span class="gmail-">On Tue, Jan 3, 2017 at 9:59 AM, Sanjoy Das via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi Michael,<br>
<span><br>
On Mon, Jan 2, 2017 at 11:49 PM, Michael Kuperstein<br>
<<a href="mailto:michael.kuperstein@gmail.com" target="_blank">michael.kuperstein@gmail.com</a>> wrote:<br>
> This sounds right to me.<br>
><br>
> IIUC, historically, readonly and readnone are meant to model the "pure" and<br>
> "const" GCC attributes. These attributes make pretty strong guarantees:<br>
><br>
> "[a pure] function can be subject to common subexpression elimination and<br>
> loop optimization just as an arithmetic operator would be. These functions<br>
> should be declared with the attribute pure [...] Interesting non-pure<br>
> functions are functions with infinite loops or those depending on volatile<br>
> memory or other system resource, that may change between two consecutive<br>
> calls (such as feof in a multithreading environment)."<br>
><br>
> In particular, pure/const imply termination - something that's not entirely<br>
> clear w.r.t readonly. However, apparently, they don't imply nothrow. I've<br>
> actually always thought they *do* imply it - and said so on-list :-) - but<br>
> it looks like GCC itself doesn't interpret them that way. E.g. see John<br>
> Regher's example here: <a href="https://t.co/REzy5m1tT3" rel="noreferrer" target="_blank">https://t.co/REzy5m1tT3</a><br>
> So there's at least one use-case for possibly throwing readonly/readnone.<br>
<br>
</span>One important thing to note then: clang marks const and pure functions<br>
as nounwind *explicitly*. That needs to be fixed.<br>
<br>
<a href="https://godbolt.org/g/SMF4C9" rel="noreferrer" target="_blank">https://godbolt.org/g/SMF4C9</a><br>
<span><br></span></blockquote><div><br></div></span><div>Hah. So it does.</div><div>Eric, this was originally your change. Do I understand GCC's behavior incorrectly?</div></div></div></div></blockquote><div><br></div><div>No, but I was in the office when Kenny wrote ipa-pure-const, which is the equivalent to llvm's pass to find function attributions, and it mostly wasn't thought about.</div><div><br></div><div>GCC isn't as consistent as one may think here.</div><div><br></div><div> /* Non-looping const functions always return normally.</div><div> Otherwise the call might not return or have side-effects</div><div> that forbids hoisting possibly trapping expressions</div><div> before it. */</div><div> int flags = gimple_call_flags (stmt);</div><div> if (!(flags & ECF_CONST)</div><div> || (flags & ECF_LOOPING_CONST_OR_PURE))</div><div> BB_MAY_NOTRETURN (block) = 1;</div><div> } </div><div><br></div><div>It also, for example, will do this:<br><div> double cos (double) __attribute__ ((const));</div><div> double sin (double) __attribute__ ((const));</div><div> double f(double a)</div><div> {</div><div> double b;</div><div> double c,d;</div><div> double (*fp) (double) __attribute__ ((const));</div><div> /* Partially redundant call */</div><div> if (a < 2.0)</div><div> {</div><div> fp = sin;</div><div> c = fp (a);</div><div> }</div><div> else</div><div> {</div><div> c = 1.0;</div><div> fp = cos;</div><div> }</div><div> d = fp (a);</div><div> return d + c;</div><div> }</div></div><div><br></div><div><br></div><div>into</div><div><div> double cos (double) __attribute__ ((const));</div><div> double sin (double) __attribute__ ((const));</div><div> double f(double a)</div><div> {</div><div> double b;</div><div> double c,d;</div><div> double (*fp) (double) __attribute__ ((const));</div><div> /* Partially redundant call */</div><div> if (a < 2.0)</div><div> {</div><div> fp = sin;</div><div> c = fp (a);</div><div> }</div><div> else</div><div> {</div><div> c = 1.0;</div><div> fp = cos;</div><div> temp = fp(a)</div><div> }</div><div> d = phi(c, temp)</div><div> return d + c;</div><div> }</div></div><div><br></div><div>This only works if the second call to sin is guaranteed not to throw, no?<br><br></div><div>In any case, optimizations check throwing separately from pure/const, but i'm not sure it's well thought out here.</div><div><br></div><div>Note that GCC also has a notion of possibly infinite looping pure/const as well:)</div><div><br></div></div></div></div>