blob: 7f27e96602ce5232560d9b98ce16bf15b5159461 [file] [log] [blame]
Piotr Padlewski84abc742016-07-29 00:27:16 +00001//===-- ImportedFunctionsInliningStats.cpp ----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9// Generating inliner statistics for imported functions, mostly useful for
10// ThinLTO.
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Transforms/Utils/ImportedFunctionsInliningStatistics.h"
14#include "llvm/ADT/STLExtras.h"
15#include "llvm/IR/Function.h"
16#include "llvm/IR/Module.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/raw_ostream.h"
19#include <algorithm>
20#include <iomanip>
21#include <sstream>
22using namespace llvm;
23
24ImportedFunctionsInliningStatistics::InlineGraphNode &
25ImportedFunctionsInliningStatistics::createInlineGraphNode(const Function &F) {
26
27 auto &ValueLookup = NodesMap[F.getName()];
28 if (!ValueLookup) {
29 ValueLookup = llvm::make_unique<InlineGraphNode>();
30 ValueLookup->Imported = F.getMetadata("thinlto_src_module") != nullptr;
31 }
32 return *ValueLookup;
33}
34
35void ImportedFunctionsInliningStatistics::recordInline(const Function &Caller,
36 const Function &Callee) {
37
38 InlineGraphNode &CallerNode = createInlineGraphNode(Caller);
39 InlineGraphNode &CalleeNode = createInlineGraphNode(Callee);
40 CalleeNode.NumberOfInlines++;
41
42 if (!CallerNode.Imported && !CalleeNode.Imported) {
43 // Direct inline from not imported callee to not imported caller, so we
44 // don't have to add this to graph. It might be very helpful if you wanna
45 // get the inliner statistics in compile step where there are no imported
46 // functions. In this case the graph would be empty.
47 CalleeNode.NumberOfRealInlines++;
48 return;
49 }
50
51 CallerNode.InlinedCallees.push_back(&CalleeNode);
52 if (!CallerNode.Imported)
53 // Save Caller as a starting node for traversal.
54 NonImportedCallers.push_back(Caller.getName());
55}
56
57void ImportedFunctionsInliningStatistics::setModuleInfo(const Module &M) {
58 ModuleName = M.getName();
59 for (const auto &F : M.functions()) {
60 AllFunctions++;
61 ImportedFunctions += int(F.getMetadata("thinlto_src_module") != nullptr);
62 }
63}
64static std::string getStatString(const char *Msg, int32_t Fraction, int32_t All,
65 const char *PercentageOfMsg,
66 bool LineEnd = true) {
67 double Result = 0;
68 if (All != 0)
69 Result = 100 * static_cast<double>(Fraction) / All;
70
71 std::stringstream Str;
72 Str << std::setprecision(4) << Msg << ": " << Fraction << " [" << Result
73 << "% of " << PercentageOfMsg << "]";
74 if (LineEnd)
75 Str << "\n";
76 return Str.str();
77}
78
79void ImportedFunctionsInliningStatistics::dump(const bool Verbose) {
80 calculateRealInlines();
81 NonImportedCallers.clear();
82
83 int32_t InlinedImportedFunctionsCount = 0;
84 int32_t InlinedNotImportedFunctionsCount = 0;
85
86 int32_t InlinedImportedFunctionsToImportingModuleCount = 0;
87 int32_t InlinedNotImportedFunctionsToImportingModuleCount = 0;
88
89 const auto SortedNodes = getSortedNodes();
90 std::string Out;
91 Out.reserve(5000);
92 raw_string_ostream Ostream(Out);
93
94 Ostream << "------- Dumping inliner stats for [" << ModuleName
95 << "] -------\n";
96
97 if (Verbose)
98 Ostream << "-- List of inlined functions:\n";
99
100 for (const auto &Node : SortedNodes) {
101 assert(Node.second->NumberOfInlines >= Node.second->NumberOfRealInlines);
102 if (Node.second->NumberOfInlines == 0)
103 continue;
104
105 if (Node.second->Imported) {
106 InlinedImportedFunctionsCount++;
107 InlinedImportedFunctionsToImportingModuleCount +=
108 int(Node.second->NumberOfRealInlines > 0);
109 } else {
110 InlinedNotImportedFunctionsCount++;
111 InlinedNotImportedFunctionsToImportingModuleCount +=
112 int(Node.second->NumberOfRealInlines > 0);
113 }
114
115 if (Verbose)
116 Ostream << "Inlined "
117 << (Node.second->Imported ? "imported " : "not imported ")
118 << "function [" << Node.first << "]"
119 << ": #inlines = " << Node.second->NumberOfInlines
120 << ", #inlines_to_importing_module = "
121 << Node.second->NumberOfRealInlines << "\n";
122 }
123
124 auto InlinedFunctionsCount =
125 InlinedImportedFunctionsCount + InlinedNotImportedFunctionsCount;
126 auto NotImportedFuncCount = AllFunctions - ImportedFunctions;
127 auto ImportedNotInlinedIntoModule =
128 ImportedFunctions - InlinedImportedFunctionsToImportingModuleCount;
129
130 Ostream << "-- Summary:\n"
131 << "All functions: " << AllFunctions
132 << ", imported functions: " << ImportedFunctions << "\n"
133 << getStatString("inlined functions", InlinedFunctionsCount,
134 AllFunctions, "all functions")
135 << getStatString("imported functions inlined anywhere",
136 InlinedImportedFunctionsCount, ImportedFunctions,
137 "imported functions")
138 << getStatString("imported functions inlined into importing module",
139 InlinedImportedFunctionsToImportingModuleCount,
140 ImportedFunctions, "imported functions",
141 /*LineEnd=*/false)
142 << getStatString(", remaining", ImportedNotInlinedIntoModule,
143 ImportedFunctions, "imported functions")
144 << getStatString("non-imported functions inlined anywhere",
145 InlinedNotImportedFunctionsCount,
146 NotImportedFuncCount, "non-imported functions")
147 << getStatString(
148 "non-imported functions inlined into importing module",
149 InlinedNotImportedFunctionsToImportingModuleCount,
150 NotImportedFuncCount, "non-imported functions");
151 Ostream.flush();
152 dbgs() << Out;
153}
154
155void ImportedFunctionsInliningStatistics::calculateRealInlines() {
156 // Removing duplicated Callers.
157 std::sort(NonImportedCallers.begin(), NonImportedCallers.end());
158 NonImportedCallers.erase(
159 std::unique(NonImportedCallers.begin(), NonImportedCallers.end()),
160 NonImportedCallers.end());
161
162 for (const auto &Name : NonImportedCallers) {
163 auto &Node = *NodesMap[Name];
164 if (!Node.Visited)
165 dfs(Node);
166 }
167}
168
169void ImportedFunctionsInliningStatistics::dfs(InlineGraphNode &GraphNode) {
170 assert(!GraphNode.Visited);
171 GraphNode.Visited = true;
172 for (auto *const InlinedFunctionNode : GraphNode.InlinedCallees) {
173 InlinedFunctionNode->NumberOfRealInlines++;
174 if (!InlinedFunctionNode->Visited)
175 dfs(*InlinedFunctionNode);
176 }
177}
178
179ImportedFunctionsInliningStatistics::SortedNodesTy
180ImportedFunctionsInliningStatistics::getSortedNodes() {
181 SortedNodesTy SortedNodes;
182 SortedNodes.reserve(NodesMap.size());
183
184 for (auto &&Node : NodesMap)
185 SortedNodes.emplace_back(Node.first, std::move(Node.second));
186
187 NodesMap.clear(); // We don't want to leave nullptrs.
188
189 std::sort(
190 SortedNodes.begin(), SortedNodes.end(),
191 [&](const SortedNodesTy::value_type &Lhs,
192 const SortedNodesTy::value_type &Rhs) {
193 if (Lhs.second->NumberOfInlines != Rhs.second->NumberOfInlines)
194 return Lhs.second->NumberOfInlines > Rhs.second->NumberOfInlines;
195 if (Lhs.second->NumberOfRealInlines != Rhs.second->NumberOfRealInlines)
196 return Lhs.second->NumberOfRealInlines >
197 Rhs.second->NumberOfRealInlines;
198 return Lhs.first < Rhs.first;
199 });
200 return SortedNodes;
201}