xref: /src/contrib/llvm-project/llvm/lib/Analysis/ImportedFunctionsInliningStatistics.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1b915e9e0SDimitry Andric //===-- ImportedFunctionsInliningStats.cpp ----------------------*- C++ -*-===//
2b915e9e0SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6b915e9e0SDimitry Andric //
7b915e9e0SDimitry Andric //===----------------------------------------------------------------------===//
8b915e9e0SDimitry Andric // Generating inliner statistics for imported functions, mostly useful for
9b915e9e0SDimitry Andric // ThinLTO.
10b915e9e0SDimitry Andric //===----------------------------------------------------------------------===//
11b915e9e0SDimitry Andric 
12b60736ecSDimitry Andric #include "llvm/Analysis/Utils/ImportedFunctionsInliningStatistics.h"
13b915e9e0SDimitry Andric #include "llvm/ADT/STLExtras.h"
14b915e9e0SDimitry Andric #include "llvm/IR/Function.h"
15b915e9e0SDimitry Andric #include "llvm/IR/Module.h"
16b60736ecSDimitry Andric #include "llvm/Support/CommandLine.h"
17b915e9e0SDimitry Andric #include "llvm/Support/Debug.h"
18b915e9e0SDimitry Andric #include "llvm/Support/raw_ostream.h"
19b915e9e0SDimitry Andric #include <algorithm>
20b915e9e0SDimitry Andric #include <iomanip>
21b915e9e0SDimitry Andric #include <sstream>
22344a3780SDimitry Andric #include <string>
23344a3780SDimitry Andric 
24b915e9e0SDimitry Andric using namespace llvm;
25b915e9e0SDimitry Andric 
26e3b55780SDimitry Andric namespace llvm {
27b60736ecSDimitry Andric cl::opt<InlinerFunctionImportStatsOpts> InlinerFunctionImportStats(
28b60736ecSDimitry Andric     "inliner-function-import-stats",
29b60736ecSDimitry Andric     cl::init(InlinerFunctionImportStatsOpts::No),
30b60736ecSDimitry Andric     cl::values(clEnumValN(InlinerFunctionImportStatsOpts::Basic, "basic",
31b60736ecSDimitry Andric                           "basic statistics"),
32b60736ecSDimitry Andric                clEnumValN(InlinerFunctionImportStatsOpts::Verbose, "verbose",
33b60736ecSDimitry Andric                           "printing of statistics for each inlined function")),
34b60736ecSDimitry Andric     cl::Hidden, cl::desc("Enable inliner stats for imported functions"));
35ac9a064cSDimitry Andric } // namespace llvm
36b60736ecSDimitry Andric 
37b915e9e0SDimitry Andric ImportedFunctionsInliningStatistics::InlineGraphNode &
createInlineGraphNode(const Function & F)38b915e9e0SDimitry Andric ImportedFunctionsInliningStatistics::createInlineGraphNode(const Function &F) {
39b915e9e0SDimitry Andric 
40b915e9e0SDimitry Andric   auto &ValueLookup = NodesMap[F.getName()];
41b915e9e0SDimitry Andric   if (!ValueLookup) {
421d5ae102SDimitry Andric     ValueLookup = std::make_unique<InlineGraphNode>();
431d5ae102SDimitry Andric     ValueLookup->Imported = F.hasMetadata("thinlto_src_module");
44b915e9e0SDimitry Andric   }
45b915e9e0SDimitry Andric   return *ValueLookup;
46b915e9e0SDimitry Andric }
47b915e9e0SDimitry Andric 
recordInline(const Function & Caller,const Function & Callee)48b915e9e0SDimitry Andric void ImportedFunctionsInliningStatistics::recordInline(const Function &Caller,
49b915e9e0SDimitry Andric                                                        const Function &Callee) {
50b915e9e0SDimitry Andric 
51b915e9e0SDimitry Andric   InlineGraphNode &CallerNode = createInlineGraphNode(Caller);
52b915e9e0SDimitry Andric   InlineGraphNode &CalleeNode = createInlineGraphNode(Callee);
53b915e9e0SDimitry Andric   CalleeNode.NumberOfInlines++;
54b915e9e0SDimitry Andric 
55b915e9e0SDimitry Andric   if (!CallerNode.Imported && !CalleeNode.Imported) {
56b915e9e0SDimitry Andric     // Direct inline from not imported callee to not imported caller, so we
57b915e9e0SDimitry Andric     // don't have to add this to graph. It might be very helpful if you wanna
58b915e9e0SDimitry Andric     // get the inliner statistics in compile step where there are no imported
59b915e9e0SDimitry Andric     // functions. In this case the graph would be empty.
60b915e9e0SDimitry Andric     CalleeNode.NumberOfRealInlines++;
61b915e9e0SDimitry Andric     return;
62b915e9e0SDimitry Andric   }
63b915e9e0SDimitry Andric 
64b915e9e0SDimitry Andric   CallerNode.InlinedCallees.push_back(&CalleeNode);
65b915e9e0SDimitry Andric   if (!CallerNode.Imported) {
66b915e9e0SDimitry Andric     // We could avoid second lookup, but it would make the code ultra ugly.
67b915e9e0SDimitry Andric     auto It = NodesMap.find(Caller.getName());
68b915e9e0SDimitry Andric     assert(It != NodesMap.end() && "The node should be already there.");
69b915e9e0SDimitry Andric     // Save Caller as a starting node for traversal. The string has to be one
70b915e9e0SDimitry Andric     // from map because Caller can disappear (and function name with it).
71b915e9e0SDimitry Andric     NonImportedCallers.push_back(It->first());
72b915e9e0SDimitry Andric   }
73b915e9e0SDimitry Andric }
74b915e9e0SDimitry Andric 
setModuleInfo(const Module & M)75b915e9e0SDimitry Andric void ImportedFunctionsInliningStatistics::setModuleInfo(const Module &M) {
76b915e9e0SDimitry Andric   ModuleName = M.getName();
77b915e9e0SDimitry Andric   for (const auto &F : M.functions()) {
7871d5a254SDimitry Andric     if (F.isDeclaration())
7971d5a254SDimitry Andric       continue;
80b915e9e0SDimitry Andric     AllFunctions++;
811d5ae102SDimitry Andric     ImportedFunctions += int(F.hasMetadata("thinlto_src_module"));
82b915e9e0SDimitry Andric   }
83b915e9e0SDimitry Andric }
getStatString(const char * Msg,int32_t Fraction,int32_t All,const char * PercentageOfMsg,bool LineEnd=true)84b915e9e0SDimitry Andric static std::string getStatString(const char *Msg, int32_t Fraction, int32_t All,
85b915e9e0SDimitry Andric                                  const char *PercentageOfMsg,
86b915e9e0SDimitry Andric                                  bool LineEnd = true) {
87b915e9e0SDimitry Andric   double Result = 0;
88b915e9e0SDimitry Andric   if (All != 0)
89b915e9e0SDimitry Andric     Result = 100 * static_cast<double>(Fraction) / All;
90b915e9e0SDimitry Andric 
91b915e9e0SDimitry Andric   std::stringstream Str;
92b915e9e0SDimitry Andric   Str << std::setprecision(4) << Msg << ": " << Fraction << " [" << Result
93b915e9e0SDimitry Andric       << "% of " << PercentageOfMsg << "]";
94b915e9e0SDimitry Andric   if (LineEnd)
95b915e9e0SDimitry Andric     Str << "\n";
96b915e9e0SDimitry Andric   return Str.str();
97b915e9e0SDimitry Andric }
98b915e9e0SDimitry Andric 
dump(const bool Verbose)99b915e9e0SDimitry Andric void ImportedFunctionsInliningStatistics::dump(const bool Verbose) {
100b915e9e0SDimitry Andric   calculateRealInlines();
101b915e9e0SDimitry Andric   NonImportedCallers.clear();
102b915e9e0SDimitry Andric 
103b915e9e0SDimitry Andric   int32_t InlinedImportedFunctionsCount = 0;
104b915e9e0SDimitry Andric   int32_t InlinedNotImportedFunctionsCount = 0;
105b915e9e0SDimitry Andric 
106b915e9e0SDimitry Andric   int32_t InlinedImportedFunctionsToImportingModuleCount = 0;
107b915e9e0SDimitry Andric   int32_t InlinedNotImportedFunctionsToImportingModuleCount = 0;
108b915e9e0SDimitry Andric 
109b915e9e0SDimitry Andric   const auto SortedNodes = getSortedNodes();
110b915e9e0SDimitry Andric   std::string Out;
111b915e9e0SDimitry Andric   Out.reserve(5000);
112b915e9e0SDimitry Andric   raw_string_ostream Ostream(Out);
113b915e9e0SDimitry Andric 
114b915e9e0SDimitry Andric   Ostream << "------- Dumping inliner stats for [" << ModuleName
115b915e9e0SDimitry Andric           << "] -------\n";
116b915e9e0SDimitry Andric 
117b915e9e0SDimitry Andric   if (Verbose)
118b915e9e0SDimitry Andric     Ostream << "-- List of inlined functions:\n";
119b915e9e0SDimitry Andric 
120b915e9e0SDimitry Andric   for (const auto &Node : SortedNodes) {
121b915e9e0SDimitry Andric     assert(Node->second->NumberOfInlines >= Node->second->NumberOfRealInlines);
122b915e9e0SDimitry Andric     if (Node->second->NumberOfInlines == 0)
123b915e9e0SDimitry Andric       continue;
124b915e9e0SDimitry Andric 
125b915e9e0SDimitry Andric     if (Node->second->Imported) {
126b915e9e0SDimitry Andric       InlinedImportedFunctionsCount++;
127b915e9e0SDimitry Andric       InlinedImportedFunctionsToImportingModuleCount +=
128b915e9e0SDimitry Andric           int(Node->second->NumberOfRealInlines > 0);
129b915e9e0SDimitry Andric     } else {
130b915e9e0SDimitry Andric       InlinedNotImportedFunctionsCount++;
131b915e9e0SDimitry Andric       InlinedNotImportedFunctionsToImportingModuleCount +=
132b915e9e0SDimitry Andric           int(Node->second->NumberOfRealInlines > 0);
133b915e9e0SDimitry Andric     }
134b915e9e0SDimitry Andric 
135b915e9e0SDimitry Andric     if (Verbose)
136b915e9e0SDimitry Andric       Ostream << "Inlined "
137b915e9e0SDimitry Andric               << (Node->second->Imported ? "imported " : "not imported ")
138b915e9e0SDimitry Andric               << "function [" << Node->first() << "]"
139b915e9e0SDimitry Andric               << ": #inlines = " << Node->second->NumberOfInlines
140b915e9e0SDimitry Andric               << ", #inlines_to_importing_module = "
141b915e9e0SDimitry Andric               << Node->second->NumberOfRealInlines << "\n";
142b915e9e0SDimitry Andric   }
143b915e9e0SDimitry Andric 
144b915e9e0SDimitry Andric   auto InlinedFunctionsCount =
145b915e9e0SDimitry Andric       InlinedImportedFunctionsCount + InlinedNotImportedFunctionsCount;
146b915e9e0SDimitry Andric   auto NotImportedFuncCount = AllFunctions - ImportedFunctions;
147b915e9e0SDimitry Andric   auto ImportedNotInlinedIntoModule =
148b915e9e0SDimitry Andric       ImportedFunctions - InlinedImportedFunctionsToImportingModuleCount;
149b915e9e0SDimitry Andric 
150b915e9e0SDimitry Andric   Ostream << "-- Summary:\n"
151b915e9e0SDimitry Andric           << "All functions: " << AllFunctions
152b915e9e0SDimitry Andric           << ", imported functions: " << ImportedFunctions << "\n"
153b915e9e0SDimitry Andric           << getStatString("inlined functions", InlinedFunctionsCount,
154b915e9e0SDimitry Andric                            AllFunctions, "all functions")
155b915e9e0SDimitry Andric           << getStatString("imported functions inlined anywhere",
156b915e9e0SDimitry Andric                            InlinedImportedFunctionsCount, ImportedFunctions,
157b915e9e0SDimitry Andric                            "imported functions")
158b915e9e0SDimitry Andric           << getStatString("imported functions inlined into importing module",
159b915e9e0SDimitry Andric                            InlinedImportedFunctionsToImportingModuleCount,
160b915e9e0SDimitry Andric                            ImportedFunctions, "imported functions",
161b915e9e0SDimitry Andric                            /*LineEnd=*/false)
162b915e9e0SDimitry Andric           << getStatString(", remaining", ImportedNotInlinedIntoModule,
163b915e9e0SDimitry Andric                            ImportedFunctions, "imported functions")
164b915e9e0SDimitry Andric           << getStatString("non-imported functions inlined anywhere",
165b915e9e0SDimitry Andric                            InlinedNotImportedFunctionsCount,
166b915e9e0SDimitry Andric                            NotImportedFuncCount, "non-imported functions")
167b915e9e0SDimitry Andric           << getStatString(
168b915e9e0SDimitry Andric                  "non-imported functions inlined into importing module",
169b915e9e0SDimitry Andric                  InlinedNotImportedFunctionsToImportingModuleCount,
170b915e9e0SDimitry Andric                  NotImportedFuncCount, "non-imported functions");
171b915e9e0SDimitry Andric   Ostream.flush();
172b915e9e0SDimitry Andric   dbgs() << Out;
173b915e9e0SDimitry Andric }
174b915e9e0SDimitry Andric 
calculateRealInlines()175b915e9e0SDimitry Andric void ImportedFunctionsInliningStatistics::calculateRealInlines() {
176b915e9e0SDimitry Andric   // Removing duplicated Callers.
177d8e91e46SDimitry Andric   llvm::sort(NonImportedCallers);
178ac9a064cSDimitry Andric   NonImportedCallers.erase(llvm::unique(NonImportedCallers),
179b915e9e0SDimitry Andric                            NonImportedCallers.end());
180b915e9e0SDimitry Andric 
181b915e9e0SDimitry Andric   for (const auto &Name : NonImportedCallers) {
182b915e9e0SDimitry Andric     auto &Node = *NodesMap[Name];
183b915e9e0SDimitry Andric     if (!Node.Visited)
184b915e9e0SDimitry Andric       dfs(Node);
185b915e9e0SDimitry Andric   }
186b915e9e0SDimitry Andric }
187b915e9e0SDimitry Andric 
dfs(InlineGraphNode & GraphNode)188b915e9e0SDimitry Andric void ImportedFunctionsInliningStatistics::dfs(InlineGraphNode &GraphNode) {
189b915e9e0SDimitry Andric   assert(!GraphNode.Visited);
190b915e9e0SDimitry Andric   GraphNode.Visited = true;
191b915e9e0SDimitry Andric   for (auto *const InlinedFunctionNode : GraphNode.InlinedCallees) {
192b915e9e0SDimitry Andric     InlinedFunctionNode->NumberOfRealInlines++;
193b915e9e0SDimitry Andric     if (!InlinedFunctionNode->Visited)
194b915e9e0SDimitry Andric       dfs(*InlinedFunctionNode);
195b915e9e0SDimitry Andric   }
196b915e9e0SDimitry Andric }
197b915e9e0SDimitry Andric 
198b915e9e0SDimitry Andric ImportedFunctionsInliningStatistics::SortedNodesTy
getSortedNodes()199b915e9e0SDimitry Andric ImportedFunctionsInliningStatistics::getSortedNodes() {
200b915e9e0SDimitry Andric   SortedNodesTy SortedNodes;
201b915e9e0SDimitry Andric   SortedNodes.reserve(NodesMap.size());
202b915e9e0SDimitry Andric   for (const NodesMapTy::value_type &Node : NodesMap)
203b915e9e0SDimitry Andric     SortedNodes.push_back(&Node);
204b915e9e0SDimitry Andric 
205d8e91e46SDimitry Andric   llvm::sort(SortedNodes, [&](const SortedNodesTy::value_type &Lhs,
206b915e9e0SDimitry Andric                               const SortedNodesTy::value_type &Rhs) {
207b915e9e0SDimitry Andric     if (Lhs->second->NumberOfInlines != Rhs->second->NumberOfInlines)
208b915e9e0SDimitry Andric       return Lhs->second->NumberOfInlines > Rhs->second->NumberOfInlines;
209d8e91e46SDimitry Andric     if (Lhs->second->NumberOfRealInlines != Rhs->second->NumberOfRealInlines)
210b915e9e0SDimitry Andric       return Lhs->second->NumberOfRealInlines >
211b915e9e0SDimitry Andric              Rhs->second->NumberOfRealInlines;
212b915e9e0SDimitry Andric     return Lhs->first() < Rhs->first();
213b915e9e0SDimitry Andric   });
214b915e9e0SDimitry Andric   return SortedNodes;
215b915e9e0SDimitry Andric }
216