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

Matthias Braun via llvm-dev llvm-dev at lists.llvm.org
Thu Jun 22 11:55:35 PDT 2017


It looks like what you are trying to do here is define a weak function. Marking the function as weak should have the desired effect, though last time I looked apple paltforms only had limited support for weak functions...

- Matthias

> On Jun 22, 2017, at 11:31 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?  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



More information about the llvm-dev mailing list