public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Frederik Harwath <frederik@codesourcery.com>
To: <gcc-patches@gcc.gnu.org>, <tobias@codesourcery.com>, <jakub@redhat.com>
Subject: [PATCH 4/4] openmp: Fix number of iterations computation for "omp unroll full"
Date: Fri, 28 Jul 2023 13:04:33 +0000	[thread overview]
Message-ID: <20230728130433.2377366-5-frederik@codesourcery.com> (raw)
In-Reply-To: <20230728130433.2377366-1-frederik@codesourcery.com>

gcc/ChangeLog:

        * omp-transform-loops.cc (gomp_for_number_of_iterations):
        Always compute "final - init" and do not take absolute value.
        Identify non-iterating and infinite loops for constant init,
        final, step values for better diagnostic messages, consistent
        behaviour in those corner cases, and better testability.
        (gomp_for_constant_iterations_p): Add new argument to pass
        on information about infinite loops, and ...
        (full_unroll): ... use from here to emit a warning and remove
        unrolled, known infinite loops consistently.
        (process_omp_for): Only print dump message if loop has not
        been removed by transformation.

gcc/testsuite/ChangeLog:

        * c-c++-common/gomp/loop-transforms/unroll-8.c: New test.
---
 gcc/omp-transform-loops.cc                    | 94 ++++++++++++++-----
 .../gomp/loop-transforms/unroll-8.c           | 76 +++++++++++++++
 2 files changed, 146 insertions(+), 24 deletions(-)
 create mode 100644 gcc/testsuite/c-c++-common/gomp/loop-transforms/unroll-8.c

diff --git a/gcc/omp-transform-loops.cc b/gcc/omp-transform-loops.cc
index c8853bcee89..b0645397641 100644
--- a/gcc/omp-transform-loops.cc
+++ b/gcc/omp-transform-loops.cc
@@ -153,20 +153,27 @@ subst_defs (tree expr, gimple_seq seq)
   return expr;
 }

-/* Return an expression for the number of iterations of the outermost loop of
-   OMP_FOR. */
+/* Return an expression for the number of iterations of the loop at
+   the given LEVEL of OMP_FOR.
+
+   If the expression is a negative constant, this means that the loop
+   is infinite. This can only be recognized for loops with constant
+   initial, final, and step values.  In general, according to the
+   OpenMP specification, the behaviour is unspecified if the number of
+   iterations does not fit the types used for their computation, and
+   hence in particular if the loop is infinite. */

 tree
 gomp_for_number_of_iterations (const gomp_for *omp_for, size_t level)
 {
   gcc_assert (!non_rectangular_p (omp_for));
-
   tree init = gimple_omp_for_initial (omp_for, level);
   tree final = gimple_omp_for_final (omp_for, level);
   tree_code cond = gimple_omp_for_cond (omp_for, level);
   tree index = gimple_omp_for_index (omp_for, level);
   tree type = gomp_for_iter_count_type (index, final);
-  tree step = TREE_OPERAND (gimple_omp_for_incr (omp_for, level), 1);
+  tree incr = gimple_omp_for_incr (omp_for, level);
+  tree step = omp_get_for_step_from_incr (gimple_location (omp_for), incr);

   init = subst_defs (init, gimple_omp_for_pre_body (omp_for));
   init = fold (init);
@@ -181,34 +188,64 @@ gomp_for_number_of_iterations (const gomp_for *omp_for, size_t level)
       diff_type = ptrdiff_type_node;
     }

-  tree diff;
-  if (cond == GT_EXPR)
-    diff = fold_build2 (minus_code, diff_type, init, final);
-  else if (cond == LT_EXPR)
-    diff = fold_build2 (minus_code, diff_type, final, init);
-  else
-    gcc_unreachable ();

