c - GCC pragma 优化不覆盖 `-Og` 命令行设置

标签 c gcc compiler-optimization

是否可以为单个函数或编译单元使用 O2/O3 覆盖 -Og GCC 优化级别?

如果 -Og 用于命令行。

例如,this function doing saturating addition在 ARM Cortex-M4 上。只有当我使用 -O2 编译时,它才会编译为 ssat,但最右边的编译器使用 -Og 并忽略 pragma。

另一方面,使用较低优化级别的 pragma 是可行的,即指定 O0 作为 pragma 在命令行中覆盖 -Og

这是预期的行为吗?

最佳答案

Is it possible to override the -Og GCC optimization level with O2/O3 for a single function or compilation unit?

是也不是。 The documentation for the optimize attribute ,pragma 文档所指的是:

The optimize attribute is used to specify that a function is to be compiled with different optimization options than specified on the command line.

因此属性或编译指示确实覆盖了命令行。然而,这适用于每个功能级别,而 the -Og option has both per-function and global effects :

Like -O0, -Og completely disables a number of optimization passes so that individual options controlling them have no effect.

因此,虽然使用属性或 pragma 设置优化级别 O2O3 确实会打开该级别中包含的所有单独优化,但这样做可能不会具有在命令行上设置它们的全部效果。

另请注意文档的警告

The optimize attribute should be used for debugging purposes only. It is not suitable in production code.

我认为您正在调试,因为这是 -Og 的目的,但是您应该从该评论中了解到的是您不应该依赖用于强制打开或关闭优化选项的技术。正如您所发现的,它可能无法有效或充分地做到这一点。

Is this expected behavior?

您描述的行为似乎与文档一致,尽管它们没有指定细节。然而,您描述它的方式是,当在命令行上设置 -Og 时编译指示被忽略——这不是预期的,我认为不准确。

关于c - GCC pragma 优化不覆盖 `-Og` 命令行设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56879091/

相关文章:

gcc - Fortran 77 提示常见 block

ld - Binutils LD 创建超大文件

c++ - 编译器是否有某些优化启发式来支持分支预测?如果不是,为什么不呢?

c - 是否可以通过编程关闭电脑风扇+处理器风扇?

c - 为什么我们需要 "-pthread"标志来编译 c 文件

gcc - 为什么 C 允许缺少函数声明?

c++ - gcc -O0 在矩阵大小为 2 的幂(矩阵转置)上优于 -O3

c - 在 CUDA C 中使用简单的设备功能获取 "multiple definition"错误

应用程序能否以编程方式生成自己的堆栈跟踪?

linux - .bash_profile 中的路径设置不会反射(reflect)在整个系统中