From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [63.128.21.124]) by sourceware.org (Postfix) with ESMTP id 7D0A83857027 for ; Mon, 12 Oct 2020 14:41:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 7D0A83857027 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-572-i17JMdNtN3S0i9xzTk_QKw-1; Mon, 12 Oct 2020 10:41:00 -0400 X-MC-Unique: i17JMdNtN3S0i9xzTk_QKw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7459A802B48; Mon, 12 Oct 2020 14:40:59 +0000 (UTC) Received: from ovpn-112-135.phx2.redhat.com (ovpn-112-135.phx2.redhat.com [10.3.112.135]) by smtp.corp.redhat.com (Postfix) with ESMTP id DB03B10013BD; Mon, 12 Oct 2020 14:40:57 +0000 (UTC) Message-ID: Subject: Re: Questions about using array as parameter in gccjit generated funtion From: David Malcolm To: =?UTF-8?Q?=E6=9D=8E=E9=B9=8F=E6=B3=BD?= Cc: jit@gcc.gnu.org Date: Mon, 12 Oct 2020 10:40:56 -0400 In-Reply-To: <3b7ce66e.4d53b.1751d114365.Coremail.lipengze@pku.edu.cn> References: <3b7ce66e.4d53b.1751d114365.Coremail.lipengze@pku.edu.cn> User-Agent: Evolution 3.36.5 (3.36.5-1.fc32) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-6.0 required=5.0 tests=BAYES_00, BODY_8BITS, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: jit@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Jit mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 12 Oct 2020 14:41:08 -0000 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=, argv=) 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