4

I'm having an annoying problem with my iPhone app. Whenever I set the optimization level to something other than "None", I get computation errors. This only happens in when building for the iPhone SDK (the iPhone Simulator is always fine).

I wouldn't mind disabling optimizations in release mode, but the application is a tiny bit too slow when I do that.

The application is complex, so it is hard to locate the part that is too aggressively optimized.

I think that the problem is on the GCC side since it seems to have problem optimizing the code for the ARM architecture.

Is there a way to only disable optimizations only for certain part of the code? How would you deal with that kind of issue?

Alex Brown
  • 41,819
  • 10
  • 94
  • 108
Martin Cote
  • 28,864
  • 15
  • 75
  • 99
  • Solution for Xcode 4 (Apple LLVM Compiler): http://stackoverflow.com/questions/5625624/trouble-disabling-llvm-optimizations-via-pragma?lq=1 – CodeSmile Aug 22 '12 at 16:15

3 Answers3

12

Yes, that's entirely possible. GCC has an attribute for that:

/* disable optimization for this function */
void my_function(void) __attribute__((optimize(0)));

void my_function(void) {
    /* ... */
}

Sets the optimization level for that function to -O0. You can enable/disable specific optimizations:

/* disable optimization for this function */
void my_function(void) __attribute__((optimize("no-inline-functions")));

void my_function(void) {
    /* ... */
}
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • That "answer" from above does NOT work. __attribute__((optimize)) as well as the coresponding pragmas are not available for GCC 4.0 / 4.2 and therefore don't work for the iPhone. –  Jan 08 '10 at 10:02
  • I wonder why this answer was marked as accepted, optimize attribute is unknown by the compiler. Or can someone tell me how can make this work? thanks! – Fede Mika Nov 09 '11 at 00:27
  • For Apple LLVM Compiler (Xcode 4.0+) follow this advice: http://stackoverflow.com/questions/5625624/trouble-disabling-llvm-optimizations-via-pragma?lq=1 – CodeSmile Aug 22 '12 at 16:14
  • When you write `gcc` on Mac, you don't execute GCC, you execute LLVM. – Kristian Spangsege Apr 22 '13 at 20:59
4

If optimization changes your program's behavior, you might unwittingly be relying on undefined or implementation-defined behavior. It could be worth taking a closer look at your code with an eye toward assumptions about variables' values and orders of evaluation.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 1
    Interesting. In my case, the problematic code is in an open source 3rd party library. The code base is huge, so it's going to be hard to nail it down. – Martin Cote Jan 11 '09 at 21:24
0

Please check you are properly returning values from your functions. In my experience, the following only sometimes works:

int myFunc()
{
  x+7;
}

note the deliberate and unsafe omission of the return keyword

due to the register being used in the expression calculation being the same as the return register.

When optimisations are turned on, register use changes and the function fails to do what you want.

Check your compiler warnings.

Alex Brown
  • 41,819
  • 10
  • 94
  • 108