The original generated .c reproducer for PR jit/117886 did not compile,
with:

main.c: In function ‘create_code’:
main.c:600:9: error: initialization of ‘gcc_jit_rvalue *’ from incompatible 
pointer type ‘gcc_jit_lvalue *’ [-Wincompatible-pointer-types]
  600 |         local__1,
      |         ^~~~~~~~

The issue is that recording::ctor::write_reproducer was missing
creation of casts to gcc_jit_rvalue * for
gcc_jit_context_new_array_constructor and
gcc_jit_context_new_struct_constructor.

Fixed thusly.

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r15-7179-g4d18acf8023ba0.

gcc/jit/ChangeLog:
        PR jit/117886
        * jit-recording.cc (reproducer::get_identifier_as_rvalue): Handle
        null memento.
        (reproducer::get_identifier_as_lvalue): Likewise.
        (reproducer::get_identifier_as_type): Likewise.
        (recording::ctor::write_reproducer): Use get_identifier_as_rvalue
        rather than get_identifier when writing out gcc_jit_rvalue *
        expressions.

gcc/testsuite/ChangeLog:
        PR jit/117886
        * jit.dg/all-non-failing-tests.h: Add
        test-pr117886-write-reproducer.c.
        * jit.dg/test-pr117886-write-reproducer.c: New test.

Signed-off-by: David Malcolm <dmalc...@redhat.com>
---
 gcc/jit/jit-recording.cc                      |  10 +-
 gcc/testsuite/jit.dg/all-non-failing-tests.h  |  10 ++
 .../jit.dg/test-pr117886-write-reproducer.c   | 103 ++++++++++++++++++
 3 files changed, 121 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/jit.dg/test-pr117886-write-reproducer.c

diff --git a/gcc/jit/jit-recording.cc b/gcc/jit/jit-recording.cc
index e6fef5bca076..8da3cb059156 100644
--- a/gcc/jit/jit-recording.cc
+++ b/gcc/jit/jit-recording.cc
@@ -406,6 +406,8 @@ reproducer::get_identifier (recording::memento *m)
 const char *
 reproducer::get_identifier_as_rvalue (recording::rvalue *m)
 {
+  if (!m)
+    return "NULL";
   return m->access_as_rvalue (*this);
 }
 
@@ -415,6 +417,8 @@ reproducer::get_identifier_as_rvalue (recording::rvalue *m)
 const char *
 reproducer::get_identifier_as_lvalue (recording::lvalue *m)
 {
+  if (!m)
+    return "NULL";
   return m->access_as_lvalue (*this);
 }
 
@@ -424,6 +428,8 @@ reproducer::get_identifier_as_lvalue (recording::lvalue *m)
 const char *
 reproducer::get_identifier_as_type (recording::type *m)
 {
+  if (!m)
+    return "NULL";
   return m->access_as_type (*this);
 }
 
@@ -6041,7 +6047,7 @@ recording::ctor::write_reproducer (reproducer &r)
        r.write ("    gcc_jit_rvalue *value = NULL;\n");
       else
        r.write ("    gcc_jit_rvalue *value = %s;\n",
-                r.get_identifier (m_values[0]));
+                r.get_identifier_as_rvalue (m_values[0]));
 
       if (m_fields.length () == 0)
        r.write ("    gcc_jit_field *field = NULL;\n");
@@ -6058,7 +6064,7 @@ recording::ctor::write_reproducer (reproducer &r)
        {
          r.write ("    gcc_jit_rvalue *values[] = {\n");
          for (size_t i = 0; i < m_values.length (); i++)
-           r.write ("        %s,\n", r.get_identifier (m_values[i]));
+           r.write ("        %s,\n", r.get_identifier_as_rvalue (m_values[i]));
          r.write ("      };\n");
        }
       /* Write the array of fields.  */
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h 
b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index b566925fd3dd..add5619aebd4 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -344,6 +344,13 @@
 #undef create_code
 #undef verify_code
 
+/* test-pr117886-write-reproducer.c.  */
+#define create_code create_code_pr117886_write_reproducer
+#define verify_code verify_code_pr117886_write_reproducer
+#include "test-pr117886-write-reproducer.c"
+#undef create_code
+#undef verify_code
+
 /* test-pure-attribute.c: This can't be in the testcases array as it needs
    the `-O3` flag.  */
 
