Re: [PATCH v2] IPA: Provide a mechanism to register static DTORs via cxa_atexit.

2021-11-13 Thread Jan Hubicka via Gcc-patches
> sheesh … EWRONGREVISEDPATCH
> 
> > On 5 Nov 2021, at 13:08, Iain Sandoe  wrote:
> > 
> > I tried enabling this on x86-64-linux (just for interest) and it seems to 
> > work
> > OK there too - but that testing revealed a thinko that didn’t show with a
> > a normal regstrap.
> 
> … now with the correct patch.
> 
> [PATCH v2] IPA: Provide a mechanism to register static DTORs via
>  cxa_atexit.
> 
> For at least one target (Darwin) the platform convention is to
> register static destructors (i.e. __attribute__((destructor)))
> with __cxa_atexit rather than placing them into a list that is
> run by some other mechanism.
> 
> This patch provides a target hook that allows a target to opt
> into this and handling for the process in ipa_cdtor_merge ().
> 
> When the mode is enabled (dtors_from_cxa_atexit is set) we:
> 
>  * Generate new CTORs to register static destructors with
>__cxa_atexit and add them to the existing list of CTORs;
>we then process the revised CTORs list.
> 
>  * We sort the DTORs into priority and then TU order, this
>means that they are registered in that order with
>__cxa_atexit () and therefore will be run in the reverse
>order.
> 
>  * Likewise, CTORs are sorted into priority and then TU order,
>which means that they will run in that order.
> 
> This matches the behavior of using init/fini (or
> mod_init_func/mod_term_func) sections.
> 
> Signed-off-by: Iain Sandoe 
> 
> gcc/ChangeLog:
> 
>   * config/darwin.h (TARGET_DTORS_FROM_CXA_ATEXIT): New.
>   * doc/tm.texi: Regenerated.
>   * doc/tm.texi.in: Add TARGET_DTORS_FROM_CXA_ATEXIT hook.
>   * ipa.c (ipa_discover_variable_flags):
>   (cgraph_build_static_cdtor_1): Return the built function
>   decl.
>   (build_cxa_atexit_decl): New.
>   (build_dso_handle_decl): New.
>   (build_cxa_dtor_registrations): New.
>   (compare_cdtor_tu_order): New.
>   (build_cxa_atexit_fns): New.
>   (ipa_cdtor_merge): If dtors_from_cxa_atexit is set,
>   process the DTORs/CTORs accordingly.
>   (pass_ipa_cdtor_merge::gate): Also run if
>   dtors_from_cxa_atexit is set.
>   * target.def (dtors_from_cxa_atexit): New hook.

OK, thanks!
Honza


Re: [PATCH v2] IPA: Provide a mechanism to register static DTORs via cxa_atexit.

2021-11-05 Thread Iain Sandoe via Gcc-patches
sheesh … EWRONGREVISEDPATCH

> On 5 Nov 2021, at 13:08, Iain Sandoe  wrote:
> 
> I tried enabling this on x86-64-linux (just for interest) and it seems to work
> OK there too - but that testing revealed a thinko that didn’t show with a
> a normal regstrap.

… now with the correct patch.

[PATCH v2] IPA: Provide a mechanism to register static DTORs via
 cxa_atexit.

For at least one target (Darwin) the platform convention is to
register static destructors (i.e. __attribute__((destructor)))
with __cxa_atexit rather than placing them into a list that is
run by some other mechanism.

This patch provides a target hook that allows a target to opt
into this and handling for the process in ipa_cdtor_merge ().

When the mode is enabled (dtors_from_cxa_atexit is set) we:

 * Generate new CTORs to register static destructors with
   __cxa_atexit and add them to the existing list of CTORs;
   we then process the revised CTORs list.

 * We sort the DTORs into priority and then TU order, this
   means that they are registered in that order with
   __cxa_atexit () and therefore will be run in the reverse
   order.

 * Likewise, CTORs are sorted into priority and then TU order,
   which means that they will run in that order.

This matches the behavior of using init/fini (or
mod_init_func/mod_term_func) sections.

Signed-off-by: Iain Sandoe 

gcc/ChangeLog:

