<div dir="ltr"><div dir="ltr"><div>Hi Johannes,</div><div><br></div><div>This got me puzzled:</div><div><br></div><div>> In case of library functions we actually "know" the side effects and will add the appropriate attributes.</div><div><br></div><div>How can we make sure that a function in the LLVM IR is a specific library function and not something that the user wrote themself? For example, we see a call to "printf" in the code but it could be a function that just happened to be named "printf" that does all sorts of things that we wouldn't expect from a genuine libc printf.</div><div><br></div><div>Alexey<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, Apr 30, 2020 at 11:35 AM Johannes Doerfert 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:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br>
On 4/29/20 4:12 PM, Reid Kleckner via llvm-dev wrote:<br>
 > On Tue, Apr 28, 2020 at 12:58 PM Ejjeh, Adel via llvm-dev <<br>
 > <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br>
 ><br>
 >> Specifically, I want to be able to know when a called function does not<br>
 >> have any side effects (e.g. math library functions like sqrt)<br>
 >><br>
 ><br>
 > Apologies for the pedantry, but I believe sqrt may set errno, so it<br>
 > actually can have side effects. :( See -fno-math-errno and the<br>
 > documentation around it.<br>
 ><br>
 ><br>
 >> , and was wondering if there are attributes that specify this <br>
behavior (I<br>
 >> know there is the ‘noread’ attribute but wasn’t sure if there’s <br>
something<br>
 >> similar for writes)? Also, how can I tell clang to generate those<br>
 >> attributes at the function declaration? Any information would be <br>
helpful.<br>
 >><br>
 ><br>
 > Yep, I believe the IR attributes are `readonly` and `readnone`. Readonly<br>
 > implies no side effects (no writes), and readnone implies no memory<br>
 > dependencies at all. The return value is a pure function of the <br>
arguments.<br>
 > Two calls with the same arguments can be folded together.<br>
 ><br>
 > There are a couple of passes (Attributor, FunctionAttrs, I'm not<br>
 > up-to-date) that will infer these attributes if they can see a precise<br>
 > definition of the function body. Learning anything interesting usually<br>
 > requires expanding the scope of analysis with LTO, so these passes <br>
can look<br>
 > across translation unit boundaries.<br>
<br>
Often true. In case of library functions we actually "know" the side<br>
effects and will add the appropriate attributes. As you said, fast math<br>
flags are needed for math library functions that may otherwise write<br>
errno.<br>
<br>
<br>
The full list of attributes we have so far is:<br>
<br>
    access locations: `readnone`, `inaccessiblememonly`, `argmemonly`, <br>
and `inaccessiblemem_or_argmemonly`<br>
  and access "kinds": `readonly` and `writeonly`<br>
<br>
Except for `readnone` you can combine a location attribute with a "kind"<br>
or have one of either alone. The Attributor does internally derive more<br>
"locations", basically any combination of:<br>
   local memory<br>
   constant memory<br>
   internal global memory<br>
   external global memory<br>
   argument memory<br>
   inaccessible memory<br>
   malloced memory (returned by a function with the `noalias` return <br>
attribute)<br>
   unknown memory<br>
I want to add some/all of these as attributes but didn't find the time<br>
yet.<br>
<br>
<br>
Cheers,<br>
<br>
   Johannes<br>
<br>
<br>
<br>
 ><br>
 > HTH<br>
 ><br>
 ><br>
 > _______________________________________________<br>
 > LLVM Developers mailing list<br>
 > <a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
 > <a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>