public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/omp/gcc-11] openmp: Scale type precision of collapsed iterator variable
@ 2021-05-13 16:19 Kwok Yeung
  0 siblings, 0 replies; only message in thread
From: Kwok Yeung @ 2021-05-13 16:19 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:22317bf493e755b0bef013b658d897f2e454eeec

commit 22317bf493e755b0bef013b658d897f2e454eeec
Author: Kwok Cheung Yeung <kcy@codesourcery.com>
Date:   Mon Mar 1 14:15:30 2021 -0800

    openmp: Scale type precision of collapsed iterator variable
    
    This sets the type precision of the collapsed iterator variable to the
    sum of the precision of the collapsed loop variables, up to a maximum of
    sizeof(long long) (i.e. 64-bits).
    
    2021-03-01  Kwok Cheung Yeung  <kcy@codesourcery.com>
    
            gcc/
            * omp-expand.c (expand_oacc_for): Convert .tile variable to
            diff_type before multiplying.
            * omp-general.c (omp_extract_for_data): Use accumulated precision
            of all collapsed for-loops as precision of iteration variable, up
            to the precision of a long long.
    
            libgomp/
            * testsuite/libgomp.c-c++-common/collapse-4.c: New.
            * testsuite/libgomp.fortran/collapse5.f90: New.

Diff:
---
 gcc/ChangeLog.omp                                  |  8 ++++++
 gcc/omp-expand.c                                   |  5 +++-
 gcc/omp-general.c                                  | 29 +++++++++++++++++-----
 libgomp/ChangeLog.omp                              |  5 ++++
 .../testsuite/libgomp.c-c++-common/collapse-4.c    | 23 +++++++++++++++++
 libgomp/testsuite/libgomp.fortran/collapse5.f90    | 23 +++++++++++++++++
 6 files changed, 86 insertions(+), 7 deletions(-)

diff --git a/gcc/ChangeLog.omp b/gcc/ChangeLog.omp
index d207a8eef18..ba99bebf072 100644
--- a/gcc/ChangeLog.omp
+++ b/gcc/ChangeLog.omp
@@ -1,3 +1,11 @@
+2021-03-01  Kwok Cheung Yeung  <kcy@codesourcery.com>
+
+	* omp-expand.c (expand_oacc_for): Convert .tile variable to
+	diff_type before multiplying.
+	* omp-general.c (omp_extract_for_data): Use accumulated precision
+	of all collapsed for-loops as precision of iteration variable, up
+	to the precision of a long long.
+
 2020-08-24  Tobias Burnus  <tobias@codesourcery.com>
 
 	* omp-sese.c: Fix comment typo.
diff --git a/gcc/omp-expand.c b/gcc/omp-expand.c
index bc7337442cf..3828679ee35 100644
--- a/gcc/omp-expand.c
+++ b/gcc/omp-expand.c
@@ -7631,7 +7631,10 @@ expand_oacc_for (struct omp_region *region, struct omp_for_data *fd)
       tile_size = create_tmp_var (diff_type, ".tile_size");
       expr = build_int_cst (diff_type, 1);
       for (int ix = 0; ix < fd->collapse; ix++)
-	expr = fold_build2 (MULT_EXPR, diff_type, counts[ix].tile, expr);
+	{
+	  tree tile = fold_convert (diff_type, counts[ix].tile);
+	  expr = fold_build2 (MULT_EXPR, diff_type, tile, expr);
+	}
       expr = force_gimple_operand_gsi (&gsi, expr, true,
 				       NULL_TREE, true, GSI_SAME_STMT);
       ass = gimple_build_assign (tile_size, expr);
diff --git a/gcc/omp-general.c b/gcc/omp-general.c
index ed21aca0be9..b9cd78bdeb0 100644
--- a/gcc/omp-general.c
+++ b/gcc/omp-general.c
@@ -357,6 +357,7 @@ omp_extract_for_data (gomp_for *for_stmt, struct omp_for_data *fd,
 	  fd->non_rect = true;
 	}
     }
