Bug 56318 - [4.6/4.7/4.8 Regression] Wrong result with MATMUL of PARAMETER
Summary: [4.6/4.7/4.8 Regression] Wrong result with MATMUL of PARAMETER
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.8.0
: P4 normal
Target Milestone: 4.6.4
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization, wrong-code
Depends on:
Blocks: 56342
  Show dependency treegraph
 
Reported: 2013-02-14 10:19 UTC by Tobias Burnus
Modified: 2013-02-15 14:29 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-02-14 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2013-02-14 10:19:54 UTC
Reported by Alberto Luaces at http://gcc.gnu.org/ml/fortran/2013-02/msg00074.html

The code below should print the following - and it does with GCC 4.1/4.3/4.4 and NAG f95:
   0.333   0.000   0.000
   0.000   0.333   0.000
   0.000   0.000   0.000

However, with GCC 4.5/4.6/4.7/4.8, it prints:
   0.333   0.000   0.000
   0.000   0.000   0.333
   0.000   0.000   0.000


SUBROUTINE mass_matrix        
  DOUBLE PRECISION,PARAMETER::m1=1.d0
  DOUBLE PRECISION,DIMENSION(3,2),PARAMETER::A1=reshape([1.d0,0.d0, 0.d0, &
       0.d0,1.d0, 0.d0],[3,2])
  DOUBLE PRECISION,DIMENSION(2,2),PARAMETER::Mel=reshape([1.d0/3.d0, 0.d0, &
       0.d0, 1.d0/3.d0],[2,2])

  DOUBLE PRECISION,DIMENSION(3,3)::MM1

  MM1=m1*matmul(A1,matmul(Mel,transpose(A1)))
  print '(3f8.3)', MM1
  if (any (abs (MM1 &
                - reshape ([1.d0/3.d0, 0.d0,      0.d0,  &
                            0.d0,      1.d0/3.d0, 0.d0,  &
                            0.d0,      0.d0,      0.d0], &
                           [3,3])) > epsilon(1.0d0))) &
    call abort ()
END SUBROUTINE mass_matrix

program name
  implicit none
  call mass_matrix
end program name
Comment 1 Jakub Jelinek 2013-02-14 11:02:22 UTC
Started with http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=148243
Comment 2 Tobias Burnus 2013-02-14 11:29:55 UTC
Simplified test case:

!------------------------------------
integer, parameter :: A(3,2) = reshape([1,2,3,4,5,6],[3,2])
integer, parameter :: B(2,3) = reshape([1,1,1,1,1,1],[2,3])
integer, parameter :: m1 = 1

print '(3i3)', m1*matmul(A,B)
end
!------------------------------------

Expected:
  5  7  9
  5  7  9
  5  7  9

Actual result:
  9 12  8
  9 12  8
  4  6  8



a) BUG 1: The matrix is not always simplified


The "m1*" is crucial. When gfc_simplify_matmul is called initially, it returns NULL as  "is_constant_array_expr (matrix_a)" is false.

The "m1*" causes a re-evaluation of the RHS expression, namely in gfc_simplify_expr:
      for (ap = p->value.function.actual; ap; ap = ap->next)
        if (gfc_simplify_expr (ap->expr, type) == FAILURE)

That converts an EXPR_VARIABLE with flavor FL_PARAMETER into an EXPR_ARRAY, which can then be processed by calling gfc_simplify_matmul



b) BUG 2: matmul gives the wrong result
Comment 3 Mikael Morin 2013-02-14 12:55:29 UTC
(In reply to comment #2)
> b) BUG 2: matmul gives the wrong result:

Here is a fix for that.  It gives the expected results of comment #0 and comment #2.  Untested otherwise.

diff --git a/simplify.c b/simplify.c
index f7401e9..dcf3e89 100644
--- a/simplify.c
+++ b/simplify.c
@@ -3873,7 +3873,7 @@ gfc_simplify_matmul (gfc_expr *matrix_a, gfc_expr *matrix_b)
     {
       result_rows = mpz_get_si (matrix_a->shape[0]);
       result_columns = mpz_get_si (matrix_b->shape[1]);
-      stride_a = mpz_get_si (matrix_a->shape[1]);
+      stride_a = mpz_get_si (matrix_a->shape[0]);
       stride_b = mpz_get_si (matrix_b->shape[0]);
 
       result->rank = 2;
Comment 4 Tobias Burnus 2013-02-15 11:17:23 UTC
Author: burnus
Date: Fri Feb 15 11:17:15 2013
New Revision: 196075

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196075
Log:
2013-02-14  Tobias Burnus  <burnus@net-b.de>
            Mikael Morin  <mikael@gcc.gnu.org>

        PR fortran/56318
        * simplify.c (gfc_simplify_matmul): Fix result shape
        and matmul result.

2013-02-14  Tobias Burnus  <burnus@net-b.de>

        PR fortran/56318
        * gcc/testsuite/gfortran.dg/matmul_9.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/matmul_9.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog
Comment 5 Tobias Burnus 2013-02-15 14:20:30 UTC
Author: burnus
Date: Fri Feb 15 14:20:22 2013
New Revision: 196078

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196078
Log:
2013-02-15  Tobias Burnus  <burnus@net-b.de>
            Mikael Morin  <mikael@gcc.gnu.org>

        PR fortran/56318
        * simplify.c (gfc_simplify_matmul): Fix result shape
        and matmul result.

2013-02-15  Tobias Burnus  <burnus@net-b.de>

        PR fortran/56318
        * gcc/testsuite/gfortran.dg/matmul_9.f90: New.


Added:
    branches/gcc-4_7-branch/gcc/testsuite/gfortran.dg/matmul_9.f90
Modified:
    branches/gcc-4_7-branch/gcc/fortran/ChangeLog
    branches/gcc-4_7-branch/gcc/fortran/simplify.c
    branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
Comment 6 Tobias Burnus 2013-02-15 14:20:58 UTC
Author: burnus
Date: Fri Feb 15 14:20:49 2013
New Revision: 196079

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196079
Log:
2013-02-15  Tobias Burnus  <burnus@net-b.de>
            Mikael Morin  <mikael@gcc.gnu.org>

        PR fortran/56318
        * simplify.c (gfc_simplify_matmul): Fix result shape
        and matmul result.

2013-02-15  Tobias Burnus  <burnus@net-b.de>

        PR fortran/56318
        * gcc/testsuite/gfortran.dg/matmul_9.f90: New.


Added:
    branches/gcc-4_6-branch/gcc/testsuite/gfortran.dg/matmul_9.f90
Modified:
    branches/gcc-4_6-branch/gcc/fortran/ChangeLog
    branches/gcc-4_6-branch/gcc/fortran/simplify.c
    branches/gcc-4_6-branch/gcc/testsuite/ChangeLog
Comment 7 Tobias Burnus 2013-02-15 14:29:49 UTC
The wrong-code issue is now FIXED on the 4.8 trunk and the 4.6 and 4.7 branches. (4.5 is also affected but no longer maintained.)


The remaining issue, namely the missed optimization, is now tracked in PR 56342.