From patchwork Fri Oct 11 14:57:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 1996180 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=h12biCYE; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4XQ8v91CY1z1xt1 for ; Sat, 12 Oct 2024 02:00:37 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 574C6385843F for ; Fri, 11 Oct 2024 15:00:35 +0000 (GMT) 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.133.124]) by sourceware.org (Postfix) with ESMTP id A85C53857C5F for ; Fri, 11 Oct 2024 14:59:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A85C53857C5F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A85C53857C5F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728658779; cv=none; b=xpM04qdUorFzQZwctylumr3rlA/Wd7fslMW8Tde6BLlWrTPf3oaSY6a3Ji8IqCkPbqz8yxHvG3yvcwKIXUibyouqZ8HUfr3CGjJW4Re7HzApt43iQEAo5hXbH3rMpc1VavlL2LGCkUjP3/Ow4rFvAnOOf+Of4GsX9eqmX92FVuE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1728658779; c=relaxed/simple; bh=blRouGE3ai5I14ddyKFYO2/INlN3yzDFwMBbNl60Spg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=xUTPVZSyCBBf1Ea8BfUAWmx3Lhfi708QJk8/i+R7qEZgwk2DWf2I9n5l5W2k1Z3oCQbbqYUNYJ2pL8ZJvsMyuMPxv0tvSnz4Hqnedb0+OZ+h1fFy3Se7yJIfkuCLgJgwgh/tqpP2/CWZx8c9oKMM+T8Lq9nZ+YUsZaNEqhanSDs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1728658774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hDuUzIC5krI58E2pkfXA435f0OwkOeOHYPMYS6zLv40=; b=h12biCYEVUQplXUsCqSzZupRO2DUlIxprGclC4Vla9ay/5EtT4W5qtC3Du8oS7NPJ3rPN4 UPNuoVX4ACCFeTbxVus1TaR7qg9fn/gru9a6csKurVndcwlLAvIZo8l9XydwCJnxrnwFNx fd+r9qRK3muXXLOqFXgvhY9yzl1IpYw= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-608-L20sDSCkMdiKGAZJHlFnrg-1; Fri, 11 Oct 2024 10:59:30 -0400 X-MC-Unique: L20sDSCkMdiKGAZJHlFnrg-1 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 68E5C1955EB4; Fri, 11 Oct 2024 14:59:29 +0000 (UTC) Received: from localhost (unknown [10.42.28.27]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id EC0EA19560B2; Fri, 11 Oct 2024 14:59:27 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH v2 2/2] libstdc++: Enable memset optimizations for distinct character types [PR93059] Date: Fri, 11 Oct 2024 15:57:24 +0100 Message-ID: <20241011145924.1323101-2-jwakely@redhat.com> In-Reply-To: <20241011145924.1323101-1-jwakely@redhat.com> References: <20241011145924.1323101-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.0 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, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org This adds __are_same to the enable_if condition, to ensure that the optimized overload can still be selected for std::byte (because that's no longer done via a __memcpyable_integer specialization). Tested x86_64-linux. -- >8 -- Currently we only optimize std::fill to memset when the source and destination types are the same byte-sized type. This means that we fail to optimize cases like std::fill(buf. buf+n, 0) because the literal 0 is not the same type as the character buffer. Such cases can safely be optimized to use memset, because assigning an int (or other integer) to a narrow character type has the same effects as converting the integer to unsigned char then copying it with memset. This patch enables the optimized code path when the fill character is a memcpy-able integer (using the new __memcpyable_integer trait). We still need to check is_same to enable the memset optimization for filling a range of std::byte with a std::byte value, because that isn't a memcpyable integer. libstdc++-v3/ChangeLog: PR libstdc++/93059 * include/bits/stl_algobase.h (__fill_a1(T*, T*, const T&)): Change template parameters and enable_if condition to allow the fill value to be an integer. --- libstdc++-v3/include/bits/stl_algobase.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/libstdc++-v3/include/bits/stl_algobase.h b/libstdc++-v3/include/bits/stl_algobase.h index 9e92211c124..384e5fdcdc9 100644 --- a/libstdc++-v3/include/bits/stl_algobase.h +++ b/libstdc++-v3/include/bits/stl_algobase.h @@ -967,23 +967,27 @@ _GLIBCXX_END_NAMESPACE_CONTAINER #pragma GCC diagnostic pop // Specialization: for char types we can use memset. - template + template _GLIBCXX20_CONSTEXPR inline typename - __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type - __fill_a1(_Tp* __first, _Tp* __last, const _Tp& __c) + __gnu_cxx::__enable_if<__is_byte<_Up>::__value + && (__are_same<_Up, _Tp>::__value // for std::byte + || __memcpyable_integer<_Tp>::__value), + void>::__type + __fill_a1(_Up* __first, _Up* __last, const _Tp& __x) { - const _Tp __tmp = __c; + // This hoists the load out of the loop and also ensures that we don't + // use memset for cases where the assignment would be ill-formed. + const _Up __val = __x; #if __cpp_lib_is_constant_evaluated if (std::is_constant_evaluated()) { for (; __first != __last; ++__first) - *__first = __tmp; - return; + *__first = __val; } #endif if (const size_t __len = __last - __first) - __builtin_memset(__first, static_cast(__tmp), __len); + __builtin_memset(__first, static_cast(__val), __len); } template