[Patch, committed] Fortran: Fix 'fn spec' for deferred character length (was: Re: [r12-4457 Regression] FAIL: gfortran.dg/deferred_type_param_6.f90 -Os execution test on Linux/x86_64)

2021-10-19 Thread Tobias Burnus

On 16.10.21 20:54, Jan Hubicka wrote:

I wrote:

Fortran has for a long time 'character(len=5), allocatable" or
"character(len=*)". In the first case, the "5" can be ignored as both
caller and callee know the length. In the second case, the length is
determined by the argument, but it cannot be changed.

Since a not-that-short while, 'len=:' together with allocatable/pointer
is supported.

In the latter case, the value can be change when the array
association/allocation is changed.
...
+  if (!sym->ts.u.cl->length
+  && ((sym->attr.allocatable && sym->attr.target)
+  || sym->attr.pointer))
+spec[spec_len++] = '.';
+  if (!sym->ts.u.cl->length && sym->attr.allocatable)
+spec[spec_len++] = 'w';
+  else
+spec[spec_len++] = 'R';

Also escaping is quite important bit of information so it would be
good to figure out if it really can escape rather than playing safe.


The pointer to the string length variable itself does not escape,
only its integer string value:

subroutine foo(x)
  character(len=:), pointer :: x
  character(len=:), pointer :: y
  y => x
has in the dump:
  .y = *_x;
  y = (character(kind=1)[1:.y] *) *x;

Thus, 'w' can always be used.

Committed as obvious as r12-4511-gff0eec94e87dfb7dc387f120ca5ade2707aecf50

Tobias
-
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 
München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas 
Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht 
München, HRB 106955
commit ff0eec94e87dfb7dc387f120ca5ade2707aecf50
Author: Tobias Burnus 
Date:   Tue Oct 19 16:38:56 2021 +0200

Fortran: Fix 'fn spec' for deferred character length

Shows now up with gfortran.dg/deferred_type_param_6.f90 due to more ME
optimizations, causing fails without this commit.

gcc/fortran/ChangeLog:

* trans-types.c (create_fn_spec): For allocatable/pointer
character(len=:), use 'w' not 'R' as fn spec for the length dummy
argument.
---
 gcc/fortran/trans-types.c | 6 +-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/trans-types.c b/gcc/fortran/trans-types.c
index 50fceebc941..42778067dbe 100644
--- a/gcc/fortran/trans-types.c
+++ b/gcc/fortran/trans-types.c
@@ -3014,7 +3014,11 @@ create_fn_spec (gfc_symbol *sym, tree fntype)
 	}
   if (sym->ts.type == BT_CHARACTER)
 	{
-	  spec[spec_len++] = 'R';
+	  if (!sym->ts.u.cl->length
+	  && (sym->attr.allocatable || sym->attr.pointer))
+	spec[spec_len++] = 'w';
+	  else
+	spec[spec_len++] = 'R';
 	  spec[spec_len++] = ' ';
 	}
 }


Re: [r12-4457 Regression] FAIL: gfortran.dg/deferred_type_param_6.f90 -Os execution test on Linux/x86_64

2021-10-18 Thread Richard Biener via Gcc-patches
On Sat, Oct 16, 2021 at 8:24 PM Jan Hubicka via Gcc-patches
 wrote:
