]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libgccjit: Make is_same_type_as() support floating point types
authorRobert Zakrzewski <robert.zakrzewski1@stellantis.com>
Mon, 29 Apr 2024 10:19:28 +0000 (12:19 +0200)
committerAntoni Boucher <bouanto@zoho.com>
Wed, 17 Jun 2026 14:29:57 +0000 (10:29 -0400)
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 <bouanto@zoho.com>
gcc/jit/jit-recording.h
gcc/jit/libgccjit.cc
gcc/testsuite/jit.dg/all-non-failing-tests.h
gcc/testsuite/jit.dg/test-comparison.c [new file with mode: 0644]

index f5dc500da00895c89400ce7bf8e27aa20d5121a0..2a5f23a058a1d0f9a0f9d51ba3675195cf8e9452 100644 (file)
@@ -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 ())
     {
index ff44fd4f7960a3db684bcc88fffea7e5254dfb8c..ad47547135554411ef37af9622b189d9d3716458 100644 (file)
@@ -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)",
index 1b4dbec3fcb9892b24eb61335e5357c620b43545..e8f8463e817ebb7037b5cb88ef6e70b89e2c1cac 100644 (file)
 #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 (file)
index 0000000..a7c4182
--- /dev/null
@@ -0,0 +1,79 @@
+#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:
+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);
+}