Bug 45689 - [F03] Missing transformational intrinsic in the trans_func_f2003 list
Summary: [F03] Missing transformational intrinsic in the trans_func_f2003 list
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Thomas Koenig
URL:
Keywords:
: 25104 29962 (view as bug list)
Depends on: 47988 83316 83650
Blocks: F2003 30932 31392
  Show dependency treegraph
 
Reported: 2010-09-16 10:40 UTC by Dominique d'Humieres
Modified: 2018-01-04 21:06 UTC (History)
8 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-06-16 00:00:00


Attachments
Patch which extends minval/maxval (937 bytes, patch)
2017-12-10 12:08 UTC, Thomas Koenig
Details | Diff
Patch which works for minloc(...,dim=1) for rank-one arrays (1.98 KB, patch)
2017-12-28 14:08 UTC, Thomas Koenig
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Dominique d'Humieres 2010-09-16 10:40:58 UTC
While playing with modifications of PR4581, I tried

  module m
    implicit none
    type t
      integer :: i
    end type t
    type(t), dimension(2), parameter :: a1  = (/ t(1), t(2) /)
    type(t), dimension(2), parameter :: d = cshift ( a1, 1 )
  end module m

and got

pr45081_red.f90:7.43:

    type(t), dimension(2), parameter :: d = cshift ( a1, 1 )
                                           1
Error: transformational intrinsic 'cshift' at (1) is not permitted in an initialization expression

The F2003 standard says:

(5) A reference to a transformational standard intrinsic function other than NULL, where each argument is an initialization expression,

and the F2008 version adds:

(6) a reference to a transformational standard intrinsic function other than COMMAND ARGUMENT -COUNT, NULL, NUM IMAGES, THIS IMAGE, where each argument is a constant expression,

This is fixed by the following patch:


