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 7D8E63858D28 for ; Wed, 7 Sep 2022 19:55:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7D8E63858D28 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=1662580519; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=3rbf7UBcEK3K+QX5WkhDJ3nkZXwAiXtBR6mYWjIKJFo=; b=hptFbgEl+n3KT5xEVLw5G+dM7m95MY9wRZL5HS4x1Wzw+bB62tLS4tsFCnpiaG+3R6b5XU MadlAVB78YVmudXKW0QLXG4PQi4rFQMQDu+lZmC0NOlm1ueMIhq1z8BR3z9FRVH+rVTt6z wHt77B/8yrSuk9cTRbzd0v9qVwzZvGo= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-640-Fady1VgXM_qEiBJND7PlRQ-1; Wed, 07 Sep 2022 15:55:18 -0400 X-MC-Unique: Fady1VgXM_qEiBJND7PlRQ-1 Received: by mail-qt1-f197.google.com with SMTP id fy12-20020a05622a5a0c00b00344569022f7so12701387qtb.17 for ; Wed, 07 Sep 2022 12:55:18 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date; bh=3rbf7UBcEK3K+QX5WkhDJ3nkZXwAiXtBR6mYWjIKJFo=; b=nJ3PlRAUcrgRX8BuGtzcanfeHiQS0ejO0Ssjn99ZZ8fQDbayrLwal+AhT6dtVogyLe PHEum5Y2d3Q5gke0rBFnY/XalB9sEYzY+bN0fHsc3uFIYl8+/0K4ZKWerx2mjcNouApE KXHFkLWksR8FoTuiF9HeB0oLB4mAp2w64zONumoXFLChZWCjGnk9mV1ddWlvqOrycaL6 yzWoV9+mMwZ0nOn2jlPMM29uUBGH4Sfm8m32LLlO2h+5hnIcMKtnVMUISR7qwSXcfYxx /Gnef8M5IHnWbNoEX0g5aT1dmjvytHmFwzVV0e9pfK/gE1TlOrg1K0kVsFVXQkfXb91y ktBg== X-Gm-Message-State: ACgBeo2ocnaEryKGXpJ3PdHe7JsOiqWo4Drd4klRIIoq4uXvVE0AGFqI XDAfkrjIhFHN/9gfTiwipm0RcmusqotRQA8WIll4ahdXc4ssiaaMhku4sBAeCbqlYLWYcvnIg7W g+Gr1L0hs0DjTiHZlqQ== X-Received: by 2002:a0c:f5c4:0:b0:4a7:f6cb:9e39 with SMTP id q4-20020a0cf5c4000000b004a7f6cb9e39mr4825758qvm.9.1662580517558; Wed, 07 Sep 2022 12:55:17 -0700 (PDT) X-Google-Smtp-Source: AA6agR6M6lSRKy4uO5ok6O8n0QvT3YkmqWwXTTTNLdkLqAqJXVTo2sc/j8vO+9PLPCbCRlwRdIrTKQ== X-Received: by 2002:a0c:f5c4:0:b0:4a7:f6cb:9e39 with SMTP id q4-20020a0cf5c4000000b004a7f6cb9e39mr4825736qvm.9.1662580517189; Wed, 07 Sep 2022 12:55:17 -0700 (PDT) Received: from [192.168.1.101] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id w8-20020a05620a424800b006be8713f742sm15534255qko.38.2022.09.07.12.55.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 07 Sep 2022 12:55:16 -0700 (PDT) Message-ID: <38d432fb-f9db-6f0f-1587-1b8c0f5c75e7@redhat.com> Date: Wed, 7 Sep 2022 15:55:15 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.13.0 Subject: Re: [PATCH] c++: unnecessary instantiation of constexpr var [PR99130] To: Patrick Palka , gcc-patches@gcc.gnu.org References: <20220907194100.879066-1-ppalka@redhat.com> From: Jason Merrill In-Reply-To: <20220907194100.879066-1-ppalka@redhat.com> 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=-15.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_LOW,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 9/7/22 15:41, Patrick Palka wrote: > Here the use of the constexpr member/variable specialization 'value' > from within an unevaluated context causes us to overeagerly instantiate > it, via maybe_instantiate_decl called from mark_used, despite only its > declaration not its definition being needed. If the issue is with unevaluated context, maybe maybe_instantiate_decl should guard the call to decl_maybe_constant_var_p with !cp_unevaluated_operand? > We used to have the same issue for constexpr function specializations > until r6-1309-g81371eff9bc7ef made us delay their instantiation until > necessary during constexpr evaluation. > > So this patch makes us avoid unnecessarily instantiating constexpr > variable template specializations from mark_used as well. To that end > this patch pulls out the test in maybe_instantiate_decl > > (decl_maybe_constant_var_p (decl) > || (TREE_CODE (decl) == FUNCTION_DECL > && DECL_OMP_DECLARE_REDUCTION_P (decl)) > || undeduced_auto_decl (decl)) > > into each of its three callers (including mark_used) and refines the > test appropriately. The net result is that only mark_used is changed, > because the other two callers, resolve_address_of_overloaded_function > and decl_constant_var_p, already guard the call appropriately. And > presumably decl_constant_var_p will take care of instantiation when > needed for e.g. constexpr evaluation. > > Bootstrapped and regteste on x86_64-pc-linux-gnu, does this look OK for > trunk? > > PR c++/99130 > > gcc/cp/ChangeLog: > > * decl2.cc (maybe_instantiate_decl): Adjust function comment. > Check VAR_OR_FUNCTION_DECL_P. Pull out the disjunction into ... > (mark_used): ... here, removing the decl_maybe_constant_var_p > part of it. > > gcc/testsuite/ChangeLog: > > * g++.dg/cpp1y/var-templ70.C: New test. > --- > gcc/cp/decl2.cc | 33 ++++++++---------------- > gcc/testsuite/g++.dg/cpp1y/var-templ70.C | 19 ++++++++++++++ > 2 files changed, 30 insertions(+), 22 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ70.C > > diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc > index 89ab2545d64..cd188813bee 100644 > --- a/gcc/cp/decl2.cc > +++ b/gcc/cp/decl2.cc > @@ -5381,24 +5381,15 @@ possibly_inlined_p (tree decl) > return true; > } > > -/* Normally, we can wait until instantiation-time to synthesize DECL. > - However, if DECL is a static data member initialized with a constant > - or a constexpr function, we need it right now because a reference to > - such a data member or a call to such function is not value-dependent. > - For a function that uses auto in the return type, we need to instantiate > - it to find out its type. For OpenMP user defined reductions, we need > - them instantiated for reduction clauses which inline them by hand > - directly. */ > +/* If DECL is a function or variable template specialization, instantiate > + its definition now. */ > > void > maybe_instantiate_decl (tree decl) > { > - if (DECL_LANG_SPECIFIC (decl) > + if (VAR_OR_FUNCTION_DECL_P (decl) > + && DECL_LANG_SPECIFIC (decl) > && DECL_TEMPLATE_INFO (decl) > - && (decl_maybe_constant_var_p (decl) > - || (TREE_CODE (decl) == FUNCTION_DECL > - && DECL_OMP_DECLARE_REDUCTION_P (decl)) > - || undeduced_auto_decl (decl)) > && !DECL_DECLARED_CONCEPT_P (decl) > && !uses_template_parms (DECL_TI_ARGS (decl))) > { > @@ -5700,15 +5691,13 @@ mark_used (tree decl, tsubst_flags_t complain) > return false; > } > > - /* Normally, we can wait until instantiation-time to synthesize DECL. > - However, if DECL is a static data member initialized with a constant > - or a constexpr function, we need it right now because a reference to > - such a data member or a call to such function is not value-dependent. > - For a function that uses auto in the return type, we need to instantiate > - it to find out its type. For OpenMP user defined reductions, we need > - them instantiated for reduction clauses which inline them by hand > - directly. */ > - maybe_instantiate_decl (decl); > + /* If DECL has a deduced return type, we need to instantiate it now to > + find out its type. For OpenMP user defined reductions, we need them > + instantiated for reduction clauses which inline them by hand directly. */ > + if (undeduced_auto_decl (decl) > + || (TREE_CODE (decl) == FUNCTION_DECL > + && DECL_OMP_DECLARE_REDUCTION_P (decl))) > + maybe_instantiate_decl (decl); > > if (processing_template_decl || in_template_function ()) > return true; > diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ70.C b/gcc/testsuite/g++.dg/cpp1y/var-templ70.C > new file mode 100644 > index 00000000000..80965657c32 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ70.C > @@ -0,0 +1,19 @@ > +// PR c++/99130 > +// { dg-do compile { target c++14 } } > + > +template > +struct A { > + static constexpr int value = T::value; > +}; > + > +struct B { > + template > + static constexpr int value = T::value; > +}; > + > +template > +constexpr int value = T::value; > + > +using ty1 = decltype(A::value); > +using ty2 = decltype(B::value); > +using ty3 = decltype(value);