LLVM和Clang中的优化级别

11

我正在处理一个使用LLVM 2.6和llvm-gcc前端进行编译的项目。我正在尝试使用LLVM 3.1和clang进行编译测试。当我这样做时,关于-O5优化级别,我收到了以下错误消息:

error: invalid value '5' in '-O5'

然而,在-O5标志下,LLVM 2.6和llvm-gcc运行良好。我看到了关于Clang优化级别的以下文档:

-O0 -O1 -O2 -Os -O3 -O4
       Specify which optimization level to use.  -O0 means "no optimization": this level compiles the
       fastest and generates the most debuggable code.  -O2 is a moderate level of optimization which
       enables most optimizations.  -Os is like -O2 with extra optimizations to reduce code size.  -O3
       is like -O2, except that it enables optimizations that take longer to perform or that may
       generate larger code (in an attempt to make the program run faster).  On supported platforms, -O4
       enables link-time optimization; object files are stored in the LLVM bitcode file format and whole
       program optimization is done at link time. -O1 is somewhere between -O0 and -O2.

所以我正在尝试弄清楚我正在使用的Makefile中的-O5是在做什么(我没有编写这个Makefile)。这是一些已经改变并曾经与LLVM一起使用的东西吗?还是它仍然是一个有用的功能,只是我需要以其他方式激活它。

此外,如果有用的话,我运行的命令基本上是:

/bin/clang -g -c -mcmodel=medium -fstrict-aliasing -Wstrict-aliasing -O5 -emit-llvm -fkeep-inline-functions -fno-stack-protector -c -o foo.bc foo.cpp

如果有必要的话,我还在运行 Linux (Ubuntu 10.04) x86_64 系统。


对于clang 11.0,使用大于3的任何数字都会显示使用-O3。例如,clang++ -O5 test.cc -o test会显示warning: optimization level '-O5' is not supported; using '-O3' instead - jdhao
2个回答

15

Gcc将任何 n ≥ 4 的 -On 视为 -O3

-O标志将接受0-9的任何数字,但只实现了0-3,我认为没有人打算很快实现4-9。事实上,人们使用这种 -O9习惯有点令人不安,因为它暗示他们想要gcc可能提供的每一个可能的优化,即使它没有任何益处也会让编译需要几周时间。

不应使用更高的级别,只是具有低质量的Makefile。

所以指定-O9有点像“但这个去十一!”-类型的无意义废话

因此,在使用gcc编译器时,您有有效的 -O 变体:-O0、-O1、-O2、-O3。但驱动程序将任何 -On 静默地转换为 -O3。

LLVM(clang和llvm-gcc驱动程序的两个变体)仅支持 -O4 级别(-O4-O3 -flto 完全相同)。因此,不应在没有测试的情况下使用 -O4,因为lto更慢,可能会破坏您的程序。


2
可能是Makefile中的一个错误,llvm-gcc没有识别出来,但clang识别了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接