From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 50656 invoked by alias); 26 Nov 2017 17:31:13 -0000 Mailing-List: contact jit-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Subscribe: Sender: jit-owner@gcc.gnu.org Received: (qmail 50639 invoked by uid 89); 26 Nov 2017 17:31:12 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.99.2 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.7 required=5.0 tests=AWL,BAYES_00,KAM_SHORT,KB_WAM_FROM_NAME_SINGLEWORD,SPF_PASS,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 spammy=sont, persistent, H*Ad:U*jit, remembered X-Spam-Status: No, score=-1.7 required=5.0 tests=AWL,BAYES_00,KAM_SHORT,KB_WAM_FROM_NAME_SINGLEWORD,SPF_PASS,T_RP_MATCHES_RCVD autolearn=no version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on sourceware.org X-Spam-Level: X-HELO: ovh.starynkevitch.net Received: from ovh.starynkevitch.net (HELO ovh.starynkevitch.net) (46.105.17.220) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sun, 26 Nov 2017 17:31:10 +0000 Received: from ovh.starynkevitch.net (localhost [127.0.0.1]) by ovh.starynkevitch.net (Postfix) with ESMTP id C2A5DA03CB for ; Sun, 26 Nov 2017 18:31:06 +0100 (CET) Authentication-Results: ovh.starynkevitch.net (amavisd-new); dkim=pass reason="pass (just generated, assumed good)" header.d=starynkevitch.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d= starynkevitch.net; h=content-language:content-transfer-encoding :content-type:content-type:mime-version:user-agent:date:date :message-id:to:subject:subject:from:from; s=dkim; t=1511717460; x=1512581461; bh=Hoj4joIQ6Iks40Fh4r+TIM/mffxRLld1C6pFXD4QJ7U=; b= Hx7EE5lyB7wU+NxKmAAjrLbgccQFWh6MZrNP57Q8e2QhwjR3U9obycfNbNzK+8M7 rgJ5VcOcek5IMpIdtuLuZosz8zMpXNVRfeS4qT4t7eCx9vTt89p2iZk2dyhFA8Ik bn9a8Y4o/dUybwF/GZEZNx6TdiBfeSDjvcZgK/tCdt4= X-Virus-Scanned: Debian amavisd-new at ovh.starynkevitch.net Received: from ovh.starynkevitch.net ([127.0.0.1]) by ovh.starynkevitch.net (ovh.starynkevitch.net [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id o-myz0GUqtG2 for ; Sun, 26 Nov 2017 18:31:00 +0100 (CET) Received: from [192.168.1.2] (LStLambert-656-1-266-187.w193-248.abo.wanadoo.fr [193.248.54.187]) by ovh.starynkevitch.net (Postfix) with ESMTPSA id 34ABEA03C4 for ; Sun, 26 Nov 2017 18:31:00 +0100 (CET) From: Basile Starynkevitch Subject: calling inline functions from JIT-ed code To: jit@gcc.gnu.org Message-ID: <918b0556-10b5-70a6-b609-6f13dc4fcf50@starynkevitch.net> Date: Sun, 01 Jan 2017 00:00:00 -0000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-IsSubscribed: yes X-SW-Source: 2017-q4/txt/msg00005.txt.bz2 Hello All, (assume that I am using -in a few weeks- GCC8 on Debian/Sid/x86-64 if details matter) I would like to JIT compile using libgccjit some code in my language implementation. If you really want to know which one, look on my github project https://github.com/bstarynk/bismon but we could more easily reason about JIT-compiling for GUILE 2.2 (so I am referring to header files from the guile-2.2-dev Debian package). In such a situation, it is often the case that we already have several inline functions related to short basic operations in that interpreter or language implementation. In particular the basic cons operation (to build a GUILE cons pair) is implemented by a call to the following inlined function (near line 170 of file /usr/include/guile/2.2/libguile/gc-inline.h) static inline SCM scm_inline_cons (scm_i_thread *thread, SCM x, SCM y) {   return scm_inline_cell (thread, SCM_UNPACK (x), SCM_UNPACK (y)); } My question is how to be sure that libgccjit is generating a call to that inlined function. A possible (still not implemented) approach in the future might be to be able to get a gcc_jit_context from some pre-compiled preprocessor header file, but AFAIU we don't have that (and I guess it is very difficult to implement). So in that ideal world we would just call an hypothetical function like gcc_jit_context* gcc_jit_context_new_from_precompiled_header(const char*filepath); IIRC, I remembered having vaguely expressed a similar wish in https://gcc.gnu.org/ml/jit/2015-q4/msg00001.html and in https://gcc.gnu.org/ml/jit/2015-q2/msg00093.html My feeling is that might not be really needed, if we could use Link Time Optimization. So we might perhaps compile all GUILE inline functions, perhaps by compiling with gcc -O2 -flto a simple C file myjitguile.c containing (after the appropriate GUILE includes) simplistic wrappers like: SCM myjit_scm_cons(void*th, void*x, void*y) { return scm_inline_cons ((scp__i_thread*)th, (SCM)(x), (SCM)(y)); } But then I am stuck on the detailed steps. My current thinking is that: 1. I should compile that myjitguile.c using gcc -O2 -flto -fPIC myjitguile.c -o myjitguile.pic.o 2. I should create a gcc_jit_context and fill it with the declarations of all functions from myjitguile.c; this could be tedious if that file is big enough; perhaps coding a specialized GCC plugin to help in that might be worthwhile (that plugin will extract the declarations  of mygitguile.c and put them into some persistent file or database, to be later read by the JIT-ing code to fill that gcc_jit_context). 3. I should fill that gcc_jit_context with my new JIT-ed code, with "ordinary" calls to functions named like myjit_scm_cons 4. Once that gcc_jit_context is complete, I need t compile it with -O2 -flto -fPIC. I don't know how exactly to do that, but I am guessing that I need to call both gcc_jit_context_add_command_line_option and gcc_jit_context_set_int_option but I don't understand when should I call these. Is it at that step 4 or at step 2? 5. Should I call gcc_jit_context_compile_to_file followed by dlopen or gcc_jit_context_compile ? Conceptually, I just want to JIT compile to memory, so ideally I would like to avoid writing files? 6. Should I fork some gcc linking process? Which one exactly? Regards. -- Basile STARYNKEVITCH ==http://starynkevitch.net/Basile opinions are mine only - les opinions sont seulement miennes Bourg La Reine, France