libgccjit was failing to set the DECL_CONTEXT of function RESULT_DECLs,
leading to them failing to be properly handled by the inlining machinery.
Fixed thusly.

Successfully bootstrapped & regrtested on x86_64-pc-linux-gnu.
Pushed to trunk as r12-5903-ga2f4b4b76cdd0a4150e82e69fae4a70c54b523d2.

gcc/jit/ChangeLog:
        PR jit/103562
        * jit-playback.c (gcc::jit::playback::context::new_function): Set
        DECL_CONTEXT of the result_decl.

gcc/testsuite/ChangeLog:
        PR jit/103562
        * jit.dg/all-non-failing-tests.h: Add comment about...
        * jit.dg/test-pr103562.c: New test.

Signed-off-by: David Malcolm <dmalc...@redhat.com>
---
 gcc/jit/jit-playback.c                       |  1 +
 gcc/testsuite/jit.dg/all-non-failing-tests.h |  3 +
 gcc/testsuite/jit.dg/test-pr103562.c         | 62 ++++++++++++++++++++
 3 files changed, 66 insertions(+)
 create mode 100644 gcc/testsuite/jit.dg/test-pr103562.c

diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c
index b9c05864900..a9f52cbd0ef 100644
--- a/gcc/jit/jit-playback.c
+++ b/gcc/jit/jit-playback.c
@@ -473,6 +473,7 @@ new_function (location *loc,
   DECL_ARTIFICIAL (resdecl) = 1;
   DECL_IGNORED_P (resdecl) = 1;
   DECL_RESULT (fndecl) = resdecl;
+  DECL_CONTEXT (resdecl) = fndecl;
 
   if (builtin_id)
     {
diff --git a/gcc/testsuite/jit.dg/all-non-failing-tests.h 
b/gcc/testsuite/jit.dg/all-non-failing-tests.h
index a7fddf96db8..3663f726a99 100644
--- a/gcc/testsuite/jit.dg/all-non-failing-tests.h
+++ b/gcc/testsuite/jit.dg/all-non-failing-tests.h
@@ -230,6 +230,9 @@
 #undef create_code
 #undef verify_code
 
+/* test-pr103562.c: We don't add this one, since it touches
+   the optimization level of the context as a whole.  */
+
 /* test-pr66700-observing-write-through-ptr.c */
 #define create_code create_code_pr66700_observing_write_through_ptr
 #define verify_code verify_code_pr66700_observing_write_through_ptr
diff --git a/gcc/testsuite/jit.dg/test-pr103562.c 
b/gcc/testsuite/jit.dg/test-pr103562.c
new file mode 100644
index 00000000000..de361b96bd1
--- /dev/null
+++ b/gcc/testsuite/jit.dg/test-pr103562.c
@@ -0,0 +1,62 @@
+#include <libgccjit.h>
+#include "harness.h"
+
+struct my_struct { long a; long b; long c; };
+
+void create_code (gcc_jit_context *ctxt, void *user_data)
+{
+       /* Create the equivalent of:
+               struct my_struct { long a; long b; long c; };
+               static struct my_struct deref(struct my_struct *ptr) { return 
*ptr; } 
+               long get_a(struct my_struct *s) { return deref(s).a; }
+          and compile it at -O1.  */
+       gcc_jit_context_set_int_option(ctxt, 
GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 1);
+
+       gcc_jit_type *long_type = gcc_jit_context_get_type(ctxt, 
GCC_JIT_TYPE_LONG);
+       gcc_jit_field* fields[3] = {
+               gcc_jit_context_new_field(ctxt, NULL, long_type, "a"),
+               gcc_jit_context_new_field(ctxt, NULL, long_type, "b"),
+               gcc_jit_context_new_field(ctxt, NULL, long_type, "c"),
+       };
+       gcc_jit_struct *my_struct =
+               gcc_jit_context_new_struct_type(ctxt, NULL, "my_struct", 3, 
fields);
+       gcc_jit_type *my_struct_type = gcc_jit_struct_as_type(my_struct);
+       gcc_jit_type *my_struct_ptr_type = 
gcc_jit_type_get_pointer(my_struct_type);
+
+       /* struct my_struct deref(struct my_struct *ptr) { return *ptr; } */
+       gcc_jit_param *param_deref =
+               gcc_jit_context_new_param(ctxt, NULL, my_struct_ptr_type, 
"ptr");
+       gcc_jit_function *func_deref = gcc_jit_context_new_function(
+               ctxt, NULL, GCC_JIT_FUNCTION_INTERNAL,
+               my_struct_type, "deref",
+               1, &param_deref,
+               0);
+       gcc_jit_block *blockDeref = gcc_jit_function_new_block(func_deref, 
NULL);
+       gcc_jit_block_end_with_return(
+               blockDeref, NULL,
+               
gcc_jit_lvalue_as_rvalue(gcc_jit_rvalue_dereference(gcc_jit_param_as_rvalue(param_deref),
 NULL)));
+
+       /* long get_a(struct my_struct *s) { return deref(s).a; } */
+       gcc_jit_param *param_get_a = gcc_jit_context_new_param(ctxt, NULL, 
my_struct_ptr_type, "s");
+       gcc_jit_function *func_get_a = gcc_jit_context_new_function(
+               ctxt, NULL, GCC_JIT_FUNCTION_EXPORTED,
+               long_type, "get_a",
+               1, &param_get_a,
+               0);
+       gcc_jit_block *block_get_a = gcc_jit_function_new_block(func_get_a, 
NULL);
+       gcc_jit_rvalue *argsForDeref[1] = 
{gcc_jit_param_as_rvalue(param_get_a)};
+       gcc_jit_rvalue *callDeref = gcc_jit_context_new_call(ctxt, NULL, 
func_deref, 1, argsForDeref);
+       gcc_jit_block_end_with_return(
+               block_get_a, NULL,
+               gcc_jit_rvalue_access_field(callDeref, NULL, fields[0]));
+}
+
+void
+verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
+{
+  typedef long(*fn_type) (struct my_struct*);
+  fn_type get_a = (fn_type) gcc_jit_result_get_code(result, "get_a");
+
+  struct my_struct s = {1, 2, 3};
+  CHECK_VALUE (get_a(&s), 1);
+}
-- 
2.26.3

Reply via email to