public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>, fortran <fortran@gcc.gnu.org>
Subject: [Patch] Fortran: Fix non_negative_strides_array_p
Date: Wed, 19 Oct 2022 13:27:22 +0200	[thread overview]
Message-ID: <c5888ab6-564e-33ad-452b-f69e52c66b31@codesourcery.com> (raw)

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

First, I am woefully aware that there several patches pending. I hope to do a
couple of reviews later today or in the next days.

Otherwise, I did run into another issue in existing code which was exposed by
the delinearization patch on the OG12 branch, but could potentially lead to
wrong code on mainline as well, depending on how the return value is used.
Albeit I did fail to create a testcase for it.

OK for mainline?

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

[-- Attachment #2: fix-non-neg-stride.diff --]
[-- Type: text/x-patch, Size: 2269 bytes --]

Fortran: Fix non_negative_strides_array_p

The non_negative_strides_array_p function might return wrongly 'true', e.g.
for assumed-shape arrays, if the argument is '*a.0 ...' instead of 'a.0 ...'
as then the saved array descriptor for the PARAM_DECL 'a' is not found.

This potentially leads to wrong code - but I could not find a testcase
leading to wrong code on mainline. Asserts show that this happens with
CLASS; however, for those no ARRAY_REF seems to get used.

The issue show up when applying the delinearization patch as posted
at https://gcc.gnu.org/pipermail/gcc-patches/2020-December/562230.html
that has been applied to the OG12 alias devel/omp/gcc-12 vendor branch, as
commit 39a8c371fda6136cf77c74895a00b136409e0ba3. This patch calls
gfc_build_array_ref inside gfc_conv_array_ref. The issue mentioned
above show up with this patch in gfortran.dg/array_reference_3.f90,
a testcase added together with non_negative_strides_array_p in commit
r12-8230-g7964ab6c364 for PR 102043. Here, non_negative_strides_array_p
returns true for assumed_shape_x but assumed shape arrays may have
negative strides.

gcc/fortran/ChangeLog:

	* trans-array.cc (non_negative_strides_array_p): Fix handling
	of GFC_DECL_SAVED_DESCRIPTOR.

 gcc/fortran/trans-array.cc | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 795ce14af08..ca3503b7cae 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -3695,11 +3695,16 @@ non_negative_strides_array_p (tree expr)
 
   /* If the array was originally a dummy with a descriptor, strides can be
      negative.  */
-  if (DECL_P (expr)
-      && DECL_LANG_SPECIFIC (expr)
-      && GFC_DECL_SAVED_DESCRIPTOR (expr)
-      && GFC_DECL_SAVED_DESCRIPTOR (expr) != expr)
-    return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (expr));
+  tree decl = expr;
+  STRIP_NOPS (decl);
+  if (TREE_CODE (decl) == INDIRECT_REF)
+    decl = TREE_OPERAND (decl, 0);
+
+  if (DECL_P (decl)
+      && DECL_LANG_SPECIFIC (decl)
+      && GFC_DECL_SAVED_DESCRIPTOR (decl)
+      && GFC_DECL_SAVED_DESCRIPTOR (decl) != expr)
+    return non_negative_strides_array_p (GFC_DECL_SAVED_DESCRIPTOR (decl));
 
   return true;
 }

                 reply	other threads:[~2022-10-19 11:27 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=c5888ab6-564e-33ad-452b-f69e52c66b31@codesourcery.com \
    --to=tobias@codesourcery.com \
    --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).