public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument.
@ 2022-03-07  3:12 a.shahmoradi at gmail dot com
  2022-03-07  3:57 ` [Bug fortran/104811] " kargl at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: a.shahmoradi at gmail dot com @ 2022-03-07  3:12 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

            Bug ID: 104811
           Summary: maxloc/minloc cannot accept character arguments
                    without `dim` optional argument.
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: a.shahmoradi at gmail dot com
  Target Milestone: ---

The following code appears to be standard-conforming. 

```
character(1) :: str(3)
str = ["a", "c", "a"]
print *, maxloc(str) ! requires dim = 1 argument to function.
print *, minloc(str) ! requires dim = 1 argument to function.
end
```

However, gfortran 11 or newer versions generate code that crashes with a
segfault error at runtime,

```
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

Backtrace for this error:
#0  0x7faba0a82642 in ???
#1  0x7faba0a81815 in ???
#2  0x7faba070020f in ???
#3  0x7faba0c70caa in ???
#4  0x40125b in MAIN__
        at /app/example.f90:3
#5  0x4010bc in main
        at /app/example.f90:5
```

The above code snippet can be readily tested here to reproduce the segfault:
https://godbolt.org/z/jGchG7zq3

If we add `dim = 1` to the maxloc/minloc calls, then the error is resolved. But
the dim argument is optional and should not be needed. Intel ifort compiles and
generates a correct answer with the above code.

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

* [Bug fortran/104811] maxloc/minloc cannot accept character arguments without `dim` optional argument.
  2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
@ 2022-03-07  3:57 ` kargl at gcc dot gnu.org
  2022-03-07 21:47 ` anlauf at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: kargl at gcc dot gnu.org @ 2022-03-07  3:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P4
                 CC|                            |kargl at gcc dot gnu.org,
                   |                            |tkoenig at netcologne dot de

--- Comment #1 from kargl at gcc dot gnu.org ---
Compiles and executes without optimization or if -fno-frontend-optimize is used
with optimization.

% gfcx -o z -fcheck=all -O3 a.f90
% ./z
Segmentation fault (core dumped)
% gfcx -o z -fcheck=all -O3 -fno-frontend-optimize a.f90
% ./z
           2
           1

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

* [Bug fortran/104811] maxloc/minloc cannot accept character arguments without `dim` optional argument.
  2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
  2022-03-07  3:57 ` [Bug fortran/104811] " kargl at gcc dot gnu.org
@ 2022-03-07 21:47 ` anlauf at gcc dot gnu.org
  2022-03-07 21:58 ` anlauf at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: anlauf at gcc dot gnu.org @ 2022-03-07 21:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
                 CC|                            |anlauf at gcc dot gnu.org
   Last reconfirmed|                            |2022-03-07

