Re: [PATCH 2/3] OpenMP: C++ support for imperfectly-nested loops

2023-05-25 Thread Jakub Jelinek via Gcc-patches
On Fri, Apr 28, 2023 at 05:22:53PM -0600, Sandra Loosemore wrote:
> OpenMP 5.0 removed the restriction that multiple collapsed loops must
> be perfectly nested, allowing "intervening code" (including nested
> BLOCKs) before or after each nested loop.  In GCC this code is moved
> into the inner loop body by the respective front ends.
> 
> This patch changes the C++ front end to use recursive descent parsing
> on nested loops within an "omp for" construct, rather than an
> iterative approach, in order to preserve proper nesting of compound
> statements.  Preserving cleanups (destructors) for class objects
> declared in intervening code and loop initializers complicates moving
> the former into the body of the loop; this is handled by parsing the
> entire construct before reassembling any of it.

What I wrote about the C patch applies mostly about this patch too,
so I won't review it in detail until the review comments are handled
in both, just would like to note that while C (so far) only supports
#pragma syntax for OpenMP, C++ also supports the attribute syntax
(see e.g. g++.dg/gomp/attrs-*.C testcases).
So, e.g. the no OpenMP constructs in intervening code restriction
applies also to the attribute syntax and needs to be tested for it.
It might work without too many changes because the attribute syntax
is handled by rewriting it essentially into pragma syntax and letting
the parser parse it like that.
Of course, it will matter more for the tile/unroll patch when used together
your patch, when it will need to allow the task generating constructs for
the moreloops cases.

> libgomp/ChangeLog
>   * testsuite/libgomp.c++/imperfect-class-1.C : New.
>   * testsuite/libgomp.c++/imperfect-class-2.C : New.
>   * testsuite/libgomp.c++/imperfect-class-3.C : New.
>   * testsuite/libgomp.c++/imperfect-destructor.C : New.
>   * testsuite/libgomp.c++/imperfect-template-1.C : New.
>   * testsuite/libgomp.c++/imperfect-template-2.C : New.
>   * testsuite/libgomp.c++/imperfect-template-3.C : New.

Formatting, there shouldn't be space before : in ChangeLog entries.

Jakub



[PATCH 2/3] OpenMP: C++ support for imperfectly-nested loops

2023-04-28 Thread Sandra Loosemore
OpenMP 5.0 removed the restriction that multiple collapsed loops must
be perfectly nested, allowing "intervening code" (including nested
BLOCKs) before or after each nested loop.  In GCC this code is moved
into the inner loop body by the respective front ends.

This patch changes the C++ front end to use recursive descent parsing
on nested loops within an "omp for" construct, rather than an
iterative approach, in order to preserve proper nesting of compound
statements.  Preserving cleanups (destructors) for class objects
declared in intervening code and loop initializers complicates moving
the former into the body of the loop; this is handled by parsing the
entire construct before reassembling any of it.

gcc/cp/ChangeLog
* cp-tree.h (cp_convert_omp_range_for): Adjust declaration.
* parser.cc (struct omp_for_parse_data): New.
(cp_parser_postfix_expression): Diagnose calls to OpenMP runtime
in intervening code.
(cp_parser_statement_seq_opt): Special-case nested OMP loops and
blocks in intervening code.
(cp_parser_iteration_statement): Reject loops in intervening code.
(cp_parser_omp_for_loop_init): Expand comments and tweak the
interface slightly to better distinguish input/output parameters.
(cp_parser_omp_range_for): Likewise.
(cp_convert_omp_range_for): Likewise.
(cp_parser_omp_loop_nest): New, split from cp_parser_omp_for_loop
and largely rewritten.  Add more comments.
(struct sit_data, substitute_in_tree_walker, substitute_in_tree):
New.
(fixup_blocks_walker): New.
(cp_parser_omp_for_loop): Rewrite to use recursive descent instead
of a loop.  Add logic to reshuffle the bits of code collected
during parsing so intervening code gets moved to the loop body.
(cp_parser_omp_loop): Remove call to finish_omp_for_block, which
is now redundant.
(cp_parser_omp_simd): Likewise.
(cp_parser_omp_for): Likewise.
(cp_parser_omp_distribute): Likewise.
(cp_parser_oacc_loop): Likewise.
(cp_parser_omp_taskloop): Likewise.
(cp_parser_pragma): Reject OpenMP pragmas in intervening code.
* parser.h (struct cp_parser): Add omp_for_parse_state field.
* pt.cc (tsubst_omp_for_iterator): Adjust call to
cp_convert_omp_range_for.
* semantics.cc (struct fofb_data, finish_omp_for_block_walker): New.
(finish_omp_for_block): Allow variables to be bound in a BIND_EXPR
nested inside BIND instead of directly in BIND itself.

gcc/testsuite/ChangeLog
* g++.dg/gomp/pr41967.C: Adjust expected error messages.

libgomp/ChangeLog
* testsuite/libgomp.c++/imperfect-class-1.C : New.
* testsuite/libgomp.c++/imperfect-class-2.C : New.
* testsuite/libgomp.c++/imperfect-class-3.C : New.
* testsuite/libgomp.c++/imperfect-destructor.C : New.
* testsuite/libgomp.c++/imperfect-template-1.C : New.
* testsuite/libgomp.c++/imperfect-template-2.C : New.
* testsuite/libgomp.c++/imperfect-template-3.C : New.
---
 gcc/cp/cp-tree.h  |2 +-
 gcc/cp/parser.cc  | 1180 -
 gcc/cp/parser.h   |3 +
 gcc/cp/pt.cc  |3 +-
 gcc/cp/semantics.cc   |   80 +-
 gcc/testsuite/g++.dg/gomp/pr41967.C   |2 +-
 .../testsuite/libgomp.c++/imperfect-class-1.C |  169 +++
 .../testsuite/libgomp.c++/imperfect-class-2.C |  167 +++
 .../testsuite/libgomp.c++/imperfect-class-3.C |  167 +++
 .../libgomp.c++/imperfect-destructor.C|  135 ++
 .../libgomp.c++/imperfect-template-1.C|  172 +++
 .../libgomp.c++/imperfect-template-2.C|  170 +++
 .../libgomp.c++/imperfect-template-3.C|  170 +++
 13 files changed, 2021 insertions(+), 399 deletions(-)
 create mode 100644 libgomp/testsuite/libgomp.c++/imperfect-class-1.C
 create mode 100644 libgomp/testsuite/libgomp.c++/imperfect-class-2.C
 create mode 100644 libgomp/testsuite/libgomp.c++/imperfect-class-3.C
 create mode 100644 libgomp/testsuite/libgomp.c++/imperfect-destructor.C
 create mode 100644 libgomp/testsuite/libgomp.c++/imperfect-template-1.C
 create mode 100644 libgomp/testsuite/libgomp.c++/imperfect-template-2.C
 create mode 100644 libgomp/testsuite/libgomp.c++/imperfect-template-3.C

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index c9c4cd6f32f..90d369e4f65 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7279,7 +7279,7 @@ extern bool maybe_clone_body  (tree);
 /* In parser.cc */
 extern tree cp_convert_range_for (tree, tree, tree, tree, unsigned int, bool,
  unsigned short);
-extern void cp_convert_omp_range_for (tree &, vec *, tree &,
+extern void cp_convert_omp_range_for (tree &, tree &, tree &,
  tree &, tree &,