>
> Hi,
> >
> > FAIL: gfortran.dg/deferred_type_param_6.f90   -O1  execution test
> > FAIL: gfortran.dg/deferred_type_param_6.f90   -Os  execution test
> Sorry for the breakage.  This time it seems like bug in Fortran FE
> which was previously latent:
>
> __attribute__((fn spec (". . R ")))
> void subfunc (character(kind=1)[1:..__result] * & __result, integer(kind=8) * 
> .__result)
> {
>   # PT = nonlocal
>   character(kind=1)[1:..__result] * & __result_3(D) = __result;
>   # PT = nonlocal null
>   integer(kind=8) * .__result_5(D) = .__result;
>   integer(kind=4) _1;
>
>[local count: 1073741824]:
>   *__result_3(D) = 
>   # USE = nonlocal escaped { D.4230 } (nonlocal, escaped)
>   _1 = _gfortran_compare_string (5, , 5, &"FIVEC"[1]{lb: 1 sz: 1});
>   if (_1 != 0)
> goto ; [0.04%]
>   else
> goto ; [99.96%]
>
>[local count: 429496]:
>   # USE = nonlocal escaped null
>   # CLB = nonlocal escaped null
>   _gfortran_stop_numeric (10, 0);
>
>[local count: 1073312329]:
>   *.__result_5(D) = 5;
>   return;
> }
>
> The fnspec ". . R " specifies that .__result is readonly however we
> have:
>   *.__result_5(D) = 5;
>
> I am not sure I understand fortran FE well enough to figure out why
> it is set so.  The function is declared as:
>
>   function subfunc() result(res)
> character(len=:), pointer :: res
> res => fifec
> if (len(res) /= 5) STOP 9
> if (res /= "FIVEC") STOP 10
>   end function subfunc
>
> and we indeed optimize load of the result:
> -  # USE = nonlocal escaped { D.4252 D.4254 } (nonlocal, escaped)
> -  # CLB = nonlocal escaped { D.4254 } (escaped)
> +  # USE = nonlocal escaped
> +  # CLB = { D.4254 }
>subfunc (, );
> -  .s2_34 = slen.4;
> -  # PT = nonlocal escaped null { D.4254 } (escaped)
> -  s2_35 = pstr.5;
>pstr.5 ={v} {CLOBBER};
>
> and I think tat is what breaks the testcase (I also verified that
> ignoring the fnspec 'R' fixes it).

The FE code adding the fnspec probably fails to consider the
separately passed length of the string result?

Can you open a bugreport please?

Richard.

> Honza
> >
> > with GCC configured with
> >
> > ../../gcc/configure 
> > --prefix=/local/skpandey/gccwork/toolwork/gcc-bisect-master/master/r12-4457/usr
> >  --enable-clocale=gnu --with-system-zlib --with-demangler-in-ld 
> > --with-fpmath=sse --enable-languages=c,c++,fortran --enable-cet 
> > --without-isl --enable-libmpx x86_64-linux --disable-bootstrap
> >
> > To reproduce:
> >
> > $ cd {build_dir}/gcc && make check 
> > RUNTESTFLAGS="dg.exp=gfortran.dg/deferred_type_param_6.f90 
> > --target_board='unix{-m32}'"
> > $ cd {build_dir}/gcc && make check 
> > RUNTESTFLAGS="dg.exp=gfortran.dg/deferred_type_param_6.f90 
> > --target_board='unix{-m32\ -march=cascadelake}'"
> > $ cd {build_dir}/gcc && make check 
> > RUNTESTFLAGS="dg.exp=gfortran.dg/deferred_type_param_6.f90 
> > --target_board='unix{-m64}'"
> > $ cd {build_dir}/gcc && make check 
> > RUNTESTFLAGS="dg.exp=gfortran.dg/deferred_type_param_6.f90 
> > --target_board='unix{-m64\ -march=cascadelake}'"
> >
> > (Please do not reply to this email, for question about this report, contact 
> > me at skpgkp2 at gmail dot com)


Re: [Patch] (was: Re: [r12-4457 Regression] FAIL: gfortran.dg/deferred_type_param_6.f90 -Os execution test on Linux/x86_64)

2021-10-16 Thread Jan Hubicka via Gcc-patches
> 
> Fortran has for a long time 'character(len=5), allocatable" or
> "character(len=*)". In the first case, the "5" can be ignored as both
> caller and callee know the length. In the second case, the length is
> determined by the argument, but it cannot be changed.
> 
> Since a not-that-short while, 'len=:' together with allocatable/pointer
> is supported.
> 
> In the latter case, the value can be change when the array
> association/allocation is changed.
> 
> I attached a patch, which was not tested. I am not quite sure whether
> the pointer address can actually escape or not - I think cannot but I
> played safe.
> 
> Tobias
> -
> Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 
> München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas 
> Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht 
> München, HRB 106955

