不同的优化级别会导致功能上不同的代码?(Can different optimization levels lead to functionally different code?)

编程入门 行业动态 更新时间:2024-10-19 02:14:34
不同的优化级别会导致功能上不同的代码?(Can different optimization levels lead to functionally different code?)

我好奇编译器优化时的自由。 我们把这个问题限制在GCC和C / C ++(任何版本,任何标准的风格):

根据编译的优化级别编写代码的行为是否有所不同?

我想到的例子是在C ++中的各种构造函数中打印不同的文本位,取决于复制是否被删除(尽管我无法使这样的事情发挥作用)。

不允许计数时钟周期。 如果你有一个非GCC编译器的例子,我也很好奇,但我无法检查。 奖金积分为例:C.

编辑:示例代码应该是标准兼容的,从一开始就不包含未定义的行为。

编辑2:已经有一些很棒的答案! 让我的利益有一点:代码必须构成一个格式良好的程序,并符合标准,并且必须在每个优化级别编译正确的确定性程序。 (这不包括诸如不正确的多线程代码中的竞争条件等)。另外我明白,浮点舍入可能会受到影响,但让我们打折扣。

我刚刚打800声誉,所以我认为在第一个完整的例子中,我将吹捧50个声誉作为赏金,以符合这些条件的精神; 25如果涉及滥用严格的混叠。 (根据有人向我显示如何向其他人发送赏金)

I am curious about the liberties that a compiler has when optimizing. Let's limit this question to GCC and C/C++ (any version, any flavour of standard):

Is it possible to write code which behaves differently depending on which optimization level it was compiled with?

The example I have in mind is printing different bits of text in various constructors in C++ and getting a difference depending on whether copies are elided (though I've not been able to make such a thing work).

Counting clock cycles is not permitted. If you have an example for a non-GCC compiler, I'd be curious, too, but I can't check it. Bonus points for an example in C. :-)

Edit: The example code should be standard compliant and not contain undefined behaviour from the outset.

Edit 2: Got some great answers already! Let me up the stakes a bit: The code must constitute a well-formed program and be standards-compliant, and it must compile to correct, deterministic programs in every optimization level. (That excludes things like race-conditions in ill-formed multithreaded code.) Also I appreciate that floating point rounding may be affected, but let's discount that.

I just hit 800 reputation, so I think I shall blow 50 reputation as bounty on the first complete example to conform to (the spirit) of those conditions; 25 if it involves abusing strict aliasing. (Subject to someone showing me how to send bounty to someone else.)

最满意答案

适用的C ++标准的部分是§1.9“程序执行”。 它部分地读:

需要一致的实现来模拟(仅)如下所述的抽象机器的可观察行为。 ...

执行格式良好的程序的一致性实现将产生与抽象机器的相应实例的可能执行序列之一相同的可观察行为,具有相同的程序和相同的输入。 ...

抽象机器的可观察行为是其对易失性数据的读取和写入顺序以及对库I / O功能的调用。 ...

所以,是的,代码在不同的优化级别上可能会有所不同,但是(假设所有级别都产生一致的编译器),但是它们的行为可能不同

编辑:请允许我更正我的结论:是的,只要每个行为与标准抽象机器的行为可观察地相同,代码在不同的优化级别可能会有所不同。

The portion of the C++ standard that applies is §1.9 "Program execution". It reads, in part:

conforming implementations are required to emulate (only) the observable behavior of the abstract machine as explained below. ...

A conforming implementation executing a well-formed program shall produce the same observable behavior as one of the possible execution sequences of the corresponding instance of the abstract machine with the same program and the same input. ...

The observable behavior of the abstract machine is its sequence of reads and writes to volatile data and calls to library I/O functions. ...

So, yes, code may behave differently at different optimization levels, but (assuming that all levels produce a conforming compiler), but they cannot behave observably differently.

EDIT: Allow me to correct my conclusion: Yes, code may behave differently at different optimization levels as long as each behavior is observably identical to one of the behaviors of the standard's abstract machine.

更多推荐

本文发布于:2023-07-23 18:36:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1235641.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:级别   代码   功能   optimization   code

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!