Patchwork (Re)allocation of allocatable arrays on assignment - F2003

login
register
mail settings
Submitter Tobias Burnus
Date Nov. 2, 2010, 10:21 a.m.
Message ID <4CCFE60D.7040208@net-b.de>
Download mbox | patch
Permalink /patch/69869/
State New
Headers show

Comments

Tobias Burnus - Nov. 2, 2010, 10:21 a.m.
On 10/31/2010 07:01 PM, Paul Richard Thomas wrote:
> This patch has been some weeks arriving at state where it could be
> submitted.  It was seen in an advanced state before the end of stage 1
> and it was felt then that it could be included in 4.6.0 if this
> version followed rapidly.
Well, to my knowledge, we are still in Stage 1 as there was no 
announcement stating otherwise. However, I think the end of Stage 1 is 
imminent.

> Bootstrapped and regtested on FC9/x86_64 - OK for trunk?

I will try to test and review your patch this evening. However, as 
written before I like to have the option to disable the new feature to 
allow to use -std=gnu with the old speed and old bound checks. I do not 
expect that many users will use it; however, for some programs/users it 
might make a difference.

How about something like the attached patch? (Relative to your patch and 
untested.)

Tobias

Patch

diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 2d0d4eb..126d20d 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -2238,6 +2238,7 @@  typedef struct
   int flag_align_commons;
   int flag_whole_file;
   int flag_protect_parens;
+  int flag_realloc_lhs;
 
   int fpe;
   int rtcheck;
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index bacab6b..fbbd8d2 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -171,7 +171,7 @@  and warnings}.
 -fblas-matmul-limit=@var{n} -frecursive -finit-local-zero @gol
 -finit-integer=@var{n} -finit-real=@var{<zero|inf|-inf|nan|snan>} @gol
 -finit-logical=@var{<true|false>} -finit-character=@var{n} @gol
--fno-align-commons -fno-protect-parens}
+-fno-align-commons -fno-protect-parens -frealloc_lhs}
 @end table
 
 @menu
@@ -1458,6 +1458,13 @@  levels such that the compiler does not do any re-association. Using
 @code{COMPLEX} expressions to produce faster code. Note that for the re-association
 optimization @option{-fno-signed-zeros} and @option{-fno-trapping-math}
 need to be in effect.
+
+@item -frealloc-lhs
+@opindex @code{frealloc_lhs}
+@cindex Reallocate the LHS in assignments
+An allocatable left-hand side of an intrinsic assignment is automatically
+(re)allocated if it is either unallocated or has a different shape. The
+option is enabled by default except when @option{-std=f95} is given.
 @end table
 
 @xref{Code Gen Options,,Options for Code Generation Conventions,
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
index 6088730..2462a8e 100644
--- a/gcc/fortran/lang.opt
+++ b/gcc/fortran/lang.opt
@@ -478,6 +478,10 @@  frange-check
 Fortran
 Enable range checking during compilation
 
+frealloc_lhs
+Fortran
+Reallocate the LHS in assignments
+
 frecord-marker=4
 Fortran RejectNegative
 Use a 4-byte record marker for unformatted files
diff --git a/gcc/fortran/options.c b/gcc/fortran/options.c
index 36dd9c8..a564f59 100644
--- a/gcc/fortran/options.c
+++ b/gcc/fortran/options.c
@@ -149,6 +149,7 @@  gfc_init_options (unsigned int decoded_options_count,
   gfc_option.flag_init_character_value = (char)0;
   gfc_option.flag_align_commons = 1;
   gfc_option.flag_protect_parens = 1;
+  gfc_option.flag_realloc_lhs = -1;
   
   gfc_option.fpe = 0;
   gfc_option.rtcheck = 0;
@@ -266,6 +267,16 @@  gfc_post_options (const char **pfilename)
   if (flag_associative_math == -1)
     flag_associative_math = (!flag_trapping_math && !flag_signed_zeros);
 
+  /* By default, disable (re)allocation during assignment for -std=f95,
+     and enable it for F2003/F2008/GNU/Legacy. */
+  if (gfc_option.flag_realloc_lhs == -1)
+    {
+      if (gfc_option.allow_std & GFC_STD_F2003)
+	gfc_option.flag_realloc_lhs = 1;
+      else
+	gfc_option.flag_realloc_lhs = 0;
+    }
+
   /* -fbounds-check is equivalent to -fcheck=bounds */
   if (flag_bounds_check)
     gfc_option.rtcheck |= GFC_RTCHECK_BOUNDS;
@@ -964,6 +975,10 @@  gfc_handle_option (size_t scode, const char *arg, int value,
       gfc_option.flag_protect_parens = value;
       break;
 
+    case OPT_frealloc_lhs:
+      gfc_option.flag_realloc_lhs = value;
+      break;
+
     case OPT_fcheck_:
       gfc_handle_runtime_check_option (arg);
       break;
diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c
index 966d83b..7fc44b2 100644
--- a/gcc/fortran/trans-array.c
+++ b/gcc/fortran/trans-array.c
@@ -3230,8 +3230,7 @@  gfc_conv_ss_startstride (gfc_loopinfo * loop)
 	    continue;
 
 	  /* Catch allocatable lhs in f2003.  */
-	  if (gfc_option.allow_std & GFC_STD_F2003
-		&& ss->is_alloc_lhs)
+	  if (gfc_option.flag_realloc_lhs && ss->is_alloc_lhs)
 	    continue;
 
 	  gfc_start_block (&inner);