<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 - LLVM lowers __atomic builtin to a function call when lowering __sync builtin to a single instruction with LOCK prefix"
   href="https://bugs.llvm.org/show_bug.cgi?id=38846">38846</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>LLVM lowers __atomic builtin to a function call when lowering __sync builtin to a single instruction with LOCK prefix
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libraries
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </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>Common Code Generator Code
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>twoh@fb.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org
          </td>
        </tr></table>
      <p>
        <div>
        <pre>=== Test program ===

#include <cstdint>

template <typename T>
struct __attribute__((__packed__)) Foo {
    T a;
    void incAtomic() {
        __atomic_add_fetch(&a, 1, __ATOMIC_RELEASE);
    }
    void incSync() {

        __sync_add_and_fetch(&a, 1);
    }
};

int main(int argc, char** argv) {
    Foo<uint16_t> f;
    f.a = argc;
    f.incAtomic();
    f.incSync();
    return f.a;
}

=== Disassembled instructions ===
(compiled with clang++ --std=c++17 -O3 -Wall -DNDEBUG=1) 

main:
        push    rax
        mov     word ptr [rsp], di
        mov     rdi, rsp 
        mov     esi, 1
        mov     edx, 3
        call    __atomic_fetch_add_2
        lock            add     word ptr [rsp], 1 # __sync_add_and_fetch 
        movzx   eax, word ptr [rsp]
        pop     rcx
        ret

GCC's documentation for builtins
(<a href="https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/_005f_005fatomic-Builtins.html">https://gcc.gnu.org/onlinedocs/gcc-4.7.2/gcc/_005f_005fatomic-Builtins.html</a>)
says that __atomic builtsins are intended to replace __sync builtins, so there
should be no semantic difference between corresponding ones. 

I believe the difference comes from the alignment checking logic
(<a href="https://github.com/llvm-mirror/clang/blob/master/lib/CodeGen/CGAtomic.cpp#L768">https://github.com/llvm-mirror/clang/blob/master/lib/CodeGen/CGAtomic.cpp#L768</a>)
only included in handling __atomic builtins. However, considering that LOCK
prefix works for unaligned accesses as well, I'm not sure if that alignment
check is necessary. LOCK for unaligned memory access is expensive, but not sure
if it is more expensive than library calls.</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>