]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libgccjit: Add support for creating temporary variables
authorAntoni Boucher <bouanto@zoho.com>
Thu, 18 Jan 2024 22:54:59 +0000 (17:54 -0500)
committerAntoni Boucher <bouanto@zoho.com>
Thu, 18 Jan 2024 22:54:59 +0000 (17:54 -0500)
gcc/jit/ChangeLog:

* docs/topics/compatibility.rst (LIBGCCJIT_ABI_33): New ABI tag.
* docs/topics/functions.rst: Document gcc_jit_function_new_temp.
* jit-playback.cc (new_local): Add support for temporary
variables.
* jit-recording.cc (recording::function::new_temp): New method.
(recording::local::write_reproducer): Support temporary
variables.
* jit-recording.h (new_temp): New method.
* libgccjit.cc (gcc_jit_function_new_temp): New function.
* libgccjit.h (gcc_jit_function_new_temp): New function.
* libgccjit.map: New function.

gcc/testsuite/ChangeLog:

* jit.dg/all-non-failing-tests.h: Mention test-temp.c.
* jit.dg/test-temp.c: New test.

gcc/jit/docs/topics/compatibility.rst
gcc/jit/docs/topics/functions.rst
gcc/jit/jit-playback.cc
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-temp.c [new file with mode: 0644]

index bfef40afc2a01642323df98bea1d29f3d42184a4..34c9b740a9a818bcb144626cbbaec9c5742eca53 100644 (file)
@@ -436,3 +436,12 @@ on functions and variables:
 ``LIBGCCJIT_ABI_32`` covers the addition of a function to get target builtins:
 
   * :func:`gcc_jit_context_get_target_builtin_function`
+
+.. _LIBGCCJIT_ABI_33:
+
+``LIBGCCJIT_ABI_33``
+--------------------
+``LIBGCCJIT_ABI_33`` covers the addition of a function to create a new
+temporary variable:
+
+  * :func:`gcc_jit_function_new_temp`
index 16e82a34c21f5cbf6dee89c0b88ae12e4cd64324..d986439de131678b379788bdac212da5d6b6295e 100644 (file)
@@ -190,6 +190,26 @@ Functions
    underlying string, so it is valid to pass in a pointer to an on-stack
    buffer.
 
+.. function:: gcc_jit_lvalue *\
+              gcc_jit_function_new_temp (gcc_jit_function *func,\
+                                         gcc_jit_location *loc,\
+                                         gcc_jit_type *type)
+
+   Create a new local variable within the function, of the given type.
+   This function is similar to :func:`gcc_jit_function_new_local`, but
+   it is to be used for compiler-generated variables (as opposed to
+   user-defined variables in the language to be compiled) and these
+   variables won't show up in the debug info.
+
+   The parameter ``type`` must be non-`void`.
+
+   This entrypoint was added in :ref:`LIBGCCJIT_ABI_33`; you can test
+   for its presence using
+
+   .. code-block:: c
+
+      #ifdef LIBGCCJIT_HAVE_gcc_jit_function_new_temp
+
 .. function::  size_t \
                gcc_jit_function_get_param_count (gcc_jit_function *func)
 
index e8887e96c8e0e9dd89b5a2a8e5bd991a93dc79b7..7530b0d84775ca08c6efd38c7a7553e99c72f093 100644 (file)
@@ -32,6 +32,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "toplev.h"
 #include "tree-cfg.h"
 #include "convert.h"
+#include "gimple-expr.h"
 #include "stor-layout.h"
 #include "print-tree.h"
 #include "gimplify.h"
