Kuba Mracek via llvm-dev
2017-Jun-21 23:16 UTC
[llvm-dev] How to prevent optimizing away a call + its arguments
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
Mehdi AMINI via llvm-dev
2017-Jun-22 00:25 UTC
[llvm-dev] How to prevent optimizing away a call + its arguments
Hi Kuba, Try: __attribute__(optnone) See https://clang.llvm.org/docs/AttributeReference.html#optnone-clang-optnone -- Mehdi 2017-06-21 16:16 GMT-07:00 Kuba Mracek via llvm-dev <llvm-dev at lists.llvm.org>:> 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 > > _______________________________________________ > 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/20170621/15716cb9/attachment.html>
Joerg Sonnenberger via llvm-dev
2017-Jun-22 14:35 UTC
[llvm-dev] How to prevent optimizing away a call + its arguments
On Wed, Jun 21, 2017 at 05:25:04PM -0700, Mehdi AMINI via llvm-dev wrote:> Hi Kuba, > > Try: > > __attribute__(optnone) > > See > https://clang.llvm.org/docs/AttributeReference.html#optnone-clang-optnoneActually, 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. Joerg
Apparently Analagous Threads
- How to prevent optimizing away a call + its arguments
- How to prevent optimizing away a call + its arguments
- How to generate .bc file using configure && make on Mac OS X?
- Preventing function call from being optimized out in LTO
- Preventing function call from being optimized out in LTO