David Sela
2014-Sep-04 11:42 UTC
[LLVMdev] Conditions that cause Clang refuse inlining a function
Hi, I want to have some functions in my code inline so I use the *inline * keyword: *inline void foo() {}* On some functions the compiler inlines the function but it fails to do so on other functions and thus I get a linkage error: *error: undefined reference to 'foo'* What are the conditions that make the compiler refuse inline? Thanks, David p.s. I know that there are ways to pass compilation and let the compiler decide if to inline like using *__attribute__((always_inline))* but I must have the function inline. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140904/789c346f/attachment.html>
Anton Korobeynikov
2014-Sep-04 12:30 UTC
[LLVMdev] Conditions that cause Clang refuse inlining a function
Hello First of all, such a question definitely belongs to cfe-dev, not llvmdev. Next, 'inline' is certainly a hint, and the full the answer is a first entry here: http://clang.llvm.org/compatibility.html#inline On Thu, Sep 4, 2014 at 3:42 PM, David Sela <sela.david at gmail.com> wrote:> Hi, > > I want to have some functions in my code inline so I use the inline keyword: > > inline void foo() {} > > On some functions the compiler inlines the function but it fails to do so on > other functions and thus I get a linkage error: > > error: undefined reference to 'foo' > > What are the conditions that make the compiler refuse inline? > > Thanks, > David > > p.s. I know that there are ways to pass compilation and let the compiler > decide if to inline like using __attribute__((always_inline)) but I must > have the function inline. > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >-- With best regards, Anton Korobeynikov Faculty of Mathematics and Mechanics, Saint Petersburg State University
David Sela
2014-Sep-04 12:42 UTC
[LLVMdev] Conditions that cause Clang refuse inlining a function
Hi Anton, Thanks for the answer. The link you provided doesn't answer the question. It lists the ways to overcome the linking error if the inline was not successful. The only condition that is found there to make the compiler not inline is "when compiling without optimization". I asked the question in the forum because I think that this info is not documented and most chances that the people that know the code inside-out will know the answer. Thanks, David On Thu, Sep 4, 2014 at 3:30 PM, Anton Korobeynikov <anton at korobeynikov.info> wrote:> Hello > > First of all, such a question definitely belongs to cfe-dev, not llvmdev. > > Next, 'inline' is certainly a hint, and the full the answer is a first > entry here: http://clang.llvm.org/compatibility.html#inline > > On Thu, Sep 4, 2014 at 3:42 PM, David Sela <sela.david at gmail.com> wrote: > > Hi, > > > > I want to have some functions in my code inline so I use the inline > keyword: > > > > inline void foo() {} > > > > On some functions the compiler inlines the function but it fails to do > so on > > other functions and thus I get a linkage error: > > > > error: undefined reference to 'foo' > > > > What are the conditions that make the compiler refuse inline? > > > > Thanks, > > David > > > > p.s. I know that there are ways to pass compilation and let the compiler > > decide if to inline like using __attribute__((always_inline)) but I must > > have the function inline. > > > > _______________________________________________ > > LLVM Developers mailing list > > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev > > > > > > -- > With best regards, Anton Korobeynikov > Faculty of Mathematics and Mechanics, Saint Petersburg State University >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140904/b83c06c5/attachment.html>
Joerg Sonnenberger
2014-Sep-04 13:51 UTC
[LLVMdev] Conditions that cause Clang refuse inlining a function
On Thu, Sep 04, 2014 at 02:42:17PM +0300, David Sela wrote:> I want to have some functions in my code inline so I use the *inline * > keyword: > > *inline void foo() {}* > > On some functions the compiler inlines the function but it fails to do so > on other functions and thus I get a linkage error: > > *error: undefined reference to 'foo'* > > What are the conditions that make the compiler refuse inline?C99 says that inline is just a hint. You can force the compiler to emit an instance by adding a separate prototype without inline, i.e. "void foo();" in one file. You can also specify static, in which case a local (outlined) version is used. Joerg
Reid Kleckner
2014-Sep-04 16:06 UTC
[LLVMdev] Conditions that cause Clang refuse inlining a function
On Thu, Sep 4, 2014 at 4:42 AM, David Sela <sela.david at gmail.com> wrote:> Hi, > > I want to have some functions in my code inline so I use the *inline * > keyword: > > *inline void foo() {}* > > On some functions the compiler inlines the function but it fails to do so > on other functions and thus I get a linkage error: > > *error: undefined reference to 'foo'* > > What are the conditions that make the compiler refuse inline? >There are many reasons: - recursion - varargs - code size - general unprofitability - compiling with -O0 It sounds like you're having trouble with C99 inline. Make sure to have a single .c file that redeclares the inline function without 'inline' like so: // foo.h inline int foo() { return 42; } // foo.c int foo(); The redeclaration will force the compiler to emit a strong definition in that TU, preventing linkage errors, so long as you only do it in one TU. See here for more info: http://www.greenend.org.uk/rjk/tech/inline.html -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140904/caa937e0/attachment.html>