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

Kuba Mracek via llvm-dev llvm-dev at lists.llvm.org
Wed Jun 21 16:16:02 PDT 2017


Hi llvm-dev,

I have a C function:

__attribute__((__visibility__("default")))
__attribute__((used))
__attribute__((noinline))
void please_do_not_optimize_me_away(int arg1, void *arg2) {
  asm volatile("" :::);
}

(the purpose is that this function will be used dynamically at runtime, perhaps by interposing the function, or via the debugger)

I really thought this will not get optimized out, but I've realized (the hard way) that LLVM will happily optimize a call to this function, and replace all arguments with undef, because it figures out that they're not really needed.

I'm going to fix this by passing the arguments explicitly as inputs to the asm, but is that expected?  Is there any more reasonable way (attribute) of telling that the compiler should really not expect anything from the body of the function, not assume that it's not doing anything, and not optimizing out arguments?

Thanks,
Kuba



More information about the llvm-dev mailing list