Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot (jp)

Wiki Markup
As described in-depth in [DCL34-C. Use volatile for data that cannot be cached|DCL34-C. Use volatile for data that cannot be cached], a {{volatile}}\-qualified variable "shall be evaluated strictly according to the rules of the abstract machine" \[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\].  In other words, the {{volatile}} qualifier is used to instruct the compiler to not make caching optimizations about a variable.

Wiki Markup
However, as demonstrated in \[[Eide and Regehr|AA. C References#Eide and Regehr]\], all tested compilers generated some percentage of incorrect compiled code with regards to {{volatile}} accesses.  Therefore, it is necessary to know how your compiler behaves when the standard {{volatile}} behavior is required.  There is also a workaround that eliminates some or all of these errors \[[Eide and Regehr|AA. C References#Eide and Regehr]\].

...

Wiki Markup
As demonstrated in \[[Eide and Regehr|AA. C References#Eide and Regehr]\], the following code example compiles incorrectly using GCC version 4.3.0 for IA32 and the {{\-Os}} optimization flag:

...

Wiki Markup
Because the variable {{x}} is {{volatile}}\-qualified, it should be accessed ten times in this program.  However, as shown in the compiled object code, it is only accessed once due to a loop-hoisting optimization \[[Eide and Regehr|AA. C References#Eide and Regehr]\]:

...

Wiki Markup
Eide and Regehr tested a workaround by wrapping {{volatile}} accesses with function calls.  They describe it with the intuition that "we can replace an action that compilers empirically get wrong by a different action --- a function call --- that compilers can get right" \[[Eide and Regehr|AA. C References#Eide and Regehr]\].  For example, the workaround for the noncompliant code example would be:

...

Wiki Markup
The workarounds proposed in \[[Eide and Regehr|AA. C References#Eide and Regehr]\] fix many of the {{volatile}}\-access bugs in the tested compilers.  However, compilers are always changing so critical sections of code should be compiled as if for deployment and the compiled object code should be inspected for the correct behavior.

...

Wiki Markup
\[[Eide and Regehr|AA. C References#Eide and Regehr]\] "Volatiles are miscompiled, and what to do about it"
\[[ISO/IEC 9899:1999|AA. C References#ISO/IEC 9899-1999]\] Section 6.7.3, "Type qualifiers"

...