public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* Odd bug from Stack Overflow
@ 2018-03-18 15:40 Steve Kargl
  2018-03-18 16:03 ` Steve Kargl
  2018-03-18 17:12 ` Thomas König
  0 siblings, 2 replies; 4+ messages in thread
From: Steve Kargl @ 2018-03-18 15:40 UTC (permalink / raw)
  To: fortran

So, who knows how the scalarizer works?

This code

program test
   implicit none
   integer, parameter :: n = 65536
   integer, dimension(n) :: y
   integer*4 :: i
   y = (/ (1, i=1, n) /)
   if (y(2) /= 1) stop 1
end program test

generates a loops that does the right thing.

    offset.1 = 0;
    shadow_loopvar.2 = 1;
    while (1)
      {
        if (shadow_loopvar.2 > 65536) goto L.1;
        (*(integer(kind=4)[65536] * restrict) atmp.0.data)[offset.1] = 1;
        offset.1 = offset.1 + 1;
        shadow_loopvar.2 = shadow_loopvar.2 + 1;
      }
    L.1:;

Clearly, 1 is being assigned to each element of the array 'y'.  Now
change the type of 'y' to real.

    offset.1 = 0;
    (*(real(kind=4)[65536] * restrict) atmp.0.data)[offset.1] = 1.0e+0;
    offset.1 = offset.1 + 1;
    {
      integer(kind=4) S.2;

      S.2 = 0;
      while (1)
        {
          if (S.2 > 65535) goto L.1;
          y[S.2] = (*(real(kind=4)[65536] * restrict) atmp.0.data)[S.2];
          S.2 = S.2 + 1;
        }
      L.1:;
    }

The value of 1.0 is assigned to the first element of atmp, a
temporary array.  Then the loop assigns the values from
temporary array 'atmp'.  The problem is atmp(2:65536) have
never been set.  I was expecting the -fdump-tree-original for
the integer and real codes to look substantially the same.
Something has gone sideways.  Anyone have a good guess where?

-- 
Steve

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Odd bug from Stack Overflow
  2018-03-18 15:40 Odd bug from Stack Overflow Steve Kargl
@ 2018-03-18 16:03 ` Steve Kargl
  2018-03-18 17:12 ` Thomas König
  1 sibling, 0 replies; 4+ messages in thread
From: Steve Kargl @ 2018-03-18 16:03 UTC (permalink / raw)
  To: fortran

On Sun, Mar 18, 2018 at 08:39:58AM -0700, Steve Kargl wrote:
> So, who knows how the scalarizer works?
> 
> This code
> 
> program test
>    implicit none
>    integer, parameter :: n = 65536
>    integer, dimension(n) :: y
>    integer*4 :: i
>    y = (/ (1, i=1, n) /)
>    if (y(2) /= 1) stop 1
> end program test
> 
> generates a loops that does the right thing.
> 
>     offset.1 = 0;
>     shadow_loopvar.2 = 1;
>     while (1)
>       {
>         if (shadow_loopvar.2 > 65536) goto L.1;
>         (*(integer(kind=4)[65536] * restrict) atmp.0.data)[offset.1] = 1;
>         offset.1 = offset.1 + 1;
>         shadow_loopvar.2 = shadow_loopvar.2 + 1;
>       }
>     L.1:;

Whoops left out the copying of atmp to y.

    {
      integer(kind=4) S.3;

      S.3 = 0;
      while (1)
        {
          if (S.3 > 65535) goto L.2;
          y[S.3] = (*(integer(kind=4)[65536] * restrict) atmp.0.data)[S.3];
          S.3 = S.3 + 1;
        }
      L.2:;
    }

> 
> Clearly, 1 is being assigned to each element of the array 'y'.  Now
> change the type of 'y' to real.
> 
>     offset.1 = 0;
>     (*(real(kind=4)[65536] * restrict) atmp.0.data)[offset.1] = 1.0e+0;
>     offset.1 = offset.1 + 1;
>     {
>       integer(kind=4) S.2;
> 
>       S.2 = 0;
>       while (1)
>         {
>           if (S.2 > 65535) goto L.1;
>           y[S.2] = (*(real(kind=4)[65536] * restrict) atmp.0.data)[S.2];
>           S.2 = S.2 + 1;
>         }
>       L.1:;
>     }
> 
> The value of 1.0 is assigned to the first element of atmp, a
> temporary array.  Then the loop assigns the values from
> temporary array 'atmp'.  The problem is atmp(2:65536) have
> never been set.  I was expecting the -fdump-tree-original for
> the integer and real codes to look substantially the same.
> Something has gone sideways.  Anyone have a good guess where?
> 
> -- 
> Steve

-- 
Steve
20170425 https://www.youtube.com/watch?v=VWUpyCsUKR4
20161221 https://www.youtube.com/watch?v=IbCHE-hONow

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Odd bug from Stack Overflow
  2018-03-18 15:40 Odd bug from Stack Overflow Steve Kargl
  2018-03-18 16:03 ` Steve Kargl
@ 2018-03-18 17:12 ` Thomas König
  2018-03-18 17:19   ` Steve Kargl
  1 sibling, 1 reply; 4+ messages in thread
From: Thomas König @ 2018-03-18 17:12 UTC (permalink / raw)
  To: sgk, fortran

Hi Steve,

This looks like a missing conversion somewhere,
because this works:

program test
    implicit none
    integer, parameter :: n = 65536
    real, dimension(n) :: y
    integer :: i
    y = (/ (1.0, i=1, n) /)
    print *,y(2)
    if (y(2) /= 1) stop 1
end program test

Also, n=65536 is a limit... setting n=2**16-1 actually
works as expected with the orginal test case.

This looks like a resolution problem, not something
in the scalarizer. Can you open a PR?

Regards

	Thomas

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Odd bug from Stack Overflow
  2018-03-18 17:12 ` Thomas König
@ 2018-03-18 17:19   ` Steve Kargl
  0 siblings, 0 replies; 4+ messages in thread
From: Steve Kargl @ 2018-03-18 17:19 UTC (permalink / raw)
  To: Thomas König; +Cc: fortran

On Sun, Mar 18, 2018 at 06:12:23PM +0100, Thomas König wrote:
> Hi Steve,
> 
> This looks like a missing conversion somewhere,
> because this works:
> 
> program test
>     implicit none
>     integer, parameter :: n = 65536
>     real, dimension(n) :: y
>     integer :: i
>     y = (/ (1.0, i=1, n) /)
>     print *,y(2)
>     if (y(2) /= 1) stop 1
> end program test
> 
> Also, n=65536 is a limit... setting n=2**16-1 actually
> works as expected with the orginal test case.

Yes, n=2**16-1 works because gfortran switches over from
expanding the array constructor into a static temporary
variable to scalarizing the implied-do-loop at 2**16.  It
is a trade-off we made a long time ago for

integer, parameter :: i(somebignumber) = [(i,i=1,somebignumer)]

See -fmax-array-constructor= option.

> This looks like a resolution problem, not something
> in the scalarizer. Can you open a PR?

Yes.

-- 
Steve

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2018-03-18 17:19 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-18 15:40 Odd bug from Stack Overflow Steve Kargl
2018-03-18 16:03 ` Steve Kargl
2018-03-18 17:12 ` Thomas König
2018-03-18 17:19   ` Steve Kargl

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).