@@ -2167,10 +2168,20 @@ new_local (location *loc,
                                       std::string>> &attributes)
 {
   gcc_assert (type);
-  gcc_assert (name);
-  tree inner = build_decl (UNKNOWN_LOCATION, VAR_DECL,
+  tree inner;
+  if (name)
+    inner = build_decl (UNKNOWN_LOCATION, VAR_DECL,
                           get_identifier (name),
                           type->as_tree ());
+  else
+  {
+    inner = build_decl (UNKNOWN_LOCATION, VAR_DECL,
+                       create_tmp_var_name ("JITTMP"),
+                       type->as_tree ());
+    DECL_ARTIFICIAL (inner) = 1;
+    DECL_IGNORED_P (inner) = 1;
+    DECL_NAMELESS (inner) = 1;
+  }
   DECL_CONTEXT (inner) = this->m_inner_fndecl;
 
   /* Prepend to BIND_EXPR_VARS: */
index 7c1b3fdde4102bfdaacbe9749e377d5b0fcb7ac4..4a0f8e6dff75e32f9afd7f2c5f495105e58c816f 100644 (file)
@@ -4356,6 +4356,23 @@ recording::function::new_local (recording::location *loc,
   return result;
 }
 
+/* Create a recording::local instance and add it to
+   the functions's context's list of mementos, and to the function's
+   list of locals.
+
+   Implements the post-error-checking part of
+   gcc_jit_function_new_temp.  */
+
+recording::lvalue *
+recording::function::new_temp (recording::location *loc,
+                              type *type)
+{
+  local *result = new local (this, loc, type, NULL);
+  m_ctxt->record (result);
+  m_locals.safe_push (result);
+  return result;
+}
+
 /* Create a recording::block instance and add it to
    the functions's context's list of mementos, and to the function's
    list of blocks.
@@ -7226,16 +7243,26 @@ void
 recording::local::write_reproducer (reproducer &r)
 {
   const char *id = r.make_identifier (this, "local");
-  r.write ("  gcc_jit_lvalue *%s =\n"
-          "    gcc_jit_function_new_local (%s, /* gcc_jit_function *func */\n"
-          "                                %s, /* gcc_jit_location *loc */\n"
-          "                                %s, /* gcc_jit_type *type */\n"
-          "                                %s); /* const char *name */\n",
-          id,
-          r.get_identifier (m_func),
-          r.get_identifier (m_loc),
-          r.get_identifier_as_type (m_type),
-          m_name->get_debug_string ());
+  if (m_name)
+    r.write ("  gcc_jit_lvalue *%s =\n"
+            "    gcc_jit_function_new_local (%s, /* gcc_jit_function *func */\n"
+            "                                %s, /* gcc_jit_location *loc */\n"
+            "                                %s, /* gcc_jit_type *type */\n"
+            "                                %s); /* const char *name */\n",
+            id,
+            r.get_identifier (m_func),
+            r.get_identifier (m_loc),
+            r.get_identifier_as_type (m_type),
+            m_name->get_debug_string ());
+  else
+    r.write ("  gcc_jit_lvalue *%s =\n"
+            "    gcc_jit_function_new_temp (%s, /* gcc_jit_function *func */\n"
+            "                               %s, /* gcc_jit_location *loc */\n"
+            "                               %s); /* gcc_jit_type *type */\n",
+            id,
+            r.get_identifier (m_func),
+            r.get_identifier (m_loc),
+            r.get_identifier_as_type (m_type));
 }
 
 /* The implementation of class gcc::jit::recording::statement.  */
index fa254875b20e16b9764431466302e22f665e0024..637732e33e5bc6db1b25ab421900c19eb1ff673a 100644 (file)
@@ -1523,6 +1523,10 @@ public:
             type *type,
             const char *name);
 
+  lvalue *
+  new_temp (location *loc,
+           type *type);
+
   block*
   new_block (const char *name);
 
@@ -2417,7 +2421,12 @@ public:
   void write_to_dump (dump &d) final override;
 
 private:
