Hello world,

What it says in the ChangeLog.

Regression-tested. OK for trunk and backport? (This should be one of the
safest backports ever :-)

Bets regards

        Thomas

Fix size calculation for reallocation for matmul of rank 2 a and rank 1 b.

This is a rather simple one-character (well, two-bit) fix for th
calculation of the size of the reallocation of the LHS if that
is allocatable.

gcc/fortran/ChangeLog:

        PR fortran/126386
        * frontend-passes.cc (matmul_lhs_realloc):

gcc/testsuite/ChangeLog:

        PR fortran/126386
        * gfortran.dg/inline_matmul_28.f90: New test.
diff --git a/gcc/fortran/frontend-passes.cc b/gcc/fortran/frontend-passes.cc
index 92678310ffd..70e8ec9e161 100644
--- a/gcc/fortran/frontend-passes.cc
+++ b/gcc/fortran/frontend-passes.cc
@@ -3565,9 +3565,9 @@ matmul_lhs_realloc (gfc_expr *c, gfc_expr *a, gfc_expr *b,
     case A2B1:
       ar->start[0] = get_array_inq_function (GFC_ISYM_SIZE, a, 1);
       cond = build_logical_expr (INTRINSIC_NE,
 				 get_array_inq_function (GFC_ISYM_SIZE, c, 1),
-				 get_array_inq_function (GFC_ISYM_SIZE, a, 2));
+				 get_array_inq_function (GFC_ISYM_SIZE, a, 1));
       break;
 
     case A1B2:
       ar->start[0] = get_array_inq_function (GFC_ISYM_SIZE, b, 2);
diff --git a/gcc/testsuite/gfortran.dg/inline_matmul_28.f90 b/gcc/testsuite/gfortran.dg/inline_matmul_28.f90
new file mode 100644
index 00000000000..569c29c41c8
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/inline_matmul_28.f90
@@ -0,0 +1,21 @@
+! { dg-do run }
+! { dg-options "-ffrontend-optimize" }
+! PR 126386 - size calculation for allocation of the lhs for matmmul
+! was wrong.  Original test case by Christoph Hofer.
+
+PROGRAM reallocation_bug
+    REAL(8), DIMENSION(:), ALLOCATABLE :: y
+    REAL(8), DIMENSION(3,2) :: A
+    real(8), dimension(3) :: x
+    
+    A(:,1) = [1,-2,3]
+    A(:,2) = [-4,5,6]
+    
+    x =  [7,-8,9]
+
+    ALLOCATE(y(3))
+    y = matmul(transpose(2*a),x)
+    if (size(y,1) /= 2) stop 1
+    if (any(y /= [100, -28])) stop 2
+    
+END PROGRAM reallocation_bug

Reply via email to