public inbox for jit@gcc.gnu.org
 help / color / mirror / Atom feed
* Questions about using array as parameter in gccjit generated funtion
@ 2020-10-12 13:47 李鹏泽
  2020-10-12 14:40 ` David Malcolm
  0 siblings, 1 reply; 2+ messages in thread
From: 李鹏泽 @ 2020-10-12 13:47 UTC (permalink / raw)
  To: dmalcolm; +Cc: jit

[-- Attachment #1: Type: text/plain, Size: 1753 bytes --]

Dear maintainer of lib gccjit:

  I'm using gccjit to generate a function and pass some parameters to it, but I encountered a strange problem, and I did not find solutions in the search engine and gcc jit documentation. So I am here hope to get some help from you.

  I installed with sudo dnf install libgccjit-devel the version of gccjit is 10.2.1, gcc version is 10.2.1, Fedora version is release 32.

  The problem happened when I tried to pass an int array to a gccjit function. If I don't turn on -O optimization when compile the total source code in the terminal (not the gccjit optimization option), the output will be nonsense, like:




# root @ SLOT1-4 in ~/wanpcs/jit/docs/examples [6:24:43] C:127
$ ./t3
obj: loop_body
loop_test returned: 451999691
(base)


  If I use -O option, then every thing just goes fine. But I wonder why I have to turn on the gcc -O option which is bad for debugging to get expected result.

  The attachment arr.c is a program from tutorial 3 with minor change. In this program I use the gccjit funtion to get the sum of an array(line 99 to line 111). The array is gotten from parameter (line 49). The output function is defined in (line 52-57). The call of the generated function is in line 178 and the array parameter is defined in line 177.

  To compile this file just as the tutorial said "gcc arr.c -o arr -lgccjit ". 

  Any suggestions will be helpful, and if you could take some time out of your busy schedule to give me some advice, I would greatly appreciate it. If I did not make the question clear please let me know. Gccjit is a fancy tool and I wish it could becomes better! Many Thanks!




Best Wishes!




--
Pengze Li
School of Electronics Engineering and Computer Science,Peking University

[-- Attachment #2: arr.c --]
[-- Type: text/plain, Size: 4774 bytes --]

/* Usage example for libgccjit.so
   Copyright (C) 2014-2018 Free Software Foundation, Inc.

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.

GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include <libgccjit.h>

#include <stdlib.h>
#include <stdio.h>

void
create_code (gcc_jit_context *ctxt)
{
  /*
    Simple sum-of-squares, to test conditionals and looping

    int loop_test (int n)
    {
      int i;
      int sum = 0;
      for (i = 0; i < n ; i ++)
      {
	sum += i * i;
      }
      return sum;
   */
  gcc_jit_type *the_type =
    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
  gcc_jit_type *return_type = the_type;

  gcc_jit_type *array_type = gcc_jit_context_new_array_type(ctxt, NULL, the_type, 5);

  gcc_jit_param *n =
    gcc_jit_context_new_param (ctxt, NULL, the_type, "n");
  gcc_jit_param *arr =
    gcc_jit_context_new_param (ctxt, NULL, array_type, "arr");  
  gcc_jit_param *params[2] = {n, arr};
  gcc_jit_function *func =
    gcc_jit_context_new_function (ctxt, NULL,
				  GCC_JIT_FUNCTION_EXPORTED,
				  return_type,
				  "loop_test",
				  2, params, 0);

  /* Build locals:  */
  gcc_jit_lvalue *i =
    gcc_jit_function_new_local (func, NULL, the_type, "i");
  gcc_jit_lvalue *sum =
    gcc_jit_function_new_local (func, NULL, the_type, "sum");

  gcc_jit_block *b_initial =
    gcc_jit_function_new_block (func, "initial");
  gcc_jit_block *b_loop_cond =
    gcc_jit_function_new_block (func, "loop_cond");
  gcc_jit_block *b_loop_body =
    gcc_jit_function_new_block (func, "loop_body");
  gcc_jit_block *b_after_loop =
    gcc_jit_function_new_block (func, "after_loop");

  /* sum = 0; */
  gcc_jit_block_add_assignment (
    b_initial, NULL,
    sum,
    gcc_jit_context_zero (ctxt, the_type));

  /* i = 0; */
  gcc_jit_block_add_assignment (
    b_initial, NULL,
    i,
    gcc_jit_context_zero (ctxt, the_type));

  gcc_jit_block_end_with_jump (b_initial, NULL, b_loop_cond);

  /* if (i >= n) */
  gcc_jit_block_end_with_conditional (
    b_loop_cond, NULL,
    gcc_jit_context_new_comparison (
       ctxt, NULL,
       GCC_JIT_COMPARISON_GE,
       gcc_jit_lvalue_as_rvalue (i),
       gcc_jit_param_as_rvalue (n)),
    b_after_loop,
    b_loop_body);


  gcc_jit_lvalue *element = gcc_jit_context_new_array_access (
        ctxt,
        NULL,
        gcc_jit_param_as_rvalue (arr),
        gcc_jit_lvalue_as_rvalue (i));
  /* sum += i * i */
  gcc_jit_block_add_assignment_op (
    b_loop_body, NULL,
    sum,
    GCC_JIT_BINARY_OP_PLUS,
    gcc_jit_lvalue_as_rvalue(element)
    );

  /* i++ */
  gcc_jit_block_add_assignment_op (
    b_loop_body, NULL,
    i,
    GCC_JIT_BINARY_OP_PLUS,
    gcc_jit_context_one (ctxt, the_type));

  gcc_jit_block_end_with_jump (b_loop_body, NULL, b_loop_cond);

  /* return sum */
  gcc_jit_block_end_with_return (
    b_after_loop,
    NULL,
    gcc_jit_lvalue_as_rvalue (sum));
}

int
main (int argc, char **argv)
{
  gcc_jit_context *ctxt = NULL;
  gcc_jit_result *result = NULL;

  /* Get a "context" object for working with the library.  */
  ctxt = gcc_jit_context_acquire ();
  if (!ctxt)
    {
      fprintf (stderr, "NULL ctxt");
      goto error;
    }

  /* Set some options on the context.
     Let's see the code being generated, in assembler form.  */
  gcc_jit_context_set_bool_option (
    ctxt,
    GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE,
    0);
  gcc_jit_context_set_int_option (
    ctxt,
    GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL,
    3);

  /* Populate the context.  */
  create_code (ctxt);
  gcc_jit_context_dump_to_file(ctxt, "./pre.dump", 0);

  /* Compile the code.  */
  result = gcc_jit_context_compile (ctxt);
  if (!result)
    {
      fprintf (stderr, "NULL result");
      goto error;
    }

  /* Extract the generated code from "result".  */
  typedef int (*loop_test_fn_type) (int, int[5]);
  loop_test_fn_type loop_test =
    (loop_test_fn_type)gcc_jit_result_get_code (result, "loop_test");
  if (!loop_test)
    {
      fprintf (stderr, "NULL loop_test");
      goto error;
    }

  /* Run the generated code.  */
  int arr[5] = {1 , 2, 3, 4, 5};
  int val = loop_test (5, arr);
  printf("loop_test returned: %d\n", val);

 error:
  gcc_jit_context_release (ctxt);
  gcc_jit_result_release (result);
  return 0;
}

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

* Re: Questions about using array as parameter in gccjit generated funtion
  2020-10-12 13:47 Questions about using array as parameter in gccjit generated funtion 李鹏泽
@ 2020-10-12 14:40 ` David Malcolm
  0 siblings, 0 replies; 2+ messages in thread
