public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PR fortran/96711 - ICE on NINT() Function
@ 2020-08-24 21:08 Harald Anlauf
  2020-09-03 18:42 ` *Ping*: " Harald Anlauf
  0 siblings, 1 reply; 4+ messages in thread
From: Harald Anlauf @ 2020-08-24 21:08 UTC (permalink / raw)
  To: fortran, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 957 bytes --]

When rounding a real to integer(16) an ICE happened due to an unhandled
case in build_round_expr.  The attached patch add a special case for this.

I had to change a fold_convert to a convert (which seems to be frontend
specific stuff), otherwise I would get errors from the generated GIMPLE.
Does anybody know if this could produce problems elsewhere?
I'm open to other / better solutions.

Anyway, the patch does regtest cleanly on x86_64-pc-linux-gnu.

OK for master?

Thanks,
Harald


PR fortran/96711 - ICE with NINT() for integer(16) result

When rounding a real to the nearest integer, temporarily convert the real
argument to a longer real kind when the result is of type/kind integer(16).

gcc/fortran/ChangeLog:

	* trans-intrinsic.c (build_round_expr): Use temporary with
	appropriate kind for conversion before rounding to nearest
	integer when the result precision is 128 bits.

gcc/testsuite/ChangeLog:

	* gfortran.dg/pr96711.f90: New test.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr96711.patch --]
[-- Type: text/x-patch, Size: 1867 bytes --]

diff --git a/gcc/fortran/trans-intrinsic.c b/gcc/fortran/trans-intrinsic.c
index 2483f016d8e..32fe9886c57 100644
--- a/gcc/fortran/trans-intrinsic.c
+++ b/gcc/fortran/trans-intrinsic.c
@@ -395,11 +395,24 @@ build_round_expr (tree arg, tree restype)
     fn = builtin_decl_for_precision (BUILT_IN_LROUND, argprec);
   else if (resprec <= LONG_LONG_TYPE_SIZE)
     fn = builtin_decl_for_precision (BUILT_IN_LLROUND, argprec);
+  else if (resprec >= argprec && resprec == 128)
+    {
+      /* Search for a real kind suitable as temporary for conversion.  */
+      int kind = -1;
+      for (int i = 0; kind < 0 && gfc_real_kinds[i].kind != 0; i++)
+	if (gfc_real_kinds[i].mode_precision >= resprec)
+	  kind = gfc_real_kinds[i].kind;
+      if (kind < 0)
+	gfc_internal_error ("Could not find real kind with at least %d bits",
+			    resprec);
+      arg = fold_convert (gfc_float128_type_node, arg);
+      fn = gfc_builtin_decl_for_float_kind (BUILT_IN_ROUND, kind);
+    }
   else
     gcc_unreachable ();

-  return fold_convert (restype, build_call_expr_loc (input_location,
-						 fn, 1, arg));
+  return convert (restype, build_call_expr_loc (input_location,
+						fn, 1, arg));
 }


diff --git a/gcc/testsuite/gfortran.dg/pr96711.f90 b/gcc/testsuite/gfortran.dg/pr96711.f90
new file mode 100644
index 00000000000..c6534713b52
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr96711.f90
@@ -0,0 +1,18 @@
+! { dg-do compile }
+! { dg-require-effective-target fortran_integer_16 }
+! { dg-require-effective-target fortran_real_16 }
+!
+! PR fortran/96711 - ICE on NINT() Function
+
+program p
+  implicit none
+  integer(16) :: m
+  real(8)     :: x
+  real(16)    :: y
+  x = 2 / epsilon (x)
+  y = 2 / epsilon (y)
+  m = nint (x, kind(m))
+  print *, m
+  m = nint (y, kind(m))
+  print *, m
+end program

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

* *Ping*: [PATCH] PR fortran/96711 - ICE on NINT() Function
  2020-08-24 21:08 [PATCH] PR fortran/96711 - ICE on NINT() Function Harald Anlauf
@ 2020-09-03 18:42 ` Harald Anlauf
  2020-09-07  5:09   ` Thomas Koenig
  0 siblings, 1 reply; 4+ messages in thread
From: Harald Anlauf @ 2020-09-03 18:42 UTC (permalink / raw)
  To: Harald Anlauf; +Cc: fortran, gcc-patches

*ping*

> Gesendet: Montag, 24. August 2020 um 23:08 Uhr
> Von: "Harald Anlauf" <anlauf@gmx.de>
> An: "fortran" <fortran@gcc.gnu.org>, "gcc-patches" <gcc-patches@gcc.gnu.org>
> Betreff: [PATCH] PR fortran/96711 - ICE on NINT() Function
>
> When rounding a real to integer(16) an ICE happened due to an unhandled
> case in build_round_expr.  The attached patch add a special case for this.
>
> I had to change a fold_convert to a convert (which seems to be frontend
> specific stuff), otherwise I would get errors from the generated GIMPLE.
> Does anybody know if this could produce problems elsewhere?
> I'm open to other / better solutions.
>
> Anyway, the patch does regtest cleanly on x86_64-pc-linux-gnu.
>
> OK for master?
>
> Thanks,
> Harald
>
>
> PR fortran/96711 - ICE with NINT() for integer(16) result
>
> When rounding a real to the nearest integer, temporarily convert the real
> argument to a longer real kind when the result is of type/kind integer(16).
>
> gcc/fortran/ChangeLog:
>
> 	* trans-intrinsic.c (build_round_expr): Use temporary with
> 	appropriate kind for conversion before rounding to nearest
> 	integer when the result precision is 128 bits.
>
> gcc/testsuite/ChangeLog:
>
> 	* gfortran.dg/pr96711.f90: New test.
>
>

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

* Re: *Ping*: [PATCH] PR fortran/96711 - ICE on NINT() Function
  2020-09-03 18:42 ` *Ping*: " Harald Anlauf
