Re: [PATCH] libgccjit: Add function to set the initial value of a global variable [PR96089]

2021-11-22 Thread Antoni Boucher via Gcc-patches
Hi David!

I updated the patch to allow initializing global variables with values
of type array or struct.

I also fixed the bug I was talking in my previous message by using the
following workaround: I create a new memento for the action of setting
the global variable initial value and as such, both the global variable
and the initial value are bound to exist when setting the global
variable initializer.
Is that workaround good enough?
(I guess that workaround could be used to fix the same issue that we
have for inline assembly.)

Thanks for the review!

Le vendredi 11 juin 2021 à 16:44 -0400, Antoni Boucher a écrit :
> David: this one wasn't reviewed yet by you, so you can review it.
> 
> Le jeudi 20 mai 2021 à 21:27 -0400, Antoni Boucher a écrit :
> > Hi.
> > 
> > I made this patch to set an arbitrary value to a global variable.
> > 
> > This patch suffers from the same issue as inline assembly
> > (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100380), i.e. it
> > segfaults if the `rvalue` is created after the global variable.
> > It seems to be a design issue so I'm not sure what would be the fix
> > for
> > it and having it fixed would allow me to test this new function
> > much
> > more and see if things are missing (i.e. it might require a way to
> > create a constant struct).
> > See the link above for an explanation of this issue.
> > 
> > Thanks for the review.
> 

From a095291f43ca8348b6c84f94a598895969d94d1b Mon Sep 17 00:00:00 2001
From: Antoni Boucher 
Date: Sat, 25 Sep 2021 16:37:47 -0400
Subject: [PATCH] libgccjit: Add function to set the initial value of a global
 variable [PR96089]

2021-11-22  Antoni Boucher  

gcc/jit/
PR target/96089
* docs/topics/compatibility.rst (LIBGCCJIT_ABI_21): New ABI
tag.
* docs/topics/expressions.rst: Add documentation for the
function gcc_jit_global_set_initializer_value,
gcc_jit_context_new_rvalue_from_array,
and gcc_jit_context_new_rvalue_from_struct.
* jit-common.h: Add missing reference to array_type in class
hierarchy.
* jit-playback.c: New functions (new_global_with_value,
set_global_initial_value, new_rvalue_from_struct, new_rvalue_from_array).
* jit-playback.h: New functions (new_global_with_value,
set_global_initial_value, new_rvalue_from_struct, new_rvalue_from_array).
* jit-recording.c: Add support for setting a value to a
global variable and new methods
(global_initializer::write_reproducer,
global_initializer::make_debug_string,
global_initializer::write_to_dump,
global_initializer::replay_into,
context::new_global_value_initializer,
memento_of_new_rvalue_from_struct::write_reproducer,
memento_of_new_rvalue_from_struct::make_debug_string,
memento_of_new_rvalue_from_struct::visit_children,
memento_of_new_rvalue_from_struct::replay_into,
memento_of_new_rvalue_from_struct::
  memento_of_new_rvalue_from_struct,
context::new_rvalue_from_struct,
memento_of_new_rvalue_from_array::write_reproducer,
memento_of_new_rvalue_from_array::make_debug_string,
memento_of_new_rvalue_from_array::visit_children,
memento_of_new_rvalue_from_array::replay_into,
memento_of_new_rvalue_from_array::
  memento_of_new_rvalue_from_array,
new_rvalue_from_array).
* jit-recording.h: New functions (set_initializer_value,
new_global_value_initializer, new_rvalue_from_struct, new_rvalue_from_array,
get_kind),
new field m_initializer_value and new classes (global_initializer,
memento_of_new_rvalue_from_struct, memento_of_new_rvalue_from_array).
* libgccjit.c: New macro RETURN_IF_FAIL_PRINTF5 and new
functions (gcc_jit_global_set_initializer_value,
gcc_jit_context_new_rvalue_from_struct,
gcc_jit_context_new_rvalue_from_array).
* libgccjit.h: New functions (gcc_jit_global_set_initializer_value,
gcc_jit_context_new_rvalue_from_struct,
gcc_jit_context_new_rvalue_from_array).
* libgccjit.map (LIBGCCJIT_ABI_21): New ABI tag.

gcc/testsuite/
PR target/96089
* jit.dg/test-global-set-initializer.c: Add test for the new
function (gcc_jit_global_set_initializer_value).
* jit.dg/test-error-imported-global-initializer.c: Add test
for error checking in setting a value to a global variable.
---
 gcc/jit/docs/topics/compatibility.rst |  13 +
 gcc/jit/docs/topics/expressions.rst   |  62 
 gcc/jit/jit-common.h  |   1 +
 

Re: [PATCH] libgccjit: Add function to set the initial value of a global variable [PR96089]

2021-06-11 Thread Antoni Boucher via Gcc-patches
David: this one wasn't reviewed yet by you, so you can review it.

Le jeudi 20 mai 2021 à 21:27 -0400, Antoni Boucher a écrit :
> Hi.
> 
> I made this patch to set an arbitrary value to a global variable.
> 
> This patch suffers from the same issue as inline assembly
> (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100380), i.e. it
> segfaults if the `rvalue` is created after the global variable.
> It seems to be a design issue so I'm not sure what would be the fix
> for
> it and having it fixed would allow me to test this new function much
> more and see if things are missing (i.e. it might require a way to
> create a constant struct).
> See the link above for an explanation of this issue.
> 
> Thanks for the review.




