Re: [PATCH v2] gccjit: support dynamic alloca stub

2024-11-10 Thread Schrodinger ZHU Yifan
sorry, just noticed that the check_value was wrongly written on the last line 
of this patch. Other parts should be good. Should I correct it now in a new 
revision? Or is it okay to wait for further reviews for now?
On Sun, Nov 10, 2024 at 13:17, Schrodinger ZHU Yifan  
wrote:  This patch adds dynamic alloca stubs support to GCCJIT.



DEF_BUILTIN_STUB only defines the enum for builtins instead of

providing the type. Therefore, builtins with stub will lead to

ICE before this patch. This applies to `alloca_with_align`,

`stack_save` and `stack_restore`.



This patch adds special handling for builtins defined by

DEF_BUILTIN_STUB.



Additionally, it fixes that supercontext is not

set for blocks emitted by gccjit. This triggers a SEGV error inside

`fold_builtin_with_align`.

gcc/jit/ChangeLog:



 * jit-builtins.cc (builtins_manager::make_builtin_function):

 (builtins_manager::make_type_for_stub):

 (builtins_manager::get_type_for_stub):

 (builtins_manager::get_attrs_tree):

 (builtins_manager::get_attrs_tree_for_stub):

 * jit-builtins.h:

 * jit-playback.cc (postprocess):



gcc/testsuite/ChangeLog:



 * jit.dg/test-aligned-alloca.c: New test.

 * jit.dg/test-stack-save-restore.c: New test.



---

 gcc/jit/jit-builtins.cc | 69 +-

 gcc/jit/jit-builtins.h | 7 +

 gcc/jit/jit-playback.cc | 1 +

 gcc/testsuite/jit.dg/test-aligned-alloca.c | 121 ++

 .../jit.dg/test-stack-save-restore.c | 114 +

 5 files changed, 309 insertions(+), 3 deletions(-)

 create mode 100644 gcc/testsuite/jit.dg/test-aligned-alloca.c

 create mode 100644 gcc/testsuite/jit.dg/test-stack-save-restore.c



diff --git a/gcc/jit/jit-builtins.cc b/gcc/jit/jit-builtins.cc

index 0c13c8db586..06affd66634 100644

--- a/gcc/jit/jit-builtins.cc

+++ b/gcc/jit/jit-builtins.cc

@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. If not see

 #include "target.h"

 #include "jit-playback.h"

 #include "stringpool.h"

+#include "tree-core.h"

 
 #include "jit-builtins.h"

 
@@ -185,7 +186,8 @@ builtins_manager::make_builtin_function (enum 
built_in_function builtin_id)

 {

 const struct builtin_data& bd = builtin_data[builtin_id];

 enum jit_builtin_type type_id = bd.type;

- recording::type *t = get_type (type_id);

+ recording::type *t = type_id == BT_LAST ? get_type_for_stub (builtin_id)

+ : get_type (type_id);

 if (!t)

 return NULL;

 recording::function_type *func_type = t->as_a_function_type ();

@@ -333,6 +335,52 @@ builtins_manager::get_type (enum jit_builtin_type type_id)

 return m_types[type_id];

 }

 
+/* Create the recording::type for special builtins whose types are not defined

+ in builtin-types.def. */

+

+recording::type *

+builtins_manager::make_type_for_stub (enum built_in_function builtin_id)

+{

+ switch (builtin_id)

+ {

+ default:

+ return reinterpret_cast (-1);

+ case BUILT_IN_ALLOCA_WITH_ALIGN:

+ {

+ recording::type *p = m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);

+ recording::type *r = m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);

+ recording::type *params[2] = { p, p };

+ return m_ctxt->new_function_type (r, 2, params, false);

+ }

+ case BUILT_IN_STACK_SAVE:

+ {

+ recording::type *r = m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);

+ return m_ctxt->new_function_type (r, 0, nullptr, false);

+ }

+ case BUILT_IN_STACK_RESTORE:

+ {

+ recording::type *p = m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);

+ recording::type *r = m_ctxt->get_type (GCC_JIT_TYPE_VOID);

+ recording::type *params[1] = { p };

+ return m_ctxt->new_function_type (r, 1, params, false);

+ }

+ }

+}

+

+/* Get the recording::type for a given type of builtin function,

+ by ID, creating it if it doesn't already exist. */

