tags:

views:

2426

answers:

6

Is it possible to switch off optimization of a specific function? A friend of mine has the problem that the gcc optimization makes some (unknown to me) µ-controller-code not work. We know which functions it is, but we have no clue of the code itself so the easiest and safest way would probably be to just switch it off for the whole function.

Sadly http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html shows that there is a an optimize function attribute / pragma, but it requires gcc 4.4, which we do not have.

thanks in advance

+2  A: 

I can't say for certain but as far as I'm aware, there's no such possibility. However, optimization should never alter semantics of a well-defined code. The only thing that could happen here is that a variable gets inlined or that order of read/write gets changed.

The first, and probably both problems can be addressed by declaring the variable(s) involved as volatile, thus showing the compiler that no assumptions about its content should be made, based solely on the program flow.

Konrad Rudolph
I considered this one, it just would be nice to have it as a function attribute or something similar. Thanks
Ronny
+6  A: 

If the pragma won't work for you, try splitting the function into its own file, and then compile that file without the optimisation flag set.

frankodwyer
+6  A: 

You could put the function in a separate file and compile that file without optimization but the better solution would be to figure out what is wrong with the code and fix it.

One of the most common bugs that appears when optimization is enabled with gcc is with strict aliasing. Make sure all warnings are enabled and see if you get any warnings that might help you figure out what the problem is. If you can't figure it out, try to reduce the problem to a small, complete program and post it here.

Robert Gamble
+3  A: 

Short of putting the function in its own file, I don't think there is any way to turn off optimization on a function by function level using earlier versions of GCC. But instead of switching off the optimization for this one function, you could try selecting switching off certain types of optimizations in the whole program. While you have identified a bug in this particular function, this likely points to the existence of other undiscovered bugs.

As others pointed out, it's likely that the culprit is optimizations regarding 'strict aliasing'. While in the long run you probably should fix the code in question, in the short run you could play with adding '-fno-strict-aliasing' to your command line. At -O2 and above, the compiler makes certain assumptions about interactions between pointers. Adding this option tells it not to make these assumptions.

If this fails, and if for some reason the code cannot be fixed, you could then try disabling other optimization options.

Nathan Kurz
A: 

The answers so far have neglected the key words in the original question, which are "microcontroller code" It is very common when writing such code to with to disable optimizations -- aggressive optimizers will "optimize away" whole statements whose side effect is to drive the controller. This is a different world from application coding. For an application in the usual space of programming, I came here looking for the same info in order to avoid having a routine for Kahan summation (see wikipedia) optimized into nothingness. So let's not assume that a change of optimization level generating different program behavior is automatically a sign of bad code. Somethings can be kludged by using the volatile keyword cleverly, and in some cases one should generate the actual assembly language and inspect it. (I believe this can still be done with the -S switch to gcc). Let's remember that C is intended to be a sort of portable assembler, not a kind of COBOL.

Dave

+1  A: 

Hi, I know this is an old post. The GCC page that OP gives actually says: To keep such calls from being optimized away, put asm (""); in the function. I guess this is an easy workaround. Just fine out this, hope it can helper other people like me.

peng