[llvm-dev] How to prevent optimizing away a call + its arguments

David Blaikie via llvm-dev llvm-dev at lists.llvm.org
Thu Jun 22 11:51:18 PDT 2017


On Thu, Jun 22, 2017 at 11:40 AM Kuba Mracek via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> > Actually, it should be enough to use:
> >
> > __attribute__((noinline))
> > void please_do_not_optimize_me_away(int arg1, void *arg2) {
> >  asm volatile("":::"memory");
> > }
> >
> > Creating a real barrier is important.
>
> This doesn't work – the call still gets turned into
> please_do_not_optimize_me_away(undef, undef).
>
> > __attribute__((optnone))
>
> optnone works, but I'm actually surprised by this.  I would expect that it
> would only affect the generated code of that function...
>
> Is it guaranteed to work?


Modulo bugs, yes - optnone should have the same behavior as if you put the
function definition in another file and compiled that file with -O0.


> Or is my safest bet still to use:
>
> __attribute__((noinline))
> void please_do_not_optimize_me_away(int arg1, void *arg2) {
>  asm volatile("" :: "r" (arg1), "r" (arg2) : "memory");
> }
>
> (The other benefit compared to optnone is that this will actually generate
> a nice empty function.  Using optnone generates code that stores the
> arguments to the stack.)
>
> Kuba
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170622/7938f66f/attachment.html>


More information about the llvm-dev mailing list