• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

GCC with patches for OS216


Commit MetaInfo

修订版1b05b4153a8c6f6fad1457ba15440825151cc51c (tree)
时间2020-05-30 11:16:35
作者Giuliano Belinassi <giuliano.belinassi@usp....>
CommiterGiuliano Belinassi

Log Message

Run partitioner after IPA.

We now run the partitioner after IPA analysis. We still require to
manage the assembler file to open multiple instances of these, as well
as check why some "safety" checks require to be removed.

gcc/ChangeLog
2020-05-29 Giuliano Belinassi <giuliano.belinassi@usp.br>

* Makefile.in (lto-partition.o): Add.
* cgraph.c (release_function_body): Remove dom_info_available_p
check.
* cgraphunit.c (ipa_passes): Run partitioner after
execute_ipa_summary_passes.
* ipa-visibility.c (optimize_weakref): Remove whole_program or
in_lto_p check.
* lto-cgraph.c (lto_apply_partition_mask): New.
* lto-streamer.h (ltrans_partition_def: Declare.
* (lto_apply_partition_mask): Expose symbol externally.

更改概述

差异

--- a/gcc/ChangeLog
+++ b/gcc/ChangeLog
@@ -1,3 +1,16 @@
1+2020-05-29 Giuliano Belinassi <giuliano.belinassi@usp.br>
2+
3+ * Makefile.in (lto-partition.o): Add.
4+ * cgraph.c (release_function_body): Remove dom_info_available_p
5+ check.
6+ * cgraphunit.c (ipa_passes): Run partitioner after
7+ execute_ipa_summary_passes.
8+ * ipa-visibility.c (optimize_weakref): Remove whole_program or
9+ in_lto_p check.
10+ * lto-cgraph.c (lto_apply_partition_mask): New.
11+ * lto-streamer.h (ltrans_partition_def: Declare.
12+ * (lto_apply_partition_mask): Expose symbol externally.
13+
114 2020-05-20 Giuliano Belinassi <giuliano.belinassi@usp.br>
215
316 * gcc.c (get_file_by_lines): Accept a path to file instead of FILE
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1449,6 +1449,7 @@ OBJS = \
14491449 lra-spills.o \
14501450 lto-cgraph.o \
14511451 lto-streamer.o \
1452+ lto-partition.o \
14521453 lto-streamer-in.o \
14531454 lto-streamer-out.o \
14541455 lto-section-in.o \
--- a/gcc/cgraph.c
+++ b/gcc/cgraph.c
@@ -1791,8 +1791,9 @@ release_function_body (tree decl)
17911791 }
17921792 if (fn->cfg)
17931793 {
1794- gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
1795- gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
1794+ /* FIXME: Why these assertions are necessary? */
1795+ //gcc_assert (!dom_info_available_p (fn, CDI_DOMINATORS));
1796+ //gcc_assert (!dom_info_available_p (fn, CDI_POST_DOMINATORS));
17961797 delete_tree_cfg_annotations (fn);
17971798 clear_edges (fn);
17981799 fn->cfg = NULL;
--- a/gcc/cgraphunit.c
+++ b/gcc/cgraphunit.c
@@ -206,6 +206,7 @@ along with GCC; see the file COPYING3. If not see
206206 #include "stringpool.h"
207207 #include "attribs.h"
208208 #include "ipa-inline.h"
209+#include "lto-partition.h"
209210
210211 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
211212 secondary queue used during optimization to accommodate passes that
@@ -2633,6 +2634,10 @@ ipa_passes (void)
26332634
26342635 if (!in_lto_p)
26352636 {
2637+ struct symtab_node *node;
2638+ int partitions, i, pid;
2639+ int *pids;
2640+
26362641 /* Generate coverage variables and constructors. */
26372642 coverage_finish ();
26382643
@@ -2643,8 +2648,72 @@ ipa_passes (void)
26432648
26442649 execute_ipa_summary_passes
26452650 ((ipa_opt_pass_d *) passes->all_regular_ipa_passes);
2651+
2652+ symtab_node::checking_verify_symtab_nodes ();
2653+
2654+ /* Use max map for now for debugging. */
2655+ lto_max_map ();
2656+
2657+ /* AUX pointers are used by partitioning code to bookkeep number of
2658+ partitions symbol is in. This is no longer needed. */
2659+ FOR_EACH_SYMBOL (node)
2660+ node->aux = NULL;
2661+
2662+ /* Find out statics that need to be promoted
2663+ to globals with hidden visibility because they are accessed from multiple
2664+ partitions. */
2665+ lto_promote_cross_file_statics ();
2666+
2667+ partitions = ltrans_partitions.length ();
2668+ pids = (int *) alloca (partitions * sizeof (*pids));
2669+
2670+
2671+ for (i = 0; i < partitions; ++i)
2672+ {
2673+ pid = fork ();
2674+ if (pid == 0)
2675+ {
2676+ printf ("Process %d\n", i);
2677+ lto_apply_partition_mask (ltrans_partitions[i]);
2678+ goto continue_compilation;
2679+ }
2680+ else
2681+ {
2682+ int wstatus;
2683+ waitpid (pid, &wstatus, 0);
2684+ }
2685+ }
2686+ exit(0);
2687+
2688+ /*
2689+ for (i = 0; i < partitions; ++i)
2690+ {
2691+ pid = fork ();
2692+ if (pid == 0)
2693+ {
2694+ // Child.
2695+ lto_apply_partition_mask (ltrans_partitions[i]);
2696+ goto continue_compilation;
2697+ }
2698+ else
2699+ {
2700+ // Parent.
2701+ pids[i] = partitions;
2702+ }
2703+ }
2704+
2705+ for (i = 0; i < partitions; ++i)
2706+ {
2707+ int wstatus;
2708+ waitpid (pids[i], &wstatus, 0);
2709+ }
2710+
2711+ exit(0);
2712+ */
26462713 }
26472714
2715+ continue_compilation:
2716+
26482717 /* Some targets need to handle LTO assembler output specially. */
26492718 if (flag_generate_lto || flag_generate_offload)
26502719 targetm.asm_out.lto_start ();
--- a/gcc/ipa-visibility.c
+++ b/gcc/ipa-visibility.c
@@ -540,7 +540,8 @@ optimize_weakref (symtab_node *node)
540540 static void
541541 localize_node (bool whole_program, symtab_node *node)
542542 {
543- gcc_assert (whole_program || in_lto_p || !TREE_PUBLIC (node->decl));
543+ /* FIXME: This seems not to be necessary when compiling in fsplit-args. */
544+ //gcc_assert (whole_program || in_lto_p || !TREE_PUBLIC (node->decl));
544545
545546 /* It is possible that one comdat group contains both hidden and non-hidden
546547 symbols. In this case we can privatize all hidden symbol but we need
--- a/gcc/lto-cgraph.c
+++ b/gcc/lto-cgraph.c
@@ -39,6 +39,7 @@ along with GCC; see the file COPYING3. If not see
3939 #include "omp-offload.h"
4040 #include "stringpool.h"
4141 #include "attribs.h"
42+#include "lto-partition.h"
4243
4344 /* True when asm nodes has been output. */
4445 bool asm_nodes_output = false;
@@ -2059,3 +2060,37 @@ input_cgraph_opt_summary (vec<symtab_node *> nodes)
20592060 input_cgraph_opt_section (file_data, data, len, nodes);
20602061 }
20612062 }
2063+
2064+/* Replace the partition in the symbol table, removing every node which is not
2065+ in partition. */
2066+
2067+void
2068+lto_apply_partition_mask (ltrans_partition partition)
2069+{
2070+ vec<lto_encoder_entry> &nodes = partition->encoder->nodes;
2071+ symtab_node *node;
2072+ auto_vec<symtab_node *, 16> mark_to_remove;
2073+ int i;
2074+
2075+ for (i = 0; i < nodes.length (); i++)
2076+ {
2077+ symtab_node *node = nodes[i].node;
2078+ node->aux = (void *) 1;
2079+
2080+ if (!nodes[i].in_partition)
2081+ node->in_other_partition = true;
2082+ }
2083+
2084+ FOR_EACH_SYMBOL (node)
2085+ if (!node->aux)
2086+ mark_to_remove.safe_push (node);
2087+
2088+ FOR_EACH_SYMBOL (node)
2089+ node->aux = NULL;
2090+
2091+ for (i = 0; i < mark_to_remove.length (); i++)
2092+ {
2093+ symtab_node *node = mark_to_remove[i];
2094+ node->remove ();
2095+ }
2096+}
--- /dev/null
+++ b/gcc/lto-partition.c
@@ -0,0 +1,1193 @@
1+/* LTO partitioning logic routines.
2+ Copyright (C) 2009-2020 Free Software Foundation, Inc.
3+
4+This file is part of GCC.
5+
6+GCC is free software; you can redistribute it and/or modify it under
7+the terms of the GNU General Public License as published by the Free
8+Software Foundation; either version 3, or (at your option) any later
9+version.
10+
11+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12+WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+for more details.
15+
16+You should have received a copy of the GNU General Public License
17+along with GCC; see the file COPYING3. If not see
18+<http://www.gnu.org/licenses/>. */
19+
20+#include "config.h"
21+#include "system.h"
22+#include "coretypes.h"
23+#include "target.h"
24+#include "function.h"
25+#include "basic-block.h"
26+#include "tree.h"
27+#include "gimple.h"
28+#include "alloc-pool.h"
29+#include "stringpool.h"
30+#include "cgraph.h"
31+#include "lto-streamer.h"
32+#include "symbol-summary.h"
33+#include "tree-vrp.h"
34+#include "ipa-prop.h"
35+#include "ipa-fnsummary.h"
36+#include "lto-partition.h"
37+#include "sreal.h"
38+
39+vec<ltrans_partition> ltrans_partitions;
40+
41+static void add_symbol_to_partition (ltrans_partition part, symtab_node *node);
42+
43+
44+/* Helper for qsort; compare partitions and return one with smaller order. */
45+
46+static int
47+cmp_partitions_order (const void *a, const void *b)
48+{
49+ const struct ltrans_partition_def *pa
50+ = *(struct ltrans_partition_def *const *)a;
51+ const struct ltrans_partition_def *pb
52+ = *(struct ltrans_partition_def *const *)b;
53+ int ordera = -1, orderb = -1;
54+
55+ if (lto_symtab_encoder_size (pa->encoder))
56+ ordera = lto_symtab_encoder_deref (pa->encoder, 0)->order;
57+ if (lto_symtab_encoder_size (pb->encoder))
58+ orderb = lto_symtab_encoder_deref (pb->encoder, 0)->order;
59+ return orderb - ordera;
60+}
61+
62+/* Create new partition with name NAME. */
63+
64+static ltrans_partition
65+new_partition (const char *name)
66+{
67+ ltrans_partition part = XCNEW (struct ltrans_partition_def);
68+ part->encoder = lto_symtab_encoder_new (false);
69+ part->name = name;
70+ part->insns = 0;
71+ part->symbols = 0;
72+ ltrans_partitions.safe_push (part);
73+ return part;
74+}
75+
76+/* Free memory used by ltrans datastructures. */
77+
78+void
79+free_ltrans_partitions (void)
80+{
81+ unsigned int idx;
82+ ltrans_partition part;
83+ for (idx = 0; ltrans_partitions.iterate (idx, &part); idx++)
84+ {
85+ if (part->initializers_visited)
86+ delete part->initializers_visited;
87+ /* Symtab encoder is freed after streaming. */
88+ free (part);
89+ }
90+ ltrans_partitions.release ();
91+}
92+
93+/* Return true if symbol is already in some partition. */
94+
95+static inline bool
96+symbol_partitioned_p (symtab_node *node)
97+{
98+ return node->aux;
99+}
100+
101+/* Add references into the partition. */
102+static void
103+add_references_to_partition (ltrans_partition part, symtab_node *node)
104+{
105+ int i;
106+ struct ipa_ref *ref = NULL;
107+
108+ /* Add all duplicated references to the partition. */
109+ for (i = 0; node->iterate_reference (i, ref); i++)
110+ if (ref->referred->get_partitioning_class () == SYMBOL_DUPLICATE)
111+ add_symbol_to_partition (part, ref->referred);
112+ /* References to a readonly variable may be constant foled into its value.
113+ Recursively look into the initializers of the constant variable and add
114+ references, too. */
115+ else if (is_a <varpool_node *> (ref->referred)
116+ && (dyn_cast <varpool_node *> (ref->referred)
117+ ->ctor_useable_for_folding_p ())
118+ && !lto_symtab_encoder_in_partition_p (part->encoder, ref->referred))
119+ {
120+ if (!part->initializers_visited)
121+ part->initializers_visited = new hash_set<symtab_node *>;
122+ if (!part->initializers_visited->add (ref->referred))
123+ add_references_to_partition (part, ref->referred);
124+ }
125+}
126+
127+/* Helper function for add_symbol_to_partition doing the actual dirty work
128+ of adding NODE to PART. */
129+
130+static bool
131+add_symbol_to_partition_1 (ltrans_partition part, symtab_node *node)
132+{
133+ enum symbol_partitioning_class c = node->get_partitioning_class ();
134+ struct ipa_ref *ref;
135+ symtab_node *node1;
136+
137+ /* If NODE is already there, we have nothing to do. */
138+ if (lto_symtab_encoder_in_partition_p (part->encoder, node))
139+ return true;
140+
141+ /* non-duplicated aliases or tunks of a duplicated symbol needs to be output
142+ just once.
143+
144+ Be lax about comdats; they may or may not be duplicated and we may
145+ end up in need to duplicate keyed comdat because it has unkeyed alias. */
146+ if (c == SYMBOL_PARTITION && !DECL_COMDAT (node->decl)
147+ && symbol_partitioned_p (node))
148+ return false;
149+
150+ /* Be sure that we never try to duplicate partitioned symbol
151+ or add external symbol. */
152+ gcc_assert (c != SYMBOL_EXTERNAL
153+ && (c == SYMBOL_DUPLICATE || !symbol_partitioned_p (node)));
154+
155+ part->symbols++;
156+
157+ lto_set_symtab_encoder_in_partition (part->encoder, node);
158+
159+ if (symbol_partitioned_p (node))
160+ {
161+ node->in_other_partition = 1;
162+ if (dump_file)
163+ fprintf (dump_file,
164+ "Symbol node %s now used in multiple partitions\n",
165+ node->dump_name ());
166+ }
167+ node->aux = (void *)((size_t)node->aux + 1);
168+
169+ if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
170+ {
171+ struct cgraph_edge *e;
172+ if (!node->alias && c == SYMBOL_PARTITION)
173+ {
174+ /* FIXME: Find out why this is being returned NULL in some cases. */
175+ if (ipa_size_summaries->get (cnode))
176+ part->insns += ipa_size_summaries->get (cnode)->size;
177+ }
178+
179+ /* Add all inline clones and callees that are duplicated. */
180+ for (e = cnode->callees; e; e = e->next_callee)
181+ if (!e->inline_failed)
182+ add_symbol_to_partition_1 (part, e->callee);
183+ else if (e->callee->get_partitioning_class () == SYMBOL_DUPLICATE)
184+ add_symbol_to_partition (part, e->callee);
185+
186+ /* Add all thunks associated with the function. */
187+ for (e = cnode->callers; e; e = e->next_caller)
188+ if (e->caller->thunk.thunk_p && !e->caller->inlined_to)
189+ add_symbol_to_partition_1 (part, e->caller);
190+ }
191+
192+ add_references_to_partition (part, node);
193+
194+ /* Add all aliases associated with the symbol. */
195+
196+ FOR_EACH_ALIAS (node, ref)
197+ if (!ref->referring->transparent_alias)
198+ add_symbol_to_partition_1 (part, ref->referring);
199+ else
200+ {
201+ struct ipa_ref *ref2;
202+ /* We do not need to add transparent aliases if they are not used.
203+ However we must add aliases of transparent aliases if they exist. */
204+ FOR_EACH_ALIAS (ref->referring, ref2)
205+ {
206+ /* Nested transparent aliases are not permitted. */
207+ gcc_checking_assert (!ref2->referring->transparent_alias);
208+ add_symbol_to_partition_1 (part, ref2->referring);
209+ }
210+ }
211+
212+ /* Ensure that SAME_COMDAT_GROUP lists all allways added in a group. */
213+ if (node->same_comdat_group)
214+ for (node1 = node->same_comdat_group;
215+ node1 != node; node1 = node1->same_comdat_group)
216+ if (!node->alias)
217+ {
218+ bool added = add_symbol_to_partition_1 (part, node1);
219+ gcc_assert (added);
220+ }
221+ return true;
222+}
223+
224+/* If symbol NODE is really part of other symbol's definition (i.e. it is
225+ internal label, thunk, alias or so), return the outer symbol.
226+ When add_symbol_to_partition_1 is called on the outer symbol it must
227+ eventually add NODE, too. */
228+static symtab_node *
229+contained_in_symbol (symtab_node *node)
230+{
231+ /* There is no need to consider transparent aliases to be part of the
232+ definition: they are only useful insite the partition they are output
233+ and thus we will always see an explicit reference to it. */
234+ if (node->transparent_alias)
235+ return node;
236+ if (cgraph_node *cnode = dyn_cast <cgraph_node *> (node))
237+ {
238+ cnode = cnode->function_symbol ();
239+ if (cnode->inlined_to)
240+ cnode = cnode->inlined_to;
241+ return cnode;
242+ }
243+ else if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
244+ return vnode->ultimate_alias_target ();
245+ return node;
246+}
247+
248+/* Add symbol NODE to partition. When definition of NODE is part
249+ of other symbol definition, add the other symbol, too. */
250+
251+static void
252+add_symbol_to_partition (ltrans_partition part, symtab_node *node)
253+{
254+ symtab_node *node1;
255+
256+ /* Verify that we do not try to duplicate something that cannot be. */
257+ gcc_checking_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
258+ || !symbol_partitioned_p (node));
259+
260+ while ((node1 = contained_in_symbol (node)) != node)
261+ node = node1;
262+
263+ /* If we have duplicated symbol contained in something we cannot duplicate,
264+ we are very badly screwed. The other way is possible, so we do not
265+ assert this in add_symbol_to_partition_1.
266+
267+ Be lax about comdats; they may or may not be duplicated and we may
268+ end up in need to duplicate keyed comdat because it has unkeyed alias. */
269+
270+ gcc_assert (node->get_partitioning_class () == SYMBOL_DUPLICATE
271+ || DECL_COMDAT (node->decl)
272+ || !symbol_partitioned_p (node));
273+
274+ add_symbol_to_partition_1 (part, node);
275+}
276+
277+/* Undo all additions until number of cgraph nodes in PARITION is N_CGRAPH_NODES
278+ and number of varpool nodes is N_VARPOOL_NODES. */
279+
280+static void
281+undo_partition (ltrans_partition partition, unsigned int n_nodes)
282+{
283+ while (lto_symtab_encoder_size (partition->encoder) > (int)n_nodes)
284+ {
285+ symtab_node *node = lto_symtab_encoder_deref (partition->encoder,
286+ n_nodes);
287+ partition->symbols--;
288+ cgraph_node *cnode;
289+
290+ /* After UNDO we no longer know what was visited. */
291+ if (partition->initializers_visited)
292+ delete partition->initializers_visited;
293+ partition->initializers_visited = NULL;
294+
295+ if (!node->alias && (cnode = dyn_cast <cgraph_node *> (node))
296+ && node->get_partitioning_class () == SYMBOL_PARTITION)
297+ partition->insns -= ipa_size_summaries->get (cnode)->size;
298+ lto_symtab_encoder_delete_node (partition->encoder, node);
299+ node->aux = (void *)((size_t)node->aux - 1);
300+ }
301+}
302+
303+/* Group cgrah nodes by input files. This is used mainly for testing
304+ right now. */
305+
306+void
307+lto_1_to_1_map (void)
308+{
309+ symtab_node *node;
310+ struct lto_file_decl_data *file_data;
311+ hash_map<lto_file_decl_data *, ltrans_partition> pmap;
312+ ltrans_partition partition;
313+ int npartitions = 0;
314+
315+ FOR_EACH_SYMBOL (node)
316+ {
317+ if (node->get_partitioning_class () != SYMBOL_PARTITION
318+ || symbol_partitioned_p (node))
319+ continue;
320+
321+ file_data = node->lto_file_data;
322+
323+ if (file_data)
324+ {
325+ ltrans_partition *slot = &pmap.get_or_insert (file_data);
326+ if (*slot)
327+ partition = *slot;
328+ else
329+ {
330+ partition = new_partition (file_data->file_name);
331+ *slot = partition;
332+ npartitions++;
333+ }
334+ }
335+ else if (!file_data && ltrans_partitions.length ())
336+ partition = ltrans_partitions[0];
337+ else
338+ {
339+ partition = new_partition ("");
340+ pmap.put (NULL, partition);
341+ npartitions++;
342+ }
343+
344+ add_symbol_to_partition (partition, node);
345+ }
346+
347+ /* If the cgraph is empty, create one cgraph node set so that there is still
348+ an output file for any variables that need to be exported in a DSO. */
349+ if (!npartitions)
350+ new_partition ("empty");
351+
352+ /* Order partitions by order of symbols because they are linked into binary
353+ that way. */
354+ ltrans_partitions.qsort (cmp_partitions_order);
355+}
356+
357+/* Maximal partitioning. Put every new symbol into new partition if possible. */
358+
359+void
360+lto_max_map (void)
361+{
362+ symtab_node *node;
363+ ltrans_partition partition;
364+ int npartitions = 0;
365+
366+ FOR_EACH_SYMBOL (node)
367+ {
368+ if (node->get_partitioning_class () != SYMBOL_PARTITION
369+ || symbol_partitioned_p (node))
370+ continue;
371+ partition = new_partition (node->asm_name ());
372+ add_symbol_to_partition (partition, node);
373+ npartitions++;
374+ }
375+ if (!npartitions)
376+ new_partition ("empty");
377+}
378+
379+/* Helper function for qsort; sort nodes by order. */
380+static int
381+node_cmp (const void *pa, const void *pb)
382+{
383+ const symtab_node *a = *static_cast<const symtab_node * const *> (pa);
384+ const symtab_node *b = *static_cast<const symtab_node * const *> (pb);
385+ return b->order - a->order;
386+}
387+
388+/* Add all symtab nodes from NEXT_NODE to PARTITION in order. */
389+
390+static void
391+add_sorted_nodes (vec<symtab_node *> &next_nodes, ltrans_partition partition)
392+{
393+ unsigned i;
394+ symtab_node *node;
395+
396+ next_nodes.qsort (node_cmp);
397+ FOR_EACH_VEC_ELT (next_nodes, i, node)
398+ if (!symbol_partitioned_p (node))
399+ add_symbol_to_partition (partition, node);
400+}
401+
402+/* Return true if we should account reference from N1 to N2 in cost
403+ of partition boundary. */
404+
405+bool
406+account_reference_p (symtab_node *n1, symtab_node *n2)
407+{
408+ if (cgraph_node *cnode = dyn_cast <cgraph_node *> (n1))
409+ n1 = cnode;
410+ /* Do not account references from aliases - they are never split across
411+ partitions. */
412+ if (n1->alias)
413+ return false;
414+ /* Do not account recursion - the code below will handle it incorrectly
415+ otherwise. Do not account references to external symbols: they will
416+ never become local. Finally do not account references to duplicated
417+ symbols: they will be always local. */
418+ if (n1 == n2
419+ || !n2->definition
420+ || n2->get_partitioning_class () != SYMBOL_PARTITION)
421+ return false;
422+ /* If referring node is external symbol do not account it to boundary
423+ cost. Those are added into units only to enable possible constant
424+ folding and devirtulization.
425+
426+ Here we do not know if it will ever be added to some partition
427+ (this is decided by compute_ltrans_boundary) and second it is not
428+ that likely that constant folding will actually use the reference. */
429+ if (contained_in_symbol (n1)
430+ ->get_partitioning_class () == SYMBOL_EXTERNAL)
431+ return false;
432+ return true;
433+}
434+
435+
436+/* Group cgraph nodes into equally-sized partitions.
437+
438+ The partitioning algorithm is simple: nodes are taken in predefined order.
439+ The order corresponds to the order we want functions to have in the final
440+ output. In the future this will be given by function reordering pass, but
441+ at the moment we use the topological order, which is a good approximation.
442+
443+ The goal is to partition this linear order into intervals (partitions) so
444+ that all the partitions have approximately the same size and the number of
445+ callgraph or IPA reference edges crossing boundaries is minimal.
446+
447+ This is a lot faster (O(n) in size of callgraph) than algorithms doing
448+ priority-based graph clustering that are generally O(n^2) and, since
449+ WHOPR is designed to make things go well across partitions, it leads
450+ to good results.
451+
452+ We compute the expected size of a partition as:
453+
454+ max (total_size / lto_partitions, min_partition_size)
455+
456+ We use dynamic expected size of partition so small programs are partitioned
457+ into enough partitions to allow use of multiple CPUs, while large programs
458+ are not partitioned too much. Creating too many partitions significantly
459+ increases the streaming overhead.
460+
461+ In the future, we would like to bound the maximal size of partitions so as
462+ to prevent the LTRANS stage from consuming too much memory. At the moment,
463+ however, the WPA stage is the most memory intensive for large benchmarks,
464+ since too many types and declarations are read into memory.
465+
466+ The function implements a simple greedy algorithm. Nodes are being added
467+ to the current partition until after 3/4 of the expected partition size is
468+ reached. Past this threshold, we keep track of boundary size (number of
469+ edges going to other partitions) and continue adding functions until after
470+ the current partition has grown to twice the expected partition size. Then
471+ the process is undone to the point where the minimal ratio of boundary size
472+ and in-partition calls was reached. */
473+
474+void
475+lto_balanced_map (int n_lto_partitions, int max_partition_size)
476+{
477+ int n_varpool_nodes = 0, varpool_pos = 0, best_varpool_pos = 0;
478+ int best_noreorder_pos = 0;
479+ auto_vec <cgraph_node *> order (symtab->cgraph_count);
480+ auto_vec<cgraph_node *> noreorder;
481+ auto_vec<varpool_node *> varpool_order;
482+ struct cgraph_node *node;
483+ int64_t original_total_size, total_size = 0;
484+ int64_t partition_size;
485+ ltrans_partition partition;
486+ int last_visited_node = 0;
487+ varpool_node *vnode;
488+ int64_t cost = 0, internal = 0;
489+ unsigned int best_n_nodes = 0, best_i = 0;
490+ int64_t best_cost = -1, best_internal = 0, best_size = 0;
491+ int npartitions;
492+ int current_order = -1;
493+ int noreorder_pos = 0;
494+
495+ FOR_EACH_VARIABLE (vnode)
496+ gcc_assert (!vnode->aux);
497+
498+ FOR_EACH_DEFINED_FUNCTION (node)
499+ if (node->get_partitioning_class () == SYMBOL_PARTITION)
500+ {
501+ if (node->no_reorder)
502+ noreorder.safe_push (node);
503+ else
504+ order.safe_push (node);
505+ if (!node->alias)
506+ {
507+ compute_fn_summary (node, true);
508+ total_size += ipa_size_summaries->get (node)->size;
509+ }
510+ }
511+
512+ original_total_size = total_size;
513+
514+ /* Streaming works best when the source units do not cross partition
515+ boundaries much. This is because importing function from a source
516+ unit tends to import a lot of global trees defined there. We should
517+ get better about minimizing the function bounday, but until that
518+ things works smoother if we order in source order. */
519+ order.qsort (tp_first_run_node_cmp);
520+ noreorder.qsort (node_cmp);
521+
522+ if (dump_file)
523+ {
524+ for (unsigned i = 0; i < order.length (); i++)
525+ fprintf (dump_file, "Balanced map symbol order:%s:%u\n",
526+ order[i]->dump_name (), order[i]->tp_first_run);
527+ for (unsigned i = 0; i < noreorder.length (); i++)
528+ fprintf (dump_file, "Balanced map symbol no_reorder:%s:%u\n",
529+ noreorder[i]->dump_name (), noreorder[i]->tp_first_run);
530+ }
531+
532+ /* Collect all variables that should not be reordered. */
533+ FOR_EACH_VARIABLE (vnode)
534+ if (vnode->get_partitioning_class () == SYMBOL_PARTITION
535+ && vnode->no_reorder)
536+ varpool_order.safe_push (vnode);
537+ n_varpool_nodes = varpool_order.length ();
538+ varpool_order.qsort (node_cmp);
539+
540+ /* Compute partition size and create the first partition. */
541+ if (param_min_partition_size > max_partition_size)
542+ fatal_error (input_location, "min partition size cannot be greater "
543+ "than max partition size");
544+
545+ partition_size = total_size / n_lto_partitions;
546+ if (partition_size < param_min_partition_size)
547+ partition_size = param_min_partition_size;
548+ npartitions = 1;
549+ partition = new_partition ("");
550+ if (dump_file)
551+ fprintf (dump_file, "Total unit size: %" PRId64 ", partition size: %" PRId64 "\n",
552+ total_size, partition_size);
553+
554+ auto_vec<symtab_node *> next_nodes;
555+
556+ for (unsigned i = 0; i < order.length (); i++)
557+ {
558+ if (symbol_partitioned_p (order[i]))
559+ continue;
560+
561+ current_order = order[i]->order;
562+
563+ /* Output noreorder and varpool in program order first. */
564+ next_nodes.truncate (0);
565+ while (varpool_pos < n_varpool_nodes
566+ && varpool_order[varpool_pos]->order < current_order)
567+ next_nodes.safe_push (varpool_order[varpool_pos++]);
568+ while (noreorder_pos < (int)noreorder.length ()
569+ && noreorder[noreorder_pos]->order < current_order)
570+ next_nodes.safe_push (noreorder[noreorder_pos++]);
571+ add_sorted_nodes (next_nodes, partition);
572+
573+ if (!symbol_partitioned_p (order[i]))
574+ add_symbol_to_partition (partition, order[i]);
575+
576+
577+ /* Once we added a new node to the partition, we also want to add
578+ all referenced variables unless they was already added into some
579+ earlier partition.
580+ add_symbol_to_partition adds possibly multiple nodes and
581+ variables that are needed to satisfy needs of ORDER[i].
582+ We remember last visited cgraph and varpool node from last iteration
583+ of outer loop that allows us to process every new addition.
584+
585+ At the same time we compute size of the boundary into COST. Every
586+ callgraph or IPA reference edge leaving the partition contributes into
587+ COST. Every edge inside partition was earlier computed as one leaving
588+ it and thus we need to subtract it from COST. */
589+ while (last_visited_node < lto_symtab_encoder_size (partition->encoder))
590+ {
591+ int j;
592+ struct ipa_ref *ref = NULL;
593+ symtab_node *snode = lto_symtab_encoder_deref (partition->encoder,
594+ last_visited_node);
595+
596+ if (cgraph_node *node = dyn_cast <cgraph_node *> (snode))
597+ {
598+ struct cgraph_edge *edge;
599+
600+
601+ last_visited_node++;
602+
603+ gcc_assert (node->definition || node->weakref);
604+
605+ /* Compute boundary cost of callgraph edges. */
606+ for (edge = node->callees; edge; edge = edge->next_callee)
607+ /* Inline edges will always end up local. */
608+ if (edge->inline_failed
609+ && account_reference_p (node, edge->callee))
610+ {
611+ int edge_cost = edge->frequency ();
612+ int index;
613+
614+ if (!edge_cost)
615+ edge_cost = 1;
616+ gcc_assert (edge_cost > 0);
617+ index = lto_symtab_encoder_lookup (partition->encoder,
618+ edge->callee);
619+ if (index != LCC_NOT_FOUND
620+ && index < last_visited_node - 1)
621+ cost -= edge_cost, internal += edge_cost;
622+ else
623+ cost += edge_cost;
624+ }
625+ for (edge = node->callers; edge; edge = edge->next_caller)
626+ if (edge->inline_failed
627+ && account_reference_p (edge->caller, node))
628+ {
629+ int edge_cost = edge->frequency ();
630+ int index;
631+
632+ gcc_assert (edge->caller->definition);
633+ if (!edge_cost)
634+ edge_cost = 1;
635+ gcc_assert (edge_cost > 0);
636+ index = lto_symtab_encoder_lookup (partition->encoder,
637+ edge->caller);
638+ if (index != LCC_NOT_FOUND
639+ && index < last_visited_node - 1)
640+ cost -= edge_cost, internal += edge_cost;
641+ else
642+ cost += edge_cost;
643+ }
644+ }
645+ else
646+ last_visited_node++;
647+
648+ /* Compute boundary cost of IPA REF edges and at the same time look into
649+ variables referenced from current partition and try to add them. */
650+ for (j = 0; snode->iterate_reference (j, ref); j++)
651+ if (!account_reference_p (snode, ref->referred))
652+ ;
653+ else if (is_a <varpool_node *> (ref->referred))
654+ {
655+ int index;
656+
657+ vnode = dyn_cast <varpool_node *> (ref->referred);
658+ if (!symbol_partitioned_p (vnode)
659+ && !vnode->no_reorder
660+ && vnode->get_partitioning_class () == SYMBOL_PARTITION)
661+ add_symbol_to_partition (partition, vnode);
662+ index = lto_symtab_encoder_lookup (partition->encoder,
663+ vnode);
664+ if (index != LCC_NOT_FOUND
665+ && index < last_visited_node - 1)
666+ cost--, internal++;
667+ else
668+ cost++;
669+ }
670+ else
671+ {
672+ int index;
673+
674+ node = dyn_cast <cgraph_node *> (ref->referred);
675+ index = lto_symtab_encoder_lookup (partition->encoder,
676+ node);
677+ if (index != LCC_NOT_FOUND
678+ && index < last_visited_node - 1)
679+ cost--, internal++;
680+ else
681+ cost++;
682+ }
683+ for (j = 0; snode->iterate_referring (j, ref); j++)
684+ if (!account_reference_p (ref->referring, snode))
685+ ;
686+ else if (is_a <varpool_node *> (ref->referring))
687+ {
688+ int index;
689+
690+ vnode = dyn_cast <varpool_node *> (ref->referring);
691+ gcc_assert (vnode->definition);
692+ /* It is better to couple variables with their users,
693+ because it allows them to be removed. Coupling
694+ with objects they refer to only helps to reduce
695+ number of symbols promoted to hidden. */
696+ if (!symbol_partitioned_p (vnode)
697+ && !vnode->no_reorder
698+ && !vnode->can_remove_if_no_refs_p ()
699+ && vnode->get_partitioning_class () == SYMBOL_PARTITION)
700+ add_symbol_to_partition (partition, vnode);
701+ index = lto_symtab_encoder_lookup (partition->encoder,
702+ vnode);
703+ if (index != LCC_NOT_FOUND
704+ && index < last_visited_node - 1)
705+ cost--, internal++;
706+ else
707+ cost++;
708+ }
709+ else
710+ {
711+ int index;
712+
713+ node = dyn_cast <cgraph_node *> (ref->referring);
714+ gcc_assert (node->definition);
715+ index = lto_symtab_encoder_lookup (partition->encoder,
716+ node);
717+ if (index != LCC_NOT_FOUND
718+ && index < last_visited_node - 1)
719+ cost--, internal++;
720+ else
721+ cost++;
722+ }
723+ }
724+
725+ gcc_assert (cost >= 0 && internal >= 0);
726+
727+ /* If the partition is large enough, start looking for smallest boundary cost.
728+ If partition still seems too small (less than 7/8 of target weight) accept
729+ any cost. If partition has right size, optimize for highest internal/cost.
730+ Later we stop building partition if its size is 9/8 of the target wight. */
731+ if (partition->insns < partition_size * 7 / 8
732+ || best_cost == -1
733+ || (!cost
734+ || ((sreal)best_internal * (sreal) cost
735+ < ((sreal) internal * (sreal)best_cost))))
736+ {
737+ best_cost = cost;
738+ best_internal = internal;
739+ best_size = partition->insns;
740+ best_i = i;
741+ best_n_nodes = lto_symtab_encoder_size (partition->encoder);
742+ best_varpool_pos = varpool_pos;
743+ best_noreorder_pos = noreorder_pos;
744+ }
745+ if (dump_file)
746+ fprintf (dump_file, "Step %i: added %s, size %i, "
747+ "cost %" PRId64 "/%" PRId64 " "
748+ "best %" PRId64 "/%" PRId64", step %i\n", i,
749+ order[i]->dump_name (),
750+ partition->insns, cost, internal,
751+ best_cost, best_internal, best_i);
752+ /* Partition is too large, unwind into step when best cost was reached and
753+ start new partition. */
754+ if (partition->insns > 9 * partition_size / 8
755+ || partition->insns > max_partition_size)
756+ {
757+ if (best_i != i)
758+ {
759+ if (dump_file)
760+ fprintf (dump_file, "Unwinding %i insertions to step %i\n",
761+ i - best_i, best_i);
762+ undo_partition (partition, best_n_nodes);
763+ varpool_pos = best_varpool_pos;
764+ noreorder_pos = best_noreorder_pos;
765+ }
766+ gcc_assert (best_size == partition->insns);
767+ i = best_i;
768+ if (dump_file)
769+ fprintf (dump_file,
770+ "Partition insns: %i (want %" PRId64 ")\n",
771+ partition->insns, partition_size);
772+ /* When we are finished, avoid creating empty partition. */
773+ while (i < order.length () - 1 && symbol_partitioned_p (order[i + 1]))
774+ i++;
775+ if (i == order.length () - 1)
776+ break;
777+ total_size -= partition->insns;
778+ partition = new_partition ("");
779+ last_visited_node = 0;
780+ cost = 0;
781+
782+ if (dump_file)
783+ fprintf (dump_file, "New partition\n");
784+ best_n_nodes = 0;
785+ best_cost = -1;
786+
787+ /* Since the size of partitions is just approximate, update the size after
788+ we finished current one. */
789+ if (npartitions < n_lto_partitions)
790+ partition_size = total_size / (n_lto_partitions - npartitions);
791+ else
792+ /* Watch for overflow. */
793+ partition_size = INT_MAX / 16;
794+
795+ if (dump_file)
796+ fprintf (dump_file,
797+ "Total size: %" PRId64 " partition_size: %" PRId64 "\n",
798+ total_size, partition_size);
799+ if (partition_size < param_min_partition_size)
800+ partition_size = param_min_partition_size;
801+ npartitions ++;
802+ }
803+ }
804+
805+ next_nodes.truncate (0);
806+
807+ /* Varables that are not reachable from the code go into last partition. */
808+ FOR_EACH_VARIABLE (vnode)
809+ if (vnode->get_partitioning_class () == SYMBOL_PARTITION
810+ && !symbol_partitioned_p (vnode))
811+ next_nodes.safe_push (vnode);
812+
813+ /* Output remaining ordered symbols. */
814+ while (varpool_pos < n_varpool_nodes)
815+ next_nodes.safe_push (varpool_order[varpool_pos++]);
816+ while (noreorder_pos < (int)noreorder.length ())
817+ next_nodes.safe_push (noreorder[noreorder_pos++]);
818+ /* For one partition the cost of boundary should be 0 unless we added final
819+ symbols here (these are not accounted) or we have accounting bug. */
820+ gcc_assert (next_nodes.length () || npartitions != 1 || !best_cost || best_cost == -1);
821+ add_sorted_nodes (next_nodes, partition);
822+
823+ if (dump_file)
824+ {
825+ fprintf (dump_file, "\nPartition sizes:\n");
826+ unsigned partitions = ltrans_partitions.length ();
827+
828+ for (unsigned i = 0; i < partitions ; i++)
829+ {
830+ ltrans_partition p = ltrans_partitions[i];
831+ fprintf (dump_file, "partition %d contains %d (%2.2f%%)"
832+ " symbols and %d (%2.2f%%) insns\n", i, p->symbols,
833+ 100.0 * p->symbols / order.length (), p->insns,
834+ 100.0 * p->insns / original_total_size);
835+ }
836+
837+ fprintf (dump_file, "\n");
838+ }
839+}
840+
841+/* Return true if we must not change the name of the NODE. The name as
842+ extracted from the corresponding decl should be passed in NAME. */
843+
844+static bool
845+must_not_rename (symtab_node *node, const char *name)
846+{
847+ /* Our renaming machinery do not handle more than one change of assembler name.
848+ We should not need more than one anyway. */
849+ if (node->lto_file_data
850+ && lto_get_decl_name_mapping (node->lto_file_data, name) != name)
851+ {
852+ if (dump_file)
853+ fprintf (dump_file,
854+ "Not privatizing symbol name: %s. It privatized already.\n",
855+ name);
856+ return true;
857+ }
858+ /* Avoid mangling of already mangled clones.
859+ ??? should have a flag whether a symbol has a 'private' name already,
860+ since we produce some symbols like that i.e. for global constructors
861+ that are not really clones. */
862+ if (node->unique_name)
863+ {
864+ if (dump_file)
865+ fprintf (dump_file,
866+ "Not privatizing symbol name: %s. Has unique name.\n",
867+ name);
868+ return true;
869+ }
870+ return false;
871+}
872+
873+/* If we are an offload compiler, we may have to rewrite symbols to be
874+ valid on this target. Return either PTR or a modified version of it. */
875+
876+static const char *
877+maybe_rewrite_identifier (const char *ptr)
878+{
879+#if defined ACCEL_COMPILER && (defined NO_DOT_IN_LABEL || defined NO_DOLLAR_IN_LABEL)
880+#ifndef NO_DOT_IN_LABEL
881+ char valid = '.';
882+ const char reject[] = "$";
883+#elif !defined NO_DOLLAR_IN_LABEL
884+ char valid = '$';
885+ const char reject[] = ".";
886+#else
887+ char valid = '_';
888+ const char reject[] = ".$";
889+#endif
890+
891+ char *copy = NULL;
892+ const char *match = ptr;
893+ for (;;)
894+ {
895+ size_t off = strcspn (match, reject);
896+ if (match[off] == '\0')
897+ break;
898+ if (copy == NULL)
899+ {
900+ copy = xstrdup (ptr);
901+ match = copy;
902+ }
903+ copy[off] = valid;
904+ }
905+ return match;
906+#else
907+ return ptr;
908+#endif
909+}
910+
911+/* Ensure that the symbol in NODE is valid for the target, and if not,
912+ rewrite it. */
913+
914+static void
915+validize_symbol_for_target (symtab_node *node)
916+{
917+ tree decl = node->decl;
918+ const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
919+
920+ if (must_not_rename (node, name))
921+ return;
922+
923+ const char *name2 = maybe_rewrite_identifier (name);
924+ if (name2 != name)
925+ {
926+ symtab->change_decl_assembler_name (decl, get_identifier (name2));
927+ if (node->lto_file_data)
928+ lto_record_renamed_decl (node->lto_file_data, name,
929+ IDENTIFIER_POINTER
930+ (DECL_ASSEMBLER_NAME (decl)));
931+ }
932+}
933+
934+/* Maps symbol names to unique lto clone counters. */
935+static hash_map<const char *, unsigned> *lto_clone_numbers;
936+
937+/* Helper for privatize_symbol_name. Mangle NODE symbol name
938+ represented by DECL. */
939+
940+static bool
941+privatize_symbol_name_1 (symtab_node *node, tree decl)
942+{
943+ const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
944+
945+ if (must_not_rename (node, name))
946+ return false;
947+
948+ name = maybe_rewrite_identifier (name);
949+ unsigned &clone_number = lto_clone_numbers->get_or_insert (name);
950+ symtab->change_decl_assembler_name (decl,
951+ clone_function_name (
952+ name, "lto_priv", clone_number));
953+ clone_number++;
954+
955+ if (node->lto_file_data)
956+ lto_record_renamed_decl (node->lto_file_data, name,
957+ IDENTIFIER_POINTER
958+ (DECL_ASSEMBLER_NAME (decl)));
959+
960+ if (dump_file)
961+ fprintf (dump_file,
962+ "Privatizing symbol name: %s -> %s\n",
963+ name, IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)));
964+
965+ return true;
966+}
967+
968+/* Mangle NODE symbol name into a local name.
969+ This is necessary to do
970+ 1) if two or more static vars of same assembler name
971+ are merged into single ltrans unit.
972+ 2) if previously static var was promoted hidden to avoid possible conflict
973+ with symbols defined out of the LTO world. */
974+
975+static bool
976+privatize_symbol_name (symtab_node *node)
977+{
978+ if (!privatize_symbol_name_1 (node, node->decl))
979+ return false;
980+
981+ return true;
982+}
983+
984+/* Promote variable VNODE to be static. */
985+
986+static void
987+promote_symbol (symtab_node *node)
988+{
989+ /* We already promoted ... */
990+ if (DECL_VISIBILITY (node->decl) == VISIBILITY_HIDDEN
991+ && DECL_VISIBILITY_SPECIFIED (node->decl)
992+ && TREE_PUBLIC (node->decl))
993+ {
994+ validize_symbol_for_target (node);
995+ return;
996+ }
997+
998+ gcc_checking_assert (!TREE_PUBLIC (node->decl)
999+ && !DECL_EXTERNAL (node->decl));
1000+ /* Be sure that newly public symbol does not conflict with anything already
1001+ defined by the non-LTO part. */
1002+ privatize_symbol_name (node);
1003+ TREE_PUBLIC (node->decl) = 1;
1004+ DECL_VISIBILITY (node->decl) = VISIBILITY_HIDDEN;
1005+ DECL_VISIBILITY_SPECIFIED (node->decl) = true;
1006+ if (dump_file)
1007+ fprintf (dump_file,
1008+ "Promoting as hidden: %s (%s)\n", node->dump_name (),
1009+ IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1010+
1011+ /* Promoting a symbol also promotes all transparent aliases with exception
1012+ of weakref where the visibility flags are always wrong and set to
1013+ !PUBLIC. */
1014+ ipa_ref *ref;
1015+ for (unsigned i = 0; node->iterate_direct_aliases (i, ref); i++)
1016+ {
1017+ struct symtab_node *alias = ref->referring;
1018+ if (alias->transparent_alias && !alias->weakref)
1019+ {
1020+ TREE_PUBLIC (alias->decl) = 1;
1021+ DECL_VISIBILITY (alias->decl) = VISIBILITY_HIDDEN;
1022+ DECL_VISIBILITY_SPECIFIED (alias->decl) = true;
1023+ if (dump_file)
1024+ fprintf (dump_file,
1025+ "Promoting alias as hidden: %s\n",
1026+ IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
1027+ }
1028+ gcc_assert (!alias->weakref || TREE_PUBLIC (alias->decl));
1029+ }
1030+}
1031+
1032+/* Return true if NODE needs named section even if it won't land in
1033+ the partition symbol table.
1034+
1035+ FIXME: we should really not use named sections for inline clones
1036+ and master clones. */
1037+
1038+static bool
1039+may_need_named_section_p (lto_symtab_encoder_t encoder, symtab_node *node)
1040+{
1041+ struct cgraph_node *cnode = dyn_cast <cgraph_node *> (node);
1042+ if (!cnode)
1043+ return false;
1044+ if (node->real_symbol_p ())
1045+ return false;
1046+ return (!encoder
1047+ || (lto_symtab_encoder_lookup (encoder, node) != LCC_NOT_FOUND
1048+ && lto_symtab_encoder_encode_body_p (encoder,
1049+ cnode)));
1050+}
1051+
1052+/* If NODE represents a static variable. See if there are other variables
1053+ of the same name in partition ENCODER (or in whole compilation unit if
1054+ ENCODER is NULL) and if so, mangle the statics. Always mangle all
1055+ conflicting statics, so we reduce changes of silently miscompiling
1056+ asm statements referring to them by symbol name. */
1057+
1058+static void
1059+rename_statics (lto_symtab_encoder_t encoder, symtab_node *node)
1060+{
1061+ tree decl = node->decl;
1062+ symtab_node *s;
1063+ tree name = DECL_ASSEMBLER_NAME (decl);
1064+
1065+ /* See if this is static symbol. */
1066+ if (((node->externally_visible && !node->weakref)
1067+ /* FIXME: externally_visible is somewhat illogically not set for
1068+ external symbols (i.e. those not defined). Remove this test
1069+ once this is fixed. */
1070+ || DECL_EXTERNAL (node->decl)
1071+ || !node->real_symbol_p ())
1072+ && !may_need_named_section_p (encoder, node))
1073+ return;
1074+
1075+ /* Now walk symbols sharing the same name and see if there are any conflicts.
1076+ (all types of symbols counts here, since we cannot have static of the
1077+ same name as external or public symbol.) */
1078+ for (s = symtab_node::get_for_asmname (name);
1079+ s; s = s->next_sharing_asm_name)
1080+ if ((s->real_symbol_p () || may_need_named_section_p (encoder, s))
1081+ && s->decl != node->decl
1082+ && (!encoder
1083+ || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1084+ break;
1085+
1086+ /* OK, no confict, so we have nothing to do. */
1087+ if (!s)
1088+ return;
1089+
1090+ if (dump_file)
1091+ fprintf (dump_file,
1092+ "Renaming statics with asm name: %s\n", node->dump_name ());
1093+
1094+ /* Assign every symbol in the set that shares the same ASM name an unique
1095+ mangled name. */
1096+ for (s = symtab_node::get_for_asmname (name); s;)
1097+ if ((!s->externally_visible || s->weakref)
1098+ /* Transparent aliases having same name as target are renamed at a
1099+ time their target gets new name. Transparent aliases that use
1100+ separate assembler name require the name to be unique. */
1101+ && (!s->transparent_alias || !s->definition || s->weakref
1102+ || !symbol_table::assembler_names_equal_p
1103+ (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (s->decl)),
1104+ IDENTIFIER_POINTER
1105+ (DECL_ASSEMBLER_NAME (s->get_alias_target()->decl))))
1106+ && ((s->real_symbol_p ()
1107+ && !DECL_EXTERNAL (s->decl)
1108+ && !TREE_PUBLIC (s->decl))
1109+ || may_need_named_section_p (encoder, s))
1110+ && (!encoder
1111+ || lto_symtab_encoder_lookup (encoder, s) != LCC_NOT_FOUND))
1112+ {
1113+ if (privatize_symbol_name (s))
1114+ /* Re-start from beginning since we do not know how many
1115+ symbols changed a name. */
1116+ s = symtab_node::get_for_asmname (name);
1117+ else s = s->next_sharing_asm_name;
1118+ }
1119+ else s = s->next_sharing_asm_name;
1120+}
1121+
1122+/* Find out all static decls that need to be promoted to global because
1123+ of cross file sharing. This function must be run in the WPA mode after
1124+ all inlinees are added. */
1125+
1126+void
1127+lto_promote_cross_file_statics (void)
1128+{
1129+ unsigned i, n_sets;
1130+
1131+ lto_stream_offload_p = false;
1132+ select_what_to_stream ();
1133+
1134+ /* First compute boundaries. */
1135+ n_sets = ltrans_partitions.length ();
1136+ for (i = 0; i < n_sets; i++)
1137+ {
1138+ ltrans_partition part
1139+ = ltrans_partitions[i];
1140+ part->encoder = compute_ltrans_boundary (part->encoder);
1141+ }
1142+
1143+ lto_clone_numbers = new hash_map<const char *, unsigned>;
1144+
1145+ /* Look at boundaries and promote symbols as needed. */
1146+ for (i = 0; i < n_sets; i++)
1147+ {
1148+ lto_symtab_encoder_iterator lsei;
1149+ lto_symtab_encoder_t encoder = ltrans_partitions[i]->encoder;
1150+
1151+ for (lsei = lsei_start (encoder); !lsei_end_p (lsei);
1152+ lsei_next (&lsei))
1153+ {
1154+ symtab_node *node = lsei_node (lsei);
1155+
1156+ /* If symbol is static, rename it if its assembler name
1157+ clashes with anything else in this unit. */
1158+ rename_statics (encoder, node);
1159+
1160+ /* No need to promote if symbol already is externally visible ... */
1161+ if (node->externally_visible
1162+ /* ... or if it is part of current partition ... */
1163+ || lto_symtab_encoder_in_partition_p (encoder, node)
1164+ /* ... or if we do not partition it. This mean that it will
1165+ appear in every partition referencing it. */
1166+ || node->get_partitioning_class () != SYMBOL_PARTITION)
1167+ {
1168+ validize_symbol_for_target (node);
1169+ continue;
1170+ }
1171+
1172+ promote_symbol (node);
1173+ }
1174+ }
1175+ delete lto_clone_numbers;
1176+}
1177+
1178+/* Rename statics in the whole unit in the case that
1179+ we do -flto-partition=none. */
1180+
1181+void
1182+lto_promote_statics_nonwpa (void)
1183+{
1184+ symtab_node *node;
1185+
1186+ lto_clone_numbers = new hash_map<const char *, unsigned>;
1187+ FOR_EACH_SYMBOL (node)
1188+ {
1189+ rename_statics (NULL, node);
1190+ validize_symbol_for_target (node);
1191+ }
1192+ delete lto_clone_numbers;
1193+}
--- /dev/null
+++ b/gcc/lto-partition.h
@@ -0,0 +1,41 @@
1+/* LTO partitioning logic routines.
2+ Copyright (C) 2009-2020 Free Software Foundation, Inc.
3+
4+This file is part of GCC.
5+
6+GCC is free software; you can redistribute it and/or modify it under
7+the terms of the GNU General Public License as published by the Free
8+Software Foundation; either version 3, or (at your option) any later
9+version.
10+
11+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12+WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+for more details.
15+
16+You should have received a copy of the GNU General Public License
17+along with GCC; see the file COPYING3. If not see
18+<http://www.gnu.org/licenses/>. */
19+
20+
21+/* Structure describing ltrans partitions. */
22+
23+struct ltrans_partition_def
24+{
25+ lto_symtab_encoder_t encoder;
26+ const char * name;
27+ int insns;
28+ int symbols;
29+ hash_set<symtab_node *> *initializers_visited;
30+};
31+
32+typedef struct ltrans_partition_def *ltrans_partition;
33+
34+extern vec<ltrans_partition> ltrans_partitions;
35+
36+void lto_1_to_1_map (void);
37+void lto_max_map (void);
38+void lto_balanced_map (int, int);
39+void lto_promote_cross_file_statics (void);
40+void free_ltrans_partitions (void);
41+void lto_promote_statics_nonwpa (void);
--- a/gcc/lto-streamer.h
+++ b/gcc/lto-streamer.h
@@ -956,6 +956,10 @@ bool reachable_from_this_partition_p (struct cgraph_node *,
956956 lto_symtab_encoder_t compute_ltrans_boundary (lto_symtab_encoder_t encoder);
957957 void select_what_to_stream (void);
958958
959+struct ltrans_partition_def;
960+void lto_apply_partition_mask (struct ltrans_partition_def *partition);
961+
962+
959963 /* In options-save.c. */
960964 void cl_target_option_stream_out (struct output_block *, struct bitpack_d *,
961965 struct cl_target_option *);