Fix off-by-one issue with inline matmul
authorTobias Burnus <burnus@net-b.de>
Fri, 12 Oct 2018 18:18:13 +0000 (20:18 +0200)
committerTobias Burnus <burnus@gcc.gnu.org>
Fri, 12 Oct 2018 18:18:13 +0000 (20:18 +0200)
        PR fortran/87597
        * expr.c (gfc_simplify_expr): Avoid simplifying
        the 'array' argument to lbound/ubound/lcobound/
        ucobound.

        PR fortran/87597
        * gfortran.dg/inline_matmul_24.f90: New.

From-SVN: r265126

gcc/fortran/ChangeLog
gcc/fortran/expr.c
gcc/testsuite/ChangeLog
gcc/testsuite/gfortran.dg/inline_matmul_24.f90 [new file with mode: 0644]

index 21813638bf308decd20427e49f9b89e239acdd7d..4ee065a19a0b74a89009edb994f4e5f10ce75926 100644 (file)
@@ -1,3 +1,10 @@
+2018-10-12  Tobias Burnus  <burnus@net-b.de>
+
+       PR fortran/87597
+       * expr.c (gfc_simplify_expr): Avoid simplifying
+       the 'array' argument to lbound/ubound/lcobound/
+       ucobound.
+
 2018-10-12  Tobias Burnus <burnus@net-b.de>
 
        PR fortran/58787
index 1cfda5fbfed08e57bb1cfe57744205c2d6a7ec4f..ca6f95d9d8ef02f1006f1d28f81a3c4c7d41151b 100644 (file)
@@ -1937,7 +1937,20 @@ gfc_simplify_expr (gfc_expr *p, int type)
       break;
 
     case EXPR_FUNCTION:
-      for (ap = p->value.function.actual; ap; ap = ap->next)
+      // For array-bound functions, we don't need to optimize
+      // the 'array' argument. In particular, if the argument
+      // is a PARAMETER, simplifying might convert an EXPR_VARIABLE
+      // into an EXPR_ARRAY; the latter has lbound = 1, the former
+      // can have any lbound.
+      ap = p->value.function.actual;
+      if (p->value.function.isym &&
+         (p->value.function.isym->id == GFC_ISYM_LBOUND
+          || p->value.function.isym->id == GFC_ISYM_UBOUND
+          || p->value.function.isym->id == GFC_ISYM_LCOBOUND
+          || p->value.function.isym->id == GFC_ISYM_UCOBOUND))
+       ap = ap->next;
+
+      for ( ; ap; ap = ap->next)
        if (!gfc_simplify_expr (ap->expr, type))
          return false;
 
index 4991c2ef54fbaeba3fd1cffdd5a8cfc2e7410d34..3604fe631374bd551fd69925ed3548d2953d17c4 100644 (file)
@@ -1,3 +1,8 @@
+2018-10-12  Tobias Burnus  <burnus@net-b.de>
+
+       PR fortran/87597
+       * gfortran.dg/inline_matmul_24.f90: New.
+
 2018-10-12  Tobias Burnus <burnus@net-b.de>
 
        PR fortran/58787
diff --git a/gcc/testsuite/gfortran.dg/inline_matmul_24.f90 b/gcc/testsuite/gfortran.dg/inline_matmul_24.f90
new file mode 100644 (file)
index 0000000..067f6da
--- /dev/null
@@ -0,0 +1,42 @@
+! { dg-do run }
+! { dg-options "-ffrontend-optimize -fdump-tree-original" }
+!
+! PR fortran/87597
+!
+! Contributed by gallmeister
+!
+! Before, for the inlined matmul,
+! gamma5 was converted to an EXPR_ARRAY with lbound = 1
+! instead of the lbound = 0 as declared; leading to
+! an off-by-one problem.
+!
+program testMATMUL
+  implicit none
+    complex, dimension(0:3,0:3), parameter :: gamma5 = reshape((/ 0., 0., 1., 0., &
+                                                                  0., 0., 0., 1., &
+                                                                  1., 0., 0., 0., &
+                                                                  0., 1., 0., 0. /),(/4,4/))
+    complex, dimension(0:3,0:3) :: A, B, D
+    integer :: i
+
+    A = 0.0
+    do i=0,3
+       A(i,i) = i*1.0
+    end do
+
+    B = cmplx(7,-9)
+    B = matmul(A,gamma5)
+
+    D = reshape([0, 0, 2, 0, &
+                 0, 0, 0, 3, &
+                 0, 0, 0, 0, &
+                 0, 1, 0, 0], [4, 4])
+    write(*,*) B(0,:)
+    write(*,*) B(1,:)
+    write(*,*) B(2,:)
+    write(*,*) B(3,:)
+    if (any(B /= D)) then
+      call abort()
+    end if
+end program testMATMUL
+! { dg-final { scan-tree-dump-times "gamma5\[__var_1_do \\* 4 \\+ __var_2_do\]" 1 "optimized" } }