public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gmx.de>
To: fortran <fortran@gcc.gnu.org>, gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] Fortran: fix simplification of intrinsics IBCLR and IBSET [PR106557]
Date: Sat, 20 Aug 2022 21:11:20 +0200	[thread overview]
Message-ID: <trinity-8cf16654-0cf6-472a-ab09-919261efb696-1661022680044@3c-app-gmx-bs01> (raw)

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

Dear all,

the simplification of the TRANSFER intrinsic produces a
redundant representation of the result, one in expr->value
and another in expr->representation.string.  This is done
to ensure a safe "round-trip" for nested TRANSFER.

In a subsequent use of this result we either need to make
sure that both parts stay consistent, or drop the latter
part.

The simplifications of IBCLR and IBSET do a gfc_copy_expr
of their argument x and modify only the former part.
Depending on context, either value.integer or
representation.string (if non-NULL) are used later,
leading to surprising results.

A conservative approach to fix this PR is to simply
drop the unneeded representation.string in the
simplification of the intrinsics IBCLR and IBSET,
see attached patch.  (A quick glance did not turn up
other intrinsics affected the same way.)

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Fortran-fix-simplification-of-intrinsics-IBCLR-and-I.patch --]
[-- Type: text/x-patch, Size: 2849 bytes --]

From a540a806cf02d739a408f129738252e73f03e60c Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Sat, 20 Aug 2022 20:36:28 +0200
Subject: [PATCH] Fortran: fix simplification of intrinsics IBCLR and IBSET
 [PR106557]

gcc/fortran/ChangeLog:

	PR fortran/106557
	* simplify.cc (gfc_simplify_ibclr): Ensure consistent results of
	the simplification by dropping a redundant memory representation
	of argument x.
	(gfc_simplify_ibset): Likewise.

gcc/testsuite/ChangeLog:

	PR fortran/106557
	* gfortran.dg/pr106557.f90: New test.
---
 gcc/fortran/simplify.cc                | 14 ++++++++++++++
 gcc/testsuite/gfortran.dg/pr106557.f90 | 19 +++++++++++++++++++
 2 files changed, 33 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/pr106557.f90

diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc
index fb725994653..f992c31e5d7 100644
--- a/gcc/fortran/simplify.cc
+++ b/gcc/fortran/simplify.cc
@@ -3380,6 +3380,13 @@ gfc_simplify_ibclr (gfc_expr *x, gfc_expr *y)
   k = gfc_validate_kind (x->ts.type, x->ts.kind, false);

   result = gfc_copy_expr (x);
+  /* Drop any separate memory representation of x to avoid potential
+     inconsistencies in result.  */
+  if (result->representation.string)
+    {
+      free (result->representation.string);
+      result->representation.string = NULL;
+    }

   convert_mpz_to_unsigned (result->value.integer,
 			   gfc_integer_kinds[k].bit_size);
@@ -3471,6 +3478,13 @@ gfc_simplify_ibset (gfc_expr *x, gfc_expr *y)
   k = gfc_validate_kind (x->ts.type, x->ts.kind, false);

   result = gfc_copy_expr (x);
+  /* Drop any separate memory representation of x to avoid potential
+     inconsistencies in result.  */
+  if (result->representation.string)
+    {
+      free (result->representation.string);
+      result->representation.string = NULL;
+    }

   convert_mpz_to_unsigned (result->value.integer,
 			   gfc_integer_kinds[k].bit_size);
diff --git a/gcc/testsuite/gfortran.dg/pr106557.f90 b/gcc/testsuite/gfortran.dg/pr106557.f90
new file mode 100644
index 00000000000..d073f3e7186
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pr106557.f90
@@ -0,0 +1,19 @@
+! { dg-do run }
+! { dg-additional-options "-fdump-tree-original" }
+! PR fortran/106557 - nesting intrinsics ibset and transfer gives wrong result
+
+program p
+  implicit none
+  character(1) :: s
+
+  write(s,'(i1)') ibset (transfer (0, 0), 0)
+  if (s /= '1') stop 1
+  write(s,'(i1)') ibclr (transfer (1, 0), 0)
+  if (s /= '0') stop 2
+
+  ! These shall be fully resolved at compile time:
+  if (transfer   (ibset (transfer (0, 0), 0), 0) /= 1) stop 3
+  if (transfer   (ibclr (transfer (1, 0), 0), 0) /= 0) stop 4
+end
+
+! { dg-final { scan-tree-dump-times "_gfortran_stop_numeric" 2 "original" } }
--
2.35.3


             reply	other threads:[~2022-08-20 19:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-20 19:11 Harald Anlauf [this message]
2022-08-22  9:52 ` Mikael Morin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=trinity-8cf16654-0cf6-472a-ab09-919261efb696-1661022680044@3c-app-gmx-bs01 \
    --to=anlauf@gmx.de \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).