What GCC optimization flags and techniques are safe across CPUs?

烈酒焚心 提交于 2021-01-27 12:16:59

问题


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.

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


回答1:


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

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 is always a good idea, it's mostly used for avoiding branches.

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.



来源:https://stackoverflow.com/questions/52435573/what-gcc-optimization-flags-and-techniques-are-safe-across-cpus

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!