GCC JIT PR118587 - tentative patch 1

2025-01-24 Thread Basile Starynkevitch
Hello all,

The following patch to GCC trunk commit 91fa9c15cc4fb9 could provide a (partial)
fix to 
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118587

diff --git a/gcc/jit/jit-recording.h b/gcc/jit/jit-recording.h
index 0ac9245c2df..5e54f02923f 100644
--- a/gcc/jit/jit-recording.h
+++ b/gcc/jit/jit-recording.h
@@ -580,6 +580,21 @@ private:
   int m_line;
   int m_column;
   bool m_created_by_user;
+public:
+  const std::string get_filename() const {
+if (m_filename)
+  return std::string(m_filename);
+return "";
+  };
+  int get_line() const {
+return m_line;
+  };
+  int get_column() const {
+return m_column;
+  };
+  bool is_created_by_user() const {
+return m_created_by_user;
+  };
 };
 
 class type : public memento



Could someone please review it (and if autorized to) apply it? I feel it is
pretty trivial and definitely useful.

Thanks
-- 
Basile STARYNKEVITCH   
8 rue de la Faïencerie
92340 Bourg-la-Reine,  France
http://starynkevitch.net/Basile & https://github.com/bstarynk 


[pushed: r15-7179] jit: fix for write_reproducer [PR117886]

2025-01-24 Thread David Malcolm
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 
---
 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 ..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 
+#include 
+
+#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