https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120140
--- Comment #9 from Mikael Morin <mikael at gcc dot gnu.org> ---
(In reply to anlauf from comment #7)
> Refined testcase that shows that scalarization somehow fails for the
> assignment:
>
> module mod
> implicit none
> type t1
> integer :: i = 0
> end type t1
>
> interface assignment(=)
> module procedure sub
> end interface assignment(=)
> contains
> elemental subroutine sub(x, y)
> type(t1), intent(inout) :: x
> type(t1), intent(in) :: y
> x%i = y%i + 42
> end subroutine sub
> end module mod
>
> program test
> use mod !, only: t1, assignment(=)
> implicit none
> integer :: j
> type(t1) :: p(4)
> type(t1) :: q(4) = [(t1(j),j=1,4)]
> integer :: operm(2) = [2,1] ! "Output permutation vector"
> integer :: iperm(2) = [1,2] ! "Input permutation vector"
> p%i = -1
> !--
> ! The following two lines give identical -fdump-fortran-original,
> ! but very different -fdump-tree-original:
> ! call sub (p(operm), q(iperm)) ! works (properly scalarized)
> p(operm) = q(iperm) ! fails (not correctly scalarized)
> !--
> print *, 'p(1:2)%i =', p(1:2)%i, ' (expect 44 43)'
> end program test
>
>
> When processing the output argument (p(operm)), in gfc_conv_expr_reference
> I get in the working case:
>
> 10798 if (expr->expr_type == EXPR_VARIABLE)
> 10799 {
> 10800 se->want_pointer = 1;
> 10801 gfc_conv_expr (se, expr);
> 10802 if (se->post.head)
> (gdb) p se->ss->info->data.array->ref
> $109 = (gfc_ref *) 0x3a02440
>
> and in the other case:
>
> (gdb) p se->ss->info->data.array->ref
> $110 = (gfc_ref *) 0x0
>
> Need an expert's help here.
The difference comes from the temporary generation.
In the direct call case, there is no temporary and the array ref is set.
In the assignment case, there is a temporary and the array ref is cleared.
I think this is working as expected.
The question is why there is a difference in temporary generation.
It comes from gfc_trans_call's dependency_check argument, which translates to a
different 'check_variable' argument to gfc_conv_elemental_dependencies, which
is passed as 'elemental' argument to gfc_check_fncall_dependency and then
gfc_check_argument_dependency and then gfc_check_argument_var_dependency.
The following seems to fix it, and passes fortran tests *elem* and *dep*.
diff --git a/gcc/fortran/dependency.cc b/gcc/fortran/dependency.cc
index 71b0433058f..02de810dac0 100644
--- a/gcc/fortran/dependency.cc
+++ b/gcc/fortran/dependency.cc
@@ -997,7 +997,7 @@ gfc_check_argument_var_dependency (gfc_expr *var,
sym_intent intent,
case EXPR_VARIABLE:
/* In case of elemental subroutines, there is no dependency
between two same-range array references. */
- if (gfc_ref_needs_temporary_p (expr->ref)
+ if ((elemental == NOT_ELEMENTAL && gfc_ref_needs_temporary_p
(expr->ref))
|| gfc_check_dependency (var, expr, elemental == NOT_ELEMENTAL))
{
if (elemental == ELEM_DONT_CHECK_VARIABLE)