Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with compiler optimization problems

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?

like image 474
Martin Cote Avatar asked Dec 05 '22 07:12

Martin Cote


2 Answers

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) {
    /* ... */
}
like image 106
Johannes Schaub - litb Avatar answered Jan 02 '23 05:01

Johannes Schaub - litb


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.

like image 37
Rob Kennedy Avatar answered Jan 02 '23 05:01

Rob Kennedy