-  diff = fold_build2 (CEIL_DIV_EXPR, type, diff, step);
-  diff = fold_build1 (ABS_EXPR, type, diff);
+  /* Identify a simple case in which the loop does not iterate. The
+     computation below could not tell this apart from an infinite
+     loop, hence we handle this separately for better diagnostic
+     messages. */
+  gcc_assert (cond == GT_EXPR || cond == LT_EXPR);
+  if (TREE_CONSTANT (init) && TREE_CONSTANT (final)
+      && ((cond == GT_EXPR && tree_int_cst_le (init, final))
+         || (cond == LT_EXPR && tree_int_cst_le (final, init))))
+    return build_int_cst (diff_type, 0);
+
+  tree diff = fold_build2 (minus_code, diff_type, final, init);
+
+  /* Divide diff by the step.
+
+     We could always use CEIL_DIV_EXPR since only non-negative results
+     correspond to valid number of iterations and the behaviour is
+     unspecified by the spec otherwise. But we try to get the rounding
+     right for constant negative values to identify infinite loops
+     more precisely for better warnings. */
+  tree_code div_expr = CEIL_DIV_EXPR;
+  if (TREE_CONSTANT (diff) && TREE_CONSTANT (step))
+    {
+      bool diff_is_neg = tree_int_cst_lt (diff, size_zero_node);
+      bool step_is_neg = tree_int_cst_lt (step, size_zero_node);
+      if ((diff_is_neg && !step_is_neg)
+         || (!diff_is_neg && step_is_neg))
+       div_expr = FLOOR_DIV_EXPR;
+    }

+  diff = fold_build2 (div_expr, type, diff, step);
   return diff;
 }

-/* Return true if the expression representing the number of iterations for
-   OMP_FOR is a constant expression, false otherwise. */
+/* Return true if the expression representing the number of iterations
+   for OMP_FOR is a non-negative constant and set ITERATIONS to the
+   value of that expression. Otherwise, return false.  Set INFINITE to
+   true if the number of iterations was recognized to be infinite. */

 bool
 gomp_for_constant_iterations_p (gomp_for *omp_for,
-                               unsigned HOST_WIDE_INT *iterations)
+                               unsigned HOST_WIDE_INT *iterations,
+                               bool *infinite = NULL)
 {
   tree t = gomp_for_number_of_iterations (omp_for, 0);
-  if (!TREE_CONSTANT (t)
-      || !tree_fits_uhwi_p (t))
+  if (!TREE_CONSTANT (t))
     return false;

-  *iterations = tree_to_uhwi (t);
-  return true;
+  if (infinite &&
+      tree_int_cst_lt (t, size_zero_node))
+    *infinite = true;
+  else if (tree_fits_uhwi_p (t))
+    {
+      *iterations = tree_to_uhwi (t);
+      return true;
+    }
+
+  return false;
 }

 static gimple_seq
@@ -525,10 +562,18 @@ full_unroll (gomp_for *omp_for, location_t loc, walk_ctx *ctx ATTRIBUTE_UNUSED)
 {
   tree init = gimple_omp_for_initial (omp_for, 0);
   unsigned HOST_WIDE_INT niter = 0;
-  if (!gomp_for_constant_iterations_p (omp_for, &niter))
+  bool infinite = false;
+  bool constant = gomp_for_constant_iterations_p (omp_for, &niter, &infinite);
+
+  if (infinite)
+    {
+      warning_at (loc, 0, "Cannot apply full unrolling to infinite loop");
+      return NULL;
+    }
+  if (!constant)
     {
       error_at (loc, "Cannot apply full unrolling to loop with "
-                    "non-constant number of iterations");
+               "non-constant number of iterations");
       return omp_for;
     }

@@ -1595,8 +1640,9 @@ process_omp_for (gomp_for *omp_for, gimple_seq *containing_seq, walk_ctx *ctx)
   if (!dump_enabled_p () || !(dump_flags & TDF_DETAILS))
     return;

