]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libgccjit: Add convert vector
authorAntoni Boucher <bouanto@zoho.com>
Sat, 14 May 2022 21:24:29 +0000 (17:24 -0400)
committerAntoni Boucher <bouanto@zoho.com>
Mon, 4 Nov 2024 19:58:14 +0000 (14:58 -0500)
gcc/jit/ChangeLog:

* docs/topics/compatibility.rst (LIBGCCJIT_ABI_30): New ABI tag.
* docs/topics/expressions.rst: Document gcc_jit_context_convert_vector.
* jit-playback.cc (convert_vector): New method.
* jit-playback.h: New method.
* jit-recording.cc (recording::context::new_convert_vector,
recording::convert_vector::replay_into,
recording::convert_vector::visit_children,
recording::convert_vector::make_debug_string,
recording::convert_vector::write_reproducer): New methods.
* jit-recording.h (class convert_vector): New class.
(context::new_convert_vector): New method.
* libgccjit.cc (gcc_jit_context_convert_vector): New function.
* libgccjit.h (gcc_jit_context_convert_vector): New function.
* libgccjit.map: New function.

gcc/testsuite/ChangeLog:

* jit.dg/all-non-failing-tests.h: New test.
* jit.dg/test-convert-vector.c: New test.

gcc/jit/docs/topics/compatibility.rst
gcc/jit/docs/topics/expressions.rst
gcc/jit/jit-playback.cc
gcc/jit/jit-playback.h
gcc/jit/jit-recording.cc
gcc/jit/jit-recording.h
gcc/jit/libgccjit.cc
gcc/jit/libgccjit.h
gcc/jit/libgccjit.map
gcc/testsuite/jit.dg/all-non-failing-tests.h
gcc/testsuite/jit.dg/test-convert-vector.c [new file with mode: 0644]

index 96adc03fed4d2c15460c1f070ef5f5f143065dbe..33fa763d0213e05a9c9533acf4bf9cca70d1f5fd 100644 (file)
@@ -411,3 +411,10 @@ on functions and variables:
 --------------------
 ``LIBGCCJIT_ABI_29`` covers the addition of
 :func:`gcc_jit_global_set_readonly`
+
+.. _LIBGCCJIT_ABI_30:
+
+``LIBGCCJIT_ABI_30``
+--------------------
+``LIBGCCJIT_ABI_30`` covers the addition of
+:func:`gcc_jit_context_convert_vector`
index 2aabbb82a6bcfa8cbe73ac772cdb0e45472ba51a..ca923e7f60c21a25b305fb2e1d223d71aa21bca1 100644 (file)
@@ -727,6 +727,25 @@ Type-coercion
 
       #ifdef LIBGCCJIT_HAVE_gcc_jit_context_new_bitcast
 
+.. function:: gcc_jit_rvalue *
+              gcc_jit_context_convert_vector (gcc_jit_context *ctxt, \
+                                              gcc_jit_location *loc, \
+                                              gcc_jit_rvalue *vector, \
+                                              gcc_jit_type *type)
+
+   Given a vector rvalue, cast it to the type ``type``, doing an element-wise
+   conversion.
+
+   The number of elements in ``vector`` and ``type`` must match.
+   The ``type`` must be a vector type.
+
+   This entrypoint was added in :ref:`LIBGCCJIT_ABI_30`; you can test for
+   its presence using
+
+   .. code-block:: c
+
+      #ifdef LIBGCCJIT_HAVE_gcc_jit_context_convert_vector
+
 Lvalues
 -------
 
index f0f01648ac19786b99b5c8ca5e301c44c9683f8c..e32e837f2feab3c522d7614d55789213648fb9ef 100644 (file)
@@ -1694,6 +1694,34 @@ new_array_access (location *loc,
     }
 }
 
