From: Robert Zakrzewski <[email protected]>

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 <[email protected]>
---
 gcc/jit/jit-recording.h                      |  3 +-
 gcc/jit/libgccjit.cc                         |  3 +-
 gcc/testsuite/jit.dg/all-non-failing-tests.h |  7 ++
 gcc/testsuite/jit.dg/test-comparison.c       | 78 ++++++++++++++++++++
 4 files changed, 88 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/jit.dg/test-comparison.c

diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h
index 08de684653aa..1d05899b3d9c 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 3fc96fb989a2..befe23c2ebde 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 fe9ad1da3f4d..c97eb1808dd7 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -135,6 +135,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
diff --git a/gcc/testsuite/jit.dg/test-comparison.c 
b/gcc/testsuite/jit.dg/test-comparison.c
new file mode 100644
index 000000000000..36d07f3b2079
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-comparison.c
@@ -0,0 +1,78 @@
+#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_target_info* target_info = gcc_jit_context_get_target_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);
+}
-- 
2.54.0

Reply via email to