From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 87E083857717; Thu, 29 Jun 2023 23:01:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87E083857717 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688079674; bh=aE1PDJgK6VVKsVG2CMNML/c6FdOxugeEi7xpgh+IanE=; h=From:To:Subject:Date:From; b=PRlWDWtjtsm97SeQfcoGXGjXdEneQVOpbABcKkb8LYaKKmGPXz0YeYwInFE/2+iJc pbpGDO4W4PW1RtW6zpAroAxpp59vKQK+5ihO0gX9UtqhsypbRr2q2Z61bSTXCAks+q KzVVu1ixXAk1c1h+lyjUYw9i6eMkP/VaB2cYjVrA= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r13-7509] libstdc++: Disable cacheline alignment for DJGPP [PR109741] X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-13 X-Git-Oldrev: 132015b9c6c9f9156ff31f7d66ba92cf01d0bc90 X-Git-Newrev: dbd4acd72274f3b3d542ebf68f9962eda8f8b769 Message-Id: <20230629230114.87E083857717@sourceware.org> Date: Thu, 29 Jun 2023 23:01:14 +0000 (GMT) List-Id: https://gcc.gnu.org/g:dbd4acd72274f3b3d542ebf68f9962eda8f8b769 commit r13-7509-gdbd4acd72274f3b3d542ebf68f9962eda8f8b769 Author: Jonathan Wakely Date: Tue May 16 15:09:20 2023 +0100 libstdc++: Disable cacheline alignment for DJGPP [PR109741] DJGPP (and maybe other targets) uses MAX_OFILE_ALIGNMENT=16 which means that globals (and static objects) can't have alignment greater than 16. This causes an error for the locks defined in src/c++11/shared_ptr.cc because we try to align them to the cacheline size, to avoid false sharing. Add a configure check for the increased alignment, and live with false sharing where we can't increase the alignment. libstdc++-v3/ChangeLog: PR libstdc++/109741 * acinclude.m4 (GLIBCXX_CHECK_ALIGNAS_CACHELINE): Define. * config.h.in: Regenerate. * configure: Regenerate. * configure.ac: Use GLIBCXX_CHECK_ALIGNAS_CACHELINE. * src/c++11/shared_ptr.cc (__gnu_internal::get_mutex): Do not align lock table if not supported. use __GCC_DESTRUCTIVE_SIZE instead of hardcoded 64. (cherry picked from commit 94a311abf783de754f0f1b2d4c1f00a9788e795b) Diff: --- libstdc++-v3/acinclude.m4 | 25 ++++++++++++++++++ libstdc++-v3/config.h.in | 4 +++ libstdc++-v3/configure | 49 ++++++++++++++++++++++++++++++++++++ libstdc++-v3/configure.ac | 3 +++ libstdc++-v3/src/c++11/shared_ptr.cc | 8 ++++-- 5 files changed, 87 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index 42a8e7a775e..af978e3f50c 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -5246,6 +5246,31 @@ AC_DEFUN([GLIBCXX_ZONEINFO_DIR], [ fi ]) +dnl +dnl Check whether lock tables can be aligned to avoid false sharing. +dnl +dnl Defines: +dnl _GLIBCXX_CAN_ALIGNAS_DESTRUCTIVE_SIZE if objects with static storage +dnl duration can be aligned to std::hardware_destructive_interference_size. +dnl +AC_DEFUN([GLIBCXX_CHECK_ALIGNAS_CACHELINE], [ + AC_LANG_SAVE + AC_LANG_CPLUSPLUS + + AC_MSG_CHECKING([whether static objects can be aligned to the cacheline size]) + AC_TRY_COMPILE(, [struct alignas(__GCC_DESTRUCTIVE_SIZE) Aligned { }; + alignas(Aligned) static char buf[sizeof(Aligned) * 16]; + ], [ac_alignas_cacheline=yes], [ac_alignas_cacheline=no]) + if test "$ac_alignas_cacheline" = yes; then + AC_DEFINE_UNQUOTED(_GLIBCXX_CAN_ALIGNAS_DESTRUCTIVE_SIZE, 1, + [Define if global objects can be aligned to + std::hardware_destructive_interference_size.]) + fi + AC_MSG_RESULT($ac_alignas_cacheline) + + AC_LANG_RESTORE +]) + # Macros from the top-level gcc directory. m4_include([../config/gc++filt.m4]) m4_include([../config/tls.m4]) diff --git a/libstdc++-v3/config.h.in b/libstdc++-v3/config.h.in index a336daf2303..86c8eeef8ac 100644 --- a/libstdc++-v3/config.h.in +++ b/libstdc++-v3/config.h.in @@ -819,6 +819,10 @@ /* Define if the compiler supports C++11 atomics. */ #undef _GLIBCXX_ATOMIC_BUILTINS +/* Define if global objects can be aligned to + std::hardware_destructive_interference_size. */ +#undef _GLIBCXX_CAN_ALIGNAS_DESTRUCTIVE_SIZE + /* Define to use concept checking code from the boost libraries. */ #undef _GLIBCXX_CONCEPT_CHECKS diff --git a/libstdc++-v3/configure b/libstdc++-v3/configure index d4286b67a73..776c6b7ba58 100755 --- a/libstdc++-v3/configure +++ b/libstdc++-v3/configure @@ -71611,6 +71611,55 @@ _ACEOF fi +# For src/c++11/shared_ptr.cc alignment. + + + ac_ext=cpp +ac_cpp='$CXXCPP $CPPFLAGS' +ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5' +ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' +ac_compiler_gnu=$ac_cv_cxx_compiler_gnu + + + { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether static objects can be aligned to the cacheline size" >&5 +$as_echo_n "checking whether static objects can be aligned to the cacheline size... " >&6; } + cat confdefs.h - <<_ACEOF >conftest.$ac_ext +/* end confdefs.h. */ + +int +main () +{ +struct alignas(__GCC_DESTRUCTIVE_SIZE) Aligned { }; + alignas(Aligned) static char buf[sizeof(Aligned) * 16]; + + ; + return 0; +} +_ACEOF +if ac_fn_cxx_try_compile "$LINENO"; then : + ac_alignas_cacheline=yes +else + ac_alignas_cacheline=no +fi +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext + if test "$ac_alignas_cacheline" = yes; then + +cat >>confdefs.h <<_ACEOF +#define _GLIBCXX_CAN_ALIGNAS_DESTRUCTIVE_SIZE 1 +_ACEOF + + fi + { $as_echo "$as_me:${as_lineno-$LINENO}: result: $ac_alignas_cacheline" >&5 +$as_echo "$ac_alignas_cacheline" >&6; } + + ac_ext=c +ac_cpp='$CPP $CPPFLAGS' +ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5' +ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5' +ac_compiler_gnu=$ac_cv_c_compiler_gnu + + + # Define documentation rules conditionally. # See if makeinfo has been installed and is modern enough diff --git a/libstdc++-v3/configure.ac b/libstdc++-v3/configure.ac index 0dd550a4b4b..df01f58bd83 100644 --- a/libstdc++-v3/configure.ac +++ b/libstdc++-v3/configure.ac @@ -538,6 +538,9 @@ GLIBCXX_EMERGENCY_EH_ALLOC # For src/c++20/tzdb.cc defaults. GLIBCXX_ZONEINFO_DIR +# For src/c++11/shared_ptr.cc alignment. +GLIBCXX_CHECK_ALIGNAS_CACHELINE + # Define documentation rules conditionally. # See if makeinfo has been installed and is modern enough diff --git a/libstdc++-v3/src/c++11/shared_ptr.cc b/libstdc++-v3/src/c++11/shared_ptr.cc index 74e879e5828..ae47ca03301 100644 --- a/libstdc++-v3/src/c++11/shared_ptr.cc +++ b/libstdc++-v3/src/c++11/shared_ptr.cc @@ -34,8 +34,12 @@ namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden) __gnu_cxx::__mutex& get_mutex(unsigned char i) { - // increase alignment to put each lock on a separate cache line - struct alignas(64) M : __gnu_cxx::__mutex { }; +#ifdef _GLIBCXX_CAN_ALIGNAS_DESTRUCTIVE_SIZE + // Increase alignment to put each lock on a separate cache line. + struct alignas(__GCC_DESTRUCTIVE_SIZE) M : __gnu_cxx::__mutex { }; +#else + using M = __gnu_cxx::__mutex; +#endif // Use a static buffer, so that the mutexes are not destructed // before potential users (or at all) static __attribute__ ((aligned(__alignof__(M))))