From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id E735D3858D37 for ; Fri, 28 Apr 2023 19:14:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E735D3858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1682709271; 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: in-reply-to:in-reply-to:references:references; bh=NQ+RjMJ3wD27aYjDxL2qKLwFNT3Gq3n4/42hOjVROHA=; b=DEm2RWEkn2ZBeW2uFf/M/r75ScyMXFoKkAl1sHzG8nReTp3iXXCkWKuWeC/96NaC1VqlUx euyzOi9e8pObj9vYxjYTCVSv6FrjQ4etp6jTnUUiDMwxDbC91v1pcERbYAz+Crge2l/CK6 0tN2okIGW2IXV0cBCFbffW7DdmQC9fY= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-12-gnAND0eQPJ2wWGU9bXHSQQ-1; Fri, 28 Apr 2023 15:14:30 -0400 X-MC-Unique: gnAND0eQPJ2wWGU9bXHSQQ-1 Received: by mail-qv1-f72.google.com with SMTP id 6a1803df08f44-5efd8b1643bso4211316d6.3 for ; Fri, 28 Apr 2023 12:14:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1682709270; x=1685301270; h=mime-version:references:message-id:in-reply-to:subject:cc:to:date :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=NQ+RjMJ3wD27aYjDxL2qKLwFNT3Gq3n4/42hOjVROHA=; b=bZM5Mq60kax1XIQMhsa9ioYF48FZhSMbgA2ysS0AQ4sqjKplxcl/+lHnboWmmbRmDN 5/4IjowhMCGB/6l+aqxaNh2kauCRUcZvhcKYapcAjnStapl/G1mvZtBRLed1mz3UU21H N2/piyeOiWKtVaE0HXcgSQHNcKata87cDDJq/NFvHS6YpCbrgjZQuf0UmeioWKvyl0D9 fOoZMAGuqFiv6VzE6omf+NcJY107FTmAhVNEB1HSYVS73S+p5yA7Le+aD9AjqnO7REAI WqMmDBFTi/twWkcT2wpxHAsdERfujhZq0e6LJkpswogYRtHhcOIshOCuR0Anc00nsZDX /DjA== X-Gm-Message-State: AC+VfDyWWnANhJCfH15gVSISE99NbKVFJr2OFJkSZQcHxT1s/ANGfUAV 7/uomdLZ+eyTJsu8WIPEbY6IO3Imxw88NiiupqIQBE+otNVmCoCt212LTCefHJHE8lZAV+2zNeF S/wx7FiAzruKJVs1G6g== X-Received: by 2002:a05:6214:194a:b0:5e9:2bad:c913 with SMTP id q10-20020a056214194a00b005e92badc913mr11269202qvk.23.1682709269900; Fri, 28 Apr 2023 12:14:29 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ54OiEILDIgLUSsVgJ5WldxC3UBtHOblySlIfUvmmBhoZe8TMcV55Up8NTWOp6jT8AP03ORMQ== X-Received: by 2002:a05:6214:194a:b0:5e9:2bad:c913 with SMTP id q10-20020a056214194a00b005e92badc913mr11269165qvk.23.1682709269663; Fri, 28 Apr 2023 12:14:29 -0700 (PDT) Received: from [192.168.1.130] (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id d19-20020a0cf0d3000000b005ef5e68b4a0sm6623095qvl.90.2023.04.28.12.14.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Apr 2023 12:14:29 -0700 (PDT) From: Patrick Palka X-Google-Original-From: Patrick Palka Date: Fri, 28 Apr 2023 15:14:28 -0400 (EDT) To: Patrick Palka cc: gcc-patches@gcc.gnu.org, jason@redhat.com Subject: Re: [PATCH] c++: RESULT_DECL replacement in constexpr call result [PR105440] In-Reply-To: <20230428190508.4091082-1-ppalka@redhat.com> Message-ID: <8cb1d63e-2afb-df4a-21f2-20756e9630ba@idea> References: <20230428190508.4091082-1-ppalka@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=US-ASCII X-Spam-Status: No, score=-13.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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 Fri, 28 Apr 2023, Patrick Palka wrote: > After mechanically replacing RESULT_DECL within a constexpr call result > (for sake of RVO), we can in some cases simplify the call result > further. > > In the below testcase the result of get() during evaluation of a's > initializer is the self-referential CONSTRUCTOR: > > {._M_p=(char *) &._M_local_buf} > > which after replacing RESULT_DECL with ctx->object (aka *D.2603, where > the D.2603 temporary points to the current element of _M_elems under > construction) becomes: > > {._M_p=(char *) &D.2603->_M_local_buf} > > but what we really want is: > > {._M_p=(char *) &a._M_elems[0]._M_local_buf}. > > so that the value of _M_p is independent of the value of the mutable > D.2603 temporary. > > So to that end, it seems we should constexpr evaluate the result again > after RESULT_DECL replacement, which is what this patch implements. > > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for > trunk? > > PR libstdc++/105440 > > gcc/cp/ChangeLog: > > * constexpr.cc (cxx_eval_call_expression): If any RESULT_DECLs get > replaced in the call result, try further evaluating the result. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp2a/constexpr-dtor16.C: New test. > --- > gcc/cp/constexpr.cc | 12 +++++- > gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C | 39 +++++++++++++++++++ > 2 files changed, 49 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C > > diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc > index d1097764b10..22a1609e664 100644 > --- a/gcc/cp/constexpr.cc > +++ b/gcc/cp/constexpr.cc > @@ -3213,7 +3213,12 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, > && CLASS_TYPE_P (TREE_TYPE (res)) > && !is_empty_class (TREE_TYPE (res))) > if (replace_decl (&result, res, ctx->object)) > - cacheable = false; > + { > + cacheable = false; > + result = cxx_eval_constant_expression (ctx, result, lval, > + non_constant_p, > + overflow_p); > + } > } > else > /* Couldn't get a function copy to evaluate. */ > @@ -5988,9 +5993,12 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, > object = probe; > else > { > + tree orig_probe = probe; > probe = cxx_eval_constant_expression (ctx, probe, vc_glvalue, > non_constant_p, overflow_p); > evaluated = true; > + if (orig_probe == target) > + target = probe; Whoops, thanks to an accidental git commit --amend this patch contains an alternative approach that I considered: in cxx_eval_store_expression, ensure that we always set ctx->object to a fully reduced result (so &a._M_elems[0] instead of of *D.2603 in this case), which means later RESULT_DECL replacement with ctx->object should yield an already reduced result as well. But with this approach I ran into a bogus "modifying const object" error on cpp1y/constexpr-tracking-const23.C so I gave up on it :( Here's the correct patch: PR libstdc++/105440 gcc/cp/ChangeLog: * constexpr.cc (cxx_eval_call_expression): If any RESULT_DECLs get replaced in the call result, try further evaluating the result. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/constexpr-dtor16.C: New test. --- gcc/cp/constexpr.cc | 7 +++- gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C | 39 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index d1097764b10..9dbbf6eec03 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -3213,7 +3213,12 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, && CLASS_TYPE_P (TREE_TYPE (res)) && !is_empty_class (TREE_TYPE (res))) if (replace_decl (&result, res, ctx->object)) - cacheable = false; + { + cacheable = false; + result = cxx_eval_constant_expression (ctx, result, lval, + non_constant_p, + overflow_p); + } } else /* Couldn't get a function copy to evaluate. */ diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C new file mode 100644 index 00000000000..707a3e025b1 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C @@ -0,0 +1,39 @@ +// PR c++/105440 +// { dg-do compile { target c++20 } } + +struct basic_string { + char _M_local_buf[32]; + char* _M_p; + constexpr basic_string() : _M_p{_M_local_buf} { } + constexpr void f() { if (_M_p) { } } + constexpr ~basic_string() { if (_M_p) { } } +}; + +template +struct array { + basic_string _M_elems[N]; +}; + +constexpr basic_string get() { return {}; } + +constexpr bool f1() { + array<1> a{get()}; + a._M_elems[0].f(); + + return true; +} + +constexpr bool f2() { + array<2> a2{get(), get()}; + array<3> a3{get(), get(), get()}; + + for (basic_string& e : a2._M_elems) + e.f(); + for (basic_string& e : a3._M_elems) + e.f(); + + return true; +} + +static_assert(f1()); +static_assert(f2()); -- 2.40.1.445.gf85cd430b1 > if (*non_constant_p) > return t; > } > @@ -6154,7 +6162,7 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t, > if (!empty_base && !(same_type_ignoring_top_level_qualifiers_p > (initialized_type (init), type))) > { > - gcc_assert (is_empty_class (TREE_TYPE (target))); > + gcc_assert (is_empty_class (TREE_TYPE (TREE_OPERAND (t, 0)))); > empty_base = true; > } > > diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C > new file mode 100644 > index 00000000000..707a3e025b1 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-dtor16.C > @@ -0,0 +1,39 @@ > +// PR c++/105440 > +// { dg-do compile { target c++20 } } > + > +struct basic_string { > + char _M_local_buf[32]; > + char* _M_p; > + constexpr basic_string() : _M_p{_M_local_buf} { } > + constexpr void f() { if (_M_p) { } } > + constexpr ~basic_string() { if (_M_p) { } } > +}; > + > +template > +struct array { > + basic_string _M_elems[N]; > +}; > + > +constexpr basic_string get() { return {}; } > + > +constexpr bool f1() { > + array<1> a{get()}; > + a._M_elems[0].f(); > + > + return true; > +} > + > +constexpr bool f2() { > + array<2> a2{get(), get()}; > + array<3> a3{get(), get(), get()}; > + > + for (basic_string& e : a2._M_elems) > + e.f(); > + for (basic_string& e : a3._M_elems) > + e.f(); > + > + return true; > +} > + > +static_assert(f1()); > +static_assert(f2()); > -- > 2.40.1.445.gf85cd430b1 > >