<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 - __atomic_{load,store} on i686 uses libcalls"
   href="https://bugs.llvm.org/show_bug.cgi?id=36860">36860</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>__atomic_{load,store} on i686 uses libcalls
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

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

        <tr>
          <th>OS</th>
          <td>Linux
          </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>LLVM Codegen
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>pirama@google.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>This was originall reported at the Android NDK issue tracker
(<a href="https://github.com/android-ndk/ndk/issues/521">https://github.com/android-ndk/ndk/issues/521</a>).

$ cat atomic.cpp
unsigned long long
f(unsigned long long* x)
{
  unsigned long long v;
  __atomic_load(x, &v, __ATOMIC_SEQ_CST);
  return v;  
}

void
g(unsigned long long* x, unsigned long long v)
{
  __atomic_store(x, &v, __ATOMIC_SEQ_CST);
}

$ clang++ -std=gnu++11 -O2 --target=i386-linux-android -S -o - atomic.cpp
-fno-exceptions -fno-rtti

Clang generates library calls to __atomic_{load,store}_8 while gcc implements
the operations using movq.  Marking the pointers as 8-byte aligned causes Clang
to generate cmpxchg8b.

The frontend [1] and the backend[2] delegate atomic loads and stores to
libcalls when the alighnment of the type is smaller than the size.  For i686,
where long long is aligned to 4 bytes, this causes Clang to always generate the
library call.

A possible fix is to specialize 'hasBuiltinAtomic' in TargetInfo for x86_32 and
allowing atomic operations on 4-byte-aligned pointers to 8-byte types.

[1]
<a href="https://github.com/llvm-mirror/clang/blob/f353d86deb3ea8299f37c5477dcbb9ea30558fb1/lib/CodeGen/CGAtomic.cpp#L758">https://github.com/llvm-mirror/clang/blob/f353d86deb3ea8299f37c5477dcbb9ea30558fb1/lib/CodeGen/CGAtomic.cpp#L758</a>
[2]
<a href="https://github.com/llvm-mirror/llvm/blob/74173207d108cc27f8a02ae06ad90cad42b69b91/lib/CodeGen/AtomicExpandPass.cpp#L188">https://github.com/llvm-mirror/llvm/blob/74173207d108cc27f8a02ae06ad90cad42b69b91/lib/CodeGen/AtomicExpandPass.cpp#L188</a></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>