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