+

+recording::type *

+builtins_manager::get_type_for_stub (enum built_in_function type_id)

+{

+ if (m_types[type_id] == nullptr)

+ m_types[type_id] = make_type_for_stub (type_id);

+ recording::type *t = m_types[type_id];

+ if (reinterpret_cast (t) == -1)

+ return nullptr;

+ return t;

+}

+

 /* Create the recording::type for a given type of builtin function. */

 
 recording::type *

@@ -661,15 +709,30 @@ tree

 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)

 {

 enum built_in_attribute attr = builtin_data[builtin_id].attr;

+ if (attr == ATTR_LAST)

+ return get_attrs_tree_for_stub (builtin_id);

 return get_attrs_tree (attr);

 }

 
-/* As above, but for an enum built_in_attribute. */

+/* Get attributes for builtin stubs. */

+

+tree

+builtins_manager::get_attrs_tree_for_stub (enum built_in_function builtin_id)

+{

+ switch (builtin_id)

+ {

+ default:

+ return NULL_TREE;

+ case BUILT_IN_ALLOCA_WITH_ALIGN:

+ return get_attrs_tree (BUILT_IN_ALLOCA);

+ }

+}

+

+/* As get_attrs_tree, but for an enum built_in_attribute. */

 
 tree

 builtins_manager::get_attrs_tree (enum built_in_attribute attr)

 {

- gcc_assert (attr < ATTR_LAST);

 if (!m_attributes [attr])

 m_attributes [attr] = make_at

[PATCH v3] [GCCJIT] support dynamic alloca stub

2024-11-10 Thread Schrodinger ZHU Yifan
This patch adds dynamic alloca stubs support to GCCJIT.

DEF_BUILTIN_STUB only defines the enum for builtins instead of
providing the type. Therefore, builtins with stub will lead to
ICE before this patch. This applies to `alloca_with_align`,
`stack_save` and `stack_restore`.

This patch adds special handling for builtins defined by
DEF_BUILTIN_STUB.

Additionally, it fixes that supercontext is not
set for blocks emitted by gccjit. This triggers a SEGV error inside
`fold_builtin_with_align`.

This is the third roll of the patch:

- Fix wrong test cases mistakenly introduced in V2.
- Undo the removal of `gcc_assert` inside get_attrs_tree for non-stub builtin
  functions.

gcc/jit/ChangeLog:

* jit-builtins.cc (builtins_manager::make_builtin_function): Add stub 
type handling.
(builtins_manager::make_type_for_stub): Add stub type handling.
(builtins_manager::get_type_for_stub): Add stub type handling.
(builtins_manager::get_attrs_tree): Add stub attribute hand
ling.
(builtins_manager::get_attrs_tree_for_stub): Add stub attribute 
handling.
* jit-builtins.h: Add new functions for stubs.
* jit-playback.cc (postprocess): Always set supercontext.

gcc/testsuite/ChangeLog:

* jit.dg/test-aligned-alloca.c: New test.
* jit.dg/test-stack-save-restore.c: New test.
---
 gcc/jit/jit-builtins.cc   |  68 +-
 gcc/jit/jit-builtins.h|   7 +
 gcc/jit/jit-playback.cc   |   1 +
 gcc/testsuite/jit.dg/test-aligned-alloca.c| 121 ++
 .../jit.dg/test-stack-save-restore.c  | 114 +
 5 files changed, 309 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/jit.dg/test-aligned-alloca.c
 create mode 100644 gcc/testsuite/jit.dg/test-stack-save-restore.c

diff --git a/gcc/jit/jit-builtins.cc b/gcc/jit/jit-builtins.cc
index 0c13c8db586..5f61775beb7 100644
--- a/gcc/jit/jit-builtins.cc
+++ b/gcc/jit/jit-builtins.cc
@@ -23,
6 +23,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "target.h"
 #include "jit-playback.h"
 #include "stringpool.h"
+#include "tree-core.h"
 
 #include "jit-builtins.h"
 
@@ -185,7 +186,8 @@ builtins_manager::make_builtin_function (enum 
built_in_function builtin_id)
 {
   const struct builtin_data& bd = builtin_data[builtin_id];
   enum jit_builtin_type type_id = bd.type;
-  recording::type *t = get_type (type_id);
+  recording::type *t = type_id == BT_LAST ? get_type_for_stub (builtin_id)
+: get_type (type_id);
   if (!t)
 return NULL;
   recording::function_type *func_type = t->as_a_function_type ();
@@ -333,6 +335,52 @@ builtins_manager::get_type (enum jit_builtin_type type_id)
   return m_types[type_id];
 }
 
+/* Create the recording::type for special builtins whose types are not defined
+   in builtin-types.def.  */
+
+recording::type *
+builtins_manager::make_type_for_stub (enum built_in_function builtin_id)
+{
+  switch (bui
ltin_id)
+{
+default:
+  return reinterpret_cast (-1);
+case BUILT_IN_ALLOCA_WITH_ALIGN:
+  {
+   recording::type *p = m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);
+   recording::type *r = m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
+   recording::type *params[2] = { p, p };
+   return m_ctxt->new_function_type (r, 2, params, false);
+  }
+case BUILT_IN_STACK_SAVE:
+  {
+   recording::type *r = m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
+   return m_ctxt->new_function_type (r, 0, nullptr, false);
+  }
+case BUILT_IN_STACK_RESTORE:
+  {
+   recording::type *p = m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
+   recording::type *r = m_ctxt->get_type (GCC_JIT_TYPE_VOID);
+   recording::type *params[1] = { p };
+   return m_ctxt->new_function_type (r, 1, params, false);
+  }
+}
+}
+
+/* Get the recording::type for a given type of builtin function,
+   by ID, creating it if it doesn't already exist.  */
+
+recording::type *