* config/darwin.h (TARGET_DTORS_FROM_CXA_ATEXIT): New.
* doc/tm.texi: Regenerated.
* doc/tm.texi.in: Add TARGET_DTORS_FROM_CXA_ATEXIT hook.
* ipa.c (ipa_discover_variable_flags):
(cgraph_build_static_cdtor_1): Return the built function
decl.
(build_cxa_atexit_decl): New.
(build_dso_handle_decl): New.
(build_cxa_dtor_registrations): New.
(compare_cdtor_tu_order): New.
(build_cxa_atexit_fns): New.
(ipa_cdtor_merge): If dtors_from_cxa_atexit is set,
process the DTORs/CTORs accordingly.
(pass_ipa_cdtor_merge::gate): Also run if
dtors_from_cxa_atexit is set.
* target.def (dtors_from_cxa_atexit): New hook.
---
 gcc/config/darwin.h |   7 +-
 gcc/doc/tm.texi |   8 ++
 gcc/doc/tm.texi.in  |   2 +
 gcc/ipa.c   | 200 +++-
 gcc/target.def  |  10 +++
 5 files changed, 222 insertions(+), 5 deletions(-)

diff --git a/gcc/config/darwin.h b/gcc/config/darwin.h
index 27cb3e4bb30..2b19fb7c085 100644
--- a/gcc/config/darwin.h
+++ b/gcc/config/darwin.h
@@ -54,6 +54,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If 
not, see
 
 #define DO_GLOBAL_DTORS_BODY
 
+/* Register static destructors to run from __cxa_atexit instead of putting
+   them into a .mod_term_funcs section.  */
+
+#define TARGET_DTORS_FROM_CXA_ATEXIT true
+
 /* The string value for __SIZE_TYPE__.  */
 
 #ifndef SIZE_TYPE
@@ -1160,7 +1165,7 @@ extern void darwin_driver_init (unsigned int *,struct 
cl_decoded_option **);
 
 /* The Apple assembler and linker do not support constructor priorities.  */
 #undef SUPPORTS_INIT_PRIORITY
-#define SUPPORTS_INIT_PRIORITY 0
+#define SUPPORTS_INIT_PRIORITY 1
 
 #undef STACK_CHECK_STATIC_BUILTIN
 #define STACK_CHECK_STATIC_BUILTIN 1
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index 990152f5b15..0a4df18b825 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -9233,6 +9233,14 @@ collecting constructors and destructors to be run at 
startup and exit.
 It is false if we must use @command{collect2}.
 @end deftypevr
 
+@deftypevr {Target Hook} bool TARGET_DTORS_FROM_CXA_ATEXIT
+This value is true if the target wants destructors to be queued to be
+run from __cxa_atexit.  If this is the case then, for each priority level,
+a new constructor will be entered that registers the destructors for that
+level with __cxa_atexit (and there will be no destructors emitted).
+It is false the method implied by @code{have_ctors_dtors} is used.
+@end deftypevr
+
 @deftypefn {Target Hook} void TARGET_ASM_CONSTRUCTOR (rtx @var{symbol}, int 
@var{priority})
 If defined, a function that outputs assembler code to arrange to call
 the function referenced by @var{symbol} at initialization time.
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
index 193c9bdd853..c733f356fe4 100644
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -6021,6 +6021,8 @@ encountering an @code{init_priority} attribute.
 
 @hook TARGET_HAVE_CTORS_DTORS
 
+@hook TARGET_DTORS_FROM_CXA_ATEXIT
+
 @hook TARGET_ASM_CONSTRUCTOR
 
 @hook TARGET_ASM_DESTRUCTOR
diff --git a/gcc/ipa.c b/gcc/ipa.c
index 4f62ac183ee..325b658b55e 100644
--- a/gcc/ipa.c
+++ b/gcc/ipa.c
@@ -837,7 +837,7 @@ ipa_discover_variable_flags (void)
FINAL specify whether the externally visible name for collect2 should
be produced. */
 
-static void
+static tree
 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final,
 tree optimization,
 tree target)
@

[PATCH v2] IPA: Provide a mechanism to register static DTORs via cxa_atexit.

2021-11-05 Thread Iain Sandoe via Gcc-patches


I tried enabling this on x86-64-linux (just for interest) and it seems to work
OK there too - but that testing revealed a thinko that didn’t show with a
a normal regstrap.


 
 Changes from original post:
 1. amended a comment
 2. fixed a thinko where I was not allowing for functions declared as
*both* CTOR and DTOR.

For at least one target (Darwin) the platform convention is to
register static destructors (i.e. __attribute__((destructor)))
with __cxa_atexit rather than placing them into a list that is
run by some other mechanism.

This patch provides a target hook that allows a target to opt
into this and handling for the process in ipa_cdtor_merge ().

When the mode is enabled (dtors_from_cxa_atexit is set) we:

 * Generate new CTORs to register static destructors with
   __cxa_atexit and add them to the existing list of CTORs;
   we then process the revised CTORs list.

 * We sort the DTORs into priority and then TU order, this
   means that they are registered in that order with
   __cxa_atexit () and therefore will be run in the reverse
   order.

 * Likewise, CTORs are sorted into priority and then TU order,
   which means that they will run in that order.

This matches the behavior of using init/fini (or
mod_init_func/mod_term_func) sections.

