https://gcc.gnu.org/g:91c35e3c317686b9c45cdfc72f0c4e7103c1fccd

commit 91c35e3c317686b9c45cdfc72f0c4e7103c1fccd
Author: Tobias Burnus <tbur...@baylibre.com>
Date:   Tue Sep 3 12:02:23 2024 +0200

    LTO/WPA: Ensure that output_offload_tables only writes table once [PR116535]
    
    When ltrans was written concurrently, e.g. via -flto=N (N > 1, assuming
    sufficient partiations, e.g., via -flto-partition=max), 
output_offload_tables
    wrote the output tables once per fork.
    
            PR lto/116535
    
    gcc/ChangeLog:
    
            * lto-cgraph.cc (output_offload_tables): Remove offload_ frees.
            * lto-streamer-out.cc (lto_output): Make call to it depend on
            lto_get_out_decl_state ()->output_offload_tables_p.
            * lto-streamer.h (struct lto_out_decl_state): Add
            output_offload_tables_p field.
            * tree-pass.h (ipa_write_optimization_summaries): Add bool argument.
            * passes.cc (ipa_write_summaries_1): Add bool
            output_offload_tables_p arg.
            (ipa_write_summaries): Update call.
            (ipa_write_optimization_summaries): Accept output_offload_tables_p.
    
    gcc/lto/ChangeLog:
    
            * lto.cc (stream_out): Update call to
            ipa_write_optimization_summaries to pass true for first partition.
    
    (cherry picked from commit 2fcccf21a34f92ea060b492c9b2aecb56cd5d167)

Diff:
---
 gcc/ChangeLog.omp       | 17 +++++++++++++++++
 gcc/lto-cgraph.cc       | 10 ----------
 gcc/lto-streamer-out.cc |  3 ++-
 gcc/lto-streamer.h      |  3 +++
 gcc/lto/ChangeLog.omp   |  8 ++++++++
 gcc/lto/lto.cc          |  2 +-
 gcc/passes.cc           | 11 ++++++++---
 gcc/tree-pass.h         |  3 ++-
 8 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/gcc/ChangeLog.omp b/gcc/ChangeLog.omp
index 481899b4d73e..938926d8f85d 100644
--- a/gcc/ChangeLog.omp
+++ b/gcc/ChangeLog.omp
@@ -1,3 +1,20 @@
+2024-12-18  Thomas Schwinge  <tschwi...@baylibre.com>
+
+       Backported from trunk:
+       2024-09-03  Tobias Burnus  <tbur...@baylibre.com>
+
+       PR lto/116535
+       * lto-cgraph.cc (output_offload_tables): Remove offload_ frees.
+       * lto-streamer-out.cc (lto_output): Make call to it depend on
+       lto_get_out_decl_state ()->output_offload_tables_p.
+       * lto-streamer.h (struct lto_out_decl_state): Add
+       output_offload_tables_p field.
+       * tree-pass.h (ipa_write_optimization_summaries): Add bool argument.
+       * passes.cc (ipa_write_summaries_1): Add bool
+       output_offload_tables_p arg.
+       (ipa_write_summaries): Update call.
+       (ipa_write_optimization_summaries): Accept output_offload_tables_p.
+
 2024-08-01  Tobias Burnus  <tbur...@baylibre.com>
 
        Backported from master:
diff --git a/gcc/lto-cgraph.cc b/gcc/lto-cgraph.cc
index a3dc76f219f8..768e009c100a 100644
--- a/gcc/lto-cgraph.cc
+++ b/gcc/lto-cgraph.cc
@@ -1135,16 +1135,6 @@ output_offload_tables (void)
 
   streamer_write_uhwi_stream (ob->main_stream, 0);
   lto_destroy_simple_output_block (ob);
-
-  /* In WHOPR mode during the WPA stage the joint offload tables need to be
-     streamed to one partition only.  That's why we free offload_funcs and
-     offload_vars after the first call of output_offload_tables.  */
-  if (flag_wpa)
-    {
-      vec_free (offload_funcs);
-      vec_free (offload_vars);
-      vec_free (offload_ind_funcs);
-    }
 }
 
 /* Verify the partitioning of NODE.  */
diff --git a/gcc/lto-streamer-out.cc b/gcc/lto-streamer-out.cc
index 42456d348236..897f528bac74 100644
--- a/gcc/lto-streamer-out.cc
+++ b/gcc/lto-streamer-out.cc
@@ -2835,7 +2835,8 @@ lto_output (void)
      statements using the statement UIDs.  */
   output_symtab ();
 