> Fortran: Fix fn spec for character-returning functions
> 
> gcc/fortran/ChangeLog
>   * trans-types.c (create_fn_spec): For character-returning functions,
>   set the hidden string-length argument to 'R' only when the "len=:",
>   i.e. deferred length which goes alongside with allocatable/pointer.
> 
> diff --git a/gcc/fortran/trans-types.c b/gcc/fortran/trans-types.c
> index 220976babb8..637d2c71d01 100644
> --- a/gcc/fortran/trans-types.c
> +++ b/gcc/fortran/trans-types.c
> @@ -3008,7 +3008,14 @@ create_fn_spec (gfc_symbol *sym, tree fntype)
>   }
>if (sym->ts.type == BT_CHARACTER)
>   {
> -   spec[spec_len++] = 'R';
> +   if (!sym->ts.u.cl->length
> +   && ((sym->attr.allocatable && sym->attr.target)
> +   || sym->attr.pointer))
> + spec[spec_len++] = '.';
> +   if (!sym->ts.u.cl->length && sym->attr.allocatable)
> + spec[spec_len++] = 'w';
> +   else
> + spec[spec_len++] = 'R';
> spec[spec_len++] = ' ';

Thanks a lot! I was just looking into that function and was quite
confused on what is going there. Are you going to commit the patch?
Also escaping is quite important bit of information so it would be
good to figure out if it really can escape rather than playing safe.

Honza
>   }
>  }



[Patch] (was: Re: [r12-4457 Regression] FAIL: gfortran.dg/deferred_type_param_6.f90 -Os execution test on Linux/x86_64)

2021-10-16 Thread Tobias Burnus

Hi Honza,

On 16.10.21 20:23, Jan Hubicka via Gcc-patches wrote:

FAIL: gfortran.dg/deferred_type_param_6.f90   -O1  execution test
FAIL: gfortran.dg/deferred_type_param_6.f90   -Os  execution test

Sorry for the breakage.  This time it seems like bug in Fortran FE
which was previously latent:

__attribute__((fn spec (". . R ")))
void subfunc (character(kind=1)[1:..__result] * & __result, integer(kind=8) * 
.__result)


...

Fortran has for a long time 'character(len=5), allocatable" or
"character(len=*)". In the first case, the "5" can be ignored as both
caller and callee know the length. In the second case, the length is
determined by the argument, but it cannot be changed.

Since a not-that-short while, 'len=:' together with allocatable/pointer
is supported.

In the latter case, the value can be change when the array
association/allocation is changed.

I attached a patch, which was not tested. I am not quite sure whether
the pointer address can actually escape or not - I think cannot but I
played safe.

Tobias
-
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 
München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas 
Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht 
München, HRB 106955
Fortran: Fix fn spec for character-returning functions

gcc/fortran/ChangeLog
	* trans-types.c (create_fn_spec): For character-returning functions,
	set the hidden string-length argument to 'R' only when the "len=:",
	i.e. deferred length which goes alongside with allocatable/pointer.

diff --git a/gcc/fortran/trans-types.c b/gcc/fortran/trans-types.c
index 220976babb8..637d2c71d01 100644
--- a/gcc/fortran/trans-types.c
+++ b/gcc/fortran/trans-types.c
@@ -3008,7 +3008,14 @@ create_fn_spec (gfc_symbol *sym, tree fntype)
 	}
   if (sym->ts.type == BT_CHARACTER)
 	{
-	  spec[spec_len++] = 'R';
+	  if (!sym->ts.u.cl->length
+	  && ((sym->attr.allocatable && sym->attr.target)
+		  || sym->attr.pointer))
+	spec[spec_len++] = '.';
+	  if (!sym->ts.u.cl->length && sym->attr.allocatable)
+	spec[spec_len++] = 'w';
+	  else
+	spec[spec_len++] = 'R';
 	  spec[spec_len++] = ' ';
 	}
 }


