xcode 如何处理编译器优化问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/433261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:27:45  来源:igfitidea点击:

How to deal with compiler optimization problems

iphonexcodeoptimizationgccarm

提问by Martin Cote

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?

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

回答by Johannes Schaub - litb

Yes, that's entirely possible. GCC has an attributefor 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) {
    /* ... */
}

回答by Rob Kennedy

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.

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

回答by Alex Brown

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

请检查您是否正确地return从您的函数中获取了值。根据我的经验,以下仅有时有效:

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.

检查编译器警告。