<s>
Memory	B-General_Concept
ordering	I-General_Concept
describes	O
the	O
order	O
of	O
accesses	O
to	O
computer	O
memory	O
by	O
a	O
CPU	O
.	O
</s>
<s>
The	O
term	O
can	O
refer	O
either	O
to	O
the	O
memory	B-General_Concept
ordering	I-General_Concept
generated	O
by	O
the	O
compiler	B-Language
during	O
compile	B-Application
time	I-Application
,	O
or	O
to	O
the	O
memory	B-General_Concept
ordering	I-General_Concept
generated	O
by	O
a	O
CPU	O
during	O
runtime	B-Library
.	O
</s>
<s>
In	O
modern	O
microprocessors	B-Architecture
,	O
memory	B-General_Concept
ordering	I-General_Concept
characterizes	O
the	O
CPU	O
's	O
ability	O
to	O
reorder	O
memory	O
operations	O
–	O
it	O
is	O
a	O
type	O
of	O
out-of-order	B-General_Concept
execution	I-General_Concept
.	O
</s>
<s>
Memory	O
reordering	O
can	O
be	O
used	O
to	O
fully	O
utilize	O
the	O
bus-bandwidth	O
of	O
different	O
types	O
of	O
memory	O
such	O
as	O
caches	O
and	O
memory	B-General_Concept
banks	I-General_Concept
.	O
</s>
<s>
On	O
most	O
modern	O
uniprocessors	B-Operating_System
memory	O
operations	O
are	O
not	O
executed	O
in	O
the	O
order	O
specified	O
by	O
the	O
program	O
code	O
.	O
</s>
<s>
In	O
single	O
threaded	O
programs	O
all	O
operations	O
appear	O
to	O
have	O
been	O
executed	O
in	O
the	O
order	O
specified	O
,	O
with	O
all	O
out-of-order	B-General_Concept
execution	I-General_Concept
hidden	O
to	O
the	O
programmer	O
–	O
however	O
in	O
multi-threaded	O
environments	O
(	O
or	O
when	O
interfacing	O
with	O
other	O
hardware	O
via	O
memory	O
buses	O
)	O
this	O
can	O
lead	O
to	O
problems	O
.	O
</s>
<s>
To	O
avoid	O
problems	O
,	O
memory	B-General_Concept
barriers	I-General_Concept
can	O
be	O
used	O
in	O
these	O
cases	O
.	O
</s>
<s>
Traditional	O
compilers	B-Language
translate	O
high-level	O
expressions	O
to	O
a	O
sequence	O
of	O
low-level	O
instructions	O
relative	O
to	O
a	O
program	B-General_Concept
counter	I-General_Concept
at	O
the	O
underlying	O
machine	O
level	O
.	O
</s>
<s>
Execution	O
effects	O
are	O
visible	O
at	O
two	O
levels	O
:	O
within	O
the	O
program	O
code	O
at	O
a	O
high	O
level	O
,	O
and	O
at	O
the	O
machine	O
level	O
as	O
viewed	O
by	O
other	O
threads	O
or	O
processing	O
elements	O
in	O
concurrent	B-Architecture
programming	I-Architecture
,	O
or	O
during	O
debugging	O
when	O
using	O
a	O
hardware	O
debugging	O
aid	O
with	O
access	O
to	O
the	O
machine	O
state	O
(	O
some	O
support	O
for	O
this	O
is	O
often	O
built	O
directly	O
into	O
the	O
CPU	O
or	O
microcontroller	O
as	O
functionally	O
independent	O
circuitry	O
apart	O
from	O
the	O
execution	O
core	O
which	O
continues	O
to	O
operate	O
even	O
when	O
the	O
core	O
itself	O
is	O
halted	O
for	O
static	O
inspection	O
of	O
its	O
execution	O
state	O
)	O
.	O
</s>
<s>
Compile-time	B-Application
memory	O
order	O
concerns	O
itself	O
with	O
the	O
former	O
,	O
and	O
does	O
not	O
concern	O
itself	O
with	O
these	O
other	O
views	O
.	O
</s>
<s>
During	O
compilation	B-Language
,	O
hardware	O
instructions	O
are	O
often	O
generated	O
at	O
a	O
finer	O
granularity	O
than	O
specified	O
in	O
the	O
high-level	O
code	O
.	O
</s>
<s>
The	O
primary	O
observable	O
effect	O
in	O
a	O
procedural	B-Application
programming	I-Application
is	O
assignment	O
of	O
a	O
new	O
value	O
to	O
a	O
named	O
variable	O
.	O
</s>
<s>
At	O
the	O
machine	O
level	O
,	O
few	O
machines	O
can	O
add	O
three	O
numbers	O
together	O
in	O
a	O
single	O
instruction	O
,	O
and	O
so	O
the	O
compiler	B-Language
will	O
have	O
to	O
translate	O
this	O
expression	O
into	O
two	O
addition	O
operations	O
.	O
</s>
<s>
If	O
the	O
semantics	O
of	O
the	O
program	O
language	O
restrict	O
the	O
compiler	B-Language
into	O
translating	O
the	O
expression	O
in	O
left-to-right	O
order	O
(	O
for	O
example	O
)	O
,	O
then	O
the	O
generated	O
code	O
will	O
look	O
as	O
if	O
the	O
programmer	O
had	O
written	O
the	O
following	O
statements	O
in	O
the	O
original	O
program	O
:	O
</s>
<s>
If	O
the	O
compiler	B-Language
is	O
permitted	O
to	O
exploit	O
the	O
associative	O
property	O
of	O
addition	O
,	O
it	O
might	O
instead	O
generate	O
:	O
</s>
<s>
If	O
the	O
compiler	B-Language
is	O
also	O
permitted	O
to	O
exploit	O
the	O
commutative	O
property	O
of	O
addition	O
,	O
it	O
might	O
instead	O
generate	O
:	O
</s>
<s>
Note	O
that	O
the	O
integer	O
data	O
type	O
in	O
most	O
programming	O
languages	O
only	O
follows	O
the	O
algebra	O
for	O
the	O
mathematics	O
integers	O
in	O
the	O
absence	O
of	O
integer	B-Error_Name
overflow	I-Error_Name
and	O
that	O
floating-point	B-Algorithm
arithmetic	I-Algorithm
on	O
the	O
floating	B-Algorithm
point	I-Algorithm
data	O
type	O
available	O
in	O
most	O
programming	O
languages	O
is	O
not	O
commutative	O
in	O
rounding	O
effects	O
,	O
making	O
effects	O
of	O
the	O
order	O
of	O
expression	O
visible	O
in	O
small	O
differences	O
of	O
the	O
computed	O
result	O
(	O
small	O
initial	O
differences	O
may	O
however	O
cascade	O
into	O
arbitrarily	O
large	O
differences	O
over	O
a	O
longer	O
computation	O
)	O
.	O
</s>
<s>
If	O
the	O
programmer	O
is	O
concerned	O
about	O
integer	B-Error_Name
overflow	I-Error_Name
or	O
rounding	O
effects	O
in	O
floating	B-Algorithm
point	I-Algorithm
,	O
the	O
same	O
program	O
may	O
be	O
coded	O
at	O
the	O
original	O
high	O
level	O
as	O
follows	O
:	O
</s>
<s>
Many	O
languages	O
treat	O
the	O
statement	O
boundary	O
as	O
a	O
sequence	B-Application
point	I-Application
,	O
forcing	O
all	O
effects	O
of	O
one	O
statement	O
to	O
be	O
complete	O
before	O
the	O
next	O
statement	O
is	O
executed	O
.	O
</s>
<s>
This	O
will	O
force	O
the	O
compiler	B-Language
to	O
generate	O
code	O
corresponding	O
to	O
the	O
statement	O
order	O
expressed	O
.	O
</s>
<s>
In	O
most	O
compiled	B-Language
languages	O
,	O
the	O
compiler	B-Language
is	O
free	O
to	O
order	O
the	O
function	O
calls	O
f	O
,	O
g	O
,	O
and	O
h	O
as	O
it	O
finds	O
convenient	O
,	O
resulting	O
in	O
large-scale	O
changes	O
of	O
program	O
memory	O
order	O
.	O
</s>
<s>
In	O
a	O
pure	O
functional	O
programming	O
language	O
,	O
function	O
calls	O
are	O
forbidden	O
from	O
having	O
side	O
effects	O
on	O
the	O
visible	O
program	O
state	O
(	O
other	O
than	O
its	O
return	B-Language
value	I-Language
)	O
and	O
the	O
difference	O
in	O
machine	O
memory	O
order	O
due	O
to	O
function	O
call	O
ordering	O
will	O
be	O
inconsequential	O
to	O
program	O
semantics	O
.	O
</s>
<s>
In	O
procedural	B-Application
languages	I-Application
,	O
the	O
functions	O
called	O
might	O
have	O
side-effects	O
,	O
such	O
as	O
performing	O
an	O
I/O	B-General_Concept
operation	I-General_Concept
,	O
or	O
updating	O
a	O
variable	O
in	O
global	O
program	O
scope	O
,	O
both	O
of	O
which	O
produce	O
visible	O
effects	O
with	O
the	O
program	O
model	O
.	O
</s>
<s>
In	O
programming	O
languages	O
where	O
the	O
statement	O
boundary	O
is	O
defined	O
as	O
a	O
sequence	B-Application
point	I-Application
,	O
the	O
function	O
calls	O
f	O
,	O
g	O
,	O
and	O
h	O
must	O
now	O
execute	O
in	O
that	O
precise	O
order	O
.	O
</s>
<s>
The	O
effects	O
of	O
reading	O
from	O
a	O
pointer	O
are	O
determined	O
by	O
architecture	O
's	O
memory	B-General_Concept
model	I-General_Concept
.	O
</s>
<s>
In	O
embedded	B-Architecture
system	I-Architecture
programming	O
,	O
it	O
is	O
very	O
common	O
to	O
have	O
memory-mapped	B-Architecture
I/O	I-Architecture
where	O
reads	O
and	O
writes	O
to	O
memory	O
trigger	O
I/O	B-General_Concept
operations	I-General_Concept
,	O
or	O
changes	O
to	O
the	O
processor	O
's	O
operational	O
mode	O
,	O
which	O
are	O
highly	O
visible	O
side	O
effects	O
.	O
</s>
<s>
The	O
compiler	B-Language
is	O
free	O
to	O
reorder	O
these	O
reads	O
in	O
program	O
order	O
as	O
it	O
sees	O
fit	O
,	O
and	O
there	O
will	O
be	O
no	O
program-visible	O
side	O
effects	O
.	O
</s>
<s>
Here	O
the	O
language	O
definition	O
is	O
unlikely	O
to	O
allow	O
the	O
compiler	B-Language
to	O
break	O
this	O
apart	O
as	O
follows	O
:	O
</s>
<s>
Since	O
the	O
compiler	B-Language
is	O
not	O
allowed	O
this	O
particular	O
splitting	O
transformation	O
,	O
the	O
only	O
write	O
to	O
the	O
memory	O
location	O
of	O
sum	O
must	O
logically	O
follow	O
the	O
three	O
pointer	O
reads	O
in	O
the	O
value	O
expression	O
.	O
</s>
<s>
Suppose	O
,	O
however	O
,	O
that	O
the	O
programmer	O
is	O
concerned	O
about	O
the	O
visible	O
semantics	O
of	O
integer	B-Error_Name
overflow	I-Error_Name
and	O
breaks	O
the	O
statement	O
apart	O
as	O
the	O
program	O
level	O
as	O
follows	O
:	O
</s>
<s>
This	O
guarantees	O
the	O
order	O
of	O
the	O
two	O
addition	O
operations	O
,	O
but	O
potentially	O
introduces	O
a	O
new	O
problem	O
of	O
address	O
aliasing	B-Application
:	O
any	O
of	O
these	O
pointers	O
could	O
potentially	O
refer	O
to	O
the	O
same	O
memory	O
location	O
.	O
</s>
<s>
Because	O
of	O
possible	O
aliasing	B-Application
effects	O
,	O
pointer	O
expressions	O
are	O
difficult	O
to	O
rearrange	O
without	O
risking	O
visible	O
program	O
effects	O
.	O
</s>
<s>
In	O
the	O
common	O
case	O
,	O
there	O
might	O
not	O
be	O
any	O
aliasing	B-Application
in	O
effect	O
,	O
so	O
the	O
code	O
appears	O
to	O
run	O
normally	O
as	O
before	O
.	O
</s>
<s>
But	O
in	O
the	O
edge	O
case	O
where	O
aliasing	B-Application
is	O
present	O
,	O
severe	O
program	O
errors	O
can	O
result	O
.	O
</s>
<s>
Even	O
if	O
these	O
edge	O
cases	O
are	O
entirely	O
absent	O
in	O
normal	O
execution	O
,	O
it	O
opens	O
the	O
door	O
for	O
a	O
malicious	O
adversary	O
to	O
contrive	O
an	O
input	O
where	O
aliasing	B-Application
exists	O
,	O
potentially	O
leading	O
to	O
a	O
computer	O
security	O
exploit	O
.	O
</s>
<s>
The	O
compiler	B-Language
may	O
choose	O
to	O
evaluate	O
*	O
a	O
and	O
*	O
b	O
before	O
either	O
function	O
call	O
,	O
it	O
may	O
defer	O
the	O
evaluation	O
of	O
*	O
b	O
until	O
after	O
the	O
function	O
call	O
f	O
or	O
it	O
may	O
defer	O
the	O
evaluation	O
of	O
*	O
a	O
until	O
after	O
the	O
function	O
call	O
g	O
.	O
If	O
the	O
function	O
f	O
and	O
g	O
are	O
free	O
from	O
program	O
visible	O
side-effects	O
,	O
all	O
three	O
choices	O
will	O
produce	O
a	O
program	O
with	O
the	O
same	O
visible	O
program	O
effects	O
.	O
</s>
<s>
If	O
the	O
implementation	O
of	O
f	O
or	O
g	O
contain	O
the	O
side-effect	O
of	O
any	O
pointer	O
write	O
subject	O
to	O
aliasing	B-Application
with	O
pointers	O
a	O
or	O
b	O
,	O
the	O
three	O
choices	O
are	O
liable	O
to	O
produce	O
different	O
visible	O
program	O
effects	O
.	O
</s>
<s>
In	O
general	O
,	O
compiled	B-Language
languages	O
are	O
not	O
detailed	O
enough	O
in	O
their	O
specification	O
for	O
the	O
compiler	B-Language
to	O
determine	O
formally	O
at	O
compile	B-Application
time	I-Application
which	O
pointers	O
are	O
potentially	O
aliased	O
and	O
which	O
are	O
not	O
.	O
</s>
<s>
The	O
safest	O
course	O
of	O
action	O
is	O
for	O
the	O
compiler	B-Language
to	O
assume	O
that	O
all	O
pointers	O
are	O
potentially	O
aliased	O
at	O
all	O
times	O
.	O
</s>
<s>
This	O
level	O
of	O
conservative	O
pessimism	O
tends	O
to	O
produce	O
dreadful	O
performance	O
as	O
compared	O
to	O
the	O
optimistic	O
assumption	O
that	O
no	O
aliasing	B-Application
exists	O
,	O
ever	O
.	O
</s>
<s>
As	O
a	O
result	O
,	O
many	O
high-level	O
compiled	B-Language
languages	O
,	O
such	O
as	O
C/C	O
++	O
,	O
have	O
evolved	O
to	O
have	O
intricate	O
and	O
sophisticated	O
semantic	O
specifications	O
about	O
where	O
the	O
compiler	B-Language
is	O
permitted	O
to	O
make	O
optimistic	O
assumptions	O
in	O
code	O
reordering	O
in	O
pursuit	O
of	O
the	O
highest	O
possible	O
performance	O
,	O
and	O
where	O
the	O
compiler	B-Language
is	O
required	O
to	O
make	O
pessimistic	O
assumptions	O
in	O
code	O
reordering	O
to	O
avoid	O
semantic	O
hazards	O
.	O
</s>
<s>
By	O
far	O
the	O
largest	O
class	O
of	O
side	O
effects	O
in	O
a	O
modern	O
procedural	B-Application
language	I-Application
involve	O
memory	O
write	O
operations	O
,	O
so	O
rules	O
around	O
memory	B-General_Concept
ordering	I-General_Concept
are	O
a	O
dominant	O
component	O
in	O
the	O
definition	O
of	O
program	O
order	O
semantics	O
.	O
</s>
<s>
Modern	O
compilers	B-Language
sometimes	O
take	O
this	O
a	O
step	O
further	O
by	O
means	O
of	O
an	O
as-if	B-Language
rule	I-Language
,	O
in	O
which	O
any	O
reordering	O
is	O
permitted	O
(	O
even	O
across	O
statements	O
)	O
if	O
no	O
effect	O
on	O
the	O
visible	O
program	O
semantics	O
results	O
.	O
</s>
<s>
If	O
the	O
compiler	B-Language
is	O
permitted	O
to	O
make	O
optimistic	O
assumptions	O
about	O
distinct	O
pointer	O
expressions	O
having	O
no	O
alias	O
overlap	O
in	O
a	O
case	O
where	O
such	O
aliasing	B-Application
actually	O
exists	O
(	O
this	O
would	O
normally	O
be	O
classified	O
as	O
an	O
ill-formed	O
program	O
exhibiting	O
undefined	B-Language
behavior	I-Language
)	O
,	O
the	O
adverse	O
results	O
of	O
an	O
aggressive	O
code-optimization	O
transformation	O
are	O
impossible	O
to	O
guess	O
prior	O
to	O
code	O
execution	O
or	O
direct	O
code	O
inspection	O
.	O
</s>
<s>
The	O
realm	O
of	O
undefined	B-Language
behavior	I-Language
has	O
nearly	O
limitless	O
manifestations	O
.	O
</s>
<s>
It	O
is	O
the	O
responsibility	O
of	O
the	O
programmer	O
to	O
consult	O
the	O
language	O
specification	O
to	O
avoid	O
writing	O
ill-formed	O
programs	O
where	O
the	O
semantics	O
are	O
potentially	O
changed	O
as	O
a	O
result	O
of	O
any	O
legal	O
compiler	B-Language
optimization	O
.	O
</s>
<s>
Fortran	B-Application
traditionally	O
places	O
a	O
high	O
burden	O
on	O
the	O
programmer	O
to	O
be	O
aware	O
of	O
these	O
issues	O
,	O
with	O
the	O
systems	B-Application
programming	I-Application
languages	O
C	O
and	O
C++	O
not	O
far	O
behind	O
.	O
</s>
<s>
A	O
complete	O
grasp	O
of	O
memory	O
order	O
semantics	O
is	O
considered	O
to	O
be	O
an	O
arcane	O
specialization	O
even	O
among	O
the	O
subpopulation	O
of	O
professional	O
systems	B-Application
programmers	I-Application
who	O
are	O
typically	O
best	O
informed	O
in	O
this	O
subject	O
area	O
.	O
</s>
<s>
At	O
the	O
extreme	O
end	O
of	O
specialization	O
in	O
memory	O
order	O
semantics	O
are	O
the	O
programmers	O
who	O
author	O
software	B-Architecture
frameworks	I-Architecture
in	O
support	O
of	O
concurrent	B-Architecture
computing	I-Architecture
models	O
.	O
</s>
<s>
Note	O
that	O
local	O
variables	O
can	O
not	O
be	O
assumed	O
to	O
be	O
free	O
of	O
aliasing	B-Application
if	O
a	O
pointer	O
to	O
such	O
a	O
variable	O
escapes	O
into	O
the	O
wild	O
:	O
</s>
<s>
These	O
barriers	O
prevent	O
a	O
compiler	B-Language
from	O
reordering	O
instructions	O
during	O
compile	B-Application
time	I-Application
–	O
they	O
do	O
not	O
prevent	O
reordering	O
by	O
CPU	O
during	O
runtime	B-Library
.	O
</s>
<s>
Any	O
of	O
these	O
GNU	O
inline	O
assembler	O
statements	O
forbids	O
the	O
GCC	B-Application
compiler	I-Application
to	O
reorder	O
read	O
and	O
write	O
commands	O
around	O
it	O
:	O
</s>
<s>
This	O
C11/C	O
++11	O
function	O
forbids	O
the	O
compiler	B-Language
to	O
reorder	O
read	O
and	O
write	O
commands	O
around	O
it	O
:	O
</s>
<s>
Intel	B-Language
ICC	I-Language
compiler	I-Language
uses	O
"	O
full	O
compiler	B-Language
fence	O
"	O
intrinsics	O
:	O
</s>
<s>
Microsoft	B-Application
Visual	I-Application
C++	I-Application
Compiler	B-Language
:	O
</s>
<s>
In	O
many	O
programming	O
languages	O
different	O
types	O
of	O
barriers	O
can	O
be	O
combined	O
with	O
other	O
operations	O
(	O
like	O
load	O
,	O
store	O
,	O
atomic	O
increment	O
,	O
atomic	O
compare	O
and	O
swap	O
)	O
,	O
so	O
no	O
extra	O
memory	B-General_Concept
barrier	I-General_Concept
is	O
needed	O
before	O
or	O
after	O
it	O
(	O
or	O
both	O
)	O
.	O
</s>
<s>
barrier	O
and	O
load	O
)	O
,	O
or	O
to	O
normal	O
instruction	O
,	O
depending	O
on	O
hardware	O
memory	B-General_Concept
ordering	I-General_Concept
guarantees	O
.	O
</s>
<s>
There	O
are	O
several	O
memory-consistency	O
models	O
for	O
SMP	B-Operating_System
systems	O
:	O
</s>
<s>
Atomic	B-General_Concept
operations	I-General_Concept
can	O
be	O
reordered	O
with	O
loads	O
and	O
stores	O
.	O
</s>
<s>
There	O
can	O
be	O
incoherent	O
instruction	O
cache	O
pipeline	O
,	O
which	O
prevents	O
self-modifying	B-Application
code	I-Application
from	O
being	O
executed	O
without	O
special	O
instruction	O
cache	O
flush/reload	O
instructions	O
.	O
</s>
<s>
Allowing	O
this	O
relaxation	O
makes	O
cache	O
hardware	O
simpler	O
and	O
faster	O
but	O
leads	O
to	O
the	O
requirement	O
of	O
memory	B-General_Concept
barriers	I-General_Concept
for	O
readers	O
and	O
writers	O
.	O
</s>
<s>
On	O
Alpha	O
hardware	O
(	O
like	O
multiprocessor	O
Alpha	B-General_Concept
21264	I-General_Concept
systems	O
)	O
cache	O
line	O
invalidations	O
sent	O
to	O
other	O
processors	O
are	O
processed	O
in	O
lazy	O
fashion	O
by	O
default	O
,	O
unless	O
requested	O
explicitly	O
to	O
be	O
processed	O
between	O
dependent	O
loads	O
.	O
</s>
<s>
Many	O
architectures	O
with	O
SMP	B-Operating_System
support	O
have	O
special	O
hardware	O
instruction	O
for	O
flushing	O
reads	O
and	O
writes	O
during	O
runtime	B-Library
.	O
</s>
<s>
Some	O
compilers	B-Language
support	O
builtins	B-Application
that	O
emit	O
hardware	O
memory	B-General_Concept
barrier	I-General_Concept
instructions	O
:	O
</s>
<s>
GCC	B-Application
,	O
version	O
4.4.0	O
and	O
later	O
,	O
has	O
__sync_synchronize	O
.	O
</s>
<s>
The	O
Microsoft	B-Application
Visual	I-Application
C++	I-Application
compiler	B-Language
has	O
MemoryBarrier( )	O
.	O
</s>
<s>
Sun	B-Application
Studio	I-Application
Compiler	I-Application
Suite	I-Application
has	O
__machine_r_barrier	O
,	O
__machine_w_barrier	O
and	O
__machine_rw_barrier	O
.	O
</s>