Re: [r12-4457 Regression] FAIL: gfortran.dg/deferred_type_param_6.f90 -Os execution test on Linux/x86_64

2021-10-16 Thread Jan Hubicka via Gcc-patches
Hi,
> 
> FAIL: gfortran.dg/deferred_type_param_6.f90   -O1  execution test
> FAIL: gfortran.dg/deferred_type_param_6.f90   -Os  execution test
Sorry for the breakage.  This time it seems like bug in Fortran FE
which was previously latent:

__attribute__((fn spec (". . R ")))
void subfunc (character(kind=1)[1:..__result] * & __result, integer(kind=8) * 
.__result)
{
  # PT = nonlocal 
  character(kind=1)[1:..__result] * & __result_3(D) = __result;
  # PT = nonlocal null 
  integer(kind=8) * .__result_5(D) = .__result;
  integer(kind=4) _1;

   [local count: 1073741824]:
  *__result_3(D) = 
  # USE = nonlocal escaped { D.4230 } (nonlocal, escaped)
  _1 = _gfortran_compare_string (5, , 5, &"FIVEC"[1]{lb: 1 sz: 1});
  if (_1 != 0)
goto ; [0.04%]
  else
goto ; [99.96%]

   [local count: 429496]:
  # USE = nonlocal escaped null 
  # CLB = nonlocal escaped null 
  _gfortran_stop_numeric (10, 0);

   [local count: 1073312329]:
  *.__result_5(D) = 5;
  return;
}

The fnspec ". . R " specifies that .__result is readonly however we
have:
  *.__result_5(D) = 5;

I am not sure I understand fortran FE well enough to figure out why
it is set so.  The function is declared as:

  function subfunc() result(res)
character(len=:), pointer :: res
res => fifec
if (len(res) /= 5) STOP 9
if (res /= "FIVEC") STOP 10
  end function subfunc

and we indeed optimize load of the result:
-  # USE = nonlocal escaped { D.4252 D.4254 } (nonlocal, escaped)
-  # CLB = nonlocal escaped { D.4254 } (escaped)
+  # USE = nonlocal escaped 
+  # CLB = { D.4254 }
   subfunc (, );
-  .s2_34 = slen.4;
-  # PT = nonlocal escaped null { D.4254 } (escaped)
-  s2_35 = pstr.5;
   pstr.5 ={v} {CLOBBER};

and I think tat is what breaks the testcase (I also verified that
ignoring the fnspec 'R' fixes it).

Honza
> 
> with GCC configured with
> 
> ../../gcc/configure 
> --prefix=/local/skpandey/gccwork/toolwork/gcc-bisect-master/master/r12-4457/usr
>  --enable-clocale=gnu --with-system-zlib --with-demangler-in-ld 
> --with-fpmath=sse --enable-languages=c,c++,fortran --enable-cet --without-isl 
> --enable-libmpx x86_64-linux --disable-bootstrap
> 
> To reproduce:
> 
> $ cd {build_dir}/gcc && make check 
> RUNTESTFLAGS="dg.exp=gfortran.dg/deferred_type_param_6.f90 
> --target_board='unix{-m32}'"
> $ cd {build_dir}/gcc && make check 
> RUNTESTFLAGS="dg.exp=gfortran.dg/deferred_type_param_6.f90 
> --target_board='unix{-m32\ -march=cascadelake}'"
> $ cd {build_dir}/gcc && make check 
> RUNTESTFLAGS="dg.exp=gfortran.dg/deferred_type_param_6.f90 
> --target_board='unix{-m64}'"
> $ cd {build_dir}/gcc && make check 
> RUNTESTFLAGS="dg.exp=gfortran.dg/deferred_type_param_6.f90 
> --target_board='unix{-m64\ -march=cascadelake}'"
> 
> (Please do not reply to this email, for question about this report, contact 
> me at skpgkp2 at gmail dot com)