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

    <tr>
        <th>Summary</th>
        <td>
            [libc++] `atomic_ref<float>::fetch_add` has CAS loop rather than `atomicrmw`
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            libc++
      </td>
    </tr>

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

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

<pre>
    When trying to use `std::atomic_ref<float>` I discovered that I was not getting the IR and ASM I was expecting. It turns out that `std::atomic_ref<float>` and `std::atomic<float>` have very different implementations of `fetch_add` and `fetch_sub`. 

Compare https://github.com/llvm/llvm-project/blob/56b792322aaaa82883d56a322a94448de519f789/libcxx/include/__atomic/atomic.h#L382-L389 versus https://github.com/llvm/llvm-project/blob/56b792322aaaa82883d56a322a94448de519f789/libcxx/include/__atomic/atomic_ref.h#L324-L331

`atomic_ref<float>` implements `fetch_add` using a compare-and-swap loop, whereas `atomic<float>` can emit an `atomicrmw fadd`. For architectures with atomic floating-point instructions, this propagates down into dramatically different assembler: https://godbolt.org/z/7KxG5eoe3

Is there a reason why `atomic_ref` has to handle this differently than `atomic`? The `atomic_ref<int>` implementation of `fetch_add` delegates to the builtin `__atomic_fetch_add`: https://github.com/llvm/llvm-project/blob/56b792322aaaa82883d56a322a94448de519f789/libcxx/include/__atomic/atomic_ref.h#L278-L280 Is there something special about floating-point which means the floating-point overloads of `__atomic_fetch_add` are unusable?
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzMVc1u4zYQfhr6MrBBkbIsHXTwZqsiaHrpFugxGIljk1uKFEgqTvr0BaUkm3VSoD11AQO2pfnhfD9DjNGcHVHL9p_Y_vMG56R9aP8cmq-z2vRePbV_aHKQwpNxZ0ge5kjAKh6TYvLI5BGTH81wH-jE5M3JekxM_sQqDregTBz8AwVSkDQmuIULRn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