<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/100840>100840</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Unexpected code generation for volatile atomic RMW operations
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          keinflue
      </td>
    </tr>
</table>

<pre>
    The C++ test case

```
#include<atomic>

void f(volatile std::atomic<int>& x) {
    x.fetch_add(0, std::memory_order_relaxed);
}
```

on Clang 18.1.0 x86-64 with -O1 [produces](https://godbolt.org/z/z5oW5zo76)

```
f(std::atomic<int> volatile&):
        mfence
        mov     eax, dword ptr [rdi]
 ret
```

as does the equivalent C program.

This is surprising, because only a load instead of a RMW operation is emitted. `fetch_add` is always a RMW operation on the abstract machine, even if the stored value is the same as the one loaded, and the expectation would be that `volatile` preserves this semantic.

This may or may not be a conformance issue. The standards seem to be unclear about the interpretation of "access" with regards to `volatile`-qualified RMW operations.

See discussion at https://stackoverflow.com/questions/78776735/semantics-of-volatile-stdatomict and https://stackoverflow.com/questions/78777076/semantics-of-volatile-atomic.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVFFv4zYM_jXKCxHDkWM7fvBDmy5vw4Ctwz0WtETH2sliKslJer9-kJ1er711wAWJFEvkx48faWII5uiIWlHei_JhhVMc2LdfybjeTrTqWL-0jwPBXsh7Ie8hUoigMJDIH0R-d1ur_PZdHmVhnLKTJlHsMfJolCh--9HhzEZDL-TuzBajsQQhalHcieLu1X5vXExesoKrkA2I-n7xBQC4Zj1FNTyh1kLuciH3bwAjjexfnthr8k-eLF5JC9mI4uYv6of_Zj2v7GBv0R1hs8s2WQ7XXbWutnAxcYD1HxsQ5f3Js54UBVE-CLkbYjyFFFkehDwcWXdsY8b-KOThW_qV_KX8xnWVOHyuWRLjMw3g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