If one passes something which is not a variable as an argument, there is no point to do the copy-out – the copy-in is sufficient. Note that a the result of a pointer-returning function is regarded as variable.

As cleanup, I also fixed the indentation (twice) and the pointless 'fsym ?' check inside a condition body where the condition already checks this.

Build on x86-64-gnu-linux.
OK for the trunk – and for GCC 9? [It's a 9/10 regression]

Cheers,

Tobias

PS: I lost a bit the overview. Is there any patch pending review or otherwise pending? I know that PR92178 is pending (mostly okay, but some follow-up work needed/useful). Anything else?

	PR fortran/91640
	* trans-expr.c (gfc_conv_procedure_call): Avoid copy-out for nonvariable
	arguments to contiguous dummy args.  Avoid re-checking whether fsym is
	NULL.

	PR fortran/91640
	* gfortran.dg/contiguous_10.f90: New.

 gcc/fortran/trans-expr.c                    | 19 +++++------
 gcc/testsuite/gfortran.dg/contiguous_10.f90 | 49 +++++++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 9 deletions(-)

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index f2fe538a511..471540453dc 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -6177,37 +6177,38 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
 				fsym ? fsym->attr.intent : INTENT_INOUT,
 				fsym && fsym->attr.pointer);
 
 	      else if (gfc_is_class_array_ref (e, NULL)
-			 && fsym && fsym->ts.type == BT_DERIVED)
+		       && fsym && fsym->ts.type == BT_DERIVED)
 		/* The actual argument is a component reference to an
 		   array of derived types.  In this case, the argument
 		   is converted to a temporary, which is passed and then
 		   written back after the procedure call.
 		   OOP-TODO: Insert code so that if the dynamic type is
 		   the same as the declared type, copy-in/copy-out does
 		   not occur.  */
 		gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
-				fsym ? fsym->attr.intent : INTENT_INOUT,
-				fsym && fsym->attr.pointer);
+					   fsym->attr.intent,
+					   fsym->attr.pointer);
 
 	      else if (gfc_is_class_array_function (e)
-			 && fsym && fsym->ts.type == BT_DERIVED)
+		       && fsym && fsym->ts.type == BT_DERIVED)
 		/* See previous comment.  For function actual argument,
 		   the write out is not needed so the intent is set as
 		   intent in.  */
 		{
 		  e->must_finalize = 1;
 		  gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
-					     INTENT_IN,
-					     fsym && fsym->attr.pointer);
+					     INTENT_IN, fsym->attr.pointer);
 		}
 	      else if (fsym && fsym->attr.contiguous
 		       && !gfc_is_simply_contiguous (e, false, true))
 		{
-		  gfc_conv_subref_array_arg (&parmse, e, nodesc_arg,
-				fsym ? fsym->attr.intent : INTENT_INOUT,
-				fsym && fsym->attr.pointer);
+		  sym_intent intent = fsym->attr.intent;
+		  if (!gfc_expr_is_variable (e))
+		    intent = INTENT_IN;
+		  gfc_conv_subref_array_arg (&parmse, e, nodesc_arg, intent,
+					     fsym->attr.pointer);
 		}
 	      else
 		gfc_conv_array_parameter (&parmse, e, nodesc_arg, fsym,
 					  sym->name, NULL);
diff --git a/gcc/testsuite/gfortran.dg/contiguous_10.f90 b/gcc/testsuite/gfortran.dg/contiguous_10.f90
new file mode 100644
index 00000000000..46a497d6967
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/contiguous_10.f90
@@ -0,0 +1,49 @@
+! { dg-do run }
+!
+! PR fortran/91640
+!
+! Based on G. Steinmetz's test case
+!
+program p
+   implicit none (type, external)
+   real, target :: z(3) = 1.0
+   real :: res(3)
+
+   res = 42.0
+   call sub (-z, res)
+   if (any (abs (res - (-1.0)) > epsilon(res))) stop 1
+   if (any (abs (z - 1.0) > epsilon(z))) stop 2
+
+   res = 43.0
+   call sub (z*2.0, res)
+   if (any (abs (res - 2.0) > epsilon(res))) stop 3
+   if (any (abs (z - 1.0) > epsilon(z))) stop 4
+
+   res = 44.0
+   call sub(get_var(), res)
+   if (any (abs (res - 1.0) > epsilon(res))) stop 5
+   if (any (abs (z - 1.0) > epsilon(z))) stop 6
+
+   call double(get_var())
+   if (any (abs (z - 2.0) > epsilon(z))) stop 7
+
+   call double(get_var_cont())
+   if (any (abs (z - 4.0) > epsilon(z))) stop 8
+contains
+   subroutine sub (x, res)
+      real, contiguous :: x(:), res(:)
+      res = x
+   end
+   subroutine double (x)
+      real, contiguous :: x(:)
+      x = x * 2.0
+   end
+   function get_var()
+     real, pointer :: get_var(:)
+     get_var => z
+   end
+   function get_var_cont()
+     real, pointer, contiguous :: get_var_cont(:)
+     get_var_cont => z
+   end
+end

Reply via email to