Ronny Brendel
Ronny Brendel

Reputation: 4845

Switching off optimization for a specific function in gcc 4.2.2

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

Upvotes: 13

Views: 20494

Answers (7)

DukeCityPal
DukeCityPal

Reputation: 21

For people who are still using older version of gcc: Newer version of gcc solves this problem. I have successfully used this in gcc-4.5.1. Goodluck.

Upvotes: 2

peng
peng

Reputation: 131

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.

Upvotes: 3

Woodchuck
Woodchuck

Reputation:

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

Upvotes: 5

Konrad Rudolph
Konrad Rudolph

Reputation: 546153

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.

Upvotes: 3

Robert Gamble
Robert Gamble

Reputation: 109162

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.

Upvotes: 7

Nathan Kurz
Nathan Kurz

Reputation: 1699

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.

Upvotes: 5

frankodwyer
frankodwyer

Reputation: 14048

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.

Upvotes: 12

Related Questions