Hello world,

the attached patch also performs a check for matrix-vector
multiplication.  I found it by poking around the source while
looking to do some improvements, which should be coming up
shortly once this is out of the way.

Regression-tested.  OK for trunk?

Regards

        Thomas

2017-05-01  Thomas Koenig  <tkoe...@gcc.gnu.org>

        PR fortran/37131
        * frontend-passes.c (inline_matmul_assign): Also check bounds
        for allocatable lhs and matrix-vector-multiplication.

2017-05-01  Thomas Koenig  <tkoe...@gcc.gnu.org>

        PR fortran/37131
        * gfortran.dg/matmul_bounds_11.f90:  New test.
Index: frontend-passes.c
===================================================================
--- frontend-passes.c	(Revision 247003)
+++ frontend-passes.c	(Arbeitskopie)
@@ -3068,20 +3068,31 @@ inline_matmul_assign (gfc_code **c, int *walk_subt
       /* Only need to check a single dimension for the A2B2 case for
 	 bounds checking, the rest will be allocated.  */
 
-      if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS && m_case == A2B2)
+      if (gfc_option.rtcheck & GFC_RTCHECK_BOUNDS)
 	{
 	  gfc_code *test;
 	  gfc_expr *a2, *b1;
 
-	  a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2);
-	  b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1);
-	  test = runtime_error_ne (b1, a2, "Dimension of array B incorrect "
+	if (m_case == A2B2)
+	  {
+	    a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2);
+	    b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1);
+	    test = runtime_error_ne (b1, a2, "Dimension of array B incorrect "
+				     "in MATMUL intrinsic: Is %ld, should be %ld");
+	    *next_code_point = test;
+	    next_code_point = &test->next;
+	  }
+	else if (m_case == A2B1)
+	  {
+	    a2 = get_array_inq_function (GFC_ISYM_SIZE, matrix_a, 2);
+	    b1 = get_array_inq_function (GFC_ISYM_SIZE, matrix_b, 1);
+	    test = runtime_error_ne (b1, a2, "Dimension of array B incorrect "
 				   "in MATMUL intrinsic: Is %ld, should be %ld");
-	  *next_code_point = test;
-	  next_code_point = &test->next;
+	    *next_code_point = test;
+	    next_code_point = &test->next;
+	  }
 	}
 
-
       lhs_alloc = matmul_lhs_realloc (expr1, matrix_a, matrix_b, m_case);
 
       *next_code_point = lhs_alloc;
! { dg-do run }
! { dg-options "-O -finline-matmul-limit=30 -fcheck=all" }
! { dg-shouldfail "Dimension of array B incorrect in MATMUL intrinsic" }
program main
  real, dimension(:,:), allocatable :: a
  real, dimension(:), allocatable :: b
  real, dimension(:), allocatable :: res
  allocate (a(2,2), b(3))
  call random_number(a)
  call random_number(b)
  res = matmul(a,b)
  print *,res
end program main
! { dg-output "Fortran runtime error: Dimension of array B incorrect in MATMUL intrinsic.*" }

Reply via email to