+/* Construct a playback::rvalue instance (wrapping a tree) for a
+   vector conversion.  */
+
+playback::rvalue *
+playback::context::
+convert_vector (location *loc,
+                  rvalue *vector,
+                  type *type)
+{
+  gcc_assert (vector);
+  gcc_assert (type);
+
+  /* For comparison, see:
+       c/c-common.cc: c_build_vec_convert
+  */
+
+  tree t_vector = vector->as_tree ();
+
+  tree t_result =
+    build_call_expr_internal_loc (UNKNOWN_LOCATION, IFN_VEC_CONVERT,
+      type->as_tree (), 1, t_vector);
+
+  if (loc)
+    set_tree_location (t_result, loc);
+
+  return new rvalue (this, t_result);
+}
+
 /* Construct a tree for a field access.  */
 
 tree
index 212f0b2662df643353a4c3f403ea978550768d1a..97ad62ccd6a39bc789dd8de652be053a0995ca13 100644 (file)
@@ -222,6 +222,11 @@ public:
                    rvalue *ptr,
                    rvalue *index);
 
+  rvalue *
+  convert_vector (location *loc,
+                 rvalue *vector,
+                 type *type);
+
   void
   set_str_option (enum gcc_jit_str_option opt,
                  const char *value);
index c6b9b3c54bf9f057fa78c8851975a626ed0b910f..7580b625a394eafdb931e78cc75f1385283cc356 100644 (file)
@@ -1341,6 +1341,23 @@ recording::context::new_array_access (recording::location *loc,
   return result;
 }
 
