[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 Thomas Koenig changed: What|Removed |Added See Also||https://gcc.gnu.org/bugzill ||a/show_bug.cgi?id=93114 --- Comment #12 from Thomas Koenig --- (In reply to Jan-Willem Blokland from comment #11) > If you make use of an temporary variable, it sounds like you will do an > additional memory copy. Therefore, I am wondering what the performance > impact will be. Naively, I would think the span solution would be faster. You are quite correct, but an optimized fix will take far more time than is available until a release candidate for gcc 10 comes out and all development is frozen. I'd rather have correct code on gcc 10. I will revisit this later as part of PR 93114.
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #11 from Jan-Willem Blokland --- If you make use of an temporary variable, it sounds like you will do an additional memory copy. Therefore, I am wondering what the performance impact will be. Naively, I would think the span solution would be faster. I must admit, I do realize I say this all without knowing the current implementation. So you should read my question/comment as fix the issue and later worry about making the fix as fast as possible. From my personal experience, making it fast is easily said than done. I am already happy that you looking into this issue and try to solve it.
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #10 from CVS Commits --- The master branch has been updated by Thomas Kथà¤nig : https://gcc.gnu.org/g:cf3f7b309ffdd888fdd85048ac9b4bcdc2713a45 commit r10-7960-gcf3f7b309ffdd888fdd85048ac9b4bcdc2713a45 Author: Thomas König Date: Sat Apr 25 12:28:15 2020 +0200 Fix PR 94578. Our intrinsics do not handle spans on their return values (yet), so this creates a temporary for subref array pointers. 2020-04-25 Thomas Koenig PR fortran/94578 * trans-expr.c (arrayfunc_assign_needs_temporary): If the LHS is a subref pointer, we also need a temporary. 2020-04-25 Thomas Koenig PR fortran/94578 * gfortran.dg/pointer_assign_14.f90: New test. * gfortran.dg/pointer_assign_15.f90: New test.
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #9 from Thomas Koenig --- Here's what a solution could look like. I am not really sure that this is the way to go, there may be some corner cases (pointer to an argument which was passed as a transposed argument?) which this might get wrong. diff --git a/libgfortran/generated/maxval_i4.c b/libgfortran/generated/maxval_i4.c index abad45b50ae..3a9ed436956 100644 --- a/libgfortran/generated/maxval_i4.c +++ b/libgfortran/generated/maxval_i4.c @@ -50,6 +50,7 @@ maxval_i4 (gfc_array_i4 * const restrict retarray, index_type delta; index_type dim; int continue_loop; + int ret_factor; /* Make dim zero based to avoid confusion. */ rank = GFC_DESCRIPTOR_RANK (array) - 1; @@ -112,6 +113,7 @@ maxval_i4 (gfc_array_i4 * const restrict retarray, return; } + ret_factor = 1; } else { @@ -124,12 +126,16 @@ maxval_i4 (gfc_array_i4 * const restrict retarray, if (unlikely (compile_options.bounds_check)) bounds_ifunction_return ((array_t *) retarray, extent, "return value", "MAXVAL"); + if (retarray->span != 0) + ret_factor = retarray->span / sizeof(GFC_INTEGER_4); + else + ret_factor = 1; } for (n = 0; n < rank; n++) { count[n] = 0; - dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n); + dstride[n] = GFC_DESCRIPTOR_STRIDE(retarray,n) * ret_factor; if (extent[n] <= 0) return; }
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #8 from Thomas Koenig --- The bug appears to affect intrinsics only, for example this program main implicit none type foo integer :: x, y end type foo integer, dimension(:), pointer :: bp type (foo), dimension(4), target :: b data b%x /1,2,3,4/ data b%y /-1,-2,-3,-4/ bp => b%x bp = x() print *,bp contains function x() integer, dimension(4) :: x x = [11,12,13,14] end function x end program main works as expected (and creates an array temporary). Let's see what we can do here, if this should be solved on the library side or if we should just insert a temporary array here...
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #7 from Jan-Willem Blokland --- Thanks for verifying my case. Your test makes me wonder which intrinsics are effected in this and other versions of gfortran. Furthermore, I assume it also happens when you pass the pointer to subroutine or function (see example below). This would be more difficult to debug. Fortunately, for my case the pointer assignment and the usage of the intrinsic where in the same function/subroutine. program main implicit none type foo integer :: x, y end type foo integer :: i integer, dimension (2,2) :: array2d integer, dimension(:), pointer :: array1d type(foo), dimension(2*2), target :: solution data array2d /1,3,2,4/ array1d => solution%x array1d = reshape (source=array2d, shape=shape(array1d)) print *,maxval(array2d) call printMaxval(array1d) end program main subroutine printMaxval(array1d) implicit none integer, dimension(:), intent(in) :: array1d print*,maxval(array1d) end subroutine
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #6 from Thomas Koenig --- Also wrong: program main implicit none type foo integer :: x, y end type foo integer :: i integer, dimension (2,2) :: array2d integer, dimension(:), pointer :: array1d type(foo), dimension(2*2), target :: solution data array2d /1,3,2,4/ array1d => solution%x array1d = reshape (source=array2d, shape=shape(array1d)) print *,maxval(array2d) print *,maxval(array1d) end program main prints 4 2
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #5 from Thomas Koenig --- Somewhat smaller test case: program main implicit none type foo integer :: x, y end type foo integer :: i integer, dimension (2,2) :: array2d integer, dimension(:), pointer :: array1d type(foo), dimension(2*2), target :: solution data array2d /1,2,3,4/ array1d => solution%x print *,array2d array1d = reshape (source=array2d, shape=shape(array1d)) print *,array1d end program main
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #4 from Thomas Koenig --- Looks like span is not handled in reshape (at all). It will be interesting to see how other intrinsics such as maxloc and just about everything else handles this.
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 Thomas Koenig changed: What|Removed |Added Status|UNCONFIRMED |NEW Ever confirmed|0 |1 CC||pault at gcc dot gnu.org Last reconfirmed||2020-04-14 --- Comment #3 from Thomas Koenig --- The span field is either not set correctly, or it is ignored during the RESHAPE. I'll look a little further...
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #2 from Jan-Willem Blokland --- Created attachment 48259 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48259&action=edit Program bug_reshape
[Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578 --- Comment #1 from Jan-Willem Blokland --- Below are the results of the test program. The results of array1d should have been identical to array2d. --- array2d --- 1 1 0.100E+01 0.100E+01 0.100E+01 0.100E+00 0.100E+00 0.100E+00 T 2 1 0.200E+01 0.200E+01 0.100E+01 0.200E+00 0.200E+00 0.100E+00 T 3 1 0.300E+01 0.300E+01 0.100E+01 0.300E+00 0.300E+00 0.100E+00 T 1 2 0.200E+01 0.100E+01 0.200E+01 0.200E+00 0.100E+00 0.200E+00 T 2 2 0.400E+01 0.200E+01 0.200E+01 0.400E+00 0.200E+00 0.200E+00 T 3 2 0.600E+01 0.300E+01 0.200E+01 0.600E+00 0.300E+00 0.200E+00 T 1 3 0.300E+01 0.100E+01 0.300E+01 0.300E+00 0.100E+00 0.300E+00 T 2 3 0.600E+01 0.200E+01 0.300E+01 0.600E+00 0.200E+00 0.300E+00 T 3 3 0.900E+01 0.300E+01 0.300E+01 0.900E+00 0.300E+00 0.300E+00 T 1 4 0.400E+01 0.100E+01 0.400E+01 0.400E+00 0.100E+00 0.400E+00 T 2 4 0.800E+01 0.200E+01 0.400E+01 0.800E+00 0.200E+00 0.400E+00 T 3 4 0.120E+02 0.300E+01 0.400E+01 0.120E+01 0.300E+00 0.400E+00 T --- array1d --- 1 0.100E+01 0.100E+01 0.100E+01 0.100E+00 0.100E+00 0.100E+00 T 2 0.300E+01 0.300E+01 0.100E+01 0.300E+00 0.300E+00 0.100E+00 T 3 0.400E+01 0.200E+01 0.200E+01 0.400E+00 0.200E+00 0.200E+00 T 4 0.300E+01 0.100E+01 0.300E+01 0.300E+00 0.100E+00 0.300E+00 T 5 0.900E+01 0.300E+01 0.300E+01 0.900E+00 0.300E+00 0.300E+00 T 6 0.800E+01 0.200E+01 0.400E+01 0.800E+00 0.200E+00 0.400E+00 T 7 0.6930324-309 0.6953253-309 0.6953253-309 0.6930324-309 0.6930226-309 0.6930324-309 T 8 0.6930324-309 0.3952525-322 0.1096251-314 0.000E+00 0.1712892-316 0.2302373-314 T 9 0.4940656-323 0.000E+00 0.4940656-323 0.6930324-309 0.6930324-309 0.3102445-313 T * 0.6930324-309 0.6930324-309 0.6930324-309 0.000E+00 0.4940656-323 0.6930324-309 T * 0.000E+00 0.6930324-309 0.4940656-323 0.000E+00 0.4646021-309 0.6930324-309 T * 0.6930324-309 0.6930324-309 0.4940656-323 0.3575029-316 0.2964394-322 0.4171992-308 T