public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: Patrick Palka <ppalka@redhat.com>
Cc: gcc-patches@gcc.gnu.org, jason@redhat.com
Subject: Re: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros
Date: Thu, 12 May 2022 14:14:17 -0400 (EDT)	[thread overview]
Message-ID: <e62cca7d-d7b6-d21c-7c09-8974e1d8596c@idea> (raw)
In-Reply-To: <20220512135523.2899969-1-ppalka@redhat.com>

On Thu, 12 May 2022, Patrick Palka wrote:

> After r13-332-g88459c3965e2a2, it looks like we can safely remove the
> NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which simplifies its
> semantics.
> 
> And TMPL_ARGS_LEVEL should verify the level argument is sane in the
> one-dimensional vector case.  This change uncovered a couple of latent
> bugs: in try_class_unification, we weren't correctly copying
> multidimensional targs, and in unify_pack_expansion it seems an
> inequality test needs to be reversed (not sure about this latter fix,
> the test was added in r8-6178-g2625472ffa519e FWIW).
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?  Also tested on cmcstl2 and range-v3.
> 
> gcc/cp/ChangeLog:
> 
> 	* cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test.
> 	(TMPL_ARGS_LEVEL): Assert LEVEL is 1 when
> 	TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false.
> 	* pt.cc (try_class_unification): Correctly copy multidimensional
> 	targs.
> 	(unify_pack_expansion): Fix level comparison.
> ---
>  gcc/cp/cp-tree.h |  5 +++--
>  gcc/cp/pt.cc     | 12 ++++++------
>  2 files changed, 9 insertions(+), 8 deletions(-)
> 
> diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
> index b6961a796af..f681d32ac93 100644
> --- a/gcc/cp/cp-tree.h
> +++ b/gcc/cp/cp-tree.h
> @@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl {
>  /* Nonzero if the template arguments is actually a vector of vectors,
>     rather than just a vector.  */
>  #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE)		     \
> -  (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
> +  (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
>     && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
>  
>  /* The depth of a template argument vector.  When called directly by
> @@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl {
>     args is level 1, not level 0.  */
>  #define TMPL_ARGS_LEVEL(ARGS, LEVEL)		\
>    (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS)	\
> -   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS))
> +   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1)		\
> +   : (gcc_checking_assert (LEVEL == 1), (ARGS)))
>  
>  /* Set the LEVELth level of the template ARGS to VAL.  This macro does
>     not work with single-level argument vectors.  */
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 2c7c5f8bb5d..75b21e5c88a 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -23398,8 +23398,6 @@ static tree
>  try_class_unification (tree tparms, tree targs, tree parm, tree arg,
>  		       bool explain_p)
>  {
> -  tree copy_of_targs;
> -
>    if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
>      return NULL_TREE;
>    else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
> @@ -23438,17 +23436,19 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg,
>       because there are two ways to unify base classes of S<0, 1, 2>
>       with S<I, I, I>.  If we kept the already deduced knowledge, we
>       would reject the possibility I=1.  */
> -  copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
> +  targs = copy_template_args (targs);
> +  for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
> +    targ = NULL_TREE;

It occurred to me that we don't need to make a copy of (the TREE_VECs
for) the outer template arguments, so it might be better to use
copy_node manually instead of copy_template_args.  But this redundant
copying shouldn't matter much if we make sure to ggc_free it afterwards.
So here's v2 which additionally makes try_class_unification ggc_free
this copy of targs (bootstrap and regtest in progress):

-- >8 --

Subject: [PATCH] c++: improve a couple of TMPL_ARGS_* accessor macros

After r13-332-g88459c3965e2a2, it looks like we can safely remove the
NULL test from TMPL_ARGS_HAVE_MULTIPLE_LEVELS, which should make its
uses easier to reason about.

And TMPL_ARGS_LEVEL should verify the level argument is valid in the
one-dimensional vector case.  This change uncovered a couple of latent
issues: in try_class_unification, we weren't correctly copying
multidimensional targs, and in unify_pack_expansion it seems an
inequality test needs to be reversed.  This patch fixes both these
issues, and in passing makes the former function ggc_free the copy of
targs.

gcc/cp/ChangeLog:

	* cp-tree.h (TMPL_ARGS_HAVE_MULTIPLE_LEVELS): Remove NULL test.
	(TMPL_ARGS_LEVEL): Assert LEVEL is 1 when
	TMPL_ARGS_HAVE_MULTIPLE_LEVELS is false.
	* pt.cc (try_class_unification): Correctly copy multidimensional
	targs.  Free the copy of targs.
	(unify_pack_expansion): Fix level comparison.
