From: Antoni Boucher <bouanto@zoho.com>
To: David Malcolm <dmalcolm@redhat.com>
Cc: Tom Tromey <tom@tromey.com>,
jit@gcc.gnu.org,
Antoni Boucher via Gcc-patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] libgccjit: Handle truncation and extension for casts [PR 95498]
Date: Tue, 25 May 2021 20:16:58 -0400 [thread overview]
Message-ID: <0f7f2bf386c29efa1403020cbb02bec815ce25fe.camel@zoho.com> (raw)
In-Reply-To: <597ecec1c91c1176f740796e20432372377aa554.camel@redhat.com>
[-- Attachment #1: Type: text/plain, Size: 5958 bytes --]
I updated the patch according to the comments by Tom Tromey.
There's one question left about your question regarding
C_MAYBE_CONST_EXPR, David:
I am not sure if we can get a C_MAYBE_CONST_EXPR from libgccjit, and it
indeed seems like it's only created in c-family.
However, we do use it in libgccjit here:
https://github.com/gcc-mirror/gcc/blob/master/gcc/jit/jit-playback.c#L1180
I tried removing the condition `if (TREE_CODE (t_ret) !=
C_MAYBE_CONST_EXPR)` and all the tests of libgccjit still pass.
That code was copied from here:
https://github.com/gcc-mirror/gcc/blob/master/gcc/c/c-convert.c#L175
and might not be needed in libgccjit.
Should I just remove the condition, then?
Le jeudi 13 mai 2021 à 19:58 -0400, David Malcolm a écrit :
> On Thu, 2021-05-13 at 19:31 -0400, Antoni Boucher wrote:
> > Thanks for your answer.
> >
> > See my answers below:
> >
> > Le jeudi 13 mai 2021 à 18:13 -0400, David Malcolm a écrit :
> > > On Sat, 2021-02-20 at 17:17 -0500, Antoni Boucher via Gcc-patches
> > > wrote:
> > > > Hi.
> > > > Thanks for your feedback!
> > > >
> > >
> > > Sorry about the delay in responding.
> > >
> > > In the past I was hesitant about adding more cast support to
> > > libgccjit
> > > since I felt that the user could always just create a union to do
> > > the
> > > cast. Then I tried actually using the libgccjit API to do this,
> > > and
> > > realized how much work it adds, so I now think we do want to
> > > support
> > > casting more types.
> > >
> > >
> > > > See answers below:
> > > >
> > > > On Sat, Feb 20, 2021 at 11:20:35AM -0700, Tom Tromey wrote:
> > > > > > > > > > "Antoni" == Antoni Boucher via Gcc-patches <
> > > > > > > > > > gcc-patches@gcc.gnu.org> writes:
> > > > >
> > > > > Antoni> gcc/jit/
> > > > > Antoni> PR target/95498
> > > > > Antoni> * jit-playback.c: Add support to handle
> > > > > truncation
> > > > > and extension
> > > > > Antoni> in the convert function.
> > > > >
> > > > > Antoni> + switch (dst_code)
> > > > > Antoni> + {
> > > > > Antoni> + case INTEGER_TYPE:
> > > > > Antoni> + case ENUMERAL_TYPE:
> > > > > Antoni> + t_ret = convert_to_integer (dst_type, expr);
> > > > > Antoni> + goto maybe_fold;
> > > > > Antoni> +
> > > > > Antoni> + default:
> > > > > Antoni> + gcc_assert (gcc::jit::active_playback_ctxt);
> > > > > Antoni> + gcc::jit::active_playback_ctxt->add_error (NULL,
> > > > > "unhandled conversion");
> > > > > Antoni> + fprintf (stderr, "input expression:\n");
> > > > > Antoni> + debug_tree (expr);
> > > > > Antoni> + fprintf (stderr, "requested type:\n");
> > > > > Antoni> + debug_tree (dst_type);
> > > > > Antoni> + return error_mark_node;
> > > > > Antoni> +
> > > > > Antoni> + maybe_fold:
> > > > > Antoni> + if (TREE_CODE (t_ret) != C_MAYBE_CONST_EXPR)
> > >
> > > Do we even get C_MAYBE_CONST_EXPR in libgccjit? That tree code is
> > > defined in c-family/c-common.def; how can nodes of that kind be
> > > created
> > > outside of the c-family?
> >
> > I am not sure, but that seems like it's only created in c-family
> > indeed.
> > However, we do use it in libgccjit here:
> >
> > https://github.com/gcc-mirror/gcc/blob/master/gcc/jit/jit-playback.c#L1180
> >
> > >
> > > > > Antoni> + t_ret = fold (t_ret);
> > > > > Antoni> + return t_ret;
> > > > >
> > > > > It seems weird to have a single 'goto' to maybe_fold,
> > > > > especially
> > > > > inside
> > > > > a switch like this.
> > > > >
> > > > > If you think the maybe_fold code won't be reused, then it
> > > > > should
> > > > > just
> > > > > be
> > > > > hoisted up and the 'goto' removed.
> > > >
> > > > This actually depends on how the support for cast between
> > > > integers
> > > > and
> > > > pointers will be implemented (see below).
> > > > If we will support truncating pointers (does that even make
> > > > sense?
> > > > and
> > > > I
> > > > guess we cannot extend a pointer unless we add the support for
> > > > uint128_t), that label will be reused for that case.
> > > > Otherwise, it might not be reused.
> > > >
> > > > So, please tell me which option to choose and I'll update my
> > > > patch.
> > >
> > > FWIW I don't think we'll want to support truncating or extending
> > > pointers.
> >
> > Ok, but do you think we'll want to support casts between integers and
> > pointers?
>
> Yes, though we probably want to reject truncating a pointer into a
> smaller integer type.
>
> > I opened an issue about this
> > (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95438) and would be
> > willing to do a patch for it eventually.
> >
> > > >
> > > > > On the other hand, if the maybe_fold code might be reused for
> > > > > some
> > > > > other
> > > > > case, then I suppose I would have the case end with 'break' and
> > > > > then
> > > > > have this code outside the switch.
> > > > >
> > > > >
> > > > > In another message, you wrote:
> > > > >
> > > > > Antoni> For your question, the current code already works with
> > > > > boolean and
> > > > > Antoni> reals and casts between integers and pointers is
> > > > > currently
> > > > > not
> > > > > Antoni> supported.
> > > > >
> > > > > I am curious why this wasn't supported. It seems like
> > > > > something
> > > > > that
> > > > > one might want to do.
> > > >
> > > > I have no idea as this is my first contribution to gcc.
> > > > But this would be indeed very useful and I opened an issue about
> > > > this:
> > > > https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95438
> > > >
> > > > > thanks,
> > > > > Tom
> > > >
> > > > Thanks!
> > > >
> > >
> > >
> >
> >
>
>
[-- Attachment #2: 0001-This-patch-handles-truncation-and-extension-for-cast.patch --]
[-- Type: text/x-patch, Size: 5367 bytes --]
From 964a972b25d7e3a6238046403bbd9cb336724980 Mon Sep 17 00:00:00 2001
From: Antoni Boucher <bouanto@zoho.com>
Date: Sun, 5 Jul 2020 19:07:30 -0400
Subject: [PATCH] This patch handles truncation and extension for casts in jit.
2020-07-12 Antoni Boucher <bouanto@zoho.com>
gcc/jit/
PR target/95498
* jit-playback.c (convert): Add support to handle truncation and
extension in the convert function.
gcc/testsuite/
PR target/95498
* jit.dg/all-non-failing-tests.h: New test.
* jit.dg/test-cast.c: New test.
Signed-off-by: Antoni Boucher <bouanto@zoho.com>
---
gcc/jit/jit-playback.c | 36 +++++++----
gcc/testsuite/jit.dg/all-non-failing-tests.h | 10 +++
gcc/testsuite/jit.dg/test-cast.c | 66 ++++++++++++++++++++
3 files changed, 101 insertions(+), 11 deletions(-)
create mode 100644 gcc/testsuite/jit.dg/test-cast.c
diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c
index 4fac64dcab7..ce990bd18c6 100644
--- a/gcc/jit/jit-playback.c
+++ b/gcc/jit/jit-playback.c
@@ -61,22 +61,36 @@ along with GCC; see the file COPYING3. If not see
/* gcc::jit::playback::context::build_cast uses the convert.h API,
which in turn requires the frontend to provide a "convert"
- function, apparently as a fallback.
-
- Hence we provide this dummy one, with the requirement that any casts
- are handled before reaching this. */
+ function, apparently as a fallback for casts that can be simplified
+ (truncation, extension). */
extern tree convert (tree type, tree expr);
tree
convert (tree dst_type, tree expr)
{
- gcc_assert (gcc::jit::active_playback_ctxt);
- gcc::jit::active_playback_ctxt->add_error (NULL, "unhandled conversion");
- fprintf (stderr, "input expression:\n");
- debug_tree (expr);
- fprintf (stderr, "requested type:\n");
- debug_tree (dst_type);
- return error_mark_node;
+ tree t_ret = NULL;
+ t_ret = targetm.convert_to_type (dst_type, expr);
+ if (t_ret)
+ return t_ret;
+ enum tree_code dst_code = TREE_CODE (dst_type);
+ switch (dst_code)
+ {
+ case INTEGER_TYPE:
+ case ENUMERAL_TYPE:
+ t_ret = convert_to_integer (dst_type, expr);
+ if (TREE_CODE (t_ret) != C_MAYBE_CONST_EXPR)
+ t_ret = fold (t_ret);
+ return t_ret;
+
+ default:
+ gcc_assert (gcc::jit::active_playback_ctxt);
+ gcc::jit::active_playback_ctxt->add_error (NULL, "unhandled conversion");
+ fprintf (stderr, "input expression:\n");
+ debug_tree (expr);
+ fprintf (stderr, "requested type:\n");
+ debug_tree (dst_type);
+ return error_mark_node;
+ }
}
namespace gcc {
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index 4202eb7798b..84ef54a0386 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -98,6 +98,13 @@
#undef create_code
#undef verify_code
+/* test-cast.c */
+#define create_code create_code_cast
+#define verify_code verify_code_cast
+#include "test-cast.c"
+#undef create_code
+#undef verify_code
+
/* test-compound-assignment.c */
#define create_code create_code_compound_assignment
#define verify_code verify_code_compound_assignment
@@ -361,6 +368,9 @@ const struct testcase testcases[] = {
{"calling_internal_function",
create_code_calling_internal_function,
verify_code_calling_internal_function},
+ {"cast",
+ create_code_cast,
+ verify_code_cast},
{"compound_assignment",
create_code_compound_assignment,
verify_code_compound_assignment},
diff --git a/gcc/testsuite/jit.dg/test-cast.c b/gcc/testsuite/jit.dg/test-cast.c
new file mode 100644
index 00000000000..2b1e385ae40
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-cast.c
@@ -0,0 +1,66 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "libgccjit.h"
+
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+ /* Let's try to inject the equivalent of:
+char
+my_casts (int x)
+{
+ return (char)(long) x;
+}
+ */
+ gcc_jit_type *int_type =
+ gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+ gcc_jit_type *long_type =
+ gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_LONG);
+ gcc_jit_type *return_type =
+ gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_CHAR);
+
+ gcc_jit_param *x =
+ gcc_jit_context_new_param (
+ ctxt,
+ NULL,
+ int_type, "x");
+ gcc_jit_param *params[1] = {x};
+ gcc_jit_function *func =
+ gcc_jit_context_new_function (ctxt,
+ NULL,
+ GCC_JIT_FUNCTION_EXPORTED,
+ return_type,
+ "my_casts",
+ 1, params, 0);
+
+ gcc_jit_block *initial =
+ gcc_jit_function_new_block (func, "initial");
+
+ gcc_jit_block_end_with_return(initial, NULL,
+ gcc_jit_context_new_cast(ctxt,
+ NULL,
+ gcc_jit_context_new_cast(ctxt,
+ NULL,
+ gcc_jit_param_as_rvalue(x),
+ long_type
+ ),
+ return_type
+ ));
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+ typedef int (*my_casts_fn_type) (int);
+ CHECK_NON_NULL (result);
+ my_casts_fn_type my_casts =
+ (my_casts_fn_type)gcc_jit_result_get_code (result, "my_casts");
+ CHECK_NON_NULL (my_casts);
+ char val = my_casts (10);
+ note ("my_casts returned: %d", val);
+ CHECK_VALUE (val, 10);
+}
--
2.31.1
next prev parent reply other threads:[~2021-05-26 0:17 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-07-13 0:30 Antoni Boucher
2020-07-14 21:15 ` David Malcolm
2020-07-21 21:29 ` Andrea Corallo
2020-10-15 19:00 ` Antoni Boucher
2021-02-13 1:35 ` Antoni Boucher
2021-02-20 18:20 ` Tom Tromey
2021-02-20 22:17 ` Antoni Boucher
2021-05-13 8:34 ` Martin Liška
2021-05-13 22:13 ` David Malcolm
2021-05-13 23:31 ` Antoni Boucher
2021-05-13 23:58 ` David Malcolm
2021-05-26 0:16 ` Antoni Boucher [this message]
2021-05-27 22:21 ` David Malcolm
2021-05-28 1:22 ` Antoni Boucher
2021-06-11 17:49 ` David Malcolm
2021-06-18 20:42 ` Antoni Boucher
2021-06-18 20:54 ` David Malcolm
2021-06-18 21:11 ` Antoni Boucher
2021-06-19 9:08 ` Bernhard Reutner-Fischer
2021-07-06 13:00 ` Antoni Boucher
2021-07-08 20:44 ` David Malcolm
2021-07-08 21:28 ` Antoni Boucher
2021-07-08 21:34 ` David Malcolm
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0f7f2bf386c29efa1403020cbb02bec815ce25fe.camel@zoho.com \
--to=bouanto@zoho.com \
--cc=dmalcolm@redhat.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=jit@gcc.gnu.org \
--cc=tom@tromey.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).