From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-yw1-x1131.google.com (mail-yw1-x1131.google.com [IPv6:2607:f8b0:4864:20::1131]) by sourceware.org (Postfix) with ESMTPS id 85B0A3858D20 for ; Sun, 11 Jun 2023 10:35:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 85B0A3858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-yw1-x1131.google.com with SMTP id 00721157ae682-565cfe4ece7so34029947b3.2 for ; Sun, 11 Jun 2023 03:35:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1686479755; x=1689071755; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=JVbL+rhx4/BbB9dgP+r5puBLLsOLAEQ3AlciOrPGgw4=; b=kXm67XOHj9J8rh88ObyyTGYHAsrYSiv7GkGH1Ub2ob+UgxrMSMcjFhhNK3jyrz2/Y1 pRAn1lj2b2/KNGvxah9KdgFNYqWosSYG5YZmaMSq/pjlfxzwfWeRucAVEf/XfHsDSPU+ bLex7Jpyv+pRil+nrB8UYTvU/qiaGavboWA9QD3IbQdjqiXJgd+9YmHhDeaVocbCDlhq 9ZpkTisa5QtvqOsqudEPlJzaBFuv024f/D6olrkgHpi9c3Qm5oxZf4dKAydIgGV4n7Vy fgzx3FOdl9tTvBX6xSjST2V8Rpl7g6o6CZ6eA8NcD8Jc9ywD1I5ywLnsCEJttqWbjzGH GDcQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686479755; x=1689071755; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JVbL+rhx4/BbB9dgP+r5puBLLsOLAEQ3AlciOrPGgw4=; b=Hhjnlhhn6TSrShW2ZqMt4S/glkIX/DHxm1qqwFmowFhQ/kz6E9UcD2smkSiQ6hbV+8 wblTpM88Oxabt3MwqOaaluLVsMy2GkXtlnza8v/Im6SAgNw1qYK03SSjuR3bWAHRgTzj RBWQlLenm8qzlP1199vaBKIb80F55MOjHpwraQMTJusnts/L9+yYP+oyyW87nlVJmS4T 6WQE2SftLo4YSwVOz56Rzvhdpcm4ShgFOpoW2deBIgzfqxK83sxF19HHYvrA05YCldeb UTX40oZqbigTbub74xoFbwERcH0atNynkSWYcWiP7J9ajiG2R0hXzsR5z0kqzsYj3GIw P2PA== X-Gm-Message-State: AC+VfDwrzvKiVl30mLHc5J13mlgy86t3jnvOt1CF+vXEKSd/Yr7Y28YU NNucYZY6UEoskWpIOj6A0/yntNkw8QaCk/yF+n21o9I3 X-Google-Smtp-Source: ACHHUZ4ujJD7JLaXn0wb9/+vGp7kp/0p0o0cKTJ/sw8F6JaqThRD3Re9FEgZSIFz27iEoo1ffGjw7l88vyGJPqpe+a4= X-Received: by 2002:a81:91d5:0:b0:565:9d27:c5e0 with SMTP id i204-20020a8191d5000000b005659d27c5e0mr7114139ywg.2.1686479754800; Sun, 11 Jun 2023 03:35:54 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Joshua Saxby Date: Sun, 11 Jun 2023 11:35:44 +0100 Message-ID: Subject: Rationale for compiling to memory and file To: David Malcolm Cc: jit@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=0.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: > I have a higher-level question, which is why do you want to compile to > both/reuse results? I am in the process of designing a programming language for which I want to use libgccjit as the backend. My language will feature compile-time function execution and reusing the JIT for both compile-time evaluated code and code going into the binary simplifies the design greatly (although it is probably less efficient than an interpreter for CTFE). > albeit with the drawback that it's duplicating work. Is it the > duplicated work that you're trying to avoid? I'm sure you're right that contexts can be compiled twice in this way, and this is what I considered at first, but yes, this is the work duplication I'd like to avoid, as JIT's compile times are non-trivial. > Do you have ideas about what the API you'd want would look like? I have some ideas, yes. The chief principle on my mind is the need to not break the existing API compatibility, therefore retaining the current functions that compile to memory and file in one-shot. For the new thing I propose to build, I anticipate an API approximately as follows: compile_intermediate(context) -> intermediate_result finalise_to_file(intermediate_result) -> jit_result finalise_to_memory(intermediate_result) -> jit_result I should stress, those are just provisional names I picked out of the air for functions and data structures, so I have some way to illustrate it, but I think this captures the main structure I'd aim for. I would be interested to know what you think if you have any thoughts on it= . Best Regards, J.S. On Sat, 10 Jun 2023 at 22:47, David Malcolm wrote: > > On Sat, 2023-06-10 at 17:26 +0100, Joshua Saxby via Jit wrote: > > Hi Joshua > > > After following files jit-recording.cc and jit-playback.cc, I think > > I've > > found out where I need to patch JIT to do what I want it to do. > > Indeed. In case you haven't seen it yet, see: > https://gcc.gnu.org/onlinedocs/jit/internals/index.html#overview-of-code-= structure > > You'll see there (and in jit-playback.cc) that the > gcc_jit_compile[_to_file] functions are basically running the > equivalent of cc1 in-process to generate a .s file in a tempdir, and > then calling out to subprocesses to run the assembler and linker as > needed. FWIW I've experimented in the past with libraries for the > assembler and linker (keeping it all in-process), and got some > speedups, but it needed nontrivial patches to binutils to turn as and > ld into shared libraries. > > > Looks like both compiling to file and memory ultimately rely upon > > playback::context::compile() to do the bulk of their work and then > > override > > a postprocess() method to do additional handling pertaining to their > > specific task. > > > > If I can find a way to cache or store the intermediate result > > generated by > > compile(), I should then be able to restructure this to have the > > follow-up > > tasks required by both different forms of compilation to be done > > starting > > with this intermediate result. > > > > Anyone see a problem with my approach? I'm hoping it will be possible > > to > > re=C3=BCse compilation results produced by compile() in this way withou= t > > it > > causing any conflicts... > > I have a higher-level question, which is why do you want to compile to > both/reuse results? > > I believe you ought to be able to compile a context multiple times > (provided no errors occur), so if you need both you can currently write > code like this: > > gcc_jit_context *ctxt =3D populate_ctxt (); > gcc_jit_context_compile_to_file (ctxt, > GCC_JIT_OUTPUT_KIND_ASSEMBLER, > "foo.s"); > gcc_jit_result *result =3D gcc_jit_context_compile (ctxt); > > albeit with the drawback that it's duplicating work. Is it the > duplicated work that you're trying to avoid? > > Do you have ideas about what the API you'd want would look like? > > [...snip...] > > Hope this is helpful > Dave > > > > > > > ---------- Forwarded message ---------- > > > From: Joshua Saxby > > > To: jit@gcc.gnu.org > > > Cc: > > > Bcc: > > > Date: Sat, 10 Jun 2023 00:04:43 +0100 > > > Subject: Modifying the jit compiler API? > > > Currently the JIT has two functions allowing you to compile a > > > context > > > either to memory or to file. > > > > > > But what if you want to compile to both? There doesn't seem to be > > > any way > > > to do this except by calling both functions separately which I > > > believe will > > > effectively be two separate compilations... > > > > > > Presumably, it should be possible to modify this part of the API to > > > compile > > > to some form of intermediate representation of the work that is > > > common to > > > both kinds of compilation, and then turn that into code in memory > > > and in > > > file, respectively. > > > > > > Anyone got any pointers for me on where in the code would be the > > > best place > > > for me to modify to dupport this? I did take a look in the > > > implementation > > > code of JIT sone weeks ago and remember seeing lots of complicated > > > stuff > > > regarding recordings and replays that looked relevant... > > > > > > A slighty simpler alteration I am also interested in making is > > > allowing > > > compile to file to compile to a buffer in memory as well as a file > > > (I > > > suppose it could be termed "compile to binary"). Maybe I should > > > start with > > > that... > > > > > > > > >