https://gcc.gnu.org/g:3390b0e262c7f26a440638a78e40866242067cf5

commit r15-11421-g3390b0e262c7f26a440638a78e40866242067cf5
Author: Thomas Koenig <[email protected]>
Date:   Sun Jul 26 13:41:32 2026 +0200

    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):  Fix size calculation of
            A2B1 case.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/126386
            * gfortran.dg/inline_matmul_28.f90: New test.
    
    (cherry picked from commit 4d855f3af5b18e539585d1d4832470e500323c12)

Diff:
---
 gcc/fortran/frontend-passes.cc                 |  2 +-
 gcc/testsuite/gfortran.dg/inline_matmul_28.f90 | 21 +++++++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/frontend-passes.cc b/gcc/fortran/frontend-passes.cc
index 8e2b9d73c40d..cac2998a2f91 100644
--- a/gcc/fortran/frontend-passes.cc
+++ b/gcc/fortran/frontend-passes.cc
@@ -3565,7 +3565,7 @@ matmul_lhs_realloc (gfc_expr *c, gfc_expr *a, gfc_expr *b,
       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:
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 000000000000..569c29c41c89
--- /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