Search code examples
c++cvolatilecompiler-optimization

"volatile" qualifier and compiler reorderings


A compiler cannot eliminate or reorder reads/writes to a volatile-qualified variables.

But what about the cases where other variables are present, which may or may not be volatile-qualified?

Scenario 1

volatile int a;
volatile int b;

a = 1;
b = 2;
a = 3;
b = 4;

Can the compiler reorder first and the second, or third and the fourth assignments?

Scenario 2

volatile int a;
int b, c;

b = 1;
a = 1;
c = b;
a = 3;

Same question, can the compiler reorder first and the second, or third and the fourth assignments?


Solution

  • The C++ standard says (1.9/6):

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

    In scenario 1, either of the changes you propose changes the sequence of writes to volatile data.

    In scenario 2, neither change you propose changes the sequence. So they're allowed under the "as-if" rule (1.9/1):

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

    In order to tell that this has happened, you would need to examine the machine code, use a debugger, or provoke undefined or unspecified behavior whose result you happen to know on your implementation. For example, an implementation might make guarantees about the view that concurrently-executing threads have of the same memory, but that's outside the scope of the C++ standard. So while the standard might permit a particular code transformation, a particular implementation could rule it out, on grounds that it doesn't know whether or not your code is going to run in a multi-threaded program.

    If you were to use observable behavior to test whether the re-ordering has happened or not (for example, printing the values of variables in the above code), then of course it would not be allowed by the standard.