<html>
    <head>
      <base href="https://llvm.org/bugs/" />
    </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 --- - undefined reference to `__atomic_load_8', when it comes to 64-bit atomic load."
   href="https://llvm.org/bugs/show_bug.cgi?id=23262">23262</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>undefined reference to `__atomic_load_8', when it comes to 64-bit atomic load.
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>C++11
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>rwindz0@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>clang fails to deal with 64-bit atomic load and compare_exchange_weak when it
comes with -stdlib=libstdc++, while gcc can deal with it correctly.

The test program is from
<a href="http://preshing.com/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation/">http://preshing.com/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation/</a>
:

cat >test-atomic.cc<<EOF
#include <atomic>
struct Terms
{
    uint32_t x;
    uint32_t y;
};

std::atomic<Terms> terms;

void atomicFibonacciStep()
{
    Terms oldTerms = terms.load();
    Terms newTerms;
    do
    {
        newTerms.x = oldTerms.y;
        newTerms.y = oldTerms.x + oldTerms.y;
    }
    while (!terms.compare_exchange_weak(oldTerms, newTerms));
}

int main() {
    atomicFibonacciStep();
}
EOF

test with:
$clang++-3.7 -std=c++11 -stdlib=libstdc++ -O3 test-atomic.cc

/tmp/test-atomic-190571.o: In function
`std::atomic<Terms>::load(std::memory_order) const':
test-atomic.cc:(.text[_ZNKSt6atomicI5TermsE4loadESt12memory_order]+0x14):
undefined reference to `__atomic_load_8'
/tmp/test-atomic-190571.o: In function
`std::atomic<Terms>::compare_exchange_weak(Terms&, Terms, std::memory_order,
std::memory_order)':
test-atomic.cc:(.text[_ZNSt6atomicI5TermsE21compare_exchange_weakERS0_S0_St12memory_orderS3_]+0x2b):
undefined reference to `__atomic_compare_exchange_8'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

$g++-4.9 -std=c++11 -O3 test-atomic.cc 
(exited status 0)

$clang++-3.7 -std=c++11 -stdlib=libstdc++ -O3 -S test-atomic.cc -o -
...
        callq   __atomic_load_8
        movq    %rax, 8(%rsp)
        movq    %rax, %rcx
        shrq    $32, %rcx
        movl    %ecx, %edx
        addl    %eax, %edx
        shlq    $32, %rdx
        orq     %rcx, %rdx
        leaq    8(%rsp), %rsi
        movl    $terms, %edi
        movl    $5, %ecx
        movl    $5, %r8d
        callq   __atomic_compare_exchange_8
        testb   %al, %al
        jne     .LBB0_3
...

$g++-4.9 -std=c++11 -O3 -S test-atomic.cc -o -

...
.LFB329:
        .cfi_startproc
        movq    terms(%rip), %rax
        movq    %rax, %rdx
        movl    %eax, -24(%rsp)
        shrq    $32, %rdx
        movl    %edx, -20(%rsp)
        .p2align 4,,10
        .p2align 3
.L4:
        addl    %edx, %eax
        movl    %edx, %edx
        salq    $32, %rax
        orq     %rax, %rdx
        movq    -24(%rsp), %rax
        lock cmpxchgq   %rdx, terms(%rip)
        jne     .L6
        rep ret
        .p2align 4,,10
        .p2align 3
...

BTW: clang++ works just fine with libc++. (the generated assembly code looks
almost the same to gcc's)</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>