From patchwork Tue May 16 17:31:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 94849 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp594088vqo; Tue, 16 May 2023 10:33:30 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ56vkCXcPIoOPxLRxcMPYoNxV5rhMLVQ9qoCldsHFT2RZ+b3WtGeL4T+Eto0mwodttj7lPc X-Received: by 2002:a17:907:96a1:b0:966:a691:678d with SMTP id hd33-20020a17090796a100b00966a691678dmr32427703ejc.51.1684258410007; Tue, 16 May 2023 10:33:30 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684258409; cv=none; d=google.com; s=arc-20160816; b=f43l4N4P919z75mPhpu5FYiCj+y2MIxdUldpGSOVpBRAJsDoBPC7ZW6HyIFfFEdb1Q jW9reEdJvdkoiVqrG9kdxDLGDu1fDOQqXrozoxvcZlYIF9GdKwlhHfJghNf41mcLCpdA 3vURTgOBoEaAHUBpjwmWkwiY7PnKYJqhmyBOWGVfDafHnYHy5t+1yXe91jje3Eu0t5mi 8HOqeRoRmRBD7ffg5yMBMxb1XVky5t34K0bzyaup1DveBvAGRU8KHtqGuXwxY/GzmnZw ZfLeGS218aMX88OjDArlEndFeqHedC0w9S7VnSXMd2C/55n9+AW2BB5JfQsdps1WmZMQ GFcg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Zpz1M8ZMp5WVAj94+4zKuHJ2dG8qYD2hQpzQcAYKBlY=; b=biwweyCkGljJSDfHq/1CQXN7sFY50u7uVPWiyg2CIUPOPoYNwVxlilP9f3wGdw+RZL sQSF5ITXURBOoVcI6oSUtNbWlAR5zMQ5wbY/1MownVRbqZl7h+Nm50XmVGDLpw2lAJA8 68ObRB4fH5qKYCS/NJyDilsWhaRnpmbaXwnWfwU/b3bcQNinGGBKmQtybhiji7hfeUN8 wu1v3ww3ZnoNPI1tcQsNwmwzF31wST70JOX5RHGP/Ff6G7mXH+pJtowQ8xd++IQ7iRXg f86Y/F1jqWb0EFOIz8kuxgzq6weoQHMbxKIAbYqA0D46ROqjbV4QptmOHHxiS/+LcHeq 0hTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=v899F+1e; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id b22-20020aa7df96000000b0050bd75d3157si11558231edy.25.2023.05.16.10.33.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 16 May 2023 10:33:29 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=v899F+1e; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 167E53856970 for ; Tue, 16 May 2023 17:33:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 167E53856970 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684258403; bh=Zpz1M8ZMp5WVAj94+4zKuHJ2dG8qYD2hQpzQcAYKBlY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=v899F+1ewdLsf6Wh1UMEDpaZx/doN2wau2FbM5lpsRnrNrzzJzUrE2k2ZIgfsR2vJ hNdLOBZWV331gssVJnVgGdE1mfOUFi4X+dB7ZvJP+IhrlKMlqVkMwXfkrFE86ANuYX cxxVAq+Yvuff0BsE9Um7DPLYNwU2GxWMgS9AEeE4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id EB40E385734C for ; Tue, 16 May 2023 17:32:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EB40E385734C Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-369-lALqYCMyOA6CUlGeGLlBiw-1; Tue, 16 May 2023 13:31:58 -0400 X-MC-Unique: lALqYCMyOA6CUlGeGLlBiw-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 25EF538294A9; Tue, 16 May 2023 17:31:58 +0000 (UTC) Received: from localhost (unknown [10.42.28.178]) by smtp.corp.redhat.com (Postfix) with ESMTP id DE97F1121314; Tue, 16 May 2023 17:31:57 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Disable cacheline alignment for DJGPP [PR109741] Date: Tue, 16 May 2023 18:31:56 +0100 Message-Id: <20230516173156.1826089-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766072946963760149?= X-GMAIL-MSGID: =?utf-8?q?1766072946963760149?= Tested powerpc64le-linux. Builds OK on djgpp too. Pushed to trunk. -- >8 -- 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. --- libstdc++-v3/acinclude.m4 | 25 +++++++++++++++ libstdc++-v3/config.h.in | 4 +++ libstdc++-v3/configure | 48 ++++++++++++++++++++++++++++ libstdc++-v3/configure.ac | 3 ++ libstdc++-v3/src/c++11/shared_ptr.cc | 8 +++-- 5 files changed, 86 insertions(+), 2 deletions(-) diff --git a/libstdc++-v3/acinclude.m4 b/libstdc++-v3/acinclude.m4 index 988c532c4e2..8129373e9dd 100644 --- a/libstdc++-v3/acinclude.m4 +++ b/libstdc++-v3/acinclude.m4 @@ -5471,6 +5471,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 f91f7eb9097..bbb2613ff69 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 a9589d882e6..188be08d716 100755 --- a/libstdc++-v3/configure +++ b/libstdc++-v3/configure @@ -71957,6 +71957,54 @@ _ACEOF fi + + + 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))))