--- Comment #2 from anlauf at gcc dot gnu.org ---
(In reply to kargl from comment #1)
> Compiles and executes without optimization or if -fno-frontend-optimize is
> used with optimization.

Good observation.

The inline expansion that may be helpful for integer and real arguments
will not even be done in trans-intrinsic.cc, as the following comment
explains:

  /* Special case for character maxloc.  Remove unneeded actual
     arguments, then call a library function.  */

The obvious solution is to punt on character array arguments:

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;

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

* [Bug fortran/104811] maxloc/minloc cannot accept character arguments without `dim` optional argument.
  2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
  2022-03-07  3:57 ` [Bug fortran/104811] " kargl at gcc dot gnu.org
  2022-03-07 21:47 ` anlauf at gcc dot gnu.org
@ 2022-03-07 21:58 ` anlauf at gcc dot gnu.org
  2022-03-08 18:44 ` tkoenig at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: anlauf at gcc dot gnu.org @ 2022-03-07 21:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

--- Comment #3 from anlauf at gcc dot gnu.org ---
Note that gcc-8 to gcc-10 ICE on the testcase, and gcc-7 rejects character
array arguments (which is a F2003 feature).

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

* [Bug fortran/104811] maxloc/minloc cannot accept character arguments without `dim` optional argument.
  2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
                   ` (2 preceding siblings ...)
  2022-03-07 21:58 ` anlauf at gcc dot gnu.org
@ 2022-03-08 18:44 ` tkoenig at gcc dot gnu.org
  2022-03-08 18:50 ` a.shahmoradi at gmail dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: tkoenig at gcc dot gnu.org @ 2022-03-08 18:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

Thomas Koenig <tkoenig at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tkoenig at gcc dot gnu.org

--- Comment #4 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
(In reply to anlauf from comment #2)
> (In reply to kargl from comment #1)
> > Compiles and executes without optimization or if -fno-frontend-optimize is
> > used with optimization.
> 
> Good observation.
> 
> The inline expansion that may be helpful for integer and real arguments
> will not even be done in trans-intrinsic.cc, as the following comment
> explains:
> 
>   /* Special case for character maxloc.  Remove unneeded actual
>      arguments, then call a library function.  */
> 
> The obvious solution is to punt on character array arguments:
> 
> 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;

Thanks for analyzing this before I ever got a round tuit.

Patch is pre-approved or obvious, take your pick :-)

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

* [Bug fortran/104811] maxloc/minloc cannot accept character arguments without `dim` optional argument.
  2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
                   ` (3 preceding siblings ...)
  2022-03-08 18:44 ` tkoenig at gcc dot gnu.org
@ 2022-03-08 18:50 ` a.shahmoradi at gmail dot com
  2022-03-08 20:47 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: a.shahmoradi at gmail dot com @ 2022-03-08 18:50 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

--- Comment #5 from Amir Shahmoradi <a.shahmoradi at gmail dot com> ---
(In reply to Thomas Koenig from comment #4)
> (In reply to anlauf from comment #2)
> > (In reply to kargl from comment #1)
> > > Compiles and executes without optimization or if -fno-frontend-optimize is
> > > used with optimization.
> > 
> > Good observation.
> > 
> > The inline expansion that may be helpful for integer and real arguments
> > will not even be done in trans-intrinsic.cc, as the following comment
> > explains:
> > 
> >   /* Special case for character maxloc.  Remove unneeded actual
> >      arguments, then call a library function.  */
> > 
> > The obvious solution is to punt on character array arguments:
> > 
> > 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;
> 
> Thanks for analyzing this before I ever got a round tuit.
> 
> Patch is pre-approved or obvious, take your pick :-)

Thank you for your prompt action and fix. Glad to hear both "pre-approved" and
"obvious".

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

* [Bug fortran/104811] maxloc/minloc cannot accept character arguments without `dim` optional argument.
  2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
                   ` (4 preceding siblings ...)
  2022-03-08 18:50 ` a.shahmoradi at gmail dot com
@ 2022-03-08 20:47 ` cvs-commit at gcc dot gnu.org
  2022-03-13 20:44 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-03-08 20:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

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

commit r12-7545-ge3e369dad6cbecb1b490b3f3b154c600fba5a6f3
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Mar 8 21:47:04 2022 +0100

    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.

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

* [Bug fortran/104811] maxloc/minloc cannot accept character arguments without `dim` optional argument.
  2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
                   ` (5 preceding siblings ...)
  2022-03-08 20:47 ` cvs-commit at gcc dot gnu.org
@ 2022-03-13 20:44 ` cvs-commit at gcc dot gnu.org
  2022-03-13 20:46 ` anlauf at gcc dot gnu.org
  2022-03-13 20:47 ` anlauf at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-03-13 20:44 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Harald Anlauf
<anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:47f89da89eb0ddc9ed87192886ff633a790fe08c

commit r11-9652-g47f89da89eb0ddc9ed87192886ff633a790fe08c
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Mar 8 21:47:04 2022 +0100

    Fortran: do not frontend-optimize MINLOC/MAXLOC for character arrays

    gcc/fortran/ChangeLog:

            PR fortran/104811
            * frontend-passes.c (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.

    (cherry picked from commit e3e369dad6cbecb1b490b3f3b154c600fba5a6f3)

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

* [Bug fortran/104811] maxloc/minloc cannot accept character arguments without `dim` optional argument.
  2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
                   ` (6 preceding siblings ...)
  2022-03-13 20:44 ` cvs-commit at gcc dot gnu.org
@ 2022-03-13 20:46 ` anlauf at gcc dot gnu.org
  2022-03-13 20:47 ` anlauf at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: anlauf at gcc dot gnu.org @ 2022-03-13 20:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |11.3

--- Comment #8 from anlauf at gcc dot gnu.org ---
Fixed on mainline for gcc-12, and backported to 11-branch.  Closing.

Thanks for the report!

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

* [Bug fortran/104811] maxloc/minloc cannot accept character arguments without `dim` optional argument.
  2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
                   ` (7 preceding siblings ...)
  2022-03-13 20:46 ` anlauf at gcc dot gnu.org
@ 2022-03-13 20:47 ` anlauf at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: anlauf at gcc dot gnu.org @ 2022-03-13 20:47 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104811

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |FIXED

--- Comment #9 from anlauf at gcc dot gnu.org ---
Really closing.

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

end of thread, other threads:[~2022-03-13 20:47 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-07  3:12 [Bug fortran/104811] New: maxloc/minloc cannot accept character arguments without `dim` optional argument a.shahmoradi at gmail dot com
2022-03-07  3:57 ` [Bug fortran/104811] " kargl at gcc dot gnu.org
2022-03-07 21:47 ` anlauf at gcc dot gnu.org
2022-03-07 21:58 ` anlauf at gcc dot gnu.org
2022-03-08 18:44 ` tkoenig at gcc dot gnu.org
2022-03-08 18:50 ` a.shahmoradi at gmail dot com
2022-03-08 20:47 ` cvs-commit at gcc dot gnu.org
2022-03-13 20:44 ` cvs-commit at gcc dot gnu.org
2022-03-13 20:46 ` anlauf at gcc dot gnu.org
2022-03-13 20:47 ` anlauf at gcc dot gnu.org

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