<div dir="ltr">I like the direction of this RFC and agree with Michael's points about it.<div><br></div><div>The "pure" and "const" history is definitely there, but I don't think it makes sense any more. I think narrow, precise, and well specified attributes are *much* better for LLVM's IR, especially as we diversify the set of frontends and language semantics we support.</div><div><br></div><div>There will be plenty of code changes required, but I think the changes are tractable (these are relatively easy to audit for) and not risky. If Sanjoy has the cycles to run with this, fantastic.</div><div><br></div><div>One thing we should make sure to do is update the langref to be *really clear* here. =] But I suspect Sanjoy is all over that.</div><br><div class="gmail_quote"><div dir="ltr">On Mon, Jan 2, 2017 at 11:49 PM Michael Kuperstein via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr" class="gmail_msg"><div class="gmail_msg">This sounds right to me.</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">IIUC, historically, readonly and readnone are meant to model the "pure" and "const" GCC attributes. These attributes make pretty strong guarantees:</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><div class="gmail_msg">"[a pure] function can be subject to common subexpression elimination and loop optimization just as an arithmetic operator would be. These functions should be declared with the attribute pure [...] Interesting non-pure functions are functions with infinite loops or those depending on volatile memory or other system resource, that may change between two consecutive calls (such as feof in a multithreading environment)."</div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">In particular, pure/const imply termination - something that's not entirely clear w.r.t readonly. However, apparently, they don't imply nothrow. I've actually always thought they *do* imply it - and said so on-list :-) - but it looks like GCC itself doesn't interpret them that way. E.g. see John Regher's example here: <a href="https://t.co/REzy5m1tT3" class="gmail_msg" target="_blank">https://t.co/REzy5m1tT3</a></div><div class="gmail_msg">So there's at least one use-case for possibly throwing readonly/readnone.</div><div class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">As a side note, I'm slightly less optimistic about the amount of required code fixes. One thing that comes to mind is that we need to make sure we mark all(?) the intrinsics currently marked readonly/argmemonly/readnone as nothrow. This should be mostly mechanical, I hope, but it's a decent amount of churn.</div></div></div></div><div dir="ltr" class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg">Michael<br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div><div class="gmail_msg"><br class="gmail_msg"></div></div></div></div><div class="gmail_extra gmail_msg"><br class="gmail_msg"><div class="gmail_quote gmail_msg">On 2 January 2017 at 22:18, Sanjoy Das via llvm-dev <span dir="ltr" class="gmail_msg"><<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br class="gmail_msg"><blockquote class="gmail_quote gmail_msg" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">LLVM today does not clearly specify if a function specified to not<br class="gmail_msg">
write to memory (i.e. readonly or readnone) is allowed to throw<br class="gmail_msg">
exceptions.<br class="gmail_msg">
<br class="gmail_msg">
LangRef is ambiguous on this issue. The normative statement is<br class="gmail_msg">
"[readnone/readonly functions] cannot unwind exceptions by calling the<br class="gmail_msg">
C++ exception throwing methods" which does not decide an answer for<br class="gmail_msg">
non C++ languages. It used to say (h/t Daniel Berlin): "This means<br class="gmail_msg">
that it cannot unwind exceptions by calling the C++ exception throwing<br class="gmail_msg">
methods, but could use the unwind instruction.", but that bit of<br class="gmail_msg">
documentation died with the unwind instruction.<br class="gmail_msg">
<br class="gmail_msg">
I'd like to separate unwindability from memory effects, and officially<br class="gmail_msg">
change our stance to be "readonly / readnone functions are allowed to<br class="gmail_msg">
throw exceptions".<br class="gmail_msg">
<br class="gmail_msg">
Here are two supporting reasons:<br class="gmail_msg">
<br class="gmail_msg">
# `resume` is already modeled as readnone<br class="gmail_msg">
<br class="gmail_msg">
The easiest way to verify this is via FunctionAttrs; it infers the<br class="gmail_msg">
following function as readnone:<br class="gmail_msg">
<br class="gmail_msg">
define void @f() personality i8 42 {<br class="gmail_msg">
resume i32 0<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
Modeling `resume` as `readnone` is defensible -- it is a control flow<br class="gmail_msg">
transfer instruction, not so different from `ret`. Moreover, it<br class="gmail_msg">
_cannot_ be modeled as having observable side effects or writes to<br class="gmail_msg">
memory (`resume` cannot send packets over the network or write to a<br class="gmail_msg">
global) because otherwise we'd be unable to inline @f into @g below:<br class="gmail_msg">
<br class="gmail_msg">
define void @f(i32 %x) personality i32 3 {<br class="gmail_msg">
resume i32 %x<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
define i32 @g(i32 %x) personality i32 3 {<br class="gmail_msg">
invoke void @f(i32 %x) to label %normal unwind label %unwind<br class="gmail_msg">
<br class="gmail_msg">
normal:<br class="gmail_msg">
ret i32 0<br class="gmail_msg">
<br class="gmail_msg">
unwind:<br class="gmail_msg">
%t = landingpad i32 cleanup<br class="gmail_msg">
ret i32 %t<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
since it gets rid of a `resume` and thus a side effect (by<br class="gmail_msg">
assumption).<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
# We're probably already there (but we need an audit)<br class="gmail_msg">
<br class="gmail_msg">
All said and done, the situation is not as "loosey goosey" as I made<br class="gmail_msg">
it sound like. mayHaveSideEffects() is defined as "mayWriteToMemory()<br class="gmail_msg">
|| mayThrow()"; and this shows in e.g. EarlyCSE which will refuse to<br class="gmail_msg">
DCE the call to @f in @g<br class="gmail_msg">
<br class="gmail_msg">
declare void @f() readnone<br class="gmail_msg">
<br class="gmail_msg">
define void @g() {<br class="gmail_msg">
call void @f()<br class="gmail_msg">
ret void<br class="gmail_msg">
}<br class="gmail_msg">
<br class="gmail_msg">
unless @f is also marked nounwind.<br class="gmail_msg">
<br class="gmail_msg">
I've already fixed the one other instance I was aware of in<br class="gmail_msg">
<a href="https://reviews.llvm.org/rL290794" rel="noreferrer" class="gmail_msg" target="_blank">https://reviews.llvm.org/rL290794</a> (but I will revert that patch if we<br class="gmail_msg">
decide against this RFC).<br class="gmail_msg">
<br class="gmail_msg">
We won't lose any expressive power either -- if there are situations<br class="gmail_msg">
where we have important optimizations firing under the "readonly<br class="gmail_msg">
implies nounwind" assumption, we can either<br class="gmail_msg">
<br class="gmail_msg">
- Teach FunctionAttrs to infer nounwind for readonly functions with<br class="gmail_msg">
C++ unwind personalities.<br class="gmail_msg">
<br class="gmail_msg">
- For external declarations generated by the compiler (say from the<br class="gmail_msg">
standard library): if the functions are actually nounwind, mark<br class="gmail_msg">
them as nounwind; and not rely on LLVM inferring nounwind from<br class="gmail_msg">
readonly.<br class="gmail_msg">
<br class="gmail_msg">
<br class="gmail_msg">
My (unrealistic?) hope is that this would mostly be a specification<br class="gmail_msg">
change and not involve a lot of code fixes.<br class="gmail_msg">
<br class="gmail_msg">
The change is also trivially upgrade-safe for older bitcode -- calls<br class="gmail_msg">
to readonly / readnone functions that do not throw _may_ get optimized<br class="gmail_msg">
less, but that should not be a correctness problem.<br class="gmail_msg">
<br class="gmail_msg">
What do you think?<br class="gmail_msg">
<br class="gmail_msg">
-- Sanjoy<br class="gmail_msg">
_______________________________________________<br class="gmail_msg">
LLVM Developers mailing list<br class="gmail_msg">
<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="gmail_msg">
</blockquote></div><br class="gmail_msg"></div>
_______________________________________________<br class="gmail_msg">
LLVM Developers mailing list<br class="gmail_msg">
<a href="mailto:llvm-dev@lists.llvm.org" class="gmail_msg" target="_blank">llvm-dev@lists.llvm.org</a><br class="gmail_msg">
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" class="gmail_msg" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="gmail_msg">
</blockquote></div></div>