Inlining and alloca

Hi all,

It looks like the C++ optimizer (Sun Studio 10 on Sparc) is not smart enough to avoid inlining functions using alloca when called multiple times.

For example, The following code will overflow the stack if testAlloca is inlined (CC -fast):

[code]#include <alloca.h>

#include <memory.h>

class A {

public:

static void testAlloca () {

void* buffer = alloca (100000);

memset (buffer, 0, 100000);

}

};

int main (void) {

for (int i = 0; i < 10000; i++)

A::testAlloca ();

}[/code]

I could not find a pragma to disable inilining on a specific function. This would help me, as I do not want to rework all the code...

Thanks,

Olivier.

[761 byte] By [ochedru] at [2007-11-26 8:59:15]
# 1

The C++ compiler does not have a way to disable inlining a specific function that was declared inline. If you move the function definition outside the class and don not use the inline keyword, it won't be inlined by the C++ parser:

class A {

public:

static void testAlloca ();

};

void A::testAlloca () {

void* buffer = alloca (100000);

memset (buffer, 0, 100000);

}

You could submit a request for a new feature at bugs.sun.com.

clamage45 at 2007-7-6 23:02:23 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2
As the compiler does not perform "auto inline" as other compilers may do (e.g. gcc), I just changed the code as you told me.This works fine.Thanks!
ochedru at 2007-7-6 23:02:23 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3

Well, there is a lurking "gotcha". At high optimization levels, the optimizer will inline functions even if you don't ask it to. I don't know whether the optimizer recognizes alloca, and avoids inlining such functions.

If you run into a problem at high optimization levels, you can use the option -xinline=no%xxx to prevent inlining function xxx by the optimizer. (You must use the mangled name of a C++ function.)

clamage45 at 2007-7-6 23:02:23 > top of Java-index,Development Tools,Solaris and Linux Development Tools...