+builtins_manager::get_type_for_stub (enum built_in_function type_id)
+{
+  if (m_types[type_id] == nullptr)
+m_types[type_id] = make_type_for_stub (type_id);
+  recording::type *t = m_types[type_id];
+  if (reinterpret_cast (t) == -1)
+return nullptr;
+  return t;
+}
+
 /* Create the recording::type for a given type of builtin function.  */
 
 recording::type *
@@ -661,10 +709,26 @@ tree
 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
 {
   enum built_in_attribute attr = builtin_data[builtin_id].attr;
+  if (attr == ATTR_LAST)
+return get_attrs_tree_for_stub (builtin_id);
   return get_attrs_tree (attr);
 }
 
-/* As above, but for an enum built_in_attribute.  */
+/* Get attributes for builtin stubs.  */
+
+tree
+builtins_manager::get_attrs_tree_for_stub (enum built_in_function builtin_id)
+{
+  switch (builtin_id)
+{
+default:
+  return NULL_TREE;
+case BUILT_IN_ALLOCA_WITH_ALIGN:
+  retu
rn get_attrs_tree (BUILT_IN_ALLOCA);

[PATCH v2] gccjit: support dynamic alloca stub

2024-11-10 Thread Schrodinger ZHU Yifan
This patch adds dynamic alloca stubs support to GCCJIT.

DEF_BUILTIN_STUB only defines the enum for builtins instead of
providing the type. Therefore, builtins with stub will lead to
ICE before this patch. This applies to `alloca_with_align`,
`stack_save` and `stack_restore`.

This patch adds special handling for builtins defined by
DEF_BUILTIN_STUB.

Additionally, it fixes that supercontext is not
set for blocks emitted by gccjit. This triggers a SEGV error inside
`fold_builtin_with_align`.
gcc/jit/ChangeLog:

* jit-builtins.cc (builtins_manager::make_builtin_function):
(builtins_manager::make_type_for_stub):
(builtins_manager::get_type_for_stub):
(builtins_manager::get_attrs_tree):
(builtins_manager::get_attrs_tree_for_stub):
* jit-builtins.h:
* jit-playback.cc (postprocess):

gcc/testsuite/ChangeLog:

* jit.dg/test-aligned-alloca.c: New test.
* jit.dg/test-stack-save-restore.c: New test.

---
 gcc/jit/jit-builtins.cc   |  69 
+-
 gcc/jit/jit-builtins.h|   7 +
 gcc/jit/jit-playback.cc   |   1 +
 gcc/testsuite/jit.dg/test-aligned-alloca.c| 121 ++
 .../jit.dg/test-stack-save-restore.c  | 114 +
 5 files changed, 309 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/jit.dg/test-aligned-alloca.c
 create mode 100644 gcc/testsuite/jit.dg/test-stack-save-restore.c

diff --git a/gcc/jit/jit-builtins.cc b/gcc/jit/jit-builtins.cc
index 0c13c8db586..06affd66634 100644
--- a/gcc/jit/jit-builtins.cc
+++ b/gcc/jit/jit-builtins.cc
@@ -23,6 +23,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "target.h"
 #include "jit-playback.h"
 #include "stringpool.h"
+#include "tree-core.h"
 
 #include "jit-builtins.h"
 
@@ -185,7 +186,8 @@ builtins_manager::make_builtin_function (enum 
built_in_function builtin_id)
 {
   const struct builtin_data& bd = builtin_data[builtin_id];
   enum jit
_builtin_type type_id = bd.type;
-  recording::type *t = get_type (type_id);
+  recording::type *t = type_id == BT_LAST ? get_type_for_stub (builtin_id)
+: get_type (type_id);
   if (!t)
 return NULL;
   recording::function_type *func_type = t->as_a_function_type ();
@@ -333,6 +335,52 @@ builtins_manager::get_type (enum jit_builtin_type type_id)
   return m_types[type_id];
 }
 
+/* Create the recording::type for special builtins whose types are not defined
+   in builtin-types.def.  */
+
+recording::type *
+builtins_manager::make_type_for_stub (enum built_in_function builtin_id)
+{
+  switch (builtin_id)
+{
+default:
+  return reinterpret_cast (-1);
+case BUILT_IN_ALLOCA_WITH_ALIGN:
+  {
+   recording::type *p = m_ctxt->get_type (GCC_JIT_TYPE_SIZE_T);
+   recording::type *r = m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
+   recording::type *params[2] = { p, p };
+   return m_ctxt->new_function_type (r, 2, params, false);
+ 
 }
+case BUILT_IN_STACK_SAVE:
+  {
+   recording::type *r = m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
+   return m_ctxt->new_function_type (r, 0, nullptr, false);
+  }
+case BUILT_IN_STACK_RESTORE:
+  {
+   recording::type *p = m_ctxt->get_type (GCC_JIT_TYPE_VOID_PTR);
+   recording::type *r = m_ctxt->get_type (GCC_JIT_TYPE_VOID);
+   recording::type *params[1] = { p };
+   return m_ctxt->new_function_type (r, 1, params, false);
+  }
+}
+}
+
+/* Get the recording::type for a given type of builtin function,
+   by ID, creating it if it doesn't already exist.  */
+
+recording::type *
+builtins_manager::get_type_for_stub (enum built_in_function type_id)
+{
+  if (m_types[type_id] == nullptr)
+m_types[type_id] = make_type_for_stub (type_id);
+  recording::type *t = m_types[type_id];
+  if (reinterpret_cast (t) == -1)
+return nullptr;
+  return t;
+}
+
 /* Create the recording::type for a given type of builtin function.  */

 
 recording::type *
@@ -661,15 +709,30 @@ tree
 builtins_manager::get_attrs_tree (enum built_in_function builtin_id)
 {
   enum built_in_attribute attr = builtin_data[builtin_id].attr;
+  if (attr == ATTR_LAST)
+return get_attrs_tree_for_stub (builtin_id);
   return get_attrs_tree (attr);
 }
 
-/* As above, but for an enum built_in_attribute.  */
+/* Get attributes for builtin stubs.  */
+
+tree
+builtins_manager::get_attrs_tree_for_stub (enum built_in_function builtin_id)
+{
+  switch (builtin_id)
+{
+default:
+  return NULL_TREE;
+case BUILT_IN_ALLOCA_WITH_ALIGN:
+  return get_attrs_tree (BUILT_IN_ALLOCA);
+}
+}
+
+/* As get_attrs_tree, but for an enum built_in_attribute.  */
 
 tree
 builtins_manager::get_attrs_tree (enum built_in_attribute attr)
 {
-  gcc_assert (attr < ATTR_LAST);
   if (!m_attributes [attr])
 m_attributes [attr] = make_attrs_tree (attr);
   return m_attributes [attr];
diff --git a/gcc/jit/jit-b
uiltins.h b/gcc/jit/jit-builtins.h
index 17e1