From patchwork Sun Nov 22 08:05:59 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Bernd Edlinger X-Patchwork-Id: 1404435 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=hotmail.de Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Cf2sy6W0zz9sRK for ; Sun, 22 Nov 2020 19:06:11 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 03B7F385783D; Sun, 22 Nov 2020 08:06:08 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from EUR01-HE1-obe.outbound.protection.outlook.com (mail-oln040092065077.outbound.protection.outlook.com [40.92.65.77]) by sourceware.org (Postfix) with ESMTPS id 9C4C93857801 for ; Sun, 22 Nov 2020 08:06:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 9C4C93857801 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=hotmail.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=bernd.edlinger@hotmail.de ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GjLg4VhKv+K71re2gZFGsqgQ2ZfKcnwyteD3iPBycONgc6HItUl0dy8wr5S1FUzt1ZCNmdup26SycYWv5knhyGygJNxykf+9aszomO0HGX0kwDv5VDetX0TzvK/F90UN1qNST5sG0hELFNWIDZWhLTCF+vtthCkooAv05Jf/3H2xxt2lzfl500Jagxg/5+QG8b5XUONvxr2G2Pt4aeLccMUlEZadNjLJTohTVy9ZPOP2iLZkkVdLHAhkc12vNt55NIwb7pPzF1YO0dniZDgrAlMlEw8PFiQ3H14EahfoasLEvbfkjtMfBdntRiTTbRGishHMnc86uVZx22ZB31sjtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=5EdYlKqBqt3TkuEUyIiVKkwOk4NlWCnNOaax9wcvcs4=; b=dQtZZa5UvmGhOp5HidxwiNuvTPX/oUHIcFuVBkxkuq4bjMTf4cZTnKXjGhpV9iFxdhuai0voyIOmNsH1EjnQ1Xx9DNqaggDriPfcJgiw0F6ZtTQmceHBzTyAvb1ol0yoqASN0Mtmk+lOdZJ8M+99PwB2dda7rLU+WjbroEzHQ5PmStv0+RortYGFWzFUlcvkUrMi5+qEEGhhPClaI2SOc846jRouoS/BAdndaqb9P9Jmzkd8K7XDYBl7PfHNrOJ5K533wfhokUqkiKjgEILM2AONm6G+Kwa+UXsXcDEN3VkjVqUKMUne7LHwBa8BUTeowlZ4zR5EmmWXuakKgq3FMg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none Received: from DB5EUR01FT047.eop-EUR01.prod.protection.outlook.com (2a01:111:e400:7e1a::43) by DB5EUR01HT155.eop-EUR01.prod.protection.outlook.com (2a01:111:e400:7e1a::277) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3589.20; Sun, 22 Nov 2020 08:06:01 +0000 Received: from AM6PR03MB5170.eurprd03.prod.outlook.com (2a01:111:e400:7e1a::43) by DB5EUR01FT047.mail.protection.outlook.com (2a01:111:e400:7e1a::452) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3589.20 via Frontend Transport; Sun, 22 Nov 2020 08:06:01 +0000 X-IncomingTopHeaderMarker: OriginalChecksum:93E863E5525259DB35C735FDF0BBA2CC64FABF3188BC1EC3DD82AC4B7FA22883; UpperCasedChecksum:B42494E1C2407CDA11AA2F6E09D3A69552291718181631046E9D10B446E62AEC; SizeAsReceived:9086; Count:46 Received: from AM6PR03MB5170.eurprd03.prod.outlook.com ([fe80::184e:5e8c:db8f:a596]) by AM6PR03MB5170.eurprd03.prod.outlook.com ([fe80::184e:5e8c:db8f:a596%5]) with mapi id 15.20.3589.022; Sun, 22 Nov 2020 08:06:01 +0000 Subject: [PATCH] Avoid atomic for guard acquire when that is expensive To: "Richard Earnshaw (lists)" , "gcc-patches@gcc.gnu.org" , Ramana Radhakrishnan , Jason Merrill , Nathan Sidwell , Christophe Lyon References: <8383d817-8622-4d1f-9564-8c10131db664@arm.com> From: Bernd Edlinger Message-ID: Date: Sun, 22 Nov 2020 09:05:59 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 In-Reply-To: Content-Language: en-US X-TMN: [NnFFvO3UDeinxAiv2vdEMR/tiDMLVvCM] X-ClientProxiedBy: AM3PR05CA0131.eurprd05.prod.outlook.com (2603:10a6:207:2::33) To AM6PR03MB5170.eurprd03.prod.outlook.com (2603:10a6:20b:ca::23) X-Microsoft-Original-Message-ID: MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from [192.168.1.101] (84.57.57.184) by AM3PR05CA0131.eurprd05.prod.outlook.com (2603:10a6:207:2::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3589.20 via Frontend Transport; Sun, 22 Nov 2020 08:06:00 +0000 X-MS-PublicTrafficType: Email X-IncomingHeaderCount: 46 X-EOPAttributedMessage: 0 X-MS-Office365-Filtering-Correlation-Id: 100cd5b2-73fa-46d9-0e9e-08d88ebd7316 X-MS-TrafficTypeDiagnostic: DB5EUR01HT155: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: fZp2Qc6zctSSGhGl0O0shCgGJmNbGV4npgI4SCkXM8h3vBmsSiS0zF7ocnpQfDHPZje/DseCEP4Pc1j1/diS7vFxg+BAU9sakCHIoN1ZoxZCLMEdGJNJQ8na7ud3NVWsljiWXVSOARdsDiwlbpABFxVOuBMA3YFSvq+35uVNSAJpD9dP5ViID4fVCkMq01KXiMnozP+sS8ZHuXOOKE7EbA== X-MS-Exchange-AntiSpam-MessageData: vrMp6BmYl69xPV4zkqjgkoBAhAMa7iciBRcUqi1IK35vyP+iiSYhIigh+frYaWCyoVPfvYul7H8fwyDP24YhM2rui15N+pVYTYbWT53HzI8VQLvSN7lWZd7KEYnpPHSWHC5kXLap+sZGEXLSJXpjdA== X-OriginatorOrg: outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: 100cd5b2-73fa-46d9-0e9e-08d88ebd7316 X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Nov 2020 08:06:01.3098 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-AuthSource: DB5EUR01FT047.eop-EUR01.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: Internet X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: DB5EUR01HT155 X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00, FORGED_MUA_MOZILLA, FREEMAIL_FROM, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_MANYTO, MSGID_FROM_MTA_HEADER, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" Hi, this avoids the need to use -fno-threadsafe-statics on arm-none-eabi or working around that problem by supplying a dummy __sync_synchronize function which might just lead to silent code failure of the worst kind (non-reproducable, racy) at runtime, as was pointed out on previous discussions here. When the atomic access involves a call to __sync_synchronize it is better to call __cxa_guard_acquire unconditionally, since it handles the atomics too, or is a non-threaded implementation when there is no gthread support for this target. This fixes also a bug for the ARM EABI big-endian target, that is, previously the wrong bit was checked. Regression tested successfully on arm-none-eabi with newlib-3.3.0. Is it OK for trunk? Thanks Bernd. From 9fd070407408cf10789f5e9eb5ddda2fb3798e6f Mon Sep 17 00:00:00 2001 From: Bernd Edlinger Date: Sun, 22 Nov 2020 08:11:14 +0100 Subject: [PATCH] Avoid atomic for guard acquire when that is expensive When the atomic access involves a call to __sync_synchronize it is better to call __cxa_guard_acquire unconditionally, since it handles the atomics too, or is a non-threaded implementation when there is no gthread support for this target. This fixes also a bug for the ARM EABI big-endian target, that is, previously the wrong bit was checked. gcc: 2020-11-22 Bernd Edlinger * target.def (guard_atomic_expensive): New hook. * doc/tm.texi: Document TARGET_CXX_GUARD_ATOMIC_EXPENSIVE. * doc/tm.texi.in: Likewise. * config/arm/arm.c (arm_cxx_guard_atomic_expensive): New callback. gcc/cp: 2020-11-22 Bernd Edlinger * decl2.c: (build_atomic_load_byte): Rename to... (build_atomic_load_type): ... and add new parameter type. (get_guard_cond): Skip the atomic here if that is expensive. Use the correct type for the atomic load on certain targets. --- gcc/config/arm/arm.c | 13 +++++++++++++ gcc/cp/decl2.c | 12 ++++++++---- gcc/doc/tm.texi | 7 +++++++ gcc/doc/tm.texi.in | 2 ++ gcc/target.def | 10 ++++++++++ 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/gcc/config/arm/arm.c b/gcc/config/arm/arm.c index 04190b1..04ca1fe 100644 --- a/gcc/config/arm/arm.c +++ b/gcc/config/arm/arm.c @@ -235,6 +235,7 @@ static rtx arm_dwarf_register_span (rtx); static tree arm_cxx_guard_type (void); static bool arm_cxx_guard_mask_bit (void); +static bool arm_cxx_guard_atomic_expensive (void); static tree arm_get_cookie_size (tree); static bool arm_cookie_has_size (void); static bool arm_cxx_cdtor_returns_this (void); @@ -593,6 +594,9 @@ static const struct attribute_spec arm_attribute_table[] = #undef TARGET_CXX_GUARD_MASK_BIT #define TARGET_CXX_GUARD_MASK_BIT arm_cxx_guard_mask_bit +#undef TARGET_CXX_GUARD_ATOMIC_EXPENSIVE +#define TARGET_CXX_GUARD_ATOMIC_EXPENSIVE arm_cxx_guard_atomic_expensive + #undef TARGET_CXX_GET_COOKIE_SIZE #define TARGET_CXX_GET_COOKIE_SIZE arm_get_cookie_size @@ -28882,6 +28886,15 @@ arm_cxx_guard_mask_bit (void) } +/* Return true if atomics involve a call to __sync_synchronize. */ + +static bool +arm_cxx_guard_atomic_expensive (void) +{ + return TARGET_AAPCS_BASED && !TARGET_HAVE_DMB && !TARGET_HAVE_DMB_MCR; +} + + /* The EABI specifies that all array cookies are 8 bytes long. */ static tree diff --git a/gcc/cp/decl2.c b/gcc/cp/decl2.c index 1bc7b7e..e2b29a6 100644 --- a/gcc/cp/decl2.c +++ b/gcc/cp/decl2.c @@ -3300,15 +3300,15 @@ get_guard (tree decl) /* Return an atomic load of src with the appropriate memory model. */ static tree -build_atomic_load_byte (tree src, HOST_WIDE_INT model) +build_atomic_load_type (tree src, HOST_WIDE_INT model, tree type) { - tree ptr_type = build_pointer_type (char_type_node); + tree ptr_type = build_pointer_type (type); tree mem_model = build_int_cst (integer_type_node, model); tree t, addr, val; unsigned int size; int fncode; - size = tree_to_uhwi (TYPE_SIZE_UNIT (char_type_node)); + size = tree_to_uhwi (TYPE_SIZE_UNIT (type)); fncode = BUILT_IN_ATOMIC_LOAD_N + exact_log2 (size) + 1; t = builtin_decl_implicit ((enum built_in_function) fncode); @@ -3350,8 +3350,12 @@ get_guard_cond (tree guard, bool thread_safe) if (!thread_safe) guard = get_guard_bits (guard); + else if (targetm.cxx.guard_atomic_expensive ()) + guard = integer_zero_node; else - guard = build_atomic_load_byte (guard, MEMMODEL_ACQUIRE); + guard = build_atomic_load_type (guard, MEMMODEL_ACQUIRE, + targetm.cxx.guard_mask_bit () + ? TREE_TYPE (guard) : char_type_node); /* Mask off all but the low bit. */ if (targetm.cxx.guard_mask_bit ()) diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi index 2b88f78..92215cf 100644 --- a/gcc/doc/tm.texi +++ b/gcc/doc/tm.texi @@ -10728,6 +10728,13 @@ This hook determines how guard variables are used. It should return @code{true} indicates that only the least significant bit should be used. @end deftypefn +@deftypefn {Target Hook} bool TARGET_CXX_GUARD_ATOMIC_EXPENSIVE (void) +This hook determines if the guard atomic is expensive. It should return +@code{false} (the default) if the atomic is inexpensive. A return value of +@code{true} indicates that the atomic is expensive i.e., involves a call to +__sync_synchronize. In this case let __cxa_guard_acquire handle the atomics. +@end deftypefn + @deftypefn {Target Hook} tree TARGET_CXX_GET_COOKIE_SIZE (tree @var{type}) This hook returns the size of the cookie to use when allocating an array whose elements have the indicated @var{type}. Assumes that it is already diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in index 897f289..ce1d837 100644 --- a/gcc/doc/tm.texi.in +++ b/gcc/doc/tm.texi.in @@ -7321,6 +7321,8 @@ floating-point support; they are not included in this mechanism. @hook TARGET_CXX_GUARD_MASK_BIT +@hook TARGET_CXX_GUARD_ATOMIC_EXPENSIVE + @hook TARGET_CXX_GET_COOKIE_SIZE @hook TARGET_CXX_COOKIE_HAS_SIZE diff --git a/gcc/target.def b/gcc/target.def index 810d554..0c02d5c 100644 --- a/gcc/target.def +++ b/gcc/target.def @@ -6160,6 +6160,16 @@ DEFHOOK bool, (void), hook_bool_void_false) +/* Return true if the guard atomic is expensive. */ +DEFHOOK +(guard_atomic_expensive, + "This hook determines if the guard atomic is expensive. It should return\n\ +@code{false} (the default) if the atomic is inexpensive. A return value of\n\ +@code{true} indicates that the atomic is expensive i.e., involves a call to\n\ +__sync_synchronize. In this case let __cxa_guard_acquire handle the atomics.", + bool, (void), + hook_bool_void_false) + /* Returns the size of the array cookie for an array of type. */ DEFHOOK (get_cookie_size, -- 1.9.1