llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

undefined reference to '__sync_val_compare_and_swap_16'

Open KernelAddress opened this issue 9 years ago • 21 comments

Bugzilla Link 31620
Resolution FIXED
Resolved on Sep 22, 2017 10:45
Version trunk
OS Linux
CC @brycelelbach,@DimitryAndric,@dlj-NaN,@efriedma-quic,@emaste,@eugenis,@jyknight,@kcc,@preames,@mclow,@zygoloid,@TNorthover

Extended Description

I am on r291667. Linux/x86_64. libcxx and libcxxabi are checked out into projects.

The program is:

#include int main() { struct X { int a, b, c, d; }; std::atomic x; X b, c; x.compare_exchange_strong(b, c); }

$ clang++ test.cc -std=c++11 -stdlib=libc++ /tmp/test-7383d8.o: In function main': /tmp/test.cc:(.text+0xd): undefined reference to __sync_val_compare_and_swap_16' clang-4.0: error: linker command failed with exit code 1 (use -v to see invocation)

Adding -latomic does not help, libatomic does not provide such function.

A new tsan test fails with a more elaborate error pointing to exact location, for some reason it's generated for load function:

/tmp/lit_tmp_3Uws7l/atomic_test-af5f74.o: In function load': /llvm4/build/projects/compiler-rt/lib/tsan/libcxx_tsan_x86_64/include/c++/v1/atomic:893: undefined reference to __sync_val_compare_and_swap_16' /tmp/lit_tmp_3Uws7l/atomic_test-af5f74.o:/llvm4/build/projects/compiler-rt/lib/tsan/libcxx_tsan_x86_64/include/c++/v1/atomic:893: more undefined references to `__sync_val_compare_and_swap_16' follow clang-3.9: error: linker command failed with exit code 1 (use -v to see invocation)

What am I doing wrong?

KernelAddress avatar Jan 12 '17 20:01 KernelAddress

Fixed in https://reviews.llvm.org/rL313992

efriedma-quic avatar Sep 22 '17 17:09 efriedma-quic

It looks (based on compiler-rt at least) like these are ARM-specific

CMPXCHG16 is definitely an x86 feature.

Right, but this comment was about the _sync compiler-rt functions, which are ARM-specific. For x86_64 lowering for native atomics, we don't invent a function call, we just emit the instruction.

It looks (based on compiler-rt at least) like these are ARM-specific, so presumably should not be emitted for x86_64 under any circumstances, and they seem to be an implementation detail of the lowering for natively-supported ARM atomics. But I think that still does not answer the question, which is:

CMPXCHG16 is definitely an x86 feature.

brycelelbach avatar Sep 22 '17 05:09 brycelelbach

Add -mcx16

https://wandbox.org/permlink/Wypq2VZzDZAfjoRm

Follow up: this probably only works on x86. The history is that it was a GCC flag, and clang added it back in the day. Then, GCC removed it (more recently). Not sure why.

brycelelbach avatar Sep 22 '17 05:09 brycelelbach

Add -mcx16

https://wandbox.org/permlink/Wypq2VZzDZAfjoRm

brycelelbach avatar Sep 22 '17 04:09 brycelelbach

Should the frontend be lowering atomics that are not known to be supported by the target or are not correctly aligned to _atomic* libcalls, or should LLVM be changed to do that (thereby exposing more of the program semantics in the IR)?

IMO the frontend should be doing the lowering; using the same representation for lock-free and non-lock-free atomics causes problems. For example, shrinking a load is legal for lock-free atomics, but not lock-based atomics. But apparently other people don't agree, given the backend already has some support for generating _atomic* calls, so I won't push too hard on it.


It looks like the setting for clang's MaxAtomicInlineWidth is wrong, for plain x86-64 (but IIRC correct on OS X, where the minimum CPU is a Core2, so we need to make sure we're detecting CPU features correctly).

efriedma-quic avatar Sep 08 '17 23:09 efriedma-quic

Thanks, I don't have an ARM machine to test this on, and grepping compiler-rt for __sync_val does not find them :)

It looks (based on compiler-rt at least) like these are ARM-specific, so presumably should not be emitted for x86_64 under any circumstances, and they seem to be an implementation detail of the lowering for natively-supported ARM atomics. But I think that still does not answer the question, which is:

Should the frontend be lowering atomics that are not known to be supported by the target or are not correctly aligned to _atomic* libcalls, or should LLVM be changed to do that (thereby exposing more of the program semantics in the IR)?

I think you may have missed my point: as far as I can tell, those functions simply do not exist.

_sync* definitely exists on ARM. Both compiler-rt and libgcc have implementations of them, and both clang and gcc generate calls to them. See https://godbolt.org/g/yoVrET . (They aren't in the libatomic documentation because they aren't part of libatomic.)

efriedma-quic avatar Sep 08 '17 22:09 efriedma-quic

Calling __sync_val_compare_and_swap_16 is clearly just a bug; we're only supposed to use _sync* if the atomic operation is natively supported, but can't be emitted inline for some reason (like in certain configurations on ARM).

I think you may have missed my point: as far as I can tell, those functions simply do not exist. The libcalls are named _atomic, not _sync. See https://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary for the libatomic interface. GCC happens to generate calls to them for unlowerable uses of its _sync* builtins, but I suspect that is just an implementation detail leaking out.

It seems to me that the right thing for LLVM to produce is calls to the documented _atomic* libcalls that are part of the atomic runtime library interface. (Also, it's not wrong to call __atomic_compare_exchange_16 if the operation is not natively supported. In fact, that's the main purpose of those functions.)

Like I said before, under the original design LLVM didn't generate _atomic* libcalls, and just bailed out for unsupported operations; generating _atomic* libcalls was added on later, and it looks like the documentation wasn't updated correctly (since an operation lowered using those libcalls isn't really atomic; it just appears that way if all accesses use the exact same address/size).

should LLVM really be inventing wrong runtime library calls to model them

Calling __sync_val_compare_and_swap_16 is clearly just a bug; we're only supposed to use _sync* if the atomic operation is natively supported, but can't be emitted inline for some reason (like in certain configurations on ARM).

efriedma-quic avatar Sep 08 '17 19:09 efriedma-quic

LangRef says this:

align must be explicitly specified on atomic {loads,stores}, and the {load,store} has undefined behavior if the alignment is not set to a value which is at least the size in bytes of the pointee.

We seem to be missing a corresponding constraint for atomicrmw, but presumably it's supposed to be implied (particularly as atomicrmw does not take an alignment argument). We also seem to be missing verifier enforcement of this rule.

nlewycky suggested that perhaps the above is a design mistake; we could fix this in LLVM and remove the above restrictions, since there is a defined libcall interface for LLVM to lower into and IR-level atomic load/store instructions are easier to optimize than libcalls.

Clang believes that x86 has 16-byte atomics

Err, x86_64.

Clang believes that x86 has 16-byte atomics, so it emits direct instructions to load and store them, albeit with less that 16-byte alignment.

Perhaps Clang should be lowering these atomic ops differently based on the alignment, but should LLVM really be inventing wrong runtime library calls to model them? It seems to me that either LLVM should call the correct runtime library hook or it should produce an error.

Having the same problem with 4.0 on FreeBSD 11.

$ clang++ atomic.cpp atomic.cpp:5:14: warning: template argument uses local type 'X' [-Wlocal-type-template-args] std::atomic x; ^ 1 warning generated. /tmp/atomic-960e7b.o: In function main': atomic.cpp:(.text+0x106): undefined reference to __sync_val_compare_and_swap_16' atomic.cpp:(.text+0x1cf): undefined reference to __sync_val_compare_and_swap_16' atomic.cpp:(.text+0x2fa): undefined reference to __sync_val_compare_and_swap_16' atomic.cpp:(.text+0x3a7): undefined reference to __sync_val_compare_and_swap_16' atomic.cpp:(.text+0x4dd): undefined reference to __sync_val_compare_and_swap_16' /tmp/atomic-960e7b.o:atomic.cpp:(.text+0x58a): more undefined references to `__sync_val_compare_and_swap_16' follow clang++: error: linker command failed with exit code 1 (use -v to see invocation)

