diff mbox

[fortran] Create temporary variables for matmul

Message ID 82215091-bd8f-b450-9c45-765774bf3bef@netcologne.de
State New
Headers show

Commit Message

Thomas Koenig May 7, 2017, 10:18 a.m. UTC
Hello world,

the attached patch goes one step further in matmul inlinding.

It converts statements like

   r = dot_product(matmul(a2,v1),v2)

into

   tmp = matmul(a2,v1)
   r = dot_product(tmp,v2)

to enable inlining of matmul (but only if inlining
is active, of course).

In order to detect multiple uses of matmul, this is run
several times.  I did this because, with the current
implementation, create_var can fail if -fno-realloc-lhs
is specified.  This is also not optimal, but that's a bug
for another day, and I don't see any drawbacks in
code generation for this (the extra basic blocks will
be removed).

The actual overhead in the case of non-constant bounds should
be small to non-existent, this only replaces one type of
temporary with another.

I had to adjust some test cases which counted things to use
the library version.

Regression-tested.  OK for trunk?

Regards

	Thomas

P.S: Next on the agenda is to better handle the left-over combinations
for Matmul, and to create temporaries for dependencies and
function evaluations in the arguments that we currently do not
handle.

2017-05-07  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/79930
	* frontend-passes.c (matmul_to_var_expr): New function,
	add prototype.
	(matmul_to_var_code):  Likewise.
	(optimize_namespace):  Use them from gfc_code_walker.

2017-05-07  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/79930
	* gfortran.dg/inline_transpose_1.f90:  Add
	-finline-matmul-limit=0 to options.
	* gfortran.dg/matmul_5.f90:  Likewise.
	* gfortran.dg/vect/vect-8.f90: Likewise.
	* gfortran.dg/inline_matmul_14.f90:  New test.
	* gfortran.dg/inline_matmul_15.f90:  New test.
diff mbox

Patch

Index: fortran/frontend-passes.c
===================================================================
--- fortran/frontend-passes.c	(Revision 247566)
+++ fortran/frontend-passes.c	(Arbeitskopie)
@@ -43,6 +43,8 @@  static void optimize_reduction (gfc_namespace *);
 static int callback_reduction (gfc_expr **, int *, void *);
 static void realloc_strings (gfc_namespace *);
 static gfc_expr *create_var (gfc_expr *, const char *vname=NULL);
+static int matmul_to_var_expr (gfc_expr **, int *, void *);
+static int matmul_to_var_code (gfc_code **, int *, void *);
 static int inline_matmul_assign (gfc_code **, int *, void *);
 static gfc_code * create_do_loop (gfc_expr *, gfc_expr *, gfc_expr *,
 				  locus *, gfc_namespace *,
@@ -1076,9 +1078,20 @@  optimize_namespace (gfc_namespace *ns)
   gfc_code_walker (&ns->code, cfe_code, cfe_expr_0, NULL);
   gfc_code_walker (&ns->code, optimize_code, optimize_expr, NULL);
   if (flag_inline_matmul_limit != 0)
-    gfc_code_walker (&ns->code, inline_matmul_assign, dummy_expr_callback,
-		     NULL);
-
+    {
+      bool found;
+      do
+	{
+	  found = false;
+	  gfc_code_walker (&ns->code, matmul_to_var_code, matmul_to_var_expr,
+			   (void *) &found);
+	}
+      while (found);
+	
+      gfc_code_walker (&ns->code, inline_matmul_assign, dummy_expr_callback,
+		       NULL);
+    }
+  
   /* BLOCKs are handled in the expression walker below.  */
   for (ns = ns->contained; ns; ns = ns->sibling)
     {
@@ -2086,6 +2099,64 @@  doloop_warn (gfc_namespace *ns)
 
 /* This selction deals with inlining calls to MATMUL.  */
 
+/* Replace calls to matmul outside of straight assignments with a temporary
+   variable so that later inlining will work.  */
+
+static int
+matmul_to_var_expr (gfc_expr **ep, int *walk_subtrees ATTRIBUTE_UNUSED,
+		    void *data)
+{
+  gfc_expr *e, *n;
+  bool *found = (bool *) data;
+  
+  e = *ep;
+
+  if (e->expr_type != EXPR_FUNCTION
+      || e->value.function.isym == NULL
+      || e->value.function.isym->id != GFC_ISYM_MATMUL)
+    return 0;
+
+  if (forall_level > 0 || iterator_level > 0 || in_omp_workshare
+      || in_where)
+    return 0;
+
+  /* Check if this is already in the form c = matmul(a,b).  */
+  
+  if ((*current_code)->expr2 == e)
+    return 0;
+
+  n = create_var (e, "matmul");
+  
+  /* If create_var is unable to create a variable (for example if
+     -fno-realloc-lhs is in force with a variable that does not have bounds
+     known at compile-time), just return.  */
+
+  if (n == NULL)
+    return 0;
+  
+  *ep = n;
+  *found = true;
+  return 0;
+}
+
+/* Set current_code and associated variables so that matmul_to_var_expr can
+   work.  */
+
+static int
+matmul_to_var_code (gfc_code **c, int *walk_subtrees ATTRIBUTE_UNUSED,
+		    void *data ATTRIBUTE_UNUSED)
+{
+  if (current_code != c)
+    {
+      current_code = c;
+      inserted_block = NULL;
+      changed_statement = NULL;
+    }
+  
+  return 0;
+}
+
+
 /* Auxiliary function to build and simplify an array inquiry function.
    dim is zero-based.  */
 
Index: testsuite/gfortran.dg/inline_transpose_1.f90
===================================================================
--- testsuite/gfortran.dg/inline_transpose_1.f90	(Revision 247566)
+++ testsuite/gfortran.dg/inline_transpose_1.f90	(Arbeitskopie)
@@ -1,5 +1,5 @@ 
 ! { dg-do run }
-! { dg-options "-fdump-tree-original -fdump-tree-optimized -Warray-temporaries -fbounds-check" }
+! { dg-options "-finline-matmul-limit=0 -fdump-tree-original -fdump-tree-optimized -Warray-temporaries -fbounds-check" }
 
   implicit none
 
Index: testsuite/gfortran.dg/matmul_5.f90
===================================================================
--- testsuite/gfortran.dg/matmul_5.f90	(Revision 247566)
+++ testsuite/gfortran.dg/matmul_5.f90	(Arbeitskopie)
@@ -1,5 +1,6 @@ 
 ! { dg-do run }
 ! { dg-shouldfail "dimension of array B incorrect in MATMUL intrinsic" }
+! { dg-options "-finline-matmul-limit=0" }
 program main
   real, dimension(:,:), allocatable :: a
   real, dimension(:), allocatable :: b
Index: testsuite/gfortran.dg/vect/vect-8.f90
===================================================================
--- testsuite/gfortran.dg/vect/vect-8.f90	(Revision 247566)
+++ testsuite/gfortran.dg/vect/vect-8.f90	(Arbeitskopie)
@@ -1,5 +1,6 @@ 
 ! { dg-do compile }
 ! { dg-require-effective-target vect_double }
+! { dg-additional-options "-finline-matmul-limit=0" }
 
 module lfk_prec
  integer, parameter :: dp=kind(1.d0)