From patchwork Fri Feb 24 11:44:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 65583 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5A7B73857C45 for ; Fri, 24 Feb 2023 11:45:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A7B73857C45 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677239115; bh=g0S2cgYqg/N8p7mzV+4PSFKyk4h6qaQoI9EGGcd0ZYo=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=XViTf09Ve5qwieCgKAbfwPguQ3EYLGSnspaSykWITxLbL3sIELqX5eFMjVeqM4ab2 0JcKiaUdlv2s54vS8SiDnvR3tjP8k6cdolue9pSDuVmmWtLi3TuBtMYL1DpAmmElmI S7QFIR0BoRcGfjySjaSPBbqLZwZE32A/II7kPoh0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by sourceware.org (Postfix) with ESMTPS id 933B9385840F for ; Fri, 24 Feb 2023 11:44:46 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 933B9385840F Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 9BB2938B81; Fri, 24 Feb 2023 11:44:44 +0000 (UTC) Received: from imap2.suse-dmz.suse.de (imap2.suse-dmz.suse.de [192.168.254.74]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap2.suse-dmz.suse.de (Postfix) with ESMTPS id 7E2CE13246; Fri, 24 Feb 2023 11:44:44 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id Y73EHSyj+GMoCgAAMHmgww (envelope-from ); Fri, 24 Feb 2023 11:44:44 +0000 Date: Fri, 24 Feb 2023 12:44:44 +0100 (CET) To: gcc-patches@gcc.gnu.org cc: jwakely@redhat.com, Jakub Jelinek Subject: [PATCH 2/2] Avoid default-initializing auto_vec storage, fix vec MIME-Version: 1.0 Message-Id: <20230224114444.7E2CE13246@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham 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: Richard Biener via Gcc-patches From: Richard Biener Reply-To: Richard Biener Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" The following avoids default-initializing auto_vec storage for non-POD T since that's not what the allocated storage fallback will do and it's also not expected for existing cases like auto_vec, 64> elts; which exist to optimize the allocation. It also fixes the array accesses done by vec to not use its own m_vecdata member but instead access the container provided storage via pointer arithmetic. This seems to work but it also somehow breaks genrecog which now goes OOM with this change. I'm going to see if the testsuite shows anything, but maybe it's obvious from a second eye what I did wrong ... Comments welcome of course. Thanks, Richard. * vec.h (vec::m_vecdata): Remove. (vec::m_vecpfx): Align as T to avoid changing alignment of vec and simplifying address. (vec::address): Compute as this + 1. (vec::embedded_size): Use sizeof the vector instead of the offset of the m_vecdata member. (auto_vec): Turn m_data storage into uninitialized unsigned char aligned as T. * vec.cc (test_auto_alias): New. (vec_cc_tests): Call it. --- gcc/vec.cc | 17 +++++++++++++++++ gcc/vec.h | 11 +++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/gcc/vec.cc b/gcc/vec.cc index 511e6dff50d..2128f6666b1 100644 --- a/gcc/vec.cc +++ b/gcc/vec.cc @@ -568,6 +568,22 @@ test_auto_delete_vec () ASSERT_EQ (dtor_count, 2); } +/* Verify accesses to m_vecdata are done indirectly. */ + +static void +test_auto_alias () +{ + volatile int i = 1; + auto_vec v; + v.quick_grow (2); + v[0] = 1; + v[1] = 2; + int val; + for (int ix = i; v.iterate (ix, &val); ix++) + ASSERT_EQ (val, 2); + ASSERT_EQ (val, 0); +} + /* Run all of the selftests within this file. */ void @@ -587,6 +603,7 @@ vec_cc_tests () test_qsort (); test_reverse (); test_auto_delete_vec (); + test_auto_alias (); } } // namespace selftest diff --git a/gcc/vec.h b/gcc/vec.h index 5a2ee9c0294..b680efebe7a 100644 --- a/gcc/vec.h +++ b/gcc/vec.h @@ -586,8 +586,8 @@ public: unsigned allocated (void) const { return m_vecpfx.m_alloc; } unsigned length (void) const { return m_vecpfx.m_num; } bool is_empty (void) const { return m_vecpfx.m_num == 0; } - T *address (void) { return m_vecdata; } - const T *address (void) const { return m_vecdata; } + T *address (void) { return reinterpret_cast (this + 1); } + const T *address (void) const { return reinterpret_cast (this + 1); } T *begin () { return address (); } const T *begin () const { return address (); } T *end () { return address () + length (); } @@ -631,8 +631,7 @@ public: /* FIXME - These fields should be private, but we need to cater to compilers that have stricter notions of PODness for types. */ - vec_prefix m_vecpfx; - T m_vecdata[1]; + alignas (T) vec_prefix m_vecpfx; }; @@ -1313,7 +1312,7 @@ vec::embedded_size (unsigned alloc) vec, vec_embedded>::type vec_stdlayout; static_assert (sizeof (vec_stdlayout) == sizeof (vec), ""); static_assert (alignof (vec_stdlayout) == alignof (vec), ""); - return offsetof (vec_stdlayout, m_vecdata) + alloc * sizeof (T); + return sizeof (vec_stdlayout) + alloc * sizeof (T); } @@ -1588,7 +1587,7 @@ public: private: vec m_auto; - T m_data[MAX (N - 1, 1)]; + alignas(T) unsigned char m_data[sizeof (T) * N]; }; /* auto_vec is a sub class of vec whose storage is released when it is