6

Is it possible to prevent the gcc compiler from inlining a particular function. If so, how?

Don't tell me to reduce the optimization level. I want all optimizations on, but mark a particular function to no be inlined by the compiler, just like volatile in case of variables.

And the reason I want to do this is because my function uses inline assembly defined labels, which gcc messes up when it inlines the function, as inlining causes gcc to create multiple instances of that label.

MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
  • Please explain your problem more precisely. Multiple instances of a label? This should never happen. Which version of gcc is giving you that? You are sure that you only have the function defined in one and single compilation unit? – Jens Gustedt Nov 02 '11 at 11:39
  • @Jens, the reason why this happens is that inlined functions are placed directly inside the code, so if you have an inline assembly defined label inside that funtion, the compiler will also duplicate it wherever it inserts code for that inlined function. – MetallicPriest Nov 02 '11 at 11:47
  • 4
    Note that you do not actually need to prevent inlining to work around this; you can simply use the anonymous relative labels (0,...,9) instead. – Stephen Canon Nov 02 '11 at 11:53
  • 3
    Preventing inlining for something that's important enough to have assembly in it has some serious code smell. Sounds like something you're doing needs to be rethought. It may indeed be the only way, but that sure would be a mighty unlikely situation... – Brian Knoblauch Nov 02 '11 at 12:21
  • @MetallicPriest, so you are asking the wrong question. What would be interesting for you would be to have flow control that doesn't need absolute labels. Since gcc seems to be convinced that your code is pretty small (otherwise it wouldn't inline) this can't be too complicated. – Jens Gustedt Nov 02 '11 at 14:14

6 Answers6

14

You should use the noinline attribute

like this :

void the_method_you_dont_want_to_inline() __attribute__ ((noinline))
{
  ...
}

or in recent versions of GCC :

__attribute__((noinline)) void the_method_you_dont_want_to_inline()
{
  ...
}
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
2
void foo() __attribute__ ((noinline)) { }
duedl0r
  • 9,289
  • 3
  • 30
  • 45
2

You have to set the argument __attribute__((noinline)) to the function.

This would look like something like this:

void __attribute__((noinline)) MyFunction(void)
{
    printf("This will never be inlined");
}
DipSwitch
  • 5,470
  • 2
  • 20
  • 24
2

__attribute__(noinline) is the closes you're going to get.

rubenvb
  • 74,642
  • 33
  • 187
  • 332
1

__attribute__(noinline) should be placed first, like shown below. Otherwise the compiler will complain that attributes are not allowed on a function-definition.

__attribute__(noinline) void my_function( void * arg )
{
  .....
}
MetallicPriest
  • 29,191
  • 52
  • 200
  • 356
1

Another way is to give the attribute for the declaration, not the definition of the function; e.g.

static void foo() __attribute__ ((noinline));

and later

static void foo() {
//code here
}

But perhaps you can still inline the function by using local labels with __label__ inside it, something like

static void inline foo() {
   // untested!
   __label__ foo;
  foo:
    asm ("do something tricky");
    goto foo;
}

But MetallicPriest, you should give more details, and perhaps show more of your code and goals, to get better help!

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547