Skip to content

Commit

Permalink
Add gcc_jit_rvalue_set_type
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez authored and antoyo committed Jul 2, 2024
1 parent 6ad6c52 commit 341be3b
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 25 deletions.
72 changes: 59 additions & 13 deletions gcc/jit/jit-recording.cc
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ recording::context::context (context *parent_ctxt)
m_last_error_str (NULL),
m_owns_last_error_str (false),
m_mementos (),
m_type_mementos (),
m_compound_types (),
m_globals (),
m_functions (),
Expand Down Expand Up @@ -616,6 +617,10 @@ recording::context::~context ()
{
delete m;
}
FOR_EACH_VEC_ELT (m_type_mementos, i, m)
{
delete m;
}

for (i = 0; i < GCC_JIT_NUM_STR_OPTIONS; ++i)
free (m_str_options[i]);
Expand Down Expand Up @@ -649,6 +654,14 @@ recording::context::record (memento *m)
m_mementos.safe_push (m);
}

void
recording::context::record_type (memento *m)
{
gcc_assert (m);

m_type_mementos.safe_push (m);
}

/* Replay this context (and any parents) into the given replayer. */

void
Expand Down Expand Up @@ -680,6 +693,25 @@ recording::context::replay_into (replayer *r)
return;

/* Replay this context's saved operations into r. */

FOR_EACH_VEC_ELT (m_type_mementos, i, m)
{
/* Disabled low-level debugging, here if we need it: print what
we're replaying.
Note that the calls to get_debug_string might lead to more
mementos being created for the strings.
This can also be used to exercise the debug_string
machinery. */
if (0)
printf ("context %p replaying (%p): %s\n",
(void *)this, (void *)m, m->get_debug_string ());

m->replay_into (r);

if (r->errors_occurred ())
return;
}

FOR_EACH_VEC_ELT (m_mementos, i, m)
{
/* Disabled low-level debugging, here if we need it: print what
Expand Down Expand Up @@ -719,6 +751,11 @@ recording::context::disassociate_from_playback ()
if (m_parent_ctxt)
m_parent_ctxt->disassociate_from_playback ();

FOR_EACH_VEC_ELT (m_type_mementos, i, m)
{
m->set_playback_obj (NULL);
}

FOR_EACH_VEC_ELT (m_mementos, i, m)
{
m->set_playback_obj (NULL);
Expand Down Expand Up @@ -784,7 +821,7 @@ recording::context::get_type (enum gcc_jit_types kind)
else
{
recording::type *result = new memento_of_get_type (this, kind);
record (result);
record_type (result);
m_basic_types[kind] = result;
}
}
Expand Down Expand Up @@ -862,7 +899,7 @@ recording::context::new_array_type (recording::location *loc,
}
recording::type *result =
new recording::array_type (this, loc, element_type, num_elements);
record (result);
record_type (result);
return result;
}

Expand All @@ -879,7 +916,7 @@ recording::context::new_field (recording::location *loc,
{
recording::field *result =
new recording::field (this, loc, type, new_string (name));
record (result);
record_type (result);
return result;
}

Expand Down Expand Up @@ -912,7 +949,7 @@ recording::context::new_struct_type (recording::location *loc,
const char *name)
{
recording::struct_ *result = new struct_ (this, loc, new_string (name));
record (result);
record_type (result);
m_compound_types.safe_push (result);
return result;
}
Expand All @@ -928,7 +965,7 @@ recording::context::new_union_type (recording::location *loc,
const char *name)
{
recording::union_ *result = new union_ (this, loc, new_string (name));
record (result);
record_type (result);
m_compound_types.safe_push (result);
return result;
}
Expand All @@ -952,7 +989,7 @@ recording::context::new_function_type (recording::type *return_type,
param_types,
is_variadic,
is_target_builtin);
record (fn_type);
record_type (fn_type);
return fn_type;
}

Expand Down Expand Up @@ -2178,6 +2215,8 @@ recording::context::dump_reproducer_to_file (const char *path)

r.write (" /* Replay of API calls for %s. */\n",
r.get_identifier (contexts[ctxt_idx]));
FOR_EACH_VEC_ELT (contexts[ctxt_idx]->m_type_mementos, i, m)
m->write_reproducer (r);
FOR_EACH_VEC_ELT (contexts[ctxt_idx]->m_mementos, i, m)
m->write_reproducer (r);
}
Expand Down Expand Up @@ -2480,7 +2519,7 @@ recording::type::get_pointer ()
if (!m_pointer_to_this_type)
{
m_pointer_to_this_type = new memento_of_get_pointer (this);
m_ctxt->record (m_pointer_to_this_type);
m_ctxt->record_type (m_pointer_to_this_type);
}
return m_pointer_to_this_type;
}
Expand All @@ -2494,7 +2533,7 @@ recording::type *
recording::type::get_const ()
{
recording::type *result = new memento_of_get_const (this);
m_ctxt->record (result);
m_ctxt->record_type (result);
return result;
}