+  int accum_iter_precision = 0;
   for (i = 0; i < cnt; i++)
     {
       if (i == 0
@@ -439,12 +440,28 @@ omp_extract_for_data (gomp_for *for_stmt, struct omp_for_data *fd,
 	{
 	  if (fd->collapse == 1 && !fd->tiling)
 	    iter_type = TREE_TYPE (loop->v);
-	  else if (i == 0
-		   || TYPE_PRECISION (iter_type)
-		      < TYPE_PRECISION (TREE_TYPE (loop->v)))
-	    iter_type
-	      = build_nonstandard_integer_type
-		  (TYPE_PRECISION (TREE_TYPE (loop->v)), 1);
+	  else
+	    {
+	      int loop_precision = TYPE_PRECISION (TREE_TYPE (loop->v));
+	      int iter_type_precision = 0;
+	      const int max_accum_precision
+		= TYPE_PRECISION (long_long_unsigned_type_node);
+
+	      accum_iter_precision += loop_precision;
+
+	      if (i == 0
+		  || (loop_precision >= max_accum_precision
+		      && loop_precision >= TYPE_PRECISION (iter_type)))
+		iter_type_precision = loop_precision;
+	      else if (TYPE_PRECISION (iter_type) < max_accum_precision)
+		iter_type_precision
+		  = MIN (1 << ceil_log2 (accum_iter_precision),
+			 max_accum_precision);
+
+	      if (iter_type_precision)
+		iter_type = build_nonstandard_integer_type
+			      (iter_type_precision, 1);
+	    }
 	}
       else if (iter_type != long_long_unsigned_type_node)
 	{
diff --git a/libgomp/ChangeLog.omp b/libgomp/ChangeLog.omp
index c46821cd96e..0519191a936 100644
--- a/libgomp/ChangeLog.omp
+++ b/libgomp/ChangeLog.omp
@@ -1,3 +1,8 @@
+2021-03-01  Kwok Cheung Yeung  <kcy@codesourcery.com>
+
+	* testsuite/libgomp.c-c++-common/collapse-4.c: New.
+	* testsuite/libgomp.fortran/collapse5.f90: New.
+
 2020-09-17  Kwok Cheung Yeung  <kcy@codesourcery.com>
 
 	* testsuite/libgomp.oacc-fortran/privatized-ref-1.f95 (workers, vectors):
diff --git a/libgomp/testsuite/libgomp.c-c++-common/collapse-4.c b/libgomp/testsuite/libgomp.c-c++-common/collapse-4.c
new file mode 100644
index 00000000000..c0af29f5463
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/collapse-4.c
@@ -0,0 +1,23 @@
+/* { dg-do run } */
+
+#include <stdlib.h>
+
+int
+main (void)
+{
+  int i, j;
+  int count = 0;
+
+  #pragma omp parallel for collapse(2)
+    for (i = 0; i < 80000; i++)
+      for (j = 0; j < 80000; j++)
+	if (i == 66666 && j == 77777)
+	  /* In the collapsed loop space, this is iteration
+	     66666*80000+77777==5,333,357,777.  If the type of the iterator
+	     for the collapsed loop is only a 32-bit unsigned int, then this
+	     iteration will exceed its maximum range and be skipped.  */
+	  count++;
+
+  if (count != 1)
+    abort ();
+}
diff --git a/libgomp/testsuite/libgomp.fortran/collapse5.f90 b/libgomp/testsuite/libgomp.fortran/collapse5.f90
new file mode 100644
index 00000000000..5632d9bab02
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/collapse5.f90
@@ -0,0 +1,23 @@
+! { dg-do run }
+
+program collapse5
+  implicit none
+
+  integer :: i, j
+  integer :: count = 0
+
+  !$omp parallel do collapse (2)
+    do i = 1, 80000
+      do j = 1, 80000
+        if (i .eq. 66666 .and. j .eq. 77777) then
+	  ! In the collapsed loop space, this is iteration
+	  ! 66666*80000+77777==5,333,357,777.  If the type of the iterator
+	  ! for the collapsed loop is only a 32-bit unsigned int, then this
+	  ! iteration will exceed its maximum range and be skipped.
+	  count = count + 1
+	end if
+      end do
+    end do
+
+  if (count .ne. 1) stop 1
+end


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-05-13 16:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-13 16:19 [gcc/devel/omp/gcc-11] openmp: Scale type precision of collapsed iterator variable Kwok Yeung

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