@ 2020-09-07  5:09   ` Thomas Koenig
  2020-09-07 19:46     ` Harald Anlauf
  0 siblings, 1 reply; 4+ messages in thread
From: Thomas Koenig @ 2020-09-07  5:09 UTC (permalink / raw)
  To: Harald Anlauf; +Cc: gcc-patches, fortran

Hi Harald,

> *ping*

I don't really know about the convert vs. fold_convert issue either,
but if it works, it works.

Regarding the patch: Could you change the test caes into a
run-time test and check for the results both for compile-time
simplification and evaluation at run-time?  Just to make sure that
fold_convert wasn't trying to tell us something that convert doesn't...

OK with that change.

Thanks for the patch!

Best regards

	Thomas

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

* Re: *Ping*: [PATCH] PR fortran/96711 - ICE on NINT() Function
  2020-09-07  5:09   ` Thomas Koenig
@ 2020-09-07 19:46     ` Harald Anlauf
  0 siblings, 0 replies; 4+ messages in thread
From: Harald Anlauf @ 2020-09-07 19:46 UTC (permalink / raw)
  To: Thomas Koenig; +Cc: gcc-patches, fortran

Hi Thomas,

thanks for the review!

> Regarding the patch: Could you change the test caes into a
> run-time test and check for the results both for compile-time
> simplification and evaluation at run-time?  Just to make sure that
> fold_convert wasn't trying to tell us something that convert doesn't...
>
> OK with that change.

Here's the test I committed:


! { dg-do run }
! { dg-require-effective-target fortran_integer_16 }
! { dg-require-effective-target fortran_real_16 }
! { dg-additional-options "-fdump-tree-original" }
! { dg-final { scan-tree-dump-times "_gfortran_stop_numeric" 2 "original" } }
!
! PR fortran/96711 - ICE on NINT() Function

program p
  implicit none
  real(8)                :: x
  real(16)               :: y
  integer(16), parameter :: k1 = nint (2 / epsilon (x), kind(k1))
  integer(16), parameter :: k2 = nint (2 / epsilon (y), kind(k2))
  integer(16), parameter :: m1 = 9007199254740992_16                    !2**53
  integer(16), parameter :: m2 = 10384593717069655257060992658440192_16 !2**113
  integer(16), volatile  :: m
  x = 2 / epsilon (x)
  y = 2 / epsilon (y)
  m = nint (x, kind(m))
! print *, m
  if (k1 /= m1) stop 1
  if (m  /= m1) stop 2
  m = nint (y, kind(m))
! print *, m
  if (k2 /= m2) stop 3
  if (m  /= m2) stop 4
end program


Harald


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

end of thread, other threads:[~2020-09-07 19:46 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-24 21:08 [PATCH] PR fortran/96711 - ICE on NINT() Function Harald Anlauf
2020-09-03 18:42 ` *Ping*: " Harald Anlauf
2020-09-07  5:09   ` Thomas Koenig
2020-09-07 19:46     ` Harald Anlauf

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).