137

Say I have this small function in a source file

static void foo() {}

and I build an optimized version of my binary yet I don't want this function inlined (for optimization purposes). is there a macro I can add in a source code to prevent the inlining?

vehomzzz
  • 37,854
  • 69
  • 173
  • 207

8 Answers8

161

You want the gcc-specific noinline attribute.

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 ("");

Use it like this:

void __attribute__ ((noinline)) foo() 
{
  ...
}
Quuxplusone
  • 19,419
  • 5
  • 72
  • 137
alex tingle
  • 6,174
  • 2
  • 23
  • 27
  • 32
    Using gcc 4.4.3 on Arch Linux, I get a syntax error with the attribute placed as above. It works correctly when it precedes the function (e.g., __attribute__ ((noinline)) void foo() {}) – mrkj Apr 16 '10 at 14:24
  • 2
    Arduino also wanted it placed before the function. – Peter N Lewis Feb 24 '12 at 09:49
  • 2
    Edited to fix the attribute syntax. – Quuxplusone Jun 21 '13 at 20:59
  • 1
    The asm("") construct is actually fairly cross-platform and got the job done. I did it for x86 Linux and it did not cause a build problem on PowerPC AIX. Thanks for this useful suggestion! – Marty Nov 06 '14 at 23:58
  • Using gcc 6.2.1 on Arch Linux, no problem. – ManuelSchneid3r Oct 01 '16 at 14:46
  • Just curious, Does using extern keyword For e,g extern void foo(); has the same effect as noinline attribute. – bharath Jan 19 '18 at 10:40
  • 1
    The approach which requires code changes everywhere cannot be reasonably considered an acceptable answer. – ajeh Mar 27 '18 at 14:59
  • @ajeh - I thinks its another instance of the compiler turning a working program into a non-working one. Maybe you can plead your case to GCC. (I'm happy there was a solution to [Labels in GCC inline assembly](https://stackoverflow.com/q/3898435/608639), where the optimizer duplicated our inline ASM and caused multiple symbols with the same name). – jww Dec 28 '18 at 23:10
34

GCC has a switch called

-fno-inline-small-functions

So use that when invoking gcc. But the side effect is that all other small functions are also non-inlined.

Matteo Italia
  • 115,256
  • 16
  • 181
  • 279
lukmac
  • 4,067
  • 8
  • 30
  • 34
  • Didn't work at compiler level. Was using gcc 5.2.1 20150902 (Red Hat 5.2.1-2) – John Greene Feb 16 '17 at 19:04
  • 1
    Either current GCC 6.4 is broken, or this and simpler `-fno-inline` do not work at all. `gdb` still enters methods on step-over. Something is broken, and I doubt it is `gdb`. – ajeh Mar 27 '18 at 15:12
  • It will turn off inline optimization for all, not only for a specified function. – where23 May 06 '19 at 07:36
  • @ajeh Not inlining functions means that they are called normally, doesn’t it? – Melebius Jul 20 '20 at 09:12
23

A portable way to do this is to call the function through a pointer:

void (*foo_ptr)() = foo;
foo_ptr();

Though this produces different instructions to branch, which may not be your goal. Which brings up a good point: what is your goal here?

Andy Ross
  • 10,859
  • 1
  • 28
  • 29
  • 2
    If the pointer is defined at file scope, and not static, it should work since the compiler then can't assume it has its initial value at time of use. If it's a local (as shown) it's almost certainly treated the same as foo(). ("In this decade", he added, looking at the dates) – greggo Aug 23 '16 at 22:43
22

I know the question is about GCC, but I thought it might be useful to have some information about compilers other compilers as well.

GCC's noinline function attribute is pretty popular with other compilers as well. It is supported by at least:

  • Clang (check with __has_attribute(noinline))
  • Intel C/C++ Compiler (their documentation is terrible, but I'm certain it works on 16.0+)
  • Oracle Solaris Studio back to at least 12.2
  • ARM C/C++ Compiler back to at least 4.1
  • IBM XL C/C++ back to at least 10.1
  • TI 8.0+ (or 7.3+ with --gcc, which will define __TI_GNU_ATTRIBUTE_SUPPORT__)

Additionally, MSVC supports __declspec(noinline) back to Visual Studio 7.1. Intel probably supports it too (they try to be compatible with both GCC and MSVC), but I haven't bothered to verify that. The syntax is basically the same:

__declspec(noinline)
static void foo(void) { }

PGI 10.2+ (and probably older) supports a noinline pragma which applies to the next function:

#pragma noinline
static void foo(void) { }

TI 6.0+ supports a FUNC_CANNOT_INLINE pragma which (annoyingly) works differently in C and C++. In C++, it's similar to PGI's:

#pragma FUNC_CANNOT_INLINE;
static void foo(void) { }

In C, however, the function name is required:

#pragma FUNC_CANNOT_INLINE(foo);
static void foo(void) { }

Cray 6.4+ (and possibly earlier) takes a similar approach, requiring the function name:

#pragma _CRI inline_never foo
static void foo(void) { }

Oracle Developer Studio also supports a pragma which takes the function name, going back to at least Forte Developer 6, but note that it needs to come after the declaration, even in recent versions:

static void foo(void);
#pragma no_inline(foo)

Depending on how dedicated you are, you could create a macro that would work everywhere, but you would need to have the function name as well as the declaration as arguments.

If, OTOH, you're okay with something that just works for most people, you can get away with something which is a little more aesthetically pleasing and doesn't require repeating yourself. That's the approach I've taken for Hedley, where the current version of HEDLEY_NEVER_INLINE looks like:

#if \
  HEDLEY_GNUC_HAS_ATTRIBUTE(noinline,4,0,0) || \
  HEDLEY_INTEL_VERSION_CHECK(16,0,0) || \
  HEDLEY_SUNPRO_VERSION_CHECK(5,11,0) || \
  HEDLEY_ARM_VERSION_CHECK(4,1,0) || \
  HEDLEY_IBM_VERSION_CHECK(10,1,0) || \
  HEDLEY_TI_VERSION_CHECK(8,0,0) || \
  (HEDLEY_TI_VERSION_CHECK(7,3,0) && defined(__TI_GNU_ATTRIBUTE_SUPPORT__))
#  define HEDLEY_NEVER_INLINE __attribute__((__noinline__))
#elif HEDLEY_MSVC_VERSION_CHECK(13,10,0)
#  define HEDLEY_NEVER_INLINE __declspec(noinline)
#elif HEDLEY_PGI_VERSION_CHECK(10,2,0)
#  define HEDLEY_NEVER_INLINE _Pragma("noinline")
#elif HEDLEY_TI_VERSION_CHECK(6,0,0)
#  define HEDLEY_NEVER_INLINE _Pragma("FUNC_CANNOT_INLINE;")
#else
#  define HEDLEY_NEVER_INLINE HEDLEY_INLINE
#endif

If you don't want to use Hedley (it's a single public domain / CC0 header) you can convert the version checking macros without too much effort, but more than I'm willing to put in ☺.

nemequ
  • 14,054
  • 30
  • 55
  • Thanks for the link to your project @nemequ. I've asked our other developers to evaluate it for our use. We have diverse architectures. – Daisuke Aramaki Mar 20 '19 at 12:31
  • I'd be very interested to know what they say, *especially* if they're not interested. And, of course, I'm around to answer questions (GitHub issue tracker, e-mail, whatever…). – nemequ Mar 21 '19 at 03:08
14

In case you get a compiler error for __attribute__((noinline)), you can just try:

noinline int func(int arg)
{
    ....
}
John Zwinck
  • 207,363
  • 31
  • 261
  • 371
Sam Liao
  • 36,847
  • 14
  • 49
  • 60
10
static __attribute__ ((noinline))  void foo()
{

}

This is what worked for me.

KenBee
  • 109
  • 1
  • 2
9

Use the noinline attribute:

int func(int arg) __attribute__((noinline))
{
}

You should probably use it both when you declare the function for external use and when you write the function.

Chris Lutz
  • 66,621
  • 15
  • 121
  • 178
2

I work with gcc 7.2. I specifically needed a function to be non-inlined, because it had to be instantiated in a library. I tried the __attribute__((noinline)) answer, as well as the asm("") answer. Neither one solved the problem.

Finally, I figured that defining a static variable inside the function will force the compiler to allocate space for it in the static variable block, and to issue an initialization for it when the function is first called.

This is sort of a dirty trick, but it works.

Ofri Sadowsky
  • 121
  • 1
  • 6
  • You could define your function `inline void foo(void) { ... }` in a header and declare it `extern inline void foo(void);` in a library source file. Following C99 semantics, the compiler would be allowed to inline the function when it pleases AND emit object code in your library. See [Is "inline" without "static" or "extern" ever useful in C99 ?](https://stackoverflow.com/questions/6312597/is-inline-without-static-or-extern-ever-useful-in-c99). – diapir Feb 23 '18 at 08:47