--- ../_clean/gcc/fortran/expr.c	2010-09-09 21:06:18.000000000 +0200
+++ gcc/fortran/expr.c	2010-09-16 12:24:49.000000000 +0200
@@ -2329,7 +2329,7 @@ check_transformational (gfc_expr *e)
   };
 
   static const char * const trans_func_f2003[] =  {
-    "all", "any", "count", "dot_product", "matmul", "null", "pack",
+    "all", "any", "count", "cshift", "dot_product", "eoshift", "matmul", "null", "pack",
     "product", "repeat", "reshape", "selected_char_kind", "selected_int_kind",
     "selected_real_kind", "spread", "sum", "transfer", "transpose",
     "trim", "unpack", NULL

but then I am back to PR45081!-(even with the Paul's patch).
Comment 1 Dominique d'Humieres 2010-09-16 10:42:02 UTC
pasto!-(
Comment 2 Daniel Franke 2010-09-16 11:14:11 UTC
They are not, as there, afaik, are no simplifiers yet.

Hence, with your patch they will be accepted, but you'd end up with wrong code at the end, as the functions are not properly simplified and thus not constant.
Comment 3 Dominique d'Humieres 2010-09-16 13:09:38 UTC
MAXLOC and MINLOC are also missing (see pr25104).

> They are not, as there, afaik, are no simplifiers yet.
>
> Hence, with your patch they will be accepted, but you'd end up with wrong code
> at the end, as the functions are not properly simplified and thus not constant.

I have seen that, and also in gcc/fortran/simplify.c

    /* FIXME: Returning here avoids a regression in array_simplify_1.f90.
       Replace NULL with gcc_unreachable() after implementing
       gfc_simplify_cshift(). */

Am I correct to understand that the current situation (i.e. the error message) is a temporary fix for some missing gfc_simplify_*?
Comment 4 Daniel Franke 2010-09-18 15:58:30 UTC
(In reply to comment #3)
> Am I correct to understand that the current situation (i.e. the error message)
> is a temporary fix for some missing gfc_simplify_*?

If the error message you refer to is

> Error: transformational intrinsic 'cshift' at (1) is not permitted in an
> initialization expression

then yes.
Comment 5 Dominique d'Humieres 2013-06-16 12:06:54 UTC
Related or duplicate of PR52473.
Comment 6 Dominique d'Humieres 2013-06-25 17:16:29 UTC
From http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29962#c20

Transformational intrinsics, done are:
 * all, any, count
 * product, sum
 * dot_product, matmul, transpose
 * pack, unpack, spread

Left:
 * maxloc, minloc
 * maxval, minval (generic case)
 * cshift, eoshift

While at it, see also PR29600 (kind arguments) and PR36313 (character type).

Earlier this year, I was working on the cshift/eoshift, but I strongly believe that the linear list that builds the constructor must be replaced with splay trees before this can be implemented somewhat efficiently.

I think what is left in pr29962 is a duplicate of this one.
Comment 7 Dominique d'Humieres 2013-06-25 17:17:13 UTC
*** Bug 29962 has been marked as a duplicate of this bug. ***
Comment 8 Dominique d'Humieres 2013-06-25 17:27:45 UTC
*** Bug 25104 has been marked as a duplicate of this bug. ***
Comment 9 Dominique d'Humieres 2015-11-21 20:18:57 UTC
Partial fix done at revision r230709.
Comment 10 Thomas Koenig 2017-12-04 08:16:03 UTC
Still to be done: Simplification for maxloc/minloc/maxval/minval
for character arguments.

Apart from that, I think we are done (but I will check before
closing :-)
Comment 11 Dominique d'Humieres 2017-12-04 09:20:46 UTC
> Still to be done: Simplification for maxloc/minloc/maxval/minval
> for character arguments.
>
> Apart from that, I think we are done (but I will check before
> closing :-)

From https://gcc.gnu.org/ml/fortran/2015-11/msg00120.html I see

> The attached patch provides a partial implementation for
> the simplification for CSHIFT.  It is partial in that it
> only applies to rank 1 arrays.  For arrays with rank > 1,
> gfc_simplify_cshift will issue an error.  Here, the intent
> is that hopefully someone that knows what they are doing
> with supply a patch for rank > 1.

Is it still true?
Comment 12 Thomas Koenig 2017-12-09 11:28:17 UTC
Still a lot of cases missing. None of the following are
simplified:

program main
  integer, dimension(4), parameter :: b = [1,2,3,4]
  integer, dimension(4), parameter :: beo = eoshift(b,1)
  integer, dimension(2,2), parameter :: a = reshape([1,2,3,4],shape(a))
  integer, dimension(2,2), parameter :: eo2 = eoshift(a,shift=1,dim=1)
  integer, dimension(2,2), parameter :: cs2 = cshift(a,shift=1,dim=1)
  integer, dimension(2), parameter :: miv = minval(a,dim=1)
  integer, dimension(2), parameter :: mav = maxval(a,dim=1)
  integer, dimension(2), parameter :: mil = minloc(a,dim=1)
  integer, dimension(2), parameter :: mal = maxloc(a,dim=1)
end program main
Comment 13 Thomas Koenig 2017-12-10 12:08:09 UTC
Created attachment 42822 [details]
Patch which extends minval/maxval

This looks promising - it just removes the special handling of maxval
(which is incomplete), replacing it with the general implementation.
Comment 14 Thomas Koenig 2017-12-11 17:39:39 UTC
Author: tkoenig
Date: Mon Dec 11 17:39:07 2017
New Revision: 255542

URL: https://gcc.gnu.org/viewcvs?rev=255542&root=gcc&view=rev
Log:
2017-12-11  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/45689
	* simplify.c (min_max_choose): Add prototype.
	(gfc_count): Format correctly.
	(simplify_transformation): Pass array argument to init_result_expr.
	(gfc_simplify_minval_maxval): Remove.
	(gfc_min): New function.
	(gfc_simplify_minval): Call simplify_transformation.
	(gfc_max): New function.
	(gfc_simplify_maxval): Call simplify_transformation.

2017-12-11  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/45689
	* gfortran.dg/minval_parameter_1.f90: New test.
	* gfortran.dg/maxval_parameter_1.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/maxval_parameter_1.f90
    trunk/gcc/testsuite/gfortran.dg/minval_parameter_1.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog
Comment 15 Thomas Koenig 2017-12-28 14:08:31 UTC
Created attachment 42976 [details]
Patch which works for minloc(...,dim=1) for rank-one arrays

This is a proof-of-concept patch for minloc which works for one of the
three cases that need to be looked at, i.e.

minloc(rank_one_array,dim=1)

Still missing are

minloc(arbitray_array)

and

minloc(arbitrary_array,dim=xx)
Comment 16 Thomas Koenig 2018-01-02 17:51:58 UTC
Author: tkoenig
Date: Tue Jan  2 17:51:26 2018
New Revision: 256084

URL: https://gcc.gnu.org/viewcvs?rev=256084&root=gcc&view=rev
Log:
2018-01-02  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/45689
	PR fortran/83650
	* simplify.c (gfc_simplify_cshift): Re-implement to allow full
	range of arguments.

2018-01-02  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/45689
	PR fortran/83650
	* gfortran.dg/simplify_cshift_1.f90: Correct erroneous case.
	* gfortran.dg/simplify_cshift_4.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/simplify_cshift_4.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/simplify_cshift_1.f90
Comment 17 Thomas Koenig 2018-01-02 18:14:36 UTC
Author: tkoenig
Date: Tue Jan  2 18:14:04 2018
New Revision: 256088

URL: https://gcc.gnu.org/viewcvs?rev=256088&root=gcc&view=rev
Log:
2017-01-02  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/45689
	* intrinsic.c (add_function): Add gfc_simplify_maxloc and
	gfc_simplify_minloc to maxloc and minloc, respectively.
	* intrinsic.h: Add prototypes for gfc_simplify_minloc
	and gfc_simplify_maxloc.
	* simplify.c (min_max_chose): Adjust prototype.  Modify function
	to have a return value which indicates if the extremum was found.
	(is_constant_array_expr): Fix typo in comment.
	(simplify_minmaxloc_to_scalar): New function.
	(simplify_minmaxloc_nodim): New function.
	(new_array): New function.
	(simplify_minmaxloc_to_array): New function.
	(gfc_simplify_minmaxloc): New function.
	(simplify_minloc): New function.
	(simplify_maxloc): New function.

2017-01-02  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/45689
	* gfortran.dg/minloc_4.f90: New test case.
	* gfortran.dg/maxloc_4.f90: New test case.


Added:
    trunk/gcc/testsuite/gfortran.dg/maxloc_4.f90
    trunk/gcc/testsuite/gfortran.dg/minloc_4.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/intrinsic.c
    trunk/gcc/fortran/intrinsic.h
    trunk/gcc/fortran/simplify.c
Comment 18 Thomas Koenig 2018-01-04 21:04:55 UTC
Author: tkoenig
Date: Thu Jan  4 21:04:23 2018
New Revision: 256265

URL: https://gcc.gnu.org/viewcvs?rev=256265&root=gcc&view=rev
Log:
2018-01-04  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/83683
	PR fortran/45689
	* check.c (gfc_check_eoshift): Check for string length and
	for conformance of boundary.
	* intrinsic.c (add_functions): Add gfc_simplify_eoshift.
	* intrinsic.h: Add prototype for gfc_simplify_eoshift.
	* simplify.c (gfc_simplify_eoshift): New function.

2018-01-04  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/83683
	PR fortran/45689
	* gfortran.dg/eoshift_8.f90: New test.
	* gfortran.dg/simplify_eoshift_1.f90: New test.


Added:
    trunk/gcc/testsuite/gfortran.dg/eoshift_8.f90
    trunk/gcc/testsuite/gfortran.dg/simplify_eoshift_1.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/check.c
    trunk/gcc/fortran/intrinsic.c
    trunk/gcc/fortran/intrinsic.h
    trunk/gcc/fortran/simplify.c
    trunk/gcc/testsuite/ChangeLog
Comment 19 Thomas Koenig 2018-01-04 21:06:41 UTC
Fixed on trunk, closing.