g++ O1 is not equal to O0 with all related optimization flags(g++ O1 不等于带有所有相关优化标志的 O0)
问题描述
我知道标题有点混乱.让我用一点背景来澄清我的问题:
I know the title is a bit confusing. Let me clarify my problem with a little background:
就执行时间而言,当我使用 -O1
标志与 -O0
标志编译它时,我的程序表现得很奇怪.我知道 -O1
标志做了很多优化,比如 fauto-inc-dec -fbranch-count-reg -fcombine-stack-adjustments
(根据手册页超过 40).为了找出导致这种行为的优化,我计划一次删除一个标志,然后编译和测试以查看是否有变化.
My program behaves strangely when I compile it with -O1
flag vs -O0
flag in terms of execution time. I know -O1
flag does many optimizations such as fauto-inc-dec -fbranch-count-reg -fcombine-stack-adjustments
(More than 40 according to the man page). To figure out which optimization(s) cause this behavior, I plan to remove flags one at a time then compile and test to see if something changes.
在做这个实验之前,我想确保用-O1
编译的程序和用-O0
编译的程序加上-O1的所有标志
启用(让我们调用 -O0+
)的行为类似.实际上,我希望这两种方法都应该生成相同的二进制文件,因为启用了相同的优化标志.
Before doing this experiment, I want to make sure that the program compiled with -O1
and the program compiled with -O0
plus all flags which -O1
enables (Lets call -O0+
) behave similarly. Actually, I expect both method should produce the same binary file since the same optimization flags are enabled.
使用O1
CC = g++
CFLAGS = -std=c++11 -Wall -fopenmp
SOURCE = a_count_f.cpp
EXEC = run
INC = inc
all: $(EXEC)
.PHONY: all
$(EXEC): $(SOURCE)
$(CC) $(CFLAGS) -O1 -o $(EXEC) -I$(INC) $^
使用O0+
CC = g++
CFLAGS = -std=c++11 -Wall -fopenmp
SOURCE = a_count_f.cpp
EXEC = run
INC = inc
OPT_FLAGS = -fauto-inc-dec -fbranch-count-reg -fcombine-stack-adjustments -fcompare-elim -fcprop-registers -fdce -fdefer-pop -ftree-builtin-call-dce -fdse -fforward-propagate -fguess-branch-probability -fif-conversion2 -fif-conversion -finline-functions-called-once -fipa-pure-const -fipa-profile -fipa-reference -fmerge-constants -fmove-loop-invariants -fomit-frame-pointer -freorder-blocks -fshrink-wrap -fshrink-wrap-separate -fsplit-wide-types -fssa-backprop -fssa-phiopt -ftree-bit-ccp -ftree-ccp -ftree-ch -ftree-coalesce-vars -ftree-copy-prop -ftree-dce -ftree-dominator-opts -ftree-dse -ftree-forwprop -ftree-fre -ftree-phiprop -ftree-scev-cprop -ftree-sink -ftree-slsr -ftree-sra -ftree-pta -ftree-ter -funit-at-a-time
all: $(EXEC)
.PHONY: all
$(EXEC): $(SOURCE)
$(CC) $(CFLAGS) -O0 $(OPT_FLAGS) -o $(EXEC) -I$(INC) $^
然而,结果 -O1
和 -O0+
给出了完全不同的结果.尽管存在所有优化差异,-O0
和 -O0+
给出了非常相似的结果.(通过结果,我的意思是执行时间)
However, It turned out -O1
and -O0+
give quite different result. Despite of all optimizations differences, -O0
and -O0+
give very similar results. (By results, I mean execution time)
我已经使用 -Q --help=optimizers
检查了两个编译,输出确认两者都启用了相同的标志.
I have checked both compilation with -Q --help=optimizers
and the output confirmed that both enables the same flags.
接下来对我来说是比较汇编代码.在这样做之前,我想在这里问一下是否有人知道为什么会发生这种情况.我没有包含源代码,因为问题似乎与源代码无关.但是,如果需要,我可以附上它.
The next for me is to compare assembly codes. Before doing that, I want to ask it here if anyone has an idea why this happens. I didn't include the source code as it seems the problem is not related to source code. But, I can attach it if needed.
g++ 版本:g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0
推荐答案
-O1
应用的优化标志仅在优化器打开时适用.您需要使用 n > 指定
以便优化标志实际执行任何操作.-On
0
The optimizations flags applied by -O1
only apply when the optimizer is turned on. You need to specify -On
with n > 0
in order for the optimization flags to actually do anything.
换句话说,-O0
不会打开优化器,因此优化标志不会做任何事情.
To put it another way, -O0
doesn't turn on the optimizer, so the optimization flags don't do anything.
您可以使用标志的 -fno
形式关闭优化标志.例如
You can turn off optimzations flags by using the -fno
form of the flag. For instance the
-fcompare-elim
标志由 -O1
打开,您可以使用
flag is turned on by -O1
and you can turn it back off using
-fno-compare-elim
<小时>
另一件需要注意的事情,正如 TC 所指出的,是 并非所有优化都有标记没有任何方法可以关闭这些特定的优化.
Another thing to note, as pointed out by T.C., is that not all optimizations have a flag so there isn't any way to turn those particular optimizations off.
这篇关于g++ O1 不等于带有所有相关优化标志的 O0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:g++ O1 不等于带有所有相关优化标志的 O0
基础教程推荐
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 从 std::cin 读取密码 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01