This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: RFC builtin functions allocate their results


On Monday 26 July 2004 07:33, Victor Leikehman wrote:
> I started working on the change that will allow builtin functions
> to create the return value in case that the caller does not know
> the shape of the result. This change was proposed by Paul Brook
> long ago, but now I have time to do it.

We want to make this work with all functions, not just builtins.

> Currently, if the caller does not know the shape of the result, it
> causes ICE.  The change is (at least) two-fold.  First, to change
> the caller's code so that it will create an array descriptor, but
> does not allocate memory or set array bounds.  Second, to change
> the library so that if the result array descriptor has null data
> field, it will allocate the memory and set the array bounds.

Also change compiler to generate function prologue code to perform the 
allocation.

I think it's still worth allocating the temporary in the caller when we can. 
This will allow us to reuse temporaries, amongst other things. It isn't clear 
whether you're proposing doing this for all functions, or just the ones where 
the compiler can't figure out the size.

> This note deals with the second part of the change -- the library
> part.  I changed one function just for example to get your
> opinions, and then I will post a cross-library patch.

Looks basically ok, just with a few minor issues.

> Note that without the related change in the compiler, this addition
> is just dead code.  However, we must start somewhere -- if I first
> patch the compiler, it will generate code that will crash in the
> library.

Ok. If you need any help, just shout. The array code can be ... err ... 
somewhat cryptic.

> Index: cshift0.c
> ===================================================================
> RCS file: /cvsroot/gcc/gcc/libgfortran/intrinsics/cshift0.c,v
> retrieving revision 1.2
> diff -c -p -r1.2 cshift0.c
> *** cshift0.c	13 May 2004 06:41:02 -0000	1.2
> --- cshift0.c	26 Jul 2004 06:16:30 -0000
> *************** __cshift0 (const gfc_array_char * ret, c
> *** 69,74 ****
> --- 69,93 ----
>     soffset = size;
>     len = 0;
>
> +   if (ret->data == 0)

== NULL

> +     {
> +       int i;
> +
> +       ret->data = prefix(internal_malloc) (size * prefix(size0) (array));
> +       ret->base = 0;
> +       ret->dtype = array->dtype;
> +       for (i = 0; i < GFC_DESCRIPTOR_RANK (array); i++)
> +         {
> +           ret->dim[i].lbound = 1;
> +           ret->dim[i].ubound = array->dim[i].ubound -
> array->dim[i].lbound + 1;
> +           if (i == 0)
> +             ret->dim[i].stride = 1;
> +           else
> +             ret->dim[i].stride = ret->dim[i-1].ubound *
> ret->dim[i-1].stride; +         }

The bounds and base are inconsistent. Either use zero-based bounds, or set 
base properly. Other temporary arrays use zero based bounds.

> +     }
> +
>     for (dim = 0; dim < GFC_DESCRIPTOR_RANK (array); dim++)
>       {
>         if (dim == which)

Paul


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]