openmp: Metadirective patch fixes

Message ID 1e5b98e6-d8ed-4028-f857-47b23e674082@codesourcery.com
State New
Headers
Series openmp: Metadirective patch fixes |

Commit Message

Kwok Cheung Yeung Jan. 24, 2022, 9:28 p.m. UTC
  Hello

This patch fixes a couple of issues with the latest patch series for 
metadirectives.

Firstly, the changes to c_parser_skip_to_end_of_block_or_statement and 
its C++ equivalent cause a couple of tests (e.g. gcc.dg/attr-malloc.c) 
to regress.

This is because these tests cause the parser to skip code starting from 
within a pair of brackets - this causes the unsigned nesting_depth to 
wrap around to UINT_MAX when a ')' is encountered and so semicolons no 
longer stop the skipping, causing too much code to be skipped and 
resulting in the test regressions. This is fixed by tracking the bracket 
nesting level separately from the brace nesting level in a signed int, 
and to allow skipping to end with negative values.

Secondly, user condition selectors containing only compile-time 
constants should be treated as static rather than dynamic. In practice 
though it doesn't matter much, as GCC readily eliminates the resulting 
'if (<const>)' statements via constant folding.

These fixes should be merged into the original metadirective patches.

Thanks

Kwok
From 77f419aef8a608440789b0ebb4a08f11d69f00e2 Mon Sep 17 00:00:00 2001
From: Kwok Cheung Yeung <kcy@codesourcery.com>
Date: Fri, 21 Jan 2022 18:23:57 +0000
Subject: [PATCH 8/9] openmp: Metadirective fixes

Fix regressions introduced by block/statement skipping.

If user condition selector is constant, do not return it as a dynamic
selector.

2022-01-21  Kwok Cheung Yeung  <kcy@codesourcery.com>

	gcc/c/
	* c-parser.cc (c_parser_skip_to_end_of_block_or_statement): Track
	bracket depth separately from nesting depth.

	gcc/cp/
	* parser.cc (cp_parser_skip_to_end_of_statement): Revert.
	(cp_parser_skip_to_end_of_block_or_statement): Track bracket depth
	separately from nesting depth.

	gcc/
	* omp-general.cc (omp_dynamic_cond): Do not return user condition if
	constant.
---
 gcc/c/c-parser.cc  |  9 ++++++---
 gcc/cp/parser.cc   | 20 ++++++--------------
 gcc/omp-general.cc |  8 ++++++--
 3 files changed, 18 insertions(+), 19 deletions(-)
  

Patch

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 47075973bfe..f3afc38eb65 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -1344,6 +1344,7 @@  static void
 c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
 {
   unsigned nesting_depth = 0;
+  int bracket_depth = 0;
   bool save_error = parser->error;
 
   while (true)
@@ -1366,7 +1367,7 @@  c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
 	case CPP_SEMICOLON:
 	  /* If the next token is a ';', we have reached the
 	     end of the statement.  */
-	  if (!nesting_depth)
+	  if (!nesting_depth && bracket_depth <= 0)
 	    {
 	      /* Consume the ';'.  */
 	      c_parser_consume_token (parser);
@@ -1394,11 +1395,13 @@  c_parser_skip_to_end_of_block_or_statement (c_parser *parser)
 	  /* Track parentheses in case the statement is a standalone 'for'
 	     statement - we want to skip over the semicolons separating the
 	     operands.  */
-	  nesting_depth++;
+	  if (nesting_depth == 0)
+	    ++bracket_depth;
 	  break;
 
 	case CPP_CLOSE_PAREN:
-	  nesting_depth--;
+	  if (nesting_depth == 0)
+	    --bracket_depth;
 	  break;
 
 	case CPP_PRAGMA:
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index de35f42d7c4..7cfaff9d65b 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -3931,17 +3931,6 @@  cp_parser_skip_to_end_of_statement (cp_parser* parser)
 	  ++nesting_depth;
 	  break;
 
-	case CPP_OPEN_PAREN:
-	  /* Track parentheses in case the statement is a standalone 'for'
-	     statement - we want to skip over the semicolons separating the
-	     operands.  */
-	  ++nesting_depth;
-	  break;
-
-	case CPP_CLOSE_PAREN:
-	  --nesting_depth;
-	  break;
-
 	case CPP_KEYWORD:
 	  if (token->keyword != RID__EXPORT
 	      && token->keyword != RID__MODULE
@@ -3991,6 +3980,7 @@  static void
 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
 {
   int nesting_depth = 0;
+  int bracket_depth = 0;
 
   /* Unwind generic function template scope if necessary.  */
   if (parser->fully_implicit_function_template_p)
@@ -4012,7 +4002,7 @@  cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
 
 	case CPP_SEMICOLON:
 	  /* Stop if this is an unnested ';'. */
-	  if (!nesting_depth)
+	  if (!nesting_depth && bracket_depth <= 0)
 	    nesting_depth = -1;
 	  break;
 
@@ -4035,11 +4025,13 @@  cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
 	  /* Track parentheses in case the statement is a standalone 'for'
 	     statement - we want to skip over the semicolons separating the
 	     operands.  */
-	  nesting_depth++;
+	  if (nesting_depth == 0)
+	    bracket_depth++;
 	  break;
 
 	case CPP_CLOSE_PAREN:
-	  nesting_depth--;
+	  if (nesting_depth == 0)
+	    bracket_depth--;
 	  break;
 
 	case CPP_KEYWORD:
diff --git a/gcc/omp-general.cc b/gcc/omp-general.cc
index 9db729e6d59..bab4a932f5d 100644
--- a/gcc/omp-general.cc
+++ b/gcc/omp-general.cc
@@ -1990,7 +1990,7 @@  omp_get_context_selector (tree ctx, const char *set, const char *sel)
 }
 
 /* Return a tree expression representing the dynamic part of the context
- * selector CTX.  */
+   selector CTX.  */
 
 static tree
 omp_dynamic_cond (tree ctx)
@@ -2001,8 +2001,12 @@  omp_dynamic_cond (tree ctx)
       tree expr_list = TREE_VALUE (user);
 
       gcc_assert (TREE_PURPOSE (expr_list) == NULL_TREE);
-      return TREE_VALUE (expr_list);
+
+      /* The user condition is not dynamic if it is constant.  */
+      if (!tree_fits_shwi_p (TREE_VALUE (expr_list)))
+	return TREE_VALUE (expr_list);
     }
+
   return NULL_TREE;
 }