From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x332.google.com (mail-wm1-x332.google.com [IPv6:2a00:1450:4864:20::332]) by sourceware.org (Postfix) with ESMTPS id A80BF3858D35; Thu, 17 Aug 2023 15:59:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A80BF3858D35 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-wm1-x332.google.com with SMTP id 5b1f17b1804b1-3fe426b8583so220865e9.2; Thu, 17 Aug 2023 08:59:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692287952; x=1692892752; 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=RWeaGmDy5RT0Fb28ZDT/IJI8JxzIulAdqxM6Gwguh2k=; b=O8gQmYfUpQsCTCLZGdrUjkPfgt2AusKo8YiQI8pBqcI2EbR3N/rzGmvlyp0kXsl/ze VOyDHhei2CceJDPVw5ieuiqvMmpJEYXqVv5OcSw5T4DSMq+S/WCqnZrqZjBfxkRZ/N5p 6tc5dJ86pAO2vMD4kATScqM9zaRk/p1vDiGkdB6LVs5enbsocDjmXlzhoBTi7mRgVy22 EvkaxuPPTNWujs3jMn5QwKWOfaUyLuTyQfXqmLSgkamzXDsOYGNTEX6V/fSCKI6Vuwpu YZGGoZTxCmWiGm47r7x/VDbSDX7G6UC38Oy8Pycv+aFsvi4oXHaftw7KNEqeeHz2v9no I4IA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692287952; x=1692892752; 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=RWeaGmDy5RT0Fb28ZDT/IJI8JxzIulAdqxM6Gwguh2k=; b=jhAbbgtmg2QP36QftubfSRcwjjBNqYiVlq6eS4eDWMiEZEgK4apFaE08RW7nBS14Ev zKarpRQugJnjwuKElMzyfkh9kVfx5e+IUByQ34mR3vifZxWvG+DRdujBOpwLmbBbg/Fl Q6Dur4q0PvnwSVS3k/gWo7fOM7EDc9/bUv83AArNWxLTw0gns60xKz/IpOGLqg15MS4b TM9GUG89Lh5c8ph9XNml3FYmruBwrAO+qFRkvlnuo+WhQ3RgVuLsYr/4MzJCLJzepce2 YjoBUMcWL2wjRPmBUEvd4OAg8W4tYCVLOxar1/TsyFlodv/TOtzhwSW6VhnD9fvSG6ZA oIsA== X-Gm-Message-State: AOJu0YwovBitQYA4QxvThNPZfv8IxybOW2XsjsooNJ1h5EcjC8ZrtBp9 jdzaRsOGSH/qdVjFdeb2pyT1ovjStM0wUdl7wKs= X-Google-Smtp-Source: AGHT+IHPKNh3gU0qIGk0p3zRLAB8N2LzqtWmLNaTxVDim+e+Pxt0rRonDpUMiNcMzCJhrdHemDacKtc2twbwU0WgTI8= X-Received: by 2002:a05:600c:1d08:b0:3fe:1cac:37d5 with SMTP id l8-20020a05600c1d0800b003fe1cac37d5mr141836wms.4.1692287951996; Thu, 17 Aug 2023 08:59:11 -0700 (PDT) MIME-Version: 1.0 References: <059ffebd230df2dbbac3f138ec85016bb7a7306a.camel@redhat.com> In-Reply-To: From: Guillaume Gomez Date: Thu, 17 Aug 2023 17:59:00 +0200 Message-ID: Subject: Re: [PATCH] libgccjit: Add support for `restrict` attribute on function parameters To: David Malcolm Cc: gcc-patches@gcc.gnu.org, jit@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.0 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP 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: Thanks for the review! Le jeu. 17 ao=C3=BBt 2023 =C3=A0 17:50, David Malcolm = a =C3=A9crit : > > On Thu, 2023-08-17 at 17:41 +0200, Guillaume Gomez wrote: > > And now I just discovered that a lot of commits from Antoni's fork > > haven't been sent upstream which is why the ABI count is so high in > > his repository. Fixed that as well. > > Thanks for the updated patch; I was about to comment on that. > > This version is good for gcc trunk. > > Dave > > > > > Le jeu. 17 ao=C3=BBt 2023 =C3=A0 17:26, Guillaume Gomez > > a =C3=A9crit : > > > > > > Antoni spot a typo I made: > > > > > > I added `LIBGCCJIT_HAVE_gcc_jit_type_get_size` instead of > > > `LIBGCCJIT_HAVE_gcc_jit_type_get_restrict`. Fixed in this patch, > > > sorry > > > for the noise. > > > > > > Le jeu. 17 ao=C3=BBt 2023 =C3=A0 11:30, Guillaume Gomez > > > a =C3=A9crit : > > > > > > > > Hi Dave, > > > > > > > > > What kind of testing has the patch had? (e.g. did you run "make > > > > > check- > > > > > jit" ? Has this been in use on real Rust code?) > > > > > > > > I tested it as Rust backend directly on this code: > > > > > > > > ``` > > > > pub fn foo(a: &mut i32, b: &mut i32, c: &i32) { > > > > *a +=3D *c; > > > > *b +=3D *c; > > > > } > > > > ``` > > > > > > > > I ran it with `rustc` (and the GCC backend) with the following > > > > flags: > > > > `-C link-args=3D-lc --emit=3Dasm -O --crate-type=3Dlib` which gave = the > > > > diff > > > > you can see in the attached file. Explanations: the diff on the > > > > right > > > > has the `__restrict__` attribute used whereas on the left it is > > > > the > > > > current version where we don't handle it. > > > > > > > > As for C testing, I used this code: > > > > > > > > ``` > > > > void t(int *__restrict__ a, int *__restrict__ b, char > > > > *__restrict__ c) { > > > > *a +=3D *c; > > > > *b +=3D *c; > > > > } > > > > ``` > > > > > > > > (without the `__restrict__` of course when I need to have a > > > > witness > > > > ASM). I attached the diff as well, this time the file with the > > > > use of > > > > `__restrict__` in on the left. I compiled with the following > > > > flags: > > > > `-S -O3`. > > > > > > > > > Please add a feature macro: > > > > > #define LIBGCCJIT_HAVE_gcc_jit_type_get_restrict > > > > > (see the similar ones in the header). > > > > > > > > I added `LIBGCCJIT_HAVE_gcc_jit_type_get_size` and extended the > > > > documentation as well to mention the ABI change. > > > > > > > > > Please add a new ABI tag (LIBGCCJIT_ABI_25 ?), rather than > > > > > adding this > > > > > to ABI_0. > > > > > > > > I added `LIBGCCJIT_ABI_34` as `LIBGCCJIT_ABI_33` was the last > > > > one. > > > > > > > > > This refers to a "cold attribute"; is this a vestige of a copy- > > > > > and- > > > > > paste from a different test case? > > > > > > > > It is a vestige indeed... Missed this one. > > > > > > > > > I see that the test scans the generated assembler. Does the > > > > > test > > > > > actually verify that restrict has an effect, or was that > > > > > another > > > > > vestige from a different test case? > > > > > > > > No, this time it's what I wanted. Please see the C diff I > > > > provided > > > > above to see that the ASM has a small diff that allowed me to > > > > confirm > > > > that the `__restrict__` attribute was correctly set. > > > > > > > > > If this test is meant to run at -O3 and thus can't be part of > > > > > test- > > > > > combination.c, please add a comment about it to > > > > > gcc/testsuite/jit.dg/all-non-failing-tests.h (in the > > > > > alphabetical > > > > > place). > > > > > > > > Below `-O3`, this ASM difference doesn't appear unfortunately. > > > > > > > > > The patch also needs to add documentation for the new > > > > > entrypoint (in > > > > > topics/types.rst), and for the new ABI tag (in > > > > > topics/compatibility.rst). > > > > > > > > Added! > > > > > > > > > Thanks again for the patch; hope the above is constructive > > > > > > > > It was incredibly useful! Thanks for taking time to writing down > > > > the > > > > explanations. > > > > > > > > The new patch is attached to this email. > > > > > > > > Cordially. > > > > > > > > Le jeu. 17 ao=C3=BBt 2023 =C3=A0 01:06, David Malcolm > > > > a =C3=A9crit : > > > > > > > > > > On Wed, 2023-08-16 at 22:06 +0200, Guillaume Gomez via Jit > > > > > wrote: > > > > > > My apologies, forgot to run the commit checkers. Here's the > > > > > > commit > > > > > > with the errors fixed. > > > > > > > > > > > > Le mer. 16 ao=C3=BBt 2023 =C3=A0 18:32, Guillaume Gomez > > > > > > a =C3=A9crit : > > > > > > > > > > > > > > Hi, > > > > > > > > > > Hi Guillaume, thanks for the patch. > > > > > > > > > > > > > > > > > > > This patch adds the possibility to specify the __restrict__ > > > > > > > attribute > > > > > > > for function parameters. It is used by the Rust GCC > > > > > > > backend. > > > > > > > > > > What kind of testing has the patch had? (e.g. did you run "make > > > > > check- > > > > > jit" ? Has this been in use on real Rust code?) > > > > > > > > > > Overall, this patch looks close to being ready, but some nits > > > > > below... > > > > > > > > > > [...] > > > > > > > > > > > diff --git a/gcc/jit/libgccjit.h b/gcc/jit/libgccjit.h > > > > > > index 60eaf39bff6..2e0d08a06d8 100644 > > > > > > --- a/gcc/jit/libgccjit.h > > > > > > +++ b/gcc/jit/libgccjit.h > > > > > > @@ -635,6 +635,10 @@ gcc_jit_type_get_const (gcc_jit_type > > > > > > *type); > > > > > > extern gcc_jit_type * > > > > > > gcc_jit_type_get_volatile (gcc_jit_type *type); > > > > > > > > > > > > +/* Given type "T", get type "restrict T". */ > > > > > > +extern gcc_jit_type * > > > > > > +gcc_jit_type_get_restrict (gcc_jit_type *type); > > > > > > + > > > > > > #define LIBGCCJIT_HAVE_SIZED_INTEGERS > > > > > > > > > > > > /* Given types LTYPE and RTYPE, return non-zero if they are > > > > > compatible. > > > > > > > > > > Please add a feature macro: > > > > > #define LIBGCCJIT_HAVE_gcc_jit_type_get_restrict > > > > > (see the similar ones in the header). > > > > > > > > > > > diff --git a/gcc/jit/libgccjit.map b/gcc/jit/libgccjit.map > > > > > > index e52de0057a5..b7289b13845 100644 > > > > > > --- a/gcc/jit/libgccjit.map > > > > > > +++ b/gcc/jit/libgccjit.map > > > > > > @@ -104,6 +104,7 @@ LIBGCCJIT_ABI_0 > > > > > > gcc_jit_type_as_object; > > > > > > gcc_jit_type_get_const; > > > > > > gcc_jit_type_get_pointer; > > > > > > + gcc_jit_type_get_restrict; > > > > > > gcc_jit_type_get_volatile; > > > > > > > > > > Please add a new ABI tag (LIBGCCJIT_ABI_25 ?), rather than > > > > > adding this > > > > > to ABI_0. > > > > > > > > > > > diff --git a/gcc/testsuite/jit.dg/test-restrict.c > > > > > b/gcc/testsuite/jit.dg/test-restrict.c > > > > > > new file mode 100644 > > > > > > index 00000000000..4c8c4407f91 > > > > > > --- /dev/null > > > > > > +++ b/gcc/testsuite/jit.dg/test-restrict.c > > > > > > @@ -0,0 +1,77 @@ > > > > > > +/* { dg-do compile { target x86_64-*-* } } */ > > > > > > + > > > > > > +#include > > > > > > +#include > > > > > > + > > > > > > +#include "libgccjit.h" > > > > > > + > > > > > > +/* We don't want set_options() in harness.h to set -O3 to > > > > > > see that > > > > > the cold > > > > > > + attribute affects the optimizations. */ > > > > > > > > > > This refers to a "cold attribute"; is this a vestige of a copy- > > > > > and- > > > > > paste from a different test case? > > > > > > > > > > I see that the test scans the generated assembler. Does the > > > > > test > > > > > actually verify that restrict has an effect, or was that > > > > > another > > > > > vestige from a different test case? > > > > > > > > > > > +#define TEST_ESCHEWS_SET_OPTIONS > > > > > > +static void set_options (gcc_jit_context *ctxt, const char > > > > > > *argv0) > > > > > > +{ > > > > > > + // Set "-O3". > > > > > > + gcc_jit_context_set_int_option(ctxt, > > > > > GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3); > > > > > > +} > > > > > > + > > > > > > +#define TEST_COMPILING_TO_FILE > > > > > > +#define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER > > > > > > +#define OUTPUT_FILENAME "output-of-test-restrict.c.s" > > > > > > +#include "harness.h" > > > > > > + > > > > > > +void > > > > > > +create_code (gcc_jit_context *ctxt, void *user_data) > > > > > > +{ > > > > > > + /* Let's try to inject the equivalent of: > > > > > > +void t(int *__restrict__ a, int *__restrict__ b, char > > > > > > *__restrict__ > > > > > c) { > > > > > > + *a +=3D *c; > > > > > > + *b +=3D *c; > > > > > > +} > > > > > > + */ > > > > > > + gcc_jit_type *int_type =3D > > > > > > + gcc_jit_context_get_type (ctxt, > > > > > > GCC_JIT_TYPE_INT); > > > > > > + gcc_jit_type *pint_type =3D > > > > > > gcc_jit_type_get_pointer(int_type); > > > > > > + gcc_jit_type *pint_restrict_type =3D > > > > > gcc_jit_type_get_restrict(pint_type); > > > > > > + > > > > > > + gcc_jit_type *void_type =3D > > > > > > + gcc_jit_context_get_type (ctxt, > > > > > > GCC_JIT_TYPE_VOID); > > > > > > + > > > > > > + gcc_jit_param *a =3D > > > > > > + gcc_jit_context_new_param (ctxt, NULL, > > > > > pint_restrict_type, "a"); > > > > > > + gcc_jit_param *b =3D > > > > > > + gcc_jit_context_new_param (ctxt, NULL, > > > > > pint_restrict_type, "b"); > > > > > > + gcc_jit_param *c =3D > > > > > > + gcc_jit_context_new_param (ctxt, NULL, > > > > > pint_restrict_type, "c"); > > > > > > + gcc_jit_param *params[3] =3D {a, b, c}; > > > > > > + > > > > > > + gcc_jit_function *func_t =3D > > > > > > + gcc_jit_context_new_function (ctxt, NULL, > > > > > > + > > > > > > GCC_JIT_FUNCTION_EXPORTED, > > > > > > + void_type, > > > > > > + "t", > > > > > > + 3, params, > > > > > > + 0); > > > > > > + > > > > > > + gcc_jit_block *block =3D gcc_jit_function_new_block > > > > > > (func_t, > > > > > NULL); > > > > > > + > > > > > > + /* *a +=3D *c; */ > > > > > > + gcc_jit_block_add_assignment_op ( > > > > > > + block, NULL, > > > > > > + gcc_jit_rvalue_dereference > > > > > > (gcc_jit_param_as_rvalue > > > > > (a), NULL), > > > > > > + GCC_JIT_BINARY_OP_PLUS, > > > > > > + gcc_jit_lvalue_as_rvalue ( > > > > > > + gcc_jit_rvalue_dereference > > > > > (gcc_jit_param_as_rvalue (c), NULL))); > > > > > > + /* *b +=3D *c; */ > > > > > > + gcc_jit_block_add_assignment_op ( > > > > > > + block, NULL, > > > > > > + gcc_jit_rvalue_dereference > > > > > > (gcc_jit_param_as_rvalue > > > > > (b), NULL), > > > > > > + GCC_JIT_BINARY_OP_PLUS, > > > > > > + gcc_jit_lvalue_as_rvalue ( > > > > > > + gcc_jit_rvalue_dereference > > > > > (gcc_jit_param_as_rvalue (c), NULL))); > > > > > > + > > > > > > + gcc_jit_block_end_with_void_return (block, NULL); > > > > > > +} > > > > > > + > > > > > > +/* { dg-final { jit-verify-output-file-was-created "" } } */ > > > > > > +/* { dg-final { jit-verify-assembler-output "addl %eax, > > > > > > (%rdi) > > > > > > + addl %eax, (%rsi)" } } */ > > > > > > -- > > > > > > 2.34.1 > > > > > > > > > > > > > > > > If this test is meant to run at -O3 and thus can't be part of > > > > > test- > > > > > combination.c, please add a comment about it to > > > > > gcc/testsuite/jit.dg/all-non-failing-tests.h (in the > > > > > alphabetical > > > > > place). > > > > > > > > > > The patch also needs to add documentation for the new > > > > > entrypoint (in > > > > > topics/types.rst), and for the new ABI tag (in > > > > > topics/compatibility.rst). > > > > > > > > > > > > > > > Thanks again for the patch; hope the above is constructive > > > > > Dave > > > > > >