-  output_offload_tables ();
+  if (lto_get_out_decl_state ()->output_offload_tables_p)
+    output_offload_tables ();
 
   if (flag_checking)
     {
diff --git a/gcc/lto-streamer.h b/gcc/lto-streamer.h
index 895aa27692b7..3962900f0472 100644
--- a/gcc/lto-streamer.h
+++ b/gcc/lto-streamer.h
@@ -535,6 +535,9 @@ struct lto_out_decl_state
 
   /* True if decl state is compressed.  */
   bool compressed;
+
+  /* True if offload tables should be output. */
+  bool output_offload_tables_p;
 };
 
 typedef struct lto_out_decl_state *lto_out_decl_state_ptr;
diff --git a/gcc/lto/ChangeLog.omp b/gcc/lto/ChangeLog.omp
new file mode 100644
index 000000000000..c43b9fc540b1
--- /dev/null
+++ b/gcc/lto/ChangeLog.omp
@@ -0,0 +1,8 @@
+2024-12-18  Thomas Schwinge  <tschwi...@baylibre.com>
+
+       Backported from trunk:
+       2024-09-03  Tobias Burnus  <tbur...@baylibre.com>
+
+       PR lto/116535
+       * lto.cc (stream_out): Update call to
+       ipa_write_optimization_summaries to pass true for first partition.
diff --git a/gcc/lto/lto.cc b/gcc/lto/lto.cc
index 91aa2fbddb4e..ce704189d095 100644
--- a/gcc/lto/lto.cc
+++ b/gcc/lto/lto.cc
@@ -176,7 +176,7 @@ stream_out (char *temp_filename, lto_symtab_encoder_t 
encoder, int part)
 
   gcc_assert (!dump_file);
   streamer_dump_file = dump_begin (TDI_lto_stream_out, NULL, part);
-  ipa_write_optimization_summaries (encoder);
+  ipa_write_optimization_summaries (encoder, part == 0);
 
   free (CONST_CAST (char *, file->filename));
 
diff --git a/gcc/passes.cc b/gcc/passes.cc
index e9d18b9d22e1..74a34dd19851 100644
--- a/gcc/passes.cc
+++ b/gcc/passes.cc
@@ -2829,11 +2829,13 @@ ipa_write_summaries_2 (opt_pass *pass, struct 
lto_out_decl_state *state)
    summaries.  SET is the set of nodes to be written.  */
 
 static void
-ipa_write_summaries_1 (lto_symtab_encoder_t encoder)
+ipa_write_summaries_1 (lto_symtab_encoder_t encoder,
+                      bool output_offload_tables_p)
 {
   pass_manager *passes = g->get_passes ();
   struct lto_out_decl_state *state = lto_new_out_decl_state ();
   state->symtab_node_encoder = encoder;
+  state->output_offload_tables_p = output_offload_tables_p;
 
   lto_output_init_mode_table ();
   lto_push_out_decl_state (state);
@@ -2896,7 +2898,8 @@ ipa_write_summaries (void)
     if (vnode->need_lto_streaming)
       lto_set_symtab_encoder_in_partition (encoder, vnode);
 
-  ipa_write_summaries_1 (compute_ltrans_boundary (encoder));
+  ipa_write_summaries_1 (compute_ltrans_boundary (encoder),
+                        flag_generate_offload);
 
   free (order);
   if (streamer_dump_file)
@@ -2951,10 +2954,12 @@ ipa_write_optimization_summaries_1 (opt_pass *pass,
    NULL, write out all summaries of all nodes. */
 
 void
-ipa_write_optimization_summaries (lto_symtab_encoder_t encoder)
+ipa_write_optimization_summaries (lto_symtab_encoder_t encoder,
+                                 bool output_offload_tables_p)
 {
   struct lto_out_decl_state *state = lto_new_out_decl_state ();
   state->symtab_node_encoder = encoder;
+  state->output_offload_tables_p = output_offload_tables_p;
 
   lto_output_init_mode_table ();
   lto_push_out_decl_state (state);
diff --git a/gcc/tree-pass.h b/gcc/tree-pass.h
index 29267589eeb3..a2c8f685bf3c 100644
--- a/gcc/tree-pass.h
+++ b/gcc/tree-pass.h
@@ -680,7 +680,8 @@ extern void emergency_dump_function (void);
 extern void print_current_pass (FILE *);
 extern void debug_pass (void);
 extern void ipa_write_summaries (void);
-extern void ipa_write_optimization_summaries (struct lto_symtab_encoder_d *);
+extern void ipa_write_optimization_summaries (struct lto_symtab_encoder_d *,
+                                             bool);
 extern void ipa_read_summaries (void);
 extern void ipa_read_optimization_summaries (void);
 extern void register_one_dump_file (opt_pass *);

Reply via email to