<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Possible missed optimization: relaxed stores to narrow atomic types are not coalesced"
   href="https://bugs.llvm.org/show_bug.cgi?id=51263">51263</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Possible missed optimization: relaxed stores to narrow atomic types are not coalesced
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>new-bugs
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>new bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>eric.mueller1024@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following code (<a href="https://godbolt.org/z/fcqexoYMe">https://godbolt.org/z/fcqexoYMe</a>):

#include <cstddef>
#include <atomic>

struct normal_storage
{
    uint8_t a[8];
};

struct atomic_storage
{
    std::atomic<uint8_t> a[8];
};

void f(normal_storage * ns, atomic_storage * as)
{
    for (size_t i = 0; i < 8; ++i) {
        as->a[i].store(ns->a[i], std::memory_order_relaxed);
    }
}

generates 8 single-byte loads and stores when compiled for x86_64 with -O3 with
clang trunk.

f(normal_storage*, atomic_storage*): # @f(normal_storage*, atomic_storage*)
        mov     al, byte ptr [rdi]
        mov     byte ptr [rsi], al
        mov     al, byte ptr [rdi + 1]
        mov     byte ptr [rsi + 1], al
        mov     al, byte ptr [rdi + 2]
        mov     byte ptr [rsi + 2], al
        mov     al, byte ptr [rdi + 3]
        mov     byte ptr [rsi + 3], al
        mov     al, byte ptr [rdi + 4]
        mov     byte ptr [rsi + 4], al
        mov     al, byte ptr [rdi + 5]
        mov     byte ptr [rsi + 5], al
        mov     al, byte ptr [rdi + 6]
        mov     byte ptr [rsi + 6], al
        mov     al, byte ptr [rdi + 7]
        mov     byte ptr [rsi + 7], al
        ret


Given that relaxed atomics provide no guarantees about ordering, it seems to me
like it may be legal to coalesce this into a single 8-byte store instead.

Is this a missed optimization? Or is there some specific language in the
standard that makes such an optimization illegal?

For what it's worth, from what I can tell it looks like none of GCC, ICC, nor
MSVC make this optimization.

Clearly the above code could be changed to use a single 8-byte atomic instead,
but that's somewhat besides the point. I can provide more context on why this
is useful if it helps.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>