ceph/cmake/modules/CheckCxxAtomic.cmake
Ulrich Weigand c58fc258a2 cmake: Improve test for 16-byte atomic support on IBM Z
Commit d1b9d14324586d02dc6c8dd0a9bdf4b98ae16416 added a check for
16-byte atomics without library support on IBM Z.  Unfortunately
it turns out this test only works correctly when the test case
is built without optimization.  (This is normally true, but it
may not be the case when passing explicit CXXFLAGS to cmake.)

The underlying reason is that GCC may choose to use either an
inline implementation of the 16-byte atomics or library calls,
depending on whether or not it is able to prove the atomic
variable is properly aligned.  At -O0 it is never able to prove
that, but at higher optimization levels it depends on the complexity
of the expression (in particular, whether GCC can track down the
definition of the underlying object).

As the test case uses a very simple expression, it may happen that
this test can be built without requiring library support, but some
of the "real" uses of atomics in Ceph code cannot.   This defeats
the whole purpose of the test at configure time.

Fixed by making the access pattern in the test more complex, so
that the test fails even at high optimization levels.

Fixes: https://tracker.ceph.com/issues/43747

Signed-off-by: Ulrich Weigand <ulrich.weigand@de.ibm.com>
2020-01-23 13:13:58 +01:00

56 lines
1.7 KiB
CMake

# some platforms do not offer support for atomic primitive for all integer
# types, in that case we need to link against libatomic
include(CheckCXXSourceCompiles)
include(CMakePushCheckState)
function(check_cxx_atomics var)
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
check_cxx_source_compiles("
#include <atomic>
#include <cstdint>
#if __s390x__
// Boost needs 16-byte atomics for tagged pointers.
// These are implemented via inline instructions on the platform
// if 16-byte alignment can be proven, and are delegated to libatomic
// library routines otherwise. Whether or not alignment is provably
// OK for a std::atomic unfortunately depends on compiler version and
// optimization levels, and also on the details of the expression.
// We specifically test access via an otherwise unknown pointer here
// to ensure we get the most complex case. If this access can be
// done without libatomic, then all accesses can be done.
bool atomic16(std::atomic<unsigned __int128> *ptr)
{
return *ptr != 0;
}
#endif
int main() {
std::atomic<uint8_t> w1;
std::atomic<uint16_t> w2;
std::atomic<uint32_t> w4;
std::atomic<uint64_t> w8;
return w1 + w2 + w4 + w8;
}
" ${var})
endfunction(check_cxx_atomics)
cmake_push_check_state()
check_cxx_atomics(HAVE_CXX11_ATOMIC)
cmake_pop_check_state()
if(NOT HAVE_CXX11_ATOMIC)
cmake_push_check_state()
set(CMAKE_REQUIRED_LIBRARIES "atomic")
check_cxx_atomics(HAVE_LIBATOMIC)
cmake_pop_check_state()
if(HAVE_LIBATOMIC)
set(LIBATOMIC_LINK_FLAGS "-Wl,--as-needed -latomic")
else()
message(FATAL_ERROR
"Host compiler ${CMAKE_CXX_COMPILER} requires libatomic, but it is not found")
endif()
endif()