Expand All @@ -2507,7 +2546,7 @@ recording::type *
recording::type::get_restrict ()
{
recording::type *result = new memento_of_get_restrict (this);
m_ctxt->record (result);
m_ctxt->record_type (result);
return result;
}

Expand All @@ -2520,7 +2559,7 @@ recording::type *
recording::type::get_volatile ()
{
recording::type *result = new memento_of_get_volatile (this);
m_ctxt->record (result);
m_ctxt->record_type (result);
return result;
}

Expand All @@ -2534,7 +2573,7 @@ recording::type::get_aligned (size_t alignment_in_bytes)
{
recording::type *result
= new memento_of_get_aligned (this, alignment_in_bytes);
m_ctxt->record (result);
m_ctxt->record_type (result);
return result;
}

Expand All @@ -2554,7 +2593,7 @@ recording::type::get_vector (size_t num_units)
{
recording::type *result
= new vector_type (this, num_units);
m_ctxt->record (result);
m_ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -3816,7 +3855,7 @@ recording::compound_type::set_fields (location *loc,
gcc_assert (m_fields == NULL);

m_fields = new fields (this, num_fields, field_array);
m_ctxt->record (m_fields);
m_ctxt->record_type (m_fields);
}

/* Implementation of pure virtual hook recording::type::dereference for
Expand Down Expand Up @@ -4063,6 +4102,13 @@ recording::rvalue::access_field (recording::location *loc,
return result;
}

void
recording::rvalue::set_type (type *new_type)
{
gcc_assert (new_type);
m_type = new_type;
}

/* Create a recording::dereference_field_rvalue instance and add it to
the rvalue's context's list of mementos.
Expand Down
25 changes: 14 additions & 11 deletions gcc/jit/jit-recording.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ class context : public log_user
get_builtins_manager ();

void record (memento *m);
void record_type (memento *m);
void replay_into (replayer *r);
void disassociate_from_playback ();

Expand Down Expand Up @@ -412,6 +413,7 @@ class context : public log_user

/* Recorded API usage. */
auto_vec<memento *> m_mementos;
auto_vec<memento *> m_type_mementos;

/* Specific recordings, for use by dump_to_file. */
auto_vec<compound_type *> m_compound_types;
Expand Down Expand Up @@ -759,7 +761,7 @@ class memento_of_get_pointer : public type
type* copy(context* ctxt) final override
{
type* result = new memento_of_get_pointer (m_other_type->copy (ctxt));
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -823,7 +825,7 @@ class memento_of_get_const : public decorated_type
type* copy(context* ctxt) final override
{
type* result = new memento_of_get_const (m_other_type->copy (ctxt));
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -868,7 +870,7 @@ class memento_of_get_volatile : public decorated_type
type* copy(context* ctxt) final override
{
type* result = new memento_of_get_volatile (m_other_type->copy (ctxt));
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -901,7 +903,7 @@ class memento_of_get_restrict : public decorated_type
type* copy(context* ctxt) final override
{
type* result = new memento_of_get_restrict (m_other_type->copy (ctxt));
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand All @@ -928,7 +930,7 @@ class memento_of_get_aligned : public decorated_type
type* copy(context* ctxt) final override
{
type* result = new memento_of_get_aligned (m_other_type->copy (ctxt), m_alignment_in_bytes);
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -978,7 +980,7 @@ class vector_type : public decorated_type
type* copy(context* ctxt) final override
{
type* result = new vector_type(m_other_type->copy (ctxt), m_num_units);
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -1038,7 +1040,7 @@ class array_type : public type
type* copy(context* ctxt) final override
{
type* result = new array_type (ctxt, m_loc, m_element_type->copy (ctxt), m_num_elements);
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -1087,7 +1089,7 @@ class function_type : public type

type* result = new function_type (ctxt, m_return_type->copy (ctxt), m_param_types.length (), new_params.address (),
m_is_variadic, m_is_target_builtin);
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -1244,7 +1246,7 @@ class struct_ : public compound_type
type* copy(context* ctxt) final override
{
type* result = new struct_ (ctxt, m_loc, m_name);
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -1298,7 +1300,7 @@ class union_ : public compound_type
type* copy(context* ctxt) final override
{
type* result = new union_ (ctxt, m_loc, m_name);
ctxt->record (result);
ctxt->record_type (result);
return result;
}

Expand Down Expand Up @@ -1365,6 +1367,7 @@ class rvalue : public memento
Implements the post-error-checking part of
gcc_jit_rvalue_get_type. */
type * get_type () const { return m_type; }
void set_type (type * new_type);

playback::rvalue *
playback_rvalue () const
Expand Down Expand Up @@ -2062,7 +2065,7 @@ class comparison : public rvalue
else
inner_type = element_type;
m_type = new vector_type (inner_type, vec_type->get_num_units ());
ctxt->record (m_type);
ctxt->record_type (m_type);
}
}

Expand Down
6 changes: 6 additions & 0 deletions gcc/jit/libgccjit++.h
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,12 @@ rvalue::get_type ()
return type (gcc_jit_rvalue_get_type (get_inner_rvalue ()));
}

inline void
rvalue::set_type (type *new_type)
{
gcc_jit_rvalue_set_type (get_inner_rvalue (), new_type);
}

inline rvalue
rvalue::access_field (field field,
location loc)
Expand Down
14 changes: 14 additions & 0 deletions gcc/jit/libgccjit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2013,6 +2013,20 @@ gcc_jit_lvalue_remove (gcc_jit_lvalue *lvalue)
lvalue->remove ();
}

/* Public entrypoint. See description in libgccjit.h.
After error-checking, the real work is done by the
gcc::jit::recording::rvalue::set_type method, in
jit-recording.h. */

void
gcc_jit_rvalue_set_type (gcc_jit_rvalue *rvalue, gcc_jit_type *new_type)
{
RETURN_IF_FAIL (rvalue, NULL, NULL, "NULL rvalue");

rvalue->set_type (new_type);
}

/* Verify that NUMERIC_TYPE is non-NULL, and that it is a "numeric"
type i.e. it satisfies gcc::jit::type::is_numeric (), such as the
result of gcc_jit_context_get_type (GCC_JIT_TYPE_INT). */
Expand Down
3 changes: 3 additions & 0 deletions gcc/jit/libgccjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,9 @@ gcc_jit_rvalue_get_type (gcc_jit_rvalue *rvalue);
extern void
gcc_jit_lvalue_remove (gcc_jit_lvalue *lvalue);

extern void
gcc_jit_rvalue_set_type (gcc_jit_rvalue *rvalue, gcc_jit_type *new_type);

/* Integer constants. */
extern gcc_jit_rvalue *
gcc_jit_context_new_rvalue_from_int (gcc_jit_context *ctxt,
Expand Down
1 change: 1 addition & 0 deletions gcc/jit/libgccjit.map
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,5 @@ LIBGCCJIT_ABI_40 {
LIBGCCJIT_ABI_41 {
global:
gcc_jit_lvalue_remove;
gcc_jit_rvalue_set_type;
} LIBGCCJIT_ABI_40;
Loading

0 comments on commit 341be3b

Please sign in to comment.