搜索
bottom↓
回复: 15

使用编译器优化时,编程需要注意什么地方?

[复制链接]

出0入0汤圆

发表于 2015-12-25 21:37:34 | 显示全部楼层 |阅读模式

花了两个多小时,终于解决了一个bug.使用的是MDK。
程序始终卡在一个地方
while(flag == true);

用编译器变量查看窗口看flag的值已经变成true了,可程序始终走不下去。
最后对比两个工程的配置,发现出问题的工程编译器优化等级为最高级3.后来更改为等级0,问题得到解决。


百度了一下,编译器优化时,会造成标志变量优化到寄存器造成死循环问题,此时需要添加volatile关键字防止bug,之后更改添加了volatile,问题得到解决。

那么,除了上面这个问题,使用编译器优化时,编程需要注意什么地方?

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。

出0入0汤圆

发表于 2015-12-25 21:56:15 来自手机 | 显示全部楼层
之前发现有时候加了volatile也不行  

出0入0汤圆

发表于 2015-12-25 21:59:56 来自手机 | 显示全部楼层
while(flag !=6);        死等标志到达6   改成while(flag  <6);  才通过   这个标志就是从0递增到6的  断点看也是6

出0入0汤圆

发表于 2015-12-25 22:01:28 来自手机 | 显示全部楼层
降低优化级别可以判断不等于  开优化不行  已经加volatile

出0入0汤圆

发表于 2015-12-25 22:02:31 来自手机 | 显示全部楼层
改成比大小开优化通过了

出0入4汤圆

发表于 2015-12-25 23:13:56 | 显示全部楼层
为防止出现此类问题 一直都是0优化 - -

反正stm32肚子大 怎么都够用

出0入0汤圆

发表于 2015-12-26 08:00:43 | 显示全部楼层
huarana 发表于 2015-12-25 23:13
为防止出现此类问题 一直都是0优化 - -

反正stm32肚子大 怎么都够用

+10086
我也是这么做的,反正容量足够,没必要自己跟自己找麻烦。

出0入89汤圆

发表于 2015-12-26 08:54:06 来自手机 | 显示全部楼层
最好放长点代码让大家分析

出0入0汤圆

发表于 2015-12-27 09:49:23 来自手机 | 显示全部楼层
上次移植lwIP时,开优化也出现了问题!

出0入8汤圆

发表于 2015-12-27 10:34:45 | 显示全部楼层
The compiler supports the following optimization levels:
0
Minimum optimization. Turns off most optimizations. When debugging is enabled, this option gives the best possible debug view because the structure of the generated code directly corresponds to the source code. All optimization that interferes with the debug view is disabled. In particular:
Breakpoints can be set on any reachable point, including dead code.
The value of a variable is available everywhere within its scope, except where it is uninitialized.
Backtrace gives the stack of open function activations that is expected from reading the source.
Note
Although the debug view produced by -O0 corresponds most closely to the source code, users might prefer the debug view produced by -O1 because this improves the quality of the code without changing the fundamental structure.
Note
Dead code includes reachable code that has no effect on the result of the program, for example an assignment to a local variable that is never used. Unreachable code is specifically code that cannot be reached via any control flow path, for example code that immediately follows a return statement.
1
Restricted optimization. The compiler only performs optimizations that can be described by debug information. Removes unused inline functions and unused static functions. Turns off optimizations that seriously degrade the debug view. If used with --debug, this option gives a generally satisfactory debug view with good code density.
The differences in the debug view from –O0 are:
Breakpoints cannot be set on dead code.
Values of variables might not be available within their scope after they have been initialized. For example if their assigned location has been reused.
Functions with no side-effects might be called out of sequence, or might be omitted if the result is not needed.
Backtrace might not give the stack of open function activations that is expected from reading the source because of the presence of tailcalls.
The optimization level –O1 produces good correspondence between source code and object code, especially when the source code contains no dead code. The generated code can be significantly smaller than the code at –O0, which can simplify analysis of the object code.
2
High optimization. If used with --debug, the debug view might be less satisfactory because the mapping of object code to source code is not always clear. The compiler might perform optimizations that cannot be described by debug information.
This is the default optimization level.
The differences in the debug view from –O1 are:
The source code to object code mapping might be many to one, because of the possibility of multiple source code locations mapping to one point of the file, and more aggressive instruction scheduling.
Instruction scheduling is allowed to cross sequence points. This can lead to mismatches between the reported value of a variable at a particular point, and the value you might expect from reading the source code.
The compiler automatically inlines functions.
3
Maximum optimization. When debugging is enabled, this option typically gives a poor debug view. ARM recommends debugging at lower optimization levels.
If you use -O3 and -Otime together, the compiler performs extra optimizations that are more aggressive, such as:
High-level scalar optimizations, including loop unrolling. This can give significant performance benefits at a small code size cost, but at the risk of a longer build time.
More aggressive inlining and automatic inlining.
These optimizations effectively rewrite the input source code, resulting in object code with the lowest correspondence to source code and the worst debug view. The --loop_optimization_level=option controls the amount of loop optimization performed at –O3 –Otime. The higher the amount of loop optimization the worse the correspondence between source and object code.
For extra information about the high level transformations performed on the source code at –O3 –Otime use the --remarks command-line option.
Because optimization affects the mapping of object code to source code, the choice of optimization level with -Ospace and -Otime generally impacts the debug view.
The option -O0 is the best option to use if a simple debug view is required. Selecting -O0 typically increases the size of the ELF image by 7 to 15%. To reduce the size of your debug tables, use the --remove_unneeded_entities option.

出0入8汤圆

发表于 2015-12-27 10:57:57 | 显示全部楼层
C51  一直优化开到8级。

出870入263汤圆

发表于 2015-12-27 11:20:46 | 显示全部楼层
邓爷爷说:经不起优化的代码不是好代码!

出0入0汤圆

发表于 2020-4-10 09:44:02 | 显示全部楼层
用volatile 修饰一下 flag  就好了应该

出0入0汤圆

发表于 2020-4-13 20:43:27 | 显示全部楼层
用0级优化的, 真的不关心代码效率阿

出0入0汤圆

发表于 2020-4-13 21:39:05 来自手机 | 显示全部楼层
我们强制开最高等级优化,然后加强单元测试,凡是优化出问题的代码通通算BUG
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 22:28

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表