[PATCH] libgccjit: Add function to set the initial value of a global variable [PR96089]

2021-05-20 Thread Antoni Boucher via Gcc-patches
Hi.

I made this patch to set an arbitrary value to a global variable.

This patch suffers from the same issue as inline assembly
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100380), i.e. it
segfaults if the `rvalue` is created after the global variable.
It seems to be a design issue so I'm not sure what would be the fix for
it and having it fixed would allow me to test this new function much
more and see if things are missing (i.e. it might require a way to
create a constant struct).
See the link above for an explanation of this issue.

Thanks for the review.
From 0a5fd7f759e1bd7becc993f01bdcf84ff8fc5fd5 Mon Sep 17 00:00:00 2001
From: Antoni Boucher 
Date: Sat, 15 May 2021 10:54:36 -0400
Subject: [PATCH] Add function to set the initial value of a global variable
 [PR96089]

2021-05-20  Antoni Boucher  

gcc/jit/
PR target/96089
* docs/topics/compatibility.rst (LIBGCCJIT_ABI_19): New ABI
tag.
* docs/topics/expressions.rst: Add documentation for the
function gcc_jit_global_set_initializer_value.
* jit-playback.c: New function (new_global_with_value).
* jit-playback.h: New function (new_global_with_value).
* jit-recording.c: Add support for setting a value to a
global variable.
* jit-recording.h: New function (set_initializer_value) and
new field m_initializer_value.
* libgccjit.c: New macro RETURN_IF_FAIL_PRINTF5 and new
function (gcc_jit_global_set_initializer_value).
* libgccjit.h: New function (gcc_jit_global_set_initializer_value).
* libgccjit.map (LIBGCCJIT_ABI_19): New ABI tag.

gcc/testsuite/
PR target/96089
* jit.dg/test-global-set-initializer.c: Add test for the new
function (gcc_jit_global_set_initializer_value).
---
 gcc/jit/docs/topics/compatibility.rst |  9 
 gcc/jit/docs/topics/expressions.rst   | 14 ++
 gcc/jit/jit-playback.c| 18 
 gcc/jit/jit-playback.h|  7 +++
 gcc/jit/jit-recording.c   | 34 ---
 gcc/jit/jit-recording.h   |  8 
 gcc/jit/libgccjit.c   | 43 +++
 gcc/jit/libgccjit.h   | 13 ++
 gcc/jit/libgccjit.map | 14 ++
 .../jit.dg/test-global-set-initializer.c  | 15 +++
 10 files changed, 169 insertions(+), 6 deletions(-)

diff --git a/gcc/jit/docs/topics/compatibility.rst b/gcc/jit/docs/topics/compatibility.rst
index 239b6aa1a92..666eb3a1c51 100644
--- a/gcc/jit/docs/topics/compatibility.rst
+++ b/gcc/jit/docs/topics/compatibility.rst
@@ -243,3 +243,12 @@ embedding assembler instructions:
   * :func:`gcc_jit_extended_asm_add_input_operand`
   * :func:`gcc_jit_extended_asm_add_clobber`
   * :func:`gcc_jit_context_add_top_level_asm`
+
+.. _LIBGCCJIT_ABI_19:
+
+``LIBGCCJIT_ABI_19``
+---
+``LIBGCCJIT_ABI_19`` covers the addition of an API entrypoint to set the value
+of a global variable:
+
+  * :func:`gcc_jit_global_set_initializer_value`
diff --git a/gcc/jit/docs/topics/expressions.rst b/gcc/jit/docs/topics/expressions.rst
index 396259ef07e..f638cb68fdd 100644
--- a/gcc/jit/docs/topics/expressions.rst
+++ b/gcc/jit/docs/topics/expressions.rst
@@ -603,6 +603,20 @@ Global variables
 
   #ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_initializer
 
+.. function:: void
+  gcc_jit_global_set_initializer_value (gcc_jit_lvalue *global,\
+gcc_jit_rvalue *value)
+
+   Set an initializer for ``global`` using the specified value.
+   ``global`` must be the same type as ``value``.
+
+   This entrypoint was added in :ref:`LIBGCCJIT_ABI_19`; you can test for
+   its presence using
+
+   .. code-block:: c
+
+  #ifdef LIBGCCJIT_HAVE_gcc_jit_global_set_initializer_value
+
 Working with pointers, structs and unions
 -
 
diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c
index c6136301243..d86701a8ae6 100644
--- a/gcc/jit/jit-playback.c
+++ b/gcc/jit/jit-playback.c
@@ -664,6 +664,24 @@ new_global_initialized (location *loc,
   return global_finalize_lvalue (inner);
 }
 
+playback::lvalue *
+playback::context::
+new_global_with_value (location *loc,
+		   enum gcc_jit_global_kind kind,
+		   type *type,
+		   playback::rvalue *value,
+		   const char *name)
+{
+  tree inner = global_new_decl (loc, kind, type, name);
+
+  tree inner_type = type->as_tree ();
+  tree initial = value->as_tree ();
+  gcc_assert (TREE_CONSTANT (initial));
+  DECL_INITIAL (inner) = initial;
+
+  return global_finalize_lvalue (inner);
+}
+
 /* Implementation of the various
   gcc::jit::playback::context::new_rvalue_from_const 
methods.
diff --git a/gcc/jit/jit-playback.h b/gcc/jit/jit-playback.h
index 825a3e172e9..958284d