---
 gcc/cp/cp-tree.h |  5 +++--
 gcc/cp/pt.cc     | 28 ++++++++++++++--------------
 2 files changed, 17 insertions(+), 16 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index b6961a796af..c28a3311dde 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -3766,7 +3766,7 @@ struct GTY(()) lang_decl {
 /* Nonzero if the template arguments is actually a vector of vectors,
    rather than just a vector.  */
 #define TMPL_ARGS_HAVE_MULTIPLE_LEVELS(NODE)		     \
-  (NODE && TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
+  (TREE_VEC_LENGTH (NODE) && TREE_VEC_ELT (NODE, 0)  \
    && TREE_CODE (TREE_VEC_ELT (NODE, 0)) == TREE_VEC)
 
 /* The depth of a template argument vector.  When called directly by
@@ -3783,7 +3783,8 @@ struct GTY(()) lang_decl {
    args is level 1, not level 0.  */
 #define TMPL_ARGS_LEVEL(ARGS, LEVEL)		\
   (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (ARGS)	\
-   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1) : (ARGS))
+   ? TREE_VEC_ELT (ARGS, (LEVEL) - 1)		\
+   : (gcc_checking_assert ((LEVEL) == 1), (ARGS)))
 
 /* Set the LEVELth level of the template ARGS to VAL.  This macro does
    not work with single-level argument vectors.  */
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 2c7c5f8bb5d..3df40f0d22f 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -23398,8 +23398,6 @@ static tree
 try_class_unification (tree tparms, tree targs, tree parm, tree arg,
 		       bool explain_p)
 {
-  tree copy_of_targs;
-
   if (!CLASSTYPE_SPECIALIZATION_OF_PRIMARY_TEMPLATE_P (arg))
     return NULL_TREE;
   else if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
@@ -23438,21 +23436,23 @@ try_class_unification (tree tparms, tree targs, tree parm, tree arg,
      because there are two ways to unify base classes of S<0, 1, 2>
      with S<I, I, I>.  If we kept the already deduced knowledge, we
      would reject the possibility I=1.  */
-  copy_of_targs = make_tree_vec (TREE_VEC_LENGTH (targs));
+  targs = copy_template_args (targs);
+  for (tree& targ : tree_vec_range (INNERMOST_TEMPLATE_ARGS (targs)))
+    targ = NULL_TREE;
 
+  int err;
   if (TREE_CODE (parm) == BOUND_TEMPLATE_TEMPLATE_PARM)
-    {
-      if (unify_bound_ttp_args (tparms, copy_of_targs, parm, arg, explain_p))
-	return NULL_TREE;
-      return arg;
-    }
+    err = unify_bound_ttp_args (tparms, targs, parm, arg, explain_p);
+  else
+    err = unify (tparms, targs, CLASSTYPE_TI_ARGS (parm),
+		 CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p);
 
-  /* If unification failed, we're done.  */
-  if (unify (tparms, copy_of_targs, CLASSTYPE_TI_ARGS (parm),
-	     CLASSTYPE_TI_ARGS (arg), UNIFY_ALLOW_NONE, explain_p))
-    return NULL_TREE;
+  if (TMPL_ARGS_HAVE_MULTIPLE_LEVELS (targs))
+    for (tree level : tree_vec_range (targs))
+      ggc_free (level);
+  ggc_free (targs);
 
-  return arg;
+  return err ? NULL_TREE : arg;
 }
 
 /* Given a template type PARM and a class type ARG, find the unique
@@ -23649,7 +23649,7 @@ unify_pack_expansion (tree tparms, tree targs, tree packed_parms,
 
       /* Determine the index and level of this parameter pack.  */
       template_parm_level_and_index (parm_pack, &level, &idx);
-      if (level < levels)
+      if (level > levels)
 	continue;
 
       /* Keep track of the parameter packs and their corresponding
-- 
2.36.1.74.g277cf0bc36


  reply	other threads:[~2022-05-12 18:14 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-12 13:55 Patrick Palka
2022-05-12 18:14 ` Patrick Palka [this message]
2022-05-12 19:43   ` Jason Merrill
2022-05-12 20:52     ` Patrick Palka

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=e62cca7d-d7b6-d21c-7c09-8974e1d8596c@idea \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@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).