From c58fc258a280bce62ed454bd0345076aca29a822 Mon Sep 17 00:00:00 2001 From: Ulrich Weigand Date: Thu, 23 Jan 2020 13:07:08 +0100 Subject: [PATCH] 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 --- cmake/modules/CheckCxxAtomic.cmake | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/cmake/modules/CheckCxxAtomic.cmake b/cmake/modules/CheckCxxAtomic.cmake index 68efa15314b9d..f2d89cf3e0beb 100644 --- a/cmake/modules/CheckCxxAtomic.cmake +++ b/cmake/modules/CheckCxxAtomic.cmake @@ -10,18 +10,29 @@ function(check_cxx_atomics var) check_cxx_source_compiles(" #include #include + +#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 *ptr) +{ + return *ptr != 0; +} +#endif + int main() { std::atomic w1; std::atomic w2; std::atomic w4; std::atomic w8; -#ifdef __s390x__ - // Boost needs 16-byte atomics for tagged pointers. - std::atomic w16; -#else - #define w16 0 -#endif - return w1 + w2 + w4 + w8 + w16; + return w1 + w2 + w4 + w8; } " ${var}) endfunction(check_cxx_atomics) -- 2.39.5