From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from sender4-pp-o91.zoho.com (sender4-pp-o91.zoho.com [136.143.188.91]) by sourceware.org (Postfix) with ESMTPS id D7B04385DC1A; Mon, 13 Jul 2020 00:30:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D7B04385DC1A ARC-Seal: i=1; a=rsa-sha256; t=1594600204; cv=none; d=zohomail.com; s=zohoarc; b=QChQ7iadeAfbMDKn0C0dTWH+2SEMqX75NRhSTWsDSYZg6Y63+wt+QGEn1cAcB2RMSdbOOXJ65EbhsiTf/ZaFItkyT2sPCNVqIZSJuT12TKuTqLWLDMhOZ7/ESQh7c6iVCxFMiitGBtmwY5kGHfwSG+bwyCU3DY7nULmnSgUtE2M= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594600204; h=Content-Type:Date:From:MIME-Version:Message-ID:Subject:To; bh=tPGHqT9Gqf+H/iiIa+I0WQcmf4lW4O331LMk8mz0Boo=; b=E4pwO+aSydHAsCffUXs7kmS/aSC5UjrCE5tr19121czXUzwWkVheUh+UUlZTWXxLrmT8lf0G0Wi2LCRRy7EUveobvQjSRC9zWzAjhMaF62YEFKSKhrq4qeFAUodz8+IheCrbzXYmJmw4kcSXmbPYgu6eGoMZoNfr3BZLdVs265c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass header.i=zoho.com; spf=pass smtp.mailfrom=bouanto@zoho.com; dmarc=pass header.from= header.from= Received: from localhost (38.87.1.99 [38.87.1.99]) by mx.zohomail.com with SMTPS id 159460020386432.679641556469164; Sun, 12 Jul 2020 17:30:03 -0700 (PDT) Date: Sun, 12 Jul 2020 20:30:02 -0400 From: Antoni Boucher To: gcc-patches@gcc.gnu.org, jit@gcc.gnu.org Subject: [PATCH] libgccjit: Handle truncation and extension for casts [PR 95498] Message-ID: <20200713003002.bs5hwv4gav6ml5rt@bouanto-laptop.localdomain> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ncfilj4oja67zngm" Content-Disposition: inline X-Zoho-Virus-Status: 1 X-ZohoMailClient: External X-Spam-Status: No, score=-8.6 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, RCVD_IN_MSPIKE_H4, 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, 13 Jul 2020 00:30:09 -0000 --ncfilj4oja67zngm Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline Hello. As mentioned in bug 95498, some conversions do not work. After investigation, it turns out that it's caused by multiple casts on an expression where it should do a truncation/extension. I added a testcase, but for some reasons, the tests only pass when ran via `./testsuite/jit/test-cast.c.exe`, not when ran via `make check-jit RUNTESTFLAGS="-v -v -v jit.exp=test-cast.c"`. Furthermore, some other tests failed, but they also fail on master. Also, I was under the impression that adding `STRIP_TYPE_NOPS (t_expr);` in `playback::context::build_cast` would be a better fix for this, but that doesn't fix the issue. Am I missing something? It's my first contribution to gcc, so I'd need help for fixing the tests and also a confirmation that this is the best way to fix this issue. Thanks. --ncfilj4oja67zngm Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="0001-This-patch-handles-truncation-and-extension-for-cast.patch" >From 140333077a4c5f8ce80f7e5716eecc90781594fa Mon Sep 17 00:00:00 2001 From: Antoni Boucher 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 gcc/jit/ PR target/95498 * jit-playback.c: 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. --- gcc/jit/jit-playback.c | 39 ++++++++---- gcc/testsuite/jit.dg/all-non-failing-tests.h | 10 +++ gcc/testsuite/jit.dg/test-cast.c | 66 ++++++++++++++++++++ 3 files changed, 104 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 0fddf04da87..4f4a1080c36 100644 --- a/gcc/jit/jit-playback.c +++ b/gcc/jit/jit-playback.c @@ -61,22 +61,39 @@ 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); + goto maybe_fold; + + 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; + + maybe_fold: + if (TREE_CODE (t_ret) != C_MAYBE_CONST_EXPR) + t_ret = fold (t_ret); + return t_ret; + } } 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 632ab8cfb2e..a1481699b16 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 @@ -354,6 +361,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 +#include +#include + +#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.27.0 --ncfilj4oja67zngm--