[RFA] tree: define tree_code_type in C++11/14 [PR108634]

Message ID 20230313201512.151814-1-jason@redhat.com
State New
Headers
Series [RFA] tree: define tree_code_type in C++11/14 [PR108634] |

Commit Message

Jason Merrill March 13, 2023, 8:15 p.m. UTC
  Tested x86_64-pc-linux-gnu, OK for trunk?

-- 8< --

The r13-6577 change to use tree_code_type_tmpl in earlier C++ dialects broke
gdbhooks, which expects tree_code_type to always be available.  I considered
trying to make gdbhooks more robust, but it seemed simpler to define
tree_code_type as a reference to the template.  This still ends up with a
definition of the reference in each translation unit, but that's allowed by the
ODR because it always refers to the same entity, and is much smaller than
having the whole table in each TU.

	PR plugins/108634

gcc/ChangeLog:

	* tree-core.h (tree_code_type, tree_code_length):
	Define even without inline variable support.
	* tree.h (TREE_CODE_CLASS, TREE_CODE_LENGTH):
	Only one definition.
---
 gcc/tree-core.h |  3 +++
 gcc/tree.h      | 10 ----------
 2 files changed, 3 insertions(+), 10 deletions(-)


base-commit: c227508d06a63f9b8fede3fd88813accb447060e
  

Comments

Jason Merrill March 13, 2023, 8:47 p.m. UTC | #1
On 3/13/23 16:15, Jason Merrill wrote:
> Tested x86_64-pc-linux-gnu, OK for trunk?
> 
> -- 8< --
> 
> The r13-6577 change to use tree_code_type_tmpl in earlier C++ dialects broke
> gdbhooks, which expects tree_code_type to always be available.  I considered
> trying to make gdbhooks more robust, but it seemed simpler to define
> tree_code_type as a reference to the template.  This still ends up with a
> definition of the reference in each translation unit, but that's allowed by the
> ODR because it always refers to the same entity, and is much smaller than
> having the whole table in each TU.

...or I could build with a newer bootstrap compiler, I suppose.