@@ -603,6 +610,9 @@ const struct testcase testcases[] = {
   {"pr95314_rvalue_reuse",
    create_code_pr95314_rvalue_reuse,
    verify_code_pr95314_rvalue_reuse},
+  {"pr117886_write_reproducer",
+   create_code_pr117886_write_reproducer,
+   verify_code_pr117886_write_reproducer},
   {"reading_struct ",
    create_code_reading_struct ,
    verify_code_reading_struct },
diff --git a/gcc/testsuite/jit.dg/test-pr117886-write-reproducer.c 
b/gcc/testsuite/jit.dg/test-pr117886-write-reproducer.c
new file mode 100644
index 000000000000..5018b0ea3eda
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-pr117886-write-reproducer.c
@@ -0,0 +1,103 @@
+/* Verify that we can generate and compile reproducers for
+   gcc_jit_context_new_array_constructor
+   when the values are lvalues */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "libgccjit.h"
+#include "harness.h"
+
+/* 
+   int foo[3];
+
+   void test (void)
+   {
+     int a = 1;
+     int b = 2;
+     int c = 3;
+     foo = {a,b,c};
+   }
+*/
+
+void
+create_code (gcc_jit_context *ctxt, void *user_data)
+{
+  gcc_jit_type *int_type
+    = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_INT);
+  gcc_jit_type *void_type
+    = gcc_jit_context_get_type (ctxt, GCC_JIT_TYPE_VOID);
+
+  gcc_jit_type *arr_type = gcc_jit_context_new_array_type (ctxt,
+                                                          0,
+                                                          int_type,
+                                                          3);
+  gcc_jit_lvalue *global_intarr_123 = gcc_jit_context_new_global (
+                                                                 ctxt, NULL,
+                                                                 
GCC_JIT_GLOBAL_EXPORTED,
+                                                                 arr_type,
+                                                                 
"global_intarr_123");
+
+  gcc_jit_function *func =
+    gcc_jit_context_new_function (ctxt, NULL,
+                                 GCC_JIT_FUNCTION_EXPORTED,
+                                 void_type,
+                                 "initialize_intarr_123",
+                                 0, NULL, 0);
+  gcc_jit_lvalue *a
+    = gcc_jit_function_new_local (func, NULL, int_type, "a");
+  gcc_jit_lvalue *b
+    = gcc_jit_function_new_local (func, NULL, int_type, "b");
+  gcc_jit_lvalue *c
+    = gcc_jit_function_new_local (func, NULL, int_type, "c");
+
+  gcc_jit_block *bb =
+    gcc_jit_function_new_block (func, "initial");
+
+  gcc_jit_block_add_assignment (bb, NULL,
+                               a, gcc_jit_context_new_rvalue_from_int (ctxt, 
int_type, 1));
+  gcc_jit_block_add_assignment (bb, NULL,
+                               b, gcc_jit_context_new_rvalue_from_int (ctxt, 
int_type, 2));
+  gcc_jit_block_add_assignment (bb, NULL,
+                               c, gcc_jit_context_new_rvalue_from_int (ctxt, 
int_type, 3));
+     
+  gcc_jit_rvalue *values[] = {(gcc_jit_rvalue *)a,
+                             (gcc_jit_rvalue *)b,
+                             (gcc_jit_rvalue *)c};
+  gcc_jit_rvalue *ctor = gcc_jit_context_new_array_constructor (ctxt,
+                                                               0,
+                                                               arr_type,
+                                                               3,
+                                                               values);
+  gcc_jit_block_add_assignment (bb, NULL,
+                               global_intarr_123,
+                               ctor);
+
+  gcc_jit_block_end_with_void_return (bb, NULL);  
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  CHECK_NON_NULL (result);
+
+  typedef void (*my_fn_type) (void);
+  my_fn_type initialize_intarr_123
+    = (my_fn_type)gcc_jit_result_get_code (result, "initialize_intarr_123");
+  CHECK_NON_NULL (initialize_intarr_123);
+
+  {
+    int *foo = gcc_jit_result_get_global (result, "global_intarr_123");
+    CHECK_NON_NULL (foo);
+
+    CHECK_VALUE (foo[0], 0);
+    CHECK_VALUE (foo[1], 0);
+    CHECK_VALUE (foo[2], 0);
+
+    initialize_intarr_123 ();
+
+    CHECK_VALUE (foo[0], 1);
+    CHECK_VALUE (foo[1], 2);
+    CHECK_VALUE (foo[2], 3);
+  }
+}
-- 
2.26.3

Reply via email to