From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ej1-x635.google.com (mail-ej1-x635.google.com [IPv6:2a00:1450:4864:20::635]) by sourceware.org (Postfix) with ESMTPS id BF3E53858039 for ; Mon, 28 Jun 2021 08:06:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BF3E53858039 Received: by mail-ej1-x635.google.com with SMTP id ot9so27401380ejb.8 for ; Mon, 28 Jun 2021 01:06:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=I0X9+riyKdSZqD8EAxWrIBnBE0HhgJxBengoDaGRLMs=; b=gwLs0GUbtYw0vriHMen5bs771i2ygBpJy3L2OLD4UvjGpM9xuT2eK0JBnfBUNWlFEs I4D318Ho3JneIlhV3X+8gOFZ8JUMTjDaUoRkRZDFx8nrABxSMMukP/23O9jVsZDbqSoo ALQbCgOEbQLXR1QfsRP3FAs823V38KKN/DgiMIYr0l/+77/izUEK7/V5XXQiUVUE7fOD HA0g6fjF9Pk+wc3W2JeuynbjH73xZw95eIjVoYBUZGYQ4/ihnN/evyEMJv4opAIiaFGM +BAaqdiWw1nHDzjLknMRRmUrP/0dK7oyU/NUcdA0Jm7/Sgho8D8W3klDEcaU8TZCYC4E Qk7g== X-Gm-Message-State: AOAM53161wS3MwtyH+8h7xq39TaoA0C4BoKBIto71YJogF5jhi/OSmex fJS1Nmzhdo/U4AKo4Gt4bjFw+jC55o+250PXYNs= X-Google-Smtp-Source: ABdhPJyTSmKZoJJKMIMhV+cK9gnHNbNvhdWgCjC6ikl3L7YbeAY1zujcB5xEBpTyh9jXsy00CS2MlenLwMestZECB2Q= X-Received: by 2002:a17:906:240d:: with SMTP id z13mr22874979eja.118.1624867559712; Mon, 28 Jun 2021 01:05:59 -0700 (PDT) MIME-Version: 1.0 References: <91545a73-12af-33b2-c6e7-119b5a21de60@gmail.com> <4d503394-4e82-1d36-41ca-34315042775b@redhat.com> <49569f1d-9856-55c7-b9e9-578bbd7c7b7a@gmail.com> In-Reply-To: <49569f1d-9856-55c7-b9e9-578bbd7c7b7a@gmail.com> From: Richard Biener Date: Mon, 28 Jun 2021 10:05:48 +0200 Message-ID: Subject: Re: [PATCH] define auto_vec copy ctor and assignment (PR 90904) To: Martin Sebor Cc: Jason Merrill , gcc-patches , Jonathan Wakely Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, 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: , X-List-Received-Date: Mon, 28 Jun 2021 08:06:02 -0000 On Fri, Jun 25, 2021 at 10:52 PM Martin Sebor wrote: > > On 6/1/21 3:38 PM, Jason Merrill wrote: > > On 6/1/21 3:56 PM, Martin Sebor wrote: > >> On 5/27/21 2:53 PM, Jason Merrill wrote: > >>> On 4/27/21 11:52 AM, Martin Sebor via Gcc-patches wrote: > >>>> On 4/27/21 8:04 AM, Richard Biener wrote: > >>>>> On Tue, Apr 27, 2021 at 3:59 PM Martin Sebor wrote: > >>>>>> > >>>>>> On 4/27/21 1:58 AM, Richard Biener wrote: > >>>>>>> On Tue, Apr 27, 2021 at 2:46 AM Martin Sebor via Gcc-patches > >>>>>>> wrote: > >>>>>>>> > >>>>>>>> PR 90904 notes that auto_vec is unsafe to copy and assign because > >>>>>>>> the class manages its own memory but doesn't define (or delete) > >>>>>>>> either special function. Since I first ran into the problem, > >>>>>>>> auto_vec has grown a move ctor and move assignment from > >>>>>>>> a dynamically-allocated vec but still no copy ctor or copy > >>>>>>>> assignment operator. > >>>>>>>> > >>>>>>>> The attached patch adds the two special functions to auto_vec along > >>>>>>>> with a few simple tests. It makes auto_vec safe to use in > >>>>>>>> containers > >>>>>>>> that expect copyable and assignable element types and passes > >>>>>>>> bootstrap > >>>>>>>> and regression testing on x86_64-linux. > >>>>>>> > >>>>>>> The question is whether we want such uses to appear since those > >>>>>>> can be quite inefficient? Thus the option is to delete those > >>>>>>> operators? > >>>>>> > >>>>>> I would strongly prefer the generic vector class to have the > >>>>>> properties > >>>>>> expected of any other generic container: copyable and assignable. If > >>>>>> we also want another vector type with this restriction I suggest > >>>>>> to add > >>>>>> another "noncopyable" type and make that property explicit in its > >>>>>> name. > >>>>>> I can submit one in a followup patch if you think we need one. > >>>>> > >>>>> I'm not sure (and not strictly against the copy and assign). > >>>>> Looking around > >>>>> I see that vec<> does not do deep copying. Making auto_vec<> do it > >>>>> might be surprising (I added the move capability to match how vec<> > >>>>> is used - as "reference" to a vector) > >>>> > >>>> The vec base classes are special: they have no ctors at all (because > >>>> of their use in unions). That's something we might have to live with > >>>> but it's not a model to follow in ordinary containers. > >>> > >>> I don't think we have to live with it anymore, now that we're writing > >>> C++11. > >>> > >>>> The auto_vec class was introduced to fill the need for a conventional > >>>> sequence container with a ctor and dtor. The missing copy ctor and > >>>> assignment operators were an oversight, not a deliberate feature. > >>>> This change fixes that oversight. > >>>> > >>>> The revised patch also adds a copy ctor/assignment to the auto_vec > >>>> primary template (that's also missing it). In addition, it adds > >>>> a new class called auto_vec_ncopy that disables copying and > >>>> assignment as you prefer. > >>> > >>> Hmm, adding another class doesn't really help with the confusion > >>> richi mentions. And many uses of auto_vec will pass them as vec, > >>> which will still do a shallow copy. I think it's probably better to > >>> disable the copy special members for auto_vec until we fix vec<>. > >> > >> There are at least a couple of problems that get in the way of fixing > >> all of vec to act like a well-behaved C++ container: > >> > >> 1) The embedded vec has a trailing "flexible" array member with its > >> instances having different size. They're initialized by memset and > >> copied by memcpy. The class can't have copy ctors or assignments > >> but it should disable/delete them instead. > >> > >> 2) The heap-based vec is used throughout GCC with the assumption of > >> shallow copy semantics (not just as function arguments but also as > >> members of other such POD classes). This can be changed by providing > >> copy and move ctors and assignment operators for it, and also for > >> some of the classes in which it's a member and that are used with > >> the same assumption. > >> > >> 3) The heap-based vec::block_remove() assumes its elements are PODs. > >> That breaks in VEC_ORDERED_REMOVE_IF (used in gcc/dwarf2cfi.c:2862 > >> and tree-vect-patterns.c). > >> > >> I took a stab at both and while (1) is easy, (2) is shaping up to > >> be a big and tricky project. Tricky because it involves using > >> std::move in places where what's moved is subsequently still used. > >> I can keep plugging away at it but it won't change the fact that > >> the embedded and heap-based vecs have different requirements. > >> > >> It doesn't seem to me that having a safely copyable auto_vec needs > >> to be put on hold until the rats nest above is untangled. It won't > >> make anything worse than it is. (I have a project that depends on > >> a sane auto_vec working). > >> > >> A couple of alternatives to solving this are to use std::vector or > >> write an equivalent vector class just for GCC. > > > > It occurs to me that another way to work around the issue of passing an > > auto_vec by value as a vec, and thus doing a shallow copy, would be to > > add a vec ctor taking an auto_vec, and delete that. This would mean if > > you want to pass an auto_vec to a vec interface, it needs to be by > > reference. We might as well do the same for operator=, though that > > isn't as important. > > Thanks, that sounds like a good idea. Attached is an implementation > of this change. Since the auto_vec copy ctor and assignment have > been deleted by someone else in the interim, this patch doesn't > reverse that. I will propose it separately after these changes > are finalized. > > My approach was to 1) disable the auto_vec to vec conversion, > 2) introduce an auto_vec::to_vec() to make the conversion possible > explicitly, and 3) resolve compilation errors by either changing > APIs to take a vec by reference or callers to convert auto_vec to > vec explicitly by to_vec(). In (3) I tried to minimize churn while > improving the const-correctness of the APIs. > > When changing a vec API from by-value to by-reference I used vec & > and didn't try to use vec * even though the latter seems to be more > common (I avoided this to minimize the scope of changes). If we > want to do this I suggest handling that in a separate change. > > It's possible to get rid of vNULL in initialization. It can be > replaced with value-initialization in declarations but it's still > handy when passing "empty" AKA nil vecs as arguments. I didn't > do it except in one instance (vec::copy) as a POC. > > I only touched APIs that needed to be adjusted so there may still > be others that take vec by value that didn't change because they're > not passed an auto_vec. My hope is that we'll all pitch in and > convert those over time. > > I kept an eye on the indirection due to changing vec to vec & that > Richi's concerned about but I didn't look into the cost in any > depth. I don't have the sense that it's a significant issue. In > small static functions it shouldn't matter if they get inlined and > in bigger ones I'd expect the cost of the indirection to be > outweighed by other costs. If there are cases where it should > be felt to be significant I'll adjust those if you point thm out > to me. The cases I'm concerned about are when we for example load from the vec<> in a loop but we can't hoist the extra dereference because of aliasing issues (that is, the load of the m_vec member). It's enough for the loop to store to a char * to make TBAA inefficient here. At least the situation with TBAA for pointers isn't as bad as it was a few years ago (all pointers aliased - see get_alias_set and the large comment before POINTER_TYPE_P handling). Still void * aliases all pointers. >From a performance perspective a direct load-to-load dependence is always quite bad. Richard. > Tested on x86_64-linux. > > Martin