From: Robert Zakrzewski Date: Mon, 29 Apr 2024 10:19:28 +0000 (+0200) Subject: libgccjit: Make is_same_type_as() support floating point types X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d45e81a30ab2eec1bf5b24d356bbffd36b17c32a;p=thirdparty%2Fgcc.git libgccjit: Make is_same_type_as() support floating point types gcc/jit/ChangeLog: * jit-recording.h: Fix float comparison. * libgccjit.cc: Fix type check in gcc_jit_context_new_comparison. gcc/testsuite/ChangeLog: * jit.dg/all-non-failing-tests.h: Mention new test. * jit.dg/test-comparison.c: New test. Co-authored-by: Antoni Boucher --- diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h index f5dc500da00..2a5f23a058a 100644 --- a/gcc/jit/jit-recording.h +++ b/gcc/jit/jit-recording.h @@ -653,8 +653,7 @@ public: virtual bool is_same_type_as (type *other) { - if (is_int () - && other->is_int () + if (((is_int () && other->is_int ()) || (is_float () && other->is_float ())) && get_size () == other->get_size () && is_signed () == other->is_signed ()) { diff --git a/gcc/jit/libgccjit.cc b/gcc/jit/libgccjit.cc index ff44fd4f796..ad475471355 100644 --- a/gcc/jit/libgccjit.cc +++ b/gcc/jit/libgccjit.cc @@ -2306,7 +2306,8 @@ gcc_jit_context_new_comparison (gcc_jit_context *ctxt, RETURN_NULL_IF_FAIL (a, ctxt, loc, "NULL a"); RETURN_NULL_IF_FAIL (b, ctxt, loc, "NULL b"); RETURN_NULL_IF_FAIL_PRINTF4 ( - a->get_type ()->unqualified () == b->get_type ()->unqualified (), + compatible_types (a->get_type ()->unqualified (), + b->get_type ()->unqualified ()), ctxt, loc, "mismatching types for comparison:" " a: %s (type: %s) b: %s (type: %s)", diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h b/gcc/testsuite/jit.dg/all-non-failing-tests.h index 1b4dbec3fcb..e8f8463e817 100644 --- a/gcc/testsuite/jit.dg/all-non-failing-tests.h +++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h @@ -138,6 +138,13 @@ #undef create_code #undef verify_code +/* test-comparison.c */ +#define create_code create_code_comparison +#define verify_code verify_code_comparison +#include "test-comparison.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 @@ -558,6 +565,9 @@ const struct testcase testcases[] = { {"cast", create_code_cast, verify_code_cast}, + {"comparison", + create_code_comparison, + verify_code_comparison}, {"compound_assignment", create_code_compound_assignment, verify_code_compound_assignment}, diff --git a/gcc/testsuite/jit.dg/test-comparison.c b/gcc/testsuite/jit.dg/test-comparison.c new file mode 100644 index 00000000000..a7c41826a16 --- /dev/null +++ b/gcc/testsuite/jit.dg/test-comparison.c @@ -0,0 +1,79 @@ +#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: +int +func () +{ + _Float32 float32_val = 2.3; + float float = 4.5; + if (float == float32_val) + return 1; + return 0; +} + */ + gcc_jit_type *int_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT); + gcc_jit_type *float_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT); + gcc_jit_type *float32_type; + + gcc_jit_context *info_ctxt = gcc_jit_context_acquire (); + gcc_jit_target_info* target_info = gcc_jit_context_get_target_info (info_ctxt); + if (gcc_jit_target_info_supports_target_dependent_type (target_info, + GCC_JIT_TYPE_FLOAT32)) + { + gcc_jit_type *sized_float_type = + gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT32); + float32_type = gcc_jit_type_get_aligned (sized_float_type, 4); + } + else + float32_type = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_FLOAT); + + gcc_jit_function *func = + gcc_jit_context_new_function (ctxt, + NULL, + GCC_JIT_FUNCTION_EXPORTED, + int_type, + "func", + 0, NULL, 0); + + gcc_jit_block *initial = + gcc_jit_function_new_block (func, "initial"); + gcc_jit_block *if_block = + gcc_jit_function_new_block (func, "if"); + gcc_jit_block *else_block = + gcc_jit_function_new_block (func, "else"); + + gcc_jit_rvalue *zero = gcc_jit_context_zero (ctxt, int_type); + gcc_jit_rvalue *one = gcc_jit_context_one (ctxt, int_type); + + gcc_jit_rvalue *float32_val = + gcc_jit_context_new_rvalue_from_double (ctxt, float32_type, 2.3); + gcc_jit_rvalue *float_val = + gcc_jit_context_new_rvalue_from_double (ctxt, float_type, 4.5); + + gcc_jit_rvalue *comparison = gcc_jit_context_new_comparison (ctxt, + NULL, GCC_JIT_COMPARISON_EQ, float_val, float32_val); + + gcc_jit_block_end_with_conditional (initial, + NULL, comparison, if_block, else_block); + + gcc_jit_block_end_with_return(if_block, NULL, one); + + gcc_jit_block_end_with_return(else_block, NULL, zero); +} + +void +verify_code (gcc_jit_context *ctxt, gcc_jit_result *result) +{ + CHECK_NON_NULL (result); +}