From: Jason Merrill <jason@redhat.com>
To: Patrick Palka <ppalka@redhat.com>, gcc-patches@gcc.gnu.org
Subject: Re: [PATCH] c++: unnecessary instantiation of constexpr var [PR99130]
Date: Wed, 7 Sep 2022 15:55:15 -0400 [thread overview]
Message-ID: <38d432fb-f9db-6f0f-1587-1b8c0f5c75e7@redhat.com> (raw)
In-Reply-To: <20220907194100.879066-1-ppalka@redhat.com>
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<class T>
> +struct A {
> + static constexpr int value = T::value;
> +};
> +
> +struct B {
> + template<class T>
> + static constexpr int value = T::value;
> +};
> +
> +template<class T>
> +constexpr int value = T::value;
> +
> +using ty1 = decltype(A<int>::value);
> +using ty2 = decltype(B::value<int>);
> +using ty3 = decltype(value<int>);
next prev parent reply other threads:[~2022-09-07 19:55 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-07 19:41 Patrick Palka
2022-09-07 19:55 ` Jason Merrill [this message]
2022-09-07 20:40 ` Patrick Palka
2022-09-08 12:46 ` Jason Merrill
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=38d432fb-f9db-6f0f-1587-1b8c0f5c75e7@redhat.com \
--to=jason@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=ppalka@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).