+/* Create a recording::convert_vector instance and add it to this context's list
+   of mementos.
+
+   Implements the post-error-checking part of
+   gcc_jit_context_convert_vector.  */
+
+recording::rvalue *
+recording::context::new_convert_vector (recording::location *loc,
+                                   recording::rvalue *vector,
+                                   recording::type *type)
+{
+  // TODO: instead have an "internal function" memento?
+  recording::rvalue *result = new convert_vector (this, loc, vector, type);
+  record (result);
+  return result;
+}
+
 /* Create a recording::case_ instance and add it to this context's list
    of mementos.
 
@@ -6572,6 +6589,61 @@ recording::array_access::write_reproducer (reproducer &r)
           r.get_identifier_as_rvalue (m_index));
 }
 
+/* The implementation of class gcc::jit::recording::convert_vector.  */
+
+/* Implementation of pure virtual hook recording::memento::replay_into
+   for recording::convert_vector.  */
+
+void
+recording::convert_vector::replay_into (replayer *r)
+{
+  set_playback_obj (
+    r->convert_vector (playback_location (r, m_loc),
+                         m_vector->playback_rvalue (),
+                         m_type->playback_type ()));
+}
+
+/* Implementation of pure virtual hook recording::rvalue::visit_children
+   for recording::convert_vector.  */
+
+void
+recording::convert_vector::visit_children (rvalue_visitor *v)
+{
+  v->visit (m_vector);
+}
+
+/* Implementation of recording::memento::make_debug_string for
+   array accesses.  */
+
+recording::string *
+recording::convert_vector::make_debug_string ()
+{
+  enum precedence prec = get_precedence ();
+  return string::from_printf (m_ctxt,
+                             "(%s)%s",
+                             m_type->get_debug_string (),
+                             m_vector->get_debug_string_parens (prec));
+}
+
+/* Implementation of recording::memento::write_reproducer for
+   convert_vector.  */
+
+void
+recording::convert_vector::write_reproducer (reproducer &r)
+{
+  const char *id = r.make_identifier (this, "lvalue");
+  r.write ("  gcc_jit_rvalue *%s = \n"
+          "    gcc_jit_context_convert_vector (%s, /* gcc_jit_context *ctxt */\n"
+          "                                    %s, /*gcc_jit_location *loc */\n"
+          "                                    %s, /* gcc_jit_rvalue *vector */\n"
+          "                                    %s); /* gcc_jit_type *type */\n",
+          id,
+          r.get_identifier (get_context ()),
+          r.get_identifier (m_loc),
+          r.get_identifier_as_rvalue (m_vector),
+          r.get_identifier_as_type (m_type));
+}
+
 /* The implementation of class gcc::jit::recording::access_field_of_lvalue.  */
 
 /* Implementation of pure virtual hook recording::memento::replay_into
index 07fdcb90773469fbf2508bbd1418ef37295eb540..ec01e62137af4ffd81ba98d4d0d324dfee27c48c 100644 (file)
@@ -230,6 +230,11 @@ public:
                    rvalue *ptr,
                    rvalue *index);
 
+  rvalue *
+  new_convert_vector (location *loc,
+                 rvalue *vector,
+                 type *type);
+
   case_ *
   new_case (rvalue *min_value,
            rvalue *max_value,
@@ -2004,6 +2009,35 @@ private:
   rvalue *m_index;
 };
 
+class convert_vector : public rvalue
+{
+public:
+  convert_vector (context *ctxt,
+                 location *loc,
+                 rvalue *vector,
+                 type *type)
+  : rvalue (ctxt, loc, type),
+    m_vector (vector),
+    m_type (type)
+  {}
+
+  void replay_into (replayer *r) final override;
+
+  void visit_children (rvalue_visitor *v) final override;
+
+private:
+  string * make_debug_string () final override;
+  void write_reproducer (reproducer &r) final override;
+  enum precedence get_precedence () const final override
+  {
+    return PRECEDENCE_POSTFIX;
+  }
+
+private:
+  rvalue *m_vector;
+  type *m_type;
+};
+
 class access_field_of_lvalue : public lvalue
 {
 public:
index cc36ae96433250d723740ab20eaf15c97e037273..664180177fea4b0132c21c2d18242829443abf26 100644 (file)
@@ -2556,6 +2556,42 @@ gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
   return (gcc_jit_lvalue *)ctxt->new_array_access (loc, ptr, index);
 }
 
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::context::new_convert_vector method in
+   jit-recording.cc.  */
+
+gcc_jit_rvalue *
+gcc_jit_context_convert_vector (gcc_jit_context *ctxt,
+                               gcc_jit_location *loc,
+                               gcc_jit_rvalue *vector,
+                               gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (ctxt, NULL, loc, "NULL context");
+  JIT_LOG_FUNC (ctxt->get_logger ());
+  /* LOC can be NULL.  */
+  RETURN_NULL_IF_FAIL (vector, ctxt, loc, "NULL vector");
+  RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
+
+  gcc::jit::recording::vector_type *value_vec_type
+    = vector->get_type ()->dyn_cast_vector_type ();
+  RETURN_NULL_IF_FAIL_PRINTF1 (value_vec_type, ctxt, loc,
+                              "%s is not a value of a vector type",
+                              vector->get_debug_string ());
+  gcc::jit::recording::vector_type *as_vec_type
+    = type->dyn_cast_vector_type ();
+  RETURN_NULL_IF_FAIL_PRINTF1 (as_vec_type, ctxt, loc,
+                              "%s is not a vector type",
+                              type->get_debug_string ());
+  RETURN_NULL_IF_FAIL_PRINTF2 (
+    as_vec_type->get_num_units () == value_vec_type->get_num_units (), ctxt,
+    loc, "%s should contain the same number of elements as %s",
+    vector->get_debug_string (), type->get_debug_string ());
+
+  return (gcc_jit_rvalue *)ctxt->new_convert_vector (loc, vector, type);
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
index b1b727744984149840455f4e67a2daa1fda103a6..bd4606c9868cae53cda993098ac52a33cfefbf97 100644 (file)
@@ -1338,6 +1338,16 @@ gcc_jit_context_new_array_access (gcc_jit_context *ctxt,
                                  gcc_jit_rvalue *ptr,
                                  gcc_jit_rvalue *index);
 
+#define LIBGCCJIT_HAVE_gcc_jit_context_convert_vector
+
+/* Given a vector rvalue, cast it to the type ``type``, doing an element-wise
+   conversion.  */
+extern gcc_jit_rvalue *
+gcc_jit_context_convert_vector (gcc_jit_context *ctxt,
+                               gcc_jit_location *loc,
+                               gcc_jit_rvalue *vector,
+                               gcc_jit_type *type);
+
 /* Field access is provided separately for both lvalues and rvalues.  */
 
 /* Accessing a field of an lvalue of struct type, analogous to:
index 26934ddf8a0e4ce41af85ae5a950bdda69e3e682..3082725a12bf2227404f82290c9c7541de1d08c5 100644 (file)
@@ -299,3 +299,8 @@ LIBGCCJIT_ABI_29 {
   global:
     gcc_jit_global_set_readonly;
 } LIBGCCJIT_ABI_28;
+
+LIBGCCJIT_ABI_30 {
+  global:
+    gcc_jit_context_convert_vector;
+} LIBGCCJIT_ABI_29;
index dfb4dfa98db9514f180e432d1e37e1b4d1185366..32ca70da4374ed2597f8e78481f6c1c6813ae7fb 100644 (file)
 /* test-const-attribute.c: This can't be in the testcases array as it needs
    the `-O3` flag.  */
 
+/* test-convert-vector.c */
+#define create_code create_code_convert_vector
+#define verify_code verify_code_convert_vector
+#include "test-convert-vector.c"
+#undef create_code
+#undef verify_code
+
 /* test-debug-strings.c */
 #define create_code create_code_debug_strings
 #define verify_code verify_code_debug_strings
@@ -504,6 +511,9 @@ const struct testcase testcases[] = {
   {"constants",
    create_code_constants,
    verify_code_constants},
+  {"convert_vector",
+   create_code_convert_vector,
+   verify_code_convert_vector},
   {"debug_strings",
    create_code_debug_strings,
    verify_code_debug_strings},
diff --git a/gcc/testsuite/jit.dg/test-convert-vector.c b/gcc/testsuite/jit.dg/test-convert-vector.c
new file mode 100644 (file)
index 0000000..4aba719
--- /dev/null
@@ -0,0 +1,60 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.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 __attribute__ ((__vector_size__ (8))) convert_vec(double __attribute__ ((__vector_size__ (16))) double_vec)
+     {
+      return __builtin_convertvector (double_vec, int __attribute__ ((__vector_size__ (8))));
+     }
+  */
+  gcc_jit_type *int_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+  gcc_jit_type *int_vector_type =
+    gcc_jit_type_get_vector (int_type, 2);
+  gcc_jit_type *double_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_DOUBLE);
+  gcc_jit_type *double_vector_type =
+    gcc_jit_type_get_vector (double_type, 2);
+
+  /* Build the convert_vec.  */
+  gcc_jit_param *param =
+    gcc_jit_context_new_param (ctxt, NULL, double_vector_type, "double_vec");
+  gcc_jit_function *convert_vec =
+    gcc_jit_context_new_function (ctxt, NULL,
+                                 GCC_JIT_FUNCTION_EXPORTED,
+                                 int_vector_type,
+                                 "convert_vec",
+                                 1, &param,
+                                 0);
+  gcc_jit_block *block = gcc_jit_function_new_block (convert_vec, NULL);
+
+  gcc_jit_lvalue *var = gcc_jit_function_new_local (convert_vec, NULL, double_vector_type, "mem_vec");
+  gcc_jit_block_add_assignment (block, NULL, var, gcc_jit_param_as_rvalue (param));
+
+  gcc_jit_block_end_with_return (block,
+    NULL,
+    gcc_jit_context_convert_vector (ctxt, NULL, gcc_jit_lvalue_as_rvalue (var), int_vector_type));
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  CHECK_NON_NULL (result);
+
+  typedef int __attribute__ ((__vector_size__ (8))) (*convert_vec)(double __attribute__ ((__vector_size__ (16))) double_vec);
+  double __attribute__ ((__vector_size__ (16))) double_vec = { 3.2, 7.9 };
+  convert_vec func = (convert_vec)gcc_jit_result_get_code (result, "convert_vec");
+  int __attribute__ ((__vector_size__ (8))) res = func (double_vec);
+  int __attribute__ ((__vector_size__ (8))) expected = { 3, 7 };
+  CHECK_VECTOR_VALUE (2, res, expected);
+}