public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/omp/gcc-11] Bind(c): Fix bugs in CFI_section
@ 2021-08-20  3:05 Sandra Loosemore
  0 siblings, 0 replies; only message in thread
From: Sandra Loosemore @ 2021-08-20  3:05 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:5fb197bb665363eb556690fb2d3106c2d0caff69

commit 5fb197bb665363eb556690fb2d3106c2d0caff69
Author: Sandra Loosemore <sandra@codesourcery.com>
Date:   Wed Aug 11 18:54:24 2021 -0700

    Bind(c): Fix bugs in CFI_section
    
    CFI_section was incorrectly adjusting the base pointer for the result
    array twice in different ways.  It was also overwriting the array
    dimension info in the result descriptor before computing the base
    address offset from the source descriptor, which caused problems if
    the two descriptors are the same.  This patch fixes both problems and
    makes the code simpler, too.
    
    A consequence of this patch is that the result array is now 0-based in
    all dimensions instead of starting at the numbering to match the first
    element of the source array.  The Fortran standard only specifies the
    shape of the result array, not its lower bounds, so this is permitted
    and probably less confusing for users as well as implementors.
    
    2021-07-17  Sandra Loosemore  <sandra@codesourcery.com>
    
            PR libfortran/101310
    
    libgfortran/
            * runtime/ISO_Fortran_binding.c (CFI_section): Fix the base
            address computation and simplify the code.
    
    gcc/testsuite/
            * gfortran.dg/ISO_Fortran_binding_1.c (section_c): Remove
            incorrect assertions.
    
    (cherry picked from commit b4a9bc7856ee1d3ff98b04402334a362540af2cf)

Diff:
---
 gcc/testsuite/ChangeLog.omp                       | 10 +++++++++
 gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c | 10 +++++----
 libgfortran/ChangeLog.omp                         | 10 +++++++++
 libgfortran/runtime/ISO_Fortran_binding.c         | 27 +++++++++--------------
 4 files changed, 36 insertions(+), 21 deletions(-)

diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp
index 7310ea53702..8188a8e86a6 100644
--- a/gcc/testsuite/ChangeLog.omp
+++ b/gcc/testsuite/ChangeLog.omp
@@ -1,3 +1,13 @@
+2021-08-11  Sandra Loosemore  <sandra@codesourcery.com>
+
+	Backported from master:
+
+	2021-07-17  Sandra Loosemore  <sandra@codesourcery.com>
+
+	PR libfortran/101310
+	* gfortran.dg/ISO_Fortran_binding_1.c (section_c): Remove
+	incorrect assertions.
+
 2021-08-11  Sandra Loosemore  <sandra@codesourcery.com>
 
 	Backported from master:
diff --git a/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c b/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c
index 9da5d858806..bb56ca0e04b 100644
--- a/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c
+++ b/gcc/testsuite/gfortran.dg/ISO_Fortran_binding_1.c
@@ -142,11 +142,12 @@ float section_c(int *std_case, CFI_cdesc_t * source, int *low, int *str)
 			  CFI_type_float, 0, 1, NULL);
       if (ind) return -1.0;
       ind = CFI_section((CFI_cdesc_t *)&section, source, lower, NULL, strides);
-      assert (section.dim[0].lower_bound == lower[0]);
       if (ind) return -2.0;
 
       /* Sum over the section  */
-      for (idx[0] = lower[0]; idx[0] < section.dim[0].extent + lower[0]; idx[0]++)
+      for (idx[0] = section.dim[0].lower_bound;
+	   idx[0] < section.dim[0].extent + section.dim[0].lower_bound;
+	   idx[0]++)
         ans += *(float*)CFI_address ((CFI_cdesc_t*)&section, idx);
       return ans;
     }
@@ -164,11 +165,12 @@ float section_c(int *std_case, CFI_cdesc_t * source, int *low, int *str)
       ind = CFI_section((CFI_cdesc_t *)&section, source,
 			lower, upper, strides);
       assert (section.rank == 1);
-      assert (section.dim[0].lower_bound == lower[0]);
       if (ind) return -2.0;
 
       /* Sum over the section  */
-      for (idx[0] = lower[0]; idx[0] < section.dim[0].extent + lower[0]; idx[0]++)
+      for (idx[0] = section.dim[0].lower_bound;
+	   idx[0] < section.dim[0].extent + section.dim[0].lower_bound;
+	   idx[0]++)
         ans += *(float*)CFI_address ((CFI_cdesc_t*)&section, idx);
       return ans;
     }
diff --git a/libgfortran/ChangeLog.omp b/libgfortran/ChangeLog.omp
index 8abf0e52817..cf5fae694c9 100644
--- a/libgfortran/ChangeLog.omp
+++ b/libgfortran/ChangeLog.omp
@@ -1,3 +1,13 @@
+2021-08-11  Sandra Loosemore  <sandra@codesourcery.com>
+
+	Backported from master:
+
+	2021-07-17  Sandra Loosemore  <sandra@codesourcery.com>
+
+	PR libfortran/101310
+	* runtime/ISO_Fortran_binding.c (CFI_section): Fix the base
+	address computation and simplify the code.
+
 2021-08-11  Sandra Loosemore  <sandra@codesourcery.com>
 
 	Backported from master:
diff --git a/libgfortran/runtime/ISO_Fortran_binding.c b/libgfortran/runtime/ISO_Fortran_binding.c
index b1e51612e2b..2830c4575fe 100644
--- a/libgfortran/runtime/ISO_Fortran_binding.c
+++ b/libgfortran/runtime/ISO_Fortran_binding.c
@@ -689,29 +689,22 @@ int CFI_section (CFI_cdesc_t *result, const CFI_cdesc_t *source,
 	}
     }
 
+  /* Set the base address.  We have to compute this first in the case
+     where source == result, before we overwrite the dimension data.  */
+  result->base_addr = CFI_address (source, lower);
+
   /* Set the appropriate dimension information that gives us access to the
    * data. */
-  int aux = 0;
-  for (int i = 0; i < source->rank; i++)
+  for (int i = 0, o = 0; i < source->rank; i++)
     {
       if (stride[i] == 0)
-	{
-	  aux++;
-	  /* Adjust 'lower' for the base address offset.  */
-	  lower[i] = lower[i] - source->dim[i].lower_bound;
-	  continue;
-	}
-      int idx = i - aux;
-      result->dim[idx].lower_bound = lower[i];
-      result->dim[idx].extent = 1 + (upper[i] - lower[i])/stride[i];
-      result->dim[idx].sm = stride[i] * source->dim[i].sm;
-      /* Adjust 'lower' for the base address offset.  */
-      lower[idx] = lower[idx] - source->dim[i].lower_bound;
+	continue;
+      result->dim[o].lower_bound = 0;
+      result->dim[o].extent = 1 + (upper[i] - lower[i])/stride[i];
+      result->dim[o].sm = stride[i] * source->dim[i].sm;
+      o++;
     }
 
-  /* Set the base address. */
-  result->base_addr = CFI_address (source, lower);
-
   return CFI_SUCCESS;
 }


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

only message in thread, other threads:[~2021-08-20  3:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-20  3:05 [gcc/devel/omp/gcc-11] Bind(c): Fix bugs in CFI_section Sandra Loosemore

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