From patchwork Fri Feb 24 13:46:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 65588 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 DB301385B53C for ; Fri, 24 Feb 2023 13:46:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB301385B53C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677246412; bh=y3htTL6UuaeX8htc/hUUA+y3eEm+A5WMQgCXx/gdHd0=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=hU5SHBRfTilGO90acazU+JXQV5vrUgaAItDTXjHzVECmFcZ1jG81RL9TDAerbRiYZ 61zzUvxdNyLJCikBaeTkh21AVD107vxqntqwoJW9F59OJ4VAYDR/yj7Rntc4cSXplr RFQWLxwtiY047eQ/snr/m3Z5jWhaMhsxhqW71LVQ= 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 [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id 30C8A385840F for ; Fri, 24 Feb 2023 13:46:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 30C8A385840F 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 1FB2D34D8A; Fri, 24 Feb 2023 13:46:22 +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 09FCF13246; Fri, 24 Feb 2023 13:46:22 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id RwVeAa6/+GPDUQAAMHmgww (envelope-from ); Fri, 24 Feb 2023 13:46:22 +0000 Date: Fri, 24 Feb 2023 14:46:21 +0100 (CET) To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek Subject: [PATCH 1/2] Change vec<, , vl_embed>::m_vecdata refrences into address () MIME-Version: 1.0 Message-Id: <20230224134622.09FCF13246@imap2.suse-dmz.suse.de> X-Spam-Status: No, score=-11.8 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" As preparation to remove m_vecdata in the vl_embed vector this changes references to it into calls to address (). As I was here it also fixes ::contains to avoid repeated bounds checking and the same issue in ::lower_bound which also suffers from unnecessary copying around values. * vec.h: Change m_vecdata references to address (). * vec.h (vec::lower_bound): Adjust to take a const reference to the object, use address to access data. (vec::contains): Use address to access data. (vec::operator[]): Use address instead of m_vecdata to access data. (vec::iterate): Likewise. (vec::copy): Likewise. (vec::quick_push): Likewise. (vec::pop): Likewise. (vec::quick_insert): Likewise. (vec::ordered_remove): Likewise. (vec::unordered_remove): Likewise. (vec::block_remove): Likewise. (vec::address): Likewise. --- gcc/vec.h | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/gcc/vec.h b/gcc/vec.h index a536b68732d..2b36f065234 100644 --- a/gcc/vec.h +++ b/gcc/vec.h @@ -611,10 +611,10 @@ public: void qsort (int (*) (const void *, const void *)); void sort (int (*) (const void *, const void *, void *), void *); void stablesort (int (*) (const void *, const void *, void *), void *); - T *bsearch (const void *key, int (*compar)(const void *, const void *)); + T *bsearch (const void *key, int (*compar) (const void *, const void *)); T *bsearch (const void *key, int (*compar)(const void *, const void *, void *), void *); - unsigned lower_bound (T, bool (*)(const T &, const T &)) const; + unsigned lower_bound (const T &, bool (*) (const T &, const T &)) const; bool contains (const T &search) const; static size_t embedded_size (unsigned); void embedded_init (unsigned, unsigned = 0, unsigned = 0); @@ -879,7 +879,7 @@ inline const T & vec::operator[] (unsigned ix) const { gcc_checking_assert (ix < m_vecpfx.m_num); - return m_vecdata[ix]; + return address ()[ix]; } template @@ -887,7 +887,7 @@ inline T & vec::operator[] (unsigned ix) { gcc_checking_assert (ix < m_vecpfx.m_num); - return m_vecdata[ix]; + return address ()[ix]; } @@ -929,7 +929,7 @@ vec::iterate (unsigned ix, T *ptr) const { if (ix < m_vecpfx.m_num) { - *ptr = m_vecdata[ix]; + *ptr = address ()[ix]; return true; } else @@ -955,7 +955,7 @@ vec::iterate (unsigned ix, T **ptr) const { if (ix < m_vecpfx.m_num) { - *ptr = CONST_CAST (T *, &m_vecdata[ix]); + *ptr = CONST_CAST (T *, &address ()[ix]); return true; } else @@ -978,7 +978,7 @@ vec::copy (ALONE_MEM_STAT_DECL) const { vec_alloc (new_vec, len PASS_MEM_STAT); new_vec->embedded_init (len, len); - vec_copy_construct (new_vec->address (), m_vecdata, len); + vec_copy_construct (new_vec->address (), address (), len); } return new_vec; } @@ -1018,7 +1018,7 @@ inline T * vec::quick_push (const T &obj) { gcc_checking_assert (space (1)); - T *slot = &m_vecdata[m_vecpfx.m_num++]; + T *slot = &address ()[m_vecpfx.m_num++]; *slot = obj; return slot; } @@ -1031,7 +1031,7 @@ inline T & vec::pop (void) { gcc_checking_assert (length () > 0); - return m_vecdata[--m_vecpfx.m_num]; + return address ()[--m_vecpfx.m_num]; } @@ -1056,7 +1056,7 @@ vec::quick_insert (unsigned ix, const T &obj) { gcc_checking_assert (length () < allocated ()); gcc_checking_assert (ix <= length ()); - T *slot = &m_vecdata[ix]; + T *slot = &address ()[ix]; memmove (slot + 1, slot, (m_vecpfx.m_num++ - ix) * sizeof (T)); *slot = obj; } @@ -1071,7 +1071,7 @@ inline void vec::ordered_remove (unsigned ix) { gcc_checking_assert (ix < length ()); - T *slot = &m_vecdata[ix]; + T *slot = &address ()[ix]; memmove (slot, slot + 1, (--m_vecpfx.m_num - ix) * sizeof (T)); } @@ -1118,7 +1118,8 @@ inline void vec::unordered_remove (unsigned ix) { gcc_checking_assert (ix < length ()); - m_vecdata[ix] = m_vecdata[--m_vecpfx.m_num]; + T *p = address (); + p[ix] = p[--m_vecpfx.m_num]; } @@ -1130,7 +1131,7 @@ inline void vec::block_remove (unsigned ix, unsigned len) { gcc_checking_assert (ix + len <= length ()); - T *slot = &m_vecdata[ix]; + T *slot = &address ()[ix]; m_vecpfx.m_num -= len; memmove (slot, slot + len, (m_vecpfx.m_num - ix) * sizeof (T)); } @@ -1248,9 +1249,13 @@ inline bool vec::contains (const T &search) const { unsigned int len = length (); + const T *p = address (); for (unsigned int i = 0; i < len; i++) - if ((*this)[i] == search) - return true; + { + const T *slot = &p[i]; + if (*slot == search) + return true; + } return false; } @@ -1262,7 +1267,8 @@ vec::contains (const T &search) const template unsigned -vec::lower_bound (T obj, bool (*lessthan)(const T &, const T &)) +vec::lower_bound (const T &obj, + bool (*lessthan)(const T &, const T &)) const { unsigned int len = length (); @@ -1273,7 +1279,7 @@ vec::lower_bound (T obj, bool (*lessthan)(const T &, const T &)) half = len / 2; middle = first; middle += half; - T middle_elem = (*this)[middle]; + const T &middle_elem = address ()[middle]; if (lessthan (middle_elem, obj)) { first = middle; @@ -1476,10 +1482,10 @@ public: { return m_vec ? m_vec->length () : 0; } T *address (void) - { return m_vec ? m_vec->m_vecdata : NULL; } + { return m_vec ? m_vec->address () : NULL; } const T *address (void) const - { return m_vec ? m_vec->m_vecdata : NULL; } + { return m_vec ? m_vec->address () : NULL; } T *begin () { return address (); } const T *begin () const { return address (); } From patchwork Fri Feb 24 13:47:39 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Richard Biener X-Patchwork-Id: 65589 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 CB26D38515FD for ; Fri, 24 Feb 2023 13:48:10 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CB26D38515FD DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1677246490; bh=dIRQVFZzDAga1yQIpBue/XQuCGp/txdEnHxPbIzaaFA=; h=Date:To:cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=aUSVSJFaSh6HiqTjaptQYLXuXh1W0m4bVzChtOVgd42NTMJDCYKgajrYvjKsftGMx CkTV0rbzFeAFYVWEpyeDbqom97P16yDrnC5VyMl6oIFikLE3dVDENgsu+rKclAEDbE yjl1VSJBcduEBOgRM77C49rI206wjAL3SoCNS8V8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by sourceware.org (Postfix) with ESMTPS id B7E12385482C for ; Fri, 24 Feb 2023 13:47:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B7E12385482C 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-out2.suse.de (Postfix) with ESMTPS id ECC995D844; Fri, 24 Feb 2023 13:47:39 +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 D386F13246; Fri, 24 Feb 2023 13:47:39 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap2.suse-dmz.suse.de with ESMTPSA id SAdsMvu/+GOLUgAAMHmgww (envelope-from ); Fri, 24 Feb 2023 13:47:39 +0000 Date: Fri, 24 Feb 2023 14:47:39 +0100 (CET) To: gcc-patches@gcc.gnu.org cc: Jakub Jelinek Subject: [PATCH 2/2] Avoid default-initializing auto_vec storage, fix vec MIME-Version: 1.0 Message-Id: <20230224134739.D386F13246@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. I've built the series with GCC 4.8 and clang 13 up to the stage1 target libs, a bootstrap and regtest on x86_64-unknown-linux-gnu with GCC 12 was successful with the diagnostic pragma, I'm currently re-bootstrapping and testing with a GCC 7 host compiler. OK if that succeeds? 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::m_data): Turn storage into uninitialized unsigned char. (auto_vec::auto_vec): Allow allocation of one stack member. Initialize m_vec in a special way to avoid later stringop overflow diagnostics. * vec.cc (test_auto_alias): New. (vec_cc_tests): Call it. --- gcc/vec.cc | 17 +++++++++++++++++ gcc/vec.h | 27 +++++++++++++++++---------- 2 files changed, 34 insertions(+), 10 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 2b36f065234..3b03bfe076a 100644 --- a/gcc/vec.h +++ b/gcc/vec.h @@ -586,8 +586,9 @@ 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 (); } @@ -629,10 +630,10 @@ public: friend struct va_gc_atomic; friend struct va_heap; - /* FIXME - These fields should be private, but we need to cater to + /* FIXME - This field 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]; + /* Align m_vecpfx to simplify address (). */ + alignas (T) alignas (vec_prefix) vec_prefix m_vecpfx; }; @@ -1315,7 +1316,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); } @@ -1559,8 +1560,14 @@ class auto_vec : public vec public: auto_vec () { - m_auto.embedded_init (MAX (N, 2), 0, 1); - this->m_vec = &m_auto; + m_auto.embedded_init (N, 0, 1); + /* ??? Instead of initializing m_vec from &m_auto directly use an + expression that avoids refering to a specific member of 'this' + to derail the -Wstringop-overflow diagnostic code, avoiding + the impression that data accesses are supposed to be to the + m_auto memmber storage. */ + size_t off = (char *) &m_auto - (char *) this; + this->m_vec = (vec *) ((char *) this + off); } auto_vec (size_t s CXX_MEM_STAT_INFO) @@ -1571,7 +1578,7 @@ public: return; } - m_auto.embedded_init (MAX (N, 2), 0, 1); + m_auto.embedded_init (N, 0, 1); this->m_vec = &m_auto; } @@ -1590,7 +1597,7 @@ public: private: vec m_auto; - T m_data[MAX (N - 1, 1)]; + unsigned char m_data[sizeof (T) * N]; }; /* auto_vec is a sub class of vec whose storage is released when it is