哪些 GCC 优化标志和技术跨 CPU 是安全的? [英] What GCC optimization flags and techniques are safe across CPUs?

查看:56
本文介绍了哪些 GCC 优化标志和技术跨 CPU 是安全的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译/链接旨在用于 ISA 的所有实现(例如 x86-64)的 C/C++ 库或程序时,从正确性和运行时性能的角度来看,哪些优化标志是安全的?我希望优化能够产生正确的结果,并且不会对特定 CPU 的性能产生不利影响.例如,我想避免在第 8 代英特尔酷睿 i7 上产生运行时性能改进的优化标志,但在 AMD Ryzen 上导致性能下降.

When compiling/linking C/C++ libraries or programs that are meant to work on all implementations of an ISA (e.g. x86-64), what optimization flags are safe from the correctness and run-time performance perspectives? I want optimizations that yield correct results and won't be detrimental performance-wise for a particular CPU. E.g I would like to avoid optimization flags that yield run-time performance improvements on an 8th-gen Intel Core i7, but result in performance degradation on an AMD Ryzen.

PGO、LTO 和 -O3 安全吗?它是否仅依赖于 -march-mtune(或不存在)?

Are PGO, LTO, and -O3 safe? Is it solely dependent on -march and -mtune (or the absence thereof)?

推荐答案

假设您的代码定义良好,它们都应该是安全的".

They're all supposed to be "safe", assuming that your code is well defined.

如果您不想专注于特定的 CPU 系列,那么只需将 -march-mtune 放在一边;默认适合通用 x86_64.

If you don't want to specialize for a particular CPU family then just leave -march and -mtune alone; the default suits a generic x86_64.

PGO 总是一个好主意,它主要用于避免分支.

PGO is always a good idea, it's mostly used for avoiding branches.

LTO 和 -O3 可以对不同的代码库产生不同的影响.例如,如果您的代码从向量化中受益,那么 -O3 就比 -O2 有很大优势,但是额外的内联和展开可能会导致更大的代码大小,这对于缓存更有限的系统来说可能是一个劣势.

LTO and -O3 can have different effects on different code-bases. For example, if your code benefits from vectorization then -O3 is a big win over -O2, but the extra inlining and unrolling can lead to larger code sizes, and that can be a disadvantage on systems with more limited caches.

最后,在这里真正有意义的唯一建议是:衡量它,看看什么对您的代码有好处.

In the end, the only advice that ever really means anything here is: measure it and see what's good for your code.

这篇关于哪些 GCC 优化标志和技术跨 CPU 是安全的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