From: David Malcolm @ 2020-10-12 14:40 UTC (permalink / raw)
  To: 李鹏泽; +Cc: jit

On Mon, 2020-10-12 at 21:47 +0800, 李鹏泽 wrote:
> Dear maintainer of lib gccjit:
> 
>   I'm using gccjit to generate a function and pass some parameters to
> it, but I encountered a strange problem, and I did not find solutions
> in the search engine and gcc jit documentation. So I am here hope to
> get some help from you.
> 
>   I installed with sudo dnf install libgccjit-devel the version of
> gccjit is 10.2.1, gcc version is 10.2.1, Fedora version is release
> 32.
> 
>   The problem happened when I tried to pass an int array to a gccjit
> function. If I don't turn on -O optimization when compile the total
> source code in the terminal (not the gccjit optimization option), the
> output will be nonsense, like:
> 
> 
> 
> # root @ SLOT1-4 in ~/wanpcs/jit/docs/examples [6:24:43] C:127
> $ ./t3
> obj: loop_body
> loop_test returned: 451999691
> (base)
> 
> 
>   If I use -O option, then every thing just goes fine. But I wonder
> why I have to turn on the gcc -O option which is bad for debugging to
> get expected result.

I tried your example, and I got the same behavior.

I tried adding all of the various debugging options from 
https://gcc.gnu.org/onlinedocs/jit/topics/contexts.html

