[committed] PR fortran/104811 - maxloc/minloc cannot accept character arguments without `dim` optional argument

Message ID trinity-2ee686f7-212c-49e0-9993-06dcbdffef8d-1646773143173@3c-app-gmx-bap36
State Committed
Headers
Series [committed] PR fortran/104811 - maxloc/minloc cannot accept character arguments without `dim` optional argument |

Commit Message

Harald Anlauf March 8, 2022, 8:59 p.m. UTC
  Dear all,

frontend-optimization of MINLOC/MAXLOC tries to generate code for rank-1
arrays that may be expanded inline later and optimzed.  Except when the
argument is a character array...

As there is even a comment in trans-intrinsic.cc that we will call a
library function for character arguments anyway, we better punt here.
The attached obvious patch does this and was pre-approved by Thomas in
the PR.

Regtested on x86_64-pc-linux-gnu and pushed to mainline as

https://gcc.gnu.org/g:e3e369dad6cbecb1b490b3f3b154c600fba5a6f3

As this is a wrong-code issue, I'd like to backport this to 11-branch.

Thanks,
Harald
  

Patch

From e3e369dad6cbecb1b490b3f3b154c600fba5a6f3 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 8 Mar 2022 21:47:04 +0100
Subject: [PATCH] Fortran: do not frontend-optimize MINLOC/MAXLOC for character
 arrays

gcc/fortran/ChangeLog:

	PR fortran/104811
	* frontend-passes.cc (optimize_minmaxloc): Do not attempt
	frontend-optimization of MINLOC/MAXLOC for character arrays, as
	there is no suitable code yet for inline expansion.

gcc/testsuite/ChangeLog:

	PR fortran/104811
	* gfortran.dg/minmaxloc_16.f90: New test.
---
 gcc/fortran/frontend-passes.cc             |  1 +
 gcc/testsuite/gfortran.dg/minmaxloc_16.f90 | 14 ++++++++++++++
 2 files changed, 15 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/minmaxloc_16.f90

diff --git a/gcc/fortran/frontend-passes.cc b/gcc/fortran/frontend-passes.cc
index 4033f27df99..5eba6345145 100644
--- a/gcc/fortran/frontend-passes.cc
+++ b/gcc/fortran/frontend-passes.cc
@@ -2276,6 +2276,7 @@  optimize_minmaxloc (gfc_expr **e)
   if (fn->rank != 1
       || fn->value.function.actual == NULL
       || fn->value.function.actual->expr == NULL
+      || fn->value.function.actual->expr->ts.type == BT_CHARACTER
       || fn->value.function.actual->expr->rank != 1)
     return;

diff --git a/gcc/testsuite/gfortran.dg/minmaxloc_16.f90 b/gcc/testsuite/gfortran.dg/minmaxloc_16.f90
new file mode 100644
index 00000000000..099248df2e3
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/minmaxloc_16.f90
@@ -0,0 +1,14 @@ 
+! { dg-do run }
+! { dg-options "-fdump-tree-original" }
+! PR fortran/104811
+! Frontend-optimization mis-optimized minloc/maxloc of character arrays
+
+program p
+  character(1) :: str(3)
+  str = ["a", "c", "a"]
+  if (any (maxloc (str) /= 2)) stop 1
+  if (minloc (str,dim=1) /= 1) stop 2
+end
+
+! { dg-final { scan-tree-dump-times "_gfortran_maxloc0_4_s1" 1 "original" } }
+! { dg-final { scan-tree-dump-times "_gfortran_minloc2_4_s1" 1 "original" } }
--
2.34.1