$ clang++ --version FreeBSD clang version 4.0.0 (tags/RELEASE_400/final 297347) (based on LLVM 4.0.0) Target: x86_64-unknown-freebsd11.1 Thread model: posix InstalledDir: /usr/bin

llvmbot avatar Aug 25 '17 23:08 llvmbot

Re-binning as a Clang bug since there isn't anything libc++ can do abut this.

llvmbot avatar May 05 '17 04:05 llvmbot

Humm... the error still happens on the reproducer.

$ bin/clang++ -v clang version 5.0.0 (trunk 300859)

$ bin/clang++ /tmp/atomic.cc -std=c++11 -latomic

$ bin/clang++ /tmp/atomic.cc -std=c++11 /tmp/atomic-aa822e.o: In function std::atomic<main::X>::compare_exchange_strong(main::X&, main::X, std::memory_order, std::memory_order)': /tmp/atomic.cc:(.text+0xec): undefined reference to __atomic_compare_exchange' clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

$ bin/clang++ /tmp/atomic.cc -std=c++11 -stdlib=libc++ /tmp/atomic-760340.o: In function main': /tmp/atomic.cc:(.text+0xed): undefined reference to __sync_val_compare_and_swap_16' /tmp/atomic.cc:(.text+0x1b6): undefined reference to __sync_val_compare_and_swap_16' /tmp/atomic.cc:(.text+0x2e1): undefined reference to __sync_val_compare_and_swap_16' /tmp/atomic.cc:(.text+0x38e): undefined reference to __sync_val_compare_and_swap_16' /tmp/atomic.cc:(.text+0x4c4): undefined reference to __sync_val_compare_and_swap_16' /tmp/atomic-760340.o:/tmp/atomic.cc:(.text+0x571): more undefined references to `__sync_val_compare_and_swap_16' follow clang-5.0: error: linker command failed with exit code 1 (use -v to see invocation)

KernelAddress avatar Apr 20 '17 19:04 KernelAddress

which generates the the IR instruction "load atomic volatile i128", which is supposed to get lowered to a call to __atomic_load_16

Well, either that, or clang isn't supposed to generate "load atomic i128"; it wasn't supposed to under the original design, but it looks the backend now knows how to generate _atomic* calls (see https://reviews.llvm.org/D18200).

efriedma-quic avatar Jan 12 '17 22:01 efriedma-quic

Not really a libcxx bug; libcxx is calling __c11_atomic_load, which generates the the IR instruction "load atomic volatile i128", which is supposed to get lowered to a call to __atomic_load_16. Somehow the last step isn't working correctly, and we end up with a call to __sync_val_compare_and_swap_16.

efriedma-quic avatar Jan 12 '17 21:01 efriedma-quic

$ find -name "*.cpp" -exec grep "sync_val_compare_and_swap" {} ; -print Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_1] = "__sync_val_compare_and_swap_1"; Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_2] = "__sync_val_compare_and_swap_2"; Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_4] = "__sync_val_compare_and_swap_4"; Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_8] = "__sync_val_compare_and_swap_8"; Names[RTLIB::SYNC_VAL_COMPARE_AND_SWAP_16] = "__sync_val_compare_and_swap_16"; ./lib/CodeGen/TargetLoweringBase.cpp return __sync_val_compare_and_swap(ptr, old_value, new_value); ./lib/Support/Atomic.cpp case Builtin::BI__sync_val_compare_and_swap: case Builtin::BI__sync_val_compare_and_swap_1: case Builtin::BI__sync_val_compare_and_swap_2: case Builtin::BI__sync_val_compare_and_swap_4: case Builtin::BI__sync_val_compare_and_swap_8: case Builtin::BI__sync_val_compare_and_swap_16: ./tools/clang/lib/CodeGen/CGBuiltin.cpp

KernelAddress avatar Jan 12 '17 20:01 KernelAddress

Looking at ToT clang and libc++ (on Mac OS X):

I see no use of __sync_val_compare_and_swap_16 in libc++, compiler-rt or libc++abi.

atomic:893 calls __c11_atomic_load, which is atomic:691 and calls __atomic_load, which is a compiler intrinsic.

mclow avatar Jan 12 '17 20:01 mclow