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.129.124]) by sourceware.org (Postfix) with ESMTPS id A2A7D3858D32 for ; Tue, 14 Nov 2023 22:29:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A2A7D3858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A2A7D3858D32 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700000968; cv=none; b=Bixv1SXbQQxGiqmn6sYEwpKlx7cPiUiemxpP5msrBEpVWCLiUy8W8iQ7OeDdmGfuJAXqnT0vtp2EAoT1pVN0nllT1HjOcyqDqbRdPLyDyf3xi8/CNf+WUNhdkyCXJ4DrKV8/sKkomlVwAyHQF+UhmlVdZbNUk9VRCNkCGvCj24Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700000968; c=relaxed/simple; bh=wwvDnWPFCdCzM3P3j2vbYEoMG8wqSHfZyAsr7WVmRNE=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=L7CUj9jyLu0EAzw1ifEQhjXfeAxN/XZhbsSFgMOsgpnlBD6BJKJvm8dUQQarF1JGZjkuSkIdEUaXCmgCrOyw1liF9VJV+7OXCZ1nhj+v+QaeilC1zdSEjuLsdkGxoX9XPrh/VlKPKvJntKDBC2fFiI4Pl9JOybtCYbpjB1jHNh8= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1700000966; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=x+o6OoOEWGvtbK5Y+Mc65uxiE6RR0lbwqQGhZQ7OKUA=; b=K5Sn+DesIHbG6PKwg++0tsQ6FJC7XMNvfLhlxeXxtej57I6z5S9s8of9fdTiTULbkAqMGy IForgvPtK6CXdOlrrhhhs3e7/vLiQ7IQmZAXribZBk/61UZZfNAcMK5H8vhOslyKqGISCp H8R451+2bhfr3zCqjuPeRNyOmuDZqyA= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-589-nc5vuRqrNjinPkkFMsSV6w-1; Tue, 14 Nov 2023 17:29:24 -0500 X-MC-Unique: nc5vuRqrNjinPkkFMsSV6w-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-66fbd02d104so79961946d6.1 for ; Tue, 14 Nov 2023 14:29:24 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700000964; x=1700605764; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=x+o6OoOEWGvtbK5Y+Mc65uxiE6RR0lbwqQGhZQ7OKUA=; b=n5n50KCfcH3qhAeD1HIbML9jzxvLktgtQUGj4Gf855DvJQJ2zpYVGp2ATuye8NFE02 vxYtIF8OCN34FW/S2uYlvy2d5sWxJE9CBm2+GJmRdXEFwuRzzaRcKfnuOIznlowlYNCs ivir6uOoDnX8GIa+NvQQNkcsdGO+ywORT9ecWG6qVeyk/r8mjGySwwjEY5pO+uKixQzL aKhT86bqv54/Q+ICT6ECX3EU/e5Y+Ztvu0aGGzGeog/ezRG9lKUPRN/r/7Jmq+fUMWHz cdE3wqyOtHCqvPwPB4MnfZaLOU2qdS33k9sHkV7Kne7BzRPnbwEykzDY0bNTbSIqmt/h UcYQ== X-Gm-Message-State: AOJu0YzqMt/1InbAna+SjUZNp7fvCIr3b3qHH88Ba3kWXOcss7oCUYVc awFyfDgcJAOc26QrzuAYrnF+Bola27upyEUMMK2C/XNhf1B4LxbUtY5LadiQUVHoG2+YB7U51FR avqizAlmL+KBx4Bk1GQ== X-Received: by 2002:ad4:53c6:0:b0:66d:9f68:219f with SMTP id k6-20020ad453c6000000b0066d9f68219fmr3704513qvv.3.1700000964237; Tue, 14 Nov 2023 14:29:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IHiwK3Gcjs5sV2q7Rmz0nu2+7UNOmyjDy8ANSC0afQWjvaikLfPtGHFYeGy8y593Y85tvhEoA== X-Received: by 2002:ad4:53c6:0:b0:66d:9f68:219f with SMTP id k6-20020ad453c6000000b0066d9f68219fmr3704506qvv.3.1700000963904; Tue, 14 Nov 2023 14:29:23 -0800 (PST) Received: from [192.168.1.108] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id x2-20020ad440c2000000b0065b260eafd9sm5584qvp.87.2023.11.14.14.29.22 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 14 Nov 2023 14:29:23 -0800 (PST) Message-ID: Date: Tue, 14 Nov 2023 17:29:22 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] c++: non-dependent .* folding [PR112427] To: Patrick Palka Cc: gcc-patches@gcc.gnu.org References: <20231108215904.2779753-1-ppalka@redhat.com> <375a6726-8998-4b6b-830f-32852ef116b1@redhat.com> <2fb218a2-713a-1a09-3c18-0f37ab359631@idea> <8fd05dbf-f08d-441f-90c0-10ac6fe5f7a8@redhat.com> From: Jason Merrill In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-11.6 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_H4,RCVD_IN_MSPIKE_WL,RCVD_IN_SORBS_WEB,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 11/14/23 10:43, Patrick Palka wrote: > On Fri, 10 Nov 2023, Jason Merrill wrote: > >> On 11/10/23 10:28, Patrick Palka wrote: >>> On Fri, 10 Nov 2023, Patrick Palka wrote: >>> >>>> On Thu, 9 Nov 2023, Jason Merrill wrote: >>>> >>>>> On 11/8/23 16:59, Patrick Palka wrote: >>>>>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK >>>>>> for >>>>>> trunk? >>>>>> >>>>>> -- >8 -- >>>>>> >>>>>> Here when building up the non-dependent .* expression, we crash from >>>>>> fold_convert on 'b.a' due to this (templated) COMPONENT_REF having an >>>>>> IDENTIFIER_NODE instead of FIELD_DECL operand that middle-end routines >>>>>> expect. Like in r14-4899-gd80a26cca02587, this patch fixes this by >>>>>> replacing the problematic piecemeal folding with a single call to >>>>>> cp_fully_fold. >>>>>> >>>>>> PR c++/112427 >>>>>> >>>>>> gcc/cp/ChangeLog: >>>>>> >>>>>> * typeck2.cc (build_m_component_ref): Use cp_convert, build2 >>>>>> and >>>>>> cp_fully_fold instead of fold_build_pointer_plus and >>>>>> fold_convert. >>>>> >>>>>> gcc/testsuite/ChangeLog: >>>>>> >>>>>> * g++.dg/template/non-dependent29.C: New test. >>>>>> --- >>>>>> gcc/cp/typeck2.cc | 5 ++++- >>>>>> gcc/testsuite/g++.dg/template/non-dependent29.C | 13 +++++++++++++ >>>>>> 2 files changed, 17 insertions(+), 1 deletion(-) >>>>>> create mode 100644 gcc/testsuite/g++.dg/template/non-dependent29.C >>>>>> >>>>>> diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc >>>>>> index 309903afed8..208004221da 100644 >>>>>> --- a/gcc/cp/typeck2.cc >>>>>> +++ b/gcc/cp/typeck2.cc >>>>>> @@ -2378,7 +2378,10 @@ build_m_component_ref (tree datum, tree >>>>>> component, >>>>>> tsubst_flags_t complain) >>>>>> /* Build an expression for "object + offset" where offset is >>>>>> the >>>>>> value stored in the pointer-to-data-member. */ >>>>>> ptype = build_pointer_type (type); >>>>>> - datum = fold_build_pointer_plus (fold_convert (ptype, datum), >>>>>> component); >>>>>> + datum = cp_convert (ptype, datum, complain); >>>>>> + datum = build2 (POINTER_PLUS_EXPR, ptype, >>>>>> + datum, convert_to_ptrofftype (component)); >>>>> >>>>> We shouldn't need to build the POINTER_PLUS_EXPR at all in template >>>>> context. >>>>> OK with that change. >>>> >>>> Hmm, that seems harmless at first glance, but I noticed >>>> build_min_non_dep (called from build_x_binary_op in this case) is >>>> careful to propagate TREE_SIDE_EFFECTS of the given tree, and so eliding >>>> POINTER_PLUS_EXPR here could potentially mean that the tree we >>>> ultimately return from build_x_binary_op when in a template context has >>>> TREE_SIDE_EFFECTS not set when it used to. Shall we still elide the >>>> POINTER_PLUS_EXPR in a template context despite this? >> >> True, we would need build_min_non_dep to also get TREE_SIDE_EFFECTS from the >> operands. That might be useful in general for similar situations? >> >> I also note that convert_to_ptrofftype uses fold_convert, so the new code >> could have the same problem if the pointer to member operand is also a >> COMPONENT_REF. > > Ah, makes sense... How does the following look then? I'm not sure if > we still want to replace fold_build_pointer_plus with build2 if we're > not going to use that code path in a template context? Bootstrapped and > regtested on x86_64-pc-linux-gnu. We probably do, since we now aren't folding the thing before it. OK. > -- >8 -- > - > Subject: [PATCH] c++: non-dependent .* operand folding [PR112427] > > Here when building up the non-dependent .* expression, we crash from > fold_convert on 'b.a' due to this (templated) COMPONENT_REF having an > IDENTIFIER_NODE instead of FIELD_DECL operand that middle-end routines > expect. Like in r14-4899-gd80a26cca02587, this patch fixes this by > replacing the problematic piecemeal folding with a single call to > cp_fully_fold. Additionally, don't bother building POINTER_PLUS_EXPR > in a template context. This means the returned non-dependent tree might > not have TREE_SIDE_EFFECTS set when it used to, so we need to make > build_min_non_dep compensate for this by propagating TREE_SIDE_EFFECTS > from the original arguments like buildN and build_min do, which seems > like a generally desirable thing to do anyway. > > PR c++/112427 > > gcc/cp/ChangeLog: > > * tree.cc (build_min_non_dep): Propagate TREE_SIDE_EFFECTS from > the original arguments. > (build_min_non_dep_call_vec): Likewise. > * typeck2.cc (build_m_component_ref): Use cp_convert, build2 and > cp_fully_fold instead of fold_build_pointer_plus and fold_convert. > > gcc/testsuite/ChangeLog: > > * g++.dg/template/non-dependent29.C: New test. > --- > gcc/cp/tree.cc | 11 ++++++++++- > gcc/cp/typeck2.cc | 6 +++++- > gcc/testsuite/g++.dg/template/non-dependent29.C | 13 +++++++++++++ > 3 files changed, 28 insertions(+), 2 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/template/non-dependent29.C > > diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc > index 417c92ba76f..dc4126f935c 100644 > --- a/gcc/cp/tree.cc > +++ b/gcc/cp/tree.cc > @@ -3601,7 +3601,12 @@ build_min_non_dep (enum tree_code code, tree non_dep, ...) > TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep); > > for (i = 0; i < length; i++) > - TREE_OPERAND (t, i) = va_arg (p, tree); > + { > + tree x = va_arg (p, tree); > + TREE_OPERAND (t, i) = x; > + if (x && !TYPE_P (x)) > + TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x); > + } > > va_end (p); > return convert_from_reference (t); > @@ -3636,6 +3641,10 @@ build_min_non_dep_call_vec (tree non_dep, tree fn, vec *argvec) > non_dep = TREE_OPERAND (non_dep, 0); > TREE_TYPE (t) = TREE_TYPE (non_dep); > TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep); > + if (argvec) > + for (tree x : *argvec) > + if (x && !TYPE_P (x)) > + TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x); > return convert_from_reference (t); > } > > diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc > index 309903afed8..a75f4f8c2d2 100644 > --- a/gcc/cp/typeck2.cc > +++ b/gcc/cp/typeck2.cc > @@ -2378,7 +2378,11 @@ build_m_component_ref (tree datum, tree component, tsubst_flags_t complain) > /* Build an expression for "object + offset" where offset is the > value stored in the pointer-to-data-member. */ > ptype = build_pointer_type (type); > - datum = fold_build_pointer_plus (fold_convert (ptype, datum), component); > + datum = cp_convert (ptype, datum, complain); > + if (!processing_template_decl) > + datum = build2 (POINTER_PLUS_EXPR, ptype, > + datum, convert_to_ptrofftype (component)); > + datum = cp_fully_fold (datum); > datum = cp_build_fold_indirect_ref (datum); > if (datum == error_mark_node) > return error_mark_node; > diff --git a/gcc/testsuite/g++.dg/template/non-dependent29.C b/gcc/testsuite/g++.dg/template/non-dependent29.C > new file mode 100644 > index 00000000000..41bd11ae6b4 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/template/non-dependent29.C > @@ -0,0 +1,13 @@ > +// PR c++/112427 > + > +struct A { int m; void f(); }; > +struct B { A a; }; > + > +template > +void f(B b) { > + int A::*pd = &A::m; > + b.a.*pd; > + > + void (A::*pf)() = &A::f; > + (b.a.*pf)(); > +}