i think attributes have taken control flow into account. I think readnone and nounwind functions are not safe to speculative execute because the function could run indefinitely, e.g. an infinite loop.<div>-Xin<br><br>On Tuesday, July 14, 2015, Philip Reames <<a href="mailto:listmail@philipreames.com">listmail@philipreames.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 07/14/2015 10:25 PM, Hal Finkel wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
----- Original Message -----<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
From: "Philip Reames" <<a>listmail@philipreames.com</a>><br>
To: "Thomas F Raoux" <<a>thomas.f.raoux@intel.com</a>>, <a>llvmdev@cs.uiuc.edu</a><br>
Sent: Tuesday, July 14, 2015 11:59:49 PM<br>
Subject: Re: [LLVMdev] LICM for function calls<br>
<br>
On 07/14/2015 07:45 AM, Raoux, Thomas F wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi,<br>
<br>
Right now in LICM (and many other transformations) we always assume<br>
it is never safe to speculatively execute a function call.<br>
<br>
The following function always return false for all function calls<br>
except for few intrinsics:<br>
bool llvm::isSafeToSpeculativelyExecute(const Value *V,<br>
                                          const DataLayout *TD) {<br>
...<br>
    case Instruction::Call: {<br>
     if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(Inst)) {<br>
...<br>
     }<br>
      return false; // The called function could have undefined<br>
      behavior or<br>
                    // side-effects, even if marked readnone<br>
                    nounwind.<br>
<br>
In some cases this could have an important performance impact so<br>
I'm looking for a potential way to improve it.<br>
<br>
Is there any combination of attributes which would guarantee that a<br>
function is safe to speculatively execute a function call? (As far<br>
as I can tell there isn't.)<br>
</blockquote>
In general, no there isn't.  The challenge is that a function's<br>
attributes can be control dependent.  For example, you can have a<br>
function which is "readnone argmemonly nounwind" which writes the<br>
entire<br>
heap if the global "c" is true.  Hoisting a call to such a function<br>
above "if (!c)" would be problematic.  I think that in practice this<br>
is<br>
mostly an issue with call site attributes, but I believe the same<br>
conceptual problem applies to attributes on function declarations as<br>
well.<br>
</blockquote>
I'm fairly certain we already consider declaration attributes to have no control dependencies.<br>
</blockquote>
If true, this would be great.  I wasn't sure what the rules here were and was trying to be conservative.  If nothing else, we need to clarify the docs and make sure everyone is actually in agreement about this.<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
  -Hal<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Changing that might be reasonable, but we'd need to spec it<br>
carefully.  I think there's room here for improvement, but it'll<br>
likely<br>
be a slow process at first.<br>
<br>
One area you might look into is the "guaranteed to execute" notion in<br>
LICM.  This gives a basis for hoisting a call out of a loop which<br>
doesn't require speculating it past any relevant control dependence.<br>
You still have to worry about aliasing for safety, but reasoning<br>
about<br>
the semantics of attributes and faults should be a bit more straight<br>
forward.<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Does anybody have a suggestion on what would be the best way to<br>
implement such functionality? Or do you think such optimization is<br>
undesirable?<br>
<br>
Cheers,<br>
Thomas<br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a>LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</blockquote>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a>LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br>
</blockquote></blockquote>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a>LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
</blockquote></div>