From patchwork Tue Mar 3 21:46:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1248659 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=209.132.180.131; helo=sourceware.org; envelope-from=gcc-patches-return-520578-incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha1 header.s=default header.b=hVlBwHij; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=RkYmTpgR; dkim-atps=neutral Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 48X9ZJ3R5Yz9sP7 for ; Wed, 4 Mar 2020 08:46:30 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; q=dns; s=default; b=cRP/ADrd0xCq/T8t oumBJz0e/40PZQoGHYb4yVwff4O/t/vI1FaoWeLEKZvkY3WOwhVHCaTR6pg2t+Dt Pcp9RtNIEnQQcK8ZPJIa+1ZvE9IH+6xX2aP8u5jyPdHA3dAl4NY89C2wIiYdnYCR D7qFrI5EyBcgutPlPSSRHJ/lOtc= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :to:cc:subject:date:message-id:mime-version:content-type :content-transfer-encoding; s=default; bh=ELmhg2Qfx+uzgr/3gqVR2m rbfYY=; b=hVlBwHijIGX4+9jjEvhyPVU2GOXyud7ozigFTHmy/dgfm9xsCfGHVX D+lOc41RX27+hdhcsd99AcT0U6pHRkaGpyqwv54okJdn94vSGMq3L8or+peDWpzt sYBTBwsxONjW514V2UuLH/Nel+KOQ7aOK0Xf5Cyg2dCm64E7Ycr8w= Received: (qmail 49738 invoked by alias); 3 Mar 2020 21:46:22 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 49720 invoked by uid 89); 3 Mar 2020 21:46:22 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-25.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=deciding X-HELO: us-smtp-1.mimecast.com Received: from us-smtp-delivery-1.mimecast.com (HELO us-smtp-1.mimecast.com) (205.139.110.120) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 03 Mar 2020 21:46:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583271978; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=IgLq9qgvsJ5Nbvf34AXQR0hr5GHFhT10MemJDf2Du98=; b=RkYmTpgRujyAxnlgle2HMXL4KiQFDbmlMfSVlrOiYrvSwv/yQu9VkbLUJUAoLLLbhdNZ+f m1E8YCeVP05dPUgmO7wPi5YhmzRjDwaoMZMR3L464Zla/7fCYdf4GNp4uTJSyFzOSVJ4Xg K9NbX9woycuuWbtqIp9BGvt8etlXR4g= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-486-yaWbGkPkNEedJo9vHFLhwQ-1; Tue, 03 Mar 2020 16:46:13 -0500 Received: by mail-qv1-f69.google.com with SMTP id u5so3005331qvj.10 for ; Tue, 03 Mar 2020 13:46:12 -0800 (PST) Received: from localhost.localdomain (ool-457d493a.dyn.optonline.net. [69.125.73.58]) by smtp.gmail.com with ESMTPSA id p84sm7190919qke.134.2020.03.03.13.46.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2020 13:46:11 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org, Patrick Palka Subject: [PATCH] libstdc++: Fix incorrect use of memset in ranges::fill_n (PR 94017) Date: Tue, 3 Mar 2020 16:46:09 -0500 Message-Id: <20200303214609.2229459-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-IsSubscribed: yes When deciding whether to perform the memset optimization in ranges::fill_n, we were crucially neglecting to check whether the output pointer's value type is a byte type. This patch adds such a check to the problematic condition in ranges::fill_n. I think the __is_byte<_Tp>::__value check, which checks that the fill type is a byte type, is too restrictive. It means that we won't enable the memset optimization in the following example: char c[100]; ranges::fill(c, 37); since the fill type is deduced to be int here. It seems we could get away with instead just checking that _Tp is an integral type; I've added a TODO about this in the code. libstdc++-v3/ChangeLog: PR libstdc++/94017 * include/bits/ranges_algobase.h (__fill_n_fn::operator()): Fix condition for when to use memset. * testsuite/20_util/specialized_algorithms/uninitialized_fill/94017.cc: New test. * testsuite/20_util/specialized_algorithms/uninitialized_fill_n/94017.cc: New test. * testsuite/25_algorithms/fill/94017.cc: New test. * testsuite/25_algorithms/fill_n/94017.cc: New test. --- libstdc++-v3/include/bits/ranges_algobase.h | 9 ++- .../uninitialized_fill/94017.cc | 74 +++++++++++++++++++ .../uninitialized_fill_n/94017.cc | 74 +++++++++++++++++++ .../testsuite/25_algorithms/fill/94017.cc | 73 ++++++++++++++++++ .../testsuite/25_algorithms/fill_n/94017.cc | 73 ++++++++++++++++++ 5 files changed, 301 insertions(+), 2 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill/94017.cc create mode 100644 libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/94017.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/fill/94017.cc create mode 100644 libstdc++-v3/testsuite/25_algorithms/fill_n/94017.cc diff --git a/libstdc++-v3/include/bits/ranges_algobase.h b/libstdc++-v3/include/bits/ranges_algobase.h index feb6c5723dd..35309986e53 100644 --- a/libstdc++-v3/include/bits/ranges_algobase.h +++ b/libstdc++-v3/include/bits/ranges_algobase.h @@ -531,8 +531,13 @@ namespace ranges if (__n <= 0) return __first; - // TODO: is __is_byte the best condition? - if constexpr (is_pointer_v<_Out> && __is_byte<_Tp>::__value) + // TODO: Generalize this optimization to contiguous iterators. + if constexpr (is_pointer_v<_Out> + // Note that __is_byte already implies !is_volatile. + && __is_byte>::__value + // TODO: Can we relax this next condition to just + // integral<_Tp>? + && __is_byte<_Tp>::__value) { __builtin_memset(__first, static_cast(__value), __n); return __first + __n; diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill/94017.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill/94017.cc new file mode 100644 index 00000000000..c039935d78e --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill/94017.cc @@ -0,0 +1,74 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include +#include + +using __gnu_test::test_output_range; + +namespace ranges = std::ranges; + +template +void +test01() +{ + { + Out x[5]; + ranges::uninitialized_fill(x, value); + VERIFY( ranges::count(x, static_cast(value)) == ranges::size(x) ); + } + + { + Out x[5]; + test_output_range rx(x); + ranges::uninitialized_fill(x, value); + VERIFY( ranges::count(x, static_cast(value)) == ranges::size(x) ); + } +} + +int +main() +{ + test01(); + test01(); + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + test01(); +} diff --git a/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/94017.cc b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/94017.cc new file mode 100644 index 00000000000..e7928295a82 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/specialized_algorithms/uninitialized_fill_n/94017.cc @@ -0,0 +1,74 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include +#include + +using __gnu_test::test_output_range; + +namespace ranges = std::ranges; + +template +void +test01() +{ + { + Out x[5]; + ranges::uninitialized_fill_n(x, 5, value); + VERIFY( ranges::count(x, static_cast(value)) == ranges::size(x) ); + } + + { + Out x[5]; + test_output_range rx(x); + ranges::uninitialized_fill_n(x, 5, value); + VERIFY( ranges::count(x, static_cast(value)) == ranges::size(x) ); + } +} + +int +main() +{ + test01(); + test01(); + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + test01(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/fill/94017.cc b/libstdc++-v3/testsuite/25_algorithms/fill/94017.cc new file mode 100644 index 00000000000..d2d59f927e4 --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/fill/94017.cc @@ -0,0 +1,73 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +using __gnu_test::test_output_range; + +namespace ranges = std::ranges; + +template +void +test01() +{ + { + Out x[5]; + ranges::fill(x, value); + VERIFY( ranges::count(x, static_cast(value)) == ranges::size(x) ); + } + + { + Out x[5]; + test_output_range rx(x); + ranges::fill(x, value); + VERIFY( ranges::count(x, static_cast(value)) == ranges::size(x) ); + } +} + +int +main() +{ + test01(); + test01(); + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + test01(); +} diff --git a/libstdc++-v3/testsuite/25_algorithms/fill_n/94017.cc b/libstdc++-v3/testsuite/25_algorithms/fill_n/94017.cc new file mode 100644 index 00000000000..3cfaf39301a --- /dev/null +++ b/libstdc++-v3/testsuite/25_algorithms/fill_n/94017.cc @@ -0,0 +1,73 @@ +// Copyright (C) 2020 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do run { target c++2a } } + +#include +#include +#include + +using __gnu_test::test_output_range; + +namespace ranges = std::ranges; + +template +void +test01() +{ + { + Out x[5]; + ranges::fill_n(x, 5, value); + VERIFY( ranges::count(x, static_cast(value)) == ranges::size(x) ); + } + + { + Out x[5]; + test_output_range rx(x); + ranges::fill_n(x, 5, value); + VERIFY( ranges::count(x, static_cast(value)) == ranges::size(x) ); + } +} + +int +main() +{ + test01(); + test01(); + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + test01(); + + test01(); + test01(); + test01(); + test01(); +}