In particular, I added:
  gcc_jit_context_set_bool_option (
    ctxt,
    GCC_JIT_BOOL_OPTION_DEBUGINFO,
    1);
and set the 2nd param of gcc_jit_context_dump_to_file, so that I can
step through the generated code in gdb.

With that, I recompiled with -g (so that the precompiled code can also
be stepped through in gdb), and set a breakpoint in gdb on loop_test.

Without -O, I see:


Breakpoint 1, loop_test (n=5, arr=...) at ./pre.dump:8
8	  sum = (int)0;
(gdb) p arr
$15 = {-10536, 32767, 4200493, 1, 1}

#1  0x00000000004017a3 in main (argc=1, argv=0x7fffffffd6d8) at
/tmp/arr.c:198
198	  int val = loop_test (5, arr);
(gdb) p arr
$16 = {1, 2, 3, 4, 5}


whereas with -O I see:

Breakpoint 1, loop_test (n=5, arr=...) at ./pre.dump:8
8	  sum = (int)0;
(gdb) p arr
$17 = {1, 2, 3, 4, 5}
(gdb) up
#1  0x0000000000401625 in main (argc=<optimized out>, argv=<optimized
out>) at /tmp/arr.c:198
198	  int val = loop_test (5, arr);
(gdb) p arr
$18 = {1, 2, 3, 4, 5}



So somehow "arr" is not being passed correctly to the generated code in
the "compiled without -O" case.




>   The attachment arr.c is a program from tutorial 3 with minor
> change. In this program I use the gccjit funtion to get the sum of an
> array(line 99 to line 111). The array is gotten from parameter (line
> 49). The output function is defined in (line 52-57). The call of the
> generated function is in line 178 and the array parameter is defined
> in line 177.

It's helpful to update the comments at the same time as updating the
code.


>   To compile this file just as the tutorial said "gcc arr.c -o arr
> -lgccjit ". 
> 
>   Any suggestions will be helpful, and if you could take some time
> out of your busy schedule to give me some advice, I would greatly
> appreciate it. If I did not make the question clear please let me
> know. Gccjit is a fancy tool and I wish it could becomes better! Many
> Thanks!

I'm not yet sure whether this is a bug in libgccjit, or whether there's
a subtle mistake somewhere in your code.

What happens if you construct an equivalent example in pure C without
gccjit (perhaps putting the equivalent of loop_test in a separate
source file?)


I hope this above is helpful
Dave



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

end of thread, other threads:[~2020-10-12 14:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-12 13:47 Questions about using array as parameter in gccjit generated funtion 李鹏泽
2020-10-12 14:40 ` David Malcolm

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