<div dir="ltr"><div dir="ltr">On Tue, Apr 28, 2020 at 12:58 PM Ejjeh, Adel via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">





<div lang="EN-US">
<div>
<p class="MsoNormal"><span style="font-size:11pt">Specifically, I want to be able to know when a called function does not have any side effects (e.g.
 math library functions like sqrt)</span></p></div></div></blockquote><div><br></div><div>Apologies for the pedantry, but I believe sqrt may set errno, so it actually can have side effects. :( See -fno-math-errno and the documentation around it.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div lang="EN-US"><div><p class="MsoNormal"><span style="font-size:11pt">, and was wondering if there are attributes that specify this behavior (I know there is the ‘noread’ attribute but wasn’t sure if there’s something similar for writes)? Also, how can I tell clang to generate those attributes
 at the function declaration? Any information would be helpful.</span></p></div></div></blockquote><div><br></div><div>Yep, I believe the IR attributes are `readonly` and `readnone`. Readonly implies no side effects (no writes), and readnone implies no memory dependencies at all. The return value is a pure function of the arguments. Two calls with the same arguments can be folded together.</div><div><br></div><div>There are a couple of passes (Attributor, FunctionAttrs, I'm not up-to-date) that will infer these attributes if they can see a precise definition of the function body. Learning anything interesting usually requires expanding the scope of analysis with LTO, so these passes can look across translation unit boundaries.</div><div><br></div><div>HTH</div></div></div>