From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.220.28]) by sourceware.org (Postfix) with ESMTPS id D83103858D32 for ; Mon, 23 Jan 2023 12:38:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D83103858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id A816334210; Mon, 23 Jan 2023 12:37:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1674477479; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=r40gHOegMbC7dy1/I8SheRDwsBDAl0+rGshQ8iURXG8=; b=XX8V/EqMFpzk0IvK39yDBH8lkcFJ2rBZX+k+KAJhpWU/U7xBTKtceE5oFRLyH24t3vfVze W1Ia4k9U61sElQVZXRnysKIVWKjnve6i2Ylm39BROugtIxgYIEq91jSQroS5BdyHLIbPlL 8D/5Y+JEjrttP7DjwDWq0mbauWL3p+A= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1674477479; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=r40gHOegMbC7dy1/I8SheRDwsBDAl0+rGshQ8iURXG8=; b=+Yb4uEVDxp1OZCAg62WnsHRciGaPkbY/kySwSe0rf/4K1RrjcNTNaIBlUt2uQPAJ+lAiQ1 AjgtvNt0WXn5q0BQ== Received: from wotan.suse.de (wotan.suse.de [10.160.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id A0FC62C141; Mon, 23 Jan 2023 12:37:59 +0000 (UTC) Date: Mon, 23 Jan 2023 12:37:59 +0000 (UTC) From: Richard Biener To: Jakub Jelinek cc: Jason Merrill , Jan Hubicka , gcc-patches@gcc.gnu.org Subject: Re: [PATCH] c++, cgraphbuild: Handle DECL_VALUE_EXPRs in record_reference [PR108474] In-Reply-To: Message-ID: References: <799bfe17-b60d-7353-d676-5296e64b3769@redhat.com> <6f8e0faf-55eb-cf20-e796-a1828197bb23@redhat.com> User-Agent: Alpine 2.22 (LSU 394 2020-01-19) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-5.1 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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 List-Id: On Mon, 23 Jan 2023, Jakub Jelinek wrote: > On Mon, Jan 23, 2023 at 09:25:50AM +0100, Jakub Jelinek via Gcc-patches wrote: > > On Sun, Jan 22, 2023 at 07:19:07PM -0500, Jason Merrill wrote: > > > > I was just trying to be careful, because unfortunately this spot > > > > doesn't mean it really is only expanded in static var DECL_INITIAL, > > > > it can make it into dynamic initializers, and most of DECL_VALUE_EXPRs > > > > appear only in runtime code, otherwise we'd have much more of these issues. > > > > > > But it shouldn't be harmful anywhere, right? > > > > > > > But if you think it is ok, I'll test tonight a version just with > > > > if (!data->genericize && DECL_HAS_VALUE_EXPR_P (stmt) > > > > > > OK with that change. > > > > > > Though, actually, why not instead fix expand_expr_real_1 (and staticp) to > > > look through DECL_VALUE_EXPR? > > > > I'm afraid that is way too late. GIMPLE optimizations don't try to > > regimplify expressions they take from DECL_INITIAL of static vars, they > > just unshare them and use them. So, if this is to be done in the generic > > code, it would need to be done by cgraph around the time when it gimplifies > > function if there is any such point for variables. > > I believe the right spot is record_reference, which is called through > walk_tree from record_references_in_initializer which is called from > varpool_node::analyze. > > The new tests as well as g++.dg/init/pr53932.C pass with it, will do full > bootstrap/regtest soon. > > What do you think about this? Guess it should work. Do we (accidentially?) do anything special to static var initializers in nested functions? Can you think of any other C/C++ construct that would have DECL_VALUE_EXPR in them? Richard. > 2023-01-23 Jakub Jelinek > > PR c++/108474 > * cgraphbuild.cc: Include gimplify.h. > (record_reference): Replace VAR_DECLs with DECL_HAS_VALUE_EXPR_P with > their corresponding DECL_VALUE_EXPR expressions after unsharing. > > * cp-gimplify.cc (cp_fold_r): Revert 2023-01-19 changes. > > * g++.dg/cpp1z/decomp57.C: New test. > * g++.dg/cpp1z/decomp58.C: New test. > > --- gcc/cgraphbuild.cc.jj 2023-01-02 09:32:34.889104620 +0100 > +++ gcc/cgraphbuild.cc 2023-01-23 12:10:35.042067571 +0100 > @@ -31,6 +31,7 @@ along with GCC; see the file COPYING3. > #include "gimple-walk.h" > #include "ipa-utils.h" > #include "except.h" > +#include "gimplify.h" > > /* Context of record_reference. */ > struct record_reference_ctx > @@ -79,6 +80,17 @@ record_reference (tree *tp, int *walk_su > > if (VAR_P (decl)) > { > + /* Replace vars with their DECL_VALUE_EXPR if any. > + This is normally done during gimplification, but > + static var initializers are never gimplified. */ > + if (DECL_HAS_VALUE_EXPR_P (decl)) > + { > + tree *p; > + for (p = tp; *p != decl; p = &TREE_OPERAND (*p, 0)) > + ; > + *p = unshare_expr (DECL_VALUE_EXPR (decl)); > + return record_reference (tp, walk_subtrees, data); > + } > varpool_node *vnode = varpool_node::get_create (decl); > ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR); > } > --- gcc/cp/cp-gimplify.cc.jj 2023-01-23 11:47:49.889875804 +0100 > +++ gcc/cp/cp-gimplify.cc 2023-01-23 11:49:01.227841759 +0100 > @@ -1010,16 +1010,6 @@ cp_fold_r (tree *stmt_p, int *walk_subtr > } > break; > > - case VAR_DECL: > - /* In initializers replace anon union artificial VAR_DECLs > - with their DECL_VALUE_EXPRs, as nothing will do it later. */ > - if (DECL_ANON_UNION_VAR_P (stmt) && !data->genericize) > - { > - *stmt_p = stmt = unshare_expr (DECL_VALUE_EXPR (stmt)); > - break; > - } > - break; > - > default: > break; > } > --- gcc/testsuite/g++.dg/cpp1z/decomp57.C.jj 2023-01-23 11:48:36.367202107 +0100 > +++ gcc/testsuite/g++.dg/cpp1z/decomp57.C 2023-01-23 11:48:36.367202107 +0100 > @@ -0,0 +1,27 @@ > +// PR c++/108474 > +// { dg-do link { target c++17 } } > + > +struct T { int i, j; }; > +T h; > +auto [i, j] = h; > +int &r = i; > +int s = i; > +int *t = &i; > + > +void > +foo (int **p, int *q) > +{ > + static int &u = i; > + static int v = i; > + static int *w = &i; > + int &x = i; > + int y = i; > + int *z = &i; > + *p = &i; > + *q = i; > +} > + > +int > +main () > +{ > +} > --- gcc/testsuite/g++.dg/cpp1z/decomp58.C.jj 2023-01-23 11:48:36.367202107 +0100 > +++ gcc/testsuite/g++.dg/cpp1z/decomp58.C 2023-01-23 11:48:36.367202107 +0100 > @@ -0,0 +1,39 @@ > +// PR c++/108474 > +// { dg-do link { target c++17 } } > + > +namespace std { > + template struct tuple_size; > + template struct tuple_element; > +} > + > +struct A { > + int i; > + template int& get() { return i; } > +}; > + > +template <> struct std::tuple_size { static const int value = 2; }; > +template struct std::tuple_element { using type = int; }; > + > +struct A a; > +auto [i, j] = a; > +int &r = i; > +int s = i; > +int *t = &i; > + > +void > +foo (int **p, int *q) > +{ > + static int &u = i; > + static int v = i; > + static int *w = &i; > + int &x = i; > + int y = i; > + int *z = &i; > + *p = &i; > + *q = i; > +} > + > +int > +main () > +{ > +} > > > Jakub > > -- Richard Biener SUSE Software Solutions Germany GmbH, Frankenstrasse 146, 90461 Nuernberg, Germany; GF: Ivo Totev, Andrew Myers, Andrew McDonald, Boudien Moerman; HRB 36809 (AG Nuernberg)