title: Why don't compilers merge redundant std::atomic writes?
body: <p>I'm wondering why no compilers are prepared to merge consecutive writes of the same value to a single atomic variable, e.g.:</p>
  
  <pre><code>#include &lt;atomic&gt;
  std::atomic&lt;int&gt; y(0);
  void f() {
    auto order = std::memory_order_relaxed;
    y.store(1, order);
    y.store(1, order);
    y.store(1, order);
  }
  </code></pre>
  
  <p>Every compiler I've tried will issue the above write three times. What legitimate, race-free observer could see a difference between the above code and an optimized version with a single write (i.e. doesn't the 'as-if' rule apply)?</p>
  
  <p>If the variable had been volatile, then obviously no optimization is applicable. What's preventing it in my case?</p>
  
  <p>Here's the code in <a href="https://godbolt.org/g/BLoH1x" rel="noreferrer" title="compiler explorer">compiler explorer</a>.</p>