Signed-off-by: Iain Sandoe 

gcc/ChangeLog:

* config/darwin.h (TARGET_DTORS_FROM_CXA_ATEXIT): New.
* doc/tm.texi: Regenerated.
* doc/tm.texi.in: Add TARGET_DTORS_FROM_CXA_ATEXIT hook.
* ipa.c (ipa_discover_variable_flags):
(cgraph_build_static_cdtor_1): Return the built function
decl.
(build_cxa_atexit_decl): New.
(build_dso_handle_decl): New.
(build_cxa_dtor_registrations): New.
(compare_cdtor_tu_order): New.
(build_cxa_atexit_fns): New.
(ipa_cdtor_merge): If dtors_from_cxa_atexit is set,
process the DTORs/CTORs accordingly.
(pass_ipa_cdtor_merge::gate): Also run if
dtors_from_cxa_atexit is set.
* target.def (dtors_from_cxa_atexit): New hook.
---
 gcc/config/darwin.h |   5 ++
 gcc/doc/tm.texi |   8 ++
 gcc/doc/tm.texi.in  |   2 +
 gcc/ipa.c   | 201 +++-
 gcc/target.def  |  10 +++
 5 files changed, 222 insertions(+), 4 deletions(-)

diff --git a/gcc/config/darwin.h b/gcc/config/darwin.h
index 27cb3e4bb30..5202903f5b2 100644
--- a/gcc/config/darwin.h
+++ b/gcc/config/darwin.h
@@ -54,6 +54,11 @@ see the files COPYING3 and COPYING.RUNTIME respectively.  If 
not, see
 
 #define DO_GLOBAL_DTORS_BODY
 
+/* Register static destructors to run from __cxa_atexit instead of putting
+   them into a .mod_term_funcs section.  */
+
+#define TARGET_DTORS_FROM_CXA_ATEXIT true
+
 /* The string value for __SIZE_TYPE__.  */
 
 #ifndef SIZE_TYPE
diff --git a/gcc/doc/tm.texi b/gcc/doc/tm.texi
index 78a1af1ad4d..6ec1d50b3e4 100644
--- a/gcc/doc/tm.texi
+++ b/gcc/doc/tm.texi
@@ -9210,6 +9210,14 @@ collecting constructors and destructors to be run at 
startup and exit.
 It is false if we must use @command{collect2}.
 @end deftypevr
 
+@deftypevr {Target Hook} bool TARGET_DTORS_FROM_CXA_ATEXIT
+This value is true if the target wants destructors to be queued to be
+run from __cxa_atexit.  If this is the case then, for each priority level,
+a new constructor will be entered that registers the destructors for that
+level with __cxa_atexit (and there will be no destructors emitted).
+It is false the method implied by @code{have_ctors_dtors} is used.
+@end deftypevr
+
 @deftypefn {Target Hook} void TARGET_ASM_CONSTRUCTOR (rtx @var{symbol}, int 
@var{priority})
 If defined, a function that outputs assembler code to arrange to call
 the function referenced by @var{symbol} at initialization time.
diff --git a/gcc/doc/tm.texi.in b/gcc/doc/tm.texi.in
index 4401550989e..2b9960b73d7 100644
--- a/gcc/doc/tm.texi.in
+++ b/gcc/doc/tm.texi.in
@@ -6015,6 +6015,8 @@ encountering an @code{init_priority} attribute.
 
 @hook TARGET_HAVE_CTORS_DTORS
 
+@hook TARGET_DTORS_FROM_CXA_ATEXIT
+
 @hook TARGET_ASM_CONSTRUCTOR
 
 @hook TARGET_ASM_DESTRUCTOR
diff --git a/gcc/ipa.c b/gcc/ipa.c
index 4f62ac183ee..d234a69b9fe 100644
--- a/gcc/ipa.c
+++ b/gcc/ipa.c
@@ -837,7 +837,7 @@ ipa_discover_variable_flags (void)
FINAL specify whether the externally visible name for collect2 should
be produced. */
 
-static void
+static tree
 cgraph_build_static_cdtor_1 (char which, tree body, int priority, bool final,
 tree optimization,
 tree target)
@@ -916,6 +916,7 @@ cgraph_build_static_cdtor_1 (char which, tree body, int 
priority, bool final,
 
   set_cfun (NULL);
   current_function_decl = NULL;
+  return decl;
 }
 
 /* Generate and emit a static constructor or destructor.  WHICH must
@@ -1022,6 +1023,128 @@ build_cdtor (bool ctor_p, const vec )
 }
 }
 
+/* Helper functions for build_cxa_dtor_registrations ().
+   Build a decl for __cxa_atexit ().  */
+
+static