This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Assigning the result of a function call to a variable in the Gfortran frontend
Thank you leslie!
Best,
Tobias
On Wed, Sep 6, 2017, at 05:25, Leslie Zhai wrote:
>
>
> 在 2017年09月05日 15:25, Leslie Zhai 写道:
> >
> >
> > 在 2017年09月05日 14:14, Tobias Grosser 写道:
> >> Hi Leslie,
> >>
> >> I copied you in this thread as you currently worked quite a bit with
> >> dragonegg and we are currently trying to generate metadata that makes it
> >> easier to reason about multi-dimensional fortran arrays.
> >>
> >> Best,
> >> Tobias
> >>
> >> On Mon, Sep 4, 2017, at 23:06, (IIIT) Siddharth Bhat wrote:
> >>> Hello,
> >>>
> >>> I've been hacking on the Gfortran frontend to change array index
> >>> expressions to function calls, so that I can inspect them later on
> >>> in the
> >>> pipeline. I go from Fortran -> LLVM IR (through dragonegg) where I will
> >>> look at the function call nodes.
> >>>
> >>> However, I'm not able to generate correct IR for this. I can create
> >>> function call, but I am unable to assign the return value of a function
> >>> call to a variable here.
> >>>
> >>> Here's a link to my experiments here: It includes a patch, a test file
> >>> and
> >>> the GIMPLE output
> >>> <https://gist.github.com/bollu/999184bdb3d0f1569ee0fd0a351689e3#file-m-gimple-L24>
> >>>
> >
> > I rebase the patch for GCC v8.x at first:
>
>
> diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c
> index 9efb531..5cf5ba9 100644
> --- a/gcc/fortran/trans-array.c
> +++ b/gcc/fortran/trans-array.c
> @@ -91,6 +91,8 @@ along with GCC; see the file COPYING3. If not see
> #include "dependency.h"
>
> static bool gfc_get_array_constructor_size (mpz_t *,
> gfc_constructor_base);
> +static tree call_polly_index(stmtblock_t *parent_block, tree
> *original_index,
> + gfc_array_ref *ar);
>
> /* The contents of this structure aren't actually used, just the
> address. */
> static gfc_ss gfc_ss_terminator_var;
> @@ -3251,7 +3253,13 @@ gfc_conv_scalarized_array_ref (gfc_se * se,
> gfc_array_ref * ar)
> if (build_class_array_ref (se, tmp, index))
> return;
>
> + printf("TIMSTAMP: %s - %s\n", __DATE__, __TIME__);
> + printf("======\n");
> +
> + printf("# 1. index(new):\n");
> + call_polly_index(&se->pre, &index, ar);
> se->expr = gfc_build_array_ref (tmp, index, decl);
> + printf("======\n");
> }
>
>
> @@ -3335,6 +3343,22 @@ build_array_ref (tree desc, tree offset, tree
> decl, tree vptr)
> return tmp;
> }
>
> +// See: gfc_call_malloc
> +static tree call_polly_index(stmtblock_t *parent_block, tree
> */*original_index*/,
> + gfc_array_ref */*ar*/) {
> + tree fncall, var, result;
> + stmtblock_t block;
> +
> + var = gfc_create_var(gfc_array_index_type, "pollyindex");
> + gfc_init_block (&block);
> +
> + fncall = build_call_expr_loc(input_location,
> gfor_fndecl_polly_array_index, 0);
> + gfc_add_modify(&block, var, fold_convert(gfc_array_index_type,
> fncall));
> + result = gfc_finish_block (&block);
> + gfc_add_expr_to_block(parent_block, result);
> + return var;
> +}
> +
>
> /* Build an array reference. se->expr already holds the array
> descriptor.
> This should be either a variable, indirect variable reference or
> component
> diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
> index 74d8606..28ddcfd 100644
> --- a/gcc/fortran/trans-decl.c
> +++ b/gcc/fortran/trans-decl.c
> @@ -95,6 +95,7 @@ static int seen_ieee_symbol;
>
> /* Function declarations for builtin library functions. */
>
> +tree gfor_fndecl_polly_array_index;
> tree gfor_fndecl_pause_numeric;
> tree gfor_fndecl_pause_string;
> tree gfor_fndecl_stop_numeric;
> @@ -3495,6 +3496,14 @@ gfc_build_builtin_function_decls (void)
> /* ERROR STOP doesn't return. */
> TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string) = 1;
>
> +
> + printf("building polly_array_index function decl...\n");
> + gfor_fndecl_polly_array_index = gfc_build_library_function_decl (
> + get_identifier (PREFIX("polly_array_index")),
> + gfc_array_index_type, 0);
> + TREE_THIS_VOLATILE (gfor_fndecl_polly_array_index) = 1;
> + printf("built polly_array_index function decl...\n");
> +
> gfor_fndecl_pause_numeric = gfc_build_library_function_decl (
> get_identifier (PREFIX("pause_numeric")),
> void_type_node, 1, gfc_int4_type_node);
> diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
> index d02f347..c8d24ec 100644
> --- a/gcc/fortran/trans.h
> +++ b/gcc/fortran/trans.h
> @@ -781,6 +781,7 @@ struct gimplify_omp_ctx;
> void gfc_omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *,
> tree);
>
> /* Runtime library function decls. */
> +extern GTY(()) tree gfor_fndecl_polly_array_index;
> extern GTY(()) tree gfor_fndecl_pause_numeric;
> extern GTY(()) tree gfor_fndecl_pause_string;
> extern GTY(()) tree gfor_fndecl_stop_numeric;
>
>
>
> >
> >
> >
> > diff --git a/gcc/fortran/trans-array.c b/gcc/fortran/trans-array.c
> > index 9efb531..73dad6e 100644
> > --- a/gcc/fortran/trans-array.c
> > +++ b/gcc/fortran/trans-array.c
> > @@ -3251,6 +3251,7 @@ gfc_conv_scalarized_array_ref (gfc_se * se,
> > gfc_array_ref * ar)
> > if (build_class_array_ref (se, tmp, index))
> > return;
> >
> > + call_polly_index(&se->pre, index, ar);
> > se->expr = gfc_build_array_ref (tmp, index, decl);
> > }
> >
> > @@ -3335,6 +3336,24 @@ build_array_ref (tree desc, tree offset, tree
> > decl, tree vptr)
> > return tmp;
> > }
> >
> > +// See: gfc_call_malloc
> > +static tree call_polly_index(stmtblock_t *parent_block, tree
> > *original_index,
> > + gfc_array_ref *ar) {
> > + tree fncall, var, result;
> > + stmtblock_t block;
> > +
> > + var = gfc_create_var(gfc_array_index_type, "pollyindex");
> > + gfc_init_block (&block);
> > +
> > + fncall = build_call_expr_loc(input_location,
> > gfor_fndecl_polly_array_index, 0);
> > + gfc_add_modify(&block, var, fold_convert(gfc_array_index_type,
> > fncall));
> > + result = gfc_finish_block (&block);
> > + gfc_add_expr_to_block(parent_block, result);
> > + return var;
> > +
> > + // return var;
> > +}
> > +
> >
> > /* Build an array reference. se->expr already holds the array
> > descriptor.
> > This should be either a variable, indirect variable reference or
> > component
> > diff --git a/gcc/fortran/trans-decl.c b/gcc/fortran/trans-decl.c
> > index 74d8606..28ddcfd 100644
> > --- a/gcc/fortran/trans-decl.c
> > +++ b/gcc/fortran/trans-decl.c
> > @@ -95,6 +95,7 @@ static int seen_ieee_symbol;
> >
> > /* Function declarations for builtin library functions. */
> >
> > +tree gfor_fndecl_polly_array_index;
> > tree gfor_fndecl_pause_numeric;
> > tree gfor_fndecl_pause_string;
> > tree gfor_fndecl_stop_numeric;
> > @@ -3495,6 +3496,14 @@ gfc_build_builtin_function_decls (void)
> > /* ERROR STOP doesn't return. */
> > TREE_THIS_VOLATILE (gfor_fndecl_error_stop_string) = 1;
> >
> > +
> > + printf("building polly_array_index function decl...\n");
> > + gfor_fndecl_polly_array_index = gfc_build_library_function_decl (
> > + get_identifier (PREFIX("polly_array_index")),
> > + gfc_array_index_type, 0);
> > + TREE_THIS_VOLATILE (gfor_fndecl_polly_array_index) = 1;
> > + printf("built polly_array_index function decl...\n");
> > +
> > gfor_fndecl_pause_numeric = gfc_build_library_function_decl (
> > get_identifier (PREFIX("pause_numeric")),
> > void_type_node, 1, gfc_int4_type_node);
> > diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
> > index d02f347..c8d24ec 100644
> > --- a/gcc/fortran/trans.h
> > +++ b/gcc/fortran/trans.h
> > @@ -781,6 +781,7 @@ struct gimplify_omp_ctx;
> > void gfc_omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *,
> > tree);
> >
> > /* Runtime library function decls. */
> > +extern GTY(()) tree gfor_fndecl_polly_array_index;
> > extern GTY(()) tree gfor_fndecl_pause_numeric;
> > extern GTY(()) tree gfor_fndecl_pause_string;
> > extern GTY(()) tree gfor_fndecl_stop_numeric;
> >
> >
> > then I will rebuild GCC for investigation.
>
> $ /opt/gcc-git/bin/gcc -v
> Using built-in specs.
> COLLECT_GCC=/opt/gcc-git/bin/gcc
> COLLECT_LTO_WRAPPER=/opt/gcc-git/libexec/gcc/x86_64-redhat-linux-gnu/8.0.0/lto-wrapper
> Target: x86_64-redhat-linux-gnu
> Configured with: ../configure --target=x86_64-redhat-linux-gnu
> --host=x86_64-redhat-linux-gnu --build=x86_64-redhat-linux-gnu
> --with-cpu=generic --prefix=/opt/gcc-git --enable-bootstrap
> --enable-shared --enable-threads=posix --enable-checking=release
> --enable-languages=c,c++,lto,go,fortran,objc,obj-c++ --enable-plugin
> --enable-initfini-array --enable-gnu-unique-object
> --enable-linker-build-id --with-linker-hash-style=gnu
> --enable-__cxa_atexit --enable-gnu-indirect-function --enable-c99
> --enable-long-long --enable-libgomp --enable-lto --enable-libsanitizer
> --enable-libatomic --enable-libquadmath --enable-libitm
> --enable-libcilkrts --enable-libmpx --enable-symvers
> --disable-libstdcxx-pch --disable-multilib --disable-libunwind-exceptions
> Thread model: posix
> gcc version 8.0.0 20170905 (experimental) (GCC)
>
>
> $ /opt/gcc-git/bin/gcc -o m.o -c -g -Wall -Wextra -fPIC -fdump-tree-all
> m.f90
> building polly_array_index function decl...
> built polly_array_index function decl...
> m.f90:12:17:
>
> coeff
> 1
> Warning: Unused variable ‘coeff’ declared at (1) [-Wunused-variable]
>
>
> call_polly_index not be called? please rebase the patch for GCC v8.0,
> thanks!
>
> >
> >
> >>> .
> >>>
> >>> Help would be very much appreciated!
> >>>
> >>> Thanks,
> >>> Siddharth.
> >>> --
> >>> Sending this from my phone, please excuse any typos!
> >
>
> --
> Regards,
> Leslie Zhai - https://reviews.llvm.org/p/xiangzhai/
>
>
>