https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98913
Bug ID: 98913 Summary: Create-temporary difference coarray/noncoarray – invalid code due to missing temporary Product: gcc Version: 11.0 Status: UNCONFIRMED Keywords: wrong-code Severity: normal Priority: P3 Component: fortran Assignee: unassigned at gcc dot gnu.org Reporter: burnus at gcc dot gnu.org CC: jdelia at intec dot unl.edu.ar, tkoenig at gcc dot gnu.org Target Milestone: --- Reported at https://gcc.gnu.org/pipermail/fortran/2021-January/055657.html The testcase there uses real (kind=idp), allocatable :: AA (:,:)[:] real (kind=idp), allocatable :: BB (:,:) For identical assignment, a temporary is generated for the (this_image()-access) coarray variable but not for the local variable: 37 | AA (k1:nn,j) = AA (k1:nn,j) - UU (k1:nn) * AA (k,j) | 1 Warning: Creating array temporary at (1) [-Warray-temporaries] On the compiler side, I think the behaviour differs due to: gfc_conv_resolve_dependencies(): if (flag_realloc_lhs && dest_expr != ss_expr && gfc_is_reallocatable_lhs (dest_expr) && ss_expr->rank) nDepend = gfc_check_dependency (dest_expr, ss_expr, true); * * * Looking at do j = 1, nn AA (k1:nn,j) = AA (k1:nn,j) - UU (k1:nn) * AA (k,j) end do I think a temporary is needed for "A(k,j)", i.e. do j = 1, nn tmp = AA (k,j) do ii = k1, nn AA (ii,j) = AA (ii,j) - UU (ii) * tmp end do end do without that temporary, k might be any value within k1:nn! In this example, k = 1; k1 = 2 – thus, there is no dependency. * * * For (b), I see (for "BB(k,j)"): D.3991 = (*(real(kind=8)[0:] * restrict) bb.data)[(bb.offset + (integer(kind=8)) j * bb.dim[1].stride) + (integer(kind=8)) k]; ... D.3986 = (real(kind=8)[0:] * restrict) bb.data; D.3978 = (real(kind=8)[0:] * restrict) bb.data; while (1) ... (*D.3986)[(S.0 + D.3994) + D.3997] = (*D.3978)[S.0 + D.3996] - (*D.3982)[(S.0 + D.3993) + D.3983] * D.3991; Namely: b(k,j) is accessed. I am quite certain that "k = 1; k1 = k + 1" is not used for this analysis → wrong code. If it is, it should also apply to coarrays.