Optimization allowed on volatile objects

元气小坏坏 提交于 2019-12-23 13:57:49

问题


From ISO/IEC 9899:201x section 5.1.2.3 Program execution paragraph 4:

In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object).

What exactly is the allowed optimization here regarding the volatile object? can someone give an example of a volatile access that CAN be optimized away?

Since volatiles access are an observable behaviour (described in paragraph 6) it seems that no optimization can take please regarding volatiles, so, I'm curious to know what optimization is allowed in section 4.


回答1:


can someone give an example of a volatile access that CAN be optimized away?

I think that you misinterpreted the text, IMO this paragraph means that

volatile unsigned int bla = whatever();

if (bla < 0) // the code is not evaluated even if a volatile is involved



回答2:


Reformatting a little:

An actual implementation need not evaluate part of an expression if:

  a) it can deduce that its value is not used; and

  b) it can deduce that that no needed side effects are produced (including any
     caused by calling a function or accessing a volatile object).

Reversing the logic without changing the meaning:

An actual implementation must evaluate part of an expression if:

  a) it can't deduce that its value is not used; or

  b) it can't deduce that that no needed side effects are produced (including
      any caused by calling a function or accessing a volatile object).

Simplifying to focus on the volatile part:

An actual implementation must evaluate part of an expression if needed
side effects are produced (including accessing a volatile object).



回答3:


Adding another example that fits into this in my understanding:

volatile int vol_a;
....
int b = vol_a * 0; // vol_a is not evaluated



回答4:


Accesses to volatile objects must be evaluated. The phrase “including any…” modifies “side effects.” It does not modify “if it can deduce…” It has the same meaning as:

An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects (including any caused by calling a function or accessing a volatile object) are produced.

This means “side effects” includes side effects that are caused by accessing a volatile object. In order to decide it cannot evaluate part of an expression, an implementation must deduce that no needed side effects, including any caused by calling a function or accessing a volatile object, are produced.

It does not mean that an implementation can discard evaluation of part of an expression even if that expression includes accesses to a volatile object.




回答5:


In cases where an access to a volatile object would affect system behavior in a way that would be necessary to make a program achieve its purpose, such an access must not be omitted. If the access would have no effect whatsoever on system behavior, then the operation could be "performed" on the abstract machine without having to execute any instructions. It would be rare, however, for a compiler writer to know with certainty that the effect of executing instructions to perform the accesses would be the same as the effect of pretending to do those instructions on the abstract machine while skipping them on the real one.

In the much more common scenario where a compiler writer would have no particular knowledge of any effect that a volatile access might have, but also have no particular reason to believe that such accesses couldn't have effects the compiler writer doesn't know about (e.g. because of hardware which is triggered by operations involving certain addresses), a compiler writer would have to allow for the possibility that such accesses might have "interesting" effects by performing them in the specified sequence, without regard for whether the compiler writer knows of any particular reason that the sequence of operations should matter.



来源:https://stackoverflow.com/questions/55534512/optimization-allowed-on-volatile-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!