> 	PR plugins/108634
> 
> gcc/ChangeLog:
> 
> 	* tree-core.h (tree_code_type, tree_code_length):
> 	Define even without inline variable support.
> 	* tree.h (TREE_CODE_CLASS, TREE_CODE_LENGTH):
> 	Only one definition.
> ---
>   gcc/tree-core.h |  3 +++
>   gcc/tree.h      | 10 ----------
>   2 files changed, 3 insertions(+), 10 deletions(-)
> 
> diff --git a/gcc/tree-core.h b/gcc/tree-core.h
> index fd2be57b78c..545dfd30114 100644
> --- a/gcc/tree-core.h
> +++ b/gcc/tree-core.h
> @@ -2298,6 +2298,7 @@ struct tree_code_type_tmpl {
>   
>   template <int N>
>   constexpr enum tree_code_class tree_code_type_tmpl<N>::tree_code_type[];
> +static constexpr auto &tree_code_type = tree_code_type_tmpl<0>::tree_code_type;
>   #else
>   constexpr inline enum tree_code_class tree_code_type[] = {
>   #include "all-tree.def"
> @@ -2326,6 +2327,8 @@ struct tree_code_length_tmpl {
>   
>   template <int N>
>   constexpr unsigned char tree_code_length_tmpl<N>::tree_code_length[];
> +static constexpr auto &tree_code_length
> += tree_code_length_tmpl<0>::tree_code_length;
>   #else
>   constexpr inline unsigned char tree_code_length[] = {
>   #include "all-tree.def"
> diff --git a/gcc/tree.h b/gcc/tree.h
> index 91375f9652f..92ac0e6a214 100644
> --- a/gcc/tree.h
> +++ b/gcc/tree.h
> @@ -177,12 +177,7 @@ code_helper::is_builtin_fn () const
>   #define TREE_CODE_CLASS_STRING(CLASS)\
>           tree_code_class_strings[(int) (CLASS)]
>   
> -#if __cpp_inline_variables < 201606L
> -#define TREE_CODE_CLASS(CODE)	\
> -  tree_code_type_tmpl <0>::tree_code_type[(int) (CODE)]
> -#else
>   #define TREE_CODE_CLASS(CODE)	tree_code_type[(int) (CODE)]
> -#endif
>   
>   /* Nonzero if NODE represents an exceptional code.  */
>   
> @@ -276,12 +271,7 @@ code_helper::is_builtin_fn () const
>   
>   #define EXPR_P(NODE) IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (NODE)))
>   
> -#if __cpp_inline_variables < 201606L
> -#define TREE_CODE_LENGTH(CODE)	\
> -  tree_code_length_tmpl <0>::tree_code_length[(int) (CODE)]
> -#else
>   #define TREE_CODE_LENGTH(CODE)	tree_code_length[(int) (CODE)]
> -#endif
>   
>   
>   /* Helper macros for math builtins.  */
> 
> base-commit: c227508d06a63f9b8fede3fd88813accb447060e
  
Jakub Jelinek March 13, 2023, 9:02 p.m. UTC | #2
On Mon, Mar 13, 2023 at 04:15:12PM -0400, Jason Merrill wrote:
> The r13-6577 change to use tree_code_type_tmpl in earlier C++ dialects broke
> gdbhooks, which expects tree_code_type to always be available.  I considered
> trying to make gdbhooks more robust, but it seemed simpler to define
> tree_code_type as a reference to the template.  This still ends up with a
> definition of the reference in each translation unit, but that's allowed by the
> ODR because it always refers to the same entity, and is much smaller than
> having the whole table in each TU.
> 
> 	PR plugins/108634
> 
> gcc/ChangeLog:
> 
> 	* tree-core.h (tree_code_type, tree_code_length):
> 	Define even without inline variable support.
> 	* tree.h (TREE_CODE_CLASS, TREE_CODE_LENGTH):
> 	Only one definition.

I think it would be better to change gdbhooks.py to match what the code
does.
We should adjust the
        # extern const enum tree_code_class tree_code_type[];
        # #define TREE_CODE_CLASS(CODE) tree_code_type[(int) (CODE)]
comment because that isn't true even for new bootstrap compiler.
And, I just wonder if
        val_tree_code_type = gdb.parse_and_eval('tree_code_type')
couldn't be replaced with
        try:
            val_tree_code_type = gdb.parse_and_eval('tree_code_type')
        except;
            val_tree_code_type = gdb.parse_and_eval('tree_code_type_tmpl<0>::tree_code_type')
or so.

	Jakub
  
Jakub Jelinek March 13, 2023, 9:54 p.m. UTC | #3
On Mon, Mar 13, 2023 at 10:02:41PM +0100, Jakub Jelinek wrote:
> On Mon, Mar 13, 2023 at 04:15:12PM -0400, Jason Merrill wrote:
> > The r13-6577 change to use tree_code_type_tmpl in earlier C++ dialects broke
> > gdbhooks, which expects tree_code_type to always be available.  I considered
> > trying to make gdbhooks more robust, but it seemed simpler to define
> > tree_code_type as a reference to the template.  This still ends up with a
> > definition of the reference in each translation unit, but that's allowed by the
> > ODR because it always refers to the same entity, and is much smaller than
> > having the whole table in each TU.
> > 
> > 	PR plugins/108634
> > 
> > gcc/ChangeLog:
> > 
> > 	* tree-core.h (tree_code_type, tree_code_length):
> > 	Define even without inline variable support.
> > 	* tree.h (TREE_CODE_CLASS, TREE_CODE_LENGTH):
> > 	Only one definition.
> 
> I think it would be better to change gdbhooks.py to match what the code
> does.
> We should adjust the
>         # extern const enum tree_code_class tree_code_type[];
>         # #define TREE_CODE_CLASS(CODE) tree_code_type[(int) (CODE)]
> comment because that isn't true even for new bootstrap compiler.
> And, I just wonder if
>         val_tree_code_type = gdb.parse_and_eval('tree_code_type')
> couldn't be replaced with
>         try:
>             val_tree_code_type = gdb.parse_and_eval('tree_code_type')
>         except;
>             val_tree_code_type = gdb.parse_and_eval('tree_code_type_tmpl<0>::tree_code_type')
> or so.

Or
        val_tree_code_type = gdb.parse_and_eval('tree_code_type')
        if val_tree_code_type == 0:
            val_tree_code_type = gdb.parse_and_eval('tree_code_type_tmpl<0>::tree_code_type')
Whatever works.

	Jakub
  

Patch

diff --git a/gcc/tree-core.h b/gcc/tree-core.h
index fd2be57b78c..545dfd30114 100644
--- a/gcc/tree-core.h
+++ b/gcc/tree-core.h
@@ -2298,6 +2298,7 @@  struct tree_code_type_tmpl {
 
 template <int N>
 constexpr enum tree_code_class tree_code_type_tmpl<N>::tree_code_type[];
+static constexpr auto &tree_code_type = tree_code_type_tmpl<0>::tree_code_type;
 #else
 constexpr inline enum tree_code_class tree_code_type[] = {
 #include "all-tree.def"
@@ -2326,6 +2327,8 @@  struct tree_code_length_tmpl {
 
 template <int N>
 constexpr unsigned char tree_code_length_tmpl<N>::tree_code_length[];
+static constexpr auto &tree_code_length
+= tree_code_length_tmpl<0>::tree_code_length;
 #else
 constexpr inline unsigned char tree_code_length[] = {
 #include "all-tree.def"
diff --git a/gcc/tree.h b/gcc/tree.h
index 91375f9652f..92ac0e6a214 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -177,12 +177,7 @@  code_helper::is_builtin_fn () const
 #define TREE_CODE_CLASS_STRING(CLASS)\
         tree_code_class_strings[(int) (CLASS)]
 
-#if __cpp_inline_variables < 201606L
-#define TREE_CODE_CLASS(CODE)	\
-  tree_code_type_tmpl <0>::tree_code_type[(int) (CODE)]
-#else
 #define TREE_CODE_CLASS(CODE)	tree_code_type[(int) (CODE)]
-#endif
 
 /* Nonzero if NODE represents an exceptional code.  */
 
@@ -276,12 +271,7 @@  code_helper::is_builtin_fn () const
 
 #define EXPR_P(NODE) IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (TREE_CODE (NODE)))
 
-#if __cpp_inline_variables < 201606L
-#define TREE_CODE_LENGTH(CODE)	\
-  tree_code_length_tmpl <0>::tree_code_length[(int) (CODE)]
-#else
 #define TREE_CODE_LENGTH(CODE)	tree_code_length[(int) (CODE)]
-#endif
 
 
 /* Helper macros for math builtins.  */