------- Comment #13 from amylaar at gcc dot gnu dot org 2007-10-22 23:17 ------- (In reply to comment #0) > See http://openmp.org/pipermail/omp/2007/000840.html > and the rest of the lengthy threads: > Memory consistency contradiction between 2.5 specification and GCC > OpenMP spec 2.5 seems to have incorrect flush example on page 12 > Two simpler examples (Re: OpenMP spec 2.5 seems to have incorrect flush > example > on page 12) > > Some GCC optimizations are harmful for threaded code, e.g. loop invariant > motion > of global variables: > > int var; > void > foo (int x) > { > int i; > for (i = 0; i < 100; i++) > { > if (i > x) > var = i; > } > } > > When some other thread modifies var at the same time while foo (200) is > executed, > the compiler inserted a race which doesn't really exist in the original > program, > as it will do reg = var; ... var = reg; even when var was never modified. >
This is not even a particular good choice of transformation for performance reasons. We incur memory read latency when there is no need to. Better is: auto int dummy, *addr; addr = &dummy; if (i > x) addr = &var; *addr = i; Or for the entire function: void foo (int x) { int dummy; int *addr = &dummy; if (99 > x) addr = &var; *addr = 99; } -- amylaar at gcc dot gnu dot org changed: What |Removed |Added ---------------------------------------------------------------------------- Keywords| |missed-optimization http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31862