如何处理编译器优化问题

时间:2022-08-11 02:20:02

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).

我的iPhone应用程序遇到了烦人的问题。每当我将优化级别设置为“无”以外的其他级别时,我就会出现计算错误。这只发生在为iPhone SDK构建时(iPhone模拟器总是很好)。

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.

我认为问题出在GCC方面,因为它似乎在优化ARM体系结构的代码时遇到了问题。

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

有没有办法只对代码的某些部分禁用优化?你会如何处理这类问题?

3 个解决方案

#1


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

是的,这完全有可能。 GCC有一个属性:

/* 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:

将该函数的优化级别设置为-O0。您可以启用/禁用特定优化:

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

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

#2


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.

如果优化改变了程序的行为,您可能会在不知不觉中依赖于未定义或实现定义的行为。值得仔细研究您的代码,着眼于对变量值和评估顺序的假设。

#3


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

请注意return关键字的故意和不安全遗漏

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.

检查编译器警告。

#1


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

是的,这完全有可能。 GCC有一个属性:

/* 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:

将该函数的优化级别设置为-O0。您可以启用/禁用特定优化:

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

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

#2


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.

如果优化改变了程序的行为,您可能会在不知不觉中依赖于未定义或实现定义的行为。值得仔细研究您的代码,着眼于对变量值和评估顺序的假设。

#3


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

请注意return关键字的故意和不安全遗漏

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.

检查编译器警告。