https://gcc.gnu.org/bugzilla/show_bug.cgi?id=116383

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It's valid iff no other thread does any writes that are potentially concurrent,
e.g. if the other threads are only doing atomic_load on that variable.

And that's precisely why the optimization would be valid. If any other thread
is writing to the variable (atomically or not) the non-atomic load of *i would
be a data race, so undefined. So either no other thread is changing the value,
in which case we don't need to reload it, or the code has undefined behaviour,
in which case we don't need to reload it (because returning *any* value would
be valid, and returning the one we just wrote is better than returning garbage,
since that would be a valid result to get even if an atomic load had been used
instead).

Reply via email to