Skip to content

Commit

Permalink
libgccjit: Add support for creating temporary variables
Browse files Browse the repository at this point in the history
gcc/jit/ChangeLog:

	* docs/topics/compatibility.rst (LIBGCCJIT_ABI_26): New ABI tag.
	* docs/topics/functions.rst: Document gcc_jit_function_new_temp.
	* jit-playback.cc (new_local): Add new is_temp parameter.
	* jit-playback.h: Add new is_temp parameter.
	* jit-recording.cc (recording::function::new_temp): New method.
	(recording::local::replay_into): Support temporary variables.
	(recording::local::write_reproducer): Support temporary
	variables.
	* jit-recording.h (new_temp): New method.
	(m_is_temp): New field.
	* 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.
  • Loading branch information
antoyo committed Jan 19, 2024
1 parent 72331ec commit 58d3828
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 19 deletions.
9 changes: 9 additions & 0 deletions gcc/jit/docs/topics/compatibility.rst
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,12 @@ alignment of a variable:

* :func:`gcc_jit_lvalue_set_alignment`
* :func:`gcc_jit_lvalue_get_alignment`

.. _LIBGCCJIT_ABI_26:

``LIBGCCJIT_ABI_26``
--------------------
``LIBGCCJIT_ABI_26`` covers the addition of a functions to create a new
temporary variable:

* :func:`gcc_jit_function_new_temp`
20 changes: 20 additions & 0 deletions gcc/jit/docs/topics/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,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_26`; 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)
Expand Down
21 changes: 18 additions & 3 deletions gcc/jit/jit-playback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,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"
Expand Down Expand Up @@ -2153,13 +2154,27 @@ playback::function::
new_local (location *loc,
type *type,
const char *name,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes)
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes,
bool is_temp)
{
gcc_assert (type);
gcc_assert (name);
tree inner = build_decl (UNKNOWN_LOCATION, VAR_DECL,
tree inner;
if (is_temp)
{
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;
}
else
{
gcc_assert (name);
inner = build_decl (UNKNOWN_LOCATION, VAR_DECL,
get_identifier (name),
type->as_tree ());
}
DECL_CONTEXT (inner) = this->m_inner_fndecl;

/* Prepend to BIND_EXPR_VARS: */
Expand Down
3 changes: 2 additions & 1 deletion gcc/jit/jit-playback.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,7 +554,8 @@ class function : public wrapper
new_local (location *loc,
type *type,
const char *name,
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes);
const std::vector<std::pair<gcc_jit_variable_attribute, std::string>> &attributes,
bool is_temp);

block*
new_block (const char *name);
Expand Down
52 changes: 40 additions & 12 deletions gcc/jit/jit-recording.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4447,7 +4447,24 @@ recording::function::new_local (recording::location *loc,
type *type,
const char *name)
{
local *result = new local (this, loc, type, new_string (name));
local *result = new local (this, loc, type, new_string (name), false);
m_ctxt->record (result);
m_locals.safe_push (result);
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, true);
m_ctxt->record (result);
m_locals.safe_push (result);
return result;
Expand Down Expand Up @@ -7213,7 +7230,8 @@ recording::local::replay_into (replayer *r)
->new_local (playback_location (r, m_loc),
m_type->playback_type (),
playback_string (m_name),
m_attributes);
m_attributes,
m_is_temp);

if (m_reg_name != NULL)
obj->set_register_name (m_reg_name->c_str ());
Expand Down Expand Up @@ -7244,16 +7262,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_is_temp)
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));
else
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 ());
}

/* The implementation of class gcc::jit::recording::statement. */
Expand Down
17 changes: 14 additions & 3 deletions gcc/jit/jit-recording.h
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,10 @@ class function : public memento
type *type,
const char *name);

lvalue *
new_temp (location *loc,
type *type);

block*
new_block (const char *name);

Expand Down Expand Up @@ -2418,10 +2422,11 @@ class function_pointer : public rvalue
class local : public lvalue
{
public:
local (function *func, location *loc, type *type_, string *name)
local (function *func, location *loc, type *type_, string *name, bool is_temp)
: lvalue (func->m_ctxt, loc, type_),
m_func (func),
m_name (name)
m_name (name),
m_is_temp (is_temp)
{
set_scope (func);
}
Expand All @@ -2433,7 +2438,12 @@ class local : public lvalue
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_is_temp)
return m_ctxt->new_string ("temp");
else
return m_name;
}
void write_reproducer (reproducer &r) final override;
enum precedence get_precedence () const final override
{
Expand All @@ -2443,6 +2453,7 @@ class local : public lvalue
private:
function *m_func;
string *m_name;
bool m_is_temp;
};

class statement : public memento
Expand Down
31 changes: 31 additions & 0 deletions gcc/jit/libgccjit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2928,6 +2928,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
Expand Down
7 changes: 7 additions & 0 deletions gcc/jit/libgccjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1409,6 +1409,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.
**********************************************************************/
Expand Down
5 changes: 5 additions & 0 deletions gcc/jit/libgccjit.map
Original file line number Diff line number Diff line change
Expand Up @@ -342,3 +342,8 @@ LIBGCCJIT_ABI_36 {
global:
gcc_jit_context_set_output_ident;
} LIBGCCJIT_ABI_35;

LIBGCCJIT_ABI_37 {
global:
gcc_jit_function_new_temp;
} LIBGCCJIT_ABI_36;
3 changes: 3 additions & 0 deletions gcc/testsuite/jit.dg/all-non-failing-tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@
/* 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
Expand Down
56 changes: 56 additions & 0 deletions gcc/testsuite/jit.dg/test-temp.c
Original file line number Diff line number Diff line change
@@ -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" } } */

0 comments on commit 58d3828

Please sign in to comment.