Kuperstein, Michael M
2012-Aug-19 11:55 UTC
[LLVMdev] isSafeToSpeculativelyExecute() for CallInst
Hello, Currently, llvm::isSafeToSpeculativelyExecute() always returns false for Call instructions. This has actual performance implications, because loop-invariant code motion makes this check, and will never hoist instructions that are not safe to speculatively execute. Unfortunately, there is currently no way to signal to LICM that a function is safe to speculatively execute. The implementation of isSafeToSpeculativelyExecute() explicitly states that marking a function nounwind and readnone are not sufficient. The rationale, if I understand it correctly, is that even readnone nounwind functions can contain undefined behavior, so hoisting them may be unsafe. A trivial example of this are functions with infinite loops (By the way, I saw an earlier discussion about a "halting" attribute - I understand this never made it into the trunk?) A comment in the implementation also says a readnone nounwind function may have side effects, but this, to the best of my understanding, contradicts the language reference manual which explicitly states that a readnone function may not "access any mutable state visible to caller functions". Does adding a new attribute to signal that a function is speculation-safe sound reasonable? If not, anyone has any other ideas on how this can be resolved? It doesn't seem like LICM can safely make a weaker query than isSafeToSpeculativelyExecute(). If you think the attribute is a good idea, name suggestions for it are also welcome: "pure" and "const" are already used in gcc in a slightly different meaning (equivalent to LLVM "readonly" and "readnone", I think), "nosideeffects" risks confusion with mayHaveSideEffects(), and anything that has the word "speculatively" in it is probably too long. Thanks, Michael --------------------------------------------------------------------- Intel Israel (74) Limited This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). Any review or distribution by others is strictly prohibited. If you are not the intended recipient, please contact the sender and delete all copies. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120819/5f00dcdc/attachment.html>
On Aug 19, 2012, at 2:55 PM, "Kuperstein, Michael M" <michael.m.kuperstein at intel.com> wrote:> Hello, > > Currently, llvm::isSafeToSpeculativelyExecute() always returns false for Call instructions. > This has actual performance implications, because loop-invariant code motion makes this check, and will never hoist instructions that are not safe to speculatively execute.LICM::canSinkOrHoistInst has special handling for hoisting Call Instructiobns. It looks like readonly functions should be hoisted. Do you have test cases which fail ? Thanks, Nadav -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120908/7cbd3fee/attachment.html>
Hi Nadav, On 08/09/12 22:51, Nadav Rotem wrote:> > On Aug 19, 2012, at 2:55 PM, "Kuperstein, Michael M" > <michael.m.kuperstein at intel.com <mailto:michael.m.kuperstein at intel.com>> wrote: > >> Hello, >> Currently, llvm::isSafeToSpeculativelyExecute() always returns false for Call >> instructions. >> This has actual performance implications, because loop-invariant code motion >> makes this check, and will never hoist instructions that are not safe to >> speculatively execute. > > LICM::canSinkOrHoistInst has special handling for hoisting Call Instructiobns. > It looks like readonly functions should be hoisted. Do you have test cases > which fail ?a readonly/readnone function may contain divide by zero. Ciao, Duncan.