Shishir V Jessu via llvm-dev
2020-Jun-09 18:01 UTC
[llvm-dev] Preventing function call from being optimized out in LTO
Hello, I am adding function calls to an LLVM link-time optimization (LTO) pass, using the IRBuilder::CreateCall method. I want these calls to remain in the final x86 binary at any optimization level, but on levels -O2 and -O3, some of these calls are being optimized out. So far, I've tried adding each function in the program (excluding LLVM intrinsics) to the llvm.used set, and I've also set noinline and optnone attributes on each function in the program. This has allowed me to retain *most, *but not all, of the calls I've added with IRBuilder::CreateCall. Furthermore, I have confirmed that all the calls I've created are present in the LLVM IR that results immediately after my pass. Thus, I know some future LTO pass is optimizing out some of these calls. How can I ensure that none of the calls I add are optimized out? Thanks for your help! Best, Shishir Jessu -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200609/ae51a7d1/attachment.html>
David Blaikie via llvm-dev
2020-Jun-09 18:12 UTC
[llvm-dev] Preventing function call from being optimized out in LTO
optnone on such functions should suffice - well, unless the calls turn out to be dead & I don't think there's anything you can do to thwart dead code removal. So what are you trying to preserve the function calls for? On Tue, Jun 9, 2020 at 11:01 AM Shishir V Jessu via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hello, > > I am adding function calls to an LLVM link-time optimization (LTO) pass, > using the IRBuilder::CreateCall method. I want these calls to remain in > the final x86 binary at any optimization level, but on levels -O2 and -O3, > some of these calls are being optimized out. > > So far, I've tried adding each function in the program (excluding LLVM > intrinsics) to the llvm.used set, and I've also set noinline and optnone > attributes on each function in the program. This has allowed me to retain *most, > *but not all, of the calls I've added with IRBuilder::CreateCall. > > Furthermore, I have confirmed that all the calls I've created are present > in the LLVM IR that results immediately after my pass. Thus, I know some > future LTO pass is optimizing out some of these calls. > > How can I ensure that none of the calls I add are optimized out? Thanks > for your help! > > Best, > Shishir Jessu > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://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/20200609/156f4427/attachment.html>
Shishir V Jessu via llvm-dev
2020-Jun-09 18:54 UTC
[llvm-dev] Preventing function call from being optimized out in LTO
Hi David, By "dead" do you mean unreachable? My understanding was that the removal of dead code is simply another optimization, which should be disabled after adding "optnone" (and adding the function to llvm.used so the function doesn't later get deleted entirely). I am instrumenting certain basic blocks in an LLVM pass, and would like to compile a binary which structures things the same way the LLVM pass does, to analyze some behavior. I observe that my calls are not removed on -O0 and -O1 for several programs, so it should be the case that the calls are removed on higher optimization levels - but then adding "optnone" isn't protecting all of those calls from being removed. If you have any more insight I'd appreciate it! Thanks for your help. On Tue, Jun 9, 2020 at 1:13 PM David Blaikie <dblaikie at gmail.com> wrote:> optnone on such functions should suffice - well, unless the calls turn out > to be dead & I don't think there's anything you can do to thwart dead code > removal. So what are you trying to preserve the function calls for? > > On Tue, Jun 9, 2020 at 11:01 AM Shishir V Jessu via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> Hello, >> >> I am adding function calls to an LLVM link-time optimization (LTO) pass, >> using the IRBuilder::CreateCall method. I want these calls to remain in >> the final x86 binary at any optimization level, but on levels -O2 and -O3, >> some of these calls are being optimized out. >> >> So far, I've tried adding each function in the program (excluding LLVM >> intrinsics) to the llvm.used set, and I've also set noinline and optnone >> attributes on each function in the program. This has allowed me to retain *most, >> *but not all, of the calls I've added with IRBuilder::CreateCall. >> >> Furthermore, I have confirmed that all the calls I've created are present >> in the LLVM IR that results immediately after my pass. Thus, I know some >> future LTO pass is optimizing out some of these calls. >> >> How can I ensure that none of the calls I add are optimized out? Thanks >> for your help! >> >> Best, >> Shishir Jessu >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> https://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/20200609/7e5b0d76/attachment.html>
Tobias Hieta via llvm-dev
2020-Jun-10 06:04 UTC
[llvm-dev] Preventing function call from being optimized out in LTO
I don't know about calling the internal IR stuff directly - but when I ran into this in C code I added __attribute__((used)) to the function to not have it be optimized out. On Tue, Jun 9, 2020 at 8:01 PM Shishir V Jessu via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hello, > > I am adding function calls to an LLVM link-time optimization (LTO) pass, > using the IRBuilder::CreateCall method. I want these calls to remain in > the final x86 binary at any optimization level, but on levels -O2 and -O3, > some of these calls are being optimized out. > > So far, I've tried adding each function in the program (excluding LLVM > intrinsics) to the llvm.used set, and I've also set noinline and optnone > attributes on each function in the program. This has allowed me to retain *most, > *but not all, of the calls I've added with IRBuilder::CreateCall. > > Furthermore, I have confirmed that all the calls I've created are present > in the LLVM IR that results immediately after my pass. Thus, I know some > future LTO pass is optimizing out some of these calls. > > How can I ensure that none of the calls I add are optimized out? Thanks > for your help! > > Best, > Shishir Jessu > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://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/20200610/76a31ab8/attachment.html>
Johannes Doerfert via llvm-dev
2020-Jun-10 14:33 UTC
[llvm-dev] Preventing function call from being optimized out in LTO
On 6/9/20 1:01 PM, Shishir V Jessu via llvm-dev wrote:> Hello, > > I am adding function calls to an LLVM link-time optimization (LTO) pass, > using the IRBuilder::CreateCall method. I want these calls to remain in the > final x86 binary at any optimization level, but on levels -O2 and -O3, some > of these calls are being optimized out. > > So far, I've tried adding each function in the program (excluding LLVM > intrinsics) to the llvm.used set, and I've also set noinline and optnone > attributes on each function in the program. This has allowed me to > retain *most, > *but not all, of the calls I've added with IRBuilder::CreateCall. > > Furthermore, I have confirmed that all the calls I've created are present > in the LLVM IR that results immediately after my pass. Thus, I know some > future LTO pass is optimizing out some of these calls. > > How can I ensure that none of the calls I add are optimized out? Thanks for > your help!The answer, in short, is: Don't run any optimizations/normalizations or, better, only place calls where they are not statically dead. Basically, if you want optimization, O2/O3, you get them. We even have "reaosnable" way to tell optimizations to ignore some part of the code, e.g., optnone, but even then, some trivial things will be removed (think `if (false) foo();`). That said, you can make the code "conditionally life" to avoid this. There are many ways, one would be something like: Before: ``` int foo(int a) { if (a > 3) return a-1; return 0; } ``` After: ``` int return_zero() __attribute__((pure)) // linked in as object file int foo(int a) { if (return_zero() != 0) goto calls; if (a > 3) { call1: my_new_call(1, a); return a-1; } call2: my_new_call(2, a); return 0; calls: switch (return_zero()) { case 1: goto call1; case 2: goto call2; default: goto call2; }; } Now even if you inline the above for a call site like `foo(0)`, the `my_new_call(1, a)` call site will still be considered life, thus not be removed. Hope this helps.> > Best, > Shishir Jessu > > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://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/20200610/68cc9f54/attachment.html>
Shishir V Jessu via llvm-dev
2020-Jun-14 21:06 UTC
[llvm-dev] Preventing function call from being optimized out in LTO
Hi Tobias, Thanks for your response! I was doing this as well, but it turns out adding __attribute__((used)) prevents the function itself from being removed, but does not prevent *all calls to the function* from being removed, which is what I was after. I fixed it by changing some IR that was resulting in unconditionally dead branches ("br i1 false"). Best, Shishir On Wed, Jun 10, 2020 at 1:04 AM Tobias Hieta <tobias at plexapp.com> wrote:> I don't know about calling the internal IR stuff directly - but when I ran > into this in C code I added __attribute__((used)) to the function to not > have it be optimized out. > > On Tue, Jun 9, 2020 at 8:01 PM Shishir V Jessu via llvm-dev < > llvm-dev at lists.llvm.org> wrote: > >> Hello, >> >> I am adding function calls to an LLVM link-time optimization (LTO) pass, >> using the IRBuilder::CreateCall method. I want these calls to remain in >> the final x86 binary at any optimization level, but on levels -O2 and -O3, >> some of these calls are being optimized out. >> >> So far, I've tried adding each function in the program (excluding LLVM >> intrinsics) to the llvm.used set, and I've also set noinline and optnone >> attributes on each function in the program. This has allowed me to retain *most, >> *but not all, of the calls I've added with IRBuilder::CreateCall. >> >> Furthermore, I have confirmed that all the calls I've created are present >> in the LLVM IR that results immediately after my pass. Thus, I know some >> future LTO pass is optimizing out some of these calls. >> >> How can I ensure that none of the calls I add are optimized out? Thanks >> for your help! >> >> Best, >> Shishir Jessu >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org >> https://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/20200614/a6c152f5/attachment.html>