-  string * make_debug_string () final override { return m_name; }
+  string * make_debug_string () final override {
+    if (m_name)
+      return m_name;
+    else
+      return m_ctxt->new_string ("temp");
+  }
   void write_reproducer (reproducer &r) final override;
   enum precedence get_precedence () const final override
   {
index 17cefa601a93f29d669b7f22e14fe964eb6f7787..881c4ad2354d2504c38ea1f9b16a60032b4e7e4b 100644 (file)
@@ -2947,6 +2947,37 @@ gcc_jit_function_new_local (gcc_jit_function *func,
   return (gcc_jit_lvalue *)func->new_local (loc, type, name);
 }
 
+/* Public entrypoint.  See description in libgccjit.h.
+
+   After error-checking, the real work is done by the
+   gcc::jit::recording::function::new_temp method in jit-recording.cc.  */
+
+gcc_jit_lvalue *
+gcc_jit_function_new_temp (gcc_jit_function *func,
+                          gcc_jit_location *loc,
+                          gcc_jit_type *type)
+{
+  RETURN_NULL_IF_FAIL (func, NULL, loc, "NULL function");
+  gcc::jit::recording::context *ctxt = func->m_ctxt;
+  JIT_LOG_FUNC (ctxt->get_logger ());
+  /* LOC can be NULL.  */
+  RETURN_NULL_IF_FAIL (func->get_kind () != GCC_JIT_FUNCTION_IMPORTED,
+                      ctxt, loc,
+                      "Cannot add temps to an imported function");
+  RETURN_NULL_IF_FAIL (type, ctxt, loc, "NULL type");
+  RETURN_NULL_IF_FAIL_PRINTF1 (
+    type->has_known_size (),
+    ctxt, loc,
+    "unknown size for temp (type: %s)",
+    type->get_debug_string ());
+  RETURN_NULL_IF_FAIL (
+    !type->is_void (),
+    ctxt, loc,
+    "void type for temp");
+
+  return (gcc_jit_lvalue *)func->new_temp (loc, type);
+}
+
 /* Public entrypoint.  See description in libgccjit.h.
 
    After error-checking, the real work is done by the
index 18a343628b222cdb5958c9f23d1c15664a0a9c9e..5ea401af508e2976fcdc88e7977704155e1d2cbc 100644 (file)
@@ -1476,6 +1476,13 @@ gcc_jit_function_new_local (gcc_jit_function *func,
                            gcc_jit_type *type,
                            const char *name);
 
+extern gcc_jit_lvalue *
+gcc_jit_function_new_temp (gcc_jit_function *func,
+                          gcc_jit_location *loc,
+                          gcc_jit_type *type);
+
+#define LIBGCCJIT_HAVE_gcc_jit_function_new_temp
+
 /**********************************************************************
  Statement-creation.
  **********************************************************************/
index b1c14b51d95ffc00ce345605f9c0decd1229e874..2263d278daff18ca89bc0d69ee3aa89989141d56 100644 (file)
@@ -315,3 +315,8 @@ LIBGCCJIT_ABI_32 {
   global:
     gcc_jit_context_get_target_builtin_function;
 } LIBGCCJIT_ABI_31;
+
+LIBGCCJIT_ABI_33 {
+  global:
+    gcc_jit_function_new_temp;
+} LIBGCCJIT_ABI_32;
index 6b602f95a5bb4223e0c3755a4bd78b663e1dd946..3204cb8a419986a7afe8f998cb91094fa2ce8eb3 100644 (file)
 /* test-target-builtins.c: This can't be in the testcases array as it
    is target-specific.  */
 
+/* test-temp.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-temp.c b/gcc/testsuite/jit.dg/test-temp.c
new file mode 100644 (file)
index 0000000..9b1ba9b
--- /dev/null
@@ -0,0 +1,56 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "libgccjit.h"
+
+#define TEST_COMPILING_TO_FILE
+#define OUTPUT_KIND      GCC_JIT_OUTPUT_KIND_ASSEMBLER
+#define OUTPUT_FILENAME  "output-of-test-test-temp.c.s"
+#include "harness.h"
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  /* Let's try to inject the equivalent of:
+int
+func ()
+{
+   int temp = 10;
+   return temp;
+}
+   */
+  gcc_jit_type *int_type =
+    gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+
+  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_lvalue *temp =
+    gcc_jit_function_new_temp (func, NULL, int_type);
+
+  gcc_jit_rvalue *ten =
+    gcc_jit_context_new_rvalue_from_int (ctxt, int_type, 10);
+  gcc_jit_block_add_assignment (initial, NULL, temp, ten);
+
+  gcc_jit_block_end_with_return(initial, NULL,
+    gcc_jit_lvalue_as_rvalue (temp));
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  CHECK_NON_NULL (result);
+}
+
+/* { dg-final { jit-verify-output-file-was-created "" } } */
+/* { dg-final { jit-verify-assembler-output-not "JITTMP" } } */