[ELF] Dump symbols ordered by profiled guided section layout to file.
Patch by Tiancong Wang.
In D36351, Call-Chain Clustering (C3) heuristic is implemented with
option --call-graph-ordering-file <file>.
This patch adds a flag --print-symbol-order=<file> to LLD, and when
specified, it prints out the symbols ordered by the heuristics to the
file. The symbols printout is helpful to those who want to understand
the heuristics and want to reproduce the ordering with
--symbol-ordering-file in later pass.
Differential Revision: https://reviews.llvm.org/D59311
llvm-svn: 357133
diff --git a/lld/ELF/CallGraphSort.cpp b/lld/ELF/CallGraphSort.cpp
index 2e50c50..8550177 100644
--- a/lld/ELF/CallGraphSort.cpp
+++ b/lld/ELF/CallGraphSort.cpp
@@ -226,6 +226,27 @@
for (int SecIndex : C.Sections)
OrderMap[Sections[SecIndex]] = CurOrder++;
+ if (!Config->PrintSymbolOrder.empty()) {
+ std::error_code EC;
+ raw_fd_ostream OS(Config->PrintSymbolOrder, EC, sys::fs::F_None);
+ if (EC) {
+ error("cannot open " + Config->PrintSymbolOrder + ": " + EC.message());
+ return OrderMap;
+ }
+
+ // Print the symbols ordered by C3, in the order of increasing CurOrder
+ // Instead of sorting all the OrderMap, just repeat the loops above.
+ for (const Cluster &C : Clusters)
+ for (int SecIndex : C.Sections)
+ // Search all the symbols in the file of the section
+ // and find out a Defined symbol with name that is within the section.
+ for (Symbol *Sym: Sections[SecIndex]->File->getSymbols())
+ if (!Sym->isSection()) // Filter out section-type symbols here.
+ if (auto *D = dyn_cast<Defined>(Sym))
+ if (Sections[SecIndex] == D->Section)
+ OS << Sym->getName() << "\n";
+ }
+
return OrderMap;
}