From 9236998f5ad3156ebe39e97c03d1a28ce80dd95a Mon Sep 17 00:00:00 2001 From: Antoni Boucher Date: Thu, 9 Jun 2022 20:57:41 -0400 Subject: [PATCH] libgccjit Fix a RTL bug for libgccjit This fixes a 'unrecognizable insn' error when generating some code using target-specific builtins. gcc/ChangeLog: PR jit/112576 * emit-rtl.cc (init_emit_once): Do not initialize const_int_rtx if already initialized. gcc/testsuite: PR jit/112576 * jit.dg/all-non-failing-tests.h: Mention test-rtl-bug-target-builtins.c. * jit.dg/test-rtl-bug-target-builtins.c: New test. --- gcc/emit-rtl.cc | 9 +- gcc/testsuite/jit.dg/all-non-failing-tests.h | 3 + .../jit.dg/test-rtl-bug-target-builtins.c | 87 +++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/jit.dg/test-rtl-bug-target-builtins.c diff --git a/gcc/emit-rtl.cc b/gcc/emit-rtl.cc index 84b6833225e..a18ac1de98c 100644 --- a/gcc/emit-rtl.cc +++ b/gcc/emit-rtl.cc @@ -6216,8 +6216,13 @@ init_emit_once (void) /* Don't use gen_rtx_CONST_INT here since gen_rtx_CONST_INT in this case tries to use these variables. */ for (i = - MAX_SAVED_CONST_INT; i <= MAX_SAVED_CONST_INT; i++) - const_int_rtx[i + MAX_SAVED_CONST_INT] = - gen_rtx_raw_CONST_INT (VOIDmode, (HOST_WIDE_INT) i); + { + // Do not initialize twice the constants because there are used elsewhere + // and libgccjit execute this function twice. + if (const_int_rtx[i + MAX_SAVED_CONST_INT] == NULL) + const_int_rtx[i + MAX_SAVED_CONST_INT] + = gen_rtx_raw_CONST_INT (VOIDmode, (HOST_WIDE_INT) i); + } if (STORE_FLAG_VALUE >= - MAX_SAVED_CONST_INT && STORE_FLAG_VALUE <= MAX_SAVED_CONST_INT) diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h index e762563f9bd..3da2e285b80 100644 --- a/gcc/testsuite/jit.dg/all-non-failing-tests.h +++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h @@ -322,6 +322,9 @@ /* test-setting-alignment.c: This can't be in the testcases array as it is target-specific. */ +/* test-rtl-bug-target-builtins.c: This can't be in the testcases array as it + is target-specific. */ + /* test-string-literal.c */ #define create_code create_code_string_literal #define verify_code verify_code_string_literal diff --git a/gcc/testsuite/jit.dg/test-rtl-bug-target-builtins.c b/gcc/testsuite/jit.dg/test-rtl-bug-target-builtins.c new file mode 100644 index 00000000000..d4a686271f9 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-rtl-bug-target-builtins.c @@ -0,0 +1,87 @@ +/* { dg-do compile { target x86_64-*-* } } */ + +#include +#include + +#include "libgccjit.h" + +#define TEST_PROVIDES_MAIN +#include "harness.h" + +void +create_code (gcc_jit_context *ctxt, void *user_data) +{ + gcc_jit_context_add_command_line_option (ctxt, "-mavx512vl"); + gcc_jit_function *builtin = + gcc_jit_context_get_target_builtin_function (ctxt, + "__builtin_ia32_cvtpd2udq128_mask"); + + gcc_jit_type *u8_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_UINT8_T); + gcc_jit_type *double_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE); + gcc_jit_type *v2df = gcc_jit_type_get_vector (double_type, 2); + gcc_jit_type *int_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *v4si = gcc_jit_type_get_vector (int_type, 4); + + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, NULL, + GCC_JIT_FUNCTION_EXPORTED, + v4si, + "epu32", + 0, NULL, + 0); + gcc_jit_block *block = gcc_jit_function_new_block (func, NULL); + gcc_jit_lvalue *var1 = gcc_jit_function_new_local (func, NULL, v2df, "var1"); + gcc_jit_lvalue *var2 = gcc_jit_function_new_local (func, NULL, v4si, "var2"); + gcc_jit_rvalue *args[3] = { + gcc_jit_lvalue_as_rvalue (var1), + gcc_jit_lvalue_as_rvalue (var2), + gcc_jit_context_zero (ctxt, u8_type), + }; + gcc_jit_rvalue *call = gcc_jit_context_new_call (ctxt, NULL, builtin, 3, args); + gcc_jit_block_end_with_return (block, NULL, call); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_NON_NULL (result); +} + +int +main (int argc, char **argv) +{ + /* This is the same as the main provided by harness.h, but it first create a dummy context and compile + in order to add the target builtins to libgccjit's internal state. */ + gcc_jit_context *ctxt; + ctxt = gcc_jit_context_acquire (); + if (!ctxt) + { + fail ("gcc_jit_context_acquire failed"); + return -1; + } + gcc_jit_result *result; + result = gcc_jit_context_compile (ctxt); + gcc_jit_result_release (result); + gcc_jit_context_release (ctxt); + + int i; + + for (i = 1; i <= 5; i++) + { + snprintf (test, sizeof (test), + "%s iteration %d of %d", + extract_progname (argv[0]), + i, 5); + + //printf ("ITERATION %d\n", i); + test_jit (argv[0], NULL); + //printf ("\n"); + } + + totals (); + + return 0; +} -- 2.42.1