[llvm-dev] Function attributes for memory side-effects

Reid Kleckner via llvm-dev llvm-dev at lists.llvm.org
Wed Apr 29 14:12:58 PDT 2020


On Tue, Apr 28, 2020 at 12:58 PM Ejjeh, Adel via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> 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)
>

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.


> , 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.
>

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.

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.

HTH
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200429/2cfb5e6e/attachment.html>


More information about the llvm-dev mailing list