8

I'm using gcc with the -finline-functions optimization for release builds. In order to combat code bloat because I work on an embedded system I want to say don't inline particular functions. The obvious way to do this would be through function attributes ie attribute(noinline). The problem is this doesn't seem to work when I switch on the global -finline-functions optimisation which is part of the -O3 switch.

It also has something to do with it being templated as a non templated version of the same function doesn't get inlined which is as expected.

Has anybody any idea of how to control inlining when this global switch is on?

Here's the code:

#include <cstdlib> #include <iostream> using namespace std; class Base { public: template<typename _Type_> static _Type_ fooT( _Type_ x, _Type_ y ) __attribute__ (( noinline )); }; template<typename _Type_> _Type_ Base::fooT( _Type_ x, _Type_ y ) { asm(""); return x + y; } int main(int argc, char *argv[]) { int test = Base::fooT( 1, 2 ); printf( "test = %d\n", test ); system("PAUSE"); return EXIT_SUCCESS; } 
5
  • 1
    Not related, but identifiers starting with an underscore followed by a capital letter are reserved for the compiler. Commented Mar 16, 2010 at 0:05
  • Shouldn't the __attribute__((noinline)) be attached to the definition? Commented Mar 16, 2010 at 0:09
  • @jpalecek: No thats a compilation error Commented Apr 1, 2010 at 15:42
  • If your are worried about code size, you should likely compile with -O2 Commented Apr 1, 2010 at 15:46
  • @Evan: Sadly I work on an embedded system and I need way more control than that: generally I need everything to be inlined but in the odd case where inlining is generating a lot code I need to stop it to keep code size down. Commented Apr 1, 2010 at 16:18

3 Answers 3

4

The docs for GCC's noinline say:

This function attribute prevents a function from being considered for inlining. If the function does not have side-effects, there are optimizations other than inlining that causes function calls to be optimized away, although the function call is live. To keep such calls from being optimized away, put

 asm (""); 

(see Extended Asm) in the called function, to serve as a special side-effect

I think that what might be happening to you is that since the Base::fooT<> function has no side-effects, GCC is invoking the unspecified other optimizations mentioned above.

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry for the long delay in replying! Nope sadly gcc still inlines it, I had tried this before. I'm using 4.1.1 does anybody know of a bug relating to this? Thanks for the reply though doesn't seem there are an awful lot of knowledge out there about this topic...
2

Try putting the noinline attribute after the static and before the definition like so:

 template<typename _Type_> static __attribute__ (( noinline )) _Type_ fooT( _Type_ x, _Type_ y ); 

This worked for me and seems to work for others too, see: How can I tell gcc not to inline a function?

For some reason it doesn't work putting the noinline attribute after the function or by putting the asm("") in the function body despite what the gcc documentation says.

Comments

1

Little old thread but worth answering. If nothing above works for you there is always simple workaround. You must hide implementation from translation unit where you want to use such a method by putting it in another cpp file.

EDIT

In GCC >= 4.5 there is an attribute noclone which supersede noinline in way of specializing the function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.