* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
@ 2020-04-13 13:53 ` howisjw at hotmail dot com
2020-04-13 13:56 ` howisjw at hotmail dot com
` (10 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: howisjw at hotmail dot com @ 2020-04-13 13:53 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #1 from Jan-Willem Blokland <howisjw at hotmail dot com> ---
Below are the results of the test program. The results of array1d should have
been identical to array2d.
--- array2d ---
1 1 0.1000000E+01 0.1000000E+01 0.1000000E+01 0.1000000E+00
0.1000000E+00 0.1000000E+00 T
2 1 0.2000000E+01 0.2000000E+01 0.1000000E+01 0.2000000E+00
0.2000000E+00 0.1000000E+00 T
3 1 0.3000000E+01 0.3000000E+01 0.1000000E+01 0.3000000E+00
0.3000000E+00 0.1000000E+00 T
1 2 0.2000000E+01 0.1000000E+01 0.2000000E+01 0.2000000E+00
0.1000000E+00 0.2000000E+00 T
2 2 0.4000000E+01 0.2000000E+01 0.2000000E+01 0.4000000E+00
0.2000000E+00 0.2000000E+00 T
3 2 0.6000000E+01 0.3000000E+01 0.2000000E+01 0.6000000E+00
0.3000000E+00 0.2000000E+00 T
1 3 0.3000000E+01 0.1000000E+01 0.3000000E+01 0.3000000E+00
0.1000000E+00 0.3000000E+00 T
2 3 0.6000000E+01 0.2000000E+01 0.3000000E+01 0.6000000E+00
0.2000000E+00 0.3000000E+00 T
3 3 0.9000000E+01 0.3000000E+01 0.3000000E+01 0.9000000E+00
0.3000000E+00 0.3000000E+00 T
1 4 0.4000000E+01 0.1000000E+01 0.4000000E+01 0.4000000E+00
0.1000000E+00 0.4000000E+00 T
2 4 0.8000000E+01 0.2000000E+01 0.4000000E+01 0.8000000E+00
0.2000000E+00 0.4000000E+00 T
3 4 0.1200000E+02 0.3000000E+01 0.4000000E+01 0.1200000E+01
0.3000000E+00 0.4000000E+00 T
--- array1d ---
1 0.1000000E+01 0.1000000E+01 0.1000000E+01 0.1000000E+00
0.1000000E+00 0.1000000E+00 T
2 0.3000000E+01 0.3000000E+01 0.1000000E+01 0.3000000E+00
0.3000000E+00 0.1000000E+00 T
3 0.4000000E+01 0.2000000E+01 0.2000000E+01 0.4000000E+00
0.2000000E+00 0.2000000E+00 T
4 0.3000000E+01 0.1000000E+01 0.3000000E+01 0.3000000E+00
0.1000000E+00 0.3000000E+00 T
5 0.9000000E+01 0.3000000E+01 0.3000000E+01 0.9000000E+00
0.3000000E+00 0.3000000E+00 T
6 0.8000000E+01 0.2000000E+01 0.4000000E+01 0.8000000E+00
0.2000000E+00 0.4000000E+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.0000000E+00
0.1712892-316 0.2302373-314 T
9 0.4940656-323 0.0000000E+00 0.4940656-323 0.6930324-309
0.6930324-309 0.3102445-313 T
* 0.6930324-309 0.6930324-309 0.6930324-309 0.0000000E+00
0.4940656-323 0.6930324-309 T
* 0.0000000E+00 0.6930324-309 0.4940656-323 0.0000000E+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
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
2020-04-13 13:53 ` [Bug fortran/94578] " howisjw at hotmail dot com
@ 2020-04-13 13:56 ` howisjw at hotmail dot com
2020-04-14 15:05 ` tkoenig at gcc dot gnu.org
` (9 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: howisjw at hotmail dot com @ 2020-04-13 13:56 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #2 from Jan-Willem Blokland <howisjw at hotmail dot com> ---
Created attachment 48259
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=48259&action=edit
Program bug_reshape
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
2020-04-13 13:53 ` [Bug fortran/94578] " howisjw at hotmail dot com
2020-04-13 13:56 ` howisjw at hotmail dot com
@ 2020-04-14 15:05 ` tkoenig at gcc dot gnu.org
2020-04-14 15:08 ` tkoenig at gcc dot gnu.org
` (8 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2020-04-14 15:05 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
Thomas Koenig <tkoenig at gcc dot gnu.org> 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 <tkoenig at gcc dot gnu.org> ---
The span field is either not set correctly, or it is
ignored during the RESHAPE.
I'll look a little further...
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
` (2 preceding siblings ...)
2020-04-14 15:05 ` tkoenig at gcc dot gnu.org
@ 2020-04-14 15:08 ` tkoenig at gcc dot gnu.org
2020-04-16 14:21 ` tkoenig at gcc dot gnu.org
` (7 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2020-04-14 15:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #4 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
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.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
` (3 preceding siblings ...)
2020-04-14 15:08 ` tkoenig at gcc dot gnu.org
@ 2020-04-16 14:21 ` tkoenig at gcc dot gnu.org
2020-04-16 14:24 ` tkoenig at gcc dot gnu.org
` (6 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2020-04-16 14:21 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #5 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
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
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
` (4 preceding siblings ...)
2020-04-16 14:21 ` tkoenig at gcc dot gnu.org
@ 2020-04-16 14:24 ` tkoenig at gcc dot gnu.org
2020-04-17 7:02 ` howisjw at hotmail dot com
` (5 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2020-04-16 14:24 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #6 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
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
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
` (5 preceding siblings ...)
2020-04-16 14:24 ` tkoenig at gcc dot gnu.org
@ 2020-04-17 7:02 ` howisjw at hotmail dot com
2020-04-17 14:16 ` tkoenig at gcc dot gnu.org
` (4 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: howisjw at hotmail dot com @ 2020-04-17 7:02 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #7 from Jan-Willem Blokland <howisjw at hotmail dot com> ---
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
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
` (6 preceding siblings ...)
2020-04-17 7:02 ` howisjw at hotmail dot com
@ 2020-04-17 14:16 ` tkoenig at gcc dot gnu.org
2020-04-17 15:17 ` tkoenig at gcc dot gnu.org
` (3 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2020-04-17 14:16 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #8 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
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...
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
` (7 preceding siblings ...)
2020-04-17 14:16 ` tkoenig at gcc dot gnu.org
@ 2020-04-17 15:17 ` tkoenig at gcc dot gnu.org
2020-04-25 10:29 ` cvs-commit at gcc dot gnu.org
` (2 subsequent siblings)
11 siblings, 0 replies; 13+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2020-04-17 15:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #9 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
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;
}
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
` (8 preceding siblings ...)
2020-04-17 15:17 ` tkoenig at gcc dot gnu.org
@ 2020-04-25 10:29 ` cvs-commit at gcc dot gnu.org
2020-04-27 9:42 ` howisjw at hotmail dot com
2020-04-27 16:57 ` tkoenig at gcc dot gnu.org
11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-04-25 10:29 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #10 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Thomas Kथà¤nig <tkoenig@gcc.gnu.org>:
https://gcc.gnu.org/g:cf3f7b309ffdd888fdd85048ac9b4bcdc2713a45
commit r10-7960-gcf3f7b309ffdd888fdd85048ac9b4bcdc2713a45
Author: Thomas König <tkoenig@gcc.gnu.org>
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 <tkoenig@gcc.gnu.org>
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 <tkoenig@gcc.gnu.org>
PR fortran/94578
* gfortran.dg/pointer_assign_14.f90: New test.
* gfortran.dg/pointer_assign_15.f90: New test.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
` (9 preceding siblings ...)
2020-04-25 10:29 ` cvs-commit at gcc dot gnu.org
@ 2020-04-27 9:42 ` howisjw at hotmail dot com
2020-04-27 16:57 ` tkoenig at gcc dot gnu.org
11 siblings, 0 replies; 13+ messages in thread
From: howisjw at hotmail dot com @ 2020-04-27 9:42 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
--- Comment #11 from Jan-Willem Blokland <howisjw at hotmail dot com> ---
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.
^ permalink raw reply [flat|nested] 13+ messages in thread
* [Bug fortran/94578] Incorrect assignment of RESHAPE() result to a Fortran pointer
2020-04-13 13:46 [Bug fortran/94578] New: Incorrect assignment of RESHAPE() result to a Fortran pointer howisjw at hotmail dot com
` (10 preceding siblings ...)
2020-04-27 9:42 ` howisjw at hotmail dot com
@ 2020-04-27 16:57 ` tkoenig at gcc dot gnu.org
11 siblings, 0 replies; 13+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2020-04-27 16:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94578
Thomas Koenig <tkoenig at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
See Also| |https://gcc.gnu.org/bugzill
| |a/show_bug.cgi?id=93114
--- Comment #12 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
(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.
^ permalink raw reply [flat|nested] 13+ messages in thread