问题描述
我最近收到一份有关程序的错误报告,该程序无法使用-O3开关进行编译(请参阅)。特别是,问题在于编译在某个时刻停滞。这个问题通过使用-O2编译来解决(我清楚地知道用-O3编译的程序可能会被破坏,但我不知道-O3可能会挂起编译器)。如果您想重现该问题,请运行
wget http://perso.ens-lyon.fr/xavier.pujol/fplll /libfplll-3.0.12.tar.gz
tar -xf libfplll-3.0.12.tar.gz
cd libfplll-3.0.12
./configure CXXFLAGS = - O3
make
我想知道为什么-O3会挂起编译器,所以我试图追踪问题。首先,我试图找出-O3之间-O2的区别。海湾合作委员会的手册页指出,-O3启用了-O2和以下的开关(让我们称它们为 x
):
-finline-functions -funswitch-loops -fpredictive-commoning -fgcse-after-reload
-ftree-vectorize -fipa -cp -clone
我通过比较调用时的gcc输出与 -Q -O2 --help = optimizers
和 -Q -O3 --help = optimizers
。然后,我计划有选择地删除开关,以找到导致问题的开关。但是,编译工作正常,使用-O2和上面的附加开关,所以我得出结论
-O3!= -O2 x
现在我的问题:是否有人知道-O2和-O3(无证?),有没有人经历过类似的行为?这可能是一个编译器错误?
手册页可能已过时,但您可以找到O2和O3的实际列表。
要获取 -f
优化的完整列表(几乎,请检查更新)我建议你使用 -fverbose-asm -save-temps
(或 -fverbose-asm -S
) - 在asm文件(* .s)的顶部有一个完整列表。
对于gcc-4.6.0,我得到了 x (O2和O3之间的区别)为:
-fgcse-after-reload
-finline-functions
-fipa-cp -clone
-fpredictive-commoning
-ftree-loop-distribute-patterns
-ftree-vectorize
-funswitch-loops
您的问题的另一个信息来源是GCC的来源(文件 gcc / opts.c
和可能 gcc / common.opt
)作为gcc-4.6.0:
/ * -O3优化。 * /
{OPT_LEVELS_3_PLUS,OPT_ftree_loop_distribute_patterns,NULL,1},
{OPT_LEVELS_3_PLUS,OPT_fpredictive_commoning,NULL,1},
/ *内联函数减小大小是一个好主意,-Os
,无论它们是否被声明为内联。 * /
{OPT_LEVELS_3_PLUS_AND_SIZE,OPT_finline_functions,NULL,1},
{OPT_LEVELS_3_PLUS,OPT_funswitch_loops,NULL,1},
{OPT_LEVELS_3_PLUS,OPT_fgcse_after_reload,NULL,1},
{OPT_LEVELS_3_PLUS ,OPT_ftree_vectorize,NULL,1},
{OPT_LEVELS_3_PLUS,OPT_fipa_cp_clone,NULL,1},
我也检查过,gcc在其他文件中检查 -On
(cscope符号搜索 x_optimize
)。 p>
-On 中 n
的唯一附加用法是将其值保存到宏 __ OPTIMIZE __
中。因此,对于此宏的值等于2或3,某些标头的行为可能会有所不同。
更新:中有关于它的问题:
- -O1启用了哪些特定标志(-O2, - O3或-Os)??
touch empty。 c
gcc -O1 -S -verbose-asm empty.c
cat empty.s
I recently received a bug report about a program which fails to compile with the -O3 switch (see https://github.com/cschwan/sage-on-gentoo/issues/66). In particular, the problem is that compilation hangs at a certain point. The issue is solved by compiling with -O2 (I am well aware of the fact that programs compiled with -O3 may be broken, but I did not know that -O3 may hangup the compiler). If you want to reproduce the issue run
wget http://perso.ens-lyon.fr/xavier.pujol/fplll/libfplll-3.0.12.tar.gz
tar -xf libfplll-3.0.12.tar.gz
cd libfplll-3.0.12
./configure CXXFLAGS="-O3"
make
I wondered why -O3 hangs up the compiler and so I tried to track down the issue. First, I tried to find out the the difference between -O2 between -O3. Gcc's man page states that -O3 enables the switches of -O2 and the following ones (lets call them x
):
-finline-functions -funswitch-loops -fpredictive-commoning -fgcse-after-reload
-ftree-vectorize -fipa-cp-clone
I verified that by comparing the output of gcc when invoked with -Q -O2 --help=optimizers
and -Q -O3 --help=optimizers
. I then planned to selectively remove the switches in order to find the one which causes the problem. However, compilation works fine with -O2 and the additional switches above, so I conclude
-O3 != -O2 x
Now my question: Does anybody know if there is a further difference between -O2 and -O3 (undocumented?), has anyone experienced a similar behavior ? Is this maybe a compiler bug ?
Man pages can be outdated, but you can find actual lists for O2 and O3.
To get full list (almost, check "update") of -f
optimization options actually used, I suggest you use the -fverbose-asm -save-temps
(or -fverbose-asm -S
) - there is a full list at a top of asm file (*.s).
For gcc-4.6.0 I got x (the difference between O2 and O3) to be:
-fgcse-after-reload
-finline-functions
-fipa-cp-clone
-fpredictive-commoning
-ftree-loop-distribute-patterns
-ftree-vectorize
-funswitch-loops
Another source of information for your question is the sources of GCC (file gcc/opts.c
and possibly gcc/common.opt
) as gcc-4.6.0:
/* -O3 optimizations. */
{ OPT_LEVELS_3_PLUS, OPT_ftree_loop_distribute_patterns, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_fpredictive_commoning, NULL, 1 },
/* Inlining of functions reducing size is a good idea with -Os
regardless of them being declared inline. */
{ OPT_LEVELS_3_PLUS_AND_SIZE, OPT_finline_functions, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_funswitch_loops, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_fgcse_after_reload, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_ftree_vectorize, NULL, 1 },
{ OPT_LEVELS_3_PLUS, OPT_fipa_cp_clone, NULL, 1 },
I have also checked, does gcc check -On
in other files (cscope symbol search for x_optimize
).
The only additional usage of n
from option -On
is saving it value into macro __OPTIMIZE__
. So some headers can behave differently for value of this macro equal 2 or 3.
UPDATE: There are questions about it in GCC WIKI:
- "Is -O1 (-O2,-O3 or -Os) equivalent to individual -foptimization options?"
- "What specific flags are enabled by -O1 (-O2, -O3 or -Os)?"
touch empty.c
gcc -O1 -S -fverbose-asm empty.c
cat empty.s
这篇关于-O3和(-O2 +标志man-gcc表示-O3增加了-O2)之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!