* [PATCH V2 0/5] OpenMP: support for imperfectly-nested loops
@ 2023-07-23 22:15 Sandra Loosemore
2023-07-23 22:15 ` [PATCH V2 1/5] OpenMP: Add OMP_STRUCTURED_BLOCK and GIMPLE_OMP_STRUCTURED_BLOCK Sandra Loosemore
` (5 more replies)
0 siblings, 6 replies; 15+ messages in thread
From: Sandra Loosemore @ 2023-07-23 22:15 UTC (permalink / raw)
To: gcc-patches; +Cc: jakub
Here is the latest version of my imperfectly-nested loops patches.
Compared to the initial version I'd posted in April
https://gcc.gnu.org/pipermail/gcc-patches/2023-April/617103.html
this version includes many minor cosmetic fixes suggested by Jakub in
his initial review (also present in the version I committed to the
OG13 branch last month), many new test cases to cover various corner
cases, and code fixes so that C and C++ at least behave consistently
even if the spec is unclear. The most intrusive of those fixes is
that I couldn't figure out how to make jumping between different
structured blocks of intervening code in the same OMP loop construct
produce errors without introducing new GENERIC and GIMPLE data
structures to represent a structured block without any other
associated OpenMP semantics; that's now part 1 of the patch series.
There are a few things from the review comments I haven't done anything
about:
* I left omp-api.h alone because the Fortran front end needs those
declarations without everything else in omp-general.h.
* I didn't think I ought to be speculatively implementing extensions
like allowing "do { ... } while (0);" in intervening code. If it's
really important for supporting macros, I suppose it will make it
into a future version of the OpenMP spec.
* I didn't understand the comment about needing to add "#pragma omp
ordered doacross(source) and sink" to the testcase for errors with
the "ordered" clause. Isn't that only for cross-iteration
data dependencies? There aren't any in that loop. Also note that some
of my new corner-case tests use the "ordered" clause to trigger an
error to check that things are being correctly parsed as intervening
code, so if there is something really bogus there that must be fixed,
it now affects other test cases as well.
* Likewise I didn't know what to do with coming up with a better
testcase for "scan". I could not find an existing testcase with nested
loops that I could just add intervening code to, and when I made
another attempt to write a new one from scratch I quickly realized I
couldn't do much better than the existing one, which Tobias had
originally helped me with.
-Sandra
Sandra Loosemore (5):
OpenMP: Add OMP_STRUCTURED_BLOCK and GIMPLE_OMP_STRUCTURED_BLOCK.
OpenMP: C front end support for imperfectly-nested loops
OpenMP: C++ support for imperfectly-nested loops
OpenMP: New C/C++ testcases for imperfectly nested loops.
OpenMP: Fortran support for imperfectly-nested loops
gcc/c-family/c-common.h | 1 +
gcc/c-family/c-omp.cc | 151 ++
gcc/c/c-parser.cc | 860 +++++++----
gcc/cp/constexpr.cc | 1 +
gcc/cp/cp-tree.h | 2 +-
gcc/cp/parser.cc | 1315 ++++++++++++-----
gcc/cp/parser.h | 3 +
gcc/cp/pt.cc | 4 +-
gcc/cp/semantics.cc | 117 +-
gcc/doc/generic.texi | 14 +
gcc/doc/gimple.texi | 19 +
gcc/fortran/gfortran.h | 3 +
gcc/fortran/openmp.cc | 765 ++++++++--
gcc/fortran/trans-stmt.cc | 7 +-
gcc/gimple-low.cc | 5 +
gcc/gimple-pretty-print.cc | 6 +-
gcc/gimple-walk.cc | 1 +
gcc/gimple.cc | 15 +
gcc/gimple.def | 5 +
gcc/gimple.h | 3 +
gcc/gimplify.cc | 6 +
gcc/omp-api.h | 32 +
gcc/omp-expand.cc | 5 +
gcc/omp-general.cc | 134 ++
gcc/omp-general.h | 1 +
gcc/omp-low.cc | 140 +-
gcc/testsuite/c-c++-common/goacc/collapse-1.c | 16 +-
gcc/testsuite/c-c++-common/goacc/tile-2.c | 4 +-
.../c-c++-common/gomp/imperfect-attributes.c | 81 +
.../c-c++-common/gomp/imperfect-badloops.c | 50 +
.../c-c++-common/gomp/imperfect-blocks.c | 75 +
.../c-c++-common/gomp/imperfect-extension.c | 55 +
.../c-c++-common/gomp/imperfect-gotos.c | 174 +++
.../gomp/imperfect-invalid-scope.c | 77 +
.../c-c++-common/gomp/imperfect-labels.c | 85 ++
.../gomp/imperfect-legacy-syntax.c | 44 +
.../c-c++-common/gomp/imperfect-pragmas.c | 85 ++
gcc/testsuite/c-c++-common/gomp/imperfect1.c | 38 +
gcc/testsuite/c-c++-common/gomp/imperfect2.c | 34 +
gcc/testsuite/c-c++-common/gomp/imperfect3.c | 33 +
gcc/testsuite/c-c++-common/gomp/imperfect4.c | 33 +
gcc/testsuite/c-c++-common/gomp/imperfect5.c | 57 +
gcc/testsuite/g++.dg/gomp/attrs-imperfect1.C | 38 +
gcc/testsuite/g++.dg/gomp/attrs-imperfect2.C | 34 +
gcc/testsuite/g++.dg/gomp/attrs-imperfect3.C | 33 +
gcc/testsuite/g++.dg/gomp/attrs-imperfect4.C | 33 +
gcc/testsuite/g++.dg/gomp/attrs-imperfect5.C | 57 +
gcc/testsuite/g++.dg/gomp/pr41967.C | 2 +-
.../g++.dg/gomp/tpl-imperfect-gotos.C | 161 ++
.../g++.dg/gomp/tpl-imperfect-invalid-scope.C | 94 ++
gcc/testsuite/gcc.dg/gomp/collapse-1.c | 10 +-
gcc/testsuite/gfortran.dg/gomp/collapse1.f90 | 6 +-
gcc/testsuite/gfortran.dg/gomp/collapse2.f90 | 10 +-
.../gfortran.dg/gomp/imperfect-gotos.f90 | 69 +
.../gomp/imperfect-invalid-scope.f90 | 81 +
gcc/testsuite/gfortran.dg/gomp/imperfect1.f90 | 39 +
gcc/testsuite/gfortran.dg/gomp/imperfect2.f90 | 56 +
gcc/testsuite/gfortran.dg/gomp/imperfect3.f90 | 29 +
gcc/testsuite/gfortran.dg/gomp/imperfect4.f90 | 36 +
gcc/testsuite/gfortran.dg/gomp/imperfect5.f90 | 67 +
gcc/tree-inline.cc | 6 +
gcc/tree-nested.cc | 3 +
gcc/tree-pretty-print.cc | 4 +
gcc/tree.def | 9 +
gcc/tree.h | 3 +
.../testsuite/libgomp.c++/attrs-imperfect1.C | 76 +
.../testsuite/libgomp.c++/attrs-imperfect2.C | 114 ++
.../testsuite/libgomp.c++/attrs-imperfect3.C | 119 ++
.../testsuite/libgomp.c++/attrs-imperfect4.C | 117 ++
.../testsuite/libgomp.c++/attrs-imperfect5.C | 49 +
.../testsuite/libgomp.c++/attrs-imperfect6.C | 115 ++
.../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 +++
.../libgomp.c-c++-common/imperfect1.c | 76 +
.../libgomp.c-c++-common/imperfect2.c | 114 ++
.../libgomp.c-c++-common/imperfect3.c | 119 ++
.../libgomp.c-c++-common/imperfect4.c | 117 ++
.../libgomp.c-c++-common/imperfect5.c | 49 +
.../libgomp.c-c++-common/imperfect6.c | 115 ++
.../libgomp.c-c++-common/target-imperfect1.c | 81 +
.../libgomp.c-c++-common/target-imperfect2.c | 122 ++
.../libgomp.c-c++-common/target-imperfect3.c | 125 ++
.../libgomp.c-c++-common/target-imperfect4.c | 122 ++
.../libgomp.fortran/imperfect-destructor.f90 | 142 ++
.../testsuite/libgomp.fortran/imperfect1.f90 | 67 +
.../testsuite/libgomp.fortran/imperfect2.f90 | 102 ++
.../testsuite/libgomp.fortran/imperfect3.f90 | 110 ++
.../testsuite/libgomp.fortran/imperfect4.f90 | 121 ++
.../libgomp.fortran/target-imperfect1.f90 | 72 +
.../libgomp.fortran/target-imperfect2.f90 | 110 ++
.../libgomp.fortran/target-imperfect3.f90 | 116 ++
.../libgomp.fortran/target-imperfect4.f90 | 126 ++
97 files changed, 8270 insertions(+), 912 deletions(-)
create mode 100644 gcc/omp-api.h
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-attributes.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-badloops.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-blocks.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-extension.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-gotos.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-invalid-scope.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-labels.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-legacy-syntax.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-pragmas.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect1.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect2.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect3.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect4.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect5.c
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect1.C
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect2.C
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect3.C
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect4.C
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect5.C
create mode 100644 gcc/testsuite/g++.dg/gomp/tpl-imperfect-gotos.C
create mode 100644 gcc/testsuite/g++.dg/gomp/tpl-imperfect-invalid-scope.C
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect-gotos.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect-invalid-scope.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect1.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect2.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect3.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect4.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect5.f90
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect1.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect2.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect3.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect4.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect5.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect6.C
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
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect1.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect2.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect3.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect4.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect5.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect6.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-imperfect1.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-imperfect2.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-imperfect3.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-imperfect4.c
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect-destructor.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect1.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect2.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect3.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect4.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/target-imperfect1.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/target-imperfect2.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/target-imperfect3.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/target-imperfect4.f90
--
2.31.1
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH V2 1/5] OpenMP: Add OMP_STRUCTURED_BLOCK and GIMPLE_OMP_STRUCTURED_BLOCK.
2023-07-23 22:15 [PATCH V2 0/5] OpenMP: support for imperfectly-nested loops Sandra Loosemore
@ 2023-07-23 22:15 ` Sandra Loosemore
2023-08-22 13:01 ` Jakub Jelinek
2023-07-23 22:15 ` [PATCH V2 2/5] OpenMP: C front end support for imperfectly-nested loops Sandra Loosemore
` (4 subsequent siblings)
5 siblings, 1 reply; 15+ messages in thread
From: Sandra Loosemore @ 2023-07-23 22:15 UTC (permalink / raw)
To: gcc-patches; +Cc: jakub
In order to detect invalid jumps in and out of intervening code in
imperfectly-nested loops, the front ends need to insert some sort of
marker to identify the structured block sequences that they push into
the inner body of the loop. The error checking happens in the
diagnose_omp_blocks pass, between gimplification and OMP lowering, so
we need both GENERIC and GIMPLE representations of these markers.
They are removed in OMP lowering so no subsequent passes need to know
about them.
This patch doesn't include any front-end changes to generate the new
data structures.
gcc/cp/ChangeLog
* constexpr.cc (cxx_eval_constant_expression): Handle
OMP_STRUCTURED_BLOCK.
* pt.cc (tsubst_expr): Likewise.
gcc/ChangeLog
* doc/generic.texi (OpenMP): Document OMP_STRUCTURED_BLOCK.
* doc/gimple.texi (GIMPLE instruction set): Add
GIMPLE_OMP_STRUCTURED_BLOCK.
(GIMPLE_OMP_STRUCTURED_BLOCK): New subsection.
* gimple-low.cc (lower_stmt): Error on GIMPLE_OMP_STRUCTURED_BLOCK.
* gimple-pretty-print.cc (dump_gimple_omp_block): Handle
GIMPLE_OMP_STRUCTURED_BLOCK.
(pp_gimple_stmt_1): Likewise.
* gimple-walk.cc (walk_gimple_stmt): Likewise.
* gimple.cc (gimple_build_omp_structured_block): New.
* gimple.def (GIMPLE_OMP_STRUCTURED_BLOCK): New.
* gimple.h (gimple_build_omp_structured_block): Declare.
(gimple_has_substatements): Handle GIMPLE_OMP_STRUCTURED_BLOCK.
(CASE_GIMPLE_OMP): Likewise.
* gimplify.cc (is_gimple_stmt): Handle OMP_STRUCTURED_BLOCK.
(gimplify_expr): Likewise.
* omp-expand.cc (GIMPLE_OMP_STRUCTURED_BLOCK): Error on
GIMPLE_OMP_STRUCTURED_BLOCK.
* omp-low.cc (scan_omp_1_stmt): Handle GIMPLE_OMP_STRUCTURED_BLOCK.
(lower_omp_1): Likewise.
(diagnose_sb_1): Likewise.
(diagnose_sb_2): Likewise.
* tree-inline.cc (remap_gimple_stmt): Handle
GIMPLE_OMP_STRUCTURED_BLOCK.
(estimate_num_insns): Likewise.
* tree-nested.cc (convert_nonlocal_reference_stmt): Likewise.
(convert_local_reference_stmt): Likewise.
(convert_gimple_call): Likewise.
* tree-pretty-print.cc (dump_generic_node): Handle
OMP_STRUCTURED_BLOCK.
* tree.def (OMP_STRUCTURED_BLOCK): New.
* tree.h (OMP_STRUCTURED_BLOCK_BODY): New.
---
gcc/cp/constexpr.cc | 1 +
gcc/cp/pt.cc | 1 +
gcc/doc/generic.texi | 14 ++++++++++++++
gcc/doc/gimple.texi | 19 +++++++++++++++++++
gcc/gimple-low.cc | 5 +++++
gcc/gimple-pretty-print.cc | 6 +++++-
gcc/gimple-walk.cc | 1 +
gcc/gimple.cc | 15 +++++++++++++++
gcc/gimple.def | 5 +++++
gcc/gimple.h | 3 +++
gcc/gimplify.cc | 6 ++++++
gcc/omp-expand.cc | 5 +++++
gcc/omp-low.cc | 11 +++++++++++
gcc/tree-inline.cc | 6 ++++++
gcc/tree-nested.cc | 3 +++
gcc/tree-pretty-print.cc | 4 ++++
gcc/tree.def | 9 +++++++++
gcc/tree.h | 3 +++
18 files changed, 116 insertions(+), 1 deletion(-)
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index cca0435bafc..a43e5d7f29d 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -8055,6 +8055,7 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
case OMP_SCAN:
case OMP_SCOPE:
case OMP_SECTION:
+ case OMP_STRUCTURED_BLOCK:
case OMP_MASTER:
case OMP_MASKED:
case OMP_TASKGROUP:
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index d7d774fd9e5..303f72353c0 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -19692,6 +19692,7 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl)
break;
case OMP_MASTER:
+ case OMP_STRUCTURED_BLOCK:
omp_parallel_combined_clauses = NULL;
/* FALLTHRU */
case OMP_SECTION:
diff --git a/gcc/doc/generic.texi b/gcc/doc/generic.texi
index 3f9bddd7eae..c8d6ef062f6 100644
--- a/gcc/doc/generic.texi
+++ b/gcc/doc/generic.texi
@@ -2488,6 +2488,20 @@ In some cases, @code{OMP_CONTINUE} is placed right before
occur right after the looping body, it will be emitted between
@code{OMP_CONTINUE} and @code{OMP_RETURN}.
+@item OMP_STRUCTURED_BLOCK
+
+This is another statement that doesn't correspond to an OpenMP directive.
+It is used to mark sections of code in another directive that must
+be structured block sequences, in particular for sequences of intervening code
+in the body of an @code{OMP_FOR}. It is not necessary to use this when the
+entire body of a directive is required to be a structured block sequence,
+since that is implicit in the representation of the corresponding node.
+
+This tree node is used only to allow error checking transfers of control
+in/out of the structured block sequence after gimplification.
+It has a single operand (@code{OMP_STRUCTURED_BLOCK_BODY}) that is
+the code within the structured block sequence.
+
@item OMP_ATOMIC
Represents @code{#pragma omp atomic}.
diff --git a/gcc/doc/gimple.texi b/gcc/doc/gimple.texi
index 322c7609cf3..a3ade15afc9 100644
--- a/gcc/doc/gimple.texi
+++ b/gcc/doc/gimple.texi
@@ -468,6 +468,7 @@ The following table briefly describes the GIMPLE instruction set.
@item @code{GIMPLE_OMP_SECTIONS_SWITCH} @tab x @tab x
@item @code{GIMPLE_OMP_SINGLE} @tab x @tab x
@item @code{GIMPLE_PHI} @tab @tab x
+@item @code{GIMPLE_OMP_STRUCTURED_BLOCK} @tab x @tab
@item @code{GIMPLE_RESX} @tab @tab x
@item @code{GIMPLE_RETURN} @tab x @tab x
@item @code{GIMPLE_SWITCH} @tab x @tab x
@@ -1040,6 +1041,7 @@ Return a deep copy of statement @code{STMT}.
* @code{GIMPLE_OMP_SECTION}::
* @code{GIMPLE_OMP_SECTIONS}::
* @code{GIMPLE_OMP_SINGLE}::
+* @code{GIMPLE_OMP_STRUCTURED_BLOCK}::
* @code{GIMPLE_PHI}::
* @code{GIMPLE_RESX}::
* @code{GIMPLE_RETURN}::
@@ -2160,6 +2162,23 @@ Set @code{CLAUSES} to be the clauses associated with @code{OMP_SINGLE} @code{G}.
@end deftypefn
+@node @code{GIMPLE_OMP_STRUCTURED_BLOCK}
+@subsection @code{GIMPLE_OMP_STRUCTURED_BLOCK}
+@cindex @code{GIMPLE_OMP_STRUCTURED_BLOCK}
+
+Like the GENERIC equivalent @code{OMP_STRUCTURED_BLOCK}, this GIMPLE
+statement does not correspond directly to an OpenMP directive, and
+exists only to permit error checking of transfers of control
+in/out of structured block sequences (the @code{diagnose_omp_blocks} pass
+in @file{omp-low.cc}). All @code{GIMPLE_OMP_STRUCTURED_BLOCK}
+nodes are eliminated during OpenMP lowering.
+
+@deftypefn {GIMPLE function} gimple gimple_build_omp_structured_block (gimple_seq body)
+Build a @code{GIMPLE_OMP_STRUCTURED_BLOCK} statement.
+@code{BODY} is the sequence of statements in the structured block sequence.
+@end deftypefn
+
+
@node @code{GIMPLE_PHI}
@subsection @code{GIMPLE_PHI}
@cindex @code{GIMPLE_PHI}
diff --git a/gcc/gimple-low.cc b/gcc/gimple-low.cc
index e6f04f234df..8e72253d304 100644
--- a/gcc/gimple-low.cc
+++ b/gcc/gimple-low.cc
@@ -717,6 +717,11 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
gsi_next (gsi);
return;
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
+ /* These are supposed to be removed already in OMP lowering. */
+ gcc_unreachable ();
+ break;
+
case GIMPLE_NOP:
case GIMPLE_ASM:
case GIMPLE_ASSIGN:
diff --git a/gcc/gimple-pretty-print.cc b/gcc/gimple-pretty-print.cc
index 8db221f65fe..82017b92e89 100644
--- a/gcc/gimple-pretty-print.cc
+++ b/gcc/gimple-pretty-print.cc
@@ -1896,7 +1896,7 @@ dump_gimple_omp_sections (pretty_printer *buffer, const gomp_sections *gs,
}
}
-/* Dump a GIMPLE_OMP_{MASTER,ORDERED,SECTION} tuple on the
+/* Dump a GIMPLE_OMP_{MASTER,ORDERED,SECTION,STRUCTURED_BLOCK} tuple on the
pretty_printer BUFFER. */
static void
@@ -1916,6 +1916,9 @@ dump_gimple_omp_block (pretty_printer *buffer, const gimple *gs, int spc,
case GIMPLE_OMP_SECTION:
pp_string (buffer, "#pragma omp section");
break;
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
+ pp_string (buffer, "#pragma omp __structured_block");
+ break;
default:
gcc_unreachable ();
}
@@ -2801,6 +2804,7 @@ pp_gimple_stmt_1 (pretty_printer *buffer, const gimple *gs, int spc,
case GIMPLE_OMP_MASTER:
case GIMPLE_OMP_SECTION:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
dump_gimple_omp_block (buffer, gs, spc, flags);
break;
diff --git a/gcc/gimple-walk.cc b/gcc/gimple-walk.cc
index a019515438c..9516d61ffa9 100644
--- a/gcc/gimple-walk.cc
+++ b/gcc/gimple-walk.cc
@@ -693,6 +693,7 @@ walk_gimple_stmt (gimple_stmt_iterator *gsi, walk_stmt_fn callback_stmt,
case GIMPLE_OMP_ORDERED:
case GIMPLE_OMP_SCAN:
case GIMPLE_OMP_SECTION:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
case GIMPLE_OMP_PARALLEL:
case GIMPLE_OMP_TASK:
case GIMPLE_OMP_SCOPE:
diff --git a/gcc/gimple.cc b/gcc/gimple.cc
index e0ba42add39..2dad16bee8b 100644
--- a/gcc/gimple.cc
+++ b/gcc/gimple.cc
@@ -1038,6 +1038,21 @@ gimple_build_omp_section (gimple_seq body)
}
+/* Build a GIMPLE_OMP_STRUCTURED_BLOCK statement.
+
+ BODY is the structured block sequence. */
+
+gimple *
+gimple_build_omp_structured_block (gimple_seq body)
+{
+ gimple *p = gimple_alloc (GIMPLE_OMP_STRUCTURED_BLOCK, 0);
+ if (body)
+ gimple_omp_set_body (p, body);
+
+ return p;
+}
+
+
/* Build a GIMPLE_OMP_MASTER statement.
BODY is the sequence of statements to be executed by just the master. */
diff --git a/gcc/gimple.def b/gcc/gimple.def
index 274350d9534..b164a8b847a 100644
--- a/gcc/gimple.def
+++ b/gcc/gimple.def
@@ -275,6 +275,11 @@ DEFGSCODE(GIMPLE_OMP_CRITICAL, "gimple_omp_critical", GSS_OMP_CRITICAL)
unspecified by the standards. */
DEFGSCODE(GIMPLE_OMP_FOR, "gimple_omp_for", GSS_OMP_FOR)
+/* GIMPLE_STRUCTURED_BLOCK <BODY> is an internal construct used to assert
+ that BODY is a structured block sequence, with no other semantics. It is
+ used to allow error-checking of intervening code in OMP_FOR constructs. */
+DEFGSCODE(GIMPLE_OMP_STRUCTURED_BLOCK, "gimple_omp_structured_block", GSS_OMP)
+
/* GIMPLE_OMP_MASTER <BODY> represents #pragma omp master.
BODY is the sequence of statements to execute in the master section. */
DEFGSCODE(GIMPLE_OMP_MASTER, "gimple_omp_master", GSS_OMP)
diff --git a/gcc/gimple.h b/gcc/gimple.h
index daf55242f68..69e840f28fe 100644
--- a/gcc/gimple.h
+++ b/gcc/gimple.h
@@ -1591,6 +1591,7 @@ gomp_parallel *gimple_build_omp_parallel (gimple_seq, tree, tree, tree);
gomp_task *gimple_build_omp_task (gimple_seq, tree, tree, tree, tree,
tree, tree);
gimple *gimple_build_omp_section (gimple_seq);
+gimple *gimple_build_omp_structured_block (gimple_seq);
gimple *gimple_build_omp_scope (gimple_seq, tree);
gimple *gimple_build_omp_master (gimple_seq);
gimple *gimple_build_omp_masked (gimple_seq, tree);
@@ -1879,6 +1880,7 @@ gimple_has_substatements (gimple *g)
case GIMPLE_OMP_TASKGROUP:
case GIMPLE_OMP_ORDERED:
case GIMPLE_OMP_SECTION:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
case GIMPLE_OMP_PARALLEL:
case GIMPLE_OMP_TASK:
case GIMPLE_OMP_SCOPE:
@@ -6723,6 +6725,7 @@ gimple_return_set_retval (greturn *gs, tree retval)
case GIMPLE_OMP_TEAMS: \
case GIMPLE_OMP_SCOPE: \
case GIMPLE_OMP_SECTION: \
+ case GIMPLE_OMP_STRUCTURED_BLOCK: \
case GIMPLE_OMP_MASTER: \
case GIMPLE_OMP_MASKED: \
case GIMPLE_OMP_TASKGROUP: \
diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index 36e5df050b9..264f5dcd5e4 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -5977,6 +5977,7 @@ is_gimple_stmt (tree t)
case OMP_SCOPE:
case OMP_SECTIONS:
case OMP_SECTION:
+ case OMP_STRUCTURED_BLOCK:
case OMP_SINGLE:
case OMP_MASTER:
case OMP_MASKED:
@@ -16995,6 +16996,7 @@ gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
break;
case OMP_SECTION:
+ case OMP_STRUCTURED_BLOCK:
case OMP_MASTER:
case OMP_MASKED:
case OMP_ORDERED:
@@ -17013,6 +17015,9 @@ gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
case OMP_SECTION:
g = gimple_build_omp_section (body);
break;
+ case OMP_STRUCTURED_BLOCK:
+ g = gimple_build_omp_structured_block (body);
+ break;
case OMP_MASTER:
g = gimple_build_omp_master (body);
break;
@@ -17413,6 +17418,7 @@ gimplify_expr (tree *expr_p, gimple_seq *pre_p, gimple_seq *post_p,
&& code != OMP_SCAN
&& code != OMP_SECTIONS
&& code != OMP_SECTION
+ && code != OMP_STRUCTURED_BLOCK
&& code != OMP_SINGLE
&& code != OMP_SCOPE);
}
diff --git a/gcc/omp-expand.cc b/gcc/omp-expand.cc
index db58b3cb49b..4a228a42a07 100644
--- a/gcc/omp-expand.cc
+++ b/gcc/omp-expand.cc
@@ -10592,6 +10592,11 @@ expand_omp (struct omp_region *region)
parent GIMPLE_OMP_SECTIONS region. */
break;
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
+ /* We should have gotten rid of these in gimple lowering. */
+ gcc_unreachable ();
+ break;
+
case GIMPLE_OMP_SINGLE:
case GIMPLE_OMP_SCOPE:
expand_omp_single (region);
diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index b882df048ef..00e6fb07cb6 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -4300,6 +4300,7 @@ scan_omp_1_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
}
/* FALLTHRU */
case GIMPLE_OMP_SECTION:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
case GIMPLE_OMP_MASTER:
case GIMPLE_OMP_ORDERED:
case GIMPLE_OMP_CRITICAL:
@@ -14499,6 +14500,14 @@ lower_omp_1 (gimple_stmt_iterator *gsi_p, omp_context *ctx)
gcc_assert (ctx);
lower_omp_single (gsi_p, ctx);
break;
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
+ /* We have already done error checking at this point, so these nodes
+ can be completely removed and replaced with their body. */
+ ctx = maybe_lookup_ctx (stmt);
+ gcc_assert (ctx);
+ lower_omp (gimple_omp_body_ptr (stmt), ctx);
+ gsi_replace_with_seq (gsi_p, gimple_omp_body (stmt), true);
+ break;
case GIMPLE_OMP_MASTER:
case GIMPLE_OMP_MASKED:
ctx = maybe_lookup_ctx (stmt);
@@ -14886,6 +14895,7 @@ diagnose_sb_1 (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
case GIMPLE_OMP_SECTIONS:
case GIMPLE_OMP_SINGLE:
case GIMPLE_OMP_SECTION:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
case GIMPLE_OMP_MASTER:
case GIMPLE_OMP_MASKED:
case GIMPLE_OMP_ORDERED:
@@ -14949,6 +14959,7 @@ diagnose_sb_2 (gimple_stmt_iterator *gsi_p, bool *handled_ops_p,
case GIMPLE_OMP_SECTIONS:
case GIMPLE_OMP_SINGLE:
case GIMPLE_OMP_SECTION:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
case GIMPLE_OMP_MASTER:
case GIMPLE_OMP_MASKED:
case GIMPLE_OMP_ORDERED:
diff --git a/gcc/tree-inline.cc b/gcc/tree-inline.cc
index 99efddc36c8..c408eeed053 100644
--- a/gcc/tree-inline.cc
+++ b/gcc/tree-inline.cc
@@ -1708,6 +1708,11 @@ remap_gimple_stmt (gimple *stmt, copy_body_data *id)
(s1, gimple_omp_sections_clauses (stmt));
break;
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
+ s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
+ copy = gimple_build_omp_structured_block (s1);
+ break;
+
case GIMPLE_OMP_SINGLE:
s1 = remap_gimple_seq (gimple_omp_body (stmt), id);
copy = gimple_build_omp_single
@@ -4561,6 +4566,7 @@ estimate_num_insns (gimple *stmt, eni_weights *weights)
case GIMPLE_OMP_SCAN:
case GIMPLE_OMP_SECTION:
case GIMPLE_OMP_SECTIONS:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
case GIMPLE_OMP_SINGLE:
case GIMPLE_OMP_TARGET:
case GIMPLE_OMP_TEAMS:
diff --git a/gcc/tree-nested.cc b/gcc/tree-nested.cc
index ae7d1f1f6a8..31c7b6001bd 100644
--- a/gcc/tree-nested.cc
+++ b/gcc/tree-nested.cc
@@ -1795,6 +1795,7 @@ convert_nonlocal_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
break;
case GIMPLE_OMP_SECTION:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
case GIMPLE_OMP_MASTER:
case GIMPLE_OMP_MASKED:
case GIMPLE_OMP_ORDERED:
@@ -2540,6 +2541,7 @@ convert_local_reference_stmt (gimple_stmt_iterator *gsi, bool *handled_ops_p,
break;
case GIMPLE_OMP_SECTION:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
case GIMPLE_OMP_MASTER:
case GIMPLE_OMP_MASKED:
case GIMPLE_OMP_ORDERED:
@@ -3050,6 +3052,7 @@ convert_gimple_call (gimple_stmt_iterator *gsi, bool *handled_ops_p,
/* FALLTHRU */
case GIMPLE_OMP_SECTIONS:
case GIMPLE_OMP_SECTION:
+ case GIMPLE_OMP_STRUCTURED_BLOCK:
case GIMPLE_OMP_SINGLE:
case GIMPLE_OMP_SCOPE:
case GIMPLE_OMP_MASTER:
diff --git a/gcc/tree-pretty-print.cc b/gcc/tree-pretty-print.cc
index 25d191b10fd..21a923897bb 100644
--- a/gcc/tree-pretty-print.cc
+++ b/gcc/tree-pretty-print.cc
@@ -3741,6 +3741,10 @@ dump_generic_node (pretty_printer *pp, tree node, int spc, dump_flags_t flags,
pp_string (pp, "#pragma omp section");
goto dump_omp_body;
+ case OMP_STRUCTURED_BLOCK:
+ pp_string (pp, "#pragma omp __structured_block");
+ goto dump_omp_body;
+
case OMP_SCAN:
if (OMP_SCAN_CLAUSES (node))
{
diff --git a/gcc/tree.def b/gcc/tree.def
index be94b7ece0a..abba3d764ae 100644
--- a/gcc/tree.def
+++ b/gcc/tree.def
@@ -1287,6 +1287,15 @@ DEFTREECODE (OMP_SCAN, "omp_scan", tcc_statement, 2)
Operand 0: OMP_SECTION_BODY: Section body. */
DEFTREECODE (OMP_SECTION, "omp_section", tcc_statement, 1)
+/* OpenMP structured block sequences that don't correspond to the body
+ another directive. This is used for code fragments within the body
+ of a directive that are separately required to be structured block
+ sequence; in particular, for intervening code sequences in
+ imperfectly-nested loops.
+ Operand 0: BODY: contains the statement(s) within the structured block
+ sequence. */
+DEFTREECODE (OMP_STRUCTURED_BLOCK, "omp_structured_block", tcc_statement, 1)
+
/* OpenMP - #pragma omp master
Operand 0: OMP_MASTER_BODY: Master section body. */
DEFTREECODE (OMP_MASTER, "omp_master", tcc_statement, 1)
diff --git a/gcc/tree.h b/gcc/tree.h
index fa02e2907a1..8431c5b9c6f 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -1530,6 +1530,9 @@ class auto_suppress_location_wrappers
#define OMP_SECTION_BODY(NODE) TREE_OPERAND (OMP_SECTION_CHECK (NODE), 0)
+#define OMP_STRUCTURED_BLOCK_BODY(NODE) \
+ TREE_OPERAND (OMP_STRUCTURED_BLOCK_CHECK (NODE), 0)
+
#define OMP_SINGLE_BODY(NODE) TREE_OPERAND (OMP_SINGLE_CHECK (NODE), 0)
#define OMP_SINGLE_CLAUSES(NODE) TREE_OPERAND (OMP_SINGLE_CHECK (NODE), 1)
--
2.31.1
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH V2 2/5] OpenMP: C front end support for imperfectly-nested loops
2023-07-23 22:15 [PATCH V2 0/5] OpenMP: support for imperfectly-nested loops Sandra Loosemore
2023-07-23 22:15 ` [PATCH V2 1/5] OpenMP: Add OMP_STRUCTURED_BLOCK and GIMPLE_OMP_STRUCTURED_BLOCK Sandra Loosemore
@ 2023-07-23 22:15 ` Sandra Loosemore
2023-08-22 13:23 ` Jakub Jelinek
2023-07-23 22:15 ` [PATCH V2 3/5] OpenMP: C++ " Sandra Loosemore
` (3 subsequent siblings)
5 siblings, 1 reply; 15+ messages in thread
From: Sandra Loosemore @ 2023-07-23 22:15 UTC (permalink / raw)
To: gcc-patches; +Cc: jakub
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.
New common C/C++ testcases are in a separate patch.
gcc/c-family/ChangeLog
* c-common.h (c_omp_check_loop_binding_exprs): Declare.
* c-omp.cc: Include tree-iterator.h.
(find_binding_in_body): New.
(check_loop_binding_expr_r): New.
(LOCATION_OR): New.
(check_looop_binding_expr): New.
(c_omp_check_loop_binding_exprs): New.
gcc/c/ChangeLog
* c-parser.cc (struct c_parser): Add omp_for_parse_state field.
(struct omp_for_parse_data): New.
(check_omp_intervening_code): New.
(add_structured_block_stmt): New.
(c_parser_compound_statement_nostart): Recognize intervening code,
nested loops, and other things that need special handling in
OpenMP loop constructs.
(c_parser_while_statement): Error on loop in intervening code.
(c_parser_do_statement): Likewise.
(c_parser_for_statement): Likewise.
(c_parser_postfix_expression_after_primary): Error on calls to
the OpenMP runtime in intervening code.
(c_parser_pragma): Error on OpenMP pragmas in intervening code.
(c_parser_omp_loop_nest): New.
(c_parser_omp_for_loop): Rewrite to use recursive descent, calling
c_parser_omp_loop_nest to do the heavy lifting.
gcc/ChangeLog
* omp-api.h: New.
* omp-general.cc (omp_runtime_api_procname): New.
(omp_runtime_api_call): Moved here from omp-low.cc, and make
non-static.
* omp-general.h: Include omp-api.h.
* omp-low.cc (omp_runtime_api_call): Delete this copy.
gcc/testsuite/ChangeLog
* c-c++-common/goacc/collapse-1.c: Update for new C error behavior.
* c-c++-common/goacc/tile-2.c: Likewise.
* gcc.dg/gomp/collapse-1.c: Likewise.
---
gcc/c-family/c-common.h | 1 +
gcc/c-family/c-omp.cc | 151 +++
gcc/c/c-parser.cc | 860 +++++++++++++-----
gcc/omp-api.h | 32 +
gcc/omp-general.cc | 134 +++
gcc/omp-general.h | 1 +
gcc/omp-low.cc | 129 ---
gcc/testsuite/c-c++-common/goacc/collapse-1.c | 16 +-
gcc/testsuite/c-c++-common/goacc/tile-2.c | 4 +-
gcc/testsuite/gcc.dg/gomp/collapse-1.c | 10 +-
10 files changed, 942 insertions(+), 396 deletions(-)
create mode 100644 gcc/omp-api.h
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index b5ef5ff6b2c..a05a4b54e1c 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -1296,6 +1296,7 @@ extern tree c_finish_omp_for (location_t, enum tree_code, tree, tree, tree,
extern bool c_omp_check_loop_iv (tree, tree, walk_tree_lh);
extern bool c_omp_check_loop_iv_exprs (location_t, enum tree_code, tree, int,
tree, tree, tree, walk_tree_lh);
+extern bool c_omp_check_loop_binding_exprs (tree, vec<tree> *);
extern tree c_finish_oacc_wait (location_t, tree, tree);
extern tree c_oacc_split_loop_clauses (tree, tree *, bool);
extern void c_omp_split_clauses (location_t, enum tree_code, omp_clause_mask,
diff --git a/gcc/c-family/c-omp.cc b/gcc/c-family/c-omp.cc
index 4faddb00bbc..9b7d7f789e3 100644
--- a/gcc/c-family/c-omp.cc
+++ b/gcc/c-family/c-omp.cc
@@ -36,6 +36,7 @@ along with GCC; see the file COPYING3. If not see
#include "gimplify.h"
#include "langhooks.h"
#include "bitmap.h"
+#include "tree-iterator.h"
/* Complete a #pragma oacc wait construct. LOC is the location of
@@ -1728,6 +1729,156 @@ c_omp_check_loop_iv_exprs (location_t stmt_loc, enum tree_code code,
return !data.fail;
}
+
+/* Helper function for c_omp_check_loop_binding_exprs: look for a binding
+ of DECL in BODY. Only traverse things that might be containers for
+ intervening code in an OMP loop. Returns the BIND_EXPR or DECL_EXPR
+ if found, otherwise null. */
+
+static tree
+find_binding_in_body (tree decl, tree body)
+{
+ if (!body)
+ return NULL_TREE;
+
+ switch (TREE_CODE (body))
+ {
+ case BIND_EXPR:
+ for (tree b = BIND_EXPR_VARS (body); b; b = DECL_CHAIN (b))
+ if (b == decl)
+ return body;
+ return find_binding_in_body (decl, BIND_EXPR_BODY (body));
+
+ case DECL_EXPR:
+ if (DECL_EXPR_DECL (body) == decl)
+ return body;
+ return NULL_TREE;
+
+ case STATEMENT_LIST:
+ for (tree_stmt_iterator si = tsi_start (body); !tsi_end_p (si);
+ tsi_next (&si))
+ {
+ tree b = find_binding_in_body (decl, tsi_stmt (si));
+ if (b)
+ return b;
+ }
+ return NULL_TREE;
+
+ case OMP_STRUCTURED_BLOCK:
+ return find_binding_in_body (decl, OMP_BODY (body));
+
+ default:
+ return NULL_TREE;
+ }
+}
+
+/* Traversal function for check_loop_binding_expr, to diagnose
+ errors when a binding made in intervening code is referenced outside
+ of the loop. Returns non-null if such a reference is found. DATA points
+ to the tree containing the loop body. */
+
+static tree
+check_loop_binding_expr_r (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
+ void *data)
+{
+ tree body = *(tree *)data;
+
+ if (DECL_P (*tp) && find_binding_in_body (*tp, body))
+ return *tp;
+ return NULL_TREE;
+}
+
+/* Helper macro used below. */
+
+#define LOCATION_OR(loc1, loc2) \
+ ((loc1) != UNKNOWN_LOCATION ? (loc1) : (loc2))
+
+/* Check a single expression EXPR for references to variables bound in
+ intervening code in BODY. Return true if ok, otherwise give an error
+ referencing CONTEXT and return false. Use LOC for the error message
+ if EXPR doesn't have one. */
+static bool
+check_loop_binding_expr (tree expr, tree body, const char *context,
+ location_t loc)
+{
+ tree bad = walk_tree (&expr, check_loop_binding_expr_r, (void *)&body, NULL);
+
+ if (bad)
+ {
+ location_t eloc = EXPR_LOCATION (expr);
+ error_at (LOCATION_OR (eloc, loc),
+ "variable %qD used %s is bound "
+ "in intervening code", bad, context);
+ return false;
+ }
+ return true;
+}
+
+/* STMT is an OMP_FOR construct. Check all of the iteration variable,
+ initializer, end condition, and increment for bindings inside the
+ loop body. If ORIG_INITS is provided, check those elements too.
+ Return true if OK, false otherwise. */
+bool
+c_omp_check_loop_binding_exprs (tree stmt, vec<tree> *orig_inits)
+{
+ bool ok = true;
+ location_t loc = EXPR_LOCATION (stmt);
+ tree body = OMP_FOR_BODY (stmt);
+ int orig_init_length = orig_inits ? orig_inits->length () : 0;
+
+ for (int i = 1; i < TREE_VEC_LENGTH (OMP_FOR_INIT (stmt)); i++)
+ {
+ tree init = TREE_VEC_ELT (OMP_FOR_INIT (stmt), i);
+ tree cond = TREE_VEC_ELT (OMP_FOR_COND (stmt), i);
+ tree incr = TREE_VEC_ELT (OMP_FOR_INCR (stmt), i);
+ gcc_assert (TREE_CODE (init) == MODIFY_EXPR);
+ tree decl = TREE_OPERAND (init, 0);
+ tree orig_init = i < orig_init_length ? (*orig_inits)[i] : NULL_TREE;
+ tree e;
+ location_t eloc;
+
+ e = TREE_OPERAND (init, 1);
+ eloc = LOCATION_OR (EXPR_LOCATION (init), loc);
+ if (!check_loop_binding_expr (decl, body, "as loop variable", eloc))
+ ok = false;
+ if (!check_loop_binding_expr (e, body, "in initializer", eloc))
+ ok = false;
+ if (orig_init
+ && !check_loop_binding_expr (orig_init, body,
+ "in initializer", eloc))
+ ok = false;
+
+ /* INCR and/or COND may be null if this is a template with a
+ class iterator. */
+ if (cond)
+ {
+ eloc = LOCATION_OR (EXPR_LOCATION (cond), loc);
+ if (COMPARISON_CLASS_P (cond) && TREE_OPERAND (cond, 0) == decl)
+ e = TREE_OPERAND (cond, 1);
+ else if (COMPARISON_CLASS_P (cond) && TREE_OPERAND (cond, 1) == decl)
+ e = TREE_OPERAND (cond, 0);
+ else
+ e = cond;
+ if (!check_loop_binding_expr (e, body, "in end test", eloc))
+ ok = false;
+ }
+
+ if (incr)
+ {
+ eloc = LOCATION_OR (EXPR_LOCATION (incr), loc);
+ /* INCR should be either a MODIFY_EXPR or pre/post
+ increment/decrement. We don't have to check the latter
+ since there are no operands besides the iteration variable. */
+ if (TREE_CODE (incr) == MODIFY_EXPR
+ && !check_loop_binding_expr (TREE_OPERAND (incr, 1), body,
+ "in increment expression", eloc))
+ ok = false;
+ }
+ }
+
+ return ok;
+}
+
/* This function splits clauses for OpenACC combined loop
constructs. OpenACC combined loop constructs are:
#pragma acc kernels loop
diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 24a6eb6e459..0fef6573431 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -249,6 +249,10 @@ struct GTY(()) c_parser {
/* Location of the last consumed token. */
location_t last_token_location;
+
+ /* Holds state for parsing collapsed OMP_FOR loops. Managed by
+ c_parser_omp_for_loop. */
+ struct omp_for_parse_data * GTY((skip)) omp_for_parse_state;
};
/* Return a pointer to the Nth token in PARSERs tokens_buf. */
@@ -1525,6 +1529,44 @@ struct oacc_routine_data {
/* Used for parsing objc foreach statements. */
static tree objc_foreach_break_label, objc_foreach_continue_label;
+/* Used for parsing OMP for loops.
+
+ Some notes on flags used for context:
+ parser->omp_for_parse_state is non-null anywhere inside the OMP FOR
+ construct, except for the final-loop-body.
+ The want_nested_loop flag is true if inside a {} sequence where
+ a loop-nest (or another {} sequence containing a loop-nest) is expected,
+ but has not yet been seen. It's false when parsing intervening code
+ statements or their substatements that cannot contain a loop-nest.
+ The in_intervening_code flag is true when parsing any intervening code,
+ including substatements, and whether or not want_nested_loop is true.
+
+ And, about error handling:
+ The saw_intervening_code flag is set if the loop is not perfectly
+ nested, even in the usual case where this is not an error.
+ perfect_nesting_fail is set if an error has been diagnosed because an
+ imperfectly-nested loop was found where a perfectly-nested one is
+ required (we diagnose this only once).
+ fail is set if any kind of structural error in the loop nest
+ has been found and diagnosed.
+ */
+struct omp_for_parse_data {
+ enum tree_code code;
+ tree declv, condv, incrv, initv;
+ tree pre_body;
+ tree bindings;
+ int count; /* Expected nesting depth. */
+ int depth; /* Current nesting depth. */
+ location_t for_loc;
+ bool ordered : 1;
+ bool inscan : 1;
+ bool want_nested_loop : 1;
+ bool in_intervening_code : 1;
+ bool saw_intervening_code: 1;
+ bool perfect_nesting_fail : 1;
+ bool fail : 1;
+};
+
static bool c_parser_nth_token_starts_std_attributes (c_parser *,
unsigned int);
static tree c_parser_std_attribute_specifier_sequence (c_parser *);
@@ -1616,6 +1658,7 @@ static void c_parser_omp_threadprivate (c_parser *);
static void c_parser_omp_barrier (c_parser *);
static void c_parser_omp_depobj (c_parser *);
static void c_parser_omp_flush (c_parser *);
+static tree c_parser_omp_loop_nest (c_parser *, bool *);
static tree c_parser_omp_for_loop (location_t, c_parser *, enum tree_code,
tree, tree *, bool *);
static void c_parser_omp_taskwait (c_parser *);
@@ -6128,6 +6171,68 @@ c_parser_compound_statement (c_parser *parser, location_t *endlocp)
return c_end_compound_stmt (brace_loc, stmt, true);
}
+/* Diagnose errors related to imperfectly nested loops in an OMP
+ loop construct. This function is called when such code is seen.
+ Only issue one such diagnostic no matter how much invalid
+ intervening code there is in the loop.
+ FIXME: maybe the location associated with the diagnostic should
+ be the current parser token instead of the location of the outer loop
+ nest. */
+
+static void
+check_omp_intervening_code (c_parser *parser)
+{
+ struct omp_for_parse_data *omp_for_parse_state = parser->omp_for_parse_state;
+ gcc_assert (omp_for_parse_state);
+
+ if (!omp_for_parse_state->in_intervening_code)
+ return;
+ omp_for_parse_state->saw_intervening_code = true;
+
+ /* Only diagnose errors related to perfect nesting once. */
+ if (!omp_for_parse_state->perfect_nesting_fail)
+ {
+
+ /* OpenACC does not (yet) permit intervening code, in
+ addition to situations forbidden by the OpenMP spec. */
+ if (omp_for_parse_state->code == OACC_LOOP)
+ {
+ error_at (omp_for_parse_state->for_loc,
+ "inner loops must be perfectly nested in "
+ "%<#pragma acc loop%>");
+ omp_for_parse_state->perfect_nesting_fail = true;
+ }
+ else if (omp_for_parse_state->ordered)
+ {
+ error_at (omp_for_parse_state->for_loc,
+ "inner loops must be perfectly nested with "
+ "%<ordered%> clause");
+ omp_for_parse_state->perfect_nesting_fail = true;
+ }
+ else if (omp_for_parse_state->inscan)
+ {
+ error_at (omp_for_parse_state->for_loc,
+ "inner loops must be perfectly nested with "
+ "%<reduction%> %<inscan%> clause");
+ omp_for_parse_state->perfect_nesting_fail = true;
+ }
+ /* TODO: Also reject loops with TILE directive. */
+ if (omp_for_parse_state->perfect_nesting_fail)
+ omp_for_parse_state->fail = true;
+ }
+}
+
+/* Helper function for below: wrap an OMP_STRUCTURED_BLOCK around SL
+ and add the statement to the current list. If SL is an empty statement
+ list, do nothing. */
+static void
+add_structured_block_stmt (tree sl)
+{
+ if (TREE_CODE (sl) != STATEMENT_LIST
+ || !tsi_end_p (tsi_start (sl)))
+ add_stmt (build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl));
+}
+
/* Parse a compound statement except for the opening brace. This is
used for parsing both compound statements and statement expressions
(which follow different paths to handling the opening). */
@@ -6139,6 +6244,12 @@ c_parser_compound_statement_nostart (c_parser *parser)
bool last_label = false;
bool save_valid_for_pragma = valid_location_for_stdc_pragma_p ();
location_t label_loc = UNKNOWN_LOCATION; /* Quiet warning. */
+ struct omp_for_parse_data *omp_for_parse_state
+ = parser->omp_for_parse_state;
+ bool in_omp_loop_block
+ = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
+ tree sl = NULL_TREE;
+
if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
{
location_t endloc = c_parser_peek_token (parser)->location;
@@ -6146,12 +6257,20 @@ c_parser_compound_statement_nostart (c_parser *parser)
c_parser_consume_token (parser);
return endloc;
}
+
+ /* If we're parsing a {} sequence in an OMP_FOR body, start a
+ statement list for intervening code. */
+ if (in_omp_loop_block)
+ sl = push_stmt_list ();
+
mark_valid_location_for_stdc_pragma (true);
if (c_parser_next_token_is_keyword (parser, RID_LABEL))
{
/* Read zero or more forward-declarations for labels that nested
functions can jump to. */
mark_valid_location_for_stdc_pragma (false);
+ if (in_omp_loop_block)
+ check_omp_intervening_code (parser);
while (c_parser_next_token_is_keyword (parser, RID_LABEL))
{
label_loc = c_parser_peek_token (parser)->location;
@@ -6193,6 +6312,76 @@ c_parser_compound_statement_nostart (c_parser *parser)
{
location_t loc = c_parser_peek_token (parser)->location;
loc = expansion_point_location_if_in_system_header (loc);
+
+ bool want_nested_loop = (omp_for_parse_state
+ ? omp_for_parse_state->want_nested_loop
+ : false);
+
+ /* First take care of special cases for OpenMP "canonical loop
+ nest form", that do not allow standard attributes, labels, or
+ __extension__ before the nested statement. */
+ if (in_omp_loop_block && !last_label)
+ {
+ if (want_nested_loop
+ && c_parser_next_token_is_keyword (parser, RID_FOR))
+ {
+ /* Found the next nested loop. If there were intervening
+ code statements collected before now, wrap them in an
+ OMP_STRUCTURED_BLOCK node, and start a new structured
+ block to hold statements that may come after the FOR. */
+ gcc_assert (sl);
+ add_structured_block_stmt (pop_stmt_list (sl));
+ omp_for_parse_state->depth++;
+ add_stmt (c_parser_omp_loop_nest (parser, NULL));
+ omp_for_parse_state->depth--;
+ sl = push_stmt_list ();
+ parser->error = false;
+ continue;
+ }
+ else if (want_nested_loop
+ && c_parser_next_token_is (parser, CPP_OPEN_BRACE))
+ {
+ /* If this nested compound statement contains the nested loop,
+ we need to separate the other statements in the current
+ statement into separate blocks of intervening code. If
+ there's no nested loop, it's all part of the same
+ chunk of intervening code. */
+ tree pre_sl = pop_stmt_list (sl);
+ tree nested_sl = push_stmt_list ();
+ mark_valid_location_for_stdc_pragma (false);
+ c_parser_statement_after_labels (parser, NULL);
+ nested_sl = pop_stmt_list (nested_sl);
+ if (omp_for_parse_state->want_nested_loop)
+ {
+ /* This block didn't contain a loop-nest, so it's
+ all part of the same chunk of intervening code. */
+ check_omp_intervening_code (parser);
+ sl = push_stmt_list ();
+ add_stmt (pre_sl);
+ add_stmt (nested_sl);
+ }
+ else
+ {
+ /* It contains the nested loop. */
+ add_structured_block_stmt (pre_sl);
+ add_stmt (nested_sl);
+ sl = push_stmt_list ();
+ }
+ parser->error = false;
+ continue;
+ }
+ else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
+ {
+ /* Prior to implementing the OpenMP 5.1 syntax for canonical
+ loop form, GCC used to accept an empty statements that
+ would now be flagged as intervening code. Continue to
+ do that, as an extension. */
+ /* FIXME: Maybe issue a warning or something here? */
+ c_parser_consume_token (parser);
+ continue;
+ }
+ }
+
/* Standard attributes may start a label, statement or declaration. */
bool have_std_attrs
= c_parser_nth_token_starts_std_attributes (parser, 1);
@@ -6211,6 +6400,8 @@ c_parser_compound_statement_nostart (c_parser *parser)
last_label = true;
last_stmt = false;
mark_valid_location_for_stdc_pragma (false);
+ if (in_omp_loop_block)
+ check_omp_intervening_code (parser);
c_parser_label (parser, std_attrs);
}
else if (c_parser_next_tokens_start_declaration (parser)
@@ -6221,7 +6412,13 @@ c_parser_compound_statement_nostart (c_parser *parser)
pedwarn_c11 (c_parser_peek_token (parser)->location, OPT_Wpedantic,
"a label can only be part of a statement and "
"a declaration is not a statement");
-
+ /* It's unlikely we'll see a nested loop in a declaration in
+ intervening code in an OMP loop, but disallow it anyway. */
+ if (in_omp_loop_block)
+ {
+ check_omp_intervening_code (parser);
+ omp_for_parse_state->want_nested_loop = false;
+ }
mark_valid_location_for_stdc_pragma (false);
bool fallthru_attr_p = false;
c_parser_declaration_or_fndef (parser, true, !have_std_attrs,
@@ -6229,6 +6426,8 @@ c_parser_compound_statement_nostart (c_parser *parser)
NULL, have_std_attrs, std_attrs,
NULL, &fallthru_attr_p);
+ if (in_omp_loop_block)
+ omp_for_parse_state->want_nested_loop = want_nested_loop;
if (last_stmt && !fallthru_attr_p)
pedwarn_c90 (loc, OPT_Wdeclaration_after_statement,
"ISO C90 forbids mixed declarations and code");
@@ -6256,9 +6455,18 @@ c_parser_compound_statement_nostart (c_parser *parser)
ext = disable_extension_diagnostics ();
c_parser_consume_token (parser);
last_label = false;
+ /* It's unlikely we'll see a nested loop in a declaration in
+ intervening code in an OMP loop, but disallow it anyway. */
+ if (in_omp_loop_block)
+ {
+ check_omp_intervening_code (parser);
+ omp_for_parse_state->want_nested_loop = false;
+ }
mark_valid_location_for_stdc_pragma (false);
c_parser_declaration_or_fndef (parser, true, true, true, true,
true);
+ if (in_omp_loop_block)
+ omp_for_parse_state->want_nested_loop = want_nested_loop;
/* Following the old parser, __extension__ does not
disable this diagnostic. */
restore_extension_diagnostics (ext);
@@ -6279,10 +6487,19 @@ c_parser_compound_statement_nostart (c_parser *parser)
syntactically. This ensures that the user doesn't put them
places that would turn into syntax errors if the directive
were ignored. */
+ if (omp_for_parse_state)
+ omp_for_parse_state->want_nested_loop = false;
if (c_parser_pragma (parser,
last_label ? pragma_stmt : pragma_compound,
NULL))
- last_label = false, last_stmt = true;
+ {
+ last_label = false;
+ last_stmt = true;
+ if (omp_for_parse_state)
+ check_omp_intervening_code (parser);
+ }
+ if (omp_for_parse_state)
+ omp_for_parse_state->want_nested_loop = want_nested_loop;
}
else if (c_parser_next_token_is (parser, CPP_EOF))
{
@@ -6312,7 +6529,20 @@ c_parser_compound_statement_nostart (c_parser *parser)
last_label = false;
last_stmt = true;
mark_valid_location_for_stdc_pragma (false);
- c_parser_statement_after_labels (parser, NULL);
+ if (!omp_for_parse_state)
+ c_parser_statement_after_labels (parser, NULL);
+ else
+ {
+ /* In canonical loop nest form, nested loops can only appear
+ directly, or in a directly nested compound statement. We
+ already took care of those cases above, so now we have
+ something else. This statement and everything inside
+ it must be intervening code. */
+ omp_for_parse_state->want_nested_loop = false;
+ check_omp_intervening_code (parser);
+ c_parser_statement_after_labels (parser, NULL);
+ omp_for_parse_state->want_nested_loop = want_nested_loop;
+ }
}
parser->error = false;
@@ -6321,8 +6551,21 @@ c_parser_compound_statement_nostart (c_parser *parser)
pedwarn_c11 (label_loc, OPT_Wpedantic, "label at end of compound statement");
location_t endloc = c_parser_peek_token (parser)->location;
c_parser_consume_token (parser);
+
/* Restore the value we started with. */
mark_valid_location_for_stdc_pragma (save_valid_for_pragma);
+
+ /* Package leftover intervening code, or the whole contents of the
+ compound statement if we were looking for a nested loop in an OMP_FOR
+ construct and didn't find one. */
+ if (sl)
+ {
+ sl = pop_stmt_list (sl);
+ if (omp_for_parse_state->want_nested_loop)
+ add_stmt (sl);
+ else
+ add_structured_block_stmt (sl);
+ }
return endloc;
}
@@ -7154,6 +7397,14 @@ c_parser_while_statement (c_parser *parser, bool ivdep, unsigned short unroll,
gcc_assert (c_parser_next_token_is_keyword (parser, RID_WHILE));
token_indent_info while_tinfo
= get_token_indent_info (c_parser_peek_token (parser));
+
+ if (parser->omp_for_parse_state)
+ {
+ error_at (c_parser_peek_token (parser)->location,
+ "loop not permitted in intervening code in OpenMP loop body");
+ parser->omp_for_parse_state->fail = true;
+ }
+
c_parser_consume_token (parser);
block = c_begin_compound_stmt (flag_isoc99);
loc = c_parser_peek_token (parser)->location;
@@ -7205,6 +7456,14 @@ c_parser_do_statement (c_parser *parser, bool ivdep, unsigned short unroll)
unsigned char save_in_statement;
location_t loc;
gcc_assert (c_parser_next_token_is_keyword (parser, RID_DO));
+
+ if (parser->omp_for_parse_state)
+ {
+ error_at (c_parser_peek_token (parser)->location,
+ "loop not permitted in intervening code in OpenMP loop body");
+ parser->omp_for_parse_state->fail = true;
+ }
+
c_parser_consume_token (parser);
if (c_parser_next_token_is (parser, CPP_SEMICOLON))
warning_at (c_parser_peek_token (parser)->location,
@@ -7311,6 +7570,14 @@ c_parser_for_statement (c_parser *parser, bool ivdep, unsigned short unroll,
gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
token_indent_info for_tinfo
= get_token_indent_info (c_parser_peek_token (parser));
+
+ if (parser->omp_for_parse_state)
+ {
+ error_at (for_loc,
+ "loop not permitted in intervening code in OpenMP loop body");
+ parser->omp_for_parse_state->fail = true;
+ }
+
c_parser_consume_token (parser);
/* Open a compound statement in Objective-C as well, just in case this is
as foreach expression. */
@@ -11250,6 +11517,14 @@ c_parser_postfix_expression_after_primary (c_parser *parser,
&& fndecl_built_in_p (expr.value, BUILT_IN_NORMAL)
&& vec_safe_length (exprlist) == 1)
warn_for_abs (expr_loc, expr.value, (*exprlist)[0]);
+ if (parser->omp_for_parse_state
+ && parser->omp_for_parse_state->in_intervening_code
+ && omp_runtime_api_call (expr.value))
+ {
+ error_at (expr_loc, "calls to the OpenMP runtime API are "
+ "not permitted in intervening code");
+ parser->omp_for_parse_state->fail = true;
+ }
}
start = expr.get_start ();
@@ -13084,6 +13359,17 @@ c_parser_pragma (c_parser *parser, enum pragma_context context, bool *if_p)
input_location = c_parser_peek_token (parser)->location;
id = c_parser_peek_token (parser)->pragma_kind;
gcc_assert (id != PRAGMA_NONE);
+ if (parser->omp_for_parse_state
+ && parser->omp_for_parse_state->in_intervening_code
+ && id >= PRAGMA_OMP__START_
+ && id <= PRAGMA_OMP__LAST_)
+ {
+ error_at (input_location,
+ "intervening code must not contain OpenMP directives");
+ parser->omp_for_parse_state->fail = true;
+ c_parser_skip_until_found (parser, CPP_PRAGMA_EOL, NULL);
+ return false;
+ }
switch (id)
{
@@ -20257,6 +20543,274 @@ c_parser_omp_scan_loop_body (c_parser *parser, bool open_brace_parsed)
"expected %<}%>");
}
+
+/* This function parses a single level of a loop nest, invoking itself
+ recursively if necessary.
+
+ loop-nest :: for (...) loop-body
+ loop-body :: loop-nest
+ | { [intervening-code] loop-body [intervening-code] }
+ | final-loop-body
+ intervening-code :: structured-block-sequence
+ final-loop-body :: structured-block
+
+ For a collapsed loop nest, only a single OMP_FOR is built, pulling out
+ all the iterator information from the inner loops into the
+ parser->omp_for_parse_state structure.
+
+ The iterator decl, init, cond, and incr are stored in vectors.
+
+ Initialization code for iterator variables is collected into
+ parser->omp_for_parse_state->pre_body and ends up inserted directly
+ into the OMP_FOR structure. */
+
+static tree
+c_parser_omp_loop_nest (c_parser *parser, bool *if_p)
+{
+ tree decl, cond, incr, init;
+ tree body = NULL_TREE;
+ matching_parens parens;
+ bool moreloops;
+ unsigned char save_in_statement;
+ tree loop_scope;
+ location_t loc;
+ struct omp_for_parse_data *omp_for_parse_state
+ = parser->omp_for_parse_state;
+ gcc_assert (omp_for_parse_state);
+ int depth = omp_for_parse_state->depth;
+
+ /* We have already matched the FOR token but not consumed it yet. */
+ loc = c_parser_peek_token (parser)->location;
+ gcc_assert (c_parser_next_token_is_keyword (parser, RID_FOR));
+ c_parser_consume_token (parser);
+
+ /* Forbid break/continue in the loop initializer, condition, and
+ increment expressions. */
+ save_in_statement = in_statement;
+ in_statement = IN_OMP_BLOCK;
+
+ /* We are not in intervening code now. */
+ omp_for_parse_state->in_intervening_code = false;
+
+ if (!parens.require_open (parser))
+ {
+ omp_for_parse_state->fail = true;
+ return NULL_TREE;
+ }
+
+ /* An implicit scope block surrounds each level of FOR loop, for
+ declarations of iteration variables at this loop depth. */
+ loop_scope = c_begin_compound_stmt (true);
+
+ /* Parse the initialization declaration or expression. */
+ if (c_parser_next_tokens_start_declaration (parser))
+ {
+ /* This is a declaration, which must be added to the pre_body code. */
+ tree this_pre_body = push_stmt_list ();
+ c_in_omp_for = true;
+ c_parser_declaration_or_fndef (parser, true, true, true, true, true);
+ c_in_omp_for = false;
+ this_pre_body = pop_stmt_list (this_pre_body);
+ append_to_statement_list_force (this_pre_body,
+ &(omp_for_parse_state->pre_body));
+ decl = check_for_loop_decls (omp_for_parse_state->for_loc, flag_isoc99);
+ if (decl == NULL)
+ goto error_init;
+ if (DECL_INITIAL (decl) == error_mark_node)
+ decl = error_mark_node;
+ init = decl;
+ }
+ else if (c_parser_next_token_is (parser, CPP_NAME)
+ && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
+ {
+ struct c_expr decl_exp;
+ struct c_expr init_exp;
+ location_t init_loc;
+
+ decl_exp = c_parser_postfix_expression (parser);
+ decl = decl_exp.value;
+
+ c_parser_require (parser, CPP_EQ, "expected %<=%>");
+
+ init_loc = c_parser_peek_token (parser)->location;
+ init_exp = c_parser_expr_no_commas (parser, NULL);
+ init_exp = default_function_array_read_conversion (init_loc,
+ init_exp);
+ c_in_omp_for = true;
+ init = build_modify_expr (init_loc, decl, decl_exp.original_type,
+ NOP_EXPR, init_loc, init_exp.value,
+ init_exp.original_type);
+ c_in_omp_for = false;
+ init = c_process_expr_stmt (init_loc, init);
+
+ c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
+ }
+ else
+ {
+ error_init:
+ c_parser_error (parser,
+ "expected iteration declaration or initialization");
+ c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
+ "expected %<)%>");
+ omp_for_parse_state->fail = true;
+ goto parse_next;
+ }
+
+ /* Parse the loop condition. */
+ cond = NULL_TREE;
+ if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
+ {
+ location_t cond_loc = c_parser_peek_token (parser)->location;
+ c_in_omp_for = true;
+ struct c_expr cond_expr
+ = c_parser_binary_expression (parser, NULL, NULL_TREE);
+ c_in_omp_for = false;
+
+ cond = cond_expr.value;
+ cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
+ switch (cond_expr.original_code)
+ {
+ case GT_EXPR:
+ case GE_EXPR:
+ case LT_EXPR:
+ case LE_EXPR:
+ break;
+ case NE_EXPR:
+ if (omp_for_parse_state->code != OACC_LOOP)
+ break;
+ /* FALLTHRU. */
+ default:
+ /* Can't be cond = error_mark_node, because we want to preserve
+ the location until c_finish_omp_for. */
+ cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
+ break;
+ }
+ protected_set_expr_location (cond, cond_loc);
+ }
+ c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
+
+ /* Parse the increment expression. */
+ incr = NULL_TREE;
+ if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
+ {
+ location_t incr_loc = c_parser_peek_token (parser)->location;
+
+ incr = c_process_expr_stmt (incr_loc,
+ c_parser_expression (parser).value);
+ }
+ parens.skip_until_found_close (parser);
+
+ if (decl == NULL || decl == error_mark_node || init == error_mark_node)
+ omp_for_parse_state->fail = true;
+ else
+ {
+ TREE_VEC_ELT (omp_for_parse_state->declv, depth) = decl;
+ TREE_VEC_ELT (omp_for_parse_state->initv, depth) = init;
+ TREE_VEC_ELT (omp_for_parse_state->condv, depth) = cond;
+ TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = incr;
+ }
+
+parse_next:
+ moreloops = depth < omp_for_parse_state->count - 1;
+ omp_for_parse_state->want_nested_loop = moreloops;
+ if (moreloops && c_parser_next_token_is_keyword (parser, RID_FOR))
+ {
+ omp_for_parse_state->depth++;
+ body = c_parser_omp_loop_nest (parser, if_p);
+ omp_for_parse_state->depth--;
+ }
+ else if (moreloops && c_parser_next_token_is (parser, CPP_OPEN_BRACE))
+ {
+ /* This is the open brace in the loop-body grammar production. Rather
+ than trying to special-case braces, just parse it as a compound
+ statement and handle the nested loop-body case there. Note that
+ when we see a further open brace inside the compound statement
+ loop-body, we don't know whether it is the start of intervening
+ code that is a compound statement, or a level of braces
+ surrounding a nested loop-body. Use the WANT_NESTED_LOOP state
+ bit to ensure we have only one nested loop at each level. */
+ omp_for_parse_state->in_intervening_code = true;
+ body = c_parser_compound_statement (parser, NULL);
+ omp_for_parse_state->in_intervening_code = false;
+ if (omp_for_parse_state->want_nested_loop)
+ {
+ /* We have already parsed the whole loop body and not found a
+ nested loop. */
+ error_at (omp_for_parse_state->for_loc,
+ "not enough nested loops");
+ omp_for_parse_state->fail = true;
+ }
+ if_p = NULL;
+ }
+ else
+ {
+ /* This is the final-loop-body case in the grammar: we have
+ something that is not a FOR and not an open brace. */
+ if (moreloops)
+ {
+ /* If we were expecting a nested loop, give an error and mark
+ that parsing has failed, and try to recover by parsing the
+ body as regular code without further collapsing. */
+ error_at (omp_for_parse_state->for_loc,
+ "not enough nested loops");
+ omp_for_parse_state->fail = true;
+ }
+ in_statement = IN_OMP_FOR;
+ parser->omp_for_parse_state = NULL;
+ body = push_stmt_list ();
+ if (omp_for_parse_state->inscan)
+ c_parser_omp_scan_loop_body (parser, false);
+ else
+ add_stmt (c_parser_c99_block_statement (parser, if_p));
+ body = pop_stmt_list (body);
+ parser->omp_for_parse_state = omp_for_parse_state;
+ }
+ in_statement = save_in_statement;
+ omp_for_parse_state->want_nested_loop = false;
+ omp_for_parse_state->in_intervening_code = true;
+
+ /* Pop and return the implicit scope surrounding this level of loop.
+ If the iteration variable at this depth was bound in the for loop,
+ pull out and save the binding. Later in c_parser_omp_for_loop,
+ these bindings will be moved to the scope surrounding the entire
+ OMP_FOR. That keeps the gimplifier happy later on, and meanwhile
+ we have already resolved all references to the iteration variable
+ in its true scope. */
+ add_stmt (body);
+ body = c_end_compound_stmt (loc, loop_scope, true);
+ if (decl && TREE_CODE (body) == BIND_EXPR)
+ {
+ tree t = BIND_EXPR_VARS (body);
+ tree prev = NULL_TREE, next = NULL_TREE;
+ while (t)
+ {
+ next = DECL_CHAIN (t);
+ if (t == decl)
+ {
+ if (prev)
+ DECL_CHAIN (prev) = next;
+ else
+ {
+ BIND_EXPR_VARS (body) = next;
+ BLOCK_VARS (BIND_EXPR_BLOCK (body)) = next;
+ }
+ DECL_CHAIN (t) = omp_for_parse_state->bindings;
+ omp_for_parse_state->bindings = t;
+ break;
+ }
+ else
+ {
+ prev = t;
+ t = next;
+ }
+ }
+ if (BIND_EXPR_VARS (body) == NULL_TREE)
+ body = BIND_EXPR_BODY (body);
+ }
+
+ return body;
+}
+
/* Parse the restricted form of loop statements allowed by OpenACC and OpenMP.
The real trick here is to determine the loop control variable early
so that we can push a new decl if necessary to make it private.
@@ -20267,17 +20821,14 @@ static tree
c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
tree clauses, tree *cclauses, bool *if_p)
{
- tree decl, cond, incr, body, init, stmt, cl;
- unsigned char save_in_statement;
- tree declv, condv, incrv, initv, ret = NULL_TREE;
- tree pre_body = NULL_TREE, this_pre_body;
+ tree body, stmt, cl;
+ tree ret = NULL_TREE;
tree ordered_cl = NULL_TREE;
- bool fail = false, open_brace_parsed = false;
- int i, collapse = 1, ordered = 0, count, nbraces = 0;
- location_t for_loc;
+ int i, collapse = 1, ordered = 0, count;
bool tiling = false;
bool inscan = false;
- vec<tree, va_gc> *for_block = make_tree_vector ();
+ struct omp_for_parse_data data;
+ struct omp_for_parse_data *save_data = parser->omp_for_parse_state;
for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
@@ -20310,250 +20861,62 @@ c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
count = ordered ? ordered : collapse;
- declv = make_tree_vec (count);
- initv = make_tree_vec (count);
- condv = make_tree_vec (count);
- incrv = make_tree_vec (count);
-
if (!c_parser_next_token_is_keyword (parser, RID_FOR))
{
c_parser_error (parser, "for statement expected");
return NULL;
}
- for_loc = c_parser_peek_token (parser)->location;
- c_parser_consume_token (parser);
-
- /* Forbid break/continue in the loop initializer, condition, and
- increment expressions. */
- save_in_statement = in_statement;
- in_statement = IN_OMP_BLOCK;
-
- for (i = 0; i < count; i++)
- {
- int bracecount = 0;
-
- matching_parens parens;
- if (!parens.require_open (parser))
- goto pop_scopes;
-
- /* Parse the initialization declaration or expression. */
- if (c_parser_next_tokens_start_declaration (parser))
- {
- if (i > 0)
- vec_safe_push (for_block, c_begin_compound_stmt (true));
- this_pre_body = push_stmt_list ();
- c_in_omp_for = true;
- c_parser_declaration_or_fndef (parser, true, true, true, true, true);
- c_in_omp_for = false;
- if (this_pre_body)
- {
- this_pre_body = pop_stmt_list (this_pre_body);
- if (pre_body)
- {
- tree t = pre_body;
- pre_body = push_stmt_list ();
- add_stmt (t);
- add_stmt (this_pre_body);
- pre_body = pop_stmt_list (pre_body);
- }
- else
- pre_body = this_pre_body;
- }
- decl = check_for_loop_decls (for_loc, flag_isoc99);
- if (decl == NULL)
- goto error_init;
- if (DECL_INITIAL (decl) == error_mark_node)
- decl = error_mark_node;
- init = decl;
- }
- else if (c_parser_next_token_is (parser, CPP_NAME)
- && c_parser_peek_2nd_token (parser)->type == CPP_EQ)
- {
- struct c_expr decl_exp;
- struct c_expr init_exp;
- location_t init_loc;
-
- decl_exp = c_parser_postfix_expression (parser);
- decl = decl_exp.value;
-
- c_parser_require (parser, CPP_EQ, "expected %<=%>");
-
- init_loc = c_parser_peek_token (parser)->location;
- init_exp = c_parser_expr_no_commas (parser, NULL);
- init_exp = default_function_array_read_conversion (init_loc,
- init_exp);
- c_in_omp_for = true;
- init = build_modify_expr (init_loc, decl, decl_exp.original_type,
- NOP_EXPR, init_loc, init_exp.value,
- init_exp.original_type);
- c_in_omp_for = false;
- init = c_process_expr_stmt (init_loc, init);
-
- c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
- }
- else
- {
- error_init:
- c_parser_error (parser,
- "expected iteration declaration or initialization");
- c_parser_skip_until_found (parser, CPP_CLOSE_PAREN,
- "expected %<)%>");
- fail = true;
- goto parse_next;
- }
-
- /* Parse the loop condition. */
- cond = NULL_TREE;
- if (c_parser_next_token_is_not (parser, CPP_SEMICOLON))
- {
- location_t cond_loc = c_parser_peek_token (parser)->location;
- c_in_omp_for = true;
- struct c_expr cond_expr
- = c_parser_binary_expression (parser, NULL, NULL_TREE);
- c_in_omp_for = false;
-
- cond = cond_expr.value;
- cond = c_objc_common_truthvalue_conversion (cond_loc, cond);
- switch (cond_expr.original_code)
- {
- case GT_EXPR:
- case GE_EXPR:
- case LT_EXPR:
- case LE_EXPR:
- break;
- case NE_EXPR:
- if (code != OACC_LOOP)
- break;
- /* FALLTHRU. */
- default:
- /* Can't be cond = error_mark_node, because we want to preserve
- the location until c_finish_omp_for. */
- cond = build1 (NOP_EXPR, boolean_type_node, error_mark_node);
- break;
- }
- protected_set_expr_location (cond, cond_loc);
- }
- c_parser_skip_until_found (parser, CPP_SEMICOLON, "expected %<;%>");
-
- /* Parse the increment expression. */
- incr = NULL_TREE;
- if (c_parser_next_token_is_not (parser, CPP_CLOSE_PAREN))
- {
- location_t incr_loc = c_parser_peek_token (parser)->location;
-
- incr = c_process_expr_stmt (incr_loc,
- c_parser_expression (parser).value);
- }
- parens.skip_until_found_close (parser);
-
- if (decl == NULL || decl == error_mark_node || init == error_mark_node)
- fail = true;
- else
- {
- TREE_VEC_ELT (declv, i) = decl;
- TREE_VEC_ELT (initv, i) = init;
- TREE_VEC_ELT (condv, i) = cond;
- TREE_VEC_ELT (incrv, i) = incr;
- }
-
- parse_next:
- if (i == count - 1)
- break;
-
- /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
- in between the collapsed for loops to be still considered perfectly
- nested. Hopefully the final version clarifies this.
- For now handle (multiple) {'s and empty statements. */
- do
- {
- if (c_parser_next_token_is_keyword (parser, RID_FOR))
- {
- c_parser_consume_token (parser);
- break;
- }
- else if (c_parser_next_token_is (parser, CPP_OPEN_BRACE))
- {
- c_parser_consume_token (parser);
- bracecount++;
- }
- else if (bracecount
- && c_parser_next_token_is (parser, CPP_SEMICOLON))
- c_parser_consume_token (parser);
- else
- {
- c_parser_error (parser, "not enough perfectly nested loops");
- if (bracecount)
- {
- open_brace_parsed = true;
- bracecount--;
- }
- fail = true;
- count = 0;
- break;
- }
- }
- while (1);
-
- nbraces += bracecount;
- }
-
- if (nbraces)
- if_p = NULL;
- in_statement = IN_OMP_FOR;
- body = push_stmt_list ();
-
- if (inscan)
- c_parser_omp_scan_loop_body (parser, open_brace_parsed);
- else if (open_brace_parsed)
- {
- location_t here = c_parser_peek_token (parser)->location;
- stmt = c_begin_compound_stmt (true);
- c_parser_compound_statement_nostart (parser);
- add_stmt (c_end_compound_stmt (here, stmt, true));
- }
- else
- add_stmt (c_parser_c99_block_statement (parser, if_p));
-
- body = pop_stmt_list (body);
- in_statement = save_in_statement;
-
- while (nbraces)
- {
- if (c_parser_next_token_is (parser, CPP_CLOSE_BRACE))
- {
- c_parser_consume_token (parser);
- nbraces--;
- }
- else if (c_parser_next_token_is (parser, CPP_SEMICOLON))
- c_parser_consume_token (parser);
- else
- {
- c_parser_error (parser, "collapsed loops not perfectly nested");
- while (nbraces)
- {
- location_t here = c_parser_peek_token (parser)->location;
- stmt = c_begin_compound_stmt (true);
- add_stmt (body);
- c_parser_compound_statement_nostart (parser);
- body = c_end_compound_stmt (here, stmt, true);
- nbraces--;
- }
- goto pop_scopes;
- }
+ /* Initialize parse state for recursive descent. */
+ data.declv = make_tree_vec (count);
+ data.initv = make_tree_vec (count);
+ data.condv = make_tree_vec (count);
+ data.incrv = make_tree_vec (count);
+ data.pre_body = NULL_TREE;;
+ data.bindings = NULL_TREE;
+ data.for_loc = c_parser_peek_token (parser)->location;
+ data.count = count;
+ data.depth = 0;
+ data.want_nested_loop = true;
+ data.ordered = ordered > 0;
+ data.in_intervening_code = false;
+ data.perfect_nesting_fail = false;
+ data.fail = false;
+ data.inscan = inscan;
+ data.saw_intervening_code = false;
+ data.code = code;
+ parser->omp_for_parse_state = &data;
+
+ body = c_parser_omp_loop_nest (parser, if_p);
+
+ /* Add saved bindings for iteration variables that were declared in
+ the nested for loop to the scope surrounding the entire loop. */
+ for (tree t = data.bindings; t; )
+ {
+ tree n = TREE_CHAIN (t);
+ TREE_CHAIN (t) = NULL_TREE;
+ pushdecl (t);
+ t = n;
}
/* Only bother calling c_finish_omp_for if we haven't already generated
an error from the initialization parsing. */
- if (!fail)
+ if (!data.fail)
{
c_in_omp_for = true;
- stmt = c_finish_omp_for (loc, code, declv, NULL, initv, condv,
- incrv, body, pre_body, true);
+ stmt = c_finish_omp_for (loc, code, data.declv, NULL, data.initv,
+ data.condv, data.incrv,
+ body, data.pre_body, true);
c_in_omp_for = false;
/* Check for iterators appearing in lb, b or incr expressions. */
- if (stmt && !c_omp_check_loop_iv (stmt, declv, NULL))
+ if (stmt && !c_omp_check_loop_iv (stmt, data.declv, NULL))
+ stmt = NULL_TREE;
+
+ /* Check for errors involving lb/ub/incr expressions referencing
+ variables declared in intervening code. */
+ if (data.saw_intervening_code
+ && !c_omp_check_loop_binding_exprs (stmt, NULL))
stmt = NULL_TREE;
if (stmt)
@@ -20605,7 +20968,7 @@ c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
else
{
for (i = 0; i < count; i++)
- if (TREE_VEC_ELT (declv, i) == OMP_CLAUSE_DECL (*c))
+ if (TREE_VEC_ELT (data.declv, i) == OMP_CLAUSE_DECL (*c))
break;
if (i == count)
c = &OMP_CLAUSE_CHAIN (*c);
@@ -20618,7 +20981,8 @@ c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
}
else
{
- /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
+ /* Move lastprivate (decl) clause to
+ OMP_FOR_CLAUSES. */
tree l = *c;
*c = OMP_CLAUSE_CHAIN (*c);
if (code == OMP_SIMD)
@@ -20639,16 +21003,8 @@ c_parser_omp_for_loop (location_t loc, c_parser *parser, enum tree_code code,
}
ret = stmt;
}
-pop_scopes:
- while (!for_block->is_empty ())
- {
- /* FIXME diagnostics: LOC below should be the actual location of
- this particular for block. We need to build a list of
- locations to go along with FOR_BLOCK. */
- stmt = c_end_compound_stmt (loc, for_block->pop (), true);
- add_stmt (stmt);
- }
- release_tree_vector (for_block);
+
+ parser->omp_for_parse_state = save_data;
return ret;
}
diff --git a/gcc/omp-api.h b/gcc/omp-api.h
new file mode 100644
index 00000000000..2a7ec7b72a6
--- /dev/null
+++ b/gcc/omp-api.h
@@ -0,0 +1,32 @@
+/* Functions for querying whether a function name is reserved by the
+ OpenMP API. This is used for error checking.
+
+ Copyright (C) 2023 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3. If not see
+<http://www.gnu.org/licenses/>. */
+
+#ifndef GCC_OMP_API_H
+#define GCC_OMP_API_H
+
+#include "coretypes.h"
+
+/* In omp-general.cc, but declared in a separate header file for
+ convenience of the Fortran front end. */
+extern bool omp_runtime_api_procname (const char *name);
+extern bool omp_runtime_api_call (const_tree fndecl);
+
+#endif
diff --git a/gcc/omp-general.cc b/gcc/omp-general.cc
index eefdcb54590..1e31014c454 100644
--- a/gcc/omp-general.cc
+++ b/gcc/omp-general.cc
@@ -3013,4 +3013,138 @@ omp_build_component_ref (tree obj, tree field)
return ret;
}
+/* Return true if NAME is the name of an omp_* runtime API call. */
+bool
+omp_runtime_api_procname (const char *name)
+{
+ if (!startswith (name, "omp_"))
+ return false;
+
+ static const char *omp_runtime_apis[] =
+ {
+ /* This array has 3 sections. First omp_* calls that don't
+ have any suffixes. */
+ "aligned_alloc",
+ "aligned_calloc",
+ "alloc",
+ "calloc",
+ "free",
+ "get_mapped_ptr",
+ "realloc",
+ "target_alloc",
+ "target_associate_ptr",
+ "target_disassociate_ptr",
+ "target_free",
+ "target_is_accessible",
+ "target_is_present",
+ "target_memcpy",
+ "target_memcpy_async",
+ "target_memcpy_rect",
+ "target_memcpy_rect_async",
+ NULL,
+ /* Now omp_* calls that are available as omp_* and omp_*_; however, the
+ DECL_NAME is always omp_* without tailing underscore. */
+ "capture_affinity",
+ "destroy_allocator",
+ "destroy_lock",
+ "destroy_nest_lock",
+ "display_affinity",
+ "fulfill_event",
+ "get_active_level",
+ "get_affinity_format",
+ "get_cancellation",
+ "get_default_allocator",
+ "get_default_device",
+ "get_device_num",
+ "get_dynamic",
+ "get_initial_device",
+ "get_level",
+ "get_max_active_levels",
+ "get_max_task_priority",
+ "get_max_teams",
+ "get_max_threads",
+ "get_nested",
+ "get_num_devices",
+ "get_num_places",
+ "get_num_procs",
+ "get_num_teams",
+ "get_num_threads",
+ "get_partition_num_places",
+ "get_place_num",
+ "get_proc_bind",
+ "get_supported_active_levels",
+ "get_team_num",
+ "get_teams_thread_limit",
+ "get_thread_limit",
+ "get_thread_num",
+ "get_wtick",
+ "get_wtime",
+ "in_explicit_task",
+ "in_final",
+ "in_parallel",
+ "init_lock",
+ "init_nest_lock",
+ "is_initial_device",
+ "pause_resource",
+ "pause_resource_all",
+ "set_affinity_format",
+ "set_default_allocator",
+ "set_lock",
+ "set_nest_lock",
+ "test_lock",
+ "test_nest_lock",
+ "unset_lock",
+ "unset_nest_lock",
+ NULL,
+ /* And finally calls available as omp_*, omp_*_ and omp_*_8_; however,
+ as DECL_NAME only omp_* and omp_*_8 appear. */
+ "display_env",
+ "get_ancestor_thread_num",
+ "init_allocator",
+ "get_partition_place_nums",
+ "get_place_num_procs",
+ "get_place_proc_ids",
+ "get_schedule",
+ "get_team_size",
+ "set_default_device",
+ "set_dynamic",
+ "set_max_active_levels",
+ "set_nested",
+ "set_num_teams",
+ "set_num_threads",
+ "set_schedule",
+ "set_teams_thread_limit"
+ };
+
+ int mode = 0;
+ for (unsigned i = 0; i < ARRAY_SIZE (omp_runtime_apis); i++)
+ {
+ if (omp_runtime_apis[i] == NULL)
+ {
+ mode++;
+ continue;
+ }
+ size_t len = strlen (omp_runtime_apis[i]);
+ if (strncmp (name + 4, omp_runtime_apis[i], len) == 0
+ && (name[4 + len] == '\0'
+ || (mode > 1 && strcmp (name + 4 + len, "_8") == 0)))
+ return true;
+ }
+ return false;
+}
+
+/* Return true if FNDECL is an omp_* runtime API call. */
+
+bool
+omp_runtime_api_call (const_tree fndecl)
+{
+ tree declname = DECL_NAME (fndecl);
+ if (!declname
+ || (DECL_CONTEXT (fndecl) != NULL_TREE
+ && TREE_CODE (DECL_CONTEXT (fndecl)) != TRANSLATION_UNIT_DECL)
+ || !TREE_PUBLIC (fndecl))
+ return false;
+ return omp_runtime_api_procname (IDENTIFIER_POINTER (declname));
+}
+
#include "gt-omp-general.h"
diff --git a/gcc/omp-general.h b/gcc/omp-general.h
index 92717db1628..1a52bfdb56b 100644
--- a/gcc/omp-general.h
+++ b/gcc/omp-general.h
@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. If not see
#define GCC_OMP_GENERAL_H
#include "gomp-constants.h"
+#include "omp-api.h"
/* Flags for an OpenACC loop. */
diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc
index 00e6fb07cb6..5d7c32dac39 100644
--- a/gcc/omp-low.cc
+++ b/gcc/omp-low.cc
@@ -4009,135 +4009,6 @@ setjmp_or_longjmp_p (const_tree fndecl)
return !strcmp (name, "setjmp") || !strcmp (name, "longjmp");
}
-/* Return true if FNDECL is an omp_* runtime API call. */
-
-static bool
-omp_runtime_api_call (const_tree fndecl)
-{
- tree declname = DECL_NAME (fndecl);
- if (!declname
- || (DECL_CONTEXT (fndecl) != NULL_TREE
- && TREE_CODE (DECL_CONTEXT (fndecl)) != TRANSLATION_UNIT_DECL)
- || !TREE_PUBLIC (fndecl))
- return false;
-
- const char *name = IDENTIFIER_POINTER (declname);
- if (!startswith (name, "omp_"))
- return false;
-
- static const char *omp_runtime_apis[] =
- {
- /* This array has 3 sections. First omp_* calls that don't
- have any suffixes. */
- "aligned_alloc",
- "aligned_calloc",
- "alloc",
- "calloc",
- "free",
- "get_mapped_ptr",
- "realloc",
- "target_alloc",
- "target_associate_ptr",
- "target_disassociate_ptr",
- "target_free",
- "target_is_accessible",
- "target_is_present",
- "target_memcpy",
- "target_memcpy_async",
- "target_memcpy_rect",
- "target_memcpy_rect_async",
- NULL,
- /* Now omp_* calls that are available as omp_* and omp_*_; however, the
- DECL_NAME is always omp_* without tailing underscore. */
- "capture_affinity",
- "destroy_allocator",
- "destroy_lock",
- "destroy_nest_lock",
- "display_affinity",
- "fulfill_event",
- "get_active_level",
- "get_affinity_format",
- "get_cancellation",
- "get_default_allocator",
- "get_default_device",
- "get_device_num",
- "get_dynamic",
- "get_initial_device",
- "get_level",
- "get_max_active_levels",
- "get_max_task_priority",
- "get_max_teams",
- "get_max_threads",
- "get_nested",
- "get_num_devices",
- "get_num_places",
- "get_num_procs",
- "get_num_teams",
- "get_num_threads",
- "get_partition_num_places",
- "get_place_num",
- "get_proc_bind",
- "get_supported_active_levels",
- "get_team_num",
- "get_teams_thread_limit",
- "get_thread_limit",
- "get_thread_num",
- "get_wtick",
- "get_wtime",
- "in_explicit_task",
- "in_final",
- "in_parallel",
- "init_lock",
- "init_nest_lock",
- "is_initial_device",
- "pause_resource",
- "pause_resource_all",
- "set_affinity_format",
- "set_default_allocator",
- "set_lock",
- "set_nest_lock",
- "test_lock",
- "test_nest_lock",
- "unset_lock",
- "unset_nest_lock",
- NULL,
- /* And finally calls available as omp_*, omp_*_ and omp_*_8_; however,
- as DECL_NAME only omp_* and omp_*_8 appear. */
- "display_env",
- "get_ancestor_thread_num",
- "init_allocator",
- "get_partition_place_nums",
- "get_place_num_procs",
- "get_place_proc_ids",
- "get_schedule",
- "get_team_size",
- "set_default_device",
- "set_dynamic",
- "set_max_active_levels",
- "set_nested",
- "set_num_teams",
- "set_num_threads",
- "set_schedule",
- "set_teams_thread_limit"
- };
-
- int mode = 0;
- for (unsigned i = 0; i < ARRAY_SIZE (omp_runtime_apis); i++)
- {
- if (omp_runtime_apis[i] == NULL)
- {
- mode++;
- continue;
- }
- size_t len = strlen (omp_runtime_apis[i]);
- if (strncmp (name + 4, omp_runtime_apis[i], len) == 0
- && (name[4 + len] == '\0'
- || (mode > 1 && strcmp (name + 4 + len, "_8") == 0)))
- return true;
- }
- return false;
-}
-
/* Helper function for scan_omp.
Callback for walk_gimple_stmt used to scan for OMP directives in
diff --git a/gcc/testsuite/c-c++-common/goacc/collapse-1.c b/gcc/testsuite/c-c++-common/goacc/collapse-1.c
index 11b14383983..0feac8f8ddb 100644
--- a/gcc/testsuite/c-c++-common/goacc/collapse-1.c
+++ b/gcc/testsuite/c-c++-common/goacc/collapse-1.c
@@ -8,8 +8,8 @@ f1 (void)
{
#pragma acc parallel
#pragma acc loop collapse (2)
- for (i = 0; i < 5; i++)
- ; /* { dg-error "not enough perfectly nested" } */
+ for (i = 0; i < 5; i++) /* { dg-error "not enough nested loops" } */
+ ;
{
for (j = 0; j < 5; j++)
;
@@ -38,9 +38,9 @@ f3 (void)
{
#pragma acc parallel
#pragma acc loop collapse (2)
- for (i = 0; i < 5; i++)
+ for (i = 0; i < 5; i++) /* { dg-error "inner loops must be perfectly nested" } */
{
- int k = foo (); /* { dg-error "not enough perfectly nested" } */
+ int k = foo ();
{
{
for (j = 0; j < 5; j++)
@@ -56,12 +56,12 @@ f4 (void)
{
#pragma acc parallel
#pragma acc loop collapse (2)
- for (i = 0; i < 5; i++)
+ for (i = 0; i < 5; i++) /* { dg-error "inner loops must be perfectly nested" } */
{
{
for (j = 0; j < 5; j++)
;
- foo (); /* { dg-error "collapsed loops not perfectly nested before" } */
+ foo ();
}
}
}
@@ -71,13 +71,13 @@ f5 (void)
{
#pragma acc parallel
#pragma acc loop collapse (2)
- for (i = 0; i < 5; i++)
+ for (i = 0; i < 5; i++) /* { dg-error "inner loops must be perfectly nested" } */
{
{
for (j = 0; j < 5; j++)
;
}
- foo (); /* { dg-error "collapsed loops not perfectly nested before" } */
+ foo ();
}
}
diff --git a/gcc/testsuite/c-c++-common/goacc/tile-2.c b/gcc/testsuite/c-c++-common/goacc/tile-2.c
index c8b240d225b..98abc903bdc 100644
--- a/gcc/testsuite/c-c++-common/goacc/tile-2.c
+++ b/gcc/testsuite/c-c++-common/goacc/tile-2.c
@@ -3,8 +3,8 @@ int main ()
#pragma acc parallel
{
#pragma acc loop tile (*,*)
- for (int ix = 0; ix < 30; ix++)
- ; /* { dg-error "not enough" } */
+ for (int ix = 0; ix < 30; ix++) /* { dg-error "not enough" "" { target c } } */
+ ; /* { dg-error "not enough" "" { target c++ } } */
#pragma acc loop tile (*,*)
for (int ix = 0; ix < 30; ix++)
diff --git a/gcc/testsuite/gcc.dg/gomp/collapse-1.c b/gcc/testsuite/gcc.dg/gomp/collapse-1.c
index 89b76bb669c..16a102ff3fd 100644
--- a/gcc/testsuite/gcc.dg/gomp/collapse-1.c
+++ b/gcc/testsuite/gcc.dg/gomp/collapse-1.c
@@ -8,8 +8,8 @@ void
f1 (void)
{
#pragma omp for collapse (2)
- for (i = 0; i < 5; i++)
- ; /* { dg-error "not enough perfectly nested" } */
+ for (i = 0; i < 5; i++) /* { dg-error "not enough nested loops" } */
+ ;
{
for (j = 0; j < 5; j++)
;
@@ -38,7 +38,7 @@ f3 (void)
#pragma omp for collapse (2)
for (i = 0; i < 5; i++)
{
- int k = foo (); /* { dg-error "not enough perfectly nested" } */
+ int k = foo ();
{
{
for (j = 0; j < 5; j++)
@@ -58,7 +58,7 @@ f4 (void)
{
for (j = 0; j < 5; j++)
;
- foo (); /* { dg-error "collapsed loops not perfectly nested before" } */
+ foo ();
}
}
}
@@ -73,7 +73,7 @@ f5 (void)
for (j = 0; j < 5; j++)
;
}
- foo (); /* { dg-error "collapsed loops not perfectly nested before" } */
+ foo ();
}
}
--
2.31.1
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH V2 3/5] OpenMP: C++ support for imperfectly-nested loops
2023-07-23 22:15 [PATCH V2 0/5] OpenMP: support for imperfectly-nested loops Sandra Loosemore
2023-07-23 22:15 ` [PATCH V2 1/5] OpenMP: Add OMP_STRUCTURED_BLOCK and GIMPLE_OMP_STRUCTURED_BLOCK Sandra Loosemore
2023-07-23 22:15 ` [PATCH V2 2/5] OpenMP: C front end support for imperfectly-nested loops Sandra Loosemore
@ 2023-07-23 22:15 ` Sandra Loosemore
2023-08-22 13:31 ` Jakub Jelinek
2023-07-23 22:15 ` [PATCH V2 4/5] OpenMP: New C/C++ testcases for imperfectly nested loops Sandra Loosemore
` (2 subsequent siblings)
5 siblings, 1 reply; 15+ messages in thread
From: Sandra Loosemore @ 2023-07-23 22:15 UTC (permalink / raw)
To: gcc-patches; +Cc: jakub
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.
(check_omp_intervening_code): New.
(cp_parser_statement_seq_opt): Special-case nested loops, blocks,
and other constructs for OpenMP loops.
(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_convert_omp_range_for): Likewise.
(cp_parser_omp_loop_nest): New, split from cp_parser_omp_for_loop
and largely rewritten. Add more comments.
(insert_structured_blocks): New.
(find_structured_blocks): New.
(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 (finish_omp_for): Try harder to preserve location
of loop variable init expression for use in diagnostics.
(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
* c-c++-common/goacc/tile-2.c: Adjust expected error patterns.
* g++.dg/gomp/attrs-imperfect1.C: New test.
* g++.dg/gomp/attrs-imperfect2.C: New test.
* g++.dg/gomp/attrs-imperfect3.C: New test.
* g++.dg/gomp/attrs-imperfect4.C: New test.
* g++.dg/gomp/attrs-imperfect5.C: New test.
* g++.dg/gomp/pr41967.C: Adjust expected error patterns.
* g++.dg/gomp/tpl-imperfect-gotos.C: New test.
* g++.dg/gomp/tpl-imperfect-invalid-scope.C: New test.
libgomp/ChangeLog
* testsuite/libgomp.c++/attrs-imperfect1.C: New test.
* testsuite/libgomp.c++/attrs-imperfect2.C: New test.
* testsuite/libgomp.c++/attrs-imperfect3.C: New test.
* testsuite/libgomp.c++/attrs-imperfect4.C: New test.
* testsuite/libgomp.c++/attrs-imperfect5.C: New test.
* testsuite/libgomp.c++/attrs-imperfect6.C: New test.
* testsuite/libgomp.c++/imperfect-class-1.C: New test.
* testsuite/libgomp.c++/imperfect-class-2.C: New test.
* testsuite/libgomp.c++/imperfect-class-3.C: New test.
* testsuite/libgomp.c++/imperfect-destructor.C: New test.
* testsuite/libgomp.c++/imperfect-template-1.C: New test.
* testsuite/libgomp.c++/imperfect-template-2.C: New test.
* testsuite/libgomp.c++/imperfect-template-3.C: New test.
---
gcc/cp/cp-tree.h | 2 +-
gcc/cp/parser.cc | 1315 ++++++++++++-----
gcc/cp/parser.h | 3 +
gcc/cp/pt.cc | 3 +-
gcc/cp/semantics.cc | 117 +-
gcc/testsuite/c-c++-common/goacc/tile-2.c | 4 +-
gcc/testsuite/g++.dg/gomp/attrs-imperfect1.C | 38 +
gcc/testsuite/g++.dg/gomp/attrs-imperfect2.C | 34 +
gcc/testsuite/g++.dg/gomp/attrs-imperfect3.C | 33 +
gcc/testsuite/g++.dg/gomp/attrs-imperfect4.C | 33 +
gcc/testsuite/g++.dg/gomp/attrs-imperfect5.C | 57 +
gcc/testsuite/g++.dg/gomp/pr41967.C | 2 +-
.../g++.dg/gomp/tpl-imperfect-gotos.C | 161 ++
.../g++.dg/gomp/tpl-imperfect-invalid-scope.C | 94 ++
.../testsuite/libgomp.c++/attrs-imperfect1.C | 76 +
.../testsuite/libgomp.c++/attrs-imperfect2.C | 114 ++
.../testsuite/libgomp.c++/attrs-imperfect3.C | 119 ++
.../testsuite/libgomp.c++/attrs-imperfect4.C | 117 ++
.../testsuite/libgomp.c++/attrs-imperfect5.C | 49 +
.../testsuite/libgomp.c++/attrs-imperfect6.C | 115 ++
.../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 +++
27 files changed, 3228 insertions(+), 408 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect1.C
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect2.C
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect3.C
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect4.C
create mode 100644 gcc/testsuite/g++.dg/gomp/attrs-imperfect5.C
create mode 100644 gcc/testsuite/g++.dg/gomp/tpl-imperfect-gotos.C
create mode 100644 gcc/testsuite/g++.dg/gomp/tpl-imperfect-invalid-scope.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect1.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect2.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect3.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect4.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect5.C
create mode 100644 libgomp/testsuite/libgomp.c++/attrs-imperfect6.C
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 3de0e154c12..b0d5b885692 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7305,7 +7305,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, va_gc> *, tree &,
+extern void cp_convert_omp_range_for (tree &, tree &, tree &,
tree &, tree &, tree &, tree &, tree &);
extern void cp_finish_omp_range_for (tree, tree);
extern bool parsing_nsdmi (void);
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 5e2b5cba57e..fc5e827a2e9 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -261,6 +261,10 @@ static bool cp_parser_omp_declare_reduction_exprs
static void cp_finalize_oacc_routine
(cp_parser *, tree, bool);
+static void check_omp_intervening_code
+ (cp_parser *);
+
+
/* Manifest constants. */
#define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
#define CP_SAVED_TOKEN_STACK 5
@@ -2091,6 +2095,52 @@ struct cp_parser_expression_stack_entry
typedef struct cp_parser_expression_stack_entry
cp_parser_expression_stack[NUM_PREC_VALUES];
+/* Used for parsing OMP for loops.
+
+ Some notes on flags used for context:
+ parser->omp_for_parse_state is non-null anywhere inside the OMP FOR
+ construct, except for the final-loop-body.
+ The want_nested_loop flag is true if inside a {} sequence where
+ a loop-nest (or another {} sequence containing a loop-nest) is expected,
+ but has not yet been seen. It's false when parsing intervening code
+ statements or their substatements that cannot contain a loop-nest.
+ The in_intervening_code flag is true when parsing any intervening code,
+ including substatements, and whether or not want_nested_loop is true.
+
+ And, about error handling:
+ The saw_intervening_code flag is set if the loop is not perfectly
+ nested, even in the usual case where this is not an error.
+ perfect_nesting_fail is set if an error has been diagnosed because an
+ imperfectly-nested loop was found where a perfectly-nested one is
+ required (we diagnose this only once).
+ fail is set if any kind of structural error in the loop nest
+ has been found and diagnosed.
+ */
+struct omp_for_parse_data {
+ enum tree_code code;
+ tree declv, condv, incrv, initv;
+ tree pre_body;
+ tree orig_declv;
+ auto_vec<tree, 4> orig_inits;
+ int count; /* Expected nesting depth. */
+ int depth; /* Current nesting depth. */
+ location_t for_loc;
+ releasing_vec init_blockv;
+ releasing_vec body_blockv;
+ releasing_vec init_placeholderv;
+ releasing_vec body_placeholderv;
+ bool ordered : 1;
+ bool inscan : 1;
+ bool want_nested_loop : 1;
+ bool in_intervening_code : 1;
+ bool saw_intervening_code : 1;
+ bool perfect_nesting_fail : 1;
+ bool fail : 1;
+ tree clauses;
+ tree *cclauses;
+ tree ordered_cl;
+};
+
/* Prototypes. */
/* Constructors and destructors. */
@@ -2912,6 +2962,7 @@ static bool cp_parser_skip_up_to_closing_square_bracket
static bool cp_parser_skip_to_closing_square_bracket
(cp_parser *);
static size_t cp_parser_skip_balanced_tokens (cp_parser *, size_t);
+static tree cp_parser_omp_loop_nest (cp_parser *, bool *);
// -------------------------------------------------------------------------- //
// Unevaluated Operand Guard
@@ -7999,12 +8050,22 @@ cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
complain);
else
/* All other function calls. */
- postfix_expression
- = finish_call_expr (postfix_expression, &args,
- /*disallow_virtual=*/false,
- koenig_p,
- complain);
-
+ {
+ if (DECL_P (postfix_expression)
+ && parser->omp_for_parse_state
+ && parser->omp_for_parse_state->in_intervening_code
+ && omp_runtime_api_call (postfix_expression))
+ {
+ error_at (loc, "calls to the OpenMP runtime API are "
+ "not permitted in intervening code");
+ parser->omp_for_parse_state->fail = true;
+ }
+ postfix_expression
+ = finish_call_expr (postfix_expression, &args,
+ /*disallow_virtual=*/false,
+ koenig_p,
+ complain);
+ }
if (close_paren_loc != UNKNOWN_LOCATION)
postfix_expression.set_location (combined_loc);
@@ -12522,9 +12583,15 @@ cp_parser_statement (cp_parser* parser, tree in_statement_expr,
return so that we can check for a close brace. Otherwise we
require a real statement and must go back and read one. */
if (in_compound_for_pragma)
- cp_parser_pragma (parser, pragma_compound, if_p);
+ {
+ if (cp_parser_pragma (parser, pragma_compound, if_p)
+ && parser->omp_for_parse_state)
+ check_omp_intervening_code (parser);
+ }
else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
do_restart = true;
+ else if (parser->omp_for_parse_state)
+ check_omp_intervening_code (parser);
if (parser->lexer != lexer
&& lexer->in_omp_attribute_pragma
&& (!in_omp_attribute_pragma || lexer->orphan_p))
@@ -12960,6 +13027,55 @@ cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
return compound_stmt;
}
+/* Diagnose errors related to imperfectly nested loops in an OMP
+ loop construct. This function is called when such code is seen.
+ Only issue one such diagnostic no matter how much invalid
+ intervening code there is in the loop.
+ FIXME: maybe the location associated with the diagnostic should
+ be the current parser token instead of the location of the outer loop
+ nest. */
+
+static void
+check_omp_intervening_code (cp_parser *parser)
+{
+ struct omp_for_parse_data *omp_for_parse_state
+ = parser->omp_for_parse_state;
+ gcc_assert (omp_for_parse_state);
+
+ if (!omp_for_parse_state->in_intervening_code)
+ return;
+ omp_for_parse_state->saw_intervening_code = true;
+
+ /* Only diagnose errors related to perfect nesting once. */
+ if (!omp_for_parse_state->perfect_nesting_fail)
+ {
+ if (omp_for_parse_state->code == OACC_LOOP)
+ {
+ error_at (omp_for_parse_state->for_loc,
+ "inner loops must be perfectly nested in "
+ "%<#pragma acc loop%>");
+ omp_for_parse_state->perfect_nesting_fail = true;
+ }
+ else if (omp_for_parse_state->ordered)
+ {
+ error_at (omp_for_parse_state->for_loc,
+ "inner loops must be perfectly nested with "
+ "%<ordered%> clause");
+ omp_for_parse_state->perfect_nesting_fail = true;
+ }
+ else if (omp_for_parse_state->inscan)
+ {
+ error_at (omp_for_parse_state->for_loc,
+ "inner loops must be perfectly nested with "
+ "%<reduction%> %<inscan%> clause");
+ omp_for_parse_state->perfect_nesting_fail = true;
+ }
+ /* TODO: Also reject loops with TILE directive. */
+ if (omp_for_parse_state->perfect_nesting_fail)
+ omp_for_parse_state->fail = true;
+ }
+}
+
/* Parse an (optional) statement-seq.
statement-seq:
@@ -12969,6 +13085,11 @@ cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
static void
cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
{
+ struct omp_for_parse_data *omp_for_parse_state
+ = parser->omp_for_parse_state;
+ bool in_omp_loop_block
+ = omp_for_parse_state ? omp_for_parse_state->want_nested_loop : false;
+
/* Scan statements until there aren't any more. */
while (true)
{
@@ -12996,6 +13117,50 @@ cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
}
}
+ /* Handle special cases for OMP FOR canonical loop syntax. */
+ else if (in_omp_loop_block)
+ {
+ bool want_nested_loop = omp_for_parse_state->want_nested_loop;
+ if (want_nested_loop
+ && token->type == CPP_KEYWORD && token->keyword == RID_FOR)
+ {
+ /* Found the nested loop. */
+ omp_for_parse_state->depth++;
+ add_stmt (cp_parser_omp_loop_nest (parser, NULL));
+ omp_for_parse_state->depth--;
+ }
+ else if (token->type == CPP_SEMICOLON)
+ {
+ /* Prior to implementing the OpenMP 5.1 syntax for canonical
+ loop form, GCC used to accept an empty statements as not
+ being intervening code. Continue to do that, as an
+ extension. */
+ /* FIXME: Maybe issue a warning or something here? */
+ cp_lexer_consume_token (parser->lexer);
+ }
+ else if (want_nested_loop && token->type == CPP_OPEN_BRACE)
+ /* The nested compound statement may contain the next loop, or
+ it might just be intervening code. */
+ {
+ cp_parser_statement (parser, in_statement_expr, true, NULL);
+ if (omp_for_parse_state->want_nested_loop)
+ check_omp_intervening_code (parser);
+ }
+ else
+ {
+ /* This must be intervening code. */
+ omp_for_parse_state->want_nested_loop = false;
+ /* Defer calling check_omp_intervening_code on pragmas until
+ cp_parser_statement, because we can't know until we parse
+ it whether or not the pragma is a statement. */
+ if (token->type != CPP_PRAGMA)
+ check_omp_intervening_code (parser);
+ cp_parser_statement (parser, in_statement_expr, true, NULL);
+ omp_for_parse_state->want_nested_loop = want_nested_loop;
+ }
+ continue;
+ }
+
/* Parse the statement. */
cp_parser_statement (parser, in_statement_expr, true, NULL);
}
@@ -14194,6 +14359,15 @@ cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep,
statement. */
in_statement = parser->in_statement;
+ /* Special case for OMP loop intervening code. Parsing of permitted
+ collapsed loop nests is handled elsewhere. */
+ if (parser->omp_for_parse_state)
+ {
+ error_at (token->location,
+ "loop not permitted in intervening code in OpenMP loop body");
+ parser->omp_for_parse_state->fail = true;
+ }
+
/* See what kind of keyword it is. */
keyword = token->keyword;
switch (keyword)
@@ -43053,7 +43227,19 @@ cp_parser_omp_for_incr (cp_parser *parser, tree decl)
return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
}
-/* Parse the initialization statement of an OpenMP for loop.
+/* Parse the initialization statement of an OpenMP for loop. Range-for
+ is handled separately in cp_convert_omp_range_for.
+
+ On entry SL is the current statement list. Parsing of some forms
+ of initialization pops this list and stores its contents in either INIT
+ or THIS_PRE_BODY, and sets SL to null. Initialization for class
+ iterators is added directly to SL and it is not popped until later.
+
+ On return, DECL is set if the initialization is by binding the
+ iteration variable. If the initialization is by assignment, REAL_DECL
+ is set to point to a variable in an outer scope. ORIG_INIT is set
+ if the iteration variable is of class type; this is a copy saved for
+ error checking in finish_omp_for.
Return true if the resulting construct should have an
OMP_CLAUSE_PRIVATE added to it. */
@@ -43061,7 +43247,7 @@ cp_parser_omp_for_incr (cp_parser *parser, tree decl)
static tree
cp_parser_omp_for_loop_init (cp_parser *parser,
tree &this_pre_body,
- releasing_vec &for_block,
+ tree &sl,
tree &init,
tree &orig_init,
tree &decl,
@@ -43159,18 +43345,22 @@ cp_parser_omp_for_loop_init (cp_parser *parser,
asm_specification,
LOOKUP_ONLYCONVERTING);
orig_init = init;
+
+ /* In the case of a class iterator, do not pop sl here.
+ Both class initialization and finalization must happen in
+ the enclosing init block scope. For now set the init
+ expression to null; it'll be filled in properly in
+ finish_omp_for before stuffing it in the OMP_FOR. */
if (CLASS_TYPE_P (TREE_TYPE (decl)))
+ init = NULL_TREE;
+ else /* It is a parameterized type. */
{
- vec_safe_push (for_block, this_pre_body);
- init = NULL_TREE;
- }
- else
- {
- init = pop_stmt_list (this_pre_body);
+ init = pop_stmt_list (sl);
+ sl = NULL_TREE;
if (init && TREE_CODE (init) == STATEMENT_LIST)
{
tree_stmt_iterator i = tsi_start (init);
- /* Move lambda DECL_EXPRs to FOR_BLOCK. */
+ /* Move lambda DECL_EXPRs to the enclosing block. */
while (!tsi_end_p (i))
{
tree t = tsi_stmt (i);
@@ -43178,7 +43368,7 @@ cp_parser_omp_for_loop_init (cp_parser *parser,
&& TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
{
tsi_delink (&i);
- vec_safe_push (for_block, t);
+ add_stmt (t);
continue;
}
break;
@@ -43192,9 +43382,10 @@ cp_parser_omp_for_loop_init (cp_parser *parser,
}
}
}
- this_pre_body = NULL_TREE;
}
else
+ /* This is an initialized declaration of non-class,
+ non-parameterized type iteration variable. */
{
/* Consume '='. */
cp_lexer_consume_token (parser->lexer);
@@ -43208,6 +43399,8 @@ cp_parser_omp_for_loop_init (cp_parser *parser,
/*init_const_expr_p=*/false,
asm_specification,
LOOKUP_ONLYCONVERTING);
+ this_pre_body = pop_stmt_list (sl);
+ sl = NULL_TREE;
}
if (pushed_scope)
@@ -43279,14 +43472,21 @@ cp_parser_omp_for_loop_init (cp_parser *parser,
real_decl = TREE_OPERAND (init, 0);
}
}
+ this_pre_body = pop_stmt_list (sl);
+ sl = NULL_TREE;
}
return add_private_clause;
}
-/* Helper for cp_parser_omp_for_loop, handle one range-for loop. */
+/* Helper for cp_parser_omp_loop_nest, handle one range-for loop
+ including introducing new temporaries for the range start and end,
+ doing auto deduction, and processing decomposition variables.
+ This function is also called from pt.cc during template instantiation.
+ In that case SL is NULL_TREE, otherwise it is the current statement
+ list. */
void
-cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
+cp_convert_omp_range_for (tree &this_pre_body, tree &sl,
tree &decl, tree &orig_decl, tree &init,
tree &orig_init, tree &cond, tree &incr)
{
@@ -43322,8 +43522,11 @@ cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
cond = global_namespace;
incr = NULL_TREE;
orig_init = init;
- if (this_pre_body)
- this_pre_body = pop_stmt_list (this_pre_body);
+ if (sl)
+ {
+ this_pre_body = pop_stmt_list (sl);
+ sl = NULL_TREE;
+ }
return;
}
@@ -43403,11 +43606,7 @@ cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
orig_decl = decl;
decl = begin;
- if (for_block)
- {
- vec_safe_push (for_block, this_pre_body);
- this_pre_body = NULL_TREE;
- }
+ /* Defer popping sl here. */
tree decomp_first_name = NULL_TREE;
unsigned decomp_cnt = 0;
@@ -43445,6 +43644,15 @@ cp_convert_omp_range_for (tree &this_pre_body, vec<tree, va_gc> *for_block,
}
}
+ /* The output ORIG_DECL is not a decl. Instead, it is a tree structure
+ that holds decls for variables implementing the iterator, represented
+ as a TREE_LIST whose TREE_CHAIN is a vector. The first two elements
+ of the vector are decls of scratch variables for the range start and
+ end that will eventually be bound in the implicit scope surrounding
+ the whole loop nest. The remaining elements are decls of derived
+ decomposition variables that are bound inside the loop body. This
+ structure is further mangled by finish_omp_for into the form required
+ for the OMP_FOR_ORIG_DECLS field of the OMP_FOR tree node. */
tree v = make_tree_vec (decomp_cnt + 3);
TREE_VEC_ELT (v, 0) = range_temp_decl;
TREE_VEC_ELT (v, 1) = end;
@@ -43699,404 +43907,762 @@ cp_parser_omp_scan_loop_body (cp_parser *parser)
braces.require_close (parser);
}
-/* Parse the restricted form of the for statement allowed by OpenMP. */
-static tree
-cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
- tree *cclauses, bool *if_p)
-{
- tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
- tree orig_decl;
- tree real_decl, initv, condv, incrv, declv, orig_declv;
- tree this_pre_body, cl, ordered_cl = NULL_TREE;
- location_t loc_first;
- bool collapse_err = false;
- int i, collapse = 1, ordered = 0, count, nbraces = 0;
- releasing_vec for_block;
- auto_vec<tree, 4> orig_inits;
- bool tiling = false;
- bool inscan = false;
+/* This function parses a single level of a loop nest, invoking itself
+ recursively if necessary.
- for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
- if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
- collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
- else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
- {
- tiling = true;
- collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
- }
- else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
- && OMP_CLAUSE_ORDERED_EXPR (cl))
- {
- ordered_cl = cl;
- ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
- }
- else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
- && OMP_CLAUSE_REDUCTION_INSCAN (cl)
- && (code == OMP_SIMD || code == OMP_FOR))
- inscan = true;
+ loop-nest :: for (...) loop-body
+ loop-body :: loop-nest
+ | { [intervening-code] loop-body [intervening-code] }
+ | final-loop-body
+ intervening-code :: structured-block-sequence
+ final-loop-body :: structured-block
- if (ordered && ordered < collapse)
- {
- error_at (OMP_CLAUSE_LOCATION (ordered_cl),
- "%<ordered%> clause parameter is less than %<collapse%>");
- OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
- = build_int_cst (NULL_TREE, collapse);
- ordered = collapse;
- }
+ For a collapsed loop nest, only a single OMP_FOR is built, pulling out
+ all the iterator information from the inner loops into vectors in the
+ parser->omp_for_parse_state structure.
- gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
- count = ordered ? ordered : collapse;
+ In the "range for" case, it is transformed into a regular "for" iterator
+ by introducing some temporary variables for the begin/end,
+ as well as bindings of the actual iteration variables which are
+ injected into the body of the loop.
- declv = make_tree_vec (count);
- initv = make_tree_vec (count);
- condv = make_tree_vec (count);
- incrv = make_tree_vec (count);
- orig_declv = NULL_TREE;
+ Initialization code for iterator variables may end up either in the
+ init vector (simple assignments), in omp_for_parse_state->pre_body
+ (decl_exprs for iterators bound in the for statement), or in the
+ scope surrounding this level of loop initialization.
- loc_first = cp_lexer_peek_token (parser->lexer)->location;
+ The scopes of class iterator variables and their finalizers need to
+ be adjusted after parsing so that all of the initialization happens
+ in a scope surrounding all of the intervening and body code. For
+ this reason we separately store the initialization and body blocks
+ for each level of loops in the omp_for_parse_state structure and
+ reassemble/reorder them in cp_parser_omp_for. See additional
+ comments there about the use of placeholders, etc. */
- for (i = 0; i < count; i++)
- {
- int bracecount = 0;
- tree add_private_clause = NULL_TREE;
- location_t loc;
+static tree
+cp_parser_omp_loop_nest (cp_parser *parser, bool *if_p)
+{
+ tree decl, cond, incr, init;
+ tree orig_init, real_decl, orig_decl;
+ tree init_block, body_block;
+ tree init_placeholder, body_placeholder;
+ tree init_scope;
+ tree this_pre_body = NULL_TREE;
+ bool moreloops;
+ unsigned char save_in_statement;
+ tree add_private_clause = NULL_TREE;
+ location_t loc;
+ bool is_range_for = false;
+ tree sl = NULL_TREE;
+ struct omp_for_parse_data *omp_for_parse_state
+ = parser->omp_for_parse_state;
+ gcc_assert (omp_for_parse_state);
+ int depth = omp_for_parse_state->depth;
+
+ /* We have already matched the FOR token but not consumed it yet. */
+ gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR));
+ loc = cp_lexer_consume_token (parser->lexer)->location;
+
+ /* Forbid break/continue in the loop initializer, condition, and
+ increment expressions. */
+ save_in_statement = parser->in_statement;
+ parser->in_statement = IN_OMP_BLOCK;
+
+ /* We are not in intervening code now. */
+ omp_for_parse_state->in_intervening_code = false;
+
+ /* Don't create location wrapper nodes within an OpenMP "for"
+ statement. */
+ auto_suppress_location_wrappers sentinel;
- if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
- {
- if (!collapse_err)
- cp_parser_error (parser, "for statement expected");
- return NULL;
- }
- loc = cp_lexer_consume_token (parser->lexer)->location;
+ matching_parens parens;
+ if (!parens.require_open (parser))
+ return NULL;
- /* Don't create location wrapper nodes within an OpenMP "for"
- statement. */
- auto_suppress_location_wrappers sentinel;
+ init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
- matching_parens parens;
- if (!parens.require_open (parser))
- return NULL;
+ init_placeholder = build_stmt (input_location, EXPR_STMT,
+ integer_zero_node);
+ vec_safe_push (omp_for_parse_state->init_placeholderv, init_placeholder);
- init = orig_init = decl = real_decl = orig_decl = NULL_TREE;
- this_pre_body = push_stmt_list ();
+ /* The init_block acts as a container for this level of loop goo. */
+ init_block = push_stmt_list ();
+ vec_safe_push (omp_for_parse_state->init_blockv, init_block);
- if (code != OACC_LOOP && cxx_dialect >= cxx11)
- {
- /* Save tokens so that we can put them back. */
- cp_lexer_save_tokens (parser->lexer);
+ /* Wrap a scope around this entire level of loop to hold bindings
+ of loop iteration variables. We can't insert them directly
+ in the containing scope because that would cause their visibility to
+ be incorrect with respect to intervening code after this loop.
+ We will combine the nested init_scopes in postprocessing after the
+ entire loop is parsed. */
+ init_scope = begin_compound_stmt (0);
- /* Look for ':' that is not nested in () or {}. */
- bool is_range_for
- = (cp_parser_skip_to_closing_parenthesis_1 (parser,
- /*recovering=*/false,
- CPP_COLON,
- /*consume_paren=*/
- false) == -1);
+ /* Now we need another level of statement list container to capture the
+ initialization (and possible finalization) bits. In some cases this
+ container may be popped off during initializer parsing to store code in
+ INIT or THIS_PRE_BODY, depending on the form of initialization. If
+ we have a class iterator we will pop it at the end of parsing this
+ level, so the cleanups are handled correctly. */
+ sl = push_stmt_list ();
- /* Roll back the tokens we skipped. */
- cp_lexer_rollback_tokens (parser->lexer);
+ if (omp_for_parse_state->code != OACC_LOOP && cxx_dialect >= cxx11)
+ {
+ /* Save tokens so that we can put them back. */
+ cp_lexer_save_tokens (parser->lexer);
- if (is_range_for)
- {
- bool saved_colon_corrects_to_scope_p
- = parser->colon_corrects_to_scope_p;
+ /* Look for ':' that is not nested in () or {}. */
+ is_range_for
+ = (cp_parser_skip_to_closing_parenthesis_1 (parser,
+ /*recovering=*/false,
+ CPP_COLON,
+ /*consume_paren=*/
+ false) == -1);
- /* A colon is used in range-based for. */
- parser->colon_corrects_to_scope_p = false;
+ /* Roll back the tokens we skipped. */
+ cp_lexer_rollback_tokens (parser->lexer);
- /* Parse the declaration. */
- cp_parser_simple_declaration (parser,
- /*function_definition_allowed_p=*/
- false, &decl);
- parser->colon_corrects_to_scope_p
- = saved_colon_corrects_to_scope_p;
+ if (is_range_for)
+ {
+ bool saved_colon_corrects_to_scope_p
+ = parser->colon_corrects_to_scope_p;
- cp_parser_require (parser, CPP_COLON, RT_COLON);
+ /* A colon is used in range-based for. */
+ parser->colon_corrects_to_scope_p = false;
- init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
- false, 0, true);
+ /* Parse the declaration. */
+ cp_parser_simple_declaration (parser,
+ /*function_definition_allowed_p=*/
+ false, &decl);
+ parser->colon_corrects_to_scope_p
+ = saved_colon_corrects_to_scope_p;
- cp_convert_omp_range_for (this_pre_body, for_block, decl,
- orig_decl, init, orig_init,
- cond, incr);
- if (this_pre_body)
- {
- if (pre_body)
- {
- tree t = pre_body;
- pre_body = push_stmt_list ();
- add_stmt (t);
- add_stmt (this_pre_body);
- pre_body = pop_stmt_list (pre_body);
- }
- else
- pre_body = this_pre_body;
- }
+ cp_parser_require (parser, CPP_COLON, RT_COLON);
- if (ordered_cl)
- error_at (OMP_CLAUSE_LOCATION (ordered_cl),
- "%<ordered%> clause with parameter on "
- "range-based %<for%> loop");
+ init = cp_parser_range_for (parser, NULL_TREE, NULL_TREE, decl,
+ false, 0, true);
- goto parse_close_paren;
- }
- }
+ cp_convert_omp_range_for (this_pre_body, sl, decl,
+ orig_decl, init, orig_init,
+ cond, incr);
- add_private_clause
- = cp_parser_omp_for_loop_init (parser, this_pre_body, for_block,
- init, orig_init, decl, real_decl);
+ if (omp_for_parse_state->ordered_cl)
+ error_at (OMP_CLAUSE_LOCATION (omp_for_parse_state->ordered_cl),
+ "%<ordered%> clause with parameter on "
+ "range-based %<for%> loop");
- cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
- if (this_pre_body)
- {
- this_pre_body = pop_stmt_list (this_pre_body);
- if (pre_body)
- {
- tree t = pre_body;
- pre_body = push_stmt_list ();
- add_stmt (t);
- add_stmt (this_pre_body);
- pre_body = pop_stmt_list (pre_body);
- }
- else
- pre_body = this_pre_body;
+ goto parse_close_paren;
}
+ }
- if (decl)
- real_decl = decl;
- if (cclauses != NULL
- && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
- && real_decl != NULL_TREE
- && code != OMP_LOOP)
- {
- tree *c;
- for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
- if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
- && OMP_CLAUSE_DECL (*c) == real_decl)
- {
- error_at (loc, "iteration variable %qD"
- " should not be firstprivate", real_decl);
- *c = OMP_CLAUSE_CHAIN (*c);
- }
- else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
- && OMP_CLAUSE_DECL (*c) == real_decl)
+ add_private_clause
+ = cp_parser_omp_for_loop_init (parser, this_pre_body, sl,
+ init, orig_init, decl, real_decl);
+
+ cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
+
+ /* If the iteration variable was introduced via a declaration in the
+ for statement, DECL points at it. Otherwise DECL is null and
+ REAL_DECL is a variable previously declared in an outer scope.
+ Make REAL_DECL point at the iteration variable no matter where it
+ was introduced. */
+ if (decl)
+ real_decl = decl;
+
+ /* Some clauses treat iterator variables specially. */
+ if (omp_for_parse_state->cclauses != NULL
+ && omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
+ && real_decl != NULL_TREE
+ && omp_for_parse_state->code != OMP_LOOP)
+ {
+ tree *c;
+ for (c = &(omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]);
+ *c ; )
+ if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
+ && OMP_CLAUSE_DECL (*c) == real_decl)
+ {
+ error_at (loc, "iteration variable %qD"
+ " should not be firstprivate", real_decl);
+ *c = OMP_CLAUSE_CHAIN (*c);
+ }
+ else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
+ && OMP_CLAUSE_DECL (*c) == real_decl)
+ {
+ /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
+ tree l = *c;
+ *c = OMP_CLAUSE_CHAIN (*c);
+ if (omp_for_parse_state->code == OMP_SIMD)
{
- /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
- tree l = *c;
- *c = OMP_CLAUSE_CHAIN (*c);
- if (code == OMP_SIMD)
- {
- OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
- cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
- }
- else
- {
- OMP_CLAUSE_CHAIN (l) = clauses;
- clauses = l;
- }
- add_private_clause = NULL_TREE;
+ OMP_CLAUSE_CHAIN (l)
+ = omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR];
+ omp_for_parse_state->cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
}
else
{
- if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
- && OMP_CLAUSE_DECL (*c) == real_decl)
- add_private_clause = NULL_TREE;
- c = &OMP_CLAUSE_CHAIN (*c);
+ OMP_CLAUSE_CHAIN (l) = omp_for_parse_state->clauses;
+ omp_for_parse_state->clauses = l;
}
- }
+ add_private_clause = NULL_TREE;
+ }
+ else
+ {
+ if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
+ && OMP_CLAUSE_DECL (*c) == real_decl)
+ add_private_clause = NULL_TREE;
+ c = &OMP_CLAUSE_CHAIN (*c);
+ }
+ }
- if (add_private_clause)
+ if (add_private_clause)
+ {
+ tree c;
+ for (c = omp_for_parse_state->clauses; c ; c = OMP_CLAUSE_CHAIN (c))
{
- tree c;
- for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
+ if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
+ || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
+ && OMP_CLAUSE_DECL (c) == decl)
+ break;
+ else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
+ && OMP_CLAUSE_DECL (c) == decl)
+ error_at (loc, "iteration variable %qD "
+ "should not be firstprivate",
+ decl);
+ else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
+ || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
+ && OMP_CLAUSE_DECL (c) == decl)
+ error_at (loc, "iteration variable %qD should not be reduction",
+ decl);
+ }
+ if (c == NULL)
+ {
+ if ((omp_for_parse_state->code == OMP_SIMD
+ && omp_for_parse_state->count != 1)
+ || omp_for_parse_state->code == OMP_LOOP)
+ c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
+ else if (omp_for_parse_state->code != OMP_SIMD)
+ c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
+ else
+ c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
+ OMP_CLAUSE_DECL (c) = add_private_clause;
+ c = finish_omp_clauses (c, C_ORT_OMP);
+ if (c)
{
- if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
- || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
- && OMP_CLAUSE_DECL (c) == decl)
- break;
- else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
- && OMP_CLAUSE_DECL (c) == decl)
- error_at (loc, "iteration variable %qD "
- "should not be firstprivate",
- decl);
- else if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
- || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IN_REDUCTION)
- && OMP_CLAUSE_DECL (c) == decl)
- error_at (loc, "iteration variable %qD should not be reduction",
- decl);
- }
- if (c == NULL)
- {
- if ((code == OMP_SIMD && collapse != 1) || code == OMP_LOOP)
- c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
- else if (code != OMP_SIMD)
- c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
- else
- c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
- OMP_CLAUSE_DECL (c) = add_private_clause;
- c = finish_omp_clauses (c, C_ORT_OMP);
- if (c)
- {
- OMP_CLAUSE_CHAIN (c) = clauses;
- clauses = c;
- /* For linear, signal that we need to fill up
- the so far unknown linear step. */
- if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
- OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
- }
+ OMP_CLAUSE_CHAIN (c) = omp_for_parse_state->clauses;
+ omp_for_parse_state->clauses = c;
+ /* For linear, signal that we need to fill up
+ the so far unknown linear step. */
+ if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
+ OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
}
}
+ }
- cond = NULL;
- if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
- cond = cp_parser_omp_for_cond (parser, decl, code);
- cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
+ cond = NULL;
+ if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
+ cond = cp_parser_omp_for_cond (parser, decl, omp_for_parse_state->code);
+ cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
- incr = NULL;
- if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
- {
- /* If decl is an iterator, preserve the operator on decl
- until finish_omp_for. */
- if (real_decl
- && ((processing_template_decl
- && (TREE_TYPE (real_decl) == NULL_TREE
- || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
- || CLASS_TYPE_P (TREE_TYPE (real_decl))))
- incr = cp_parser_omp_for_incr (parser, real_decl);
- else
- incr = cp_parser_expression (parser);
- protected_set_expr_location_if_unset (incr, input_location);
- }
+ incr = NULL;
+ if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
+ {
+ /* If decl is an iterator, preserve the operator on decl
+ until finish_omp_for. */
+ if (real_decl
+ && ((processing_template_decl
+ && (TREE_TYPE (real_decl) == NULL_TREE
+ || !INDIRECT_TYPE_P (TREE_TYPE (real_decl))))
+ || CLASS_TYPE_P (TREE_TYPE (real_decl))))
+ incr = cp_parser_omp_for_incr (parser, real_decl);
+ else
+ incr = cp_parser_expression (parser);
+ protected_set_expr_location_if_unset (incr, input_location);
+ }
- parse_close_paren:
- if (!parens.require_close (parser))
- cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
- /*or_comma=*/false,
- /*consume_paren=*/true);
+ parse_close_paren:
+ if (!parens.require_close (parser))
+ cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
+ /*or_comma=*/false,
+ /*consume_paren=*/true);
- TREE_VEC_ELT (declv, i) = decl;
- TREE_VEC_ELT (initv, i) = init;
- TREE_VEC_ELT (condv, i) = cond;
- TREE_VEC_ELT (incrv, i) = incr;
- if (orig_init)
- {
- orig_inits.safe_grow_cleared (i + 1, true);
- orig_inits[i] = orig_init;
- }
- if (orig_decl)
+ /* We've parsed all the for (...) stuff now. Store the bits. */
+ TREE_VEC_ELT (omp_for_parse_state->declv, depth) = decl;
+ TREE_VEC_ELT (omp_for_parse_state->initv, depth) = init;
+ TREE_VEC_ELT (omp_for_parse_state->condv, depth) = cond;
+ TREE_VEC_ELT (omp_for_parse_state->incrv, depth) = incr;
+ if (orig_init)
+ {
+ omp_for_parse_state->orig_inits.safe_grow_cleared (depth + 1, true);
+ omp_for_parse_state->orig_inits[depth] = orig_init;
+ }
+ if (orig_decl)
+ {
+ if (!omp_for_parse_state->orig_declv)
+ omp_for_parse_state->orig_declv
+ = copy_node (omp_for_parse_state->declv);
+ TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = orig_decl;
+ }
+ else if (omp_for_parse_state->orig_declv)
+ TREE_VEC_ELT (omp_for_parse_state->orig_declv, depth) = decl;
+ if (this_pre_body)
+ append_to_statement_list_force (this_pre_body,
+ &(omp_for_parse_state->pre_body));
+
+ /* Start a nested block for the loop body. */
+ body_placeholder = build_stmt (input_location, EXPR_STMT,
+ integer_zero_node);
+ vec_safe_push (omp_for_parse_state->body_placeholderv, body_placeholder);
+ body_block = push_stmt_list ();
+ vec_safe_push (omp_for_parse_state->body_blockv, body_block);
+
+ moreloops = depth < omp_for_parse_state->count - 1;
+ omp_for_parse_state->want_nested_loop = moreloops;
+ if (moreloops && cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
+ {
+ omp_for_parse_state->depth++;
+ add_stmt (cp_parser_omp_loop_nest (parser, if_p));
+ omp_for_parse_state->depth--;
+ }
+ else if (moreloops
+ && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
+ {
+ /* This is the open brace in the loop-body grammar production. Rather
+ than trying to special-case braces, just parse it as a compound
+ statement and handle the nested loop-body case there. Note that
+ when we see a further open brace inside the compound statement
+ loop-body, we don't know whether it is the start of intervening
+ code that is a compound statement, or a level of braces
+ surrounding a nested loop-body. Use the WANT_NESTED_LOOP state
+ bit to ensure we have only one nested loop at each level. */
+
+ omp_for_parse_state->in_intervening_code = true;
+ cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
+ omp_for_parse_state->in_intervening_code = false;
+
+ if (omp_for_parse_state->want_nested_loop)
{
- if (!orig_declv)
- orig_declv = copy_node (declv);
- TREE_VEC_ELT (orig_declv, i) = orig_decl;
+ /* We have already parsed the whole loop body and not found a
+ nested loop. */
+ error_at (omp_for_parse_state->for_loc,
+ "not enough nested loops");
+ omp_for_parse_state->fail = true;
}
- else if (orig_declv)
- TREE_VEC_ELT (orig_declv, i) = decl;
+ if_p = NULL;
+ }
+ else
+ {
+ /* This is the final-loop-body case in the grammar: we have something
+ that is not a FOR and not an open brace. */
+ if (moreloops)
+ {
+ /* If we were expecting a nested loop, give an error and mark
+ that parsing has failed, and try to recover by parsing the
+ body as regular code without further collapsing. */
+ error_at (omp_for_parse_state->for_loc,
+ "not enough nested loops");
+ omp_for_parse_state->fail = true;
+ }
+ parser->in_statement = IN_OMP_FOR;
+
+ /* Generate the parts of range for that belong in the loop body,
+ to be executed on every iteration. This includes setting the
+ user-declared decomposition variables from the compiler-generated
+ temporaries that are the real iteration variables for OMP_FOR.
+ FIXME: Not sure if this is correct with respect to visibility
+ of the variables from intervening code. However, putting this
+ code in each level of loop instead of all around the innermost
+ body also makes the decomposition variables visible to the
+ inner for init/bound/step exressions, which is not supposed to
+ happen and causes test failures. */
+ if (omp_for_parse_state->orig_declv)
+ for (int i = 0; i < omp_for_parse_state->count; i++)
+ {
+ tree o = TREE_VEC_ELT (omp_for_parse_state->orig_declv, i);
+ tree d = TREE_VEC_ELT (omp_for_parse_state->declv, i);
+ if (o != d)
+ cp_finish_omp_range_for (o, d);
+ }
- if (i == count - 1)
- break;
+ /* Now parse the final-loop-body for the innermost loop. */
+ parser->omp_for_parse_state = NULL;
+ if (omp_for_parse_state->inscan)
+ cp_parser_omp_scan_loop_body (parser);
+ else
+ cp_parser_statement (parser, NULL_TREE, false, if_p);
+ parser->omp_for_parse_state = omp_for_parse_state;
+ }
+ parser->in_statement = save_in_statement;
+ omp_for_parse_state->want_nested_loop = false;
+ omp_for_parse_state->in_intervening_code = true;
+
+ /* Pop and remember the body block. Add the body placeholder
+ to the surrounding statement list instead. This is just a unique
+ token that will be replaced when we reassemble the generated
+ code for the entire omp for statement. */
+ body_block = pop_stmt_list (body_block);
+ omp_for_parse_state->body_blockv[depth] = body_block;
+ add_stmt (body_placeholder);
+
+ /* Pop and remember the init block. */
+ if (sl)
+ add_stmt (pop_stmt_list (sl));
+ finish_compound_stmt (init_scope);
+ init_block = pop_stmt_list (init_block);
+ omp_for_parse_state->init_blockv[depth] = init_block;
+
+ /* Return the init placeholder rather than the remembered init block.
+ Again, this is just a unique cookie that will be used to reassemble
+ code pieces when the entire omp for statement has been parsed. */
+ return init_placeholder;
+}
+
+/* Worker for find_structured_blocks. *TP points to a STATEMENT_LIST
+ and ITER is the element that is or contains a nested loop. This
+ function moves the statements before and after ITER into
+ OMP_STRUCTURED_BLOCKs and modifies *TP. */
+static void
+insert_structured_blocks (tree *tp, tree_stmt_iterator iter)
+{
+ tree sl = push_stmt_list ();
+ for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); )
+ if (i == iter)
+ {
+ sl = pop_stmt_list (sl);
+ if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
+ tsi_link_before (&i,
+ build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
+ TSI_SAME_STMT);
+ i++;
+ sl = push_stmt_list ();
+ }
+ else
+ {
+ tree s = tsi_stmt (i);
+ tsi_delink (&i); /* Advances i to next statement. */
+ add_stmt (s);
+ }
+ sl = pop_stmt_list (sl);
+ if (TREE_CODE (sl) != STATEMENT_LIST || !tsi_end_p (tsi_start (sl)))
+ tsi_link_after (&iter,
+ build1 (OMP_STRUCTURED_BLOCK, void_type_node, sl),
+ TSI_SAME_STMT);
+}
- /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
- in between the collapsed for loops to be still considered perfectly
- nested. Hopefully the final version clarifies this.
- For now handle (multiple) {'s and empty statements. */
- cp_parser_parse_tentatively (parser);
- for (;;)
+/* Helper to find and mark structured blocks in intervening code for a
+ single loop level with markers for later error checking. *TP is the
+ piece of code to be marked and INNER is the inner loop placeholder.
+ Returns true if INNER was found (recursively) in *TP. */
+static bool
+find_structured_blocks (tree *tp, tree inner)
+{
+ if (*tp == inner)
+ return true;
+ else if (TREE_CODE (*tp) == BIND_EXPR)
+ return find_structured_blocks (&(BIND_EXPR_BODY (*tp)), inner);
+ else if (TREE_CODE (*tp) == STATEMENT_LIST)
+ {
+ for (tree_stmt_iterator i = tsi_start (*tp); !tsi_end_p (i); ++i)
{
- if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
- break;
- else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
+ tree *p = tsi_stmt_ptr (i);
+ /* The normal case is that there is no intervening code and we
+ do not have to insert any OMP_STRUCTURED_BLOCK markers. */
+ if (find_structured_blocks (p, inner))
{
- cp_lexer_consume_token (parser->lexer);
- bracecount++;
- }
- else if (bracecount
- && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
- cp_lexer_consume_token (parser->lexer);
- else
- {
- loc = cp_lexer_peek_token (parser->lexer)->location;
- error_at (loc, "not enough for loops to collapse");
- collapse_err = true;
- cp_parser_abort_tentative_parse (parser);
- declv = NULL_TREE;
- break;
+ if (!(i == tsi_start (*tp) && i == tsi_last (*tp)))
+ insert_structured_blocks (tp, i);
+ return true;
}
}
+ return false;
+ }
+ else if (TREE_CODE (*tp) == TRY_FINALLY_EXPR)
+ return find_structured_blocks (&(TREE_OPERAND (*tp, 0)), inner);
+ else if (TREE_CODE (*tp) == CLEANUP_STMT)
+ return find_structured_blocks (&(CLEANUP_BODY (*tp)), inner);
+ else
+ return false;
+}
+
+/* Helpers used for relinking tree structures: In tree rooted at
+ CONTEXT, replace ORIG with REPLACEMENT. If FLATTEN is true, try to combine
+ nested BIND_EXPRs. Gives an assertion if it fails to find ORIG. */
+
+struct sit_data {
+ tree orig;
+ tree repl;
+ bool flatten;
+};
+
+static tree
+substitute_in_tree_walker (tree *tp, int *walk_subtrees ATTRIBUTE_UNUSED,
+ void *dp)
+{
+ struct sit_data *sit = (struct sit_data *)dp;
+ if (*tp == sit->orig)
+ {
+ *tp = sit->repl;
+ return *tp;
+ }
+ /* Remove redundant BIND_EXPRs with no bindings even when not specifically
+ trying to flatten. */
+ else if (TREE_CODE (*tp) == BIND_EXPR
+ && BIND_EXPR_BODY (*tp) == sit->orig
+ && !BIND_EXPR_VARS (*tp)
+ && (sit->flatten || TREE_CODE (sit->repl) == BIND_EXPR))
+ {
+ *tp = sit->repl;
+ return *tp;
+ }
+ else if (sit->flatten
+ && TREE_CODE (*tp) == BIND_EXPR
+ && TREE_CODE (sit->repl) == BIND_EXPR)
+ {
+ if (BIND_EXPR_BODY (*tp) == sit->orig)
+ {
+ /* Merge binding lists for two directly nested BIND_EXPRs,
+ keeping the outer one. */
+ BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
+ BIND_EXPR_VARS (sit->repl));
+ BIND_EXPR_BODY (*tp) = BIND_EXPR_BODY (sit->repl);
+ return *tp;
+ }
+ else if (TREE_CODE (BIND_EXPR_BODY (*tp)) == STATEMENT_LIST)
+ /* There might be a statement list containing cleanup_points
+ etc between the two levels of BIND_EXPR. We can still merge
+ them, again keeping the outer BIND_EXPR. */
+ for (tree_stmt_iterator i = tsi_start (BIND_EXPR_BODY (*tp));
+ !tsi_end_p (i); ++i)
+ {
+ tree *p = tsi_stmt_ptr (i);
+ if (*p == sit->orig)
+ {
+ BIND_EXPR_VARS (*tp) = chainon (BIND_EXPR_VARS (*tp),
+ BIND_EXPR_VARS (sit->repl));
+ *p = BIND_EXPR_BODY (sit->repl);
+ return *tp;
+ }
+ }
+ }
+ return NULL;
+}
+
+static void
+substitute_in_tree (tree *context, tree orig, tree repl, bool flatten)
+{
+ struct sit_data data;
+
+ gcc_assert (*context && orig && repl);
+ if (TREE_CODE (repl) == BIND_EXPR && !BIND_EXPR_VARS (repl))
+ repl = BIND_EXPR_BODY (repl);
+ data.orig = orig;
+ data.repl = repl;
+ data.flatten = flatten;
+
+ tree result = cp_walk_tree (context, substitute_in_tree_walker,
+ (void *)&data, NULL);
+ gcc_assert (result != NULL_TREE);
+}
- if (declv)
+/* Walker to patch up the BLOCK_NODE hierarchy after the above surgery.
+ *DP is is the parent block. */
+
+static tree
+fixup_blocks_walker (tree *tp, int *walk_subtrees, void *dp)
+{
+ tree superblock = *(tree *)dp;
+
+ if (TREE_CODE (*tp) == BIND_EXPR)
+ {
+ tree block = BIND_EXPR_BLOCK (*tp);
+ if (superblock)
{
- cp_parser_parse_definitely (parser);
- nbraces += bracecount;
+ BLOCK_SUPERCONTEXT (block) = superblock;
+ BLOCK_CHAIN (block) = BLOCK_SUBBLOCKS (superblock);
+ BLOCK_SUBBLOCKS (superblock) = block;
}
+ BLOCK_SUBBLOCKS (block) = NULL_TREE;
+ cp_walk_tree (&BIND_EXPR_BODY (*tp), fixup_blocks_walker,
+ (void *)&block, NULL);
+ *walk_subtrees = 0;
}
- if (nbraces)
- if_p = NULL;
+ return NULL;
+}
- /* Note that we saved the original contents of this flag when we entered
- the structured block, and so we don't need to re-save it here. */
- parser->in_statement = IN_OMP_FOR;
+/* Parse the restricted form of the for statement allowed by OpenMP. */
+
+static tree
+cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
+ tree *cclauses, bool *if_p)
+{
+ tree ret;
+ tree cl, ordered_cl = NULL_TREE;
+ int collapse = 1, ordered = 0;
+ unsigned int count;
+ bool tiling = false;
+ bool inscan = false;
+ struct omp_for_parse_data data;
+ struct omp_for_parse_data *save_data = parser->omp_for_parse_state;
+ tree result;
+ location_t loc_first = cp_lexer_peek_token (parser->lexer)->location;
+
+ for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
+ if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
+ collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
+ else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
+ {
+ tiling = true;
+ collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
+ }
+ else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
+ && OMP_CLAUSE_ORDERED_EXPR (cl))
+ {
+ ordered_cl = cl;
+ ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
+ }
+ else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_REDUCTION
+ && OMP_CLAUSE_REDUCTION_INSCAN (cl)
+ && (code == OMP_SIMD || code == OMP_FOR))
+ inscan = true;
- /* Note that the grammar doesn't call for a structured block here,
- though the loop as a whole is a structured block. */
- if (orig_declv)
+ if (ordered && ordered < collapse)
{
- body = begin_omp_structured_block ();
- for (i = 0; i < count; i++)
- if (TREE_VEC_ELT (orig_declv, i) != TREE_VEC_ELT (declv, i))
- cp_finish_omp_range_for (TREE_VEC_ELT (orig_declv, i),
- TREE_VEC_ELT (declv, i));
+ error_at (OMP_CLAUSE_LOCATION (ordered_cl),
+ "%<ordered%> clause parameter is less than %<collapse%>");
+ OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
+ = build_int_cst (NULL_TREE, collapse);
+ ordered = collapse;
+ }
+
+ gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
+ count = ordered ? ordered : collapse;
+
+ if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
+ {
+ cp_parser_error (parser, "for statement expected");
+ return NULL;
}
- else
- body = push_stmt_list ();
- if (inscan)
- cp_parser_omp_scan_loop_body (parser);
- else
- cp_parser_statement (parser, NULL_TREE, false, if_p);
- if (orig_declv)
- body = finish_omp_structured_block (body);
- else
- body = pop_stmt_list (body);
- if (declv == NULL_TREE)
+ /* Initialize parse state for recursive descent. */
+ data.declv = make_tree_vec (count);
+ data.initv = make_tree_vec (count);
+ data.condv = make_tree_vec (count);
+ data.incrv = make_tree_vec (count);
+ data.pre_body = NULL_TREE;
+ data.for_loc = cp_lexer_peek_token (parser->lexer)->location;
+ data.count = count;
+ data.depth = 0;
+ data.want_nested_loop = true;
+ data.ordered = ordered > 0;
+ data.in_intervening_code = false;
+ data.perfect_nesting_fail = false;
+ data.fail = false;
+ data.inscan = inscan;
+ data.saw_intervening_code = false;
+ data.code = code;
+ data.orig_declv = NULL_TREE;
+ data.clauses = clauses;
+ data.cclauses = cclauses;
+ data.ordered_cl = ordered_cl;
+ parser->omp_for_parse_state = &data;
+
+ cp_parser_omp_loop_nest (parser, if_p);
+
+ /* Bomb out early if there was an error (not enough loops, etc). */
+ if (data.fail || data.declv == NULL_TREE)
+ {
+ parser->omp_for_parse_state = save_data;
+ return NULL_TREE;
+ }
+
+ /* Relink the init and body blocks that were built during parsing. At
+ this point we have a structure nested like
+ init 0
+ body 0
+ init 1
+ body 1
+ init 2
+ body 2
+ and we want to turn it into
+ init 0
+ init 1
+ init 2
+ omp_for
+ body 0
+ body 1
+ body 2
+ We also need to flatten the init blocks, as some code for later
+ processing of combined directives gets confused otherwise. */
+
+ gcc_assert (vec_safe_length (data.init_blockv) == count);
+ gcc_assert (vec_safe_length (data.body_blockv) == count);
+ gcc_assert (vec_safe_length (data.init_placeholderv) == count);
+ gcc_assert (vec_safe_length (data.body_placeholderv) == count);
+
+ /* First insert markers for structured blocks for intervening code in
+ the loop bodies. */
+ for (unsigned int i = 0; i < count - 1; i++)
+ {
+ bool good = find_structured_blocks (&(data.body_blockv[i]),
+ data.init_placeholderv[i+1]);
+ gcc_assert (good);
+ }
+
+ /* Do the substitution from the inside out. */
+ for (unsigned int i = count - 1; i > 0; i--)
+ {
+ substitute_in_tree (&(data.body_blockv[i-1]),
+ data.init_placeholderv[i],
+ data.body_blockv[i], false);
+ substitute_in_tree (&(data.init_blockv[i-1]),
+ data.body_placeholderv[i-1],
+ data.init_blockv[i], true);
+ }
+
+ /* Generate the OMP_FOR. Note finish_omp_for adds the OMP_FOR
+ (and possibly other stuff) to the current statement list but
+ returns a pointer to the OMP_FOR itself, or null in case of error. */
+ result = push_stmt_list ();
+ ret = finish_omp_for (loc_first, code, data.declv, data.orig_declv,
+ data.initv, data.condv, data.incrv,
+ data.body_blockv[0],
+ data.pre_body, &data.orig_inits, data.clauses);
+ result = pop_stmt_list (result);
+
+ /* Check for errors involving lb/ub/incr expressions referencing
+ variables declared in intervening code. */
+ if (data.saw_intervening_code
+ && !c_omp_check_loop_binding_exprs (ret, &data.orig_inits))
ret = NULL_TREE;
- else
- ret = finish_omp_for (loc_first, code, declv, orig_declv, initv, condv,
- incrv, body, pre_body, &orig_inits, clauses);
- while (nbraces)
+ if (ret)
{
- if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
- {
- cp_lexer_consume_token (parser->lexer);
- nbraces--;
- }
- else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
- cp_lexer_consume_token (parser->lexer);
- else
+ /* Splice the omp_for into the nest of init blocks. */
+ substitute_in_tree (&(data.init_blockv[0]),
+ data.body_placeholderv[count - 1],
+ result, true);
+
+ /* Some later processing for combined directives assumes
+ that the BIND_EXPR containing range for variables appears
+ at top level in the OMP_FOR body. Fix that up if it's
+ not the case, e.g. because there is intervening code. */
+ if (code != OACC_LOOP)
+ finish_omp_for_block (data.init_blockv[0], ret);
+
+ /* Clean up the block subblock/superblock links. Per comment in
+ begin_compound_stmt, "we don't build BLOCK nodes when processing
+ templates", so skip this step in that case. */
+ if (!processing_template_decl)
{
- if (!collapse_err)
- {
- error_at (cp_lexer_peek_token (parser->lexer)->location,
- "collapsed loops not perfectly nested");
- }
- collapse_err = true;
- cp_parser_statement_seq_opt (parser, NULL);
- if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
- break;
+ tree superblock = NULL_TREE;
+ cp_walk_tree (&data.init_blockv[0], fixup_blocks_walker,
+ (void *)&superblock, NULL);
}
- }
- while (!for_block->is_empty ())
- {
- tree t = for_block->pop ();
- if (TREE_CODE (t) == STATEMENT_LIST)
- add_stmt (pop_stmt_list (t));
- else
- add_stmt (t);
+ /* Finally record the result. */
+ add_stmt (data.init_blockv[0]);
}
+ parser->omp_for_parse_state = save_data;
return ret;
}
@@ -44155,7 +44721,7 @@ cp_parser_omp_loop (cp_parser *parser, cp_token *pragma_tok,
ret = cp_parser_omp_for_loop (parser, OMP_LOOP, clauses, cclauses, if_p);
cp_parser_end_omp_structured_block (parser, save);
- add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
+ add_stmt (finish_omp_structured_block (sb));
return ret;
}
@@ -44204,7 +44770,7 @@ cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
cp_parser_end_omp_structured_block (parser, save);
- add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
+ add_stmt (finish_omp_structured_block (sb));
return ret;
}
@@ -44306,7 +44872,7 @@ cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
cp_parser_end_omp_structured_block (parser, save);
- add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
+ add_stmt (finish_omp_structured_block (sb));
return ret;
}
@@ -45154,7 +45720,7 @@ cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
cp_parser_end_omp_structured_block (parser, save);
- add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
+ add_stmt (finish_omp_structured_block (sb));
return ret;
}
@@ -46189,7 +46755,15 @@ cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
int save = cp_parser_begin_omp_structured_block (parser);
tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
cp_parser_end_omp_structured_block (parser, save);
- add_stmt (finish_omp_structured_block (block));
+
+ /* Later processing of combined acc loop constructs gets confused
+ by an extra level of empty nested BIND_EXPRs, so flatten them. */
+ block = finish_omp_structured_block (block);
+ if (TREE_CODE (block) == BIND_EXPR
+ && TREE_CODE (BIND_EXPR_BODY (block)) == BIND_EXPR
+ && !BIND_EXPR_VARS (block))
+ block = BIND_EXPR_BODY (block);
+ add_stmt (block);
return stmt;
}
@@ -48541,7 +49115,7 @@ cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
if_p);
cp_parser_end_omp_structured_block (parser, save);
- add_stmt (finish_omp_for_block (finish_omp_structured_block (sb), ret));
+ add_stmt (finish_omp_structured_block (sb));
return ret;
}
@@ -49316,6 +49890,17 @@ cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
parser->lexer->in_pragma = true;
id = cp_parser_pragma_kind (pragma_tok);
+ if (parser->omp_for_parse_state
+ && parser->omp_for_parse_state->in_intervening_code
+ && id >= PRAGMA_OMP__START_
+ && id <= PRAGMA_OMP__LAST_)
+ {
+ error_at (pragma_tok->location,
+ "intervening code must not contain OpenMP directives");
+ parser->omp_for_parse_state->fail = true;
+ cp_parser_skip_to_pragma_eol (parser, pragma_tok);
+ return false;
+ }
if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
cp_ensure_no_omp_declare_simd (parser);
switch (id)
diff --git a/gcc/cp/parser.h b/gcc/cp/parser.h
index e261d7e16e4..6cbb9a8e031 100644
--- a/gcc/cp/parser.h
+++ b/gcc/cp/parser.h
@@ -435,6 +435,9 @@ struct GTY(()) cp_parser {
specification, if any, or UNKNOWN_LOCATION otherwise. */
location_t innermost_linkage_specification_location;
+ /* Pointer to state for parsing omp_loops. Managed by
+ cp_parser_omp_for_loop in parser.cc and not used outside that file. */
+ struct omp_for_parse_data * GTY((skip)) omp_for_parse_state;
};
/* In parser.cc */
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 303f72353c0..35c7705151d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -18432,7 +18432,8 @@ tsubst_omp_for_iterator (tree t, int i, tree declv, tree &orig_declv,
tree this_pre_body = NULL_TREE;
tree orig_init = NULL_TREE;
tree orig_decl = NULL_TREE;
- cp_convert_omp_range_for (this_pre_body, NULL, decl, orig_decl, init,
+ tree init_sl = NULL_TREE;
+ cp_convert_omp_range_for (this_pre_body, init_sl, decl, orig_decl, init,
orig_init, cond, incr);
if (orig_decl)
{
diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
index 8fb47fd179e..7b8a44f5792 100644
--- a/gcc/cp/semantics.cc
+++ b/gcc/cp/semantics.cc
@@ -10520,6 +10520,7 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv,
int i;
int collapse = 1;
int ordered = 0;
+ auto_vec<location_t> init_locv;
gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (initv));
gcc_assert (TREE_VEC_LENGTH (declv) == TREE_VEC_LENGTH (condv));
@@ -10548,6 +10549,28 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv,
incr = TREE_VEC_ELT (incrv, i);
elocus = locus;
+ /* We are going to throw out the init's original MODIFY_EXPR or
+ MODOP_EXPR below. Save its location so we can use it when
+ reconstructing the expression farther down. Alternatively, if the
+ initializer is a binding of the iteration variable, save
+ that location. Any of these locations in the initialization clause
+ for the current nested loop are better than using the argument locus,
+ that points to the "for" of the the outermost loop in the nest. */
+ if (init && EXPR_HAS_LOCATION (init))
+ elocus = EXPR_LOCATION (init);
+ else if (decl && INDIRECT_REF_P (decl) && EXPR_HAS_LOCATION (decl))
+ /* This can happen for class iterators. */
+ elocus = EXPR_LOCATION (decl);
+ else if (decl && DECL_P (decl))
+ {
+ if (DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION)
+ elocus = DECL_SOURCE_LOCATION (decl);
+ else if (DECL_INITIAL (decl)
+ && EXPR_HAS_LOCATION (DECL_INITIAL (decl)))
+ elocus = EXPR_LOCATION (DECL_INITIAL (decl));
+ }
+ init_locv.safe_push (elocus);
+
if (decl == NULL)
{
if (init != NULL)
@@ -10576,9 +10599,6 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv,
}
}
- if (init && EXPR_HAS_LOCATION (init))
- elocus = EXPR_LOCATION (init);
-
if (cond == global_namespace)
continue;
@@ -10625,8 +10645,8 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv,
again and going through the cp_build_modify_expr path below when
we instantiate the thing. */
TREE_VEC_ELT (initv, i)
- = build2 (MODIFY_EXPR, void_type_node, TREE_VEC_ELT (declv, i),
- TREE_VEC_ELT (initv, i));
+ = build2_loc (init_locv[i], MODIFY_EXPR, void_type_node,
+ TREE_VEC_ELT (declv, i), TREE_VEC_ELT (initv, i));
}
TREE_TYPE (stmt) = void_type_node;
@@ -10655,10 +10675,7 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv,
incr = TREE_VEC_ELT (incrv, i);
if (orig_incr)
TREE_VEC_ELT (orig_incr, i) = incr;
- elocus = locus;
-
- if (init && EXPR_HAS_LOCATION (init))
- elocus = EXPR_LOCATION (init);
+ elocus = init_locv[i];
if (!DECL_P (decl))
{
@@ -10703,7 +10720,7 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv,
init = cp_build_modify_expr (elocus, decl, NOP_EXPR, init,
tf_warning_or_error);
else
- init = build2 (MODIFY_EXPR, void_type_node, decl, init);
+ init = build2_loc (elocus, MODIFY_EXPR, void_type_node, decl, init);
if (decl == error_mark_node || init == error_mark_node)
return NULL;
@@ -10875,47 +10892,71 @@ finish_omp_for (location_t locus, enum tree_code code, tree declv,
return omp_for;
}
-/* Fix up range for decls. Those decls were pushed into BIND's BIND_EXPR_VARS
- and need to be moved into the BIND_EXPR inside of the OMP_FOR's body. */
+/* Code walker for finish_omp_for_block: extract binding of DP->var
+ from its current block and move it to a new BIND_EXPR DP->b
+ surrounding the body of DP->omp_for. */
+
+struct fofb_data {
+ tree var;
+ tree b;
+ tree omp_for;
+};
+
+static tree
+finish_omp_for_block_walker (tree *tp, int *walk_subtrees, void *dp)
+{
+ struct fofb_data *fofb = (struct fofb_data *)dp;
+ if (TREE_CODE (*tp) == BIND_EXPR)
+ for (tree *p = &BIND_EXPR_VARS (*tp); *p; p = &DECL_CHAIN (*p))
+ {
+ if (*p == fofb->var)
+ {
+ *p = DECL_CHAIN (*p);
+ if (fofb->b == NULL_TREE)
+ {
+ fofb->b = make_node (BLOCK);
+ fofb->b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
+ OMP_FOR_BODY (fofb->omp_for), fofb->b);
+ TREE_SIDE_EFFECTS (fofb->b) = 1;
+ OMP_FOR_BODY (fofb->omp_for) = fofb->b;
+ }
+ DECL_CHAIN (fofb->var) = BIND_EXPR_VARS (fofb->b);
+ BIND_EXPR_VARS (fofb->b) = fofb->var;
+ BLOCK_VARS (BIND_EXPR_BLOCK (fofb->b)) = fofb->var;
+ BLOCK_VARS (BIND_EXPR_BLOCK (*tp)) = BIND_EXPR_VARS (*tp);
+ return *tp;
+ }
+ }
+ if (TREE_CODE (*tp) != BIND_EXPR && TREE_CODE (*tp) != STATEMENT_LIST)
+ *walk_subtrees = false;
+ return NULL_TREE;
+}
+/* Fix up range for decls. Those decls were pushed into BIND's
+ BIND_EXPR_VARS, or that of a nested BIND_EXPR inside its body,
+ and need to be moved into a new BIND_EXPR surrounding OMP_FOR's body
+ so that processing of combined loop directives can find them. */
tree
finish_omp_for_block (tree bind, tree omp_for)
{
if (omp_for == NULL_TREE
|| !OMP_FOR_ORIG_DECLS (omp_for)
- || bind == NULL_TREE
- || TREE_CODE (bind) != BIND_EXPR)
+ || bind == NULL_TREE)
return bind;
- tree b = NULL_TREE;
+ struct fofb_data fofb;
+ fofb.b = NULL_TREE;
+ fofb.omp_for = omp_for;
for (int i = 0; i < TREE_VEC_LENGTH (OMP_FOR_INIT (omp_for)); i++)
if (TREE_CODE (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)) == TREE_LIST
&& TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i)))
{
tree v = TREE_CHAIN (TREE_VEC_ELT (OMP_FOR_ORIG_DECLS (omp_for), i));
- gcc_assert (BIND_EXPR_BLOCK (bind)
- && (BIND_EXPR_VARS (bind)
- == BLOCK_VARS (BIND_EXPR_BLOCK (bind))));
for (int j = 2; j < TREE_VEC_LENGTH (v); j++)
- for (tree *p = &BIND_EXPR_VARS (bind); *p; p = &DECL_CHAIN (*p))
- {
- if (*p == TREE_VEC_ELT (v, j))
- {
- tree var = *p;
- *p = DECL_CHAIN (*p);
- if (b == NULL_TREE)
- {
- b = make_node (BLOCK);
- b = build3 (BIND_EXPR, void_type_node, NULL_TREE,
- OMP_FOR_BODY (omp_for), b);
- TREE_SIDE_EFFECTS (b) = 1;
- OMP_FOR_BODY (omp_for) = b;
- }
- DECL_CHAIN (var) = BIND_EXPR_VARS (b);
- BIND_EXPR_VARS (b) = var;
- BLOCK_VARS (BIND_EXPR_BLOCK (b)) = var;
- }
- }
- BLOCK_VARS (BIND_EXPR_BLOCK (bind)) = BIND_EXPR_VARS (bind);
+ {
+ fofb.var = TREE_VEC_ELT (v, j);
+ cp_walk_tree (&bind, finish_omp_for_block_walker,
+ (void *)&fofb, NULL);
+ }
}
return bind;
}
diff --git a/gcc/testsuite/c-c++-common/goacc/tile-2.c b/gcc/testsuite/c-c++-common/goacc/tile-2.c
index 98abc903bdc..dc306703260 100644
--- a/gcc/testsuite/c-c++-common/goacc/tile-2.c
+++ b/gcc/testsuite/c-c++-common/goacc/tile-2.c
@@ -3,8 +3,8 @@ int main ()
#pragma acc parallel
{
#pragma acc loop tile (*,*)
- for (int ix = 0; ix < 30; ix++) /* { dg-error "not enough" "" { target c } } */
- ; /* { dg-error "not enough" "" { target c++ } } */
+ for (int ix = 0; ix < 30; ix++) /* { dg-error "not enough" } */
+ ;
#pragma acc loop tile (*,*)
for (int ix = 0; ix < 30; ix++)
diff --git a/gcc/testsuite/g++.dg/gomp/attrs-imperfect1.C b/gcc/testsuite/g++.dg/gomp/attrs-imperfect1.C
new file mode 100644
index 00000000000..cf293b5081c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/attrs-imperfect1.C
@@ -0,0 +1,38 @@
+/* { dg-do compile { target c++11 } } */
+
+/* This test case is expected to fail due to errors. */
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+ [[ omp :: directive (for, collapse(3)) ]]
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ [[ omp :: directive (barrier) ]] ; /* { dg-error "intervening code must not contain OpenMP directives" } */
+ f1 (1, j);
+ if (i == 2)
+ continue; /* { dg-error "invalid exit" } */
+ else
+ break; /* { dg-error "invalid exit" } */
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ for (k = 0; k < a3; k++) /* { dg-error "loop not permitted in intervening code " } */
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (0, i);
+ }
+}
diff --git a/gcc/testsuite/g++.dg/gomp/attrs-imperfect2.C b/gcc/testsuite/g++.dg/gomp/attrs-imperfect2.C
new file mode 100644
index 00000000000..0c9154dd10c
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/attrs-imperfect2.C
@@ -0,0 +1,34 @@
+/* { dg-do compile { target c++11 } } */
+
+/* This test case is expected to fail due to errors. */
+
+/* These functions that are part of the OpenMP runtime API would ordinarily
+ be declared in omp.h, but we don't have that here. */
+extern int omp_get_num_threads(void);
+extern int omp_get_max_threads(void);
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+ [[ omp :: directive (for, collapse(3)) ]]
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ for (j = 0; j < omp_get_num_threads (); j++) /* This is OK */
+ {
+ f1 (1, omp_get_num_threads ()); /* { dg-error "not permitted in intervening code" } */
+ for (k = omp_get_num_threads (); k < a3; k++) /* This is OK */
+ {
+ f1 (2, omp_get_num_threads ());
+ f2 (2, omp_get_max_threads ());
+ }
+ f2 (1, omp_get_max_threads ()); /* { dg-error "not permitted in intervening code" } */
+ }
+ f2 (0, i);
+ }
+}
+
+
diff --git a/gcc/testsuite/g++.dg/gomp/attrs-imperfect3.C b/gcc/testsuite/g++.dg/gomp/attrs-imperfect3.C
new file mode 100644
index 00000000000..6b612afd355
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/attrs-imperfect3.C
@@ -0,0 +1,33 @@
+/* { dg-do compile { target c++11 } } */
+
+/* This test case is expected to fail due to errors. */
+
+/* Test that the imperfectly-nested loops with the ordered clause gives
+ an error, and that there is only one error (and not one on every
+ intervening statement). */
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+ [[ omp :: directive (for, ordered(3)) ]]
+ for (i = 0; i < a1; i++) /* { dg-error "inner loops must be perfectly nested" } */
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ f2 (0, i);
+ }
+}
+
diff --git a/gcc/testsuite/g++.dg/gomp/attrs-imperfect4.C b/gcc/testsuite/g++.dg/gomp/attrs-imperfect4.C
new file mode 100644
index 00000000000..16636ab3eb6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/attrs-imperfect4.C
@@ -0,0 +1,33 @@
+/* { dg-do compile { target c++11 } } */
+
+/* This test case is expected to fail due to errors. */
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+ [[ omp :: directive (for, collapse(4)) ]]
+ for (i = 0; i < a1; i++) /* { dg-error "not enough nested loops" } */
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ /* According to the grammar, this is intervening code; we
+ don't know that we are also missing a nested for loop
+ until we have parsed this whole compound expression. */
+ [[ omp :: directive (barrier) ]] ; /* { dg-error "intervening code must not contain OpenMP directives" } */
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ f2 (0, i);
+ }
+}
+
diff --git a/gcc/testsuite/g++.dg/gomp/attrs-imperfect5.C b/gcc/testsuite/g++.dg/gomp/attrs-imperfect5.C
new file mode 100644
index 00000000000..301307262a9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/attrs-imperfect5.C
@@ -0,0 +1,57 @@
+/* { dg-do compile { target c++11 } } */
+
+/* This test case is expected to fail due to errors. */
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+int ijk (int x, int y, int z);
+void f3 (int sum);
+
+/* This function isn't particularly meaningful, but it should compile without
+ error. */
+int s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+ int r = 0;
+
+ [[ omp :: directive (simd, collapse(3), reduction (inscan, +:r)) ]]
+ for (i = 0; i < a1; i++)
+ {
+ for (j = 0; j < a2; j++)
+ {
+ for (k = 0; k < a3; k++)
+ {
+ r = r + ijk (i, j, k);
+ [[ omp :: directive (scan, exclusive (r)) ]] ;
+ f3 (r);
+ }
+ }
+ }
+ return r;
+}
+
+/* Adding intervening code should trigger an error. */
+int s2 (int a1, int a2, int a3)
+{
+ int i, j, k;
+ int r = 0;
+
+ [[ omp :: directive (simd, collapse(3), reduction (inscan, +:r)) ]]
+ for (i = 0; i < a1; i++) /* { dg-error "inner loops must be perfectly nested" } */
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ r = r + ijk (i, j, k);
+ [[ omp :: directive (scan, exclusive (r)) ]] ;
+ f3 (r);
+ }
+ f2 (1, j);
+ }
+ f2 (0, i);
+ }
+ return r;
+}
diff --git a/gcc/testsuite/g++.dg/gomp/pr41967.C b/gcc/testsuite/g++.dg/gomp/pr41967.C
index 0eb489e8bee..7b59f831fe0 100644
--- a/gcc/testsuite/g++.dg/gomp/pr41967.C
+++ b/gcc/testsuite/g++.dg/gomp/pr41967.C
@@ -11,7 +11,7 @@ foo ()
{
for (int j = 0; j < 5; ++j)
++sum;
- ++sum; // { dg-error "collapsed loops not perfectly nested" }
+ ++sum;
}
return sum;
}
diff --git a/gcc/testsuite/g++.dg/gomp/tpl-imperfect-gotos.C b/gcc/testsuite/g++.dg/gomp/tpl-imperfect-gotos.C
new file mode 100644
index 00000000000..72206128fae
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/tpl-imperfect-gotos.C
@@ -0,0 +1,161 @@
+/* { dg-do compile } */
+
+/* This file contains tests that are expected to fail. */
+
+
+/* These jumps are all OK since they are to/from the same structured block. */
+
+template<typename T>
+void f1a (void)
+{
+#pragma omp for collapse(2)
+ for (T i = 0; i < 64; ++i)
+ {
+ goto a; a:;
+ for (T j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump around loop body to/from different structured blocks of intervening
+ code. */
+template<typename T>
+void f2a (void)
+{
+#pragma omp for collapse(2)
+ for (T i = 0; i < 64; ++i)
+ {
+ goto a; a:;
+ if (i > 16) goto b; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ for (T j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump into loop body from intervening code. */
+template<typename T>
+void f3a (void)
+{
+#pragma omp for collapse(2)
+ for (T i = 0; i < 64; ++i)
+ {
+ goto a; a:;
+ if (i > 16) goto c; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ for (T j = 0; j < 64; ++j)
+ {
+ c: /* { dg-error "jump to label .c." } */
+ ;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump out of loop body to intervening code. */
+template<typename T>
+void f4a (void)
+{
+#pragma omp for collapse(2)
+ for (T i = 0; i < 64; ++i)
+ {
+ goto a; a:;
+ for (T j = 0; j < 64; ++j)
+ if (i > 16) goto c; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ c:
+ ;
+ goto b; b:;
+ }
+}
+
+/* The next group of tests use the GNU extension for local labels. Expected
+ behavior is the same as the above group. */
+
+/* These jumps are all OK since they are to/from the same structured block. */
+
+template<typename T>
+void f1b (void)
+{
+#pragma omp for collapse(2)
+ for (T i = 0; i < 64; ++i)
+ {
+ __label__ a, b, c;
+ goto a; a:;
+ for (T j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump around loop body to/from different structured blocks of intervening
+ code. */
+template<typename T>
+void f2b (void)
+{
+#pragma omp for collapse(2)
+ for (T i = 0; i < 64; ++i)
+ {
+ __label__ a, b, c;
+ goto a; a:;
+ if (i > 16) goto b; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ for (T j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump into loop body from intervening code. */
+template<typename T>
+void f3b (void)
+{
+#pragma omp for collapse(2)
+ for (T i = 0; i < 64; ++i)
+ {
+ __label__ a, b, c;
+ goto a; a:;
+ if (i > 16) goto c; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ for (T j = 0; j < 64; ++j)
+ {
+ c: /* { dg-error "jump to label .c." } */
+ ;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump out of loop body to intervening code. */
+template<typename T>
+void f4b (void)
+{
+#pragma omp for collapse(2)
+ for (T i = 0; i < 64; ++i)
+ {
+ __label__ a, b, c;
+ goto a; a:;
+ for (T j = 0; j < 64; ++j)
+ if (i > 16) goto c; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ c:
+ ;
+ goto b; b:;
+ }
+}
+
+int main (void)
+{
+ f1a<int> ();
+ f2a<int> ();
+ f3a<int> ();
+ f4a<int> ();
+ f1b<int> ();
+ f2b<int> ();
+ f3b<int> ();
+ f4b<int> ();
+}
diff --git a/gcc/testsuite/g++.dg/gomp/tpl-imperfect-invalid-scope.C b/gcc/testsuite/g++.dg/gomp/tpl-imperfect-invalid-scope.C
new file mode 100644
index 00000000000..1e85e64b14a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/tpl-imperfect-invalid-scope.C
@@ -0,0 +1,94 @@
+/* { dg-do compile } */
+
+/* Check that various cases of invalid references to variables bound
+ in an intervening code scope are diagnosed and do not ICE. This test
+ is expected to produce errors. */
+
+template<typename T>
+extern void foo (T, T);
+
+template<typename T>
+void f1 (void)
+{
+#pragma omp for collapse (2)
+ for (T i = 0; i < 64; i++)
+ {
+ T v = (i + 4) * 2;
+ for (T j = v; j < 64; j++) /* { dg-error "initializer is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+template<typename T>
+void f2 (void)
+{
+#pragma omp for collapse (2)
+ for (T i = 0; i < 64; i++)
+ {
+ T v = (i + 4) * 2;
+ for (T j = 0; j < v; j++) /* { dg-error "end test is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+template<typename T>
+void f3 (void)
+{
+#pragma omp for collapse (2)
+ for (T i = 0; i < 64; i++)
+ {
+ T v = (i + 4) * 2;
+ for (T j = 0; j < 64; j = j + v) /* { dg-error "increment expression is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+template<typename T>
+void f4 (void)
+{
+#pragma omp for collapse (2)
+ for (T i = 0; i < 64; i++)
+ {
+ T v = 8;
+ for (T j = v; j < 64; j++) /* { dg-error "initializer is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+template<typename T>
+void f5 (void)
+{
+#pragma omp for collapse (2)
+ for (T i = 0; i < 64; i++)
+ {
+ T j;
+ for (j = 0; j < 64; j++) /* { dg-error "loop variable is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+template<typename T>
+void f6 (void)
+{
+#pragma omp for collapse (2)
+ for (T i = 0; i < 64; i++)
+ {
+ T j;
+ {
+ T v = 8;
+ for (j = v; j < 64; j++) /* { dg-error "loop variable is bound in intervening code" } */
+ /* { dg-error "initializer is bound in intervening code" "" { target *-*-* } .-1 } */
+ foo (i, j);
+ }
+ }
+}
+
+int main()
+{
+ f1<int> ();
+ f2<int> ();
+ f3<int> ();
+ f4<int> ();
+ f5<int> ();
+ f6<int> ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/attrs-imperfect1.C b/libgomp/testsuite/libgomp.c++/attrs-imperfect1.C
new file mode 100644
index 00000000000..4cbea6280cc
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/attrs-imperfect1.C
@@ -0,0 +1,76 @@
+/* { dg-do run } */
+
+static int f1count[3], f2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+ [[ omp :: directive (for, collapse(3)) ]]
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ f2 (0, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/attrs-imperfect2.C b/libgomp/testsuite/libgomp.c++/attrs-imperfect2.C
new file mode 100644
index 00000000000..9fb82d9c817
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/attrs-imperfect2.C
@@ -0,0 +1,114 @@
+/* { dg-do run } */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+ [[ omp :: directive (for, collapse(3)) ]]
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ {
+ g1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ {
+ g1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ {
+ g1 (2, k);
+ g2 (2, k);
+ }
+ f2 (2, k);
+ }
+ g2 (1, j);
+ }
+ f2 (1, j);
+ }
+ g2 (0, i);
+ }
+ f2 (0, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/attrs-imperfect3.C b/libgomp/testsuite/libgomp.c++/attrs-imperfect3.C
new file mode 100644
index 00000000000..51cb23aa02d
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/attrs-imperfect3.C
@@ -0,0 +1,119 @@
+/* { dg-do run } */
+
+/* Like imperfect2.c, but includes bindings in the blocks. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+ [[ omp :: directive (for, collapse(3)) ]]
+ for (i = 0; i < a1; i++)
+ {
+ int local0 = 0;
+ f1 (local0, i);
+ {
+ g1 (local0, i);
+ for (j = 0; j < a2; j++)
+ {
+ int local1 = 1;
+ f1 (local1, j);
+ {
+ g1 (local1, j);
+ for (k = 0; k < a3; k++)
+ {
+ int local2 = 2;
+ f1 (local2, k);
+ {
+ g1 (local2, k);
+ g2 (local2, k);
+ }
+ f2 (local2, k);
+ }
+ g2 (local1, j);
+ }
+ f2 (local1, j);
+ }
+ g2 (local0, i);
+ }
+ f2 (local0, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/attrs-imperfect4.C b/libgomp/testsuite/libgomp.c++/attrs-imperfect4.C
new file mode 100644
index 00000000000..cc0a034bbed
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/attrs-imperfect4.C
@@ -0,0 +1,117 @@
+/* { dg-do run } */
+
+/* Like imperfect2.c, but includes blocks that are themselves intervening
+ code. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+ [[ omp :: directive (for, collapse(3)) ]]
+ for (i = 0; i < a1; i++)
+ {
+ { f1 (0, i); }
+ {
+ g1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ { f1 (1, j); }
+ {
+ { g1 (1, j); }
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ {
+ g1 (2, k);
+ g2 (2, k);
+ }
+ f2 (2, k);
+ }
+ { g2 (1, j); }
+ }
+ { f2 (1, j); }
+ }
+ { g2 (0, i); }
+ }
+ { f2 (0, i); }
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/attrs-imperfect5.C b/libgomp/testsuite/libgomp.c++/attrs-imperfect5.C
new file mode 100644
index 00000000000..89a969db8cc
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/attrs-imperfect5.C
@@ -0,0 +1,49 @@
+/* { dg-do run } */
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+static int inner_loop_count = 0;
+static int intervening_code_count = 0;
+
+void
+g (int x, int y)
+{
+ inner_loop_count++;
+}
+
+int
+foo (int imax, int jmax)
+{
+ int j = 0;
+
+ [[ omp :: directive (for, collapse(2)) ]]
+ for (int i = 0; i < imax; ++i)
+ {
+ /* All the intervening code at the same level must be executed
+ the same number of times. */
+ ++intervening_code_count;
+ for (int j = 0; j < jmax; ++j)
+ {
+ g (i, j);
+ }
+ /* This is the outer j, not the one from the inner collapsed loop. */
+ ++j;
+ }
+ return j;
+}
+
+int
+main (void)
+{
+ int j = foo (5, 3);
+ if (j != intervening_code_count)
+ abort ();
+ if (inner_loop_count != 5 * 3)
+ abort ();
+ if (intervening_code_count < 5 || intervening_code_count > 5 * 3)
+ abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/attrs-imperfect6.C b/libgomp/testsuite/libgomp.c++/attrs-imperfect6.C
new file mode 100644
index 00000000000..01f9be123a6
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/attrs-imperfect6.C
@@ -0,0 +1,115 @@
+/* { dg-do run } */
+
+/* Like imperfect4.c, but bind the iteration variables in the loops. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+
+ [[ omp :: directive (for, collapse(3)) ]]
+ for (int i = 0; i < a1; i++)
+ {
+ { f1 (0, i); }
+ {
+ g1 (0, i);
+ for (int j = 0; j < a2; j++)
+ {
+ { f1 (1, j); }
+ {
+ { g1 (1, j); }
+ for (int k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ {
+ g1 (2, k);
+ g2 (2, k);
+ }
+ f2 (2, k);
+ }
+ { g2 (1, j); }
+ }
+ { f2 (1, j); }
+ }
+ { g2 (0, i); }
+ }
+ { f2 (0, i); }
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/imperfect-class-1.C b/libgomp/testsuite/libgomp.c++/imperfect-class-1.C
new file mode 100644
index 00000000000..3c39c42c107
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/imperfect-class-1.C
@@ -0,0 +1,169 @@
+// { dg-do run }
+// Test that class iterators and imperfectly-nested loops work together.
+// This variant tests initialization by assignment.
+
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
+typedef int T;
+typedef int S;
+
+class I
+{
+public:
+ typedef ptrdiff_t difference_type;
+ I ();
+ ~I ();
+ I (T *);
+ I (const I &);
+ T &operator * ();
+ T *operator -> ();
+ T &operator [] (const difference_type &) const;
+ I &operator = (const I &);
+ I &operator ++ ();
+ I operator ++ (int);
+ I &operator -- ();
+ I operator -- (int);
+ I &operator += (const difference_type &);
+ I &operator -= (const difference_type &);
+ I operator + (const difference_type &) const;
+ I operator - (const difference_type &) const;
+ friend bool operator == (I &, I &);
+ friend bool operator == (const I &, const I &);
+ friend bool operator < (I &, I &);
+ friend bool operator < (const I &, const I &);
+ friend bool operator <= (I &, I &);
+ friend bool operator <= (const I &, const I &);
+ friend bool operator > (I &, I &);
+ friend bool operator > (const I &, const I &);
+ friend bool operator >= (I &, I &);
+ friend bool operator >= (const I &, const I &);
+ friend typename I::difference_type operator - (I &, I &);
+ friend typename I::difference_type operator - (const I &, const I &);
+ friend I operator + (typename I::difference_type , const I &);
+private:
+ T *p;
+};
+ I::I () : p (0) {}
+ I::~I () { p = (T *) 0; }
+ I::I (T *x) : p (x) {}
+ I::I (const I &x) : p (x.p) {}
+ T &I::operator * () { return *p; }
+ T *I::operator -> () { return p; }
+ T &I::operator [] (const difference_type &x) const { return p[x]; }
+ I &I::operator = (const I &x) { p = x.p; return *this; }
+ I &I::operator ++ () { ++p; return *this; }
+ I I::operator ++ (int) { return I (p++); }
+ I &I::operator -- () { --p; return *this; }
+ I I::operator -- (int) { return I (p--); }
+ I &I::operator += (const difference_type &x) { p += x; return *this; }
+ I &I::operator -= (const difference_type &x) { p -= x; return *this; }
+ I I::operator + (const difference_type &x) const { return I (p + x); }
+ I I::operator - (const difference_type &x) const { return I (p - x); }
+ bool operator == (I &x, I &y) { return x.p == y.p; }
+ bool operator == (const I &x, const I &y) { return x.p == y.p; }
+ bool operator != (I &x, I &y) { return !(x == y); }
+ bool operator != (const I &x, const I &y) { return !(x == y); }
+ bool operator < (I &x, I &y) { return x.p < y.p; }
+ bool operator < (const I &x, const I &y) { return x.p < y.p; }
+ bool operator <= (I &x, I &y) { return x.p <= y.p; }
+ bool operator <= (const I &x, const I &y) { return x.p <= y.p; }
+ bool operator > (I &x, I &y) { return x.p > y.p; }
+ bool operator > (const I &x, const I &y) { return x.p > y.p; }
+ bool operator >= (I &x, I &y) { return x.p >= y.p; }
+ bool operator >= (const I &x, const I &y) { return x.p >= y.p; }
+ typename I::difference_type operator - (I &x, I &y) { return x.p - y.p; }
+ typename I::difference_type operator - (const I &x, const I &y) { return x.p - y.p; }
+ I operator + (typename I::difference_type x, const I &y) { return I (x + y.p); }
+
+class J
+{
+ public:
+ J(const I &x, const I &y) : b (x), e (y) {}
+ const I &begin ();
+ const I &end ();
+ private:
+ I b, e;
+};
+
+const I &J::begin () { return b; }
+const I &J::end () { return e; }
+
+static int f1count[3], f2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+void f1 (int depth)
+{
+ f1count[depth]++;
+}
+
+void f2 (int depth)
+{
+ f2count[depth]++;
+}
+
+void s1 (J a1, J a2, J a3)
+{
+ I i, j, k;
+
+#pragma omp for collapse(3)
+ for (i = a1.begin (); i < a1.end (); i++)
+ {
+ f1 (0);
+ for (j = a2.begin (); j < a2.end (); j++)
+ {
+ f1 (1);
+ for (k = a3.begin (); k < a3.end (); k++)
+ {
+ f1 (2);
+ f2 (2);
+ }
+ f2 (1);
+ }
+ f2 (0);
+ }
+}
+
+
+int
+main (void)
+{
+
+ int index[] = {0, 1, 2, 3, 4, 5};
+
+ J x (&index[0], &index[3]);
+ J y (&index[0], &index[4]);
+ J z (&index[0], &index[5]);
+
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ s1 (x, y, z);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/imperfect-class-2.C b/libgomp/testsuite/libgomp.c++/imperfect-class-2.C
new file mode 100644
index 00000000000..c6b657cabba
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/imperfect-class-2.C
@@ -0,0 +1,167 @@
+// { dg-do run }
+// Test that class iterators and imperfectly-nested loops work together.
+// This variant tests loop initialization by declaration.
+
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
+typedef int T;
+typedef int S;
+
+class I
+{
+public:
+ typedef ptrdiff_t difference_type;
+ I ();
+ ~I ();
+ I (T *);
+ I (const I &);
+ T &operator * ();
+ T *operator -> ();
+ T &operator [] (const difference_type &) const;
+ I &operator = (const I &);
+ I &operator ++ ();
+ I operator ++ (int);
+ I &operator -- ();
+ I operator -- (int);
+ I &operator += (const difference_type &);
+ I &operator -= (const difference_type &);
+ I operator + (const difference_type &) const;
+ I operator - (const difference_type &) const;
+ friend bool operator == (I &, I &);
+ friend bool operator == (const I &, const I &);
+ friend bool operator < (I &, I &);
+ friend bool operator < (const I &, const I &);
+ friend bool operator <= (I &, I &);
+ friend bool operator <= (const I &, const I &);
+ friend bool operator > (I &, I &);
+ friend bool operator > (const I &, const I &);
+ friend bool operator >= (I &, I &);
+ friend bool operator >= (const I &, const I &);
+ friend typename I::difference_type operator - (I &, I &);
+ friend typename I::difference_type operator - (const I &, const I &);
+ friend I operator + (typename I::difference_type , const I &);
+private:
+ T *p;
+};
+ I::I () : p (0) {}
+ I::~I () { p = (T *) 0; }
+ I::I (T *x) : p (x) {}
+ I::I (const I &x) : p (x.p) {}
+ T &I::operator * () { return *p; }
+ T *I::operator -> () { return p; }
+ T &I::operator [] (const difference_type &x) const { return p[x]; }
+ I &I::operator = (const I &x) { p = x.p; return *this; }
+ I &I::operator ++ () { ++p; return *this; }
+ I I::operator ++ (int) { return I (p++); }
+ I &I::operator -- () { --p; return *this; }
+ I I::operator -- (int) { return I (p--); }
+ I &I::operator += (const difference_type &x) { p += x; return *this; }
+ I &I::operator -= (const difference_type &x) { p -= x; return *this; }
+ I I::operator + (const difference_type &x) const { return I (p + x); }
+ I I::operator - (const difference_type &x) const { return I (p - x); }
+ bool operator == (I &x, I &y) { return x.p == y.p; }
+ bool operator == (const I &x, const I &y) { return x.p == y.p; }
+ bool operator != (I &x, I &y) { return !(x == y); }
+ bool operator != (const I &x, const I &y) { return !(x == y); }
+ bool operator < (I &x, I &y) { return x.p < y.p; }
+ bool operator < (const I &x, const I &y) { return x.p < y.p; }
+ bool operator <= (I &x, I &y) { return x.p <= y.p; }
+ bool operator <= (const I &x, const I &y) { return x.p <= y.p; }
+ bool operator > (I &x, I &y) { return x.p > y.p; }
+ bool operator > (const I &x, const I &y) { return x.p > y.p; }
+ bool operator >= (I &x, I &y) { return x.p >= y.p; }
+ bool operator >= (const I &x, const I &y) { return x.p >= y.p; }
+ typename I::difference_type operator - (I &x, I &y) { return x.p - y.p; }
+ typename I::difference_type operator - (const I &x, const I &y) { return x.p - y.p; }
+ I operator + (typename I::difference_type x, const I &y) { return I (x + y.p); }
+
+class J
+{
+ public:
+ J(const I &x, const I &y) : b (x), e (y) {}
+ const I &begin ();
+ const I &end ();
+ private:
+ I b, e;
+};
+
+const I &J::begin () { return b; }
+const I &J::end () { return e; }
+
+static int f1count[3], f2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+void f1 (int depth)
+{
+ f1count[depth]++;
+}
+
+void f2 (int depth)
+{
+ f2count[depth]++;
+}
+
+void s1 (J a1, J a2, J a3)
+{
+#pragma omp for collapse(3)
+ for (I i = a1.begin (); i < a1.end (); i++)
+ {
+ f1 (0);
+ for (I j = a2.begin (); j < a2.end (); j++)
+ {
+ f1 (1);
+ for (I k = a3.begin (); k < a3.end (); k++)
+ {
+ f1 (2);
+ f2 (2);
+ }
+ f2 (1);
+ }
+ f2 (0);
+ }
+}
+
+
+int
+main (void)
+{
+
+ int index[] = {0, 1, 2, 3, 4, 5};
+
+ J x (&index[0], &index[3]);
+ J y (&index[0], &index[4]);
+ J z (&index[0], &index[5]);
+
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ s1 (x, y, z);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/imperfect-class-3.C b/libgomp/testsuite/libgomp.c++/imperfect-class-3.C
new file mode 100644
index 00000000000..c33826a6b36
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/imperfect-class-3.C
@@ -0,0 +1,167 @@
+// { dg-do run }
+// Test that class iterators and imperfectly-nested loops work together.
+// This variant tests range for.
+
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
+typedef int T;
+typedef int S;
+
+class I
+{
+public:
+ typedef ptrdiff_t difference_type;
+ I ();
+ ~I ();
+ I (T *);
+ I (const I &);
+ T &operator * ();
+ T *operator -> ();
+ T &operator [] (const difference_type &) const;
+ I &operator = (const I &);
+ I &operator ++ ();
+ I operator ++ (int);
+ I &operator -- ();
+ I operator -- (int);
+ I &operator += (const difference_type &);
+ I &operator -= (const difference_type &);
+ I operator + (const difference_type &) const;
+ I operator - (const difference_type &) const;
+ friend bool operator == (I &, I &);
+ friend bool operator == (const I &, const I &);
+ friend bool operator < (I &, I &);
+ friend bool operator < (const I &, const I &);
+ friend bool operator <= (I &, I &);
+ friend bool operator <= (const I &, const I &);
+ friend bool operator > (I &, I &);
+ friend bool operator > (const I &, const I &);
+ friend bool operator >= (I &, I &);
+ friend bool operator >= (const I &, const I &);
+ friend typename I::difference_type operator - (I &, I &);
+ friend typename I::difference_type operator - (const I &, const I &);
+ friend I operator + (typename I::difference_type , const I &);
+private:
+ T *p;
+};
+ I::I () : p (0) {}
+ I::~I () { p = (T *) 0; }
+ I::I (T *x) : p (x) {}
+ I::I (const I &x) : p (x.p) {}
+ T &I::operator * () { return *p; }
+ T *I::operator -> () { return p; }
+ T &I::operator [] (const difference_type &x) const { return p[x]; }
+ I &I::operator = (const I &x) { p = x.p; return *this; }
+ I &I::operator ++ () { ++p; return *this; }
+ I I::operator ++ (int) { return I (p++); }
+ I &I::operator -- () { --p; return *this; }
+ I I::operator -- (int) { return I (p--); }
+ I &I::operator += (const difference_type &x) { p += x; return *this; }
+ I &I::operator -= (const difference_type &x) { p -= x; return *this; }
+ I I::operator + (const difference_type &x) const { return I (p + x); }
+ I I::operator - (const difference_type &x) const { return I (p - x); }
+ bool operator == (I &x, I &y) { return x.p == y.p; }
+ bool operator == (const I &x, const I &y) { return x.p == y.p; }
+ bool operator != (I &x, I &y) { return !(x == y); }
+ bool operator != (const I &x, const I &y) { return !(x == y); }
+ bool operator < (I &x, I &y) { return x.p < y.p; }
+ bool operator < (const I &x, const I &y) { return x.p < y.p; }
+ bool operator <= (I &x, I &y) { return x.p <= y.p; }
+ bool operator <= (const I &x, const I &y) { return x.p <= y.p; }
+ bool operator > (I &x, I &y) { return x.p > y.p; }
+ bool operator > (const I &x, const I &y) { return x.p > y.p; }
+ bool operator >= (I &x, I &y) { return x.p >= y.p; }
+ bool operator >= (const I &x, const I &y) { return x.p >= y.p; }
+ typename I::difference_type operator - (I &x, I &y) { return x.p - y.p; }
+ typename I::difference_type operator - (const I &x, const I &y) { return x.p - y.p; }
+ I operator + (typename I::difference_type x, const I &y) { return I (x + y.p); }
+
+class J
+{
+ public:
+ J(const I &x, const I &y) : b (x), e (y) {}
+ const I &begin ();
+ const I &end ();
+ private:
+ I b, e;
+};
+
+const I &J::begin () { return b; }
+const I &J::end () { return e; }
+
+static int f1count[3], f2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+void f1 (int depth)
+{
+ f1count[depth]++;
+}
+
+void f2 (int depth)
+{
+ f2count[depth]++;
+}
+
+void s1 (J a1, J a2, J a3)
+{
+#pragma omp for collapse(3)
+ for (auto i : a1)
+ {
+ f1 (0);
+ for (auto j : a2)
+ {
+ f1 (1);
+ for (auto k : a3)
+ {
+ f1 (2);
+ f2 (2);
+ }
+ f2 (1);
+ }
+ f2 (0);
+ }
+}
+
+
+int
+main (void)
+{
+
+ int index[] = {0, 1, 2, 3, 4, 5};
+
+ J x (&index[0], &index[3]);
+ J y (&index[0], &index[4]);
+ J z (&index[0], &index[5]);
+
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ s1 (x, y, z);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/imperfect-destructor.C b/libgomp/testsuite/libgomp.c++/imperfect-destructor.C
new file mode 100644
index 00000000000..bd87760e076
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/imperfect-destructor.C
@@ -0,0 +1,135 @@
+/* { dg-do run } */
+
+/* Make sure destructors are called for class variables bound
+ in intervening code. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+
+static int ccount[3], dcount[3];
+
+class C {
+ public:
+ int n;
+ C (int nn) { n = nn; ccount[n]++; }
+ ~C () { dcount[n]++; n = 0; }
+};
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for collapse(3)
+ for (i = 0; i < a1; i++)
+ {
+ C local0(0);
+ f1 (local0.n, i);
+ {
+ g1 (local0.n, i);
+ for (j = 0; j < a2; j++)
+ {
+ C local1(1);
+ f1 (local1.n, j);
+ {
+ g1 (local1.n, j);
+ for (k = 0; k < a3; k++)
+ {
+ C local2(2);
+ f1 (local2.n, k);
+ {
+ g1 (local2.n, k);
+ g2 (local2.n, k);
+ }
+ f2 (local2.n, k);
+ }
+ g2 (local1.n, j);
+ }
+ f2 (local1.n, j);
+ }
+ g2 (local0.n, i);
+ }
+ f2 (local0.n, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+
+ /* Check that each class object declared in intervening code was
+ constructed and destructed an equal number of times. */
+ if (ccount[0] != dcount[0]) abort ();
+ if (ccount[1] != dcount[1]) abort ();
+ if (ccount[2] != dcount[2]) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/imperfect-template-1.C b/libgomp/testsuite/libgomp.c++/imperfect-template-1.C
new file mode 100644
index 00000000000..4ed96c8319b
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/imperfect-template-1.C
@@ -0,0 +1,172 @@
+// { dg-do run }
+// Test that template class iterators and imperfectly-nested loops
+// work together.
+// This variant tests initialization by assignment.
+
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
+extern "C" void abort ();
+
+template <typename T>
+class I
+{
+public:
+ typedef ptrdiff_t difference_type;
+ I ();
+ ~I ();
+ I (T *);
+ I (const I &);
+ T &operator * ();
+ T *operator -> ();
+ T &operator [] (const difference_type &) const;
+ I &operator = (const I &);
+ I &operator ++ ();
+ I operator ++ (int);
+ I &operator -- ();
+ I operator -- (int);
+ I &operator += (const difference_type &);
+ I &operator -= (const difference_type &);
+ I operator + (const difference_type &) const;
+ I operator - (const difference_type &) const;
+ template <typename S> friend bool operator == (I<S> &, I<S> &);
+ template <typename S> friend bool operator == (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator < (I<S> &, I<S> &);
+ template <typename S> friend bool operator < (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator <= (I<S> &, I<S> &);
+ template <typename S> friend bool operator <= (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator > (I<S> &, I<S> &);
+ template <typename S> friend bool operator > (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator >= (I<S> &, I<S> &);
+ template <typename S> friend bool operator >= (const I<S> &, const I<S> &);
+ template <typename S> friend typename I<S>::difference_type operator - (I<S> &, I<S> &);
+ template <typename S> friend typename I<S>::difference_type operator - (const I<S> &, const I<S> &);
+ template <typename S> friend I<S> operator + (typename I<S>::difference_type , const I<S> &);
+private:
+ T *p;
+};
+template <typename T> I<T>::I () : p (0) {}
+template <typename T> I<T>::~I () { p = (T *) 0; }
+template <typename T> I<T>::I (T *x) : p (x) {}
+template <typename T> I<T>::I (const I &x) : p (x.p) {}
+template <typename T> T &I<T>::operator * () { return *p; }
+template <typename T> T *I<T>::operator -> () { return p; }
+template <typename T> T &I<T>::operator [] (const difference_type &x) const { return p[x]; }
+template <typename T> I<T> &I<T>::operator = (const I &x) { p = x.p; return *this; }
+template <typename T> I<T> &I<T>::operator ++ () { ++p; return *this; }
+template <typename T> I<T> I<T>::operator ++ (int) { return I (p++); }
+template <typename T> I<T> &I<T>::operator -- () { --p; return *this; }
+template <typename T> I<T> I<T>::operator -- (int) { return I (p--); }
+template <typename T> I<T> &I<T>::operator += (const difference_type &x) { p += x; return *this; }
+template <typename T> I<T> &I<T>::operator -= (const difference_type &x) { p -= x; return *this; }
+template <typename T> I<T> I<T>::operator + (const difference_type &x) const { return I (p + x); }
+template <typename T> I<T> I<T>::operator - (const difference_type &x) const { return I (p - x); }
+template <typename T> bool operator == (I<T> &x, I<T> &y) { return x.p == y.p; }
+template <typename T> bool operator == (const I<T> &x, const I<T> &y) { return x.p == y.p; }
+template <typename T> bool operator != (I<T> &x, I<T> &y) { return !(x == y); }
+template <typename T> bool operator != (const I<T> &x, const I<T> &y) { return !(x == y); }
+template <typename T> bool operator < (I<T> &x, I<T> &y) { return x.p < y.p; }
+template <typename T> bool operator < (const I<T> &x, const I<T> &y) { return x.p < y.p; }
+template <typename T> bool operator <= (I<T> &x, I<T> &y) { return x.p <= y.p; }
+template <typename T> bool operator <= (const I<T> &x, const I<T> &y) { return x.p <= y.p; }
+template <typename T> bool operator > (I<T> &x, I<T> &y) { return x.p > y.p; }
+template <typename T> bool operator > (const I<T> &x, const I<T> &y) { return x.p > y.p; }
+template <typename T> bool operator >= (I<T> &x, I<T> &y) { return x.p >= y.p; }
+template <typename T> bool operator >= (const I<T> &x, const I<T> &y) { return x.p >= y.p; }
+template <typename T> typename I<T>::difference_type operator - (I<T> &x, I<T> &y) { return x.p - y.p; }
+template <typename T> typename I<T>::difference_type operator - (const I<T> &x, const I<T> &y) { return x.p - y.p; }
+template <typename T> I<T> operator + (typename I<T>::difference_type x, const I<T> &y) { return I<T> (x + y.p); }
+
+template <typename T>
+class J
+{
+public:
+ J(const I<T> &x, const I<T> &y) : b (x), e (y) {}
+ const I<T> &begin ();
+ const I<T> &end ();
+private:
+ I<T> b, e;
+};
+
+template <typename T> const I<T> &J<T>::begin () { return b; }
+template <typename T> const I<T> &J<T>::end () { return e; }
+
+static int f1count[3], f2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+void f1 (int depth)
+{
+ f1count[depth]++;
+}
+
+void f2 (int depth)
+{
+ f2count[depth]++;
+}
+
+template <typename T>
+void s1 (J<T> a1, J<T> a2, J<T> a3)
+{
+ I<T> i, j, k;
+
+#pragma omp for collapse(3)
+ for (i = a1.begin (); i < a1.end (); i++)
+ {
+ f1 (0);
+ for (j = a2.begin (); j < a2.end (); j++)
+ {
+ f1 (1);
+ for (k = a3.begin (); k < a3.end (); k++)
+ {
+ f1 (2);
+ f2 (2);
+ }
+ f2 (1);
+ }
+ f2 (0);
+ }
+}
+
+
+int
+main (void)
+{
+
+ int index[] = {0, 1, 2, 3, 4, 5};
+
+ J<int> x (&index[0], &index[3]);
+ J<int> y (&index[0], &index[4]);
+ J<int> z (&index[0], &index[5]);
+
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ s1<int> (x, y, z);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/imperfect-template-2.C b/libgomp/testsuite/libgomp.c++/imperfect-template-2.C
new file mode 100644
index 00000000000..a41c87c481f
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/imperfect-template-2.C
@@ -0,0 +1,170 @@
+// { dg-do run }
+// Test that template class iterators and imperfectly-nested loops
+// work together.
+// This variant tests initialization by declaration.
+
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
+extern "C" void abort ();
+
+template <typename T>
+class I
+{
+public:
+ typedef ptrdiff_t difference_type;
+ I ();
+ ~I ();
+ I (T *);
+ I (const I &);
+ T &operator * ();
+ T *operator -> ();
+ T &operator [] (const difference_type &) const;
+ I &operator = (const I &);
+ I &operator ++ ();
+ I operator ++ (int);
+ I &operator -- ();
+ I operator -- (int);
+ I &operator += (const difference_type &);
+ I &operator -= (const difference_type &);
+ I operator + (const difference_type &) const;
+ I operator - (const difference_type &) const;
+ template <typename S> friend bool operator == (I<S> &, I<S> &);
+ template <typename S> friend bool operator == (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator < (I<S> &, I<S> &);
+ template <typename S> friend bool operator < (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator <= (I<S> &, I<S> &);
+ template <typename S> friend bool operator <= (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator > (I<S> &, I<S> &);
+ template <typename S> friend bool operator > (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator >= (I<S> &, I<S> &);
+ template <typename S> friend bool operator >= (const I<S> &, const I<S> &);
+ template <typename S> friend typename I<S>::difference_type operator - (I<S> &, I<S> &);
+ template <typename S> friend typename I<S>::difference_type operator - (const I<S> &, const I<S> &);
+ template <typename S> friend I<S> operator + (typename I<S>::difference_type , const I<S> &);
+private:
+ T *p;
+};
+template <typename T> I<T>::I () : p (0) {}
+template <typename T> I<T>::~I () { p = (T *) 0; }
+template <typename T> I<T>::I (T *x) : p (x) {}
+template <typename T> I<T>::I (const I &x) : p (x.p) {}
+template <typename T> T &I<T>::operator * () { return *p; }
+template <typename T> T *I<T>::operator -> () { return p; }
+template <typename T> T &I<T>::operator [] (const difference_type &x) const { return p[x]; }
+template <typename T> I<T> &I<T>::operator = (const I &x) { p = x.p; return *this; }
+template <typename T> I<T> &I<T>::operator ++ () { ++p; return *this; }
+template <typename T> I<T> I<T>::operator ++ (int) { return I (p++); }
+template <typename T> I<T> &I<T>::operator -- () { --p; return *this; }
+template <typename T> I<T> I<T>::operator -- (int) { return I (p--); }
+template <typename T> I<T> &I<T>::operator += (const difference_type &x) { p += x; return *this; }
+template <typename T> I<T> &I<T>::operator -= (const difference_type &x) { p -= x; return *this; }
+template <typename T> I<T> I<T>::operator + (const difference_type &x) const { return I (p + x); }
+template <typename T> I<T> I<T>::operator - (const difference_type &x) const { return I (p - x); }
+template <typename T> bool operator == (I<T> &x, I<T> &y) { return x.p == y.p; }
+template <typename T> bool operator == (const I<T> &x, const I<T> &y) { return x.p == y.p; }
+template <typename T> bool operator != (I<T> &x, I<T> &y) { return !(x == y); }
+template <typename T> bool operator != (const I<T> &x, const I<T> &y) { return !(x == y); }
+template <typename T> bool operator < (I<T> &x, I<T> &y) { return x.p < y.p; }
+template <typename T> bool operator < (const I<T> &x, const I<T> &y) { return x.p < y.p; }
+template <typename T> bool operator <= (I<T> &x, I<T> &y) { return x.p <= y.p; }
+template <typename T> bool operator <= (const I<T> &x, const I<T> &y) { return x.p <= y.p; }
+template <typename T> bool operator > (I<T> &x, I<T> &y) { return x.p > y.p; }
+template <typename T> bool operator > (const I<T> &x, const I<T> &y) { return x.p > y.p; }
+template <typename T> bool operator >= (I<T> &x, I<T> &y) { return x.p >= y.p; }
+template <typename T> bool operator >= (const I<T> &x, const I<T> &y) { return x.p >= y.p; }
+template <typename T> typename I<T>::difference_type operator - (I<T> &x, I<T> &y) { return x.p - y.p; }
+template <typename T> typename I<T>::difference_type operator - (const I<T> &x, const I<T> &y) { return x.p - y.p; }
+template <typename T> I<T> operator + (typename I<T>::difference_type x, const I<T> &y) { return I<T> (x + y.p); }
+
+template <typename T>
+class J
+{
+public:
+ J(const I<T> &x, const I<T> &y) : b (x), e (y) {}
+ const I<T> &begin ();
+ const I<T> &end ();
+private:
+ I<T> b, e;
+};
+
+template <typename T> const I<T> &J<T>::begin () { return b; }
+template <typename T> const I<T> &J<T>::end () { return e; }
+
+static int f1count[3], f2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+void f1 (int depth)
+{
+ f1count[depth]++;
+}
+
+void f2 (int depth)
+{
+ f2count[depth]++;
+}
+
+template <typename T>
+void s1 (J<T> a1, J<T> a2, J<T> a3)
+{
+#pragma omp for collapse(3)
+ for (I<T> i = a1.begin (); i < a1.end (); i++)
+ {
+ f1 (0);
+ for (I<T> j = a2.begin (); j < a2.end (); j++)
+ {
+ f1 (1);
+ for (I<T> k = a3.begin (); k < a3.end (); k++)
+ {
+ f1 (2);
+ f2 (2);
+ }
+ f2 (1);
+ }
+ f2 (0);
+ }
+}
+
+
+int
+main (void)
+{
+
+ int index[] = {0, 1, 2, 3, 4, 5};
+
+ J<int> x (&index[0], &index[3]);
+ J<int> y (&index[0], &index[4]);
+ J<int> z (&index[0], &index[5]);
+
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ s1<int> (x, y, z);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c++/imperfect-template-3.C b/libgomp/testsuite/libgomp.c++/imperfect-template-3.C
new file mode 100644
index 00000000000..2e464ed5510
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c++/imperfect-template-3.C
@@ -0,0 +1,170 @@
+// { dg-do run }
+// Test that template class iterators and imperfectly-nested loops
+// work together.
+// This variant tests range for syntax.
+
+typedef __PTRDIFF_TYPE__ ptrdiff_t;
+extern "C" void abort ();
+
+template <typename T>
+class I
+{
+public:
+ typedef ptrdiff_t difference_type;
+ I ();
+ ~I ();
+ I (T *);
+ I (const I &);
+ T &operator * ();
+ T *operator -> ();
+ T &operator [] (const difference_type &) const;
+ I &operator = (const I &);
+ I &operator ++ ();
+ I operator ++ (int);
+ I &operator -- ();
+ I operator -- (int);
+ I &operator += (const difference_type &);
+ I &operator -= (const difference_type &);
+ I operator + (const difference_type &) const;
+ I operator - (const difference_type &) const;
+ template <typename S> friend bool operator == (I<S> &, I<S> &);
+ template <typename S> friend bool operator == (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator < (I<S> &, I<S> &);
+ template <typename S> friend bool operator < (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator <= (I<S> &, I<S> &);
+ template <typename S> friend bool operator <= (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator > (I<S> &, I<S> &);
+ template <typename S> friend bool operator > (const I<S> &, const I<S> &);
+ template <typename S> friend bool operator >= (I<S> &, I<S> &);
+ template <typename S> friend bool operator >= (const I<S> &, const I<S> &);
+ template <typename S> friend typename I<S>::difference_type operator - (I<S> &, I<S> &);
+ template <typename S> friend typename I<S>::difference_type operator - (const I<S> &, const I<S> &);
+ template <typename S> friend I<S> operator + (typename I<S>::difference_type , const I<S> &);
+private:
+ T *p;
+};
+template <typename T> I<T>::I () : p (0) {}
+template <typename T> I<T>::~I () { p = (T *) 0; }
+template <typename T> I<T>::I (T *x) : p (x) {}
+template <typename T> I<T>::I (const I &x) : p (x.p) {}
+template <typename T> T &I<T>::operator * () { return *p; }
+template <typename T> T *I<T>::operator -> () { return p; }
+template <typename T> T &I<T>::operator [] (const difference_type &x) const { return p[x]; }
+template <typename T> I<T> &I<T>::operator = (const I &x) { p = x.p; return *this; }
+template <typename T> I<T> &I<T>::operator ++ () { ++p; return *this; }
+template <typename T> I<T> I<T>::operator ++ (int) { return I (p++); }
+template <typename T> I<T> &I<T>::operator -- () { --p; return *this; }
+template <typename T> I<T> I<T>::operator -- (int) { return I (p--); }
+template <typename T> I<T> &I<T>::operator += (const difference_type &x) { p += x; return *this; }
+template <typename T> I<T> &I<T>::operator -= (const difference_type &x) { p -= x; return *this; }
+template <typename T> I<T> I<T>::operator + (const difference_type &x) const { return I (p + x); }
+template <typename T> I<T> I<T>::operator - (const difference_type &x) const { return I (p - x); }
+template <typename T> bool operator == (I<T> &x, I<T> &y) { return x.p == y.p; }
+template <typename T> bool operator == (const I<T> &x, const I<T> &y) { return x.p == y.p; }
+template <typename T> bool operator != (I<T> &x, I<T> &y) { return !(x == y); }
+template <typename T> bool operator != (const I<T> &x, const I<T> &y) { return !(x == y); }
+template <typename T> bool operator < (I<T> &x, I<T> &y) { return x.p < y.p; }
+template <typename T> bool operator < (const I<T> &x, const I<T> &y) { return x.p < y.p; }
+template <typename T> bool operator <= (I<T> &x, I<T> &y) { return x.p <= y.p; }
+template <typename T> bool operator <= (const I<T> &x, const I<T> &y) { return x.p <= y.p; }
+template <typename T> bool operator > (I<T> &x, I<T> &y) { return x.p > y.p; }
+template <typename T> bool operator > (const I<T> &x, const I<T> &y) { return x.p > y.p; }
+template <typename T> bool operator >= (I<T> &x, I<T> &y) { return x.p >= y.p; }
+template <typename T> bool operator >= (const I<T> &x, const I<T> &y) { return x.p >= y.p; }
+template <typename T> typename I<T>::difference_type operator - (I<T> &x, I<T> &y) { return x.p - y.p; }
+template <typename T> typename I<T>::difference_type operator - (const I<T> &x, const I<T> &y) { return x.p - y.p; }
+template <typename T> I<T> operator + (typename I<T>::difference_type x, const I<T> &y) { return I<T> (x + y.p); }
+
+template <typename T>
+class J
+{
+public:
+ J(const I<T> &x, const I<T> &y) : b (x), e (y) {}
+ const I<T> &begin ();
+ const I<T> &end ();
+private:
+ I<T> b, e;
+};
+
+template <typename T> const I<T> &J<T>::begin () { return b; }
+template <typename T> const I<T> &J<T>::end () { return e; }
+
+static int f1count[3], f2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+void f1 (int depth)
+{
+ f1count[depth]++;
+}
+
+void f2 (int depth)
+{
+ f2count[depth]++;
+}
+
+template <typename T>
+void s1 (J<T> a1, J<T> a2, J<T> a3)
+{
+#pragma omp for collapse(3)
+ for (auto i : a1)
+ {
+ f1 (0);
+ for (auto j : a2)
+ {
+ f1 (1);
+ for (auto k : a3)
+ {
+ f1 (2);
+ f2 (2);
+ }
+ f2 (1);
+ }
+ f2 (0);
+ }
+}
+
+
+int
+main (void)
+{
+
+ int index[] = {0, 1, 2, 3, 4, 5};
+
+ J<int> x (&index[0], &index[3]);
+ J<int> y (&index[0], &index[4]);
+ J<int> z (&index[0], &index[5]);
+
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ s1<int> (x, y, z);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
--
2.31.1
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH V2 4/5] OpenMP: New C/C++ testcases for imperfectly nested loops.
2023-07-23 22:15 [PATCH V2 0/5] OpenMP: support for imperfectly-nested loops Sandra Loosemore
` (2 preceding siblings ...)
2023-07-23 22:15 ` [PATCH V2 3/5] OpenMP: C++ " Sandra Loosemore
@ 2023-07-23 22:15 ` Sandra Loosemore
2023-08-22 13:34 ` Jakub Jelinek
2023-07-23 22:15 ` [PATCH V2 5/5] OpenMP: Fortran support for imperfectly-nested loops Sandra Loosemore
2023-08-22 12:56 ` [PATCH V2 0/5] OpenMP: " Jakub Jelinek
5 siblings, 1 reply; 15+ messages in thread
From: Sandra Loosemore @ 2023-07-23 22:15 UTC (permalink / raw)
To: gcc-patches; +Cc: jakub
gcc/testsuite/ChangeLog
* c-c++-common/gomp/imperfect-attributes.c: New.
* c-c++-common/gomp/imperfect-badloops.c: New.
* c-c++-common/gomp/imperfect-blocks.c: New.
* c-c++-common/gomp/imperfect-extension.c: New.
* c-c++-common/gomp/imperfect-gotos.c: New.
* c-c++-common/gomp/imperfect-invalid-scope.c: New.
* c-c++-common/gomp/imperfect-labels.c: New.
* c-c++-common/gomp/imperfect-legacy-syntax.c: New.
* c-c++-common/gomp/imperfect-pragmas.c: New.
* c-c++-common/gomp/imperfect1.c: New.
* c-c++-common/gomp/imperfect2.c: New.
* c-c++-common/gomp/imperfect3.c: New.
* c-c++-common/gomp/imperfect4.c: New.
* c-c++-common/gomp/imperfect5.c: New.
libgomp/ChangeLog
* testsuite/libgomp.c-c++-common/imperfect1.c: New.
* testsuite/libgomp.c-c++-common/imperfect2.c: New.
* testsuite/libgomp.c-c++-common/imperfect3.c: New.
* testsuite/libgomp.c-c++-common/imperfect4.c: New.
* testsuite/libgomp.c-c++-common/imperfect5.c: New.
* testsuite/libgomp.c-c++-common/imperfect6.c: New.
* testsuite/libgomp.c-c++-common/target-imperfect1.c: New.
* testsuite/libgomp.c-c++-common/target-imperfect2.c: New.
* testsuite/libgomp.c-c++-common/target-imperfect3.c: New.
* testsuite/libgomp.c-c++-common/target-imperfect4.c: New.
---
.../c-c++-common/gomp/imperfect-attributes.c | 81 ++++++++
.../c-c++-common/gomp/imperfect-badloops.c | 50 +++++
.../c-c++-common/gomp/imperfect-blocks.c | 75 ++++++++
.../c-c++-common/gomp/imperfect-extension.c | 55 ++++++
.../c-c++-common/gomp/imperfect-gotos.c | 174 ++++++++++++++++++
.../gomp/imperfect-invalid-scope.c | 77 ++++++++
.../c-c++-common/gomp/imperfect-labels.c | 85 +++++++++
.../gomp/imperfect-legacy-syntax.c | 44 +++++
.../c-c++-common/gomp/imperfect-pragmas.c | 85 +++++++++
gcc/testsuite/c-c++-common/gomp/imperfect1.c | 38 ++++
gcc/testsuite/c-c++-common/gomp/imperfect2.c | 34 ++++
gcc/testsuite/c-c++-common/gomp/imperfect3.c | 33 ++++
gcc/testsuite/c-c++-common/gomp/imperfect4.c | 33 ++++
gcc/testsuite/c-c++-common/gomp/imperfect5.c | 57 ++++++
.../libgomp.c-c++-common/imperfect1.c | 76 ++++++++
.../libgomp.c-c++-common/imperfect2.c | 114 ++++++++++++
.../libgomp.c-c++-common/imperfect3.c | 119 ++++++++++++
.../libgomp.c-c++-common/imperfect4.c | 117 ++++++++++++
.../libgomp.c-c++-common/imperfect5.c | 49 +++++
.../libgomp.c-c++-common/imperfect6.c | 115 ++++++++++++
.../libgomp.c-c++-common/target-imperfect1.c | 81 ++++++++
.../libgomp.c-c++-common/target-imperfect2.c | 122 ++++++++++++
.../libgomp.c-c++-common/target-imperfect3.c | 125 +++++++++++++
.../libgomp.c-c++-common/target-imperfect4.c | 122 ++++++++++++
24 files changed, 1961 insertions(+)
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-attributes.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-badloops.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-blocks.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-extension.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-gotos.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-invalid-scope.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-labels.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-legacy-syntax.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect-pragmas.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect1.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect2.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect3.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect4.c
create mode 100644 gcc/testsuite/c-c++-common/gomp/imperfect5.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect1.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect2.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect3.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect4.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect5.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/imperfect6.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-imperfect1.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-imperfect2.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-imperfect3.c
create mode 100644 libgomp/testsuite/libgomp.c-c++-common/target-imperfect4.c
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect-attributes.c b/gcc/testsuite/c-c++-common/gomp/imperfect-attributes.c
new file mode 100644
index 00000000000..776295ce22a
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect-attributes.c
@@ -0,0 +1,81 @@
+/* { dg-do compile { target { c || c++11 } } } */
+
+/* Check that a nested FOR loop with standard c/c++ attributes on it
+ is treated as intervening code, since it doesn't match the grammar
+ for canonical loop nest form. */
+
+extern void do_something (void);
+
+void imperfect1 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ {
+ [[]] for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+}
+
+void perfect1 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ /* { dg-error "inner loops must be perfectly nested" "" { target *-*-*} .-1 } */
+ {
+ [[]] for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+}
+
+/* Similar, but put the attributes on a block wrapping the nested loop
+ instead. */
+
+void imperfect2 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ {
+ [[]]
+ {
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+ }
+}
+
+void perfect2 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ /* { dg-error "inner loops must be perfectly nested" "" { target *-*-*} .-1 } */
+ {
+ [[]]
+ {
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+ }
+}
+
+/* Make sure attributes are accepted in the innermost loop body, which has
+ no intervening code restrictions. */
+
+void imperfect3 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++)
+ for (int j = 0; j < y; j++)
+ {
+ [[]] do_something ();
+ }
+}
+
+void perfect3 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++)
+ for (int j = 0; j < y; j++)
+ {
+ [[]] do_something ();
+ }
+}
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect-badloops.c b/gcc/testsuite/c-c++-common/gomp/imperfect-badloops.c
new file mode 100644
index 00000000000..dfd40b6cb2d
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect-badloops.c
@@ -0,0 +1,50 @@
+/* { dg-do compile } */
+
+/* This test case is expected to fail due to errors. */
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+void do_something (void);
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for collapse(3)
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ if (a1 < a2)
+ {
+ int z = 0;
+ while (z < i) /* { dg-error "loop not permitted in intervening code " } */
+ {
+ do_something ();
+ z++;
+ }
+ do /* { dg-error "loop not permitted in intervening code " } */
+ {
+ do_something ();
+ z--;
+ } while (z >= 0);
+ }
+ for (j = 0; j < a2; j++)
+ {
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ if (a1 < a3)
+ {
+ int z;
+ for (z = 0; z < i; z++) /* { dg-error "loop not permitted in intervening code " } */
+ {
+ do_something ();
+ }
+ }
+ f2 (0, i);
+ }
+}
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect-blocks.c b/gcc/testsuite/c-c++-common/gomp/imperfect-blocks.c
new file mode 100644
index 00000000000..0fea58faf78
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect-blocks.c
@@ -0,0 +1,75 @@
+/* { dg-do compile } */
+
+/* Check that compound statements in intervening code are correctly
+ handled. */
+
+extern void do_something (void);
+
+void imperfect1 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++)
+ {
+ {}
+ for (int j = 0; j < y; j++)
+ do_something ();
+ }
+}
+
+void perfect1 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++) /* { dg-error "inner loops must be perfectly nested" } */
+ {
+ {}
+ for (int j = 0; j < y; j++)
+ do_something ();
+ }
+}
+
+void imperfect2 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++)
+ {
+ for (int j = 0; j < y; j++)
+ do_something ();
+ {}
+ }
+}
+
+void perfect2 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++) /* { dg-error "inner loops must be perfectly nested" } */
+ {
+ for (int j = 0; j < y; j++)
+ do_something ();
+ {}
+ }
+}
+
+void imperfect3 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++)
+ {
+ { do_something (); }
+ for (int j = 0; j < y; j++)
+ do_something ();
+ { do_something (); }
+ }
+}
+
+void perfect3 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++) /* { dg-error "inner loops must be perfectly nested" } */
+ {
+ { do_something (); }
+ for (int j = 0; j < y; j++)
+ do_something ();
+ { do_something (); }
+ }
+}
+
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect-extension.c b/gcc/testsuite/c-c++-common/gomp/imperfect-extension.c
new file mode 100644
index 00000000000..a8a8f9ebe1d
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect-extension.c
@@ -0,0 +1,55 @@
+/* { dg-do compile } */
+
+/* Check that __extension__ introduces intervening code. */
+
+extern void do_something (void);
+
+void imperfect1 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ {
+ __extension__ ({
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ });
+ }
+}
+
+void perfect1 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ /* { dg-error "inner loops must be perfectly nested" "" { target *-*-*} .-1 } */
+ {
+ __extension__ ({
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ });
+ }
+}
+
+/* Check that we don't barf on __extension__ in the inner loop body. */
+void imperfect2 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++)
+ for (int j = 0; j < y; j++)
+ {
+ __extension__ ({
+ do_something ();
+ });
+ }
+}
+
+void perfect2 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++)
+ for (int j = 0; j < y; j++)
+ {
+ __extension__ ({
+ do_something ();
+ });
+ }
+}
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect-gotos.c b/gcc/testsuite/c-c++-common/gomp/imperfect-gotos.c
new file mode 100644
index 00000000000..897eed275ec
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect-gotos.c
@@ -0,0 +1,174 @@
+/* { dg-do compile } */
+
+/* This file contains tests that are expected to fail. */
+
+
+/* These jumps are all OK since they are to/from the same structured block. */
+
+void f1a (void)
+{
+#pragma omp for collapse(2)
+ for (int i = 0; i < 64; ++i)
+ {
+ goto a; a:;
+ for (int j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump around loop body to/from different structured blocks of intervening
+ code. */
+void f2a (void)
+{
+#pragma omp for collapse(2)
+ for (int i = 0; i < 64; ++i)
+ {
+ goto a; a:;
+ if (i > 16) goto b; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ for (int j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump into loop body from intervening code. */
+void f3a (void)
+{
+#pragma omp for collapse(2)
+ for (int i = 0; i < 64; ++i)
+ {
+ goto a; a:;
+ if (i > 16) goto c; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ for (int j = 0; j < 64; ++j)
+ {
+ c:
+ ;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump out of loop body to intervening code. */
+void f4a (void)
+{
+#pragma omp for collapse(2)
+ for (int i = 0; i < 64; ++i)
+ {
+ goto a; a:;
+ for (int j = 0; j < 64; ++j)
+ if (i > 16) goto c; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ c:
+ ;
+ goto b; b:;
+ }
+}
+
+/* The next group of tests use the GNU extension for local labels. Expected
+ behavior is the same as the above group. */
+
+/* These jumps are all OK since they are to/from the same structured block. */
+
+void f1b (void)
+{
+#pragma omp for collapse(2)
+ for (int i = 0; i < 64; ++i)
+ {
+ __label__ a, b, c;
+ goto a; a:;
+ for (int j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump around loop body to/from different structured blocks of intervening
+ code. */
+void f2b (void)
+{
+#pragma omp for collapse(2)
+ for (int i = 0; i < 64; ++i)
+ {
+ __label__ a, b, c;
+ goto a; a:;
+ if (i > 16) goto b; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ for (int j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump into loop body from intervening code. */
+void f3b (void)
+{
+#pragma omp for collapse(2)
+ for (int i = 0; i < 64; ++i)
+ {
+ __label__ a, b, c;
+ goto a; a:;
+ if (i > 16) goto c; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ for (int j = 0; j < 64; ++j)
+ {
+ c:
+ ;
+ }
+ goto b; b:;
+ }
+}
+
+/* Jump out of loop body to intervening code. */
+void f4b (void)
+{
+#pragma omp for collapse(2)
+ for (int i = 0; i < 64; ++i)
+ {
+ __label__ a, b, c;
+ goto a; a:;
+ for (int j = 0; j < 64; ++j)
+ if (i > 16) goto c; /* { dg-error "invalid branch to/from OpenMP structured block" } */
+ c:
+ ;
+ goto b; b:;
+ }
+}
+
+/* Test that the even the valid jumps are rejected when intervening code
+ is not allowed at all. */
+
+void f1c (void)
+{
+#pragma omp for ordered(2)
+ for (int i = 0; i < 64; ++i) /* { dg-error "inner loops must be perfectly nested" } */
+ {
+ goto a; a:;
+ for (int j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
+void f1d (void)
+{
+#pragma omp for ordered(2)
+ for (int i = 0; i < 64; ++i) /* { dg-error "inner loops must be perfectly nested" } */
+ {
+ __label__ a, b, c;
+ goto a; a:;
+ for (int j = 0; j < 64; ++j)
+ {
+ goto c; c:;
+ }
+ goto b; b:;
+ }
+}
+
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect-invalid-scope.c b/gcc/testsuite/c-c++-common/gomp/imperfect-invalid-scope.c
new file mode 100644
index 00000000000..5c24aae07cc
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect-invalid-scope.c
@@ -0,0 +1,77 @@
+/* { dg-do compile } */
+
+/* Check that various cases of invalid references to variables bound
+ in an intervening code scope are diagnosed and do not ICE. This test
+ is expected to produce errors. */
+
+extern void foo (int, int);
+
+void f1 (void)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < 64; i++)
+ {
+ int v = (i + 4) * 2;
+ for (int j = v; j < 64; j++) /* { dg-error "initializer is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+void f2 (void)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < 64; i++)
+ {
+ int v = (i + 4) * 2;
+ for (int j = 0; j < v; j++) /* { dg-error "end test is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+void f3 (void)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < 64; i++)
+ {
+ int v = (i + 4) * 2;
+ for (int j = 0; j < 64; j = j + v) /* { dg-error "increment expression is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+void f4 (void)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < 64; i++)
+ {
+ int v = 8;
+ for (int j = v; j < 64; j++) /* { dg-error "initializer is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+void f5 (void)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < 64; i++)
+ {
+ int j;
+ for (j = 0; j < 64; j++) /* { dg-error "loop variable is bound in intervening code" } */
+ foo (i, j);
+ }
+}
+
+void f6 (void)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < 64; i++)
+ {
+ int j;
+ {
+ int v = 8;
+ for (j = v; j < 64; j++) /* { dg-error "loop variable is bound in intervening code" } */
+ /* { dg-error "initializer is bound in intervening code" "" { target *-*-* } .-1 } */
+ foo (i, j);
+ }
+ }
+}
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect-labels.c b/gcc/testsuite/c-c++-common/gomp/imperfect-labels.c
new file mode 100644
index 00000000000..b7a7a4c8358
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect-labels.c
@@ -0,0 +1,85 @@
+/* { dg-do compile } */
+
+/* Check that a nested FOR loop with a label on it is treated as
+ intervening code, since it doesn't match the grammar for canonical
+ loop nest form. */
+
+extern void do_something (void);
+
+void imperfect1 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ {
+ foo:
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+}
+
+void perfect1 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ /* { dg-error "inner loops must be perfectly nested" "" { target *-*-*} .-1 } */
+ {
+ foo:
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+}
+
+
+/* Similar, but put the label on a block wrapping the nested loop instead. */
+
+void imperfect2 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ {
+ foo:
+ {
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+ }
+}
+
+void perfect2 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ /* { dg-error "inner loops must be perfectly nested" "" { target *-*-*} .-1 } */
+ {
+ foo:
+ {
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+ }
+}
+
+/* Sanity check that labels are allowed in the innermost loop body. */
+
+void imperfect3 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++)
+ for (int j = 0; j < y; j++)
+ {
+ foo:
+ do_something ();
+ }
+}
+
+void perfect3 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++)
+ for (int j = 0; j < y; j++)
+ {
+ foo:
+ do_something ();
+ }
+}
+
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect-legacy-syntax.c b/gcc/testsuite/c-c++-common/gomp/imperfect-legacy-syntax.c
new file mode 100644
index 00000000000..571e067091b
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect-legacy-syntax.c
@@ -0,0 +1,44 @@
+/* { dg-do compile } */
+
+/* Braces may enclose a nested FOR even when intervening code is not
+ permitted. Before GCC implemented OpenMP 5.1 canonical loop syntax
+ and support for intervening code, it used to ignore empty statements
+ instead of treating them as intervening code; as an extension, those
+ are still accepted without complaint even in constructs where intervening
+ code is not supposed to be valid. */
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for ordered(3)
+ for (i = 0; i < a1; i++)
+ {
+ for (j = 0; j < a2; j++)
+ {
+ for (k = 0; k < a3; k++)
+ {
+ }
+ }
+ }
+}
+
+void s2 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for ordered(3)
+ for (i = 0; i < a1; i++)
+ {
+ ;
+ for (j = 0; j < a2; j++)
+ {
+ ;
+ for (k = 0; k < a3; k++)
+ {
+ }
+ ;
+ }
+ ;
+ }
+}
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect-pragmas.c b/gcc/testsuite/c-c++-common/gomp/imperfect-pragmas.c
new file mode 100644
index 00000000000..a9f55224158
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect-pragmas.c
@@ -0,0 +1,85 @@
+/* { dg-do compile } */
+
+/* Check that non-statement pragmas are accepted in a canonical loop nest
+ even when perfect nesting is required. */
+
+extern void do_something (void);
+
+void imperfect1 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++)
+ {
+#pragma GCC diagnostic push
+ for (int j = 0; j < y; j++)
+ do_something ();
+#pragma GCC diagnostic pop
+ }
+}
+
+void perfect1 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++)
+ {
+#pragma GCC diagnostic push
+ for (int j = 0; j < y; j++)
+ do_something ();
+#pragma GCC diagnostic pop
+ }
+}
+
+
+/* "GCC unroll" is a statement pragma that consumes the following loop as
+ a substatement. Thus, the inner loop should be treated as intervening
+ code rather than part of the loop nest. */
+
+void imperfect2 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ {
+#pragma GCC unroll 4
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+}
+
+void perfect2 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++) /* { dg-error "not enough nested loops" } */
+ /* { dg-error "inner loops must be perfectly nested" "" { target *-*-*} .-1 } */
+ {
+#pragma GCC unroll 4
+ for (int j = 0; j < y; j++) /* { dg-error "loop not permitted in intervening code" } */
+ do_something ();
+ }
+}
+
+
+/* Check that statement pragmas are accepted in the innermost loop body. */
+
+void imperfect3 (int x, int y)
+{
+#pragma omp for collapse (2)
+ for (int i = 0; i < x; i++)
+ for (int j = 0; j < y; j++)
+ {
+#pragma GCC unroll 4
+ for (int k = 0; k < 4; k++)
+ do_something ();
+ }
+}
+
+void perfect3 (int x, int y)
+{
+#pragma omp for ordered (2)
+ for (int i = 0; i < x; i++)
+ for (int j = 0; j < y; j++)
+ {
+#pragma GCC unroll 4
+ for (int k = 0; k < 4; k++)
+ do_something ();
+ }
+}
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect1.c b/gcc/testsuite/c-c++-common/gomp/imperfect1.c
new file mode 100644
index 00000000000..705626ad169
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect1.c
@@ -0,0 +1,38 @@
+/* { dg-do compile } */
+
+/* This test case is expected to fail due to errors. */
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for collapse(3)
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+#pragma omp barrier /* { dg-error "intervening code must not contain OpenMP directives" } */
+ f1 (1, j);
+ if (i == 2)
+ continue; /* { dg-error "invalid exit" } */
+ else
+ break; /* { dg-error "invalid exit" } */
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ for (k = 0; k < a3; k++) /* { dg-error "loop not permitted in intervening code " } */
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (0, i);
+ }
+}
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect2.c b/gcc/testsuite/c-c++-common/gomp/imperfect2.c
new file mode 100644
index 00000000000..dff17dd3ca5
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect2.c
@@ -0,0 +1,34 @@
+/* { dg-do compile } */
+
+/* This test case is expected to fail due to errors. */
+
+/* These functions that are part of the OpenMP runtime API would ordinarily
+ be declared in omp.h, but we don't have that here. */
+extern int omp_get_num_threads(void);
+extern int omp_get_max_threads(void);
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+#pragma omp for collapse(3)
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ for (j = 0; j < omp_get_num_threads (); j++) /* This is OK */
+ {
+ f1 (1, omp_get_num_threads ()); /* { dg-error "not permitted in intervening code" } */
+ for (k = omp_get_num_threads (); k < a3; k++) /* This is OK */
+ {
+ f1 (2, omp_get_num_threads ());
+ f2 (2, omp_get_max_threads ());
+ }
+ f2 (1, omp_get_max_threads ()); /* { dg-error "not permitted in intervening code" } */
+ }
+ f2 (0, i);
+ }
+}
+
+
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect3.c b/gcc/testsuite/c-c++-common/gomp/imperfect3.c
new file mode 100644
index 00000000000..ad727ed3170
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect3.c
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+
+/* This test case is expected to fail due to errors. */
+
+/* Test that the imperfectly-nested loops with the ordered clause gives
+ an error, and that there is only one error (and not one on every
+ intervening statement). */
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for ordered(3)
+ for (i = 0; i < a1; i++) /* { dg-error "inner loops must be perfectly nested" } */
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ f2 (0, i);
+ }
+}
+
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect4.c b/gcc/testsuite/c-c++-common/gomp/imperfect4.c
new file mode 100644
index 00000000000..1a0c07cd48e
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect4.c
@@ -0,0 +1,33 @@
+/* { dg-do compile } */
+
+/* This test case is expected to fail due to errors. */
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for collapse(4)
+ for (i = 0; i < a1; i++) /* { dg-error "not enough nested loops" } */
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ /* According to the grammar, this is intervening code; we
+ don't know that we are also missing a nested for loop
+ until we have parsed this whole compound expression. */
+#pragma omp barrier /* { dg-error "intervening code must not contain OpenMP directives" } */
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ f2 (0, i);
+ }
+}
+
diff --git a/gcc/testsuite/c-c++-common/gomp/imperfect5.c b/gcc/testsuite/c-c++-common/gomp/imperfect5.c
new file mode 100644
index 00000000000..585d89ff789
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/imperfect5.c
@@ -0,0 +1,57 @@
+/* { dg-do compile } */
+
+/* This test case is expected to fail due to errors. */
+
+int f1 (int depth, int iter);
+int f2 (int depth, int iter);
+int ijk (int x, int y, int z);
+void f3 (int sum);
+
+/* This function isn't particularly meaningful, but it should compile without
+ error. */
+int s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+ int r = 0;
+
+#pragma omp simd collapse(3) reduction (inscan, +:r)
+ for (i = 0; i < a1; i++)
+ {
+ for (j = 0; j < a2; j++)
+ {
+ for (k = 0; k < a3; k++)
+ {
+ r = r + ijk (i, j, k);
+#pragma omp scan exclusive (r)
+ f3 (r);
+ }
+ }
+ }
+ return r;
+}
+
+/* Adding intervening code should trigger an error. */
+int s2 (int a1, int a2, int a3)
+{
+ int i, j, k;
+ int r = 0;
+
+#pragma omp simd collapse(3) reduction (inscan, +:r)
+ for (i = 0; i < a1; i++) /* { dg-error "inner loops must be perfectly nested" } */
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ r = r + ijk (i, j, k);
+#pragma omp scan exclusive (r)
+ f3 (r);
+ }
+ f2 (1, j);
+ }
+ f2 (0, i);
+ }
+ return r;
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/imperfect1.c b/libgomp/testsuite/libgomp.c-c++-common/imperfect1.c
new file mode 100644
index 00000000000..cafdcaf25b0
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/imperfect1.c
@@ -0,0 +1,76 @@
+/* { dg-do run } */
+
+static int f1count[3], f2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for collapse(3)
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ f2 (0, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/imperfect2.c b/libgomp/testsuite/libgomp.c-c++-common/imperfect2.c
new file mode 100644
index 00000000000..e2098006eab
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/imperfect2.c
@@ -0,0 +1,114 @@
+/* { dg-do run } */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for collapse(3)
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ {
+ g1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ {
+ g1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ {
+ g1 (2, k);
+ g2 (2, k);
+ }
+ f2 (2, k);
+ }
+ g2 (1, j);
+ }
+ f2 (1, j);
+ }
+ g2 (0, i);
+ }
+ f2 (0, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/imperfect3.c b/libgomp/testsuite/libgomp.c-c++-common/imperfect3.c
new file mode 100644
index 00000000000..feb5e32d1d6
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/imperfect3.c
@@ -0,0 +1,119 @@
+/* { dg-do run } */
+
+/* Like imperfect2.c, but includes bindings in the blocks. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for collapse(3)
+ for (i = 0; i < a1; i++)
+ {
+ int local0 = 0;
+ f1 (local0, i);
+ {
+ g1 (local0, i);
+ for (j = 0; j < a2; j++)
+ {
+ int local1 = 1;
+ f1 (local1, j);
+ {
+ g1 (local1, j);
+ for (k = 0; k < a3; k++)
+ {
+ int local2 = 2;
+ f1 (local2, k);
+ {
+ g1 (local2, k);
+ g2 (local2, k);
+ }
+ f2 (local2, k);
+ }
+ g2 (local1, j);
+ }
+ f2 (local1, j);
+ }
+ g2 (local0, i);
+ }
+ f2 (local0, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/imperfect4.c b/libgomp/testsuite/libgomp.c-c++-common/imperfect4.c
new file mode 100644
index 00000000000..e29301bfbad
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/imperfect4.c
@@ -0,0 +1,117 @@
+/* { dg-do run } */
+
+/* Like imperfect2.c, but includes blocks that are themselves intervening
+ code. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp for collapse(3)
+ for (i = 0; i < a1; i++)
+ {
+ { f1 (0, i); }
+ {
+ g1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ { f1 (1, j); }
+ {
+ { g1 (1, j); }
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ {
+ g1 (2, k);
+ g2 (2, k);
+ }
+ f2 (2, k);
+ }
+ { g2 (1, j); }
+ }
+ { f2 (1, j); }
+ }
+ { g2 (0, i); }
+ }
+ { f2 (0, i); }
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/imperfect5.c b/libgomp/testsuite/libgomp.c-c++-common/imperfect5.c
new file mode 100644
index 00000000000..7bd4f12d472
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/imperfect5.c
@@ -0,0 +1,49 @@
+/* { dg-do run } */
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+static int inner_loop_count = 0;
+static int intervening_code_count = 0;
+
+void
+g (int x, int y)
+{
+ inner_loop_count++;
+}
+
+int
+foo (int imax, int jmax)
+{
+ int j = 0;
+
+#pragma omp for collapse(2)
+ for (int i = 0; i < imax; ++i)
+ {
+ /* All the intervening code at the same level must be executed
+ the same number of times. */
+ ++intervening_code_count;
+ for (int j = 0; j < jmax; ++j)
+ {
+ g (i, j);
+ }
+ /* This is the outer j, not the one from the inner collapsed loop. */
+ ++j;
+ }
+ return j;
+}
+
+int
+main (void)
+{
+ int j = foo (5, 3);
+ if (j != intervening_code_count)
+ abort ();
+ if (inner_loop_count != 5 * 3)
+ abort ();
+ if (intervening_code_count < 5 || intervening_code_count > 5 * 3)
+ abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/imperfect6.c b/libgomp/testsuite/libgomp.c-c++-common/imperfect6.c
new file mode 100644
index 00000000000..808c6540890
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/imperfect6.c
@@ -0,0 +1,115 @@
+/* { dg-do run } */
+
+/* Like imperfect4.c, but bind the iteration variables in the loops. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+
+#pragma omp for collapse(3)
+ for (int i = 0; i < a1; i++)
+ {
+ { f1 (0, i); }
+ {
+ g1 (0, i);
+ for (int j = 0; j < a2; j++)
+ {
+ { f1 (1, j); }
+ {
+ { g1 (1, j); }
+ for (int k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ {
+ g1 (2, k);
+ g2 (2, k);
+ }
+ f2 (2, k);
+ }
+ { g2 (1, j); }
+ }
+ { f2 (1, j); }
+ }
+ { g2 (0, i); }
+ }
+ { f2 (0, i); }
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-imperfect1.c b/libgomp/testsuite/libgomp.c-c++-common/target-imperfect1.c
new file mode 100644
index 00000000000..53bc611ace3
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/target-imperfect1.c
@@ -0,0 +1,81 @@
+/* { dg-do run } */
+
+/* Like imperfect1.c, but enables offloading. */
+
+static int f1count[3], f2count[3];
+#pragma omp declare target enter (f1count, f2count)
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ #pragma omp atomic
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ #pragma omp atomic
+ f2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp target parallel for collapse(3) map(always, tofrom:f1count, f2count)
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ f2 (2, k);
+ }
+ f2 (1, j);
+ }
+ f2 (0, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-imperfect2.c b/libgomp/testsuite/libgomp.c-c++-common/target-imperfect2.c
new file mode 100644
index 00000000000..bc2901a517e
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/target-imperfect2.c
@@ -0,0 +1,122 @@
+/* { dg-do run } */
+
+/* Like imperfect2.c, but enables offloading. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+#pragma omp declare target enter (f1count, f2count)
+#pragma omp declare target enter (g1count, g2count)
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ #pragma omp atomic
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ #pragma omp atomic
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ #pragma omp atomic
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ #pragma omp atomic
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp target parallel for collapse(3) map(always, tofrom:f1count, f2count, g1count, g2count)
+ for (i = 0; i < a1; i++)
+ {
+ f1 (0, i);
+ {
+ g1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ f1 (1, j);
+ {
+ g1 (1, j);
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ {
+ g1 (2, k);
+ g2 (2, k);
+ }
+ f2 (2, k);
+ }
+ g2 (1, j);
+ }
+ f2 (1, j);
+ }
+ g2 (0, i);
+ }
+ f2 (0, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-imperfect3.c b/libgomp/testsuite/libgomp.c-c++-common/target-imperfect3.c
new file mode 100644
index 00000000000..ddcfcf4b7eb
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/target-imperfect3.c
@@ -0,0 +1,125 @@
+/* { dg-do run } */
+
+/* Like imperfect3.c, but enables offloading. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+#pragma omp declare target enter (f1count, f2count)
+#pragma omp declare target enter (g1count, g2count)
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ #pragma omp atomic
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ #pragma omp atomic
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ #pragma omp atomic
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ #pragma omp atomic
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp target parallel for collapse(3) map(always, tofrom:f1count, f2count, g1count, g2count)
+ for (i = 0; i < a1; i++)
+ {
+ int local0 = 0;
+ f1 (local0, i);
+ {
+ g1 (local0, i);
+ for (j = 0; j < a2; j++)
+ {
+ int local1 = 1;
+ f1 (local1, j);
+ {
+ g1 (local1, j);
+ for (k = 0; k < a3; k++)
+ {
+ int local2 = 2;
+ f1 (local2, k);
+ {
+ g1 (local2, k);
+ g2 (local2, k);
+ }
+ f2 (local2, k);
+ }
+ g2 (local1, j);
+ }
+ f2 (local1, j);
+ }
+ g2 (local0, i);
+ }
+ f2 (local0, i);
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-imperfect4.c b/libgomp/testsuite/libgomp.c-c++-common/target-imperfect4.c
new file mode 100644
index 00000000000..ede488977b8
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/target-imperfect4.c
@@ -0,0 +1,122 @@
+/* { dg-do run } */
+
+/* Like imperfect4.c, but enables offloading. */
+
+static int f1count[3], f2count[3];
+static int g1count[3], g2count[3];
+#pragma omp declare target enter (f1count, f2count)
+#pragma omp declare target enter (g1count, g2count)
+
+#ifndef __cplusplus
+extern void abort (void);
+#else
+extern "C" void abort (void);
+#endif
+
+int f1 (int depth, int iter)
+{
+ #pragma omp atomic
+ f1count[depth]++;
+ return iter;
+}
+
+int f2 (int depth, int iter)
+{
+ #pragma omp atomic
+ f2count[depth]++;
+ return iter;
+}
+
+int g1 (int depth, int iter)
+{
+ #pragma omp atomic
+ g1count[depth]++;
+ return iter;
+}
+
+int g2 (int depth, int iter)
+{
+ #pragma omp atomic
+ g2count[depth]++;
+ return iter;
+}
+
+void s1 (int a1, int a2, int a3)
+{
+ int i, j, k;
+
+#pragma omp target parallel for collapse(3) map(always, tofrom:f1count, f2count, g1count, g2count)
+ for (i = 0; i < a1; i++)
+ {
+ { f1 (0, i); }
+ {
+ g1 (0, i);
+ for (j = 0; j < a2; j++)
+ {
+ { f1 (1, j); }
+ {
+ { g1 (1, j); }
+ for (k = 0; k < a3; k++)
+ {
+ f1 (2, k);
+ {
+ g1 (2, k);
+ g2 (2, k);
+ }
+ f2 (2, k);
+ }
+ { g2 (1, j); }
+ }
+ { f2 (1, j); }
+ }
+ { g2 (0, i); }
+ }
+ { f2 (0, i); }
+ }
+}
+
+int
+main (void)
+{
+ f1count[0] = 0;
+ f1count[1] = 0;
+ f1count[2] = 0;
+ f2count[0] = 0;
+ f2count[1] = 0;
+ f2count[2] = 0;
+
+ g1count[0] = 0;
+ g1count[1] = 0;
+ g1count[2] = 0;
+ g2count[0] = 0;
+ g2count[1] = 0;
+ g2count[2] = 0;
+
+ s1 (3, 4, 5);
+
+ /* All intervening code at the same depth must be executed the same
+ number of times. */
+ if (f1count[0] != f2count[0]) abort ();
+ if (f1count[1] != f2count[1]) abort ();
+ if (f1count[2] != f2count[2]) abort ();
+ if (g1count[0] != f1count[0]) abort ();
+ if (g2count[0] != f1count[0]) abort ();
+ if (g1count[1] != f1count[1]) abort ();
+ if (g2count[1] != f1count[1]) abort ();
+ if (g1count[2] != f1count[2]) abort ();
+ if (g2count[2] != f1count[2]) abort ();
+
+ /* Intervening code must be executed at least as many times as the loop
+ that encloses it. */
+ if (f1count[0] < 3) abort ();
+ if (f1count[1] < 3 * 4) abort ();
+
+ /* Intervening code must not be executed more times than the number
+ of logical iterations. */
+ if (f1count[0] > 3 * 4 * 5) abort ();
+ if (f1count[1] > 3 * 4 * 5) abort ();
+
+ /* Check that the innermost loop body is executed exactly the number
+ of logical iterations expected. */
+ if (f1count[2] != 3 * 4 * 5) abort ();
+}
--
2.31.1
^ permalink raw reply [flat|nested] 15+ messages in thread
* [PATCH V2 5/5] OpenMP: Fortran support for imperfectly-nested loops
2023-07-23 22:15 [PATCH V2 0/5] OpenMP: support for imperfectly-nested loops Sandra Loosemore
` (3 preceding siblings ...)
2023-07-23 22:15 ` [PATCH V2 4/5] OpenMP: New C/C++ testcases for imperfectly nested loops Sandra Loosemore
@ 2023-07-23 22:15 ` Sandra Loosemore
2023-08-22 13:37 ` Jakub Jelinek
2023-08-22 12:56 ` [PATCH V2 0/5] OpenMP: " Jakub Jelinek
5 siblings, 1 reply; 15+ messages in thread
From: Sandra Loosemore @ 2023-07-23 22:15 UTC (permalink / raw)
To: gcc-patches; +Cc: jakub
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.
In the Fortran front end, most of the semantic processing happens during
the translation phase, so the parse phase just collects the intervening
statements, checks them for errors, and splices them around the loop body.
gcc/fortran/ChangeLog
* gfortran.h (struct gfc_namespace): Add omp_structured_block bit.
* openmp.cc: Include omp-api.h.
(resolve_omp_clauses): Consolidate inscan reduction clause conflict
checking here.
(find_nested_loop_in_chain): New.
(find_nested_loop_in_block): New.
(gfc_resolve_omp_do_blocks): Set omp_current_do_collapse properly.
Handle imperfectly-nested loops when looking for nested omp scan.
Refactor to move inscan reduction clause conflict checking to
resolve_omp_clauses.
(gfc_resolve_do_iterator): Handle imperfectly-nested loops.
(struct icode_error_state): New.
(icode_code_error_callback): New.
(icode_expr_error_callback): New.
(diagnose_intervening_code_errors_1): New.
(diagnose_intervening_code_errors): New.
(make_structured_block): New.
(restructure_intervening_code): New.
(is_outer_iteration_variable): Do not assume loops are perfectly
nested.
(check_nested_loop_in_chain): New.
(check_nested_loop_in_block_state): New.
(check_nested_loop_in_block_symbol): New.
(check_nested_loop_in_block): New.
(expr_uses_intervening_var): New.
(is_intervening_var): New.
(expr_is_invariant): Do not assume loops are perfectly nested.
(resolve_omp_do): Handle imperfectly-nested loops.
* trans-stmt.cc (gfc_trans_block_construct): Generate
OMP_STRUCTURED_BLOCK if magic bit is set on block namespace.
gcc/testsuite/ChangeLog
* gfortran.dg/gomp/collapse1.f90: Adjust expected errors.
* gfortran.dg/gomp/collapse2.f90: Likewise.
* gfortran.dg/gomp/imperfect-gotos.f90: New.
* gfortran.dg/gomp/imperfect-invalid-scope.f90: New.
* gfortran.dg/gomp/imperfect1.f90: New.
* gfortran.dg/gomp/imperfect2.f90: New.
* gfortran.dg/gomp/imperfect3.f90: New.
* gfortran.dg/gomp/imperfect4.f90: New.
* gfortran.dg/gomp/imperfect5.f90: New.
libgomp/ChangeLog
* testsuite/libgomp.fortran/imperfect-destructor.f90: New.
* testsuite/libgomp.fortran/imperfect1.f90: New.
* testsuite/libgomp.fortran/imperfect2.f90: New.
* testsuite/libgomp.fortran/imperfect3.f90: New.
* testsuite/libgomp.fortran/imperfect4.f90: New.
* testsuite/libgomp.fortran/target-imperfect1.f90: New.
* testsuite/libgomp.fortran/target-imperfect2.f90: New.
* testsuite/libgomp.fortran/target-imperfect3.f90: New.
* testsuite/libgomp.fortran/target-imperfect4.f90: New.
---
gcc/fortran/gfortran.h | 3 +
gcc/fortran/openmp.cc | 765 +++++++++++++++---
gcc/fortran/trans-stmt.cc | 7 +-
gcc/testsuite/gfortran.dg/gomp/collapse1.f90 | 6 +-
gcc/testsuite/gfortran.dg/gomp/collapse2.f90 | 10 +-
.../gfortran.dg/gomp/imperfect-gotos.f90 | 69 ++
.../gomp/imperfect-invalid-scope.f90 | 81 ++
gcc/testsuite/gfortran.dg/gomp/imperfect1.f90 | 39 +
gcc/testsuite/gfortran.dg/gomp/imperfect2.f90 | 56 ++
gcc/testsuite/gfortran.dg/gomp/imperfect3.f90 | 29 +
gcc/testsuite/gfortran.dg/gomp/imperfect4.f90 | 36 +
gcc/testsuite/gfortran.dg/gomp/imperfect5.f90 | 67 ++
.../libgomp.fortran/imperfect-destructor.f90 | 142 ++++
.../testsuite/libgomp.fortran/imperfect1.f90 | 67 ++
.../testsuite/libgomp.fortran/imperfect2.f90 | 102 +++
.../testsuite/libgomp.fortran/imperfect3.f90 | 110 +++
.../testsuite/libgomp.fortran/imperfect4.f90 | 121 +++
.../libgomp.fortran/target-imperfect1.f90 | 72 ++
.../libgomp.fortran/target-imperfect2.f90 | 110 +++
.../libgomp.fortran/target-imperfect3.f90 | 116 +++
.../libgomp.fortran/target-imperfect4.f90 | 126 +++
21 files changed, 2025 insertions(+), 109 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect-gotos.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect-invalid-scope.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect1.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect2.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect3.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect4.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/imperfect5.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect-destructor.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect1.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect2.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect3.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/imperfect4.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/target-imperfect1.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/target-imperfect2.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/target-imperfect3.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/target-imperfect4.f90
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 30631abd788..b7429e39a5b 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -2232,6 +2232,9 @@ typedef struct gfc_namespace
/* OpenMP requires. */
unsigned omp_requires:6;
unsigned omp_target_seen:1;
+
+ /* Set to 1 if this is an implicit OMP structured block. */
+ unsigned omp_structured_block:1;
}
gfc_namespace;
diff --git a/gcc/fortran/openmp.cc b/gcc/fortran/openmp.cc
index 8efc4b3ecfa..e0d88dca41b 100644
--- a/gcc/fortran/openmp.cc
+++ b/gcc/fortran/openmp.cc
@@ -30,6 +30,7 @@ along with GCC; see the file COPYING3. If not see
#include "gomp-constants.h"
#include "target-memory.h" /* For gfc_encode_character. */
#include "bitmap.h"
+#include "omp-api.h" /* For omp_runtime_api_procname. */
static gfc_statement omp_code_to_statement (gfc_code *);
@@ -7499,15 +7500,24 @@ resolve_omp_clauses (gfc_code *code, gfc_omp_clauses *omp_clauses,
gfc_error ("Object %qs is not a variable at %L", n->sym->name,
&n->where);
}
- if (omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN]
- && code->op != EXEC_OMP_DO
- && code->op != EXEC_OMP_SIMD
- && code->op != EXEC_OMP_DO_SIMD
- && code->op != EXEC_OMP_PARALLEL_DO
- && code->op != EXEC_OMP_PARALLEL_DO_SIMD)
- gfc_error ("%<inscan%> REDUCTION clause on construct other than DO, SIMD, "
- "DO SIMD, PARALLEL DO, PARALLEL DO SIMD at %L",
- &omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN]->where);
+ if (omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN])
+ {
+ locus *loc = &omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN]->where;
+ if (code->op != EXEC_OMP_DO
+ && code->op != EXEC_OMP_SIMD
+ && code->op != EXEC_OMP_DO_SIMD
+ && code->op != EXEC_OMP_PARALLEL_DO
+ && code->op != EXEC_OMP_PARALLEL_DO_SIMD)
+ gfc_error ("%<inscan%> REDUCTION clause on construct other than DO, "
+ "SIMD, DO SIMD, PARALLEL DO, PARALLEL DO SIMD at %L",
+ loc);
+ if (omp_clauses->ordered)
+ gfc_error ("ORDERED clause specified together with %<inscan%> "
+ "REDUCTION clause at %L", loc);
+ if (omp_clauses->sched_kind != OMP_SCHED_NONE)
+ gfc_error ("SCHEDULE clause specified together with %<inscan%> "
+ "REDUCTION clause at %L", loc);
+ }
for (list = 0; list < OMP_LIST_NUM; list++)
if (list != OMP_LIST_FIRSTPRIVATE
@@ -9398,68 +9408,114 @@ static struct fortran_omp_context
static gfc_code *omp_current_do_code;
static int omp_current_do_collapse;
+/* Forward declaration for mutually recursive functions. */
+static gfc_code *
+find_nested_loop_in_block (gfc_code *block);
+
+/* Return the first nested DO loop in CHAIN, or NULL if there
+ isn't one. Does no error checking on intervening code. */
+
+static gfc_code *
+find_nested_loop_in_chain (gfc_code *chain)
+{
+ gfc_code *code;
+
+ if (!chain)
+ return NULL;
+
+ for (code = chain; code; code = code->next)
+ {
+ if (code->op == EXEC_DO)
+ return code;
+ else if (code->op == EXEC_BLOCK)
+ {
+ gfc_code *c = find_nested_loop_in_block (code);
+ if (c)
+ return c;
+ }
+ }
+ return NULL;
+}
+
+/* Return the first nested DO loop in BLOCK, or NULL if there
+ isn't one. Does no error checking on intervening code. */
+static gfc_code *
+find_nested_loop_in_block (gfc_code *block)
+{
+ gfc_namespace *ns;
+ gcc_assert (block->op == EXEC_BLOCK);
+ ns = block->ext.block.ns;
+ gcc_assert (ns);
+ return find_nested_loop_in_chain (ns->code);
+}
+
void
gfc_resolve_omp_do_blocks (gfc_code *code, gfc_namespace *ns)
{
if (code->block->next && code->block->next->op == EXEC_DO)
{
int i;
- gfc_code *c;
omp_current_do_code = code->block->next;
if (code->ext.omp_clauses->orderedc)
omp_current_do_collapse = code->ext.omp_clauses->orderedc;
- else
+ else if (code->ext.omp_clauses->collapse)
omp_current_do_collapse = code->ext.omp_clauses->collapse;
- for (i = 1, c = omp_current_do_code; i < omp_current_do_collapse; i++)
- {
- c = c->block;
- if (c->op != EXEC_DO || c->next == NULL)
- break;
- c = c->next;
- if (c->op != EXEC_DO)
- break;
- }
- if (i < omp_current_do_collapse || omp_current_do_collapse <= 0)
+ else
omp_current_do_collapse = 1;
if (code->ext.omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN])
{
+ /* Checking that there is a matching EXEC_OMP_SCAN in the
+ innermost body cannot be deferred to resolve_omp_do because
+ we process directives nested in the loop before we get
+ there. */
locus *loc
= &code->ext.omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN]->where;
- if (code->ext.omp_clauses->ordered)
- gfc_error ("ORDERED clause specified together with %<inscan%> "
- "REDUCTION clause at %L", loc);
- if (code->ext.omp_clauses->sched_kind != OMP_SCHED_NONE)
- gfc_error ("SCHEDULE clause specified together with %<inscan%> "
- "REDUCTION clause at %L", loc);
- gfc_code *block = c->block ? c->block->next : NULL;
- if (block && block->op != EXEC_OMP_SCAN)
- while (block && block->next && block->next->op != EXEC_OMP_SCAN)
- block = block->next;
- if (!block
- || (block->op != EXEC_OMP_SCAN
- && (!block->next || block->next->op != EXEC_OMP_SCAN)))
- gfc_error ("With INSCAN at %L, expected loop body with !$OMP SCAN "
- "between two structured block sequences", loc);
- else
+ gfc_code *c;
+
+ for (i = 1, c = omp_current_do_code;
+ i < omp_current_do_collapse; i++)
{
- if (block->op == EXEC_OMP_SCAN)
- gfc_warning (0, "!$OMP SCAN at %L with zero executable "
- "statements in preceding structured block "
- "sequence", &block->loc);
- if ((block->op == EXEC_OMP_SCAN && !block->next)
- || (block->next && block->next->op == EXEC_OMP_SCAN
- && !block->next->next))
- gfc_warning (0, "!$OMP SCAN at %L with zero executable "
- "statements in succeeding structured block "
- "sequence", block->op == EXEC_OMP_SCAN
- ? &block->loc : &block->next->loc);
- }
- if (block && block->op != EXEC_OMP_SCAN)
- block = block->next;
- if (block && block->op == EXEC_OMP_SCAN)
- /* Mark 'omp scan' as checked; flag will be unset later. */
- block->ext.omp_clauses->if_present = true;
+ c = find_nested_loop_in_chain (c->block->next);
+ if (!c || c->op != EXEC_DO || c->block == NULL)
+ break;
+ }
+
+ /* Skip this if we don't have enough nested loops. That
+ problem will be diagnosed elsewhere. */
+ if (c && c->op == EXEC_DO)
+ {
+ gfc_code *block = c->block ? c->block->next : NULL;
+ if (block && block->op != EXEC_OMP_SCAN)
+ while (block && block->next
+ && block->next->op != EXEC_OMP_SCAN)
+ block = block->next;
+ if (!block
+ || (block->op != EXEC_OMP_SCAN
+ && (!block->next || block->next->op != EXEC_OMP_SCAN)))
+ gfc_error ("With INSCAN at %L, expected loop body with "
+ "!$OMP SCAN between two "
+ "structured block sequences", loc);
+ else
+ {
+ if (block->op == EXEC_OMP_SCAN)
+ gfc_warning (0, "!$OMP SCAN at %L with zero executable "
+ "statements in preceding structured block "
+ "sequence", &block->loc);
+ if ((block->op == EXEC_OMP_SCAN && !block->next)
+ || (block->next && block->next->op == EXEC_OMP_SCAN
+ && !block->next->next))
+ gfc_warning (0, "!$OMP SCAN at %L with zero executable "
+ "statements in succeeding structured block "
+ "sequence", block->op == EXEC_OMP_SCAN
+ ? &block->loc : &block->next->loc);
+ }
+ if (block && block->op != EXEC_OMP_SCAN)
+ block = block->next;
+ if (block && block->op == EXEC_OMP_SCAN)
+ /* Mark 'omp scan' as checked; flag will be unset later. */
+ block->ext.omp_clauses->if_present = true;
+ }
}
}
gfc_resolve_blocks (code->block, ns);
@@ -9589,13 +9645,12 @@ gfc_resolve_do_iterator (gfc_code *code, gfc_symbol *sym, bool add_clause)
private just in the !$omp do resp. !$omp parallel do construct,
with no implications for the outer parallel constructs. */
- while (i-- >= 1)
+ while (i-- >= 1 && c)
{
if (code == c)
return;
-
- c = c->block->next;
- }
+ c = find_nested_loop_in_chain (c->block->next);
+ }
/* An openacc context may represent a data clause. Abort if so. */
if (!omp_current_ctx->is_openmp && !oacc_is_loop (omp_current_ctx->code))
@@ -9634,20 +9689,464 @@ gfc_resolve_omp_local_vars (gfc_namespace *ns)
gfc_traverse_ns (ns, handle_local_var);
}
+
+/* Error checking on intervening code uses a code walker. */
+
+struct icode_error_state
+{
+ const char *name;
+ bool errorp;
+ gfc_code *nested;
+ gfc_code *next;
+};
+
+static int
+icode_code_error_callback (gfc_code **codep,
+ int *walk_subtrees ATTRIBUTE_UNUSED, void *opaque)
+{
+ gfc_code *code = *codep;
+ icode_error_state *state = (icode_error_state *)opaque;
+
+ /* gfc_code_walker walks down CODE's next chain as well as
+ walking things that are actually nested in CODE. We need to
+ special-case traversal of outer blocks, so stop immediately if we
+ are heading down such a next chain. */
+ if (code == state->next)
+ return 1;
+
+ switch (code->op)
+ {
+ case EXEC_DO:
+ case EXEC_DO_WHILE:
+ case EXEC_DO_CONCURRENT:
+ gfc_error ("%s cannot contain loop in intervening code at %L",
+ state->name, &code->loc);
+ state->errorp = true;
+ break;
+ case EXEC_CYCLE:
+ case EXEC_EXIT:
+ /* Errors have already been diagnosed in match_exit_cycle. */
+ state->errorp = true;
+ break;
+ case EXEC_OMP_CRITICAL:
+ case EXEC_OMP_DO:
+ case EXEC_OMP_FLUSH:
+ case EXEC_OMP_MASTER:
+ case EXEC_OMP_ORDERED:
+ case EXEC_OMP_PARALLEL:
+ case EXEC_OMP_PARALLEL_DO:
+ case EXEC_OMP_PARALLEL_SECTIONS:
+ case EXEC_OMP_PARALLEL_WORKSHARE:
+ case EXEC_OMP_SECTIONS:
+ case EXEC_OMP_SINGLE:
+ case EXEC_OMP_WORKSHARE:
+ case EXEC_OMP_ATOMIC:
+ case EXEC_OMP_BARRIER:
+ case EXEC_OMP_END_NOWAIT:
+ case EXEC_OMP_END_SINGLE:
+ case EXEC_OMP_TASK:
+ case EXEC_OMP_TASKWAIT:
+ case EXEC_OMP_TASKYIELD:
+ case EXEC_OMP_CANCEL:
+ case EXEC_OMP_CANCELLATION_POINT:
+ case EXEC_OMP_TASKGROUP:
+ case EXEC_OMP_SIMD:
+ case EXEC_OMP_DO_SIMD:
+ case EXEC_OMP_PARALLEL_DO_SIMD:
+ case EXEC_OMP_TARGET:
+ case EXEC_OMP_TARGET_DATA:
+ case EXEC_OMP_TEAMS:
+ case EXEC_OMP_DISTRIBUTE:
+ case EXEC_OMP_DISTRIBUTE_SIMD:
+ case EXEC_OMP_DISTRIBUTE_PARALLEL_DO:
+ case EXEC_OMP_DISTRIBUTE_PARALLEL_DO_SIMD:
+ case EXEC_OMP_TARGET_TEAMS:
+ case EXEC_OMP_TEAMS_DISTRIBUTE:
+ case EXEC_OMP_TEAMS_DISTRIBUTE_SIMD:
+ case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE:
+ case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_SIMD:
+ case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO:
+ case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO:
+ case EXEC_OMP_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
+ case EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD:
+ case EXEC_OMP_TARGET_UPDATE:
+ case EXEC_OMP_END_CRITICAL:
+ case EXEC_OMP_TARGET_ENTER_DATA:
+ case EXEC_OMP_TARGET_EXIT_DATA:
+ case EXEC_OMP_TARGET_PARALLEL:
+ case EXEC_OMP_TARGET_PARALLEL_DO:
+ case EXEC_OMP_TARGET_PARALLEL_DO_SIMD:
+ case EXEC_OMP_TARGET_SIMD:
+ case EXEC_OMP_TASKLOOP:
+ case EXEC_OMP_TASKLOOP_SIMD:
+ case EXEC_OMP_SCAN:
+ case EXEC_OMP_DEPOBJ:
+ case EXEC_OMP_PARALLEL_MASTER:
+ case EXEC_OMP_PARALLEL_MASTER_TASKLOOP:
+ case EXEC_OMP_PARALLEL_MASTER_TASKLOOP_SIMD:
+ case EXEC_OMP_MASTER_TASKLOOP:
+ case EXEC_OMP_MASTER_TASKLOOP_SIMD:
+ case EXEC_OMP_LOOP:
+ case EXEC_OMP_PARALLEL_LOOP:
+ case EXEC_OMP_TEAMS_LOOP:
+ case EXEC_OMP_TARGET_PARALLEL_LOOP:
+ case EXEC_OMP_TARGET_TEAMS_LOOP:
+ case EXEC_OMP_MASKED:
+ case EXEC_OMP_PARALLEL_MASKED:
+ case EXEC_OMP_PARALLEL_MASKED_TASKLOOP:
+ case EXEC_OMP_PARALLEL_MASKED_TASKLOOP_SIMD:
+ case EXEC_OMP_MASKED_TASKLOOP:
+ case EXEC_OMP_MASKED_TASKLOOP_SIMD:
+ case EXEC_OMP_SCOPE:
+ case EXEC_OMP_ERROR:
+ gfc_error ("%s cannot contain OpenMP directive in intervening code "
+ "at %L",
+ state->name, &code->loc);
+ state->errorp = true;
+ break;
+ case EXEC_CALL:
+ /* Per OpenMP 5.2, the "omp_" prefix is reserved, so we don't have to
+ consider the possibility that some locally-bound definition
+ overrides the runtime routine. */
+ if (code->resolved_sym
+ && omp_runtime_api_procname (code->resolved_sym->name))
+ {
+ gfc_error ("%s cannot contain OpenMP API call in intervening code "
+ "at %L",
+ state->name, &code->loc);
+ state->errorp = true;
+ }
+ break;
+ default:
+ break;
+ }
+ return 0;
+}
+
+static int
+icode_expr_error_callback (gfc_expr **expr,
+ int *walk_subtrees ATTRIBUTE_UNUSED, void *opaque)
+{
+ icode_error_state *state = (icode_error_state *)opaque;
+
+ switch ((*expr)->expr_type)
+ {
+ /* As for EXPR_CALL with "omp_"-prefixed symbols. */
+ case EXPR_FUNCTION:
+ {
+ gfc_symbol *sym = (*expr)->value.function.esym;
+ if (sym && omp_runtime_api_procname (sym->name))
+ {
+ gfc_error ("%s cannot contain OpenMP API call in intervening code "
+ "at %L",
+ state->name, &((*expr)->where));
+ state->errorp = true;
+ }
+ }
+
+ break;
+ default:
+ break;
+ }
+
+ /* FIXME: The description of canonical loop form in the OpenMP standard
+ also says "array expressions" are not permitted in intervening code.
+ That term is not defined in either the OpenMP spec or the Fortran
+ standard, although the latter uses it informally to refer to any
+ expression that is not scalar-valued. It is also apparently not the
+ thing GCC internally calls EXPR_ARRAY. It seems the intent of the
+ OpenMP restriction is to disallow elemental operations/intrinsics
+ (including things that are not expressions, like assignment
+ statements) that generate implicit loops over array operands
+ (even if the result is a scalar), but even if the spec said
+ that there is no list of all the cases that would be forbidden.
+ This is OpenMP issue 3326. */
+
+ return 0;
+}
+
+static void
+diagnose_intervening_code_errors_1 (gfc_code *chain,
+ struct icode_error_state *state)
+{
+ gfc_code *code;
+ for (code = chain; code; code = code->next)
+ {
+ if (code == state->nested)
+ /* Do not walk the nested loop or its body, we are only
+ interested in intervening code. */
+ ;
+ else if (code->op == EXEC_BLOCK
+ && find_nested_loop_in_block (code) == state->nested)
+ /* This block contains the nested loop, recurse on its
+ statements. */
+ {
+ gfc_namespace* ns = code->ext.block.ns;
+ diagnose_intervening_code_errors_1 (ns->code, state);
+ }
+ else
+ /* Treat the whole statement as a unit. */
+ {
+ gfc_code *temp = state->next;
+ state->next = code->next;
+ gfc_code_walker (&code, icode_code_error_callback,
+ icode_expr_error_callback, state);
+ state->next = temp;
+ }
+ }
+}
+
+/* Diagnose intervening code errors in BLOCK with nested loop NESTED.
+ NAME is the user-friendly name of the OMP directive, used for error
+ messages. Returns true if any error was found. */
+static bool
+diagnose_intervening_code_errors (gfc_code *chain, const char *name,
+ gfc_code *nested)
+{
+ struct icode_error_state state;
+ state.name = name;
+ state.errorp = false;
+ state.nested = nested;
+ state.next = NULL;
+ diagnose_intervening_code_errors_1 (chain, &state);
+ return state.errorp;
+}
+
+/* Helper function for restructure_intervening_code: wrap CHAIN in
+ a marker to indicate that it is a structured block sequence. That
+ information will be used later on (in omp-low.cc) for error checking. */
+static gfc_code *
+make_structured_block (gfc_code *chain)
+{
+ gcc_assert (chain);
+ gfc_namespace *ns = gfc_build_block_ns (gfc_current_ns);
+ gfc_code *result = gfc_get_code (EXEC_BLOCK);
+ result->op = EXEC_BLOCK;
+ result->ext.block.ns = ns;
+ result->ext.block.assoc = NULL;
+ result->loc = chain->loc;
+ ns->omp_structured_block = 1;
+ ns->code = chain;
+ return result;
+}
+
+/* Push intervening code surrounding a loop, including nested scopes,
+ into the body of the loop. CHAINP is the pointer to the head of
+ the next-chain to scan, OUTER_LOOP is the EXEC_DO for the next outer
+ loop level, and COLLAPSE is the number of nested loops we need to
+ process.
+ Note that CHAINP may point at outer_loop->block->next when we
+ are scanning the body of a loop, but if there is an intervening block
+ CHAINP points into the block's chain rather than its enclosing outer
+ loop. This is why OUTER_LOOP is passed separately. */
+static gfc_code *
+restructure_intervening_code (gfc_code **chainp, gfc_code *outer_loop,
+ int count)
+{
+ gfc_code *code;
+ gfc_code *head = *chainp;
+ gfc_code *tail = NULL;
+ gfc_code *innermost_loop = NULL;
+
+ for (code = *chainp; code; code = code->next, chainp = &((*chainp)->next))
+ {
+ if (code->op == EXEC_DO)
+ {
+ /* Cut CODE free from its chain, leaving the ends dangling. */
+ *chainp = NULL;
+ tail = code->next;
+ code->next = NULL;
+
+ if (count == 1)
+ innermost_loop = code;
+ else
+ innermost_loop
+ = restructure_intervening_code (&(code->block->next),
+ code, count - 1);
+ break;
+ }
+ else if (code->op == EXEC_BLOCK
+ && find_nested_loop_in_block (code))
+ {
+ gfc_namespace *ns = code->ext.block.ns;
+
+ /* Cut CODE free from its chain, leaving the ends dangling. */
+ *chainp = NULL;
+ tail = code->next;
+ code->next = NULL;
+
+ innermost_loop
+ = restructure_intervening_code (&(ns->code), outer_loop,
+ count);
+
+ /* At this point we have already pulled out the nested loop and
+ pointed outer_loop at it, and moved the intervening code that
+ was previously in the block into the body of innermost_loop.
+ Now we want to move the BLOCK itself so it wraps the entire
+ current body of innermost_loop. */
+ ns->code = innermost_loop->block->next;
+ innermost_loop->block->next = code;
+ break;
+ }
+ }
+
+ gcc_assert (innermost_loop);
+
+ /* Now we have split the intervening code into two parts:
+ head is the start of the part before the loop/block, terminating
+ at *chainp, and tail is the part after it. Mark each part as
+ a structured block sequence, and splice the two parts around the
+ existing body of the innermost loop. */
+ if (head != code)
+ {
+ gfc_code *block = make_structured_block (head);
+ if (innermost_loop->block->next)
+ gfc_append_code (block, innermost_loop->block->next);
+ innermost_loop->block->next = block;
+ }
+ if (tail)
+ {
+ gfc_code *block = make_structured_block (tail);
+ if (innermost_loop->block->next)
+ gfc_append_code (innermost_loop->block->next, block);
+ else
+ innermost_loop->block->next = block;
+ }
+
+ /* For loops, finally splice CODE into OUTER_LOOP. We already handled
+ relinking EXEC_BLOCK above. */
+ if (code->op == EXEC_DO && outer_loop)
+ outer_loop->block->next = code;
+
+ return innermost_loop;
+}
+
/* CODE is an OMP loop construct. Return true if VAR matches an iteration
variable outer to level DEPTH. */
static bool
is_outer_iteration_variable (gfc_code *code, int depth, gfc_symbol *var)
{
int i;
- gfc_code *do_code = code->block->next;
+ gfc_code *do_code = code;
for (i = 1; i < depth; i++)
{
+ do_code = find_nested_loop_in_chain (do_code->block->next);
+ gcc_assert (do_code);
gfc_symbol *ivar = do_code->ext.iterator->var->symtree->n.sym;
if (var == ivar)
return true;
- do_code = do_code->block->next;
+ }
+ return false;
+}
+
+/* Forward declaration for recursive functions. */
+static gfc_code *
+check_nested_loop_in_block (gfc_code *block, gfc_expr *expr, gfc_symbol *sym,
+ bool *bad);
+
+/* Like find_nested_loop_in_chain, but additionally check that EXPR
+ does not reference any variables bound in intervening EXEC_BLOCKs
+ and that SYM is not bound in such intervening blocks. Either EXPR or SYM
+ may be null. Sets *BAD to true if either test fails. */
+static gfc_code *
+check_nested_loop_in_chain (gfc_code *chain, gfc_expr *expr, gfc_symbol *sym,
+ bool *bad)
+{
+ for (gfc_code *code = chain; code; code = code->next)
+ {
+ if (code->op == EXEC_DO)
+ return code;
+ else if (code->op == EXEC_BLOCK)
+ {
+ gfc_code *c = check_nested_loop_in_block (code, expr, sym, bad);
+ if (c)
+ return c;
+ }
+ }
+ return NULL;
+}
+
+/* Code walker for block symtrees. It doesn't take any kind of state
+ argument, so use a static variable. */
+static struct check_nested_loop_in_block_state_t {
+ gfc_expr *expr;
+ gfc_symbol *sym;
+ bool *bad;
+} check_nested_loop_in_block_state;
+
+static void
+check_nested_loop_in_block_symbol (gfc_symbol *sym)
+{
+ if (sym == check_nested_loop_in_block_state.sym
+ || (check_nested_loop_in_block_state.expr
+ && gfc_find_sym_in_expr (sym,
+ check_nested_loop_in_block_state.expr)))
+ *check_nested_loop_in_block_state.bad = true;
+}
+
+/* Return the first nested DO loop in BLOCK, or NULL if there
+ isn't one. Set *BAD to true if EXPR references any variables in BLOCK, or
+ SYM is bound in BLOCK. Either EXPR or SYM may be null. */
+static gfc_code *
+check_nested_loop_in_block (gfc_code *block, gfc_expr *expr,
+ gfc_symbol *sym, bool *bad)
+{
+ gfc_namespace *ns;
+ gcc_assert (block->op == EXEC_BLOCK);
+ ns = block->ext.block.ns;
+ gcc_assert (ns);
+
+ /* Skip the check if this block doesn't contain the nested loop, or
+ if we already know it's bad. */
+ gfc_code *result = check_nested_loop_in_chain (ns->code, expr, sym, bad);
+ if (result && !*bad)
+ {
+ check_nested_loop_in_block_state.expr = expr;
+ check_nested_loop_in_block_state.sym = sym;
+ check_nested_loop_in_block_state.bad = bad;
+ gfc_traverse_ns (ns, check_nested_loop_in_block_symbol);
+ check_nested_loop_in_block_state.expr = NULL;
+ check_nested_loop_in_block_state.sym = NULL;
+ check_nested_loop_in_block_state.bad = NULL;
+ }
+ return result;
+}
+
+/* CODE is an OMP loop construct. Return true if EXPR references
+ any variables bound in intervening code, to level DEPTH. */
+static bool
+expr_uses_intervening_var (gfc_code *code, int depth, gfc_expr *expr)
+{
+ int i;
+ gfc_code *do_code = code;
+
+ for (i = 0; i < depth; i++)
+ {
+ bool bad = false;
+ do_code = check_nested_loop_in_chain (do_code->block->next,
+ expr, NULL, &bad);
+ if (bad)
+ return true;
+ }
+ return false;
+}
+
+/* CODE is an OMP loop construct. Return true if SYM is bound in
+ intervening code, to level DEPTH. */
+static bool
+is_intervening_var (gfc_code *code, int depth, gfc_symbol *sym)
+{
+ int i;
+ gfc_code *do_code = code;
+
+ for (i = 0; i < depth; i++)
+ {
+ bool bad = false;
+ do_code = check_nested_loop_in_chain (do_code->block->next,
+ NULL, sym, &bad);
+ if (bad)
+ return true;
}
return false;
}
@@ -9658,14 +10157,15 @@ static bool
expr_is_invariant (gfc_code *code, int depth, gfc_expr *expr)
{
int i;
- gfc_code *do_code = code->block->next;
+ gfc_code *do_code = code;
for (i = 1; i < depth; i++)
{
+ do_code = find_nested_loop_in_chain (do_code->block->next);
+ gcc_assert (do_code);
gfc_symbol *ivar = do_code->ext.iterator->var->symtree->n.sym;
if (gfc_find_sym_in_expr (ivar, expr))
return false;
- do_code = do_code->block->next;
}
return true;
}
@@ -9736,12 +10236,14 @@ bound_expr_is_canonical (gfc_code *code, int depth, gfc_expr *expr,
static void
resolve_omp_do (gfc_code *code)
{
- gfc_code *do_code, *c;
- int list, i, collapse;
+ gfc_code *do_code, *next;
+ int list, i, count;
gfc_omp_namelist *n;
gfc_symbol *dovar;
const char *name;
bool is_simd = false;
+ bool errorp = false;
+ bool perfect_nesting_errorp = false;
switch (code->op)
{
@@ -9844,12 +10346,12 @@ resolve_omp_do (gfc_code *code)
do_code = code->block->next;
if (code->ext.omp_clauses->orderedc)
- collapse = code->ext.omp_clauses->orderedc;
+ count = code->ext.omp_clauses->orderedc;
else
{
- collapse = code->ext.omp_clauses->collapse;
- if (collapse <= 0)
- collapse = 1;
+ count = code->ext.omp_clauses->collapse;
+ if (count <= 0)
+ count = 1;
}
/* While the spec defines the loop nest depth independently of the COLLAPSE
@@ -9857,29 +10359,36 @@ resolve_omp_do (gfc_code *code)
depth and treats any further inner loops as the final-loop-body. So
here we also check canonical loop nest form only for the number of
outer loops specified by the COLLAPSE clause too. */
- for (i = 1; i <= collapse; i++)
+ for (i = 1; i <= count; i++)
{
gfc_symbol *start_var = NULL, *end_var = NULL;
+ /* Parse errors are not recoverable. */
if (do_code->op == EXEC_DO_WHILE)
{
gfc_error ("%s cannot be a DO WHILE or DO without loop control "
"at %L", name, &do_code->loc);
- break;
+ return;
}
if (do_code->op == EXEC_DO_CONCURRENT)
{
gfc_error ("%s cannot be a DO CONCURRENT loop at %L", name,
&do_code->loc);
- break;
+ return;
}
gcc_assert (do_code->op == EXEC_DO);
if (do_code->ext.iterator->var->ts.type != BT_INTEGER)
- gfc_error ("%s iteration variable must be of type integer at %L",
- name, &do_code->loc);
+ {
+ gfc_error ("%s iteration variable must be of type integer at %L",
+ name, &do_code->loc);
+ errorp = true;
+ }
dovar = do_code->ext.iterator->var->symtree->n.sym;
if (dovar->attr.threadprivate)
- gfc_error ("%s iteration variable must not be THREADPRIVATE "
- "at %L", name, &do_code->loc);
+ {
+ gfc_error ("%s iteration variable must not be THREADPRIVATE "
+ "at %L", name, &do_code->loc);
+ errorp = true;
+ }
if (code->ext.omp_clauses)
for (list = 0; list < OMP_LIST_NUM; list++)
if (!is_simd || code->ext.omp_clauses->collapse > 1
@@ -9898,13 +10407,20 @@ resolve_omp_do (gfc_code *code)
gfc_error ("%s iteration variable present on clause "
"other than PRIVATE, LASTPRIVATE, ALLOCATE or "
"LINEAR at %L", name, &do_code->loc);
- break;
+ errorp = true;
}
if (is_outer_iteration_variable (code, i, dovar))
{
gfc_error ("%s iteration variable used in more than one loop at %L",
name, &do_code->loc);
- break;
+ errorp = true;
+ }
+ else if (is_intervening_var (code, i, dovar))
+ {
+ gfc_error ("%s iteration variable at %L is bound in "
+ "intervening code",
+ name, &do_code->loc);
+ errorp = true;
}
else if (!bound_expr_is_canonical (code, i,
do_code->ext.iterator->start,
@@ -9912,7 +10428,15 @@ resolve_omp_do (gfc_code *code)
{
gfc_error ("%s loop start expression not in canonical form at %L",
name, &do_code->loc);
- break;
+ errorp = true;
+ }
+ else if (expr_uses_intervening_var (code, i,
+ do_code->ext.iterator->start))
+ {
+ gfc_error ("%s loop start expression at %L uses variable bound in "
+ "intervening code",
+ name, &do_code->loc);
+ errorp = true;
}
else if (!bound_expr_is_canonical (code, i,
do_code->ext.iterator->end,
@@ -9920,48 +10444,89 @@ resolve_omp_do (gfc_code *code)
{
gfc_error ("%s loop end expression not in canonical form at %L",
name, &do_code->loc);
- break;
+ errorp = true;
+ }
+ else if (expr_uses_intervening_var (code, i,
+ do_code->ext.iterator->end))
+ {
+ gfc_error ("%s loop end expression at %L uses variable bound in "
+ "intervening code",
+ name, &do_code->loc);
+ errorp = true;
}
else if (start_var && end_var && start_var != end_var)
{
gfc_error ("%s loop bounds reference different "
"iteration variables at %L", name, &do_code->loc);
- break;
+ errorp = true;
}
else if (!expr_is_invariant (code, i, do_code->ext.iterator->step))
{
gfc_error ("%s loop increment not in canonical form at %L",
name, &do_code->loc);
- break;
+ errorp = true;
+ }
+ else if (expr_uses_intervening_var (code, i,
+ do_code->ext.iterator->step))
+ {
+ gfc_error ("%s loop increment expression at %L uses variable "
+ "bound in intervening code",
+ name, &do_code->loc);
+ errorp = true;
}
if (start_var || end_var)
code->ext.omp_clauses->non_rectangular = 1;
- for (c = do_code->next; c; c = c->next)
- if (c->op != EXEC_NOP && c->op != EXEC_CONTINUE)
- {
- gfc_error ("collapsed %s loops not perfectly nested at %L",
- name, &c->loc);
- break;
- }
- if (i == collapse || c)
+ /* Only parse loop body into nested loop and intervening code if
+ there are supposed to be more loops in the nest to collapse. */
+ if (i == count)
break;
- do_code = do_code->block;
- if (do_code->op != EXEC_DO && do_code->op != EXEC_DO_WHILE)
+
+ next = find_nested_loop_in_chain (do_code->block->next);
+
+ if (!next)
{
- gfc_error ("not enough DO loops for collapsed %s at %L",
- name, &code->loc);
- break;
+ /* Parse error, can't recover from this. */
+ gfc_error ("not enough DO loops for collapsed %s (level %d) at %L",
+ name, i, &code->loc);
+ return;
}
- do_code = do_code->next;
- if (do_code == NULL
- || (do_code->op != EXEC_DO && do_code->op != EXEC_DO_WHILE))
+ else if (next != do_code->block->next || next->next)
+ /* Imperfectly nested loop found. */
{
- gfc_error ("not enough DO loops for collapsed %s at %L",
- name, &code->loc);
- break;
+ /* Only diagnose violation of imperfect nesting constraints once. */
+ if (!perfect_nesting_errorp)
+ {
+ if (code->ext.omp_clauses->orderedc)
+ {
+ gfc_error ("%s inner loops must be perfectly nested with "
+ "ORDERED clause at %L",
+ name, &code->loc);
+ perfect_nesting_errorp = true;
+ }
+ else if (code->ext.omp_clauses->lists[OMP_LIST_REDUCTION_INSCAN])
+ {
+ gfc_error ("%s inner loops must be perfectly nested with "
+ "REDUCTION INSCAN clause at %L",
+ name, &code->loc);
+ perfect_nesting_errorp = true;
+ }
+ /* FIXME: Also diagnose for TILE directives. */
+ if (perfect_nesting_errorp)
+ errorp = true;
+ }
+ if (diagnose_intervening_code_errors (do_code->block->next,
+ name, next))
+ errorp = true;
}
+ do_code = next;
}
+
+ /* Give up now if we found any constraint violations. */
+ if (errorp)
+ return;
+
+ restructure_intervening_code (&(code->block->next), code, count);
}
diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index 7e768343a57..4b508003e32 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -2334,6 +2334,7 @@ gfc_trans_block_construct (gfc_code* code)
tree exit_label;
stmtblock_t body;
gfc_association_list *ass;
+ tree translated_body;
ns = code->ext.block.ns;
gcc_assert (ns);
@@ -2352,7 +2353,11 @@ gfc_trans_block_construct (gfc_code* code)
finish_oacc_declare (ns, sym, true);
- gfc_add_expr_to_block (&body, gfc_trans_code (ns->code));
+ translated_body = gfc_trans_code (ns->code);
+ if (ns->omp_structured_block)
+ translated_body = build1 (OMP_STRUCTURED_BLOCK, void_type_node,
+ translated_body);
+ gfc_add_expr_to_block (&body, translated_body);
gfc_add_expr_to_block (&body, build1_v (LABEL_EXPR, exit_label));
/* Finish everything. */
diff --git a/gcc/testsuite/gfortran.dg/gomp/collapse1.f90 b/gcc/testsuite/gfortran.dg/gomp/collapse1.f90
index 77b2bdd7fcb..613f06f6ea9 100644
--- a/gcc/testsuite/gfortran.dg/gomp/collapse1.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/collapse1.f90
@@ -31,11 +31,11 @@ subroutine collapse1
do i = 1, 3
do j = 4, 6
end do
- k = 4 ! { dg-error "loops not perfectly nested" }
+ k = 4
end do
- !$omp parallel do collapse(2)
+ !$omp parallel do collapse(2) ! { dg-error "not enough DO loops" }
do i = 1, 3
- do ! { dg-error "cannot be a DO WHILE or DO without loop control" }
+ do
end do
end do
!$omp parallel do collapse(2)
diff --git a/gcc/testsuite/gfortran.dg/gomp/collapse2.f90 b/gcc/testsuite/gfortran.dg/gomp/collapse2.f90
index 1ab934e3d0d..9af3b656829 100644
--- a/gcc/testsuite/gfortran.dg/gomp/collapse2.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/collapse2.f90
@@ -6,24 +6,24 @@ program p
do j = 1, 8
do k = 1, 8
end do
- x = 5 ! { dg-error "loops not perfectly nested" }
+ x = 5
end do
end do
- !$omp parallel do ordered(3)
+ !$omp parallel do ordered(3) ! { dg-error "inner loops must be perfectly nested" }
do i = 1, 8
do j = 1, 8
do k = 1, 8
end do
end do
- x = 5 ! { dg-error "loops not perfectly nested" }
+ x = 5
end do
- !$omp parallel do collapse(2) ! { dg-error "not enough DO loops for collapsed" }
+ !$omp parallel do collapse(2)
do i = 1, 8
x = 5
do j = 1, 8
end do
end do
- !$omp parallel do ordered(2) ! { dg-error "not enough DO loops for collapsed" }
+ !$omp parallel do ordered(2) ! { dg-error "inner loops must be perfectly nested" }
do i = 1, 8
x = 5
do j = 1, 8
diff --git a/gcc/testsuite/gfortran.dg/gomp/imperfect-gotos.f90 b/gcc/testsuite/gfortran.dg/gomp/imperfect-gotos.f90
new file mode 100644
index 00000000000..e184ffe631e
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/imperfect-gotos.f90
@@ -0,0 +1,69 @@
+! This test case is expected to fail due to errors.
+
+! These jumps are all OK since they are to/from the same structured block.
+subroutine f1 ()
+ integer :: i, j
+ !$omp do collapse(2)
+ do i = 1, 64
+ go to 10
+10 continue
+ do j = 1, 64
+ go to 11
+11 continue
+ end do
+ go to 12
+12 continue
+ end do
+end subroutine
+
+! Jump around loop body to/from different structured blocks of intervening
+! code.
+subroutine f2 ()
+ integer :: i, j
+ !$omp do collapse(2)
+ do i = 1, 64
+ go to 20
+20 continue
+ if (i > 16) go to 22 ! { dg-error "invalid branch to/from OpenMP structured block" }
+ do j = 1, 64
+ go to 21
+21 continue
+ end do
+ go to 22
+22 continue
+ end do
+end subroutine
+
+! Jump into loop body from intervening code.
+subroutine f3 ()
+ integer :: i, j
+ !$omp do collapse(2)
+ do i = 1, 64
+ go to 30
+30 continue
+ if (i > 16) go to 31 ! { dg-error "invalid branch to/from OpenMP structured block" }
+ ! { dg-warning "Legacy Extension:" "" { target *-*-* } .-1 }
+ do j = 1, 64
+ go to 31
+31 continue ! { dg-warning "Legacy Extension:" }
+ end do
+ go to 32
+32 continue
+ end do
+end subroutine
+
+! Jump out of loop body to intervening code.
+subroutine f4 ()
+ integer :: i, j
+ !$omp do collapse(2)
+ do i = 1, 64
+ go to 40
+40 continue
+ do j = 1, 64
+ if (i > 16) go to 41 ! { dg-error "invalid branch to/from OpenMP structured block" }
+ end do
+41 continue
+ go to 42
+42 continue
+ end do
+end subroutine
diff --git a/gcc/testsuite/gfortran.dg/gomp/imperfect-invalid-scope.f90 b/gcc/testsuite/gfortran.dg/gomp/imperfect-invalid-scope.f90
new file mode 100644
index 00000000000..7cc60944131
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/imperfect-invalid-scope.f90
@@ -0,0 +1,81 @@
+! Test that various errors involving references to variables bound
+! in intervening code in the DO loop control expressions are diagnosed.
+
+subroutine foo (x, y)
+ integer :: x, y
+end subroutine
+
+subroutine f1 ()
+ integer :: i, j
+
+ !$omp do collapse (2)
+ do i = 1, 64
+ block
+ integer :: v
+ v = (i + 4) * 2
+ do j = v, 64 ! { dg-error "loop start expression at .1. uses variable bound in intervening code" }
+ call foo (i, j)
+ end do
+ end block
+ end do
+end subroutine
+
+subroutine f2 ()
+ integer :: i, j
+
+ !$omp do collapse (2)
+ do i = 1, 64
+ block
+ integer :: v
+ v = (i + 4) * 2
+ do j = 1, v ! { dg-error "loop end expression at .1. uses variable bound in intervening code" }
+ call foo (i, j)
+ end do
+ end block
+ end do
+end subroutine
+
+subroutine f3 ()
+ integer :: i, j
+
+ !$omp do collapse (2)
+ do i = 1, 64
+ block
+ integer :: v
+ v = (i + 4) * 2
+ do j = 1, 64, v ! { dg-error "loop increment expression at .1. uses variable bound in intervening code" }
+ call foo (i, j)
+ end do
+ end block
+ end do
+end subroutine
+
+subroutine f4 ()
+ integer :: i
+
+ !$omp do collapse (2)
+ do i = 1, 64
+ block
+ integer :: j
+ do j = 1, 64 ! { dg-error "iteration variable at .1. is bound in intervening code" }
+ call foo (i, j)
+ end do
+ end block
+ end do
+end subroutine
+
+subroutine f5 ()
+ integer :: i
+
+ !$omp do collapse (2)
+ do i = 1, 64
+ block
+ integer :: j
+ integer :: v
+ v = (i + 4) * 2
+ do j = v, 64 ! { dg-error "iteration variable at .1. is bound in intervening code" }
+ call foo (i, j)
+ end do
+ end block
+ end do
+end subroutine
diff --git a/gcc/testsuite/gfortran.dg/gomp/imperfect1.f90 b/gcc/testsuite/gfortran.dg/gomp/imperfect1.f90
new file mode 100644
index 00000000000..4e750d9ad05
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/imperfect1.f90
@@ -0,0 +1,39 @@
+! This test case is expected to fail due to errors.
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp do collapse(3)
+ do i = 1, a1
+ call f1 (1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ if (i == 3) then
+ cycle ! { dg-error "CYCLE statement" }
+ else
+ exit ! { dg-error "EXIT statement" }
+ endif
+!$omp barrier ! { dg-error "OpenMP directive in intervening code" }
+ do k = 1, a3
+ call f1 (3, k)
+ call f2 (3, k)
+ end do
+ call f2 (2, j)
+ end do
+ do k = 1, a3 ! { dg-error "loop in intervening code" }
+ call f1 (3, k)
+ call f2 (3, k)
+ end do
+ call f2 (1, i)
+ end do
+
+end subroutine
diff --git a/gcc/testsuite/gfortran.dg/gomp/imperfect2.f90 b/gcc/testsuite/gfortran.dg/gomp/imperfect2.f90
new file mode 100644
index 00000000000..d02191050d9
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/imperfect2.f90
@@ -0,0 +1,56 @@
+! This test case is expected to fail due to errors.
+
+! Note that the calls to these functions in the test case don't make
+! any sense in terms of behavior, they're just there to test the error
+! behavior.
+
+module omp_lib
+ use iso_c_binding
+ interface
+ integer function omp_get_thread_num ()
+ end
+ subroutine omp_set_max_levels (i)
+ integer :: i
+ end
+ end interface
+end module
+
+program junk
+ use omp_lib
+ implicit none
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ integer :: m
+
+ !$omp do collapse(3)
+ do i = 1, a1
+ call f1 (1, i)
+ m = omp_get_thread_num () ! { dg-error "OpenMP API call in intervening code" }
+ do j = 1, a2 + omp_get_thread_num () ! This is OK
+ call f1 (2, j)
+ do k = 1, a3
+ call f1 (m, k)
+ call omp_set_max_active_levels (k) ! This is OK too
+ call f2 (m, k)
+ end do
+ call f2 (2, j)
+ call omp_set_max_active_levels (i) ! { dg-error "OpenMP API call in intervening code" }
+ end do
+ call f2 (1, i)
+ end do
+end subroutine
+
+end program
diff --git a/gcc/testsuite/gfortran.dg/gomp/imperfect3.f90 b/gcc/testsuite/gfortran.dg/gomp/imperfect3.f90
new file mode 100644
index 00000000000..2eccdfc8b58
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/imperfect3.f90
@@ -0,0 +1,29 @@
+! This test case is expected to fail due to errors.
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp do ordered(3) ! { dg-error "inner loops must be perfectly nested" }
+ do i = 1, a1
+ call f1 (1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ do k = 1, a3
+ call f1 (3, k)
+ call f2 (3, k)
+ end do
+ call f2 (2, j)
+ end do
+ call f2 (1, i)
+ end do
+
+end subroutine
diff --git a/gcc/testsuite/gfortran.dg/gomp/imperfect4.f90 b/gcc/testsuite/gfortran.dg/gomp/imperfect4.f90
new file mode 100644
index 00000000000..b7ccd8b6c53
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/imperfect4.f90
@@ -0,0 +1,36 @@
+! This test case is expected to fail due to errors.
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+! Unlike the C/C++ front ends, the Fortran front end already has the whole
+! parse tree for the OMP DO construct before doing error checking on it.
+! It gives up immediately if there are not enough nested loops for the
+! specified COLLAPSE depth, without error-checking intervening code.
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp do collapse(4) ! { dg-error "not enough DO loops" }
+ do i = 1, a1
+ call f1 (1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ do k = 1, a3
+! This is not valid intervening code, but the above error takes precedence.
+!$omp barrier
+ call f1 (3, k)
+ call f2 (3, k)
+ end do
+ call f2 (2, j)
+ end do
+ call f2 (1, i)
+ end do
+
+end subroutine
diff --git a/gcc/testsuite/gfortran.dg/gomp/imperfect5.f90 b/gcc/testsuite/gfortran.dg/gomp/imperfect5.f90
new file mode 100644
index 00000000000..95cc7f144a2
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/imperfect5.f90
@@ -0,0 +1,67 @@
+! This test case is expected to fail due to errors.
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+end subroutine
+
+function ijk (x, y, z)
+ integer :: ijk
+ integer :: x, y, z
+end function
+
+subroutine f3 (sum)
+ integer :: sum
+end subroutine
+
+! This function isn't particularly meaningful, but it should compile without
+! error.
+function s1 (a1, a2, a3)
+ integer :: s1
+ integer :: a1, a2, a3
+ integer :: i, j, k
+ integer :: r
+
+ r = 0
+ !$omp simd collapse(3) reduction (inscan, +:r)
+ do i = 1, a1
+ do j = 1, a2
+ do k = 1, a3
+ r = r + ijk (i, j, k)
+!$omp scan exclusive (r)
+ call f3 (r)
+ end do
+ end do
+ end do
+
+ s1 = r
+end function
+
+! Adding intervening code should trigger an error.
+function s2 (a1, a2, a3)
+ integer :: s2
+ integer :: a1, a2, a3
+ integer :: i, j, k
+ integer :: r
+
+ r = 0
+ !$omp simd collapse(3) reduction (inscan, +:r) ! { dg-error "inner loops must be perfectly nested" }
+ do i = 1, a1
+ call f1 (1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ do k = 1, a3
+ r = r + ijk (i, j, k)
+!$omp scan exclusive (r)
+ call f3 (r)
+ end do
+ call f2 (2, j)
+ end do
+ call f2 (1, i)
+ end do
+
+ s2 = r
+end function
diff --git a/libgomp/testsuite/libgomp.fortran/imperfect-destructor.f90 b/libgomp/testsuite/libgomp.fortran/imperfect-destructor.f90
new file mode 100644
index 00000000000..664d27fe968
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/imperfect-destructor.f90
@@ -0,0 +1,142 @@
+! { dg-do run }
+
+! Like imperfect2.f90, but adds bindings to the blocks.
+
+module m
+ implicit none
+ type t
+ integer :: i
+ contains
+ final :: fini
+ end type t
+
+ integer :: ccount(3), dcount(3)
+
+ contains
+
+ subroutine init(x, n)
+ type(t) :: x
+ integer :: n
+ x%i = n
+ ccount(x%i) = ccount(x%i) + 1
+ end subroutine init
+
+ subroutine fini(x)
+ type(t) :: x
+ dcount(x%i) = dcount(x%i) + 1
+ end subroutine fini
+end module m
+
+program foo
+ use m
+
+ integer :: f1count(3), f2count(3), g1count(3), g2count(3)
+
+ f1count(1) = 0
+ f1count(2) = 0
+ f1count(3) = 0
+ f2count(1) = 0
+ f2count(2) = 0
+ f2count(3) = 0
+
+ g1count(1) = 0
+ g1count(2) = 0
+ g1count(3) = 0
+ g2count(1) = 0
+ g2count(2) = 0
+ g2count(3) = 0
+
+ call s1 (3, 4, 5)
+
+ ! All intervening code at the same depth must be executed the same
+ ! number of times.
+ if (f1count(1) /= f2count(1)) error stop 101
+ if (f1count(2) /= f2count(2)) error stop 102
+ if (f1count(3) /= f2count(3)) error stop 103
+ if (g1count(1) /= f1count(1)) error stop 104
+ if (g2count(1) /= f1count(1)) error stop 105
+ if (g1count(2) /= f1count(2)) error stop 106
+ if (g2count(2) /= f1count(2)) error stop 107
+ if (g1count(3) /= f1count(3)) error stop 108
+ if (g2count(3) /= f1count(3)) error stop 109
+
+ ! Intervening code must be executed at least as many times as the loop
+ ! that encloses it.
+ if (f1count(1) < 3) error stop 111
+ if (f1count(2) < 3 * 4) error stop 112
+
+ ! Intervening code must not be executed more times than the number
+ ! of logical iterations.
+ if (f1count(1) > 3 * 4 * 5) error stop 121
+ if (f1count(2) > 3 * 4 * 5) error stop 122
+
+ ! Check that the innermost loop body is executed exactly the number
+ ! of logical iterations expected.
+ if (f1count(3) /= 3 * 4 * 5) error stop 131
+
+ ! Check that constructors and destructors are called equal number of times.
+ if (ccount(1) /= dcount(1)) error stop 141
+ if (ccount(2) /= dcount(2)) error stop 142
+ if (ccount(3) /= dcount(3)) error stop 143
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+ f1count(depth) = f1count(depth) + 1
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+ f2count(depth) = f2count(depth) + 1
+end subroutine
+
+subroutine g1 (depth, iter)
+ integer :: depth, iter
+ g1count(depth) = g1count(depth) + 1
+end subroutine
+
+subroutine g2 (depth, iter)
+ integer :: depth, iter
+ g2count(depth) = g2count(depth) + 1
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp do collapse(3)
+ do i = 1, a1
+ call f1 (1, i)
+ block
+ type (t) :: local1
+ call init (local1, 1)
+ call g1 (local1%i, i)
+ do j = 1, a2
+ call f1 (2, j)
+ block
+ type (t) :: local2
+ call init (local2, 2)
+ call g1 (local2%i, j)
+ do k = 1, a3
+ call f1 (3, k)
+ block
+ type (t) :: local3
+ call init (local3, 3)
+ call g1 (local3%i, k)
+ call g2 (local3%i, k)
+ end block
+ call f2 (3, k)
+ end do
+ call g2 (local2%i, j)
+ end block
+ call f2 (2, j)
+ end do
+ call g2 (local1%i, i)
+ end block
+ call f2 (1, i)
+ end do
+
+end subroutine
+
+end program
diff --git a/libgomp/testsuite/libgomp.fortran/imperfect1.f90 b/libgomp/testsuite/libgomp.fortran/imperfect1.f90
new file mode 100644
index 00000000000..8c483c2a4e5
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/imperfect1.f90
@@ -0,0 +1,67 @@
+! { dg-do run }
+
+program foo
+ integer, save :: f1count(3), f2count(3)
+
+ f1count(1) = 0
+ f1count(2) = 0
+ f1count(3) = 0
+ f2count(1) = 0
+ f2count(2) = 0
+ f2count(3) = 0
+
+ call s1 (3, 4, 5)
+
+ ! All intervening code at the same depth must be executed the same
+ ! number of times.
+ if (f1count(1) /= f2count(1)) error stop 101
+ if (f1count(2) /= f2count(2)) error stop 102
+ if (f1count(3) /= f2count(3)) error stop 103
+
+ ! Intervening code must be executed at least as many times as the loop
+ ! that encloses it.
+ if (f1count(1) < 3) error stop 111
+ if (f1count(2) < 3 * 4) error stop 112
+
+ ! Intervening code must not be executed more times than the number
+ ! of logical iterations.
+ if (f1count(1) > 3 * 4 * 5) error stop 121
+ if (f1count(2) > 3 * 4 * 5) error stop 122
+
+ ! Check that the innermost loop body is executed exactly the number
+ ! of logical iterations expected.
+ if (f1count(3) /= 3 * 4 * 5) error stop 131
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+ f1count(depth) = f1count(depth) + 1
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+ f2count(depth) = f2count(depth) + 1
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp do collapse(3)
+ do i = 1, a1
+ call f1 (1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ do k = 1, a3
+ call f1 (3, k)
+ call f2 (3, k)
+ end do
+ call f2 (2, j)
+ end do
+ call f2 (1, i)
+ end do
+
+end subroutine
+
+end program
diff --git a/libgomp/testsuite/libgomp.fortran/imperfect2.f90 b/libgomp/testsuite/libgomp.fortran/imperfect2.f90
new file mode 100644
index 00000000000..e42cb08031b
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/imperfect2.f90
@@ -0,0 +1,102 @@
+! { dg-do run }
+
+program foo
+ integer, save :: f1count(3), f2count(3), g1count(3), g2count(3)
+
+ f1count(1) = 0
+ f1count(2) = 0
+ f1count(3) = 0
+ f2count(1) = 0
+ f2count(2) = 0
+ f2count(3) = 0
+
+ g1count(1) = 0
+ g1count(2) = 0
+ g1count(3) = 0
+ g2count(1) = 0
+ g2count(2) = 0
+ g2count(3) = 0
+
+ call s1 (3, 4, 5)
+
+ ! All intervening code at the same depth must be executed the same
+ ! number of times.
+ if (f1count(1) /= f2count(1)) error stop 101
+ if (f1count(2) /= f2count(2)) error stop 102
+ if (f1count(3) /= f2count(3)) error stop 103
+ if (g1count(1) /= f1count(1)) error stop 104
+ if (g2count(1) /= f1count(1)) error stop 105
+ if (g1count(2) /= f1count(2)) error stop 106
+ if (g2count(2) /= f1count(2)) error stop 107
+ if (g1count(3) /= f1count(3)) error stop 108
+ if (g2count(3) /= f1count(3)) error stop 109
+
+ ! Intervening code must be executed at least as many times as the loop
+ ! that encloses it.
+ if (f1count(1) < 3) error stop 111
+ if (f1count(2) < 3 * 4) error stop 112
+
+ ! Intervening code must not be executed more times than the number
+ ! of logical iterations.
+ if (f1count(1) > 3 * 4 * 5) error stop 121
+ if (f1count(2) > 3 * 4 * 5) error stop 122
+
+ ! Check that the innermost loop body is executed exactly the number
+ ! of logical iterations expected.
+ if (f1count(3) /= 3 * 4 * 5) error stop 131
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+ f1count(depth) = f1count(depth) + 1
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+ f2count(depth) = f2count(depth) + 1
+end subroutine
+
+subroutine g1 (depth, iter)
+ integer :: depth, iter
+ g1count(depth) = g1count(depth) + 1
+end subroutine
+
+subroutine g2 (depth, iter)
+ integer :: depth, iter
+ g2count(depth) = g2count(depth) + 1
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp do collapse(3)
+ do i = 1, a1
+ call f1 (1, i)
+ block
+ call g1 (1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ block
+ call g1 (2, j)
+ do k = 1, a3
+ call f1 (3, k)
+ block
+ call g1 (3, k)
+ call g2 (3, k)
+ end block
+ call f2 (3, k)
+ end do
+ call g2 (2, j)
+ end block
+ call f2 (2, j)
+ end do
+ call g2 (1, i)
+ end block
+ call f2 (1, i)
+ end do
+
+end subroutine
+
+end program
diff --git a/libgomp/testsuite/libgomp.fortran/imperfect3.f90 b/libgomp/testsuite/libgomp.fortran/imperfect3.f90
new file mode 100644
index 00000000000..da094612332
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/imperfect3.f90
@@ -0,0 +1,110 @@
+! { dg-do run }
+
+! Like imperfect2.f90, but adds bindings to the blocks.
+
+program foo
+ integer, save :: f1count(3), f2count(3), g1count(3), g2count(3)
+
+ f1count(1) = 0
+ f1count(2) = 0
+ f1count(3) = 0
+ f2count(1) = 0
+ f2count(2) = 0
+ f2count(3) = 0
+
+ g1count(1) = 0
+ g1count(2) = 0
+ g1count(3) = 0
+ g2count(1) = 0
+ g2count(2) = 0
+ g2count(3) = 0
+
+ call s1 (3, 4, 5)
+
+ ! All intervening code at the same depth must be executed the same
+ ! number of times.
+ if (f1count(1) /= f2count(1)) error stop 101
+ if (f1count(2) /= f2count(2)) error stop 102
+ if (f1count(3) /= f2count(3)) error stop 103
+ if (g1count(1) /= f1count(1)) error stop 104
+ if (g2count(1) /= f1count(1)) error stop 105
+ if (g1count(2) /= f1count(2)) error stop 106
+ if (g2count(2) /= f1count(2)) error stop 107
+ if (g1count(3) /= f1count(3)) error stop 108
+ if (g2count(3) /= f1count(3)) error stop 109
+
+ ! Intervening code must be executed at least as many times as the loop
+ ! that encloses it.
+ if (f1count(1) < 3) error stop 111
+ if (f1count(2) < 3 * 4) error stop 112
+
+ ! Intervening code must not be executed more times than the number
+ ! of logical iterations.
+ if (f1count(1) > 3 * 4 * 5) error stop 121
+ if (f1count(2) > 3 * 4 * 5) error stop 122
+
+ ! Check that the innermost loop body is executed exactly the number
+ ! of logical iterations expected.
+ if (f1count(3) /= 3 * 4 * 5) error stop 131
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+ f1count(depth) = f1count(depth) + 1
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+ f2count(depth) = f2count(depth) + 1
+end subroutine
+
+subroutine g1 (depth, iter)
+ integer :: depth, iter
+ g1count(depth) = g1count(depth) + 1
+end subroutine
+
+subroutine g2 (depth, iter)
+ integer :: depth, iter
+ g2count(depth) = g2count(depth) + 1
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp do collapse(3)
+ do i = 1, a1
+ call f1 (1, i)
+ block
+ integer :: local1
+ local1 = 1
+ call g1 (local1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ block
+ integer :: local2
+ local2 = 2
+ call g1 (local2, j)
+ do k = 1, a3
+ call f1 (3, k)
+ block
+ integer :: local3
+ local3 = 3
+ call g1 (local3, k)
+ call g2 (local3, k)
+ end block
+ call f2 (3, k)
+ end do
+ call g2 (local2, j)
+ end block
+ call f2 (2, j)
+ end do
+ call g2 (local1, i)
+ end block
+ call f2 (1, i)
+ end do
+
+end subroutine
+
+end program
diff --git a/libgomp/testsuite/libgomp.fortran/imperfect4.f90 b/libgomp/testsuite/libgomp.fortran/imperfect4.f90
new file mode 100644
index 00000000000..1679c8c5b92
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/imperfect4.f90
@@ -0,0 +1,121 @@
+! { dg-do run }
+
+! Like imperfect2.f90, but includes blocks that are themselves wholly
+! intervening code and not containers for nested loops.
+
+program foo
+ integer, save :: f1count(3), f2count(3), g1count(3), g2count(3)
+
+ f1count(1) = 0
+ f1count(2) = 0
+ f1count(3) = 0
+ f2count(1) = 0
+ f2count(2) = 0
+ f2count(3) = 0
+
+ g1count(1) = 0
+ g1count(2) = 0
+ g1count(3) = 0
+ g2count(1) = 0
+ g2count(2) = 0
+ g2count(3) = 0
+
+ call s1 (3, 4, 5)
+
+ ! All intervening code at the same depth must be executed the same
+ ! number of times.
+ if (f1count(1) /= f2count(1)) error stop 101
+ if (f1count(2) /= f2count(2)) error stop 102
+ if (f1count(3) /= f2count(3)) error stop 103
+ if (g1count(1) /= f1count(1)) error stop 104
+ if (g2count(1) /= f1count(1)) error stop 105
+ if (g1count(2) /= f1count(2)) error stop 106
+ if (g2count(2) /= f1count(2)) error stop 107
+ if (g1count(3) /= f1count(3)) error stop 108
+ if (g2count(3) /= f1count(3)) error stop 109
+
+ ! Intervening code must be executed at least as many times as the loop
+ ! that encloses it.
+ if (f1count(1) < 3) error stop 111
+ if (f1count(2) < 3 * 4) error stop 112
+
+ ! Intervening code must not be executed more times than the number
+ ! of logical iterations.
+ if (f1count(1) > 3 * 4 * 5) error stop 121
+ if (f1count(2) > 3 * 4 * 5) error stop 122
+
+ ! Check that the innermost loop body is executed exactly the number
+ ! of logical iterations expected.
+ if (f1count(3) /= 3 * 4 * 5) error stop 131
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+ f1count(depth) = f1count(depth) + 1
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+ f2count(depth) = f2count(depth) + 1
+end subroutine
+
+subroutine g1 (depth, iter)
+ integer :: depth, iter
+ g1count(depth) = g1count(depth) + 1
+end subroutine
+
+subroutine g2 (depth, iter)
+ integer :: depth, iter
+ g2count(depth) = g2count(depth) + 1
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp do collapse(3)
+ do i = 1, a1
+ block
+ call f1 (1, i)
+ end block
+ block
+ block
+ call g1 (1, i)
+ end block
+ do j = 1, a2
+ block
+ call f1 (2, j)
+ end block
+ block
+ block
+ call g1 (2, j)
+ end block
+ do k = 1, a3
+ call f1 (3, k)
+ block
+ call g1 (3, k)
+ call g2 (3, k)
+ end block
+ call f2 (3, k)
+ end do
+ block
+ call g2 (2, j)
+ end block
+ end block
+ block
+ call f2 (2, j)
+ end block
+ end do
+ block
+ call g2 (1, i)
+ end block
+ end block
+ block
+ call f2 (1, i)
+ end block
+ end do
+
+end subroutine
+
+end program
diff --git a/libgomp/testsuite/libgomp.fortran/target-imperfect1.f90 b/libgomp/testsuite/libgomp.fortran/target-imperfect1.f90
new file mode 100644
index 00000000000..608eee7e424
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/target-imperfect1.f90
@@ -0,0 +1,72 @@
+! { dg-do run }
+
+! Like imperfect1.f90, but enables offloading.
+
+program foo
+ integer, save :: f1count(3), f2count(3)
+ !$omp declare target enter (f1count, f2count)
+
+ f1count(1) = 0
+ f1count(2) = 0
+ f1count(3) = 0
+ f2count(1) = 0
+ f2count(2) = 0
+ f2count(3) = 0
+
+ call s1 (3, 4, 5)
+
+ ! All intervening code at the same depth must be executed the same
+ ! number of times.
+ if (f1count(1) /= f2count(1)) error stop 101
+ if (f1count(2) /= f2count(2)) error stop 102
+ if (f1count(3) /= f2count(3)) error stop 103
+
+ ! Intervening code must be executed at least as many times as the loop
+ ! that encloses it.
+ if (f1count(1) < 3) error stop 111
+ if (f1count(2) < 3 * 4) error stop 112
+
+ ! Intervening code must not be executed more times than the number
+ ! of logical iterations.
+ if (f1count(1) > 3 * 4 * 5) error stop 121
+ if (f1count(2) > 3 * 4 * 5) error stop 122
+
+ ! Check that the innermost loop body is executed exactly the number
+ ! of logical iterations expected.
+ if (f1count(3) /= 3 * 4 * 5) error stop 131
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ f1count(depth) = f1count(depth) + 1
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ f2count(depth) = f2count(depth) + 1
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp target parallel do collapse(3) map(always, tofrom:f1count, f2count)
+ do i = 1, a1
+ call f1 (1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ do k = 1, a3
+ call f1 (3, k)
+ call f2 (3, k)
+ end do
+ call f2 (2, j)
+ end do
+ call f2 (1, i)
+ end do
+
+end subroutine
+
+end program
diff --git a/libgomp/testsuite/libgomp.fortran/target-imperfect2.f90 b/libgomp/testsuite/libgomp.fortran/target-imperfect2.f90
new file mode 100644
index 00000000000..982661c278a
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/target-imperfect2.f90
@@ -0,0 +1,110 @@
+! { dg-do run }
+
+! Like imperfect2.f90, but enables offloading.
+
+program foo
+ integer, save :: f1count(3), f2count(3), g1count(3), g2count(3)
+ !$omp declare target enter (f1count, f2count)
+ !$omp declare target enter (g1count, g2count)
+
+ f1count(1) = 0
+ f1count(2) = 0
+ f1count(3) = 0
+ f2count(1) = 0
+ f2count(2) = 0
+ f2count(3) = 0
+
+ g1count(1) = 0
+ g1count(2) = 0
+ g1count(3) = 0
+ g2count(1) = 0
+ g2count(2) = 0
+ g2count(3) = 0
+
+ call s1 (3, 4, 5)
+
+ ! All intervening code at the same depth must be executed the same
+ ! number of times.
+ if (f1count(1) /= f2count(1)) error stop 101
+ if (f1count(2) /= f2count(2)) error stop 102
+ if (f1count(3) /= f2count(3)) error stop 103
+ if (g1count(1) /= f1count(1)) error stop 104
+ if (g2count(1) /= f1count(1)) error stop 105
+ if (g1count(2) /= f1count(2)) error stop 106
+ if (g2count(2) /= f1count(2)) error stop 107
+ if (g1count(3) /= f1count(3)) error stop 108
+ if (g2count(3) /= f1count(3)) error stop 109
+
+ ! Intervening code must be executed at least as many times as the loop
+ ! that encloses it.
+ if (f1count(1) < 3) error stop 111
+ if (f1count(2) < 3 * 4) error stop 112
+
+ ! Intervening code must not be executed more times than the number
+ ! of logical iterations.
+ if (f1count(1) > 3 * 4 * 5) error stop 121
+ if (f1count(2) > 3 * 4 * 5) error stop 122
+
+ ! Check that the innermost loop body is executed exactly the number
+ ! of logical iterations expected.
+ if (f1count(3) /= 3 * 4 * 5) error stop 131
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ f1count(depth) = f1count(depth) + 1
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ f2count(depth) = f2count(depth) + 1
+end subroutine
+
+subroutine g1 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ g1count(depth) = g1count(depth) + 1
+end subroutine
+
+subroutine g2 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ g2count(depth) = g2count(depth) + 1
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp target parallel do collapse(3) map(always, tofrom:f1count, f2count, g1count, g2count)
+ do i = 1, a1
+ call f1 (1, i)
+ block
+ call g1 (1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ block
+ call g1 (2, j)
+ do k = 1, a3
+ call f1 (3, k)
+ block
+ call g1 (3, k)
+ call g2 (3, k)
+ end block
+ call f2 (3, k)
+ end do
+ call g2 (2, j)
+ end block
+ call f2 (2, j)
+ end do
+ call g2 (1, i)
+ end block
+ call f2 (1, i)
+ end do
+
+end subroutine
+
+end program
diff --git a/libgomp/testsuite/libgomp.fortran/target-imperfect3.f90 b/libgomp/testsuite/libgomp.fortran/target-imperfect3.f90
new file mode 100644
index 00000000000..6f4f92d6f3f
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/target-imperfect3.f90
@@ -0,0 +1,116 @@
+! { dg-do run }
+
+! Like imperfect3.f90, but enables offloading.
+
+program foo
+ integer, save :: f1count(3), f2count(3), g1count(3), g2count(3)
+ !$omp declare target enter (f1count, f2count)
+ !$omp declare target enter (g1count, g2count)
+
+ f1count(1) = 0
+ f1count(2) = 0
+ f1count(3) = 0
+ f2count(1) = 0
+ f2count(2) = 0
+ f2count(3) = 0
+
+ g1count(1) = 0
+ g1count(2) = 0
+ g1count(3) = 0
+ g2count(1) = 0
+ g2count(2) = 0
+ g2count(3) = 0
+
+ call s1 (3, 4, 5)
+
+ ! All intervening code at the same depth must be executed the same
+ ! number of times.
+ if (f1count(1) /= f2count(1)) error stop 101
+ if (f1count(2) /= f2count(2)) error stop 102
+ if (f1count(3) /= f2count(3)) error stop 103
+ if (g1count(1) /= f1count(1)) error stop 104
+ if (g2count(1) /= f1count(1)) error stop 105
+ if (g1count(2) /= f1count(2)) error stop 106
+ if (g2count(2) /= f1count(2)) error stop 107
+ if (g1count(3) /= f1count(3)) error stop 108
+ if (g2count(3) /= f1count(3)) error stop 109
+
+ ! Intervening code must be executed at least as many times as the loop
+ ! that encloses it.
+ if (f1count(1) < 3) error stop 111
+ if (f1count(2) < 3 * 4) error stop 112
+
+ ! Intervening code must not be executed more times than the number
+ ! of logical iterations.
+ if (f1count(1) > 3 * 4 * 5) error stop 121
+ if (f1count(2) > 3 * 4 * 5) error stop 122
+
+ ! Check that the innermost loop body is executed exactly the number
+ ! of logical iterations expected.
+ if (f1count(3) /= 3 * 4 * 5) error stop 131
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ f1count(depth) = f1count(depth) + 1
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ f2count(depth) = f2count(depth) + 1
+end subroutine
+
+subroutine g1 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ g1count(depth) = g1count(depth) + 1
+end subroutine
+
+subroutine g2 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ g2count(depth) = g2count(depth) + 1
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp target parallel do collapse(3) map(always, tofrom:f1count, f2count, g1count, g2count)
+ do i = 1, a1
+ call f1 (1, i)
+ block
+ integer :: local1
+ local1 = 1
+ call g1 (local1, i)
+ do j = 1, a2
+ call f1 (2, j)
+ block
+ integer :: local2
+ local2 = 2
+ call g1 (local2, j)
+ do k = 1, a3
+ call f1 (3, k)
+ block
+ integer :: local3
+ local3 = 3
+ call g1 (local3, k)
+ call g2 (local3, k)
+ end block
+ call f2 (3, k)
+ end do
+ call g2 (local2, j)
+ end block
+ call f2 (2, j)
+ end do
+ call g2 (local1, i)
+ end block
+ call f2 (1, i)
+ end do
+
+end subroutine
+
+end program
diff --git a/libgomp/testsuite/libgomp.fortran/target-imperfect4.f90 b/libgomp/testsuite/libgomp.fortran/target-imperfect4.f90
new file mode 100644
index 00000000000..59ec0e92b05
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/target-imperfect4.f90
@@ -0,0 +1,126 @@
+! { dg-do run }
+
+! Like imperfect4.f90, but enables offloading.
+
+program foo
+ integer, save :: f1count(3), f2count(3), g1count(3), g2count(3)
+ !$omp declare target enter (f1count, f2count)
+ !$omp declare target enter (g1count, g2count)
+
+ f1count(1) = 0
+ f1count(2) = 0
+ f1count(3) = 0
+ f2count(1) = 0
+ f2count(2) = 0
+ f2count(3) = 0
+
+ g1count(1) = 0
+ g1count(2) = 0
+ g1count(3) = 0
+ g2count(1) = 0
+ g2count(2) = 0
+ g2count(3) = 0
+
+ call s1 (3, 4, 5)
+
+ ! All intervening code at the same depth must be executed the same
+ ! number of times.
+ if (f1count(1) /= f2count(1)) error stop 101
+ if (f1count(2) /= f2count(2)) error stop 102
+ if (f1count(3) /= f2count(3)) error stop 103
+ if (g1count(1) /= f1count(1)) error stop 104
+ if (g2count(1) /= f1count(1)) error stop 105
+ if (g1count(2) /= f1count(2)) error stop 106
+ if (g2count(2) /= f1count(2)) error stop 107
+ if (g1count(3) /= f1count(3)) error stop 108
+ if (g2count(3) /= f1count(3)) error stop 109
+
+ ! Intervening code must be executed at least as many times as the loop
+ ! that encloses it.
+ if (f1count(1) < 3) error stop 111
+ if (f1count(2) < 3 * 4) error stop 112
+
+ ! Intervening code must not be executed more times than the number
+ ! of logical iterations.
+ if (f1count(1) > 3 * 4 * 5) error stop 121
+ if (f1count(2) > 3 * 4 * 5) error stop 122
+
+ ! Check that the innermost loop body is executed exactly the number
+ ! of logical iterations expected.
+ if (f1count(3) /= 3 * 4 * 5) error stop 131
+
+contains
+
+subroutine f1 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ f1count(depth) = f1count(depth) + 1
+end subroutine
+
+subroutine f2 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ f2count(depth) = f2count(depth) + 1
+end subroutine
+
+subroutine g1 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ g1count(depth) = g1count(depth) + 1
+end subroutine
+
+subroutine g2 (depth, iter)
+ integer :: depth, iter
+ !$omp atomic
+ g2count(depth) = g2count(depth) + 1
+end subroutine
+
+subroutine s1 (a1, a2, a3)
+ integer :: a1, a2, a3
+ integer :: i, j, k
+
+ !$omp target parallel do collapse(3) map(always, tofrom:f1count, f2count, g1count, g2count)
+ do i = 1, a1
+ block
+ call f1 (1, i)
+ end block
+ block
+ block
+ call g1 (1, i)
+ end block
+ do j = 1, a2
+ block
+ call f1 (2, j)
+ end block
+ block
+ block
+ call g1 (2, j)
+ end block
+ do k = 1, a3
+ call f1 (3, k)
+ block
+ call g1 (3, k)
+ call g2 (3, k)
+ end block
+ call f2 (3, k)
+ end do
+ block
+ call g2 (2, j)
+ end block
+ end block
+ block
+ call f2 (2, j)
+ end block
+ end do
+ block
+ call g2 (1, i)
+ end block
+ end block
+ block
+ call f2 (1, i)
+ end block
+ end do
+
+end subroutine
+
+end program
--
2.31.1
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH V2 0/5] OpenMP: support for imperfectly-nested loops
2023-07-23 22:15 [PATCH V2 0/5] OpenMP: support for imperfectly-nested loops Sandra Loosemore
` (4 preceding siblings ...)
2023-07-23 22:15 ` [PATCH V2 5/5] OpenMP: Fortran support for imperfectly-nested loops Sandra Loosemore
@ 2023-08-22 12:56 ` Jakub Jelinek
5 siblings, 0 replies; 15+ messages in thread
From: Jakub Jelinek @ 2023-08-22 12:56 UTC (permalink / raw)
To: Sandra Loosemore; +Cc: gcc-patches
On Sun, Jul 23, 2023 at 04:15:16PM -0600, Sandra Loosemore wrote:
> Here is the latest version of my imperfectly-nested loops patches.
> Compared to the initial version I'd posted in April
>
> https://gcc.gnu.org/pipermail/gcc-patches/2023-April/617103.html
>
> this version includes many minor cosmetic fixes suggested by Jakub in
> his initial review (also present in the version I committed to the
> OG13 branch last month), many new test cases to cover various corner
> cases, and code fixes so that C and C++ at least behave consistently
> even if the spec is unclear. The most intrusive of those fixes is
> that I couldn't figure out how to make jumping between different
> structured blocks of intervening code in the same OMP loop construct
> produce errors without introducing new GENERIC and GIMPLE data
> structures to represent a structured block without any other
> associated OpenMP semantics; that's now part 1 of the patch series.
>
> There are a few things from the review comments I haven't done anything
> about:
>
> * I left omp-api.h alone because the Fortran front end needs those
> declarations without everything else in omp-general.h.
Ok.
> * I didn't think I ought to be speculatively implementing extensions
> like allowing "do { ... } while (0);" in intervening code. If it's
> really important for supporting macros, I suppose it will make it
> into a future version of the OpenMP spec.
Ack.
> * I didn't understand the comment about needing to add "#pragma omp
> ordered doacross(source) and sink" to the testcase for errors with
> the "ordered" clause. Isn't that only for cross-iteration
> data dependencies? There aren't any in that loop. Also note that some
> of my new corner-case tests use the "ordered" clause to trigger an
> error to check that things are being correctly parsed as intervening
> code, so if there is something really bogus there that must be fixed,
> it now affects other test cases as well.
ordered(N) clause is meant to be used with doacross loops, where one uses
#pragma omp ordered depend/doacross in the body.
So, when one is testing the rejection of imperfectly nested loops with it,
it is better to actually test it on something properly formed except for the
extra code making the loop imperfectly nested, rather than test it on
something which doesn't have the ordered directives in the body at all.
> * Likewise I didn't know what to do with coming up with a better
> testcase for "scan". I could not find an existing testcase with nested
> loops that I could just add intervening code to, and when I made
What about libgomp.c-c++-common/scan-1.c ?
Obviously, you can cut the initialization and checking, because that is a
runtime testcase and all you need is a compile time test; perhaps put each
of the 2 loop nests into a separate function and just add some code in
between the loops + dg-error.
Jakub
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH V2 1/5] OpenMP: Add OMP_STRUCTURED_BLOCK and GIMPLE_OMP_STRUCTURED_BLOCK.
2023-07-23 22:15 ` [PATCH V2 1/5] OpenMP: Add OMP_STRUCTURED_BLOCK and GIMPLE_OMP_STRUCTURED_BLOCK Sandra Loosemore
@ 2023-08-22 13:01 ` Jakub Jelinek
0 siblings, 0 replies; 15+ messages in thread
From: Jakub Jelinek @ 2023-08-22 13:01 UTC (permalink / raw)
To: Sandra Loosemore; +Cc: gcc-patches
On Sun, Jul 23, 2023 at 04:15:17PM -0600, Sandra Loosemore wrote:
> In order to detect invalid jumps in and out of intervening code in
> imperfectly-nested loops, the front ends need to insert some sort of
> marker to identify the structured block sequences that they push into
> the inner body of the loop. The error checking happens in the
> diagnose_omp_blocks pass, between gimplification and OMP lowering, so
> we need both GENERIC and GIMPLE representations of these markers.
> They are removed in OMP lowering so no subsequent passes need to know
> about them.
>
> This patch doesn't include any front-end changes to generate the new
> data structures.
>
> gcc/cp/ChangeLog
> * constexpr.cc (cxx_eval_constant_expression): Handle
> OMP_STRUCTURED_BLOCK.
> * pt.cc (tsubst_expr): Likewise.
>
> gcc/ChangeLog
> * doc/generic.texi (OpenMP): Document OMP_STRUCTURED_BLOCK.
> * doc/gimple.texi (GIMPLE instruction set): Add
> GIMPLE_OMP_STRUCTURED_BLOCK.
> (GIMPLE_OMP_STRUCTURED_BLOCK): New subsection.
> * gimple-low.cc (lower_stmt): Error on GIMPLE_OMP_STRUCTURED_BLOCK.
> * gimple-pretty-print.cc (dump_gimple_omp_block): Handle
> GIMPLE_OMP_STRUCTURED_BLOCK.
> (pp_gimple_stmt_1): Likewise.
> * gimple-walk.cc (walk_gimple_stmt): Likewise.
> * gimple.cc (gimple_build_omp_structured_block): New.
> * gimple.def (GIMPLE_OMP_STRUCTURED_BLOCK): New.
> * gimple.h (gimple_build_omp_structured_block): Declare.
> (gimple_has_substatements): Handle GIMPLE_OMP_STRUCTURED_BLOCK.
> (CASE_GIMPLE_OMP): Likewise.
> * gimplify.cc (is_gimple_stmt): Handle OMP_STRUCTURED_BLOCK.
> (gimplify_expr): Likewise.
> * omp-expand.cc (GIMPLE_OMP_STRUCTURED_BLOCK): Error on
> GIMPLE_OMP_STRUCTURED_BLOCK.
> * omp-low.cc (scan_omp_1_stmt): Handle GIMPLE_OMP_STRUCTURED_BLOCK.
> (lower_omp_1): Likewise.
> (diagnose_sb_1): Likewise.
> (diagnose_sb_2): Likewise.
> * tree-inline.cc (remap_gimple_stmt): Handle
> GIMPLE_OMP_STRUCTURED_BLOCK.
> (estimate_num_insns): Likewise.
> * tree-nested.cc (convert_nonlocal_reference_stmt): Likewise.
> (convert_local_reference_stmt): Likewise.
> (convert_gimple_call): Likewise.
> * tree-pretty-print.cc (dump_generic_node): Handle
> OMP_STRUCTURED_BLOCK.
> * tree.def (OMP_STRUCTURED_BLOCK): New.
> * tree.h (OMP_STRUCTURED_BLOCK_BODY): New.
> --- a/gcc/gimple-low.cc
> +++ b/gcc/gimple-low.cc
> @@ -717,6 +717,11 @@ lower_stmt (gimple_stmt_iterator *gsi, struct lower_data *data)
> gsi_next (gsi);
> return;
>
> + case GIMPLE_OMP_STRUCTURED_BLOCK:
> + /* These are supposed to be removed already in OMP lowering. */
> + gcc_unreachable ();
> + break;
Please don't add break; after gcc_unreachable ();
> --- a/gcc/omp-expand.cc
> +++ b/gcc/omp-expand.cc
> @@ -10592,6 +10592,11 @@ expand_omp (struct omp_region *region)
> parent GIMPLE_OMP_SECTIONS region. */
> break;
>
> + case GIMPLE_OMP_STRUCTURED_BLOCK:
> + /* We should have gotten rid of these in gimple lowering. */
> + gcc_unreachable ();
> + break;
And here neither.
Otherwise LGTM.
Jakub
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH V2 2/5] OpenMP: C front end support for imperfectly-nested loops
2023-07-23 22:15 ` [PATCH V2 2/5] OpenMP: C front end support for imperfectly-nested loops Sandra Loosemore
@ 2023-08-22 13:23 ` Jakub Jelinek
2023-08-22 18:53 ` Sandra Loosemore
0 siblings, 1 reply; 15+ messages in thread
From: Jakub Jelinek @ 2023-08-22 13:23 UTC (permalink / raw)
To: Sandra Loosemore; +Cc: gcc-patches
> New common C/C++ testcases are in a separate patch.
>
> gcc/c-family/ChangeLog
> * c-common.h (c_omp_check_loop_binding_exprs): Declare.
> * c-omp.cc: Include tree-iterator.h.
> (find_binding_in_body): New.
> (check_loop_binding_expr_r): New.
> (LOCATION_OR): New.
> (check_looop_binding_expr): New.
> (c_omp_check_loop_binding_exprs): New.
>
> gcc/c/ChangeLog
> * c-parser.cc (struct c_parser): Add omp_for_parse_state field.
> (struct omp_for_parse_data): New.
> (check_omp_intervening_code): New.
> (add_structured_block_stmt): New.
> (c_parser_compound_statement_nostart): Recognize intervening code,
> nested loops, and other things that need special handling in
> OpenMP loop constructs.
> (c_parser_while_statement): Error on loop in intervening code.
> (c_parser_do_statement): Likewise.
> (c_parser_for_statement): Likewise.
> (c_parser_postfix_expression_after_primary): Error on calls to
> the OpenMP runtime in intervening code.
> (c_parser_pragma): Error on OpenMP pragmas in intervening code.
> (c_parser_omp_loop_nest): New.
> (c_parser_omp_for_loop): Rewrite to use recursive descent, calling
> c_parser_omp_loop_nest to do the heavy lifting.
>
> gcc/ChangeLog
> * omp-api.h: New.
> * omp-general.cc (omp_runtime_api_procname): New.
> (omp_runtime_api_call): Moved here from omp-low.cc, and make
> non-static.
> * omp-general.h: Include omp-api.h.
> * omp-low.cc (omp_runtime_api_call): Delete this copy.
>
> gcc/testsuite/ChangeLog
> * c-c++-common/goacc/collapse-1.c: Update for new C error behavior.
> * c-c++-common/goacc/tile-2.c: Likewise.
> * gcc.dg/gomp/collapse-1.c: Likewise.
> diff --git a/gcc/testsuite/c-c++-common/goacc/collapse-1.c b/gcc/testsuite/c-c++-common/goacc/collapse-1.c
> index 11b14383983..0feac8f8ddb 100644
> --- a/gcc/testsuite/c-c++-common/goacc/collapse-1.c
> +++ b/gcc/testsuite/c-c++-common/goacc/collapse-1.c
> @@ -8,8 +8,8 @@ f1 (void)
> {
> #pragma acc parallel
> #pragma acc loop collapse (2)
> - for (i = 0; i < 5; i++)
> - ; /* { dg-error "not enough perfectly nested" } */
> + for (i = 0; i < 5; i++) /* { dg-error "not enough nested loops" } */
> + ;
> {
> for (j = 0; j < 5; j++)
> ;
All these c-c++-common testsuite changes will now FAIL after the C patch but
before the C++. It is nice to have the new c-c++-common tests in a separate
patch, but these tweaks which can't be just avoided need the temporary
{ target c } vs. { target c++} hacks undone later in the C++ patch.
> --- a/gcc/testsuite/c-c++-common/goacc/tile-2.c
> +++ b/gcc/testsuite/c-c++-common/goacc/tile-2.c
> @@ -3,8 +3,8 @@ int main ()
> #pragma acc parallel
> {
> #pragma acc loop tile (*,*)
> - for (int ix = 0; ix < 30; ix++)
> - ; /* { dg-error "not enough" } */
> + for (int ix = 0; ix < 30; ix++) /* { dg-error "not enough" "" { target c } } */
> + ; /* { dg-error "not enough" "" { target c++ } } */
E.g. like you do here.
Otherwise LGTM.
Jakub
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH V2 3/5] OpenMP: C++ support for imperfectly-nested loops
2023-07-23 22:15 ` [PATCH V2 3/5] OpenMP: C++ " Sandra Loosemore
@ 2023-08-22 13:31 ` Jakub Jelinek
0 siblings, 0 replies; 15+ messages in thread
From: Jakub Jelinek @ 2023-08-22 13:31 UTC (permalink / raw)
To: Sandra Loosemore; +Cc: gcc-patches
On Sun, Jul 23, 2023 at 04:15:19PM -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.
>
> 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.
> (check_omp_intervening_code): New.
> (cp_parser_statement_seq_opt): Special-case nested loops, blocks,
> and other constructs for OpenMP loops.
> (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_convert_omp_range_for): Likewise.
> (cp_parser_omp_loop_nest): New, split from cp_parser_omp_for_loop
> and largely rewritten. Add more comments.
> (insert_structured_blocks): New.
> (find_structured_blocks): New.
> (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 (finish_omp_for): Try harder to preserve location
> of loop variable init expression for use in diagnostics.
> (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
> * c-c++-common/goacc/tile-2.c: Adjust expected error patterns.
> * g++.dg/gomp/attrs-imperfect1.C: New test.
> * g++.dg/gomp/attrs-imperfect2.C: New test.
> * g++.dg/gomp/attrs-imperfect3.C: New test.
> * g++.dg/gomp/attrs-imperfect4.C: New test.
> * g++.dg/gomp/attrs-imperfect5.C: New test.
> * g++.dg/gomp/pr41967.C: Adjust expected error patterns.
> * g++.dg/gomp/tpl-imperfect-gotos.C: New test.
> * g++.dg/gomp/tpl-imperfect-invalid-scope.C: New test.
>
> libgomp/ChangeLog
> * testsuite/libgomp.c++/attrs-imperfect1.C: New test.
> * testsuite/libgomp.c++/attrs-imperfect2.C: New test.
> * testsuite/libgomp.c++/attrs-imperfect3.C: New test.
> * testsuite/libgomp.c++/attrs-imperfect4.C: New test.
> * testsuite/libgomp.c++/attrs-imperfect5.C: New test.
> * testsuite/libgomp.c++/attrs-imperfect6.C: New test.
> * testsuite/libgomp.c++/imperfect-class-1.C: New test.
> * testsuite/libgomp.c++/imperfect-class-2.C: New test.
> * testsuite/libgomp.c++/imperfect-class-3.C: New test.
> * testsuite/libgomp.c++/imperfect-destructor.C: New test.
> * testsuite/libgomp.c++/imperfect-template-1.C: New test.
> * testsuite/libgomp.c++/imperfect-template-2.C: New test.
> * testsuite/libgomp.c++/imperfect-template-3.C: New test.
Ok (though, if the c-c++-common tests are tweaked in the C patch,
this patch needs to undo that).
Jakub
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH V2 4/5] OpenMP: New C/C++ testcases for imperfectly nested loops.
2023-07-23 22:15 ` [PATCH V2 4/5] OpenMP: New C/C++ testcases for imperfectly nested loops Sandra Loosemore
@ 2023-08-22 13:34 ` Jakub Jelinek
0 siblings, 0 replies; 15+ messages in thread
From: Jakub Jelinek @ 2023-08-22 13:34 UTC (permalink / raw)
To: Sandra Loosemore; +Cc: gcc-patches
On Sun, Jul 23, 2023 at 04:15:20PM -0600, Sandra Loosemore wrote:
> gcc/testsuite/ChangeLog
> * c-c++-common/gomp/imperfect-attributes.c: New.
> * c-c++-common/gomp/imperfect-badloops.c: New.
> * c-c++-common/gomp/imperfect-blocks.c: New.
> * c-c++-common/gomp/imperfect-extension.c: New.
> * c-c++-common/gomp/imperfect-gotos.c: New.
> * c-c++-common/gomp/imperfect-invalid-scope.c: New.
> * c-c++-common/gomp/imperfect-labels.c: New.
> * c-c++-common/gomp/imperfect-legacy-syntax.c: New.
> * c-c++-common/gomp/imperfect-pragmas.c: New.
> * c-c++-common/gomp/imperfect1.c: New.
> * c-c++-common/gomp/imperfect2.c: New.
> * c-c++-common/gomp/imperfect3.c: New.
> * c-c++-common/gomp/imperfect4.c: New.
> * c-c++-common/gomp/imperfect5.c: New.
>
> libgomp/ChangeLog
> * testsuite/libgomp.c-c++-common/imperfect1.c: New.
> * testsuite/libgomp.c-c++-common/imperfect2.c: New.
> * testsuite/libgomp.c-c++-common/imperfect3.c: New.
> * testsuite/libgomp.c-c++-common/imperfect4.c: New.
> * testsuite/libgomp.c-c++-common/imperfect5.c: New.
> * testsuite/libgomp.c-c++-common/imperfect6.c: New.
> * testsuite/libgomp.c-c++-common/target-imperfect1.c: New.
> * testsuite/libgomp.c-c++-common/target-imperfect2.c: New.
> * testsuite/libgomp.c-c++-common/target-imperfect3.c: New.
> * testsuite/libgomp.c-c++-common/target-imperfect4.c: New.
As I wrote in reply to the cover letter, I'd prefer the
ordered(2)/ordered(3) nests to have #pragma omp ordered doacross(source:)
and #pragma omp ordered doacross(sink: ...) directives and
use the libgomp scan-1.c as basis for the scan tests.
Otherwise LGTM.
Jakub
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH V2 5/5] OpenMP: Fortran support for imperfectly-nested loops
2023-07-23 22:15 ` [PATCH V2 5/5] OpenMP: Fortran support for imperfectly-nested loops Sandra Loosemore
@ 2023-08-22 13:37 ` Jakub Jelinek
2023-08-24 16:36 ` Tobias Burnus
0 siblings, 1 reply; 15+ messages in thread
From: Jakub Jelinek @ 2023-08-22 13:37 UTC (permalink / raw)
To: Sandra Loosemore, Tobias Burnus; +Cc: gcc-patches
On Sun, Jul 23, 2023 at 04:15:21PM -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.
>
> In the Fortran front end, most of the semantic processing happens during
> the translation phase, so the parse phase just collects the intervening
> statements, checks them for errors, and splices them around the loop body.
>
> gcc/fortran/ChangeLog
> * gfortran.h (struct gfc_namespace): Add omp_structured_block bit.
> * openmp.cc: Include omp-api.h.
> (resolve_omp_clauses): Consolidate inscan reduction clause conflict
> checking here.
> (find_nested_loop_in_chain): New.
> (find_nested_loop_in_block): New.
> (gfc_resolve_omp_do_blocks): Set omp_current_do_collapse properly.
> Handle imperfectly-nested loops when looking for nested omp scan.
> Refactor to move inscan reduction clause conflict checking to
> resolve_omp_clauses.
> (gfc_resolve_do_iterator): Handle imperfectly-nested loops.
> (struct icode_error_state): New.
> (icode_code_error_callback): New.
> (icode_expr_error_callback): New.
> (diagnose_intervening_code_errors_1): New.
> (diagnose_intervening_code_errors): New.
> (make_structured_block): New.
> (restructure_intervening_code): New.
> (is_outer_iteration_variable): Do not assume loops are perfectly
> nested.
> (check_nested_loop_in_chain): New.
> (check_nested_loop_in_block_state): New.
> (check_nested_loop_in_block_symbol): New.
> (check_nested_loop_in_block): New.
> (expr_uses_intervening_var): New.
> (is_intervening_var): New.
> (expr_is_invariant): Do not assume loops are perfectly nested.
> (resolve_omp_do): Handle imperfectly-nested loops.
> * trans-stmt.cc (gfc_trans_block_construct): Generate
> OMP_STRUCTURED_BLOCK if magic bit is set on block namespace.
>
> gcc/testsuite/ChangeLog
> * gfortran.dg/gomp/collapse1.f90: Adjust expected errors.
> * gfortran.dg/gomp/collapse2.f90: Likewise.
> * gfortran.dg/gomp/imperfect-gotos.f90: New.
> * gfortran.dg/gomp/imperfect-invalid-scope.f90: New.
> * gfortran.dg/gomp/imperfect1.f90: New.
> * gfortran.dg/gomp/imperfect2.f90: New.
> * gfortran.dg/gomp/imperfect3.f90: New.
> * gfortran.dg/gomp/imperfect4.f90: New.
> * gfortran.dg/gomp/imperfect5.f90: New.
>
> libgomp/ChangeLog
> * testsuite/libgomp.fortran/imperfect-destructor.f90: New.
> * testsuite/libgomp.fortran/imperfect1.f90: New.
> * testsuite/libgomp.fortran/imperfect2.f90: New.
> * testsuite/libgomp.fortran/imperfect3.f90: New.
> * testsuite/libgomp.fortran/imperfect4.f90: New.
> * testsuite/libgomp.fortran/target-imperfect1.f90: New.
> * testsuite/libgomp.fortran/target-imperfect2.f90: New.
> * testsuite/libgomp.fortran/target-imperfect3.f90: New.
> * testsuite/libgomp.fortran/target-imperfect4.f90: New.
LGTM, but please let Tobias have a second look unless he has done so
already.
Jakub
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH V2 2/5] OpenMP: C front end support for imperfectly-nested loops
2023-08-22 13:23 ` Jakub Jelinek
@ 2023-08-22 18:53 ` Sandra Loosemore
2023-08-24 11:49 ` Jakub Jelinek
0 siblings, 1 reply; 15+ messages in thread
From: Sandra Loosemore @ 2023-08-22 18:53 UTC (permalink / raw)
To: Jakub Jelinek, Sandra Loosemore; +Cc: gcc-patches
On 8/22/23 07:23, Jakub Jelinek wrote:
>
>> diff --git a/gcc/testsuite/c-c++-common/goacc/collapse-1.c b/gcc/testsuite/c-c++-common/goacc/collapse-1.c
>> index 11b14383983..0feac8f8ddb 100644
>> --- a/gcc/testsuite/c-c++-common/goacc/collapse-1.c
>> +++ b/gcc/testsuite/c-c++-common/goacc/collapse-1.c
>> @@ -8,8 +8,8 @@ f1 (void)
>> {
>> #pragma acc parallel
>> #pragma acc loop collapse (2)
>> - for (i = 0; i < 5; i++)
>> - ; /* { dg-error "not enough perfectly nested" } */
>> + for (i = 0; i < 5; i++) /* { dg-error "not enough nested loops" } */
>> + ;
>> {
>> for (j = 0; j < 5; j++)
>> ;
>
> All these c-c++-common testsuite changes will now FAIL after the C patch but
> before the C++. It is nice to have the new c-c++-common tests in a separate
> patch, but these tweaks which can't be just avoided need the temporary
> { target c } vs. { target c++} hacks undone later in the C++ patch.
In spite of being in the c-c++-common subdirectory, this particular testcase is
presently run only for C:
/* { dg-skip-if "not yet" { c++ } } */
I did previously do incremental testing between applying the the C and C++
parts of the series to confirm that there were no regressions.
BTW, thanks for your previous detailed review of the original version of this
patch, and pointing out many things I'd overlooked; I feel that V2 is much more
robust and correct as a result. :-)
-Sandra
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH V2 2/5] OpenMP: C front end support for imperfectly-nested loops
2023-08-22 18:53 ` Sandra Loosemore
@ 2023-08-24 11:49 ` Jakub Jelinek
0 siblings, 0 replies; 15+ messages in thread
From: Jakub Jelinek @ 2023-08-24 11:49 UTC (permalink / raw)
To: Sandra Loosemore; +Cc: Sandra Loosemore, gcc-patches
On Tue, Aug 22, 2023 at 12:53:19PM -0600, Sandra Loosemore wrote:
> > All these c-c++-common testsuite changes will now FAIL after the C patch but
> > before the C++. It is nice to have the new c-c++-common tests in a separate
> > patch, but these tweaks which can't be just avoided need the temporary
> > { target c } vs. { target c++} hacks undone later in the C++ patch.
>
> In spite of being in the c-c++-common subdirectory, this particular testcase
> is presently run only for C:
>
> /* { dg-skip-if "not yet" { c++ } } */
Oh, sorry, I didn't think of such an possibility, it is just weird.
Please ignore my comments on this topic then.
Jakub
^ permalink raw reply [flat|nested] 15+ messages in thread
* Re: [PATCH V2 5/5] OpenMP: Fortran support for imperfectly-nested loops
2023-08-22 13:37 ` Jakub Jelinek
@ 2023-08-24 16:36 ` Tobias Burnus
0 siblings, 0 replies; 15+ messages in thread
From: Tobias Burnus @ 2023-08-24 16:36 UTC (permalink / raw)
To: Jakub Jelinek, Sandra Loosemore; +Cc: gcc-patches
On 22.08.23 15:37, Jakub Jelinek wrote:
> On Sun, Jul 23, 2023 at 04:15:21PM -0600, Sandra Loosemore wrote:
>> [...]
>> In the Fortran front end, most of the semantic processing happens during
>> the translation phase, so the parse phase just collects the intervening
>> statements, checks them for errors, and splices them around the loop body.
> LGTM, but please let Tobias have a second look unless he has done so
> already.
LGTM but some minor comments:
@@ -9764,13 +9820,12 @@ gfc_resolve_do_iterator (gfc_code *code, gfc_symbol *sym, bool add_clause)
...
- c = c->block->next;
- }
+ c = find_nested_loop_in_chain (c->block->next);
+ }
Here the indentation of '}' is now in col 3 instead of 4.
* * *
I was wondering whether any removed error message (due to early return on error)
will cause missed error checking, but it looks as if all are covered elsewhere.
:-)
* * *
Can you update the implementation status - either as part of this patch as it is
the last in the series or as follow up? It feels good to see 'Y' getting added there.
https://gcc.gnu.org/onlinedocs/libgomp/OpenMP-5_002e0.html
"Collapse of associated loops that are imperfectly nested loops"
alias libgomp/libgomp.texi in line 203.
Thanks,
Tobias
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
^ permalink raw reply [flat|nested] 15+ messages in thread
end of thread, other threads:[~2023-08-24 16:37 UTC | newest]
Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-23 22:15 [PATCH V2 0/5] OpenMP: support for imperfectly-nested loops Sandra Loosemore
2023-07-23 22:15 ` [PATCH V2 1/5] OpenMP: Add OMP_STRUCTURED_BLOCK and GIMPLE_OMP_STRUCTURED_BLOCK Sandra Loosemore
2023-08-22 13:01 ` Jakub Jelinek
2023-07-23 22:15 ` [PATCH V2 2/5] OpenMP: C front end support for imperfectly-nested loops Sandra Loosemore
2023-08-22 13:23 ` Jakub Jelinek
2023-08-22 18:53 ` Sandra Loosemore
2023-08-24 11:49 ` Jakub Jelinek
2023-07-23 22:15 ` [PATCH V2 3/5] OpenMP: C++ " Sandra Loosemore
2023-08-22 13:31 ` Jakub Jelinek
2023-07-23 22:15 ` [PATCH V2 4/5] OpenMP: New C/C++ testcases for imperfectly nested loops Sandra Loosemore
2023-08-22 13:34 ` Jakub Jelinek
2023-07-23 22:15 ` [PATCH V2 5/5] OpenMP: Fortran support for imperfectly-nested loops Sandra Loosemore
2023-08-22 13:37 ` Jakub Jelinek
2023-08-24 16:36 ` Tobias Burnus
2023-08-22 12:56 ` [PATCH V2 0/5] OpenMP: " Jakub Jelinek
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).