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

Kuba Mracek via llvm-dev llvm-dev at lists.llvm.org
Thu Jun 22 11:31:41 PDT 2017


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



More information about the llvm-dev mailing list