-  dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, transformed,
-                  "Transformed loop: %G\n\n", transformed);
+  if (transformed)
+    dump_printf_loc (MSG_NOTE | MSG_PRIORITY_INTERNALS, transformed,
+                    "Transformed loop: %G\n\n", transformed);
 }

 /* Traverse SEQ in depth-first order and apply the loop transformation
diff --git a/gcc/testsuite/c-c++-common/gomp/loop-transforms/unroll-8.c b/gcc/testsuite/c-c++-common/gomp/loop-transforms/unroll-8.c
new file mode 100644
index 00000000000..d49d7c42c87
--- /dev/null
+++ b/gcc/testsuite/c-c++-common/gomp/loop-transforms/unroll-8.c
@@ -0,0 +1,76 @@
+extern void dummy(int);
+
+void
+test1 ()
+{
+#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */
+  for (int i = 101; i > 100; i++)
+    dummy (i);
+}
+
+
+void
+test2 ()
+{
+#pragma omp unroll full
+  for (int i = 101; i != 100; i++)
+    dummy (i);
+}
+
+void
+test3 ()
+{
+#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */
+  for (int i = 0; i <= 0; i--)
+    dummy (i);
+}
+
+void
+test4 ()
+{
+#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */
+  for (int i = 101; i > 100; i=i+2)
+    dummy (i);
+}
+
+void
+test5 ()
+{
+#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */
+  for (int i = -101; i < 100; i=i-10)
+    dummy (i);
+}
+
+void
+test6 ()
+{
+#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */
+  for (int i = -101; i < 100; i=i-300)
+    dummy (i);
+}
+
+void
+test7 ()
+{
+#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */
+  for (int i = 101; i > -100; i=i+300)
+    dummy (i);
+
+  /* Loop does not iterate, hence no warning. */
+#pragma omp unroll full
+  for (int i = 101; i > 101; i=i+300)
+    dummy (i);
+}
+
+void
+test8 ()
+{
+#pragma omp unroll full /* { dg-warning "Cannot apply full unrolling to infinite loop" } */
+  for (int i = -21; i < -20; i=i-40)
+    dummy (i);
+
+  /* Loop does not iterate, hence no warning. */
+#pragma omp unroll full
+  for (int i = -21; i > 20; i=i-40)
+    dummy (i);
+}
--
2.36.1

-----------------
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

      parent reply	other threads:[~2023-07-28 13:05 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-24 15:30 [PATCH 0/7] openmp: OpenMP 5.1 loop transformation directives Frederik Harwath
2023-03-24 15:30 ` [PATCH 1/7] openmp: Add Fortran support for "omp unroll" directive Frederik Harwath
2023-04-01  8:42   ` Thomas Schwinge
2023-04-06 13:07     ` Frederik Harwath
2023-03-24 15:30 ` [PATCH 2/7] openmp: Add C/C++ " Frederik Harwath
2023-03-24 15:30 ` [PATCH 3/7] openacc: Rename OMP_CLAUSE_TILE to OMP_CLAUSE_OACC_TILE Frederik Harwath
2023-03-24 15:30 ` [PATCH 4/7] openmp: Add Fortran support for "omp tile" Frederik Harwath
2023-03-24 15:30 ` [PATCH 5/7] openmp: Add C/C++ " Frederik Harwath
2023-03-24 15:30 ` [PATCH 6/7] openmp: Add Fortran support for loop transformations on inner loops Frederik Harwath
2023-03-24 15:30 ` [PATCH 7/7] openmp: Add C/C++ " Frederik Harwath
2023-05-15 10:19 ` [PATCH 0/7] openmp: OpenMP 5.1 loop transformation directives Jakub Jelinek
2023-05-15 11:03   ` Jakub Jelinek
2023-05-16  9:45   ` Frederik Harwath
2023-05-16 11:00     ` Jakub Jelinek
2023-05-17 11:55       ` Frederik Harwath
2023-05-22 14:20         ` Jakub Jelinek
2023-07-28 13:04 ` [PATCH 0/4] openmp: loop transformation fixes Frederik Harwath
2023-07-28 13:04   ` [PATCH 1/4] openmp: Fix loop transformation tests Frederik Harwath
2023-07-28 13:04   ` [PATCH 2/4] openmp: Fix initialization for 'unroll full' Frederik Harwath
2023-07-28 13:04   ` [PATCH 3/4] openmp: Fix diagnostic message for "omp unroll" Frederik Harwath
2023-07-28 13:04   ` Frederik Harwath [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230728130433.2377366-5-frederik@codesourcery.com \
    --to=frederik@